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") . " (" . count($reviews) . ")", |
31
|
|
|
"url" => [ |
32
|
|
|
"/review/backend-review/index", |
33
|
|
|
"SearchModel" => [ |
34
|
|
|
"object_id" => $objectId, |
35
|
|
|
"object_model_id" => $modelId |
36
|
|
|
] |
37
|
|
|
], |
38
|
|
|
"target" => "_blank" |
39
|
|
|
]; |
40
|
|
|
} else { |
41
|
|
|
return []; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function run() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
app\backend\assets\FrontendEditingAsset::register($this->view); |
48
|
|
|
|
49
|
|
|
$items = [ |
50
|
|
|
[ |
51
|
|
|
'label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'), |
52
|
|
|
'url' => ['/backend/'], |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
switch (Yii::$app->requestedRoute) { |
57
|
|
|
case 'shop/product/list': |
58
|
|
|
if (isset($_GET['properties'])) { |
59
|
|
|
$apply_if_params = []; |
60
|
|
|
foreach ($_GET['properties'] as $property_id => $values) { |
61
|
|
|
if (isset($values[0])) { |
62
|
|
|
$apply_if_params[$property_id] = $values[0]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
if (Yii::$app->response->dynamic_content_trait === true) { |
66
|
|
|
$items[] = [ |
67
|
|
|
'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'), |
68
|
|
|
'url' => [ |
69
|
|
|
'/backend/dynamic-content/edit', |
70
|
|
|
'id' => Yii::$app->response->matched_dynamic_content_trait_model->id, |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} else { |
74
|
|
|
if (isset($_GET['properties'], $_GET['last_category_id'])) { |
75
|
|
|
$items[] = [ |
76
|
|
|
'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'), |
77
|
|
|
'url' => [ |
78
|
|
|
'/backend/dynamic-content/edit', |
79
|
|
|
'DynamicContent' => [ |
80
|
|
|
'apply_if_params' => Json::encode($apply_if_params), |
81
|
|
|
'apply_if_last_category_id' => $_GET['last_category_id'], |
82
|
|
|
'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id, |
|
|
|
|
83
|
|
|
'route' => 'shop/product/list', |
84
|
|
|
] |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
View Code Duplication |
} else { |
92
|
|
|
// no properties selected - go to category edit page |
93
|
|
|
|
94
|
|
|
if (isset($_GET['last_category_id'])) { |
95
|
|
|
$reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id'])); |
96
|
|
|
$cat = app\modules\shop\models\Category::findById($_GET['last_category_id']); |
97
|
|
|
$items[] = [ |
98
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'), |
99
|
|
|
'url' => [ |
100
|
|
|
'/shop/backend-category/edit', |
101
|
|
|
'id' => $cat->id, |
102
|
|
|
'parent_id' => $cat->parent_id, |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
break; |
109
|
|
|
case 'shop/product/show': |
110
|
|
|
if (isset($_GET['model_id'])) { |
111
|
|
|
$reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id'])); |
112
|
|
|
$items[] = [ |
113
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'), |
114
|
|
|
'url' => [ |
115
|
|
|
'/shop/backend-product/edit', |
116
|
|
|
'id' => intval($_GET['model_id']) |
117
|
|
|
], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
case '/page/page/show': |
123
|
|
|
case '/page/page/list': |
124
|
|
View Code Duplication |
if (isset($_GET['id'])) { |
125
|
|
|
$page = app\modules\page\models\Page::findById($_GET['id']); |
126
|
|
|
$reviewsLink = $this->getReviewEditParams("Page", $_GET['id']); |
127
|
|
|
$items[] = [ |
128
|
|
|
'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'), |
129
|
|
|
'url' => [ |
130
|
|
|
'/page/backend/edit', |
131
|
|
|
'id' => $page->id, |
132
|
|
|
'parent_id' =>$page->parent_id, |
133
|
|
|
|
134
|
|
|
], |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (!empty($reviewsLink)) { |
141
|
|
|
$items[] = $reviewsLink; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->render( |
145
|
|
|
'floating-panel', |
146
|
|
|
[ |
147
|
|
|
'items' => $items, |
148
|
|
|
'bottom' => $this->bottom, |
149
|
|
|
] |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
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: