|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\backend\actions; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use app\models\Object; |
|
7
|
|
|
use app\modules\shop\models\Category; |
|
8
|
|
|
use app\modules\shop\models\Product; |
|
9
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
10
|
|
|
use yii\base\Action; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\caching\TagDependency; |
|
13
|
|
|
use yii\db\Query; |
|
14
|
|
|
use yii\web\NotFoundHttpException; |
|
15
|
|
|
use yii\web\ServerErrorHttpException; |
|
16
|
|
|
use app\backend\widgets\CategoryMovementsButtons; |
|
17
|
|
|
|
|
18
|
|
|
class CategoryMovementsAction extends Action |
|
19
|
|
|
{ |
|
20
|
|
|
private $categoryId; |
|
21
|
|
|
private $items = []; |
|
22
|
|
|
private $action; |
|
23
|
|
|
/** @var Object */ |
|
24
|
|
|
private static $object; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @throws NotFoundHttpException |
|
28
|
|
|
* @throws ServerErrorHttpException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function init() |
|
31
|
|
|
{ |
|
32
|
|
|
if (false === Yii::$app->request->isAjax) { |
|
33
|
|
|
throw new NotFoundHttpException('Page not found'); |
|
34
|
|
|
} |
|
35
|
|
|
$catId = Yii::$app->request->post('cat-id'); |
|
36
|
|
|
if (null !== Category::findOne(['id' => $catId])) { |
|
37
|
|
|
$this->categoryId = $catId; |
|
38
|
|
|
} else { |
|
39
|
|
|
throw new ServerErrorHttpException("Can't find Category with id {$catId}"); |
|
40
|
|
|
} |
|
41
|
|
|
if (true === empty(static::$object)) { |
|
|
|
|
|
|
42
|
|
|
static::$object = Object::getForClass(Product::className()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
$this->action = Yii::$app->request->post('action', ''); |
|
45
|
|
|
$this->items = Yii::$app->request->post('mc-items', []); |
|
46
|
|
|
parent::init(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function run() |
|
53
|
|
|
{ |
|
54
|
|
|
$n = 0; |
|
55
|
|
|
switch($this->action) { |
|
56
|
|
|
case CategoryMovementsButtons::ADD_ACTION : |
|
|
|
|
|
|
57
|
|
|
$n = $this->add(); |
|
58
|
|
|
break; |
|
59
|
|
|
case CategoryMovementsButtons::MOVE_ACTION : |
|
|
|
|
|
|
60
|
|
|
$n = $this->move(); |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
$tags = []; |
|
64
|
|
|
foreach ($this->items as $id) { |
|
65
|
|
|
$tags[] = ActiveRecordHelper::getObjectTag(Product::className(), $id); |
|
66
|
|
|
} |
|
67
|
|
|
TagDependency::invalidate(Yii::$app->cache, $tags); |
|
68
|
|
|
Yii::$app->session->setFlash('info', Yii::t('app', 'Items updated: {n}', ['n' => $n])); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
private function move() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->add(); |
|
77
|
|
|
return Product::updateAll(['main_category_id' => $this->categoryId],['id' => $this->items]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return int |
|
82
|
|
|
* @throws \yii\db\Exception |
|
83
|
|
|
*/ |
|
84
|
|
|
private function add() |
|
85
|
|
|
{ |
|
86
|
|
|
$exists = (new Query())->select('object_model_id') |
|
87
|
|
|
->from(static::$object->categories_table_name) |
|
|
|
|
|
|
88
|
|
|
->where([ |
|
89
|
|
|
'category_id' => $this->categoryId, |
|
90
|
|
|
'object_model_id' => $this->items |
|
91
|
|
|
]) |
|
92
|
|
|
->column(); |
|
93
|
|
|
$new = array_diff($this->items, $exists); |
|
94
|
|
|
$rows = []; |
|
95
|
|
|
foreach ($new as $id) { |
|
96
|
|
|
$rows[] = [$this->categoryId, $id, 0]; |
|
97
|
|
|
} |
|
98
|
|
|
$n = 0; |
|
99
|
|
|
if (false === empty($rows)) { |
|
100
|
|
|
$n = Yii::$app->db->createCommand()->batchInsert( |
|
101
|
|
|
static::$object->categories_table_name, |
|
|
|
|
|
|
102
|
|
|
['category_id', 'object_model_id', 'sort_order'], |
|
103
|
|
|
$rows |
|
104
|
|
|
)->execute(); |
|
105
|
|
|
} |
|
106
|
|
|
return $n; |
|
107
|
|
|
} |
|
108
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: