Blame view

common/modules/product/controllers/ManageController.php 10.6 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php

36d1807a   Yarik   Big commit.
2
3
4
      

      namespace common\modules\product\controllers;

      

e8ccb1b4   Yarik   Import beta
5
      use common\modules\language\models\Language;

36d1807a   Yarik   Big commit.
6
7
8
      use common\modules\product\models\Export;

      use common\modules\product\models\Import;

      use common\modules\product\models\ProductImage;

36d1807a   Yarik   Big commit.
9
10
11
      use Yii;

      use common\modules\product\models\Product;

      use common\modules\product\models\ProductSearch;

5c2eb7c8   Yarik   Big commit almost...
12
      use yii\db\ActiveQuery;

36d1807a   Yarik   Big commit.
13
14
15
      use yii\web\Controller;

      use yii\web\NotFoundHttpException;

      use yii\filters\VerbFilter;

5c2eb7c8   Yarik   Big commit almost...
16
      use yii\web\Response;

36d1807a   Yarik   Big commit.
17
18
      use yii\web\UploadedFile;

      

d8c1a2e0   Yarik   Big commit artbox
19
      /**

36d1807a   Yarik   Big commit.
20
       * ManageController implements the CRUD actions for Product model.

d8c1a2e0   Yarik   Big commit artbox
21
       */

36d1807a   Yarik   Big commit.
22
      class ManageController extends Controller

d8c1a2e0   Yarik   Big commit artbox
23
      {

36d1807a   Yarik   Big commit.
24
25
26
27
28
29
30
31
32
33
34
35
          

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'verbs' => [

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

                      'actions' => [

                          'delete' => [ 'POST' ],

                      ],

d8c1a2e0   Yarik   Big commit artbox
36
                  ],

36d1807a   Yarik   Big commit.
37
38
39
              ];

          }

          

36d1807a   Yarik   Big commit.
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
          /**

           * Lists all Product models.

           * @return mixed

           */

          public function actionIndex()

          {

              $searchModel = new ProductSearch();

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

              

              return $this->render('index', [

                  'searchModel'  => $searchModel,

                  'dataProvider' => $dataProvider,

              ]);

          }

          

          /**

           * Displays a single Product model.

           *

           * @param integer $id

           *

           * @return mixed

           */

          public function actionView($id)

          {

00731fef   Yarik   Before vitex test
64
65
66
67
68
69
70
              $model = $this->findModel($id);

              $categories = $model->getCategories()

                                  ->with('lang')

                                  ->all();

              $variants = $model->getVariants()

                                ->with('lang')

                                ->all();

4e55ce81   Yarik   Another one admin...
71
              $properties = $model->getProperties();

36d1807a   Yarik   Big commit.
72
              return $this->render('view', [

00731fef   Yarik   Before vitex test
73
74
75
                  'model'      => $this->findModel($id),

                  'categories' => $categories,

                  'variants'   => $variants,

4e55ce81   Yarik   Another one admin...
76
                  'properties' => $properties,

36d1807a   Yarik   Big commit.
77
78
79
80
81
82
83
84
85
86
87
              ]);

          }

          

          /**

           * Creates a new Product model.

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

           * @return mixed

           */

          public function actionCreate()

          {

              $model = new Product();

c3c1539f   Yarik   Tax Option fix
88
              $model->detachBehavior('techSpec');

9f8be5c9   Yarik   Import browser be...
89
              $model->generateLangs();

36d1807a   Yarik   Big commit.
90
              if($model->load(Yii::$app->request->post())) {

9f8be5c9   Yarik   Import browser be...
91
                  $model->loadLangs(\Yii::$app->request);

72a992f5   Yarik   Import browser v1.0
92
                  if($model->save() && $model->transactionStatus) {

5c2eb7c8   Yarik   Big commit almost...
93
94
95
96
                      return $this->redirect([

                          'view',

                          'id' => $model->product_id,

                      ]);

d55d2fe0   Yarik   Multilanguage
97
                  }

d8c1a2e0   Yarik   Big commit artbox
98
              }

d8c1a2e0   Yarik   Big commit artbox
99
              return $this->render('create', [

5c2eb7c8   Yarik   Big commit almost...
100
101
                  'model'       => $model,

                  'model_langs' => $model->model_langs,

d8c1a2e0   Yarik   Big commit artbox
102
103
              ]);

          }

36d1807a   Yarik   Big commit.
104
105
106
107
108
109
110
111
112
113
114
115
          

          /**

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

72a992f5   Yarik   Import browser v1.0
116
              $model->generateLangs();

36d1807a   Yarik   Big commit.
117
              if($model->load(Yii::$app->request->post())) {

72a992f5   Yarik   Import browser v1.0
118
                  $model->loadLangs(\Yii::$app->request);

72a992f5   Yarik   Import browser v1.0
119
                  if($model->save() && $model->transactionStatus) {

5c2eb7c8   Yarik   Big commit almost...
120
121
122
123
                      return $this->redirect([

                          'view',

                          'id' => $model->product_id,

                      ]);

d8c1a2e0   Yarik   Big commit artbox
124
                  }

d8c1a2e0   Yarik   Big commit artbox
125
              }

5c2eb7c8   Yarik   Big commit almost...
126
127
128
              /**

               * @var ActiveQuery $groups

               */

d8c1a2e0   Yarik   Big commit artbox
129
              $groups = $model->getTaxGroupsByLevel(0);

d8c1a2e0   Yarik   Big commit artbox
130
              return $this->render('update', [

5c2eb7c8   Yarik   Big commit almost...
131
132
133
                  'model'       => $model,

                  'model_langs' => $model->model_langs,

                  'groups'      => $groups,

d8c1a2e0   Yarik   Big commit artbox
134
135
              ]);

          }

36d1807a   Yarik   Big commit.
136
137
138
139
140
141
142
143
144
145
146
147
148
          

          /**

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

36d1807a   Yarik   Big commit.
149
              return $this->redirect([ 'index' ]);

d8c1a2e0   Yarik   Big commit artbox
150
          }

36d1807a   Yarik   Big commit.
151
152
153
154
155
156
157
          

          public function actionDelimg($id)

          {

              $image = ProductImage::findOne($id);

              

              if($image) {

                  $image->delete();

d8c1a2e0   Yarik   Big commit artbox
158
              }

36d1807a   Yarik   Big commit.
159
160
161
              

              print '1';

              exit;

d8c1a2e0   Yarik   Big commit artbox
162
          }

d55d2fe0   Yarik   Multilanguage
163
          

36d1807a   Yarik   Big commit.
164
165
166
167
168
169
170
171
172
          public function actionIs_top($id)

          {

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

              

              $model->is_top = intval(empty( $model->is_top ));

              

              $model->save(false, [ 'is_top' ]);

              

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

d8c1a2e0   Yarik   Big commit artbox
173
          }

36d1807a   Yarik   Big commit.
174
175
176
177
178
179
180
181
182
183
          

          public function actionIs_new($id)

          {

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

              

              $model->is_new = intval(empty( $model->is_new ));

              

              $model->save(false, [ 'is_new' ]);

              

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

d8c1a2e0   Yarik   Big commit artbox
184
          }

36d1807a   Yarik   Big commit.
185
186
187
188
189
190
191
192
193
194
          

          public function actionAkciya($id)

          {

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

              

              $model->akciya = intval(empty( $model->akciya ));

              

              $model->save(false, [ 'akciya' ]);

              

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

d8c1a2e0   Yarik   Big commit artbox
195
          }

36d1807a   Yarik   Big commit.
196
197
198
199
200
          

          public function actionImport()

          {

              $model = new Import();

              

e8ccb1b4   Yarik   Import beta
201
202
203
204
205
206
207
208
209
210
211
              $languages = Language::find()

                                   ->select([

                                       'name',

                                       'language_id',

                                   ])

                                   ->where([ 'status' => 1 ])

                                   ->orderBy([ 'default' => SORT_DESC ])

                                   ->asArray()

                                   ->indexBy('language_id')

                                   ->column();

              

36d1807a   Yarik   Big commit.
212
              if($model->load(Yii::$app->request->post())) {

e8ccb1b4   Yarik   Import beta
213
                  \Yii::$app->session->set('export_lang', $model->lang);

36d1807a   Yarik   Big commit.
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
                  $file = UploadedFile::getInstances($model, 'file');

                  $method = 'go' . ucfirst($model->type);

                  $target = Yii::getAlias('@uploadDir') . '/' . Yii::getAlias('@uploadFile' . ucfirst($model->type));

                  if(empty( $file )) {

                      $model->errors[] = 'File not upload';

                  } elseif($method == 'goPrices' && $file[ 0 ]->name != 'file_1.csv') {

                      $model->errors[] = 'File need "file_1.csv"';

                  } elseif($method == 'goProducts' && $file[ 0 ]->name == 'file_1.csv') {

                      $model->errors[] = 'File can not "file_1.csv"';

                  } elseif($model->validate() && $file[ 0 ]->saveAs($target)) {

                      // PROCESS PAGE

                      return $this->render('import-process', [

                          'model'  => $model,

                          'method' => $model->type,

                          'target' => $target,

                      ]);

36d1807a   Yarik   Big commit.
230
231
232
233
234
235
                  } else {

                      $model->errors[] = 'File can not be upload or other error';

                  }

              }

              

              return $this->render('import', [

e8ccb1b4   Yarik   Import beta
236
237
                  'model'     => $model,

                  'languages' => $languages,

36d1807a   Yarik   Big commit.
238
239
240
241
242
243
244
245
246
247
              ]);

          }

          

          public function actionProducts()

          {

              $from = Yii::$app->request->get('from', 0);

              

              $model = new Import();

              

              if(Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
248
                  Yii::$app->response->format = Response::FORMAT_JSON;

7d10d812   Yarik   Import browser be...
249
                  return $model->goProducts($from, 1);

5c2eb7c8   Yarik   Big commit almost...
250
251
              } else {

                  throw new \HttpRequestException('Must be AJAX');

36d1807a   Yarik   Big commit.
252
253
254
255
256
257
258
259
260
261
              }

          }

          

          public function actionPrices()

          {

              $from = Yii::$app->request->get('from', 0);

              

              $model = new Import();

              

              if(Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
262
                  Yii::$app->response->format = Response::FORMAT_JSON;

36d1807a   Yarik   Big commit.
263
                  return $model->goPrices($from, 10);

5c2eb7c8   Yarik   Big commit almost...
264
265
              } else {

                  throw new \HttpRequestException('Must be AJAX');

36d1807a   Yarik   Big commit.
266
267
268
              }

          }

          

96410438   Yarik   Project admin com...
269
270
271
272
273
          public function actionExportProcess($from, $filename)

          {

              

              $model = new Export();

              if(Yii::$app->request->isAjax) {

5c2eb7c8   Yarik   Big commit almost...
274
                  Yii::$app->response->format = Response::FORMAT_JSON;

e8ccb1b4   Yarik   Import beta
275
                  return $model->process($filename, $from);

5c2eb7c8   Yarik   Big commit almost...
276
277
              } else {

                  throw new \HttpRequestException('Must be AJAX');

96410438   Yarik   Project admin com...
278
279
280
              }

          }

          

36d1807a   Yarik   Big commit.
281
282
283
          public function actionExport()

          {

              $model = new Export();

96410438   Yarik   Project admin com...
284
285
286
287
              

              if($model->load(Yii::$app->request->post())) {

                  \Yii::$app->session->set('export_lang', $model->lang);

                  return $this->render('export-process', [

e8ccb1b4   Yarik   Import beta
288
289
                      'model'  => $model,

                      'method' => 'export',

96410438   Yarik   Project admin com...
290
                  ]);

36d1807a   Yarik   Big commit.
291
              }

96410438   Yarik   Project admin com...
292
293
294
295
              

              return $this->render('export', [

                  'model' => $model,

              ]);

36d1807a   Yarik   Big commit.
296
297
298
299
300
301
302
303
304
305
306
307
308
          }

          

          /**

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

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

           *

           * @param integer $id

           *

           * @return Product the loaded model

           * @throws NotFoundHttpException if the model cannot be found

           */

          protected function findModel($id)

          {

00731fef   Yarik   Before vitex test
309
310
311
312
313
              if(( $model = Product::find()

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

                                   ->with('lang')

                                   ->one() ) !== NULL

              ) {

36d1807a   Yarik   Big commit.
314
315
316
317
                  return $model;

              } else {

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

              }

d8c1a2e0   Yarik   Big commit artbox
318
319
          }

      }