Blame view

controllers/ManageController.php 16.1 KB
2f25da09   Yarik   first commit
1
2
3
4
  <?php

      

      namespace artweb\artbox\ecommerce\controllers;

      

2b315849   Alexey Boroda   -Product card (wi...
5
      use artweb\artbox\ecommerce\models\ProductVideo;

8a7e6ecf   Yarik   Namespaces
6
      use artweb\artbox\language\models\Language;

2f25da09   Yarik   first commit
7
8
9
10
11
12
13
      use artweb\artbox\ecommerce\models\Export;

      use artweb\artbox\ecommerce\models\Import;

      use artweb\artbox\ecommerce\models\ProductImage;

      use Yii;

      use artweb\artbox\ecommerce\models\Product;

      use artweb\artbox\ecommerce\models\ProductSearch;

      use yii\db\ActiveQuery;

55123523   Alexey Boroda   -Status switcher
14
      use yii\helpers\Html;

70753ad4   Alexey Boroda   -Product card (vi...
15
      use yii\helpers\VarDumper;

2f25da09   Yarik   first commit
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
      use yii\web\Controller;

      use yii\web\NotFoundHttpException;

      use yii\filters\VerbFilter;

      use yii\web\Response;

      use yii\web\UploadedFile;

      

      /**

       * ManageController implements the CRUD actions for Product model.

       */

      class ManageController extends Controller

      {

          

          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

                  'verbs' => [

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

                      'actions' => [

                          'delete' => [ 'POST' ],

                      ],

                  ],

              ];

          }

          

          /**

           * 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)

          {

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

              $categories = $model->getCategories()

                                  ->with('lang')

                                  ->all();

              $variants = $model->getVariants()

                                ->with('lang')

                                ->all();

              $properties = $model->getProperties();

              return $this->render(

                  'view',

                  [

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

                      'categories' => $categories,

                      'variants'   => $variants,

                      'properties' => $properties,

                  ]

              );

          }

          

          /**

           * 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();

              $model->generateLangs();

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

                  $model->loadLangs(\Yii::$app->request);

                  if ($model->save() && $model->transactionStatus) {

70753ad4   Alexey Boroda   -Product card (vi...
103
104
105
106
107
108
                      if (!empty( \Yii::$app->request->post('ProductVideo') ))

                      {

                          foreach (\Yii::$app->request->post('ProductVideo') as $video)

                          {

                              $modelVideo = new ProductVideo();

                              $modelVideo->url = $video['url'];

797936c7   Alexey Boroda   -Product video mo...
109
                              $modelVideo->title = $video['title'];

70753ad4   Alexey Boroda   -Product card (vi...
110
111
112
113
                              $model->link('videos', $modelVideo);

                          }

                      }

                      

2f25da09   Yarik   first commit
114
115
116
117
118
119
120
121
122
123
124
125
126
                      return $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      );

                  }

              }

              return $this->render(

                  'create',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

2ffeed3d   Yarik   Image delete func...
127
                      'videos' => !empty( $model->videos ) ? $model->videos : [ new ProductVideo() ],

2f25da09   Yarik   first commit
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
                  ]

              );

          }

          

          /**

           * 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);

              $model->generateLangs();

70753ad4   Alexey Boroda   -Product card (vi...
144
              

2f25da09   Yarik   first commit
145
146
              if ($model->load(Yii::$app->request->post())) {

                  $model->loadLangs(\Yii::$app->request);

70753ad4   Alexey Boroda   -Product card (vi...
147
                  $model->unlinkAll('videos', true);

2f25da09   Yarik   first commit
148
                  if ($model->save() && $model->transactionStatus) {

70753ad4   Alexey Boroda   -Product card (vi...
149
150
151
152
153
154
                      if (!empty( \Yii::$app->request->post('ProductVideo') ))

                      {

                          foreach (\Yii::$app->request->post('ProductVideo') as $video)

                          {

                              $modelVideo = new ProductVideo();

                              $modelVideo->url = $video['url'];

797936c7   Alexey Boroda   -Product video mo...
155
                              $modelVideo->title = $video['title'];

70753ad4   Alexey Boroda   -Product card (vi...
156
157
158
159
                              $model->link('videos', $modelVideo);

                          }

                      }

                      

2f25da09   Yarik   first commit
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
                      return $this->redirect(

                          [

                              'view',

                              'id' => $model->id,

                          ]

                      );

                  }

              }

              /**

               * @var ActiveQuery $groups

               */

              $groups = $model->getTaxGroupsByLevel(0);

              return $this->render(

                  'update',

                  [

                      'model'      => $model,

                      'modelLangs' => $model->modelLangs,

                      'groups'     => $groups,

2b315849   Alexey Boroda   -Product card (wi...
178
                      'videos'     => !empty( $model->videos ) ? $model->videos : [ new ProductVideo() ],

2f25da09   Yarik   first commit
179
180
181
182
183
184
185
186
187
188
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
                  ]

              );

          }

          

          /**

           * 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();

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

          }

          

          /**

           * Deletes an existing ProductImage model.

           *

           * @param int $id

           */

          public function actionDeleteImage($id)

          {

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

              

              if ($image) {

                  $image->delete();

              }

              

              print '1';

              exit;

          }

          

          /**

           * Toggle product top status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

          public function actionIsTop($id)

          {

55123523   Alexey Boroda   -Status switcher
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
              \Yii::$app->response->format = Response::FORMAT_JSON;

              $product = Product::findOne($id);

              $product->is_top = !$product->is_top;

              if ($product->save()) {

                  if ($product->is_top) {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-star',

                      ]);

                  } else {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-star-empty',

                      ]);

                  }

                  return [

                      'success' => true,

                      'tag' => $tag,

                      'message' => 'Статус ТОП успешно изменен',

                  ];

              } else {

                  return [];

              }

2f25da09   Yarik   first commit
245
246
247
248
249
250
251
252
253
254
255
          }

          

          /**

           * Toggle product new status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

          public function actionIsNew($id)

          {

55123523   Alexey Boroda   -Status switcher
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
              \Yii::$app->response->format = Response::FORMAT_JSON;

              $product = Product::findOne($id);

              $product->is_new = !$product->is_new;

              if ($product->save()) {

                  if ($product->is_new) {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-heart',

                      ]);

                  } else {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-heart-empty',

                      ]);

                  }

                  return [

                      'success' => true,

                      'tag' => $tag,

                      'message' => 'Статус НОВЫЙ успешно изменен',

                  ];

              } else {

                  return [];

              }

2f25da09   Yarik   first commit
277
278
279
280
281
282
283
284
285
286
287
          }

          

          /**

           * Toggle product discount status

           *

           * @param int $id Product ID

           *

           * @return \yii\web\Response

           */

          public function actionIsDiscount($id)

          {

55123523   Alexey Boroda   -Status switcher
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
              \Yii::$app->response->format = Response::FORMAT_JSON;

              $product = Product::findOne($id);

              $product->is_discount = !$product->is_discount;

              if ($product->save()) {

                  if ($product->is_discount) {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-tags',

                      ]);

                  } else {

                      $tag = Html::tag('span', '', [

                          'class' => 'glyphicon glyphicon-tag',

                      ]);

                  }

                  return [

                      'success' => true,

                      'tag' => $tag,

                      'message' => 'Статус АКЦИОННЫЙ успешно изменен',

                  ];

              } else {

                  return [];

              }

2f25da09   Yarik   first commit
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
          }

          

          /**

           * Perform product import

           *

           * @return string

           */

          public function actionImport()

          {

              $model = new Import();

              

              $languages = Language::find()

                                   ->select(

                                       [

                                           'name',

                                           'id',

                                       ]

                                   )

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

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

                                   ->asArray()

                                   ->indexBy('id')

                                   ->column();

              

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

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

                  $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,

                          ]

                      );

                  } else {

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

                  }

              }

              

              return $this->render(

                  'import',

                  [

                      'model'     => $model,

                      'languages' => $languages,

                  ]

              );

          }

          

          /**

           * Import products via AJAX

           *

           * @return array

           * @throws \HttpRequestException

           */

          public function actionProducts()

          {

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

              

              $model = new Import();

              

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

                  Yii::$app->response->format = Response::FORMAT_JSON;

5f2a765a   Administrator   full commit
382
                  return $model->goProducts($from, 10);

2f25da09   Yarik   first commit
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
              } else {

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

              }

          }

          

          /**

           * Import prices via AJAX

           *

           * @return array

           * @throws \HttpRequestException

           */

          public function actionPrices()

          {

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

              

              $model = new Import();

              

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

                  Yii::$app->response->format = Response::FORMAT_JSON;

                  return $model->goPrices($from, 10);

              } else {

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

              }

          }

          

          /**

           * Export proccess via AJAX

           *

           * @param int    $from

           * @param string $filename

           *

           * @return array

           * @throws \HttpRequestException

           */

          public function actionExportProcess($from, $filename)

          {

              

              $model = new Export();

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

                  Yii::$app->response->format = Response::FORMAT_JSON;

                  return $model->process($filename, $from);

              } else {

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

              }

          }

2b315849   Alexey Boroda   -Product card (wi...
428
          

2f25da09   Yarik   first commit
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
          /**

           * Perform export

           *

           * @return string

           */

          public function actionExport()

          {

              $model = new Export();

              

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

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

                  return $this->render(

                      'export-process',

                      [

                          'model'  => $model,

                          'method' => 'export',

                      ]

                  );

              }

              

              return $this->render(

                  'export',

                  [

                      'model' => $model,

                  ]

              );

          }

2ffeed3d   Yarik   Image delete func...
456
457
458
459
460
461
462
463
      

          public function actionDeleteSize($id)

          {

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

              $model->size_image = null;

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

              return true;

          }

2f25da09   Yarik   first commit
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
          

          /**

           * 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)

          {

              if (( $model = Product::find()

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

                                    ->with('lang')

2b315849   Alexey Boroda   -Product card (wi...
479
                                    ->one() ) !== NULL

2f25da09   Yarik   first commit
480
481
482
483
484
485
486
              ) {

                  return $model;

              } else {

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

              }

          }

      }