This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ProductController.php
778 lines (691 loc) · 31 KB
/
ProductController.php
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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
382
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
428
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php
namespace xalberteinsteinx\shop\backend\controllers;
use xalberteinsteinx\shop\common\components\user\models\UserGroup;
use bl\seo\entities\SeoData;
use Yii;
use yii\base\{
Exception, Model
};
use yii\helpers\{
ArrayHelper, Inflector, Url
};
use yii\filters\AccessControl;
use bl\multilang\entities\Language;
use xalberteinsteinx\shop\backend\components\events\ProductEvent;
use yii\web\{
Controller, ForbiddenHttpException, NotFoundHttpException, UploadedFile
};
use xalberteinsteinx\shop\backend\components\form\{
ProductImageForm, ProductVideoForm
};
use xalberteinsteinx\shop\common\entities\{
Product, ProductImage, ProductImageTranslation, Price,
ProductPrice, SearchProduct, ProductTranslation, ProductVideo
};
use yii2tech\ar\position\PositionBehavior;
/**
* @author Albert Gainutdinov <xalbert.einsteinx@gmail.com>
*/
class ProductController extends Controller
{
/**
* Event is triggered before creating new product.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_BEFORE_CREATE_PRODUCT = 'beforeCreateProduct';
/**
* Event is triggered after creating new product.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_AFTER_CREATE_PRODUCT = 'afterCreateProduct';
/**
* Event is triggered after editing product translation.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_BEFORE_EDIT_PRODUCT = 'beforeEditProduct';
/**
* Event is triggered before editing product translation.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_AFTER_EDIT_PRODUCT = 'afterEditProduct';
/**
* Event is triggered before deleting product.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_BEFORE_DELETE_PRODUCT = 'beforeDeleteProduct';
/**
* Event is triggered after deleting product.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_AFTER_DELETE_PRODUCT = 'afterDeleteProduct';
/**
* Event is triggered after moderator accepting partner's product.
* Triggered with xalberteinsteinx\shop\backend\events\ProductEvent.
*/
const EVENT_AFTER_ACCEPT_PRODUCT = 'afterAcceptProduct';
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index'],
'roles' => ['viewProductList'],
'allow' => true,
],
[
'actions' => [
'save',
'add-image', 'delete-image', 'edit-image',
'add-video', 'delete-video',
'image-up', 'image-down',
'up', 'down', 'generate-seo-url',
],
'roles' => ['createProduct', 'createProductWithoutModeration',
'updateProduct', 'updateOwnProduct'],
'allow' => true,
],
[
'actions' => ['delete'],
'roles' => ['deleteProduct', 'deleteOwnProduct'],
'allow' => true,
],
[
'actions' => ['change-product-status'],
'roles' => ['moderateProductCreation'],
'allow' => true,
]
],
],
];
}
/**
* Displays products list.
*
* DataProvider sends created by user product models for users which have 'viewProductList' permission
* and for users which have 'viewCompleteProductList' permission it sends all product models.
*
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionIndex()
{
$searchModel = new SearchProduct();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$notModeratedProductsCount = Product::find()->where(['status' => Product::STATUS_ON_MODERATION])->count();
return $this->render('index', [
'notModeratedProductsCount' => $notModeratedProductsCount,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'languages' => Language::findAll(['active' => true])
]);
}
/**
* Creates or edits product model, adds basic info.
*
* Users which have 'updateOwnProduct' permission can edit only Product models that have been created by their.
* Users which have 'updateProduct' permission can create and editing all Product models.
* Users which have 'createProduct' permission can create Product models with status column equal to constant STATUS_ON_MODERATION.
* Users which have 'createProductWithoutModeration' permission can create Product models with status column equal to constant STATUS_SUCCESS.
*
* @param integer|NULL $id
* @param integer|NULL $languageId
* @return string|\yii\web\Response
* @throws Exception
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
*/
public function actionSave(int $id = null, int $languageId = null)
{
$selectedLanguage = (!empty($languageId)) ? Language::findOne($languageId) : Language::getCurrent();
//Getting or creating Product and ProductTranslation models
if (!empty($id)) {
$product = Product::findOne($id);
if (!empty($product)) {
if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) {
$productTranslation = ProductTranslation::find()->where([
'product_id' => $id,
'language_id' => $languageId
])->one() ?? new ProductTranslation();
} else throw new ForbiddenHttpException();
}
else throw new NotFoundHttpException();
} else {
if (\Yii::$app->user->can('createProduct')) {
$this->trigger(self::EVENT_BEFORE_CREATE_PRODUCT);
$product = new Product();
$productTranslation = new ProductTranslation();
} else throw new ForbiddenHttpException();
}
//Creates array of base prices
$prices = [];
$userGroups = UserGroup::find()->all();
foreach ($userGroups as $userGroup) {
$price = Price::find()->joinWith('productPrice')
->where(['product_id' => $product->id, 'user_group_id' => $userGroup->id])->one() ?? new Price();
$prices[$userGroup->id] = $price;
}
if (Yii::$app->request->isPost) {
$this->trigger(($product->isNewRecord) ? self::EVENT_BEFORE_CREATE_PRODUCT : self::EVENT_BEFORE_EDIT_PRODUCT);
$post = \Yii::$app->request->post();
if ($product->load($post)) {
$product->category_id = (!empty($product->category_id)) ? $product->category_id : NULL;
if ($product->isNewRecord) {
$product->owner = Yii::$app->user->id;
$product->status = (\Yii::$app->user->can('createProductWithoutModeration')) ?
Product::STATUS_SUCCESS : Product::STATUS_ON_MODERATION;
if ($product->validate()) {
$product->save();
}
else throw new Exception(\Yii::t('shop', 'An error occurred during the creation of the product'));
}
else {
if (!$product->validate()) throw new Exception(
\Yii::t('shop', 'An error occurred during the creation of the product'));
}
if ($productTranslation->load($post)) {
//Sets SEO-Url
if (empty($productTranslation->seoUrl)) {
$productTranslation->seoUrl = Inflector::slug($productTranslation->title);
}
$product->save();
$productTranslation->product_id = $product->id;
$productTranslation->language_id = $selectedLanguage->id;
if ($productTranslation->validate()) {
$productTranslation->save();
//Loads prices
if (Model::loadMultiple($prices, Yii::$app->request->post()) && Model::validateMultiple($prices)) {
foreach ($prices as $key => $price) {
$price->save(false);
$productPrice = ProductPrice::find()
->where(['product_id' => $product->id, 'user_group_id' => $key])
->one();
if (empty($productPrice)) {
$productPrice = new ProductPrice();
$productPrice->product_id = $product->id;
$productPrice->price_id = $price->id;
$productPrice->user_group_id = $key;
if ($productPrice->validate()) $productPrice->save();
}
}
}
$eventName = $productTranslation->isNewRecord ? self::EVENT_AFTER_CREATE_PRODUCT :
self::EVENT_AFTER_EDIT_PRODUCT;
$this->trigger($eventName, new ProductEvent([
'id' => $product->id
]));
return $this->redirect(Url::to(['save', 'id' => $product->id, 'languageId' => $selectedLanguage->id]));
}
}
}
}
if (Yii::$app->request->isPjax) {
return $this->renderPartial('add-basic', [
'prices' => $prices,
'selectedLanguageId' => $selectedLanguage->id,
'product' => $product,
'productTranslation' => $productTranslation,
]);
} else {
return $this->render('save', [
'product' => $product,
'selectedLanguage' => $selectedLanguage,
'viewName' => 'add-basic',
'params' => [
'prices' => $prices,
'selectedLanguageId' => $selectedLanguage->id,
'product' => $product,
'productTranslation' => $productTranslation,
]
]);
}
}
/**
* Users which have 'updateOwnProduct' permission can add image only for Product models that have been created by their.
* Users which have 'updateProduct' permission can add image for all Product models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionAddImage($id, $languageId)
{
if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($id)->owner])) {
$image_form = new ProductImageForm();
$image = new ProductImage();
$imageTranslation = new ProductImageTranslation();
if (Yii::$app->request->isPost) {
$image_form->load(Yii::$app->request->post());
$image_form->image = UploadedFile::getInstance($image_form, 'image');
if (!empty($image_form->image)) {
if ($uploadedImageName = $image_form->upload()) {
$image->file_name = $uploadedImageName;
$imageTranslation->alt = $image_form->alt2;
$image->product_id = $id;
if ($image->validate()) {
$image->save();
$imageTranslation->image_id = $image->id;
$imageTranslation->language_id = $languageId;
if ($imageTranslation->validate()) {
$imageTranslation->save();
}
}
}
}
if (!empty($image_form->link)) {
$image_name = $image_form->copy($image_form->link);
$image->file_name = $image_name;
$imageTranslation->alt = $image_form->alt1;
$image->product_id = $id;
if ($image->validate()) {
$image->save();
$imageTranslation->image_id = $image->id;
$imageTranslation->language_id = $languageId;
if ($imageTranslation->validate()) {
$imageTranslation->save();
}
}
}
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $id
]));
}
if (Yii::$app->request->isPjax) {
return $this->renderPartial('add-image', [
'selectedLanguage' => Language::findOne($languageId),
'productId' => $id,
'image_form' => new ProductImageForm(),
'images' => ProductImage::find()->where(['product_id' => $id])->orderBy('position')->all(),
]);
}
return $this->render('save', [
'product' => Product::findOne($id),
'selectedLanguage' => Language::findOne($languageId),
'viewName' => 'add-image',
'params' => [
'selectedLanguage' => Language::findOne($languageId),
'productId' => $id,
'image_form' => new ProductImageForm(),
'images' => ProductImage::find()->where(['product_id' => $id])->orderBy('position')->all(),
]
]);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* @param $id
* @param $languageId
* @return string|\yii\web\Response
*/
public function actionEditImage($id, $languageId)
{
$image = ProductImage::findOne($id);
$imageTranslation = ProductImageTranslation::find()->where([
'image_id' => $id,
'language_id' => $languageId
])->one();
if (empty($imageTranslation)) {
$imageTranslation = new ProductImageTranslation();
}
if (Yii::$app->request->isPost) {
$imageTranslation->load(Yii::$app->request->post());
$imageTranslation->image_id = $id;
$imageTranslation->language_id = $languageId;
if ($imageTranslation->validate()) {
$imageTranslation->save();
return $this->redirect(Url::to(['add-image',
'id' => $image->product_id,
'languageId' => $languageId
]));
} else die(var_dump($imageTranslation->errors));
}
if (Yii::$app->request->isPjax) {
return $this->renderPartial('edit-image', [
'selectedLanguage' => Language::findOne($languageId),
'imageTranslation' => $imageTranslation,
'image' => $image
]);
}
return $this->render('save', [
'product' => Product::findOne($image->product_id),
'selectedLanguage' => Language::findOne($languageId),
'viewName' => 'edit-image',
'params' => [
'selectedLanguage' => Language::findOne($languageId),
'imageTranslation' => $imageTranslation,
'image' => $image
]
]);
}
/**
* Users which have 'updateOwnProduct' permission can delete image only from Product models that have been created by their.
* Users which have 'updateProduct' permission can delete image from all Product models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
* @throws Exception
*/
public function actionDeleteImage($id, $languageId)
{
if (!empty($id)) {
$image = ProductImage::find()->where(['id' => $id])->one();
if (!empty($image)) {
$product = Product::findOne($image->product_id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) {
$image->removeImage($id);
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $image->product_id
]));
return $this->redirect(['add-image', 'id' => $image->product_id, 'languageId' => $languageId]);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
} else throw new Exception();
}
/**
* Changes ProductImage position to down
*
* Users which have 'updateOwnProduct' permission can change position only for ProductImage models that have been created by their.
* Users which have 'updateProduct' permission can change position for all ProductImage models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionImageDown($id, $languageId)
{
$productImage = ProductImage::findOne($id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($productImage->product_id)->owner])) {
if ($productImage) {
$productImage->moveNext();
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $productImage->product_id
]));
}
return $this->actionAddImage($productImage->product_id, $languageId);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* Changes ProductImage position to up
*
* Users which have 'updateOwnProduct' permission can change position only for ProductImage models that have been created by their.
* Users which have 'updateProduct' permission can change position for all ProductImage models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionImageUp($id, $languageId)
{
$productImage = ProductImage::findOne($id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($productImage->product_id)->owner])) {
if ($productImage) {
$productImage->movePrev();
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $productImage->product_id
]));
}
return $this->actionAddImage($productImage->product_id, $languageId);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* Users which have 'updateOwnProduct' permission can add video only from Product models that have been created by their.
* Users which have 'updateProduct' permission can add video from all Product models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionAddVideo($id, $languageId)
{
if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($id)->owner])) {
$product = Product::findOne($id);
$video = new ProductVideo();
$videoForm = new ProductVideoForm();
if (Yii::$app->request->isPost) {
$video->load(Yii::$app->request->post());
$videoForm->load(Yii::$app->request->post());
$videoForm->file_name = UploadedFile::getInstance($videoForm, 'file_name');
if ($fileName = $videoForm->upload()) {
$video->file_name = $fileName;
$video->resource = 'videofile';
$video->product_id = $id;
$video->save();
}
if ($video->resource == 'youtube') {
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video->file_name, $match)) {
$id = $match[1];
$video->product_id = $product->id;
$video->file_name = $id;
if ($video->validate()) {
$video->save();
}
} else {
\Yii::$app->session->setFlash('error', \Yii::t('shop', 'Sorry, this format is not supported'));
}
} elseif ($video->resource == 'vimeo') {
$regexstr = '~
# Match Vimeo link and embed code
(?:<iframe [^>]*src=")? # If iframe match up to first quote of src
(?: # Group vimeo url
https?:\/\/ # Either http or https
(?:[\w]+\.)* # Optional subdomains
vimeo\.com # Match vimeo.com
(?:[\/\w]*\/videos?)? # Optional video sub directory this handles groups links also
\/ # Slash before Id
([0-9]+) # $1: VIDEO_ID is numeric
[^\s]* # Not a space
) # End group
"? # Match end quote if part of src
(?:[^>]*></iframe>)? # Match the end of the iframe
(?:<p>.*</p>)? # Match any title information stuff
~ix';
if (preg_match($regexstr, $video->file_name, $match)) {
$id = $match[1];
$video->product_id = $product->id;
$video->file_name = $id;
if ($video->validate()) {
$video->save();
}
} else {
\Yii::$app->session->setFlash('error', \Yii::t('shop', 'Sorry, this format is not supported'));
}
}
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $id
]));
}
if (Yii::$app->request->isPjax) {
return $this->renderPartial('add-video', [
'product' => $product,
'selectedLanguage' => Language::findOne($languageId),
'video_form' => new ProductVideo(),
'video_form_upload' => new ProductVideoForm(),
'video' => $video,
'videos' => ProductVideo::find()->where(['product_id' => $product->id])->all()
]);
}
return $this->render('save', [
'product' => $product,
'selectedLanguage' => Language::findOne($languageId),
'viewName' => 'add-video',
'params' => [
'product' => $product,
'selectedLanguage' => Language::findOne($languageId),
'video_form' => new ProductVideo(),
'video_form_upload' => new ProductVideoForm(),
'video' => $video,
'videos' => ProductVideo::find()->where(['product_id' => $product->id])->all()
]
]);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* Users which have 'updateOwnProduct' permission can delete video only from Product models that have been created by their.
* Users which have 'updateProduct' permission can delete video from all Product models.
*
* @param integer $id
* @param integer $languageId
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionDeleteVideo($id, $languageId)
{
if (!empty($id)) {
$video = ProductVideo::findOne($id);
$product = Product::findOne($video->product_id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) {
if ($video->resource == 'videofile') {
$dir = Yii::getAlias('@frontend/web/video');
unlink($dir . '/' . $video->file_name);
}
ProductVideo::deleteAll(['id' => $id]);
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $video->product_id
]));
return $this->renderPartial('add-video', [
'product' => $product,
'selectedLanguage' => Language::findOne($languageId),
'video_form' => new ProductVideo(),
'video_form_upload' => new ProductVideoForm(),
'videos' => ProductVideo::find()->where(['product_id' => $product->id])->all()
]);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
return false;
}
/**
* Changes product status property by ModerationManager
*
* Users which have 'moderateProductCreation' permission can change product status.
*
* @param integer $id
* @param integer $status
* @return mixed
*/
public function actionChangeProductStatus($id, $status)
{
if (!empty($id) && !empty($status)) {
$product = Product::findOne($id);
if ($product->status == Product::STATUS_ON_MODERATION) {
switch ($status) {
case Product::STATUS_SUCCESS:
$product->status = Product::STATUS_SUCCESS;
$product->save();
$this->trigger(self::EVENT_AFTER_ACCEPT_PRODUCT, new ProductEvent([
'id' => $id
]));
break;
case Product::STATUS_DECLINED:
$product->status = Product::STATUS_DECLINED;
$product->save();
break;
}
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent([
'id' => $id
]));
}
}
return $this->redirect(Yii::$app->request->referrer);
}
/**
* Changes product position to up
*
* Users which have 'updateOwnProduct' permission can change position only for Product models that have been created by their.
* Users which have 'updateProduct' permission can change position for all Product models.
*
* @param integer $id
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionUp($id)
{
$product = Product::findOne($id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => $product->owner])) {
if (!empty($product)) {
/**
* @var $product PositionBehavior
*/
$product->movePrev();
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent(['id' => $id]));
}
if (\Yii::$app->request->isPjax) return $this->actionIndex();
return $this->redirect(\Yii::$app->request->referrer);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* Changes product position to down
*
* Users which have 'updateOwnProduct' permission can change position only for Product models that have been created by their.
* Users which have 'updateProduct' permission can change position for all Product models.
*
* @param integer $id
* @return mixed
* @throws ForbiddenHttpException
*/
public function actionDown($id)
{
$product = Product::findOne($id);
if (\Yii::$app->user->can('updateProduct', ['productOwner' => Product::findOne($id)->owner])) {
if ($product) {
/**
* @var $product PositionBehavior
*/
$product->moveNext();
$this->trigger(self::EVENT_AFTER_EDIT_PRODUCT, new ProductEvent(['id' => $id]));
}
if (\Yii::$app->request->isPjax) return $this->actionIndex();
return $this->redirect(\Yii::$app->request->referrer);
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to do this action.'));
}
/**
* Deletes product.
*
* Users which have 'deleteProduct' permission can delete all Product models.
* Users which have 'deleteOwnProduct' permission can delete only Product models that have been created by their.
*
* @param integer $id
* @return mixed
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
*/
public function actionDelete($id)
{
$product = Product::findOne($id);
if (empty($product)) throw new NotFoundHttpException();
if (\Yii::$app->user->can('deleteProduct', ['productOwner' => $product->owner])) {
$this->trigger(self::EVENT_BEFORE_DELETE_PRODUCT);
SeoData::deleteAll(
['AND',
['entity_name' => ProductTranslation::className()],
['in', 'entity_id', ArrayHelper::getColumn($product->translations, 'id')]]);
$product->delete();
$this->trigger(self::EVENT_AFTER_DELETE_PRODUCT, new ProductEvent(['id' => $id]));
if (\Yii::$app->request->isPjax) return $this->actionIndex();
return $this->redirect('index');
} else throw new ForbiddenHttpException(\Yii::t('shop', 'You have not permission to delete this product.'));
}
/**
* Generates seo Url from title on add-basic page
*
* @param string $title
* @return string
*/
public function actionGenerateSeoUrl($title)
{
$newSeoUrl = Inflector::slug($title);
$seoUrl = SeoData::find()
->where(['entity_name' => ProductTranslation::className(), 'seo_url' => $newSeoUrl])
->one();
if (!empty($seoUrl)) $newSeoUrl = $newSeoUrl . '-' . date("d-m-y-H-i-s");
return $newSeoUrl;
}
}