/
Module.php
195 lines (157 loc) · 5.08 KB
/
Module.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
<?php
namespace sap55\order;
use Yii;
use yii\base\Module as BaseModule;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\helpers\ArrayHelper;
use yii\filters\AccessControl;
use sap55\order\models\OrderAttribute;
use sap55\order\models\AuthItem;
use sap55\order\models\Store;
class Module extends BaseModule
{
CONST CACHE_ORDER = 'orderModule';
public $usernameField = 'username';
private $bgColors = [
'bg-light-blue',
'bg-green',
'bg-yellow',
'bg-red',
'bg-aqua',
'bg-purple',
'bg-blue',
'bg-navy',
'bg-teal',
'bg-maroon',
'bg-black',
'bg-gray',
'bg-olive',
'bg-lime',
'bg-orange',
'bg-fuchsia',
];
/**
* @var string The prefix for order module URL.
* @See [[GroupUrlRule::prefix]]
*/
public $urlPrefix = 'order';
/** @var array The rules to be used in URL management. */
public $urlRules = [
'<id:\d+>' => 'order/view',
'setting' => 'setting',
'dashboard' => 'order/index',
'<action:\w+>' => 'order/<action>',
];
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
]
],
],
];
}
public function getVisiableAttributes()
{
$attributes = $this->getAttributesByRole()->visible_attributes;
return $attributes;
}
public function getEditableAttributes()
{
$attributes = ArrayHelper::merge($this->getAttributesByRole()->editable_attributes, $this->getOrderAttributes('system'));
return $attributes;
}
public function getRolesByUser($user_id = 0)
{
if ($user_id == 0) {
$user_id = Yii::$app->user->id;
}
$user_roles = Yii::$app->authManager->getRolesByUser($user_id);
foreach ($user_roles as $role) {
$user_role[] = $role->name;
}
return $user_role[0];
}
public function getOrderAttributes($assignment = 'all')
{
if ($assignment == 'all') {
$order_attributes = OrderAttribute::find()->asArray()->all();
} elseif ($assignment == 'user') {
$order_attributes = OrderAttribute::find()->user()->asArray()->all();
} elseif ($assignment == 'system') {
$order_attributes = OrderAttribute::find()->system()->asArray()->all();
}
if ($order_attributes !== null)
return ArrayHelper::getColumn($order_attributes, 'attribute_name');
else
return [];
}
public function getAttributesByRole()
{
$model = AuthItem::find()
->select('name')
->where(['name' => $this->getRolesByUser()])
->with('visibleAttributes', 'editableAttributes')
->one();
if ($model->visibleAttributes !== null || $model->editableAttributes !== null)
return $model;
else
throw new NotFoundHttpException('The project not have any roles configured.');
}
public function getStoresIdsByUser($user_id = 0)
{
return ArrayHelper::getColumn($this->getStoreQueryByUserId($user_id), 'store_id');
}
public function getStoresByUser($user_id = 0)
{
return ArrayHelper::map($this->getStoreQueryByUserId($user_id), 'store_id', 'name');
}
public function getOrderStatusesByUser($user_id = 0)
{
$orderStatuses = [];
foreach ($this->getStoreQueryByUserId($user_id) as $store) {
$orderStatus = ArrayHelper::map($store->orderStatuses, 'order_status_id', 'name');
$orderStatuses = ArrayHelper::merge($orderStatuses, $orderStatus);
}
return array_unique($orderStatuses);
}
public function getUsersArray()
{
$model = \Yii::createObject([
'class' => Yii::$app->user->identityClass,
]);
$query = $model::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return ArrayHelper::map($model::find()->select(['id', $this->usernameField])->orderBy($this->usernameField . ' ASC')->asArray()->all(), 'id', $this->usernameField);
}
public function getBgColors()
{
foreach ($this->bgColors as $value) {
$colors[$value] = $value;
$options[$value] = ['class' => $value];
$colorsHtml[$value] = '<span class="' . $value . '">' . $value . '</span>';
}
return [
'colors' => $colors,
'options' => $options,
'colorsHtml' => $colorsHtml
];
}
private function getStoreQueryByUserId($user_id)
{
if ($user_id == 0) {
$user_id = Yii::$app->user->id;
}
return Store::find()->joinWith('users')->where(['user.id' => $user_id])->with([
'orderStatuses'
])->all();
}
}