1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\backend\widgets; |
4
|
|
|
|
5
|
|
|
use app\models\Object; |
6
|
|
|
use app\modules\review\models\Review; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\base\Widget; |
9
|
|
|
use app; |
10
|
|
|
use kartik\icons\Icon; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
|
13
|
|
|
class FloatingPanel extends Widget |
14
|
|
|
{ |
15
|
|
|
public $bottom = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Check if model has any reviews and if so - generate link to edit them |
19
|
|
|
* |
20
|
|
|
* @param string $objectName |
21
|
|
|
* @param int $modelId |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
private function getReviewEditParams($objectName, $modelId) |
25
|
|
|
{ |
26
|
|
|
$objectId = array_search($objectName, Object::getSelectArray()); |
27
|
|
|
$reviews = Review::getForObjectModel($modelId, $objectId, 1); |
28
|
|
|
if (!empty($reviews)) { |
29
|
|
|
return [ |
30
|
|
|
"label" => Icon::show("pencil") . Yii::t("app", "Edit reviews"), |
31
|
|
|
"url" => [ |
32
|
|
|
"/review/backend-review/index", |
33
|
|
|
"SearchModel" => [ |
34
|
|
|
"object_id" => $objectId, |
35
|
|
|
"object_model_id" => $modelId |
36
|
|
|
] |
37
|
|
|
] |
38
|
|
|
]; |
39
|
|
|
} else { |
40
|
|
|
return []; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function run() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
app\backend\assets\FrontendEditingAsset::register($this->view); |
47
|
|
|
|
48
|
|
|
$items = [ |
49
|
|
|
[ |
50
|
|
|
'label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'), |
51
|
|
|
'url' => ['/backend/'], |
52
|
|
|
] |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
switch (Yii::$app->requestedRoute) { |
56
|
|
|
case 'shop/product/list': |
57
|
|
|
if (isset($_GET['properties'])) { |
58
|
|
|
$apply_if_params = []; |
59
|
|
|
foreach ($_GET['properties'] as $property_id => $values) { |
60
|
|
|
if (isset($values[0])) { |
61
|
|
|
$apply_if_params[$property_id] = $values[0]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
if (Yii::$app->response->dynamic_content_trait === true) { |
65
|
|
|
$items[] = [ |
66
|
|
|
'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'), |
67
|
|
|
'url' => [ |
68
|
|
|
'/backend/dynamic-content/edit', |
69
|
|
|
'id' => Yii::$app->response->matched_dynamic_content_trait_model->id, |
70
|
|
|
], |
71
|
|
|
]; |
72
|
|
|
} else { |
73
|
|
|
if (isset($_GET['properties'], $_GET['last_category_id'])) { |
74
|
|
|
$items[] = [ |
75
|
|
|
'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'), |
76
|
|
|
'url' => [ |
77
|
|
|
'/backend/dynamic-content/edit', |
78
|
|
|
'DynamicContent' => [ |
79
|
|
|
'apply_if_params' => Json::encode($apply_if_params), |
80
|
|
|
'apply_if_last_category_id' => $_GET['last_category_id'], |
81
|
|
|
'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id, |
|
|
|
|
82
|
|
|
'route' => 'shop/product/list', |
83
|
|
|
] |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
View Code Duplication |
} else { |
91
|
|
|
// no properties selected - go to category edit page |
92
|
|
|
|
93
|
|
|
if (isset($_GET['last_category_id'])) { |
94
|
|
|
$reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id'])); |
95
|
|
|
$cat = app\modules\shop\models\Category::findById($_GET['last_category_id']); |
96
|
|
|
$items[] = [ |
97
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'), |
98
|
|
|
'url' => [ |
99
|
|
|
'/shop/backend-category/edit', |
100
|
|
|
'id' => $cat->id, |
101
|
|
|
'parent_id' => $cat->parent_id, |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
case 'shop/product/show': |
109
|
|
|
if (isset($_GET['model_id'])) { |
110
|
|
|
$reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id'])); |
111
|
|
|
$items[] = [ |
112
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'), |
113
|
|
|
'url' => [ |
114
|
|
|
'/shop/backend-product/edit', |
115
|
|
|
'id' => intval($_GET['model_id']) |
116
|
|
|
], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
break; |
120
|
|
|
|
121
|
|
|
case '/page/page/show': |
122
|
|
|
case '/page/page/list': |
123
|
|
View Code Duplication |
if (isset($_GET['id'])) { |
124
|
|
|
$page = app\modules\page\models\Page::findById($_GET['id']); |
125
|
|
|
$reviewsLink = $this->getReviewEditParams("Page", $_GET['id']); |
126
|
|
|
$items[] = [ |
127
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'), |
128
|
|
|
'url' => [ |
129
|
|
|
'/page/backend/edit', |
130
|
|
|
'id' => $page->id, |
131
|
|
|
'parent_id' =>$page->parent_id, |
132
|
|
|
|
133
|
|
|
], |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (!empty($reviewsLink)) { |
140
|
|
|
$items[] = $reviewsLink; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->render( |
144
|
|
|
'floating-panel', |
145
|
|
|
[ |
146
|
|
|
'items' => $items, |
147
|
|
|
'bottom' => $this->bottom, |
148
|
|
|
] |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: