Blame view

controllers/CategoryController.php 7.19 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;
ba196ec2   Alexey Boroda   -Blog in process
13
  
593851ec   Alexey Boroda   First commit
14
15
16
      /**
       * BlogCategoryController implements the CRUD actions for BlogCategory model.
       */
ba196ec2   Alexey Boroda   -Blog in process
17
      class CategoryController extends Controller
593851ec   Alexey Boroda   First commit
18
19
20
21
      {
          /**
           * @inheritdoc
           */
ba196ec2   Alexey Boroda   -Blog in process
22
23
24
25
26
27
28
29
          public function getViewPath()
          {
              return '@artbox/weblog/views/blog-category';
          }
      
          /**
           * @inheritdoc
           */
593851ec   Alexey Boroda   First commit
30
31
32
33
34
35
36
37
38
39
40
          public function behaviors()
          {
              return [
                  'verbs' => [
                      'class'   => VerbFilter::className(),
                      'actions' => [
                          'delete' => [ 'POST' ],
                      ],
                  ],
              ];
          }
ba196ec2   Alexey Boroda   -Blog in process
41
      
593851ec   Alexey Boroda   First commit
42
43
44
45
46
47
48
          /**
           * Lists all BlogCategory models.
           *
           * @return mixed
           */
          public function actionIndex()
          {
ba196ec2   Alexey Boroda   -Blog in process
49
              $searchModel = new CategorySearch();
593851ec   Alexey Boroda   First commit
50
              $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
ba196ec2   Alexey Boroda   -Blog in process
51
      
593851ec   Alexey Boroda   First commit
52
53
54
55
56
57
58
59
              return $this->render(
                  'index',
                  [
                      'searchModel'  => $searchModel,
                      'dataProvider' => $dataProvider,
                  ]
              );
          }
ba196ec2   Alexey Boroda   -Blog in process
60
      
593851ec   Alexey Boroda   First commit
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
          /**
           * 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
77
      
593851ec   Alexey Boroda   First commit
78
79
80
81
82
83
84
85
          /**
           * 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
86
              $model = new Category();
593851ec   Alexey Boroda   First commit
87
88
              $model->generateLangs();
              $parentCategories = ArrayHelper::map(
ee588281   Alexey Boroda   -Blog backend ready
89
90
91
92
93
94
95
96
                  Category::find()
                          ->joinWith('lang')
                          ->where(
                              [
                                  'parent_id' => 0,
                              ]
                          )
                          ->all(),
593851ec   Alexey Boroda   First commit
97
98
99
                  'id',
                  'lang.title'
              );
ba196ec2   Alexey Boroda   -Blog in process
100
      
ee588281   Alexey Boroda   -Blog backend ready
101
102
103
104
105
106
107
              if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                  return $this->redirect(
                      [
                          'view',
                          'id' => $model->id,
                      ]
                  );
593851ec   Alexey Boroda   First commit
108
109
110
111
112
113
114
115
116
              }
              return $this->render(
                  'create',
                  [
                      'model'            => $model,
                      'modelLangs'       => $model->modelLangs,
                      'parentCategories' => $parentCategories,
                  ]
              );
ba196ec2   Alexey Boroda   -Blog in process
117
      
593851ec   Alexey Boroda   First commit
118
          }
ba196ec2   Alexey Boroda   -Blog in process
119
      
593851ec   Alexey Boroda   First commit
120
121
122
123
124
125
126
127
128
129
130
131
132
          /**
           * 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                  Category::find()
                          ->joinWith('lang')
                          ->where(
                              [
                                  'parent_id' => 0,
                              ]
                          )
                          ->andWhere(
                              [
                                  '!=',
                                  Category::tableName() . '_id',
                                  $model->id,
                              ]
                          )
                          ->all(),
593851ec   Alexey Boroda   First commit
148
149
150
                  'id',
                  'lang.title'
              );
ee588281   Alexey Boroda   -Blog backend ready
151
152
153
154
155
156
157
158
              if ($model->loadWithLangs(\Yii::$app->request) && $model->saveWithLangs()) {
                  return $this->redirect(
                      [
                          'view',
                          'id' => $model->id,
                      ]
                  );
                  
593851ec   Alexey Boroda   First commit
159
160
161
162
163
164
165
166
167
              }
              return $this->render(
                  'update',
                  [
                      'model'            => $model,
                      'modelLangs'       => $model->modelLangs,
                      'parentCategories' => $parentCategories,
                  ]
              );
ba196ec2   Alexey Boroda   -Blog in process
168
      
593851ec   Alexey Boroda   First commit
169
          }
ba196ec2   Alexey Boroda   -Blog in process
170
      
593851ec   Alexey Boroda   First commit
171
172
173
174
175
176
177
178
179
180
181
182
          /**
           * 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
183
      
593851ec   Alexey Boroda   First commit
184
185
186
              return $this->redirect([ 'index' ]);
          }
      
ee588281   Alexey Boroda   -Blog backend ready
187
          public function actionList(string $q = null)
593851ec   Alexey Boroda   First commit
188
          {
ee588281   Alexey Boroda   -Blog backend ready
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
              \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
224
          }
ba196ec2   Alexey Boroda   -Blog in process
225
      
593851ec   Alexey Boroda   First commit
226
227
228
          /**
           * 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
229
           
593851ec   Alexey Boroda   First commit
230
           *
ee588281   Alexey Boroda   -Blog backend ready
231
  *@param integer $id
593851ec   Alexey Boroda   First commit
232
           *
ee588281   Alexey Boroda   -Blog backend ready
233
           * @return Category the loaded model
593851ec   Alexey Boroda   First commit
234
235
236
237
           * @throws NotFoundHttpException if the model cannot be found
           */
          protected function findModel($id)
          {
ee588281   Alexey Boroda   -Blog backend ready
238
              if (( $model = Category::findOne($id) ) !== null) {
593851ec   Alexey Boroda   First commit
239
240
241
242
243
244
                  return $model;
              } else {
                  throw new NotFoundHttpException('The requested page does not exist.');
              }
          }
      }