|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Data manager controller |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
class DataManagerController extends Controller |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
function indexAction() |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
$result = new Server\Result(); |
|
16
|
|
|
ob_start(); |
|
17
|
|
|
$params = []; |
|
18
|
|
|
if (!empty($_GET['params'])) { |
|
19
|
|
|
$params = $_GET['params']; |
|
20
|
|
|
} |
|
21
|
|
|
if (strpos($_GET['item'], ':')) { |
|
22
|
|
|
$raw = explode(':', $_GET['item']); |
|
23
|
|
|
$modelName = $raw[0]; |
|
24
|
|
|
$id = $raw[1]; |
|
25
|
|
|
$model = $modelName::get($id, $modelName::index(), $params); |
|
26
|
|
|
} else { |
|
27
|
|
|
$modelName = $_GET['item']; |
|
28
|
|
|
$id = null; |
|
|
|
|
|
|
29
|
|
|
$model = null; |
|
30
|
|
|
} |
|
31
|
|
|
if (!empty($_GET['params']['relation'])) { |
|
32
|
|
|
$params['relation'] = $_GET['params']['relation']; |
|
33
|
|
|
$relations = $modelName::relations(); |
|
34
|
|
|
$type = !empty($relations[$_GET['params']['relation']]['type']) ? $relations[$_GET['params']['relation']]['type'] : 'to'; |
|
35
|
|
|
|
|
36
|
|
|
switch ($type) { |
|
37
|
|
|
case 'relModel': |
|
38
|
|
|
$modelName = $relations[$_GET['params']['relation']]['relModel']; |
|
39
|
|
|
break; |
|
40
|
|
|
default: |
|
41
|
|
|
$modelName = $relations[$_GET['params']['relation']]['model']; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
$dataManager = new Ui\DataManager($modelName, 'manager'); |
|
45
|
|
|
$dataManager->draw($params, $model); |
|
46
|
|
|
$result->content = ob_get_contents(); |
|
47
|
|
|
ob_end_clean(); |
|
48
|
|
|
$result->send(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
//function |
|
52
|
|
|
|
|
53
|
|
|
function loadRowsAction() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$result = new Server\Result(); |
|
56
|
|
|
$result->content = []; |
|
57
|
|
|
ob_start(); |
|
58
|
|
|
$params = []; |
|
59
|
|
|
if (!empty($_GET['params'])) { |
|
60
|
|
|
$params = $_GET['params']; |
|
61
|
|
|
} |
|
62
|
|
View Code Duplication |
if (strpos($_GET['modelName'], ':')) { |
|
|
|
|
|
|
63
|
|
|
$raw = explode(':', $_GET['modelName']); |
|
64
|
|
|
$modelName = $raw[0]; |
|
65
|
|
|
$id = $raw[1]; |
|
66
|
|
|
$model = $modelName::get($id, $modelName::index(), $params); |
|
67
|
|
|
} else { |
|
68
|
|
|
$modelName = $_GET['modelName']; |
|
69
|
|
|
$id = null; |
|
|
|
|
|
|
70
|
|
|
$model = null; |
|
71
|
|
|
} |
|
72
|
|
|
if (!empty($_GET['params']['relation'])) { |
|
73
|
|
|
$params['relation'] = $_GET['params']['relation']; |
|
74
|
|
|
$relations = $modelName::relations(); |
|
75
|
|
|
$type = !empty($relations[$_GET['params']['relation']]['type']) ? $relations[$_GET['params']['relation']]['type'] : 'to'; |
|
76
|
|
|
|
|
77
|
|
|
switch ($type) { |
|
78
|
|
|
case 'relModel': |
|
79
|
|
|
$modelName = $relations[$_GET['params']['relation']]['relModel']; |
|
80
|
|
|
break; |
|
81
|
|
|
default: |
|
82
|
|
|
$modelName = $relations[$_GET['params']['relation']]['model']; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
if (!empty($_GET['filters'])) { |
|
86
|
|
|
$params['filters'] = $_GET['filters']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!empty($_GET['sortered'])) { |
|
90
|
|
|
$params['sortered'] = $_GET['sortered']; |
|
91
|
|
|
} |
|
92
|
|
|
if (!empty($_GET['mode'])) { |
|
93
|
|
|
$params['mode'] = $_GET['mode']; |
|
94
|
|
|
} |
|
95
|
|
|
if (!empty($_GET['all'])) { |
|
96
|
|
|
$params['all'] = true; |
|
97
|
|
|
} |
|
98
|
|
|
$dataManager = new Ui\DataManager($modelName, $_GET['managerName']); |
|
99
|
|
|
if (!empty($_GET['download'])) { |
|
100
|
|
|
$params['all'] = true; |
|
101
|
|
|
$params['download'] = true; |
|
102
|
|
|
set_time_limit(0); |
|
103
|
|
|
ob_end_clean(); |
|
104
|
|
|
header('Content-Encoding: UTF-8'); |
|
105
|
|
|
header("Content-Type: text/csv"); |
|
106
|
|
|
header("Content-Disposition: attachment; filename=" . $modelName::$objectName . '.csv'); |
|
107
|
|
|
echo "\xEF\xBB\xBF"; // UTF-8 BOM |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$cols = $dataManager->getCols(); |
|
111
|
|
|
$cols = array_slice($cols, (!empty($dataManager->managerOptions['groupActions']) ? 1 : 0)); |
|
112
|
|
|
$endRow = true; |
|
113
|
|
|
foreach ($cols as $colName => $options) { |
|
114
|
|
|
if (!$endRow) { |
|
115
|
|
|
echo ";"; |
|
116
|
|
|
} |
|
117
|
|
|
$endRow = false; |
|
118
|
|
|
echo '"' . $options['label'] . '"'; |
|
119
|
|
|
} |
|
120
|
|
|
echo "\n"; |
|
121
|
|
|
$endRow = true; |
|
122
|
|
|
} |
|
123
|
|
|
$rows = $dataManager->getRows($params, $model); |
|
124
|
|
|
foreach ($rows as $row) { |
|
125
|
|
|
if (!empty($_GET['download'])) { |
|
126
|
|
|
$row = array_slice($row, (!empty($dataManager->managerOptions['groupActions']) ? 1 : 0), -1); |
|
127
|
|
|
foreach ($row as $col) { |
|
128
|
|
|
if (!$endRow) { |
|
|
|
|
|
|
129
|
|
|
echo ";"; |
|
130
|
|
|
} |
|
131
|
|
|
$endRow = false; |
|
132
|
|
|
echo '"' . str_replace("\n", '', $col) . '"'; |
|
133
|
|
|
} |
|
134
|
|
|
echo "\n"; |
|
135
|
|
|
$endRow = true; |
|
136
|
|
|
} else { |
|
137
|
|
|
Ui\Table::drawRow($row); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
if (!empty($_GET['download'])) { |
|
141
|
|
|
exit(); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
$result->content['rows'] = ob_get_contents(); |
|
144
|
|
|
ob_clean(); |
|
145
|
|
|
$result->content['pages'] = ''; |
|
146
|
|
|
if (empty($params['all'])) { |
|
147
|
|
|
$pages = $dataManager->getPages($params, $model); |
|
148
|
|
|
|
|
149
|
|
|
if ($pages) { |
|
150
|
|
|
$pages->draw(); |
|
151
|
|
|
} |
|
152
|
|
|
$result->content['pages'] = ob_get_contents(); |
|
153
|
|
|
ob_end_clean(); |
|
154
|
|
|
} |
|
155
|
|
|
$result->send(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
function loadCategorysAction() |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
$result = new Server\Result(); |
|
161
|
|
|
ob_start(); |
|
162
|
|
View Code Duplication |
if (strpos($_GET['modelName'], ':')) { |
|
|
|
|
|
|
163
|
|
|
$raw = explode(':', $_GET['modelName']); |
|
164
|
|
|
$modelName = $raw[0]; |
|
165
|
|
|
$id = $raw[1]; |
|
166
|
|
|
$model = $modelName::get($id); |
|
|
|
|
|
|
167
|
|
|
} else { |
|
168
|
|
|
$modelName = $_GET['modelName']; |
|
169
|
|
|
$id = null; |
|
|
|
|
|
|
170
|
|
|
$model = null; |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
if (!empty($_GET['params'])) { |
|
173
|
|
|
$params = $_GET['params']; |
|
174
|
|
|
if (!empty($params['relation'])) { |
|
175
|
|
|
$relations = $modelName::relations(); |
|
176
|
|
|
$modelName = $relations[$params['relation']]['model']; |
|
177
|
|
|
} |
|
178
|
|
|
} else { |
|
179
|
|
|
$params = []; |
|
180
|
|
|
} |
|
181
|
|
|
if (!empty($_GET['filters'])) { |
|
182
|
|
|
$params['filters'] = $_GET['filters']; |
|
183
|
|
|
} |
|
184
|
|
|
$dataManager = new Ui\DataManager($modelName, $_GET['managerName']); |
|
185
|
|
|
$dataManager->drawCategorys(); |
|
186
|
|
|
$result->content = ob_get_contents(); |
|
187
|
|
|
ob_end_clean(); |
|
188
|
|
|
$result->send(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
function delRowAction() |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
|
|
194
|
|
View Code Duplication |
if (strpos($_GET['modelName'], ':')) { |
|
|
|
|
|
|
195
|
|
|
$raw = explode(':', $_GET['modelName']); |
|
196
|
|
|
$modelName = $raw[0]; |
|
197
|
|
|
$id = $raw[1]; |
|
198
|
|
|
$model = $modelName::get($id); |
|
|
|
|
|
|
199
|
|
|
} else { |
|
200
|
|
|
$modelName = $_GET['modelName']; |
|
201
|
|
|
$id = null; |
|
|
|
|
|
|
202
|
|
|
$model = null; |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
View Code Duplication |
if (!empty($_GET['params'])) { |
|
|
|
|
|
|
205
|
|
|
$params = $_GET['params']; |
|
206
|
|
|
if (!empty($params['relation'])) { |
|
207
|
|
|
$relations = $modelName::relations(); |
|
208
|
|
|
|
|
209
|
|
|
$type = !empty($relations[$_GET['params']['relation']]['type']) ? $relations[$_GET['params']['relation']]['type'] : 'to'; |
|
210
|
|
|
|
|
211
|
|
|
switch ($type) { |
|
212
|
|
|
case 'relModel': |
|
213
|
|
|
$modelName = $relations[$_GET['params']['relation']]['relModel']; |
|
214
|
|
|
break; |
|
215
|
|
|
default: |
|
216
|
|
|
$modelName = $relations[$_GET['params']['relation']]['model']; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} else { |
|
220
|
|
|
$params = []; |
|
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
$dataManager = new Ui\DataManager($modelName, $_GET['managerName']); |
|
223
|
|
|
if ($dataManager->checkAccess()) { |
|
224
|
|
|
$model = $modelName::get($_GET['key'], $modelName::index(), !empty($_GET['params']) ? $_GET['params'] : []); |
|
225
|
|
|
if ($model) { |
|
226
|
|
|
$model->delete(!empty($_GET['params']) ? $_GET['params'] : []); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
$result = new Server\Result(); |
|
230
|
|
|
$result->successMsg = empty($_GET['silence']) ? 'Запись удалена' : ''; |
|
231
|
|
|
$result->send(); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
function updateRowAction() |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
|
|
237
|
|
View Code Duplication |
if (strpos($_GET['modelName'], ':')) { |
|
|
|
|
|
|
238
|
|
|
$raw = explode(':', $_GET['modelName']); |
|
239
|
|
|
$modelName = $raw[0]; |
|
240
|
|
|
$id = $raw[1]; |
|
241
|
|
|
$model = $modelName::get($id); |
|
|
|
|
|
|
242
|
|
|
} else { |
|
243
|
|
|
$modelName = $_GET['modelName']; |
|
244
|
|
|
$id = null; |
|
|
|
|
|
|
245
|
|
|
$model = null; |
|
|
|
|
|
|
246
|
|
|
} |
|
247
|
|
|
$params = []; |
|
248
|
|
View Code Duplication |
if (!empty($_GET['params'])) { |
|
|
|
|
|
|
249
|
|
|
$params = $_GET['params']; |
|
250
|
|
|
if (!empty($_GET['params']['relation'])) { |
|
251
|
|
|
$relations = $modelName::relations(); |
|
252
|
|
|
$type = !empty($relations[$_GET['params']['relation']]['type']) ? $relations[$_GET['params']['relation']]['type'] : 'to'; |
|
253
|
|
|
|
|
254
|
|
|
switch ($type) { |
|
255
|
|
|
case 'relModel': |
|
256
|
|
|
$modelName = $relations[$_GET['params']['relation']]['relModel']; |
|
257
|
|
|
break; |
|
258
|
|
|
default: |
|
259
|
|
|
$modelName = $relations[$_GET['params']['relation']]['model']; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
$dataManager = new Ui\DataManager($modelName, $_GET['managerName']); |
|
264
|
|
|
if ($dataManager->checkAccess()) { |
|
265
|
|
|
$model = $modelName::get($_GET['key'], $modelName::index(), !empty($_GET['params']) ? $_GET['params'] : []); |
|
266
|
|
|
if ($model) { |
|
267
|
|
|
$model->$_GET['col'] = $_GET['col_value']; |
|
268
|
|
|
$model->save($params); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
$result = new Server\Result(); |
|
272
|
|
|
$result->successMsg = empty($_GET['silence']) ? 'Запись Обновлена' : ''; |
|
273
|
|
|
$result->send(); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
function groupActionAction() |
|
|
|
|
|
|
277
|
|
|
{ |
|
278
|
|
|
|
|
279
|
|
|
|
|
280
|
|
View Code Duplication |
if (strpos($_GET['modelName'], ':')) { |
|
|
|
|
|
|
281
|
|
|
$raw = explode(':', $_GET['modelName']); |
|
282
|
|
|
$modelName = $raw[0]; |
|
283
|
|
|
$id = $raw[1]; |
|
284
|
|
|
$model = $modelName::get($id); |
|
285
|
|
|
} else { |
|
286
|
|
|
$modelName = $_GET['modelName']; |
|
287
|
|
|
$id = null; |
|
|
|
|
|
|
288
|
|
|
$model = null; |
|
289
|
|
|
} |
|
290
|
|
View Code Duplication |
if (!empty($_GET['params'])) { |
|
|
|
|
|
|
291
|
|
|
$params = $_GET['params']; |
|
292
|
|
|
if (!empty($params['relation'])) { |
|
293
|
|
|
$relations = $modelName::relations(); |
|
294
|
|
|
|
|
295
|
|
|
$type = !empty($relations[$_GET['params']['relation']]['type']) ? $relations[$_GET['params']['relation']]['type'] : 'to'; |
|
296
|
|
|
|
|
297
|
|
|
switch ($type) { |
|
298
|
|
|
case 'relModel': |
|
299
|
|
|
$modelName = $relations[$_GET['params']['relation']]['relModel']; |
|
300
|
|
|
break; |
|
301
|
|
|
default: |
|
302
|
|
|
$modelName = $relations[$_GET['params']['relation']]['model']; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
} else { |
|
306
|
|
|
$params = []; |
|
|
|
|
|
|
307
|
|
|
} |
|
308
|
|
|
$dataManager = new Ui\DataManager($modelName, $_GET['managerName']); |
|
309
|
|
|
if ($dataManager->checkAccess()) { |
|
310
|
|
|
if (!empty($_GET['action']) && !empty($dataManager->managerOptions['groupActions'][$_GET['action']]) && !empty($_GET['ids'])) { |
|
311
|
|
|
$action = $dataManager->managerOptions['groupActions'][$_GET['action']]; |
|
312
|
|
|
switch ($action['action']) { |
|
313
|
|
|
case'delete': |
|
|
|
|
|
|
314
|
|
|
$ids = filter_input(INPUT_GET, 'ids', FILTER_SANITIZE_STRING); |
|
315
|
|
|
if ($ids) { |
|
316
|
|
|
$ids = trim($ids, ','); |
|
317
|
|
|
$models = $modelName::getList(['where' => [[$modelName::index(), $ids, 'IN']]]); |
|
318
|
|
|
foreach ($models as $model) { |
|
319
|
|
|
$model->delete(); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
break; |
|
323
|
|
|
case 'changeParam': |
|
324
|
|
|
$ids = filter_input(INPUT_GET, 'ids', FILTER_SANITIZE_STRING); |
|
325
|
|
|
if ($ids) { |
|
326
|
|
|
$ids = trim($ids, ','); |
|
327
|
|
|
$models = $modelName::getList(['where' => [[$modelName::index(), $ids, 'IN']]]); |
|
328
|
|
|
foreach ($models as $model) { |
|
329
|
|
|
$model->{$action['col']} = $action['value']; |
|
330
|
|
|
$model->save(!empty($_GET['params']) ? $_GET['params'] : []); |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
break; |
|
334
|
|
|
case 'moduleMethod': |
|
335
|
|
|
$ids = filter_input(INPUT_GET, 'ids', FILTER_SANITIZE_STRING); |
|
336
|
|
|
if ($ids) { |
|
337
|
|
|
$comandResult = App::$cur->$action['module']->$action['method']($dataManager, trim($ids, ','), !empty($_GET['adInfo']) ? $_GET['adInfo'] : []); |
|
338
|
|
|
} |
|
339
|
|
|
break; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
$result = new Server\Result(); |
|
344
|
|
|
if (!empty($comandResult)) { |
|
345
|
|
|
$result->success = $comandResult['success']; |
|
346
|
|
|
$result->content = $comandResult['content']; |
|
347
|
|
|
} |
|
348
|
|
|
$result->successMsg = 'Операция выполнена'; |
|
349
|
|
|
$result->send(); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
function delCategoryAction() |
|
|
|
|
|
|
353
|
|
|
{ |
|
354
|
|
|
|
|
355
|
|
|
$dataManager = new Ui\DataManager($_GET['modelName'], $_GET['managerName']); |
|
356
|
|
|
if ($dataManager->checkAccess() && !empty($dataManager->managerOptions['categorys'])) { |
|
357
|
|
|
$categoryModel = $dataManager->managerOptions['categorys']['model']; |
|
358
|
|
|
$model = $categoryModel::get($_GET['key'], $categoryModel::index(), !empty($_GET['params']) ? $_GET['params'] : []); |
|
359
|
|
|
if ($model) { |
|
360
|
|
|
$model->delete(!empty($_GET['params']) ? $_GET['params'] : []); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
$result = new Server\Result(); |
|
364
|
|
|
$result->send(); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
} |
|
368
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.