Blame view

backend/controllers/BookController.php 3.18 KB
8f340aa7   Anastasia   - main page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  <?php
      /**
       * Created by PhpStorm.
       * User: stes
       * Date: 07.06.18
       * Time: 17:11
       */
      
      namespace backend\controllers;
      
      use artbox\core\admin\actions\Index;
      use common\models\Book;
      use yii\filters\AccessControl;
      use yii\filters\VerbFilter;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
  
      class BookController extends Controller
      {
          public function behaviors()
          {
              return [
                  'verbs'  => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'delete' => [ 'POST' ],
                      ],
                  ],
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
                      ],
                  ],
              ];
          }
          public function actions()
          {
              return [
                  'index'  => [
                      'class'            => Index::className(),
                      'columns'          => [
                          'title'      => [
                              'type' => Index::ACTION_COL,
                          ],
                          'author' => [
                              'type' => Index::RELATION_COL,
                              'columnConfig' => [
                                  'relationField' => 'secondname',
                              ],
                          ],
                          'created_at' => [
                              'type' => Index::DATETIME_COL,
                          ],
                          'status'       => [
                              'type' => Index::STRING_COL,
                          ],
                          'on_main'       => [
                              'type' => Index::STATUS_COL,
                          ],
                      ],
                      'model'            => Book::className(),
                      'hasLanguage'      => false,
                      'enableMassDelete' => true,
                      'modelPrimaryKey'  => 'id',
                      'create' => false,
                  ],
              ];
          }
      
      
      
          public function actionUpdate($id)
          {
              $model = $this->findModel($id);
          
              $model->on_main = true;
              $model->save(false, [ 'on_main' ]);
          
              if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                  return $this->redirect('index');
              } else {
                  return $this->render(
                      'update',
                      [
                          'model' => $model,
                      ]
                  );
              }
          }
      
          public function findModel($id)
          {
              $model = Book::find()
                             ->where([ 'id' => $id ])
                             ->one();
              if ($model !== null) {
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
      }