diff --git a/frontend/controllers/BookController.php b/frontend/controllers/BookController.php
index 64296ba..6d52755 100644
--- a/frontend/controllers/BookController.php
+++ b/frontend/controllers/BookController.php
@@ -9,9 +9,10 @@
namespace frontend\controllers;
use common\models\Book;
+ use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\UploadedFile;
-
+
class BookController extends Controller
{
/**
@@ -22,13 +23,14 @@
if ($action->id == 'add' or $action->id == 'edit') {
$this->enableCsrfValidation = false;
}
-
+
return parent::beforeAction($action);
}
-
- public function actionAdd(){
+
+ public function actionAdd()
+ {
/**
- * @var \common\models\Author $user;
+ * @var \common\models\Author $user ;
*/
$user = \Yii::$app->user->identity;
if (\Yii::$app->user->isGuest) {
@@ -36,41 +38,69 @@
}
$model = new Book();
if (\Yii::$app->request->isPost) {
-
+
if ($model->load(\Yii::$app->request->post(), '') and $model->validate()) {
$model->author_id = $user->id;
$model->status = $model::STATUS_MODERATION;
- return($model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')));
+ return ( $model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')) );
} else {
return false;
}
-
+
}
- return $this->render('add', [
- 'book' => array()
- ]);
+ return $this->render(
+ 'add',
+ [
+ 'book' => [],
+ ]
+ );
}
- public function actionEdit($id){
+ public function actionEdit($id)
+ {
$model = Book::findOne($id);
- if ($model->author_id !== \Yii::$app->user->getId()){
- return $this->redirect(['site/index']);
+ if ($model->author_id !== \Yii::$app->user->getId()) {
+ return $this->redirect([ 'site/index' ]);
}
if (\Yii::$app->request->isPost) {
-
+
if ($model->load(\Yii::$app->request->post(), '') and $model->validate()) {
- return($model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')));
+ return ( $model->save() && $model->saveImage(UploadedFile::getInstanceByName('file')) );
} else {
return false;
}
-
+
}
- return $this->render('add', [
- 'book' => $model->attributes
- ]);
+ return $this->render(
+ 'add',
+ [
+ 'book' => $model->attributes,
+ ]
+ );
}
- public function actionIndex(){
+ public function actionIndex()
+ {
+ $dataProvider = new ActiveDataProvider(
+ [
+ 'query' => Book::find()
+ ->with('author')
+ ->where([ 'status' => Book::STATUS_ACTIVE ]),
+ 'pagination' => [
+ 'pageSize' => 10,
+ ],
+ ]
+ );
+ return $this->render(
+ 'index',
+ [
+ 'dataProvider' => $dataProvider,
+ ]
+ );
+ }
+ public function actionView($id){
+ $model = Book::find()->with('author') ->where(['id' => $id])->one();
+ return $this->render('view', ['model' => $model]);
}
}
\ No newline at end of file
diff --git a/frontend/views/book/_book.php b/frontend/views/book/_book.php
new file mode 100644
index 0000000..1f9d1e6
--- /dev/null
+++ b/frontend/views/book/_book.php
@@ -0,0 +1,32 @@
+
+
-
-
-

-
-
-
=$book->title?>
-
Автор: =$book->author->name?> =$book->author->secondname?>
-
- =$book->preview?>
-
-
-
-
-
-
110000 грн. необхідно
-
37620 грн. зібрано
-
-
-
-
-
-
-
+ =$this->render('@frontend/views/book/_book', [
+ 'model' => $book
+ ])?>
diff --git a/frontend/web/js/book.js b/frontend/web/js/book.js
index 4618c2f..8fca295 100644
--- a/frontend/web/js/book.js
+++ b/frontend/web/js/book.js
@@ -38,11 +38,15 @@
.replace(/\r/g, "\\\\r")
.replace(/\t/g, "\\\\t");
data = JSON.parse(st);
- title = data.title;
- description = data.description;
- preview = data.preview;
- image = data.image;
- id = data.id;
+ console.log(data);
+ if (data.length > 0){
+ title = data.title;
+ description = data.description;
+ preview = data.preview;
+ image = data.image;
+ id = data.id;
+ }
+
}
titleSelector.addEventListener('blur', function(e) {
e.preventDefault();
@@ -156,7 +160,7 @@
function sendData() {
var xhr = new XMLHttpRequest();
var url = '/book/add';
- if (id !== 0){
+ if (id != 0){
url = '/book/edit?id='+id;
}
xhr.open('POST', url, true);
diff --git a/frontend/widgets/FrontendPager.php b/frontend/widgets/FrontendPager.php
new file mode 100755
index 0000000..41d96a5
--- /dev/null
+++ b/frontend/widgets/FrontendPager.php
@@ -0,0 +1,175 @@
+pagination->getPageCount();
+ if ($pageCount < 2 && $this->hideOnSinglePage) {
+ return '';
+ }
+
+ $buttons = [];
+ $currentPage = $this->pagination->getPage();
+
+ // first page
+// $firstPageLabel = $this->firstPageLabel === true ? '1' : $this->firstPageLabel;
+// if ($firstPageLabel !== false) {
+// $buttons[] = $this->renderPageButton($firstPageLabel, 0, $this->firstPageCssClass, $currentPage <= 0, false);
+// }
+ // prev page
+ if ($this->prevPageLabel !== false) {
+ if (( $page = $currentPage - 1 ) < 0) {
+ $page = 0;
+ }
+ $buttons[] = $this->renderPageButton(
+ $this->prevPageLabel,
+ $page,
+ $this->prevPageCssClass,
+ $currentPage <= 0,
+ false
+ );
+ }
+ $firstPageLabel = '1';
+ list($beginPage, $endPage) = $this->getPageRange();
+ if ($beginPage !== 0) {
+ $buttons[] = $this->renderPageButton(
+ $firstPageLabel,
+ 0,
+ $this->firstPageCssClass,
+ $currentPage <= 0,
+ false
+ );
+ }
+ // internal pages
+
+ for ($i = $beginPage; $i <= $endPage; ++$i) {
+ $buttons[] = $this->renderPageButton(
+ $i + 1,
+ $i,
+ null,
+ $this->disableCurrentPageButton && $i == $currentPage,
+ $i == $currentPage
+ );
+ }
+ // last page
+ $lastPageLabel = $this->lastPageLabel === true ? $pageCount : $this->lastPageLabel;
+ if ($lastPageLabel !== false and $endPage !== $pageCount - 1) {
+ if ($endPage + 1 < $pageCount - 1 and $this->dots) {
+ $buttons[] = $this->renderPageButton('...', $endPage + 1, $this->dotsClass, true, false);
+ }
+ if ($lastPageLabel == 'last_number') {
+ $lastPageLabel = $pageCount;
+ }
+ $buttons[] = $this->renderPageButton(
+ $lastPageLabel,
+ $pageCount - 1,
+ $this->lastPageCssClass,
+ $currentPage >= $pageCount - 1,
+ false
+ );
+ }
+ // next page
+ if ($this->nextPageLabel !== false) {
+ if (( $page = $currentPage + 1 ) >= $pageCount - 1) {
+ $page = $pageCount - 1;
+ }
+ $buttons[] = $this->renderPageButton(
+ $this->nextPageLabel,
+ $page,
+ $this->nextPageCssClass,
+ $currentPage >= $pageCount - 1,
+ false
+ );
+ }
+
+ return Html::tag('ul', implode("\n", $buttons), $this->options);
+ }
+
+ protected function renderPageButton($label, $page, $class, $disabled, $active)
+ {
+ $options = [ 'class' => empty($class) ? $this->pageCssClass : $class ];
+ if ($active) {
+ Html::addCssClass($options, $this->activePageCssClass);
+ }
+ if ($disabled) {
+ Html::addCssClass($options, $this->disabledPageCssClass);
+ $tag = ArrayHelper::remove($this->disabledListItemSubTagOptions, 'tag', 'span');
+
+ return Html::tag('li', Html::tag($tag, $label, $this->disabledListItemSubTagOptions), $options);
+ }
+ $linkOptions = $this->linkOptions;
+ $linkOptions[ 'data-page' ] = $page;
+ if ($page == 0 and !$active) {
+
+ $link = trim(
+ Url::current(
+ [
+ 'page' => null,
+ 'per-page' => null,
+ ]
+ ),
+ '?'
+ );
+ /**
+ * @var \artbox\catalog\models\Filter $filter
+ */
+ $filter = \Yii::$app->get('filter')->filterObj;
+ if (key_exists($link, $filter->getReplacedFilters())) {
+ $link = $filter->getReplacedFilters()[ $link ];
+ }
+ return Html::tag(
+ 'li',
+ Html::a($label, $link, $linkOptions),
+ $options
+ );
+ }
+
+
+ if ($active) {
+ return Html::tag('li', Html::a($label, null, $linkOptions), $options);
+ } else {
+ $link = $this->pagination->createUrl($page);
+ $query = parse_url($link);
+ parse_str($query[ 'query' ], $params);
+ unset($query[ 'query' ]);
+ ksort($params);
+ $link = $query[ 'path' ] . '?' . http_build_query($params);
+ return Html::tag('li', Html::a($label, $link, $linkOptions), $options);
+ }
+
+ }
+
+ }
\ No newline at end of file
--
libgit2 0.21.4