1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data manager |
4
|
|
|
* |
5
|
|
|
* @author Alexey Krupskiy <[email protected]> |
6
|
|
|
* @link http://inji.ru/ |
7
|
|
|
* @copyright 2015 Alexey Krupskiy |
8
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ui; |
12
|
|
|
|
13
|
|
|
class DataManager extends \Object { |
14
|
|
|
|
15
|
|
|
public $modelName = ''; |
16
|
|
|
public $managerOptions = []; |
17
|
|
|
public $managerName = 'customManager'; |
18
|
|
|
public $name = 'Менеджер данных'; |
19
|
|
|
public $limit = 30; |
20
|
|
|
public $page = 1; |
21
|
|
|
public $table = null; |
22
|
|
|
public $joins = []; |
23
|
|
|
public $predraw = false; |
24
|
|
|
public $cols = []; |
25
|
|
|
public $managerId = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Construct new data manager |
29
|
|
|
* |
30
|
|
|
* @param string|array $modelNameOrOptions |
31
|
|
|
* @param string $managerName |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function __construct($modelNameOrOptions, $managerName = 'manager') { |
35
|
|
|
$this->managerName = $managerName; |
36
|
|
|
|
37
|
|
|
if (!is_array($modelNameOrOptions)) { |
38
|
|
|
if (!class_exists($modelNameOrOptions)) { |
39
|
|
|
throw new \Exception("model {$modelNameOrOptions} not exists"); |
40
|
|
|
} |
41
|
|
|
$this->modelName = $modelNameOrOptions; |
42
|
|
|
$this->managerOptions = !empty($modelNameOrOptions::$dataManagers[$managerName]) ? $modelNameOrOptions::$dataManagers[$managerName] : []; |
43
|
|
|
if (isset($modelNameOrOptions::$objectName)) { |
44
|
|
|
$this->name = $modelNameOrOptions::$objectName; |
45
|
|
|
} else { |
46
|
|
|
$this->name = $modelNameOrOptions; |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
$this->managerOptions = $modelNameOrOptions; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!$this->managerOptions || !is_array($this->managerOptions)) { |
53
|
|
|
throw new \Exception('empty DataManager'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!empty($this->managerOptions['name'])) { |
57
|
|
|
$this->name = $this->managerOptions['name']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->managerId = str_replace('\\', '_', 'dataManager_' . $this->modelName . '_' . $this->managerName . '_' . \Tools::randomString()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get buttons for manager |
65
|
|
|
* |
66
|
|
|
* @param string $params |
67
|
|
|
* @param object $model |
68
|
|
|
*/ |
69
|
|
|
public function getButtons($params = [], $model = null) { |
70
|
|
|
$modelName = $this->modelName; |
71
|
|
|
|
72
|
|
|
$formParams = [ |
73
|
|
|
'dataManagerParams' => $params, |
74
|
|
|
'formName' => !empty($this->managerOptions['editForm']) ? $this->managerOptions['editForm'] : 'manager' |
75
|
|
|
]; |
76
|
|
|
if ($model) { |
77
|
|
|
$formModelName = get_class($model); |
78
|
|
|
$relations = $formModelName::relations(); |
79
|
|
|
$type = !empty($relations[$params['relation']]['type']) ? $relations[$params['relation']]['type'] : 'to'; |
80
|
|
|
switch ($type) { |
81
|
|
|
case 'relModel': |
82
|
|
|
$formParams['preset'] = [ |
83
|
|
|
$formModelName::index() => $model->pk() |
84
|
|
|
]; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
$formParams['preset'] = [ |
88
|
|
|
$relations[$params['relation']]['col'] => $model->pk() |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$buttons = []; |
94
|
|
|
if (!empty($this->managerOptions['sortMode'])) { |
95
|
|
|
$buttons[] = [ |
96
|
|
|
'class' => 'modeBtn', |
97
|
|
|
'data-mode' => 'sort', |
98
|
|
|
'text' => 'Сортировать', |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
if (!empty($this->managerOptions['filters'])) { |
102
|
|
|
$buttons[] = [ |
103
|
|
|
'text' => 'Фильтры', |
104
|
|
|
'onclick' => ' var modal = $("#' . $this->managerId . '_filters"); |
105
|
|
|
modal.modal("show");', |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
if (!empty($modelName::$forms['simpleItem'])) { |
109
|
|
|
$formParams['formName'] = 'simpleItem'; |
110
|
|
|
$buttons[] = [ |
111
|
|
|
'text' => '<i class = "glyphicon glyphicon-send"></i> Быстрое создание', |
112
|
|
|
'onclick' => 'inji.Ui.dataManagers.get(this).newItem("' . str_replace('\\', '\\\\', $modelName) . '",' . json_encode($formParams) . ');', |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
$formParams['formName'] = !empty($this->managerOptions['editForm']) ? $this->managerOptions['editForm'] : 'manager'; |
116
|
|
|
$actions = $this->getActions(false, true); |
117
|
|
|
foreach ($actions as $action) { |
118
|
|
|
$btn = $action['className']::managerButton($this, $formParams, $action); |
119
|
|
|
if ($btn) { |
120
|
|
|
$buttons[] = $btn; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $buttons; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
function getActions($groupActions = false, $managerActions = false) { |
|
|
|
|
128
|
|
|
$actions = [ |
129
|
|
|
'Open' => ['className' => 'Open'], 'Create' => ['className' => 'Create'], 'Edit' => ['className' => 'Edit'], 'Delete' => ['className' => 'Delete'] |
130
|
|
|
]; |
131
|
|
|
if (isset($this->managerOptions['actions'])) { |
132
|
|
|
$actions = array_merge($actions, $this->managerOptions['actions']); |
133
|
|
|
} |
134
|
|
|
$return = []; |
135
|
|
|
foreach ($actions as $key => $action) { |
136
|
|
|
if ($action === false) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
if (is_array($action)) { |
140
|
|
|
if (!empty($action['access']['groups']) && !in_array(\Users\User::$cur->group_id, $action['access']['groups'])) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
if (empty($action['className'])) { |
144
|
|
|
$action['className'] = $key; |
145
|
|
|
} |
146
|
|
|
$return[$key] = $action; |
147
|
|
|
} else { |
148
|
|
|
$key = $action; |
149
|
|
|
$return[$key] = [ |
150
|
|
|
'className' => $action |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
$return[$key]['className'] = strpos($return[$key]['className'], '\\') === false && class_exists('Ui\DataManager\Action\\' . $return[$key]['className']) ? 'Ui\DataManager\Action\\' . $return[$key]['className'] : $return[$key]['className']; |
154
|
|
|
if (!class_exists($return[$key]['className']) || ($groupActions && !$return[$key]['className']::$groupAction) || ($managerActions && !$return[$key]['className']::$managerAction)) { |
155
|
|
|
unset($return[$key]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
return $return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get cols for manager |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getCols() { |
167
|
|
|
$actions = $this->getActions(); |
168
|
|
|
ob_start(); |
169
|
|
|
?> |
170
|
|
|
<div class="dropdown"> |
171
|
|
|
<a id="dLabel" data-target="#" href="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> |
172
|
|
|
<i class="glyphicon glyphicon-cog"></i> |
173
|
|
|
<span class="caret"></span> |
174
|
|
|
</a> |
175
|
|
|
|
176
|
|
|
<ul class="dropdown-menu" aria-labelledby="dLabel"> |
177
|
|
|
<li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("selectAll");return false;'>Выделить все</a></li> |
178
|
|
|
<li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("unSelectAll");return false;'>Снять все</a></li> |
179
|
|
|
<li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("inverse");return false;'>Инвертировать</a></li> |
180
|
|
|
<li role="separator" class="divider"></li> |
181
|
|
|
<?php |
182
|
|
|
foreach ($actions as $action => $actionParams) { |
183
|
|
|
if (class_exists($actionParams['className']) && $actionParams['className']::$groupAction) { |
184
|
|
|
echo "<li><a role='button' href ='#' onclick='inji.Ui.dataManagers.get(this).groupAction(\"" . str_replace('\\', '\\\\', $action) . "\");return false;'>{$actionParams['className']::$name}</a></li>"; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
?> |
188
|
|
|
</ul> |
189
|
|
|
</div> |
190
|
|
|
<?php |
191
|
|
|
$dropdown = ob_get_contents(); |
192
|
|
|
ob_end_clean(); |
193
|
|
|
|
194
|
|
|
$cols = []; |
195
|
|
|
$cols[] = ['label' => $dropdown]; |
196
|
|
|
$cols['id'] = ['label' => '№', 'sortable' => true]; |
197
|
|
|
|
198
|
|
|
$modelName = $this->modelName; |
199
|
|
|
foreach ($this->managerOptions['cols'] as $key => $col) { |
200
|
|
|
if (is_array($col)) { |
201
|
|
|
$colName = $key; |
202
|
|
|
$colOptions = $col; |
203
|
|
|
} else { |
204
|
|
|
$colName = $col; |
205
|
|
|
$colOptions = []; |
206
|
|
|
} |
207
|
|
|
$colInfo = []; |
208
|
|
|
if ($modelName) { |
209
|
|
|
$colInfo = $modelName::getColInfo($colName); |
210
|
|
|
} |
211
|
|
|
if (empty($colOptions['label']) && !empty($colInfo['label'])) { |
212
|
|
|
$colOptions['label'] = $colInfo['label']; |
213
|
|
|
} elseif (empty($colOptions['label'])) { |
214
|
|
|
$colOptions['label'] = $colName; |
215
|
|
|
} |
216
|
|
|
$cols[$colName] = $colOptions; |
217
|
|
|
} |
218
|
|
|
return $cols; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get rows for manager |
223
|
|
|
* |
224
|
|
|
* @param array $params |
225
|
|
|
* @param object $model |
226
|
|
|
* @return type |
227
|
|
|
*/ |
228
|
|
|
public function getRows($params = [], $model = null) { |
|
|
|
|
229
|
|
|
$modelName = $this->modelName; |
230
|
|
|
if (!class_exists($modelName)) { |
231
|
|
|
return []; |
232
|
|
|
} |
233
|
|
View Code Duplication |
if (!$this->checkAccess()) { |
|
|
|
|
234
|
|
|
$this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->managerName . '"'); |
235
|
|
|
return []; |
236
|
|
|
} |
237
|
|
|
$modelName = $this->modelName; |
238
|
|
|
$queryParams = []; |
239
|
|
|
if (empty($params['all'])) { |
240
|
|
|
if (!empty($params['limit'])) { |
241
|
|
|
$this->limit = (int) $params['limit']; |
242
|
|
|
} |
243
|
|
|
if (!empty($params['page'])) { |
244
|
|
|
$this->page = (int) $params['page']; |
245
|
|
|
} |
246
|
|
|
$queryParams['limit'] = $this->limit; |
247
|
|
|
$queryParams['start'] = $this->page * $this->limit - $this->limit; |
248
|
|
|
} |
249
|
|
View Code Duplication |
if (!empty($params['categoryPath']) && $modelName::$categoryModel) { |
|
|
|
|
250
|
|
|
$queryParams['where'][] = ['tree_path', $params['categoryPath'] . '%', 'LIKE']; |
251
|
|
|
} |
252
|
|
|
if (!empty($params['appType'])) { |
253
|
|
|
$queryParams['appType'] = $params['appType']; |
254
|
|
|
} |
255
|
|
|
if ($this->joins) { |
256
|
|
|
$queryParams['joins'] = $this->joins; |
257
|
|
|
} |
258
|
|
View Code Duplication |
if (!empty($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'])) { |
|
|
|
|
259
|
|
|
foreach ($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'] as $colName => $colOptions) { |
260
|
|
|
if (!empty($colOptions['userCol'])) { |
261
|
|
|
$queryParams['where'][] = [$colName, \Model::getColValue(\Users\User::$cur, $colOptions['userCol'])]; |
262
|
|
|
} elseif (isset($colOptions['value'])) { |
263
|
|
|
if (is_array($colOptions['value'])) { |
264
|
|
|
foreach ($colOptions['value'] as $key => $value) { |
265
|
|
|
if ($key === 'userCol') { |
266
|
|
|
$colOptions['value'][$key] = \Model::getColValue(\Users\User::$cur, $value); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
$queryParams['where'][] = [$colName, $colOptions['value'], is_array($colOptions['value']) ? 'IN' : '=']; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
if (!empty($this->managerOptions['filters'])) { |
275
|
|
|
foreach ($this->managerOptions['filters'] as $col) { |
276
|
|
|
$colInfo = $modelName::getColInfo($col); |
277
|
|
|
switch ($colInfo['colParams']['type']) { |
278
|
|
View Code Duplication |
case 'select': |
|
|
|
|
279
|
|
|
if (empty($params['filters'][$col]['value'])) { |
280
|
|
|
continue; |
281
|
|
|
} |
282
|
|
|
if (is_array($params['filters'][$col]['value'])) { |
283
|
|
|
foreach ($params['filters'][$col]['value'] as $key => $value) { |
284
|
|
|
if ($value === '') { |
285
|
|
|
unset($params['filters'][$col]['value'][$key]); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
if (!$params['filters'][$col]['value']) { |
290
|
|
|
continue; |
291
|
|
|
} |
292
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value'], is_array($params['filters'][$col]['value']) ? 'IN' : '=']; |
293
|
|
|
break; |
294
|
|
|
case 'bool': |
|
|
|
|
295
|
|
|
|
296
|
|
|
if (!isset($params['filters'][$col]['value']) || $params['filters'][$col]['value'] === '') { |
297
|
|
|
continue; |
298
|
|
|
} |
299
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value']]; |
300
|
|
|
break; |
301
|
|
|
case 'dateTime': |
302
|
|
View Code Duplication |
case 'date': |
|
|
|
|
303
|
|
|
if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) { |
304
|
|
|
continue; |
305
|
|
|
} |
306
|
|
|
if (!empty($params['filters'][$col]['min'])) { |
307
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>=']; |
308
|
|
|
} |
309
|
|
|
if (!empty($params['filters'][$col]['max'])) { |
310
|
|
|
if ($colInfo['colParams']['type'] == 'dateTime' && !strpos($params['filters'][$col]['max'], ' ')) { |
311
|
|
|
|
312
|
|
|
$date = $params['filters'][$col]['max'] . ' 23:59:59'; |
313
|
|
|
} else { |
314
|
|
|
$date = $params['filters'][$col]['max']; |
315
|
|
|
} |
316
|
|
|
$queryParams['where'][] = [$col, $date, '<=']; |
317
|
|
|
} |
318
|
|
|
break; |
319
|
|
View Code Duplication |
case 'number': |
|
|
|
|
320
|
|
|
if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) { |
321
|
|
|
continue; |
322
|
|
|
} |
323
|
|
|
if (!empty($params['filters'][$col]['min'])) { |
324
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>=']; |
325
|
|
|
} |
326
|
|
|
if (!empty($params['filters'][$col]['max'])) { |
327
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['max'], '<=']; |
328
|
|
|
} |
329
|
|
|
break; |
330
|
|
|
case 'email': |
331
|
|
|
case 'text': |
332
|
|
|
case 'textarea': |
333
|
|
View Code Duplication |
case 'html': |
|
|
|
|
334
|
|
|
if (empty($params['filters'][$col]['value'])) { |
335
|
|
|
continue; |
336
|
|
|
} |
337
|
|
|
switch ($params['filters'][$col]['compareType']) { |
338
|
|
|
case 'contains': |
339
|
|
|
$queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'] . '%', 'LIKE']; |
340
|
|
|
break; |
341
|
|
|
case 'equals': |
342
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value']]; |
343
|
|
|
break; |
344
|
|
|
case 'starts_with': |
345
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value'] . '%', 'LIKE']; |
346
|
|
|
break; |
347
|
|
|
case 'ends_with': |
348
|
|
|
$queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'], 'LIKE']; |
349
|
|
|
break; |
350
|
|
|
} |
351
|
|
|
break; |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
if (!empty($params['mode']) && $params['mode'] == 'sort') { |
356
|
|
|
$queryParams['order'] = ['weight', 'asc']; |
357
|
|
|
} elseif (!empty($params['sortered']) && !empty($this->managerOptions['sortable'])) { |
358
|
|
|
foreach ($params['sortered'] as $colName => $sortType) { |
359
|
|
|
if ($colName && in_array($colName, $this->managerOptions['sortable'])) { |
360
|
|
|
$sortType = in_array($sortType, ['desc', 'asc']) ? $sortType : 'desc'; |
361
|
|
|
$queryParams['order'][] = [$colName, $sortType]; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
if ($model && !empty($params['relation'])) { |
366
|
|
|
$relation = $model::getRelation($params['relation']); |
367
|
|
|
$items = $model->$params['relation']($queryParams); |
368
|
|
|
} else { |
369
|
|
|
$relation = false; |
370
|
|
|
$items = $modelName::getList($queryParams); |
371
|
|
|
} |
372
|
|
|
$rows = []; |
373
|
|
|
foreach ($items as $item) { |
374
|
|
|
if ($relation && !empty($relation['relModel'])) { |
375
|
|
|
$item = $relation['relModel']::get([[$item->index(), $item->id], [$model->index(), $model->id]]); |
376
|
|
|
} |
377
|
|
|
$row = []; |
378
|
|
|
$row[] = '<input type ="checkbox" name = "pk[]" value =' . $item->pk() . '>'; |
379
|
|
|
$redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '%5C', get_class($originalItem)); |
|
|
|
|
380
|
|
|
$row[] = "<a href ='/admin/" . $item->genViewLink() . "?redirectUrl={$redirectUrl}'>{$item->pk()}</a>"; |
381
|
|
|
foreach ($this->managerOptions['cols'] as $key => $colName) { |
382
|
|
|
if (!empty($params['download'])) { |
383
|
|
|
$row[] = \Model::getColValue($item, is_array($colName) ? $key : $colName, true, false); |
384
|
|
|
} else { |
385
|
|
|
$row[] = DataManager::drawCol($item, is_array($colName) ? $key : $colName, $params, $this); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
$row[] = $this->rowButtons($item, $params); |
389
|
|
|
$rows[] = $row; |
390
|
|
|
} |
391
|
|
|
return $rows; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public static function drawCol($item, $colName, $params = [], $dataManager = null, $originalCol = '', $originalItem = null) { |
|
|
|
|
395
|
|
|
$modelName = get_class($item); |
396
|
|
|
if (!class_exists($modelName)) { |
397
|
|
|
return false; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
if (!$originalCol) { |
401
|
|
|
$originalCol = $colName; |
402
|
|
|
} |
403
|
|
|
if (!$originalItem) { |
404
|
|
|
$originalItem = $item; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
$relations = $modelName::relations(); |
408
|
|
|
if (strpos($colName, ':') !== false && !empty($relations[substr($colName, 0, strpos($colName, ':'))])) { |
409
|
|
|
$rel = substr($colName, 0, strpos($colName, ':')); |
410
|
|
|
$col = substr($colName, strpos($colName, ':') + 1); |
411
|
|
|
if ($item->$rel) { |
412
|
|
|
return DataManager::drawCol($item->$rel, $col, $params, $dataManager, $originalCol, $originalItem); |
413
|
|
|
} else { |
414
|
|
|
return 'Не указано'; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
if (!empty($modelName::$cols[$colName]['relation'])) { |
418
|
|
|
$type = !empty($relations[$modelName::$cols[$colName]['relation']]['type']) ? $relations[$modelName::$cols[$colName]['relation']]['type'] : 'to'; |
419
|
|
|
switch ($type) { |
420
|
|
|
case 'relModel': |
421
|
|
|
$managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; |
422
|
|
|
$count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); |
423
|
|
|
$count = $count ? $count : 'Нет'; |
424
|
|
|
return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count}</a>"; |
425
|
|
|
case 'many': |
426
|
|
|
$managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; |
427
|
|
View Code Duplication |
if (!empty($modelName::$cols[$colName]['manager'])) { |
|
|
|
|
428
|
|
|
$managerParams['managerName'] = $modelName::$cols[$colName]['manager']; |
429
|
|
|
} |
430
|
|
|
$count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); |
431
|
|
|
$count = $count ? $count : 'Нет'; |
432
|
|
|
return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count}</a>"; |
433
|
|
|
default : |
|
|
|
|
434
|
|
|
if ($item->{$modelName::$cols[$colName]['relation']}) { |
435
|
|
|
if (\App::$cur->name == 'admin') { |
436
|
|
|
$href = "<a href ='/admin/" . $item->{$modelName::$cols[$colName]['relation']}->genViewLink() . "'>"; |
437
|
|
|
if (!empty($modelName::$cols[$colName]['showCol'])) { |
438
|
|
|
$href .= $item->{$modelName::$cols[$colName]['relation']}->{$modelName::$cols[$colName]['showCol']}; |
439
|
|
|
} else { |
440
|
|
|
|
441
|
|
|
$href .= $item->{$modelName::$cols[$colName]['relation']}->name(); |
442
|
|
|
} |
443
|
|
|
$href .= '</a>'; |
444
|
|
|
return $href; |
445
|
|
|
} else { |
446
|
|
|
return $item->{$modelName::$cols[$colName]['relation']}->name(); |
447
|
|
|
} |
448
|
|
|
} else { |
449
|
|
|
return $item->$colName; |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
} else { |
453
|
|
|
if (!empty($modelName::$cols[$colName]['view']['type'])) { |
454
|
|
|
switch ($modelName::$cols[$colName]['view']['type']) { |
455
|
|
|
case 'widget': |
456
|
|
|
ob_start(); |
457
|
|
|
\App::$cur->view->widget($modelName::$cols[$colName]['view']['widget'], ['item' => $item, 'colName' => $colName, 'colParams' => $modelName::$cols[$colName]]); |
458
|
|
|
$content = ob_get_contents(); |
459
|
|
|
ob_end_clean(); |
460
|
|
|
return $content; |
461
|
|
|
case 'moduleMethod': |
462
|
|
|
return \App::$cur->{$modelName::$cols[$colName]['view']['module']}->{$modelName::$cols[$colName]['view']['method']}($item, $colName, $modelName::$cols[$colName]); |
463
|
|
|
case 'many': |
464
|
|
|
$managerParams = ['relation' => $modelName::$cols[$colName]['relation']]; |
465
|
|
View Code Duplication |
if (!empty($modelName::$cols[$colName]['manager'])) { |
|
|
|
|
466
|
|
|
$managerParams['managerName'] = $modelName::$cols[$colName]['manager']; |
467
|
|
|
} |
468
|
|
|
$count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1])); |
469
|
|
|
return "<a class = 'btn btn-xs btn-primary' onclick = 'inji.Ui.dataManagers.popUp(\"" . str_replace('\\', '\\\\', $modelName) . ":" . $item->pk() . "\"," . json_encode(array_merge($params, $managerParams)) . ")'>{$count} " . \Tools::getNumEnding($count, ['Элемент', 'Элемента', 'Элементов']) . "</a>"; |
470
|
|
|
default: |
471
|
|
|
return $item->$colName; |
472
|
|
|
} |
473
|
|
|
} elseif (!empty($modelName::$cols[$colName]['type'])) { |
474
|
|
|
if (\App::$cur->name == 'admin' && $originalCol == 'name' || ($dataManager && !empty($dataManager->managerOptions['colToView']) && $dataManager->managerOptions['colToView'] == $originalCol)) { |
475
|
|
|
$formName = $dataManager && !empty($dataManager->managerOptions['editForm']) ? $dataManager->managerOptions['editForm'] : 'manager'; |
476
|
|
|
$redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem)); |
477
|
|
|
return "<a href ='/admin/{$originalItem->genViewLink()}?formName={$formName}&redirectUrl={$redirectUrl}'>{$item->$colName}</a>"; |
478
|
|
|
} elseif (\App::$cur->name == 'admin' && $colName == 'name') { |
479
|
|
|
$redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem)); |
480
|
|
|
return "<a href ='/admin/{$item->genViewLink()}?redirectUrl={$redirectUrl}'>{$item->$colName}</a>"; |
481
|
|
|
} elseif ($modelName::$cols[$colName]['type'] == 'html') { |
482
|
|
|
$uid = \Tools::randomString(); |
483
|
|
|
$script = "<script>inji.onLoad(function(){ |
484
|
|
|
var el{$uid}=$('#{$uid}'); |
485
|
|
|
var height{$uid} = el{$uid}.height(); |
486
|
|
|
el{$uid}.css('maxHeight','none'); |
487
|
|
|
function el{$uid}Toggle(){ |
488
|
|
|
console.log($('#{$uid}').css('height')); |
489
|
|
|
|
490
|
|
|
if( $('#{$uid}').css('height')=='44px'){ |
491
|
|
|
$('#{$uid}').css('height','auto'); |
492
|
|
|
var height = $('#{$uid}').height(); |
493
|
|
|
$('#{$uid}').css('height','44px'); |
494
|
|
|
$('#{$uid}').animate({height:height}); |
495
|
|
|
$('#{$uid}').next().text('Свернуть' ) |
496
|
|
|
} |
497
|
|
|
else { |
498
|
|
|
$('#{$uid}').next().text('Развернуть') |
499
|
|
|
$('#{$uid}').animate({height:'44px'}); |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
window['el{$uid}Toggle']= el{$uid}Toggle; |
503
|
|
|
if(el{$uid}.height()>height{$uid}){ |
504
|
|
|
el{$uid}.css('height','44px'); |
505
|
|
|
|
506
|
|
|
el{$uid}.after('<a href=\"#\" onclick=\"el{$uid}Toggle();return false;\">Развернуть</a>'); |
507
|
|
|
} |
508
|
|
|
})</script>"; |
509
|
|
|
return "<div id = '{$uid}' style='max-height:44px;overflow:hidden;'>{$item->$colName}</div>" . $script; |
510
|
|
|
} else { |
511
|
|
|
return \Model::resloveTypeValue($item, $colName); |
512
|
|
|
} |
513
|
|
|
} else { |
514
|
|
|
return $item->$colName; |
515
|
|
|
} |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
public function rowButtons($item, $params) { |
520
|
|
|
$modelName = $this->modelName; |
521
|
|
|
if (!class_exists($modelName)) { |
522
|
|
|
return false; |
523
|
|
|
} |
524
|
|
|
ob_start(); |
525
|
|
|
$widgetName = !empty($this->managerOptions['rowButtonsWidget']) ? $this->managerOptions['rowButtonsWidget'] : 'Ui\DataManager/rowButtons'; |
526
|
|
|
\App::$cur->view->widget($widgetName, [ |
527
|
|
|
'dataManager' => $this, |
528
|
|
|
'item' => $item, |
529
|
|
|
'params' => $params |
530
|
|
|
]); |
531
|
|
|
$buttons = ob_get_contents(); |
532
|
|
|
ob_end_clean(); |
533
|
|
|
return $buttons; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public function getPages($params = [], $model = null) { |
537
|
|
|
$modelName = $this->modelName; |
538
|
|
|
if (!class_exists($modelName)) { |
539
|
|
|
return []; |
540
|
|
|
} |
541
|
|
View Code Duplication |
if (!$this->checkAccess()) { |
|
|
|
|
542
|
|
|
$this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->managerName . '"'); |
543
|
|
|
return []; |
544
|
|
|
} |
545
|
|
|
if (!empty($params['limit'])) { |
546
|
|
|
$this->limit = (int) $params['limit']; |
547
|
|
|
} |
548
|
|
|
if (!empty($params['page'])) { |
549
|
|
|
$this->page = (int) $params['page']; |
550
|
|
|
} |
551
|
|
|
$queryParams = [ |
552
|
|
|
'count' => true |
553
|
|
|
]; |
554
|
|
|
$modelName = $this->modelName; |
555
|
|
View Code Duplication |
if (!empty($params['categoryPath']) && $modelName::$categoryModel) { |
|
|
|
|
556
|
|
|
$queryParams['where'][] = ['tree_path', $params['categoryPath'] . '%', 'LIKE']; |
557
|
|
|
} |
558
|
|
View Code Duplication |
if (!empty($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'])) { |
|
|
|
|
559
|
|
|
foreach ($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'] as $colName => $colOptions) { |
560
|
|
|
if (!empty($colOptions['userCol'])) { |
561
|
|
|
$queryParams['where'][] = [$colName, \Model::getColValue(\Users\User::$cur, $colOptions['userCol'])]; |
562
|
|
|
} elseif (isset($colOptions['value'])) { |
563
|
|
|
if (is_array($colOptions['value'])) { |
564
|
|
|
foreach ($colOptions['value'] as $key => $value) { |
565
|
|
|
if ($key === 'userCol') { |
566
|
|
|
$colOptions['value'][$key] = \Model::getColValue(\Users\User::$cur, $value); |
567
|
|
|
} |
568
|
|
|
} |
569
|
|
|
} |
570
|
|
|
$queryParams['where'][] = [$colName, $colOptions['value'], is_array($colOptions['value']) ? 'IN' : '=']; |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
$modelName = $this->modelName; |
575
|
|
|
if (!empty($this->managerOptions['filters'])) { |
576
|
|
|
foreach ($this->managerOptions['filters'] as $col) { |
577
|
|
|
$colInfo = $modelName::getColInfo($col); |
578
|
|
|
switch ($colInfo['colParams']['type']) { |
579
|
|
View Code Duplication |
case 'select': |
|
|
|
|
580
|
|
|
if (empty($params['filters'][$col]['value'])) { |
581
|
|
|
continue; |
582
|
|
|
} |
583
|
|
|
if (is_array($params['filters'][$col]['value'])) { |
584
|
|
|
foreach ($params['filters'][$col]['value'] as $key => $value) { |
585
|
|
|
if ($value === '') { |
586
|
|
|
unset($params['filters'][$col]['value'][$key]); |
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
if (!$params['filters'][$col]['value']) { |
591
|
|
|
continue; |
592
|
|
|
} |
593
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value'], is_array($params['filters'][$col]['value']) ? 'IN' : '=']; |
594
|
|
|
break; |
595
|
|
|
case 'bool': |
|
|
|
|
596
|
|
|
|
597
|
|
|
if (empty($params['filters'][$col]['value'])) { |
598
|
|
|
continue; |
599
|
|
|
} |
600
|
|
|
$queryParams['where'][] = [$col, '1']; |
601
|
|
|
break; |
602
|
|
|
case 'dateTime': |
603
|
|
View Code Duplication |
case 'date': |
|
|
|
|
604
|
|
|
if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) { |
605
|
|
|
continue; |
606
|
|
|
} |
607
|
|
|
if (!empty($params['filters'][$col]['min'])) { |
608
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>=']; |
609
|
|
|
} |
610
|
|
|
if (!empty($params['filters'][$col]['max'])) { |
611
|
|
|
if ($colInfo['colParams']['type'] == 'dateTime' && !strpos($params['filters'][$col]['max'], ' ')) { |
612
|
|
|
|
613
|
|
|
$date = $params['filters'][$col]['max'] . ' 23:59:59'; |
614
|
|
|
} else { |
615
|
|
|
$date = $params['filters'][$col]['max']; |
616
|
|
|
} |
617
|
|
|
$queryParams['where'][] = [$col, $date, '<=']; |
618
|
|
|
} |
619
|
|
|
break; |
620
|
|
View Code Duplication |
case 'number': |
|
|
|
|
621
|
|
|
if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) { |
622
|
|
|
continue; |
623
|
|
|
} |
624
|
|
|
if (!empty($params['filters'][$col]['min'])) { |
625
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>=']; |
626
|
|
|
} |
627
|
|
|
if (!empty($params['filters'][$col]['max'])) { |
628
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['max'], '<=']; |
629
|
|
|
} |
630
|
|
|
break; |
631
|
|
|
case 'email': |
632
|
|
|
case 'text': |
633
|
|
|
case 'textarea': |
634
|
|
View Code Duplication |
case 'html': |
|
|
|
|
635
|
|
|
if (empty($params['filters'][$col]['value'])) { |
636
|
|
|
continue; |
637
|
|
|
} |
638
|
|
|
switch ($params['filters'][$col]['compareType']) { |
639
|
|
|
case 'contains': |
640
|
|
|
$queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'] . '%', 'LIKE']; |
641
|
|
|
break; |
642
|
|
|
case 'equals': |
643
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value']]; |
644
|
|
|
break; |
645
|
|
|
case 'starts_with': |
646
|
|
|
$queryParams['where'][] = [$col, $params['filters'][$col]['value'] . '%', 'LIKE']; |
647
|
|
|
break; |
648
|
|
|
case 'ends_with': |
649
|
|
|
$queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'], 'LIKE']; |
650
|
|
|
break; |
651
|
|
|
} |
652
|
|
|
break; |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
if ($model && !empty($params['relation'])) { |
657
|
|
|
$count = $model->$params['relation']($queryParams); |
658
|
|
|
} else { |
659
|
|
|
$count = $modelName::getCount($queryParams); |
660
|
|
|
} |
661
|
|
|
$pages = new Pages([ |
662
|
|
|
'limit' => $this->limit, |
663
|
|
|
'page' => $this->page, |
664
|
|
|
], [ |
665
|
|
|
'count' => $count, |
666
|
|
|
'dataManager' => $this |
667
|
|
|
]); |
668
|
|
|
return $pages; |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
public function preDraw($params = [], $model = null) { |
672
|
|
|
$this->predraw = true; |
673
|
|
|
|
674
|
|
|
$cols = $this->getCols(); |
675
|
|
|
|
676
|
|
|
$this->table = new Table(); |
677
|
|
|
$tableCols = []; |
678
|
|
|
foreach ($cols as $colName => $colOptions) { |
679
|
|
|
$tableCols[] = [ |
680
|
|
|
'attributes' => ['class' => $this->managerId . '_colname_' . $colName, 'data-colname' => $colName], |
681
|
|
|
'text' => !empty($colOptions['label']) ? $colOptions['label'] : $colName |
682
|
|
|
]; |
683
|
|
|
} |
684
|
|
|
$tableCols[] = ''; |
685
|
|
|
$this->table->class .= ' datamanagertable'; |
686
|
|
|
$this->table->setCols($tableCols); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
public function draw($params = [], $model = null) { |
690
|
|
|
if (!$this->predraw) { |
691
|
|
|
$this->preDraw($params, $model); |
692
|
|
|
} |
693
|
|
|
\App::$cur->view->widget('Ui\DataManager/DataManager', [ |
694
|
|
|
'dataManager' => $this, |
695
|
|
|
'model' => $model, |
696
|
|
|
'table' => $this->table, |
697
|
|
|
'params' => $params |
698
|
|
|
]); |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
public function drawCategorys() { |
702
|
|
|
if (!class_exists($this->modelName)) { |
703
|
|
|
return false; |
704
|
|
|
} |
705
|
|
View Code Duplication |
if (!$this->checkAccess()) { |
|
|
|
|
706
|
|
|
$this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->managerName . '"'); |
707
|
|
|
return []; |
708
|
|
|
} |
709
|
|
|
$tree = new Tree(); |
710
|
|
|
$tree->ul($this->managerOptions['categorys']['model'], 0, function($category) { |
711
|
|
|
$path = $category->tree_path . ($category->pk() ? $category->pk() . "/" : ''); |
712
|
|
|
$cleanClassName = str_replace('\\', '\\\\', get_class($category)); |
713
|
|
|
return "<a href='#' onclick='inji.Ui.dataManagers.get(this).switchCategory(this);return false;' data-index='{$category->index()}' data-path ='{$path}' data-id='{$category->pk()}' data-model='{$this->managerOptions['categorys']['model']}'> {$category->name}</a> |
714
|
|
|
|
715
|
|
|
<a href = '#' class ='glyphicon glyphicon-edit' onclick = 'inji.Ui.forms.popUp(\"{$cleanClassName}:{$category->pk()}\")'></a> |
716
|
|
|
<a href = '#' class ='glyphicon glyphicon-remove' onclick = 'inji.Ui.dataManagers.get(this).delCategory({$category->pk()});return false;'></a>"; |
717
|
|
|
}); |
718
|
|
|
?> |
719
|
|
|
<?php |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Draw error message |
724
|
|
|
* |
725
|
|
|
* @param string $errorText |
726
|
|
|
*/ |
727
|
|
|
public function drawError($errorText) { |
728
|
|
|
echo $errorText; |
729
|
|
|
} |
730
|
|
|
|
731
|
|
|
/** |
732
|
|
|
* Check access cur user to manager with name in param |
733
|
|
|
* |
734
|
|
|
* @return boolean |
735
|
|
|
*/ |
736
|
|
|
public function checkAccess() { |
737
|
|
|
if (\App::$cur->Access && !\App::$cur->Access->checkAccess($this)) { |
738
|
|
|
return false; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
View Code Duplication |
if (!empty($this->managerOptions['options']['access']['apps']) && !in_array(\App::$cur->name, $this->managerOptions['options']['access']['apps'])) { |
|
|
|
|
742
|
|
|
return false; |
743
|
|
|
} |
744
|
|
View Code Duplication |
if (!empty($this->managerOptions['options']['access']['groups']) && in_array(\Users\User::$cur->group_id, $this->managerOptions['options']['access']['groups'])) { |
|
|
|
|
745
|
|
|
return true; |
746
|
|
|
} |
747
|
|
|
if ($this->managerName == 'manager' && !\Users\User::$cur->isAdmin()) { |
748
|
|
|
return false; |
749
|
|
|
} |
750
|
|
|
return true; |
751
|
|
|
} |
752
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.