Completed
Push — master ( f81666...55a9f3 )
by Alexey
05:42
created

DataManager::drawError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 $modelName
31
     * @param string|array $dataManager
32
     * @throws Exception
33
     */
34
    public function __construct($modelName, $dataManager = 'manager')
35
    {
36
        if (!class_exists($modelName)) {
37
            throw new \Exception("model {$modelName} not exists");
38
        }
39
40
        $this->modelName = $modelName;
41
42
        if (is_string($dataManager)) {
43
            $this->managerName = $dataManager;
44
            $dataManager = !empty($modelName::$dataManagers[$dataManager]) ? $modelName::$dataManagers[$dataManager] : [];
45
        }
46
        $this->managerOptions = $dataManager;
47
48
        if (!$this->managerOptions || !is_array($this->managerOptions)) {
49
            throw new \Exception('empty DataManager');
50
        }
51
52
        if (!empty($this->managerOptions['name'])) {
53
            $this->name = $this->managerOptions['name'];
54
        } elseif ($modelName && isset($modelName::$objectName)) {
55
            $this->name = $modelName::$objectName;
56
        } else {
57
            $this->name = $modelName;
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
    {
71
        $modelName = $this->modelName;
72
        $formParams = [
73
            'dataManagerParams' => $params
74
        ];
75
        if ($model) {
76
            $formModelName = get_class($model);
77
            $relations = $formModelName::relations();
78
            $type = !empty($relations[$params['relation']]['type']) ? $relations[$params['relation']]['type'] : 'to';
79
            switch ($type) {
80
                case 'relModel':
81
                    $formParams['preset'] = [
82
                        $formModelName::index() => $model->pk()
83
                    ];
84
                    break;
85
                default:
86
                    $formParams['preset'] = [
87
                        $relations[$params['relation']]['col'] => $model->pk()
88
                    ];
89
            }
90
        }
91
92
        $buttons = [];
93
        if (!empty($this->managerOptions['sortMode'])) {
94
            $buttons[] = [
95
                'class' => 'modeBtn',
96
                'data-mode' => 'sort',
97
                'text' => 'Сортировать',
98
            ];
99
        }
100
        if (!empty($this->managerOptions['filters'])) {
101
            $buttons[] = [
102
                'text' => 'Фильтры',
103
                'onclick' => '  var modal = $("#' . $this->managerId . '_filters");
104
                modal.modal("show");',
105
            ];
106
        }
107
        $buttons[] = [
108
            'text' => 'Добавить элемент',
109
            'onclick' => 'inji.Ui.forms.popUp("' . str_replace('\\', '\\\\', $modelName) . '",' . json_encode($formParams) . ')',
110
        ];
111
112
        return $buttons;
113
    }
114
115
    function getActions()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
    {
117
        $actions = [
118
            'Open', 'Edit', 'Delete'
119
        ];
120
        if (isset($this->managerOptions['actions'])) {
121
            $actions = $this->managerOptions['actions'];
122
        }
123
        $return = [];
124
        foreach ($actions as $key => $action) {
125
            if (is_array($action)) {
126
                $return[$key] = $action;
127
            } else {
128
                $key = $action;
129
                $return[$key] = [
130
                    'className' => $action
131
                ];
132
            }
133
            $return[$key]['className'] = strpos($return[$key]['className'], '\\') === false && class_exists('Ui\DataManager\Action\\' . $return[$key]['className']) ? 'Ui\DataManager\Action\\' . $return[$key]['className'] : $action;
134
        }
135
        return $return;
136
    }
137
138
    /**
139
     * Get cols for manager
140
     * 
141
     * @return string
142
     */
143
    public function getCols()
144
    {
145
        $modelName = $this->modelName;
146
        if (!class_exists($modelName)) {
147
            return [];
148
        }
149
        $modelName = $this->modelName;
150
        $cols = [];
151
        $actions = $this->getActions();
152
        ob_start();
153
        ?>
154
        <div class="dropdown">
155
          <a id="dLabel" data-target="#" href="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
156
            <i class="glyphicon glyphicon-cog"></i>
157
            <span class="caret"></span>
158
          </a>
159
160
          <ul class="dropdown-menu" aria-labelledby="dLabel">
161
            <li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("selectAll");return false;'>Выделить все</a></li>
162
            <li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("unSelectAll");return false;'>Снять все</a></li>
163
            <li><a href ='' onclick='inji.Ui.dataManagers.get(this).rowSelection("inverse");return false;'>Инвертировать</a></li>
164
            <li role="separator" class="divider"></li>
165
            <?php
166
            foreach ($actions as $action => $actionParams) {
167
                if (class_exists($actionParams['className']) && $actionParams['className']::$groupAction) {
168
                    echo "<li><a href ='' onclick='inji.Ui.dataManagers.get(this).groupAction(\"" . str_replace('\\', '\\\\', $action) . "\");return false;'>{$actionParams['className']::$name}</a></li>";
169
                }
170
            }
171
            ?>
172
          </ul>
173
        </div>
174
        <?php
175
        $dropdown = ob_get_contents();
176
        ob_end_clean();
177
        $cols[] = ['label' => $dropdown];
178
179
        $cols['id'] = ['label' => '№', 'sortable' => true];
180
        foreach ($this->managerOptions['cols'] as $key => $col) {
181
            if (is_array($col)) {
182
                $colName = $key;
183
                $colOptions = $col;
184
            } else {
185
                $colName = $col;
186
                $colOptions = [];
187
            }
188
            $colInfo = [];
189
            if ($modelName) {
190
                $colInfo = $modelName::getColInfo($colName);
191
            }
192
            if (empty($colOptions['label']) && !empty($colInfo['label'])) {
193
                $colOptions['label'] = $colInfo['label'];
194
            } elseif (empty($colOptions['label'])) {
195
                $colOptions['label'] = $colName;
196
            }
197
            $cols[$colName] = $colOptions;
198
        }
199
        return $cols;
200
    }
201
202
    /**
203
     * Get rows for manager
204
     * 
205
     * @param array $params
206
     * @param object $model
207
     * @return type
208
     */
209
    public function getRows($params = [], $model = null)
210
    {
211
        $modelName = $this->modelName;
212
        if (!class_exists($modelName)) {
213
            return [];
214
        }
215 View Code Duplication
        if (!$this->checkAccess()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
            $this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->managerName . '"');
217
            return [];
218
        }
219
        $modelName = $this->modelName;
220
        $queryParams = [];
221
        if (empty($params['all'])) {
222
            if (!empty($params['limit'])) {
223
                $this->limit = (int) $params['limit'];
224
            }
225
            if (!empty($params['page'])) {
226
                $this->page = (int) $params['page'];
227
            }
228
            $queryParams['limit'] = $this->limit;
229
            $queryParams['start'] = $this->page * $this->limit - $this->limit;
230
        }
231 View Code Duplication
        if (!empty($params['categoryPath']) && $modelName::$categoryModel) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
            $queryParams['where'][] = ['tree_path', $params['categoryPath'] . '%', 'LIKE'];
233
        }
234
        if (!empty($params['appType'])) {
235
            $queryParams['appType'] = $params['appType'];
236
        }
237
        if ($this->joins) {
238
            $queryParams['joins'] = $this->joins;
239
        }
240 View Code Duplication
        if (!empty($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
            foreach ($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'] as $colName => $colOptions) {
242
                if (!empty($colOptions['userCol'])) {
243
                    if (strpos($colOptions['userCol'], ':')) {
244
                        $rel = substr($colOptions['userCol'], 0, strpos($colOptions['userCol'], ':'));
245
                        $param = substr($colOptions['userCol'], strpos($colOptions['userCol'], ':') + 1);
246
                        $queryParams['where'][] = [$colName, \Users\User::$cur->$rel->$param];
247
                    }
248
                } elseif (isset($colOptions['value'])) {
249
                    $queryParams['where'][] = [$colName, $colOptions['value']];
250
                }
251
            }
252
        }
253
        if (!empty($this->managerOptions['filters'])) {
254
            foreach ($this->managerOptions['filters'] as $col) {
255
                $colInfo = $modelName::getColInfo($col);
256
                switch ($colInfo['colParams']['type']) {
257 View Code Duplication
                    case 'select':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
                        if (empty($params['filters'][$col]['value'])) {
259
                            continue;
260
                        }
261
                        foreach ($params['filters'][$col]['value'] as $key => $value) {
262
                            if ($value === '') {
263
                                unset($params['filters'][$col]['value'][$key]);
264
                            }
265
                        }
266
                        if (!$params['filters'][$col]['value']) {
267
                            continue;
268
                        }
269
                        $queryParams['where'][] = [$col, implode(',', $params['filters'][$col]['value']), 'IN'];
270
                        break;
271
                    case 'bool':
272
273
                        if (!isset($params['filters'][$col]['value']) || $params['filters'][$col]['value'] === '') {
274
                            continue;
275
                        }
276
                        $queryParams['where'][] = [$col, $params['filters'][$col]['value']];
277
                        break;
278
                    case 'dateTime':
279 View Code Duplication
                    case 'date':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
                        if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) {
281
                            continue;
282
                        }
283
                        if (!empty($params['filters'][$col]['min'])) {
284
                            $queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>='];
285
                        }
286
                        if (!empty($params['filters'][$col]['max'])) {
287
                            if ($colInfo['colParams']['type'] == 'dateTime' && !strpos($params['filters'][$col]['max'], ' ')) {
288
289
                                $date = $params['filters'][$col]['max'] . ' 23:59:59';
290
                            } else {
291
                                $date = $params['filters'][$col]['max'];
292
                            }
293
                            $queryParams['where'][] = [$col, $date, '<='];
294
                        }
295
                        break;
296 View Code Duplication
                    case 'number':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
                        if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) {
298
                            continue;
299
                        }
300
                        if (!empty($params['filters'][$col]['min'])) {
301
                            $queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>='];
302
                        }
303
                        if (!empty($params['filters'][$col]['max'])) {
304
                            $queryParams['where'][] = [$col, $params['filters'][$col]['max'], '<='];
305
                        }
306
                        break;
307
                    case 'email':
308
                    case 'text':
309
                    case 'textarea':
310 View Code Duplication
                    case 'html':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
311
                        if (empty($params['filters'][$col]['value'])) {
312
                            continue;
313
                        }
314
                        switch ($params['filters'][$col]['compareType']) {
315
                            case 'contains':
316
                                $queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'] . '%', 'LIKE'];
317
                                break;
318
                            case 'equals':
319
                                $queryParams['where'][] = [$col, $params['filters'][$col]['value']];
320
                                break;
321
                            case 'starts_with':
322
                                $queryParams['where'][] = [$col, $params['filters'][$col]['value'] . '%', 'LIKE'];
323
                                break;
324
                            case 'ends_with':
325
                                $queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'], 'LIKE'];
326
                                break;
327
                        }
328
                        break;
329
                }
330
            }
331
        }
332
        if (!empty($params['mode']) && $params['mode'] == 'sort') {
333
            $queryParams['order'] = ['weight', 'asc'];
334
        } elseif (!empty($params['sortered']) && !empty($this->managerOptions['sortable'])) {
335
            foreach ($params['sortered'] as $key => $sortType) {
336
                $keys = array_keys($this->managerOptions['cols']);
337
                $colName = '';
338
                if (isset($keys[$key])) {
339
                    if (is_array($this->managerOptions['cols'][$keys[$key]])) {
340
                        $colName = $keys[$key];
341
                    } else {
342
                        $colName = $this->managerOptions['cols'][$keys[$key]];
343
                    }
344
                }
345
                if ($colName && in_array($colName, $this->managerOptions['sortable'])) {
346
                    $sortType = in_array($sortType, ['desc', 'asc']) ? $sortType : 'desc';
347
                    $queryParams['order'][] = [$colName, $sortType];
348
                }
349
            }
350
        }
351
        if ($model && !empty($params['relation'])) {
352
            $relation = $model::getRelation($params['relation']);
353
            $items = $model->$params['relation']($queryParams);
354
        } else {
355
            $relation = false;
356
            $items = $modelName::getList($queryParams);
357
        }
358
        $rows = [];
359
        foreach ($items as $item) {
360
            if ($relation && !empty($relation['relModel'])) {
361
                $item = $relation['relModel']::get([[$item->index(), $item->id], [$model->index(), $model->id]]);
362
            }
363
            $row = [];
364
            $row[] = '<input type ="checkbox" name = "pk[]" value =' . $item->pk() . '>';
365
            $row[] = $item->pk();
366
            foreach ($this->managerOptions['cols'] as $key => $colName) {
367
                if (!empty($params['download'])) {
368
                    $row[] = \Model::getColValue($item, is_array($colName) ? $key : $colName, true, false);
369
                } else {
370
                    $row[] = DataManager::drawCol($item, is_array($colName) ? $key : $colName, $params, $this);
371
                }
372
            }
373
            $row[] = $this->rowButtons($item, $params);
374
            $rows[] = $row;
375
        }
376
        return $rows;
377
    }
378
379
    public static function drawCol($item, $colName, $params = [], $dataManager = null, $originalCol = '', $originalItem = null)
1 ignored issue
show
Coding Style introduced by
drawCol uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
380
    {
381
        $modelName = get_class($item);
382
        if (!class_exists($modelName)) {
383
            return false;
384
        }
385
386
        if (!$originalCol) {
387
            $originalCol = $colName;
388
        }
389
        if (!$originalItem) {
390
            $originalItem = $item;
391
        }
392
393
        $relations = $modelName::relations();
394
        if (strpos($colName, ':') !== false && !empty($relations[substr($colName, 0, strpos($colName, ':'))])) {
395
            $rel = substr($colName, 0, strpos($colName, ':'));
396
            $col = substr($colName, strpos($colName, ':') + 1);
397
            if ($item->$rel) {
398
                return DataManager::drawCol($item->$rel, $col, $params, $dataManager, $originalCol, $originalItem);
399
            } else {
400
                return 'Не указано';
401
            }
402
        }
403
        if (!empty($modelName::$cols[$colName]['relation'])) {
404
            $type = !empty($relations[$modelName::$cols[$colName]['relation']]['type']) ? $relations[$modelName::$cols[$colName]['relation']]['type'] : 'to';
405
            switch ($type) {
406 View Code Duplication
                case 'relModel':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
407
                    $managerParams = ['relation' => $modelName::$cols[$colName]['relation']];
408
                    $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1]));
409
                    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>";
410 View Code Duplication
                case 'many':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
411
                    $managerParams = ['relation' => $modelName::$cols[$colName]['relation']];
412
                    $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1]));
413
                    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>";
414
                default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
415
                    if ($item->{$modelName::$cols[$colName]['relation']}) {
416
                        if (\App::$cur->name == 'admin') {
417
                            $href = "<a href ='/admin/" . str_replace('\\', '/view/', $relations[$modelName::$cols[$colName]['relation']]['model']) . "/" . $item->{$modelName::$cols[$colName]['relation']}->pk() . "'>";
418
                            if (!empty($modelName::$cols[$colName]['showCol'])) {
419
                                $href .= $item->{$modelName::$cols[$colName]['relation']}->{$modelName::$cols[$colName]['showCol']};
420
                            } else {
421
422
                                $href .= $item->{$modelName::$cols[$colName]['relation']}->name();
423
                            }
424
                            $href .= '</a>';
425
                            return $href;
426
                        } else {
427
                            return $item->{$modelName::$cols[$colName]['relation']}->name();
428
                        }
429
                    } else {
430
                        return $item->$colName;
431
                    }
432
            }
433
        } else {
434
            if (!empty($modelName::$cols[$colName]['view']['type'])) {
435
                switch ($modelName::$cols[$colName]['view']['type']) {
436
                    case 'widget':
437
                        ob_start();
438
                        \App::$cur->view->widget($modelName::$cols[$colName]['view']['widget'], ['item' => $item, 'colName' => $colName, 'colParams' => $modelName::$cols[$colName]]);
439
                        $content = ob_get_contents();
440
                        ob_end_clean();
441
                        return $content;
442
                    case 'moduleMethod':
443
                        return \App::$cur->{$modelName::$cols[$colName]['view']['module']}->{$modelName::$cols[$colName]['view']['method']}($item, $colName, $modelName::$cols[$colName]);
444 View Code Duplication
                    case'many':
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
445
                        $managerParams = ['relation' => $modelName::$cols[$colName]['relation']];
446
                        $count = $item->{$modelName::$cols[$colName]['relation']}(array_merge($params, ['count' => 1]));
447
                        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>";
448
                    default:
449
                        return $item->$colName;
450
                }
451
            } elseif (!empty($modelName::$cols[$colName]['type'])) {
452
                if (\App::$cur->name == 'admin' && $originalCol == 'name' || ( $dataManager && !empty($dataManager->managerOptions['colToView']) && $dataManager->managerOptions['colToView'] == $originalCol)) {
453
                    $formName = $dataManager && !empty($dataManager->managerOptions['editForm']) ? $dataManager->managerOptions['editForm'] : 'manager';
454
                    $redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem));
455
                    return "<a href ='/admin/" . str_replace('\\', '/view/', get_class($originalItem)) . "/{$originalItem->id}?formName={$formName}&redirectUrl={$redirectUrl}'>{$item->$colName}</a>";
456
                } elseif (\App::$cur->name == 'admin' && $colName == 'name') {
457
                    $redirectUrl = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/admin/' . str_replace('\\', '/', get_class($originalItem));
458
                    return "<a href ='/admin/" . str_replace('\\', '/view/', get_class($item)) . "/{$item->id}?redirectUrl={$redirectUrl}'>{$item->$colName}</a>";
459
                } else {
460
                    return \Model::resloveTypeValue($item, $colName);
461
                }
462
            } else {
463
                return $item->$colName;
464
            }
465
        }
466
    }
467
468
    public function rowButtons($item, $params)
469
    {
470
        $modelName = $this->modelName;
471
        if (!class_exists($modelName)) {
472
            return false;
473
        }
474
        ob_start();
475
        $widgetName = !empty($this->managerOptions['rowButtonsWidget']) ? $this->managerOptions['rowButtonsWidget'] : 'Ui\DataManager/rowButtons';
476
        \App::$cur->view->widget($widgetName, [
477
            'dataManager' => $this,
478
            'item' => $item,
479
            'params' => $params
480
        ]);
481
        $buttons = ob_get_contents();
482
        ob_end_clean();
483
        return $buttons;
484
    }
485
486
    public function getPages($params = [], $model = null)
487
    {
488
        $modelName = $this->modelName;
489
        if (!class_exists($modelName)) {
490
            return [];
491
        }
492 View Code Duplication
        if (!$this->checkAccess()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
493
            $this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->managerName . '"');
494
            return [];
495
        }
496
        if (!empty($params['limit'])) {
497
            $this->limit = (int) $params['limit'];
498
        }
499
        if (!empty($params['page'])) {
500
            $this->page = (int) $params['page'];
501
        }
502
        $queryParams = [
503
            'count' => true
504
        ];
505
        $modelName = $this->modelName;
506 View Code Duplication
        if (!empty($params['categoryPath']) && $modelName::$categoryModel) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
507
            $queryParams['where'][] = ['tree_path', $params['categoryPath'] . '%', 'LIKE'];
508
        }
509 View Code Duplication
        if (!empty($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
510
            foreach ($this->managerOptions['userGroupFilter'][\Users\User::$cur->group_id]['getRows'] as $colName => $colOptions) {
511
                if (!empty($colOptions['userCol'])) {
512
                    if (strpos($colOptions['userCol'], ':')) {
513
                        $rel = substr($colOptions['userCol'], 0, strpos($colOptions['userCol'], ':'));
514
                        $param = substr($colOptions['userCol'], strpos($colOptions['userCol'], ':') + 1);
515
                        $queryParams['where'][] = [$colName, \Users\User::$cur->$rel->$param];
516
                    }
517
                } elseif (isset($colOptions['value'])) {
518
                    $queryParams['where'][] = [$colName, $colOptions['value']];
519
                }
520
            }
521
        }
522
        $modelName = $this->modelName;
523
        if (!empty($this->managerOptions['filters'])) {
524
            foreach ($this->managerOptions['filters'] as $col) {
525
                $colInfo = $modelName::getColInfo($col);
526
                switch ($colInfo['colParams']['type']) {
527 View Code Duplication
                    case 'select':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
528
                        if (empty($params['filters'][$col]['value'])) {
529
                            continue;
530
                        }
531
                        foreach ($params['filters'][$col]['value'] as $key => $value) {
532
                            if ($value === '') {
533
                                unset($params['filters'][$col]['value'][$key]);
534
                            }
535
                        }
536
                        if (!$params['filters'][$col]['value']) {
537
                            continue;
538
                        }
539
                        $queryParams['where'][] = [$col, implode(',', $params['filters'][$col]['value']), 'IN'];
540
                        break;
541
                    case 'bool':
542
543
                        if (empty($params['filters'][$col]['value'])) {
544
                            continue;
545
                        }
546
                        $queryParams['where'][] = [$col, '1'];
547
                        break;
548
                    case 'dateTime':
549 View Code Duplication
                    case 'date':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
550
                        if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) {
551
                            continue;
552
                        }
553
                        if (!empty($params['filters'][$col]['min'])) {
554
                            $queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>='];
555
                        }
556
                        if (!empty($params['filters'][$col]['max'])) {
557
                            if ($colInfo['colParams']['type'] == 'dateTime' && !strpos($params['filters'][$col]['max'], ' ')) {
558
559
                                $date = $params['filters'][$col]['max'] . ' 23:59:59';
560
                            } else {
561
                                $date = $params['filters'][$col]['max'];
562
                            }
563
                            $queryParams['where'][] = [$col, $date, '<='];
564
                        }
565
                        break;
566 View Code Duplication
                    case 'number':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
567
                        if (empty($params['filters'][$col]['min']) && empty($params['filters'][$col]['max'])) {
568
                            continue;
569
                        }
570
                        if (!empty($params['filters'][$col]['min'])) {
571
                            $queryParams['where'][] = [$col, $params['filters'][$col]['min'], '>='];
572
                        }
573
                        if (!empty($params['filters'][$col]['max'])) {
574
                            $queryParams['where'][] = [$col, $params['filters'][$col]['max'], '<='];
575
                        }
576
                        break;
577
                    case 'email':
578
                    case 'text':
579
                    case 'textarea':
580 View Code Duplication
                    case 'html':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
581
                        if (empty($params['filters'][$col]['value'])) {
582
                            continue;
583
                        }
584
                        switch ($params['filters'][$col]['compareType']) {
585
                            case 'contains':
586
                                $queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'] . '%', 'LIKE'];
587
                                break;
588
                            case 'equals':
589
                                $queryParams['where'][] = [$col, $params['filters'][$col]['value']];
590
                                break;
591
                            case 'starts_with':
592
                                $queryParams['where'][] = [$col, $params['filters'][$col]['value'] . '%', 'LIKE'];
593
                                break;
594
                            case 'ends_with':
595
                                $queryParams['where'][] = [$col, '%' . $params['filters'][$col]['value'], 'LIKE'];
596
                                break;
597
                        }
598
                        break;
599
                }
600
            }
601
        }
602
        if ($model && !empty($params['relation'])) {
603
            $count = $model->$params['relation']($queryParams);
604
        } else {
605
            $count = $modelName::getCount($queryParams);
606
        }
607
        $pages = new Pages([
608
            'limit' => $this->limit,
609
            'page' => $this->page,
610
                ], [
611
            'count' => $count,
612
            'dataManager' => $this
613
        ]);
614
        return $pages;
615
    }
616
617
    public function preDraw($params = [], $model = null)
618
    {
619
        if (!class_exists($this->modelName)) {
620
            return false;
621
        }
622
        $this->predraw = true;
623
624
        $cols = $this->getCols();
625
626
        $this->table = new Table();
627
        $tableCols = [];
628
        foreach ($cols as $colName => $colOptions) {
629
            $tableCols[] = !empty($colOptions['label']) ? $colOptions['label'] : $colName;
630
        }
631
        $tableCols[] = '';
632
        $this->table->setCols($tableCols);
633
    }
634
635
    public function draw($params = [], $model = null)
636
    {
637
        if (!class_exists($this->modelName)) {
638
            return false;
639
        }
640
        if (!$this->predraw) {
641
            $this->preDraw($params, $model);
642
        }
643
        \App::$cur->view->widget('Ui\DataManager/DataManager', [
644
            'dataManager' => $this,
645
            'model' => $model,
646
            'table' => $this->table,
647
            'params' => $params
648
        ]);
649
    }
650
651
    public function drawCategorys()
652
    {
653
        if (!class_exists($this->modelName)) {
654
            return false;
655
        }
656
        ?>
657
        <ul class="nav nav-list-categorys" data-col='tree_path'>
658
          <?php
659
          $categoryModel = $this->managerOptions['categorys']['model'];
660
          $order = [];
661
          if (!empty($this->managerOptions['sortMode'])) {
662
              $order[] = ['weight', 'asc'];
663
          }
664
          $categorys = $categoryModel::getList(['order' => $order]);
665
          echo "<li>
666
                        <label class='nav-header'>
667
                            <a href='#' onclick='inji.Ui.dataManagers.get(this).switchCategory(this);return false;' data-path ='/'>/</a> 
668
                        </label>
669
                    </li>";
670
          foreach ($categorys as $category) {
671
              if ($category->parent_id == 0)
672
                  $this->showCategory($categorys, $category);
673
          }
674
          ?>
675
        </ul>
676
        <?php
677
    }
678
679
    public function showCategory($categorys, $category)
680
    {
681
        $isset = false;
682
        foreach ($categorys as $categoryChild) {
683
            if ($categoryChild->parent_id == $category->pk()) {
684 View Code Duplication
                if (!$isset) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
685
                    $isset = true;
686
                    echo "<li data-id='{$category->pk()}' data-model = '" . get_class($category) . "'>
687
                            <label class='nav-toggle nav-header'>
688
                                <span class='nav-toggle-icon glyphicon glyphicon-chevron-right'></span> 
689
                                <a href='#' onclick='inji.Ui.dataManagers.get(this).switchCategory(this);return false;' data-path ='" . $category->tree_path . ($category->pk() ? $category->pk() . "/" : '') . "'> " . $category->name . "</a> 
690
                                    <a href = '#' onclick = 'inji.Ui.forms.popUp(\"" . str_replace('\\', '\\\\', get_class($category)) . ':' . $category->pk() . "\")' class ='glyphicon glyphicon-edit'></a>&nbsp;    
691
                <a onclick='inji.Ui.dataManagers.get(this).delCategory({$category->pk()});return false;' class ='glyphicon glyphicon-remove'></a>
692
                    </label>
693
                            <ul class='nav nav-list nav-left-ml'>";
694
                }
695
                $this->showCategory($categorys, $categoryChild);
696
            }
697
        }
698 View Code Duplication
        if ($isset) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
699
            echo '</ul>
700
                    </li>';
701
        } else {
702
            echo "<li data-id='{$category->pk()}' data-model = '" . get_class($category) . "'>
703
                <label class='nav-header'>
704
                    <span  class=' nav-toggle-icon glyphicon glyphicon-minus'></span>
705
                    <div class ='pull-right actions'>
706
                        <a href = '#' onclick = 'inji.Ui.forms.popUp(\"" . str_replace('\\', '\\\\', get_class($category)) . ':' . $category->pk() . "\")' class ='glyphicon glyphicon-edit'></a>
707
                        <a onclick='inji.Ui.dataManagers.get(this).delCategory({$category->pk()});return false;' class ='glyphicon glyphicon-remove'></a>
708
                    </div>
709
                    <a href='#' onclick='inji.Ui.dataManagers.get(this).switchCategory(this);return false;' data-path ='" . $category->tree_path . ($category->pk() ? $category->pk() . "/" : '') . "'> " . $category->name . "</a> 
710
                </label>
711
            </li>";
712
        }
713
    }
714
715
    /**
716
     * Draw error message
717
     * 
718
     * @param string $errorText
719
     */
720
    public function drawError($errorText)
721
    {
722
        echo $errorText;
723
    }
724
725
    /**
726
     * Check access cur user to manager with name in param
727
     * 
728
     * @return boolean
729
     */
730
    public function checkAccess()
731
    {
732
        if ($this->managerName == 'manager' && !\Users\User::$cur->isAdmin()) {
733
            return false;
734
        }
735 View Code Duplication
        if (!empty($this->managerOptions['options']['access']['apps']) && !in_array(\App::$cur->name, $this->managerOptions['options']['access']['apps'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
736
            return false;
737
        }
738 View Code Duplication
        if (!empty($this->managerOptions['options']['access']['groups']) && !in_array(\Users\User::$cur->group_id, $this->managerOptions['options']['access']['groups'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
739
            return false;
740
        }
741
        return true;
742
    }
743
744
}
745