Blame view

controllers/BrandController.php 6.3 KB
b7e90569   Yarik   Namespaces
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
  <?php

      

      namespace artweb\artbox\ecommerce\controllers;

      

      use Yii;

      use artweb\artbox\ecommerce\models\Brand;

      use artweb\artbox\ecommerce\models\BrandSearch;

      use yii\web\Controller;

      use yii\web\NotFoundHttpException;

      use yii\filters\VerbFilter;

      use yii\filters\AccessControl;

      

      /**

       * BrandController implements the CRUD actions for Brand model.

       */

      class BrandController extends Controller

      {

          

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'access' => [

                      'class' => AccessControl::className(),

                      'rules' => [

                          [

                              'actions' => [

                                  'login',

                                  'error',

                              ],

                              'allow'   => true,

                          ],

                          [

                              'actions' => [

                                  'logout',

                                  'index',

                                  'create',

                                  'update',

                                  'view',

                                  'delete',

2ffeed3d   Yarik   Image delete func...
43
                                  'delete-image',

b7e90569   Yarik   Namespaces
44
45
46
47
48
49
50
51
52
                              ],

                              'allow'   => true,

                              'roles'   => [ '@' ],

                          ],

                      ],

                  ],

                  'verbs'  => [

                      'class'   => VerbFilter::className(),

                      'actions' => [

2ffeed3d   Yarik   Image delete func...
53
54
                          'logout'       => [ 'post' ],

                          'delete-image' => [ 'post' ],

b7e90569   Yarik   Namespaces
55
56
57
58
59
60
61
                      ],

                  ],

              ];

          }

          

          /**

           * Lists all Brand models.

2ffeed3d   Yarik   Image delete func...
62
           *

b7e90569   Yarik   Namespaces
63
64
65
66
67
68
69
           * @return mixed

           */

          public function actionIndex()

          {

              $searchModel = new BrandSearch();

              $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

              

2ffeed3d   Yarik   Image delete func...
70
71
72
73
74
75
76
              return $this->render(

                  'index',

                  [

                      'searchModel'  => $searchModel,

                      'dataProvider' => $dataProvider,

                  ]

              );

b7e90569   Yarik   Namespaces
77
78
79
80
81
82
83
84
85
86
87
          }

          

          /**

           * Displays a single Brand model.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionView($id)

          {

2ffeed3d   Yarik   Image delete func...
88
89
90
91
92
93
              return $this->render(

                  'view',

                  [

                      'model' => $this->findModel($id),

                  ]

              );

b7e90569   Yarik   Namespaces
94
95
96
97
98
          }

          

          /**

           * Creates a new Brand model.

           * If creation is successful, the browser will be redirected to the 'view' page.

2ffeed3d   Yarik   Image delete func...
99
           *

b7e90569   Yarik   Namespaces
100
101
102
103
104
105
           * @return mixed

           */

          public function actionCreate()

          {

              $model = new Brand();

              $model->generateLangs();

2ffeed3d   Yarik   Image delete func...
106
              if ($model->load(Yii::$app->request->post())) {

b7e90569   Yarik   Namespaces
107
                  $model->loadLangs(\Yii::$app->request);

2ffeed3d   Yarik   Image delete func...
108
109
110
111
112
113
114
                  if ($model->save() && $model->transactionStatus) {

                      return is_null(Yii::$app->request->post('create_and_new')) ? $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      ) : $this->redirect(array_merge([ 'create' ], Yii::$app->request->queryParams));

b7e90569   Yarik   Namespaces
115
116
                  }

              }

2ffeed3d   Yarik   Image delete func...
117
118
119
120
121
122
123
              return $this->render(

                  'create',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

                  ]

              );

b7e90569   Yarik   Namespaces
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
          }

          

          /**

           * Updates an existing Brand 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();

              

2ffeed3d   Yarik   Image delete func...
139
              if ($model->load(Yii::$app->request->post())) {

b7e90569   Yarik   Namespaces
140
                  $model->loadLangs(\Yii::$app->request);

2ffeed3d   Yarik   Image delete func...
141
142
143
144
145
146
147
                  if ($model->save() && $model->transactionStatus) {

                      return $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      );

b7e90569   Yarik   Namespaces
148
149
                  }

              }

2ffeed3d   Yarik   Image delete func...
150
151
152
153
154
155
156
              return $this->render(

                  'update',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

                  ]

              );

b7e90569   Yarik   Namespaces
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
          }

          

          /**

           * Deletes an existing Brand 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();

              

              return $this->redirect([ 'index' ]);

          }

          

2ffeed3d   Yarik   Image delete func...
175
176
177
178
179
180
181
182
          public function actionDeleteImage($id)

          {

              $model = $this->findModel($id);

              $model->image = null;

              $model->updateAttributes(['image']);

              return true;

          }

          

b7e90569   Yarik   Namespaces
183
184
185
186
187
188
189
190
191
192
193
          /**

           * Finds the Brand model based on its primary key value.

           * If the model is not found, a 404 HTTP exception will be thrown.

           *

           * @param integer $id

           *

           * @return Brand the loaded model

           * @throws NotFoundHttpException if the model cannot be found

           */

          protected function findModel($id)

          {

2ffeed3d   Yarik   Image delete func...
194
195
196
197
198
              if (( $model = Brand::find()

                                  ->with('lang')

                                  ->where([ 'id' => $id ])

                                  ->one() ) !== null

              ) {

b7e90569   Yarik   Namespaces
199
200
201
202
203
204
                  return $model;

              } else {

                  throw new NotFoundHttpException('The requested page does not exist.');

              }

          }

      }