Blame view

controllers/CategoryController.php 7.78 KB
593851ec   Alexey Boroda   First commit
1
2
  <?php
      
65dd0219   Alexey Boroda   -Bar tabs
3
      namespace artbox\weblog\controllers;
ba196ec2   Alexey Boroda   -Blog in process
4
  
ee588281   Alexey Boroda   -Blog backend ready
5
      use artbox\weblog\models\Category;
ba196ec2   Alexey Boroda   -Blog in process
6
      use artbox\weblog\models\CategorySearch;
593851ec   Alexey Boroda   First commit
7
      use Yii;
593851ec   Alexey Boroda   First commit
8
9
10
11
      use yii\helpers\ArrayHelper;
      use yii\web\Controller;
      use yii\web\NotFoundHttpException;
      use yii\filters\VerbFilter;
ee588281   Alexey Boroda   -Blog backend ready
12
      use yii\web\Response;
c1101752   Anastasia   access control
13
      use yii\filters\AccessControl;
ba196ec2   Alexey Boroda   -Blog in process
14
  
593851ec   Alexey Boroda   First commit
15
16
17
      /**
       * BlogCategoryController implements the CRUD actions for BlogCategory model.
       */
ba196ec2   Alexey Boroda   -Blog in process
18
      class CategoryController extends Controller
593851ec   Alexey Boroda   First commit
19
20
21
22
      {
          /**
           * @inheritdoc
           */
ba196ec2   Alexey Boroda   -Blog in process
23
24
25
26
27
28
29
30
          public function getViewPath()
          {
              return '@artbox/weblog/views/blog-category';
          }
      
          /**
           * @inheritdoc
           */
593851ec   Alexey Boroda   First commit
31
32
33
34
35
36
37
38
39
          public function behaviors()
          {
              return [
                  'verbs' => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'delete' => [ 'POST' ],
                      ],
                  ],
c1101752   Anastasia   access control
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
                  'access' => [
                      'class' => AccessControl::className(),
                      'rules' => [
                          [
                              'actions' => [
                                  'login',
                                  'error',
                              ],
                              'allow'   => true,
                          ],
                          [
                              'allow' => true,
                              'roles' => [ '@' ],
                          ],
                      ],
                  ],
593851ec   Alexey Boroda   First commit
56
57
              ];
          }
ba196ec2   Alexey Boroda   -Blog in process
58
      
593851ec   Alexey Boroda   First commit
59
60
61
62
63
64
65
          /**
           * Lists all BlogCategory models.
           *
           * @return mixed
           */
          public function actionIndex()
          {
ba196ec2   Alexey Boroda   -Blog in process
66
              $searchModel = new CategorySearch();
593851ec   Alexey Boroda   First commit
67
              $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
ba196ec2   Alexey Boroda   -Blog in process
68
      
593851ec   Alexey Boroda   First commit
69
70
71
72
73
74
75
76
              return $this->render(
                  'index',
                  [
                      'searchModel'  => $searchModel,
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
ba196ec2   Alexey Boroda   -Blog in process
77
      
593851ec   Alexey Boroda   First commit
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
          /**
           * Displays a single BlogCategory model.
           *
           * @param integer $id
           *
           * @return mixed
           */
          public function actionView($id)
          {
              return $this->render(
                  'view',
                  [
                      'model' => $this->findModel($id),
                  ]
              );
          }
ba196ec2   Alexey Boroda   -Blog in process
94
      
593851ec   Alexey Boroda   First commit
95
96
97
98
99
100
101
102
          /**
           * Creates a new BlogCategory model.
           * If creation is successful, the browser will be redirected to the 'view' page.
           *
           * @return mixed
           */
          public function actionCreate()
          {
ee588281   Alexey Boroda   -Blog backend ready
103
              $model = new Category();
593851ec   Alexey Boroda   First commit
104
105
              $model->generateLangs();
              $parentCategories = ArrayHelper::map(
ee588281   Alexey Boroda   -Blog backend ready
106
107
108
109
110
111
112
113
                  Category::find()
                          ->joinWith('lang')
                          ->where(
                              [
                                  'parent_id' => 0,
                              ]
                          )
                          ->all(),
593851ec   Alexey Boroda   First commit
114
115
116
                  'id',
                  'lang.title'
              );
ba196ec2   Alexey Boroda   -Blog in process
117
      
ee588281   Alexey Boroda   -Blog backend ready
118
119
120
121
122
123
124
              if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                  return $this->redirect(
                      [
                          'view',
                          'id' => $model->id,
                      ]
                  );
593851ec   Alexey Boroda   First commit
125
126
127
128
129
130
131
132
133
              }
              return $this->render(
                  'create',
                  [
                      'model'            => $model,
                      'modelLangs'       => $model->modelLangs,
                      'parentCategories' => $parentCategories,
                  ]
              );
ba196ec2   Alexey Boroda   -Blog in process
134
      
593851ec   Alexey Boroda   First commit
135
          }
ba196ec2   Alexey Boroda   -Blog in process
136
      
593851ec   Alexey Boroda   First commit
137
138
139
140
141
142
143
144
145
146
147
148
149
          /**
           * Updates an existing BlogCategory model.
           * If update is successful, the browser will be redirected to the 'view' page.
           *
           * @param integer $id
           *
           * @return mixed
           */
          public function actionUpdate($id)
          {
              $model = $this->findModel($id);
              $model->generateLangs();
              $parentCategories = ArrayHelper::map(
ee588281   Alexey Boroda   -Blog backend ready
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                  Category::find()
                          ->joinWith('lang')
                          ->where(
                              [
                                  'parent_id' => 0,
                              ]
                          )
                          ->andWhere(
                              [
                                  '!=',
                                  Category::tableName() . '_id',
                                  $model->id,
                              ]
                          )
                          ->all(),
593851ec   Alexey Boroda   First commit
165
166
167
                  'id',
                  'lang.title'
              );
ee588281   Alexey Boroda   -Blog backend ready
168
169
170
171
172
173
174
175
              if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                  return $this->redirect(
                      [
                          'view',
                          'id' => $model->id,
                      ]
                  );
                  
593851ec   Alexey Boroda   First commit
176
177
178
179
180
181
182
183
184
              }
              return $this->render(
                  'update',
                  [
                      'model'            => $model,
                      'modelLangs'       => $model->modelLangs,
                      'parentCategories' => $parentCategories,
                  ]
              );
ba196ec2   Alexey Boroda   -Blog in process
185
      
593851ec   Alexey Boroda   First commit
186
          }
ba196ec2   Alexey Boroda   -Blog in process
187
      
593851ec   Alexey Boroda   First commit
188
189
190
191
192
193
194
195
196
197
198
199
          /**
           * Deletes an existing BlogCategory model.
           * If deletion is successful, the browser will be redirected to the 'index' page.
           *
           * @param integer $id
           *
           * @return mixed
           */
          public function actionDelete($id)
          {
              $this->findModel($id)
                   ->delete();
ee588281   Alexey Boroda   -Blog backend ready
200
      
593851ec   Alexey Boroda   First commit
201
202
203
              return $this->redirect([ 'index' ]);
          }
      
ee588281   Alexey Boroda   -Blog backend ready
204
          public function actionList(string $q = null)
593851ec   Alexey Boroda   First commit
205
          {
ee588281   Alexey Boroda   -Blog backend ready
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
              \Yii::$app->response->format = Response::FORMAT_JSON;
              $out = [
                  'results' => [
                      [
                          'id'   => '',
                          'text' => '',
                      ],
                  ],
              ];
              if (!is_null($q)) {
                  $categories = Category::find()
                                        ->joinWith('lang')
                                        ->select(
                                            [
                                                'id'   => 'blog_category.id',
                                                'text' => 'blog_category_lang.title',
                                            ]
                                        )
                                        ->andFilterWhere(
                                            [
                                                'like',
                                                'blog_category_lang.title',
                                                $q,
                                            ]
                                        )
                                        ->limit(20)
                                        ->asArray()
                                        ->all();
          
                  if (!empty($categories)) {
                      $out[ 'results' ] = $categories;
                  }
              }
      
              return $out;
593851ec   Alexey Boroda   First commit
241
          }
ba196ec2   Alexey Boroda   -Blog in process
242
      
593851ec   Alexey Boroda   First commit
243
244
245
          /**
           * Finds the BlogCategory model based on its primary key value.
           * If the model is not found, a 404 HTTP exception will be thrown.
ee588281   Alexey Boroda   -Blog backend ready
246
           
593851ec   Alexey Boroda   First commit
247
           *
ee588281   Alexey Boroda   -Blog backend ready
248
  *@param integer $id
593851ec   Alexey Boroda   First commit
249
           *
ee588281   Alexey Boroda   -Blog backend ready
250
           * @return Category the loaded model
593851ec   Alexey Boroda   First commit
251
252
253
254
           * @throws NotFoundHttpException if the model cannot be found
           */
          protected function findModel($id)
          {
ee588281   Alexey Boroda   -Blog backend ready
255
              if (( $model = Category::findOne($id) ) !== null) {
593851ec   Alexey Boroda   First commit
256
257
258
259
260
261
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
      }