Completed
Push — master ( c52a8b...f81666 )
by Alexey
06:18
created

DataManagerController::parseRequest()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 50
rs 6.7272
cc 7
eloc 35
nc 24
nop 0
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
    public function parseRequest()
14
    {
15
        $return = [];
16
        $return['params'] = UserRequest::get('params', 'array', []);
17
18
        $item = UserRequest::get('modelName', 'string', '');
19
        if (!$item) {
20
            $item = UserRequest::get('item', 'string', '');
21
        }
22
23
        if (strpos($item, ':')) {
24
            $raw = explode(':', $item);
25
            $return['modelName'] = $raw[0];
26
            $return['model'] = $return['modelName']::get($raw[1], $return['modelName']::index(), $return['params']);
27
        } else {
28
            $return['modelName'] = $item;
29
            $return['model'] = null;
30
        }
31
        
32
        if (!empty($return['params']['relation'])) {
33
            $relation = $return['modelName']::getRelation($return['params']['relation']);
34
            if (!empty($relation['type']) && $relation['type'] == 'relModel') {
35
                $return['modelName'] = $relation['relModel'];
36
            } else {
37
                $return['modelName'] = $relation['model'];
38
            }
39
        }
40
        $return['params']['filters'] = UserRequest::get('filters', 'array', []);
41
        $return['params']['sortered'] = UserRequest::get('sortered', 'array', []);
42
        $return['params']['mode'] = UserRequest::get('mode', 'string', '');
43
        $return['params']['all'] = UserRequest::get('all', 'bool', false);
44
45
        $return['key'] = UserRequest::get('key', 'int', 0);
46
        $return['col'] = UserRequest::get('col', 'string', '');
47
        $return['col_value'] = UserRequest::get('col_value', 'string', '');
48
49
        $return['action'] = UserRequest::get('action', 'string', '');
50
        $return['ids'] = UserRequest::get('ids', 'string', '');
51
        $return['adInfo'] = UserRequest::get('adInfo', 'array', []);
52
53
        $return['download'] = UserRequest::get('download', 'bool', false);
54
        $return['silence'] = UserRequest::get('silence', 'bool', false);
55
56
        $return['managerName'] = UserRequest::get('managerName', 'string', 'manager');
57
        if (!$return['managerName']) {
58
            $return['managerName'] = 'manager';
59
        }
60
61
        return $return;
62
    }
63
64
    public function indexAction()
65
    {
66
        $result = new Server\Result();
67
68
        ob_start();
69
70
        $request = $this->parseRequest();
71
72
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
73
        $dataManager->draw($request['params'], $request['model']);
74
75
        $result->content = ob_get_contents();
76
77
        ob_end_clean();
78
79
        $result->send();
80
    }
81
82
    public function loadRowsAction()
83
    {
84
        $result = new Server\Result();
85
        $result->content = [];
86
87
        ob_start();
88
89
        $request = $this->parseRequest();
90
91
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
92
        if ($request['download']) {
93
            $request['params']['all'] = true;
94
            $request['params']['download'] = true;
95
            set_time_limit(0);
96
            ob_end_clean();
97
            header('Content-Encoding: UTF-8');
98
            header("Content-Type: text/csv");
99
            header("Content-Disposition: attachment; filename=" . $request['modelName']::$objectName . '.csv');
100
            echo "\xEF\xBB\xBF"; // UTF-8 BOM
101
102
103
            $cols = $dataManager->getCols();
104
            $cols = array_slice($cols, (!empty($dataManager->managerOptions['groupActions']) ? 1 : 0));
105
            $endRow = true;
106
            foreach ($cols as $colName => $options) {
107
                if (!$endRow) {
108
                    echo ";";
109
                }
110
                $endRow = false;
111
                echo '"' . $options['label'] . '"';
112
            }
113
            echo "\n";
114
            $endRow = true;
115
        }
116
        $rows = $dataManager->getRows($request['params'], $request['model']);
117
        foreach ($rows as $row) {
118
            if ($request['download']) {
119
                $row = array_slice($row, (!empty($dataManager->managerOptions['groupActions']) ? 1 : 0), -1);
120
                foreach ($row as $col) {
121
                    if (!$endRow) {
122
                        echo ";";
123
                    }
124
                    $endRow = false;
125
                    echo '"' . str_replace("\n", '', $col) . '"';
126
                }
127
                echo "\n";
128
                $endRow = true;
129
            } else {
130
                Ui\Table::drawRow($row);
131
            }
132
        }
133
        if ($request['download']) {
134
            exit();
135
        }
136
137
        $result->content['rows'] = ob_get_contents();
138
        ob_clean();
139
140
        $result->content['pages'] = '';
141
142
        if (!$request['params']['all']) {
143
            $pages = $dataManager->getPages($request['params'], $request['model']);
144
            if ($pages) {
145
                $pages->draw();
146
            }
147
            $result->content['pages'] = ob_get_contents();
148
            ob_end_clean();
149
        }
150
        $result->send();
151
    }
152
153
    public function loadCategorysAction()
154
    {
155
        $result = new Server\Result();
156
157
        ob_start();
158
159
        $request = $this->parseRequest();
160
161
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
162
        $dataManager->drawCategorys();
163
164
        $result->content = ob_get_contents();
165
        ob_end_clean();
166
167
        $result->send();
168
    }
169
170 View Code Duplication
    public function delRowAction()
171
    {
172
173
        $request = $this->parseRequest();
174
175
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
176
177
        if ($dataManager->checkAccess()) {
178
            $model = $request['modelName']::get($request['key'], $request['modelName']::index(), $request['params']);
179
            if ($model) {
180
                $model->delete($request['params']);
181
            }
182
        }
183
        $result = new Server\Result();
184
        $result->successMsg = empty($request['silence']) ? 'Запись удалена' : '';
185
        $result->send();
186
    }
187
188
    public function updateRowAction()
189
    {
190
191
        $request = $this->parseRequest();
192
193
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
194
195
        if ($dataManager->checkAccess()) {
196
            $model = $request['modelName']::get($request['key'], $request['modelName']::index(), $request['params']);
197
            if ($model) {
198
                $model->{$request['col']} = $request['col_value'];
199
                $model->save($request['params']);
200
            }
201
        }
202
        $result = new Server\Result();
203
        $result->successMsg = empty($request['silence']) ? 'Запись Обновлена' : '';
204
        $result->send();
205
    }
206
207
    public function groupActionAction()
1 ignored issue
show
Coding Style introduced by
groupActionAction uses the super-global variable $_GET 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...
208
    {
209
        $request = $this->parseRequest();
210
211
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
212
213
        if ($dataManager->checkAccess()) {
214
            if (!empty($request['action']) && !empty($dataManager->managerOptions['groupActions'][$request['action']]) && trim($request['ids'], ' ,')) {
215
                $ids = trim($request['ids'], ' ,');
216
                $action = $dataManager->managerOptions['groupActions'][$request['action']];
217
                switch ($action['action']) {
218
                    case'delete':
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...
219
                        $models = $request['modelName']::getList(['where' => [[$request['modelName']::index(), $ids, 'IN']]]);
220
                        foreach ($models as $model) {
221
                            $model->delete();
222
                        }
223
                        break;
224
                    case 'changeParam':
225
                        $models = $request['modelName']::getList(['where' => [[$request['modelName']::index(), $ids, 'IN']]]);
226
                        foreach ($models as $model) {
227
                            $model->{$action['col']} = $action['value'];
228
                            $model->save(!empty($_GET['params']) ? $_GET['params'] : []);
229
                        }
230
                        break;
231
                    case 'moduleMethod':
232
                        $comandResult = App::$cur->{$action['module']}->{$action['method']}($dataManager, $ids, $request['adInfo']);
233
                        break;
234
                }
235
            }
236
        }
237
        $result = new Server\Result();
238
        if (!empty($comandResult)) {
239
            $result->success = $comandResult['success'];
240
            $result->content = $comandResult['content'];
241
        }
242
        $result->successMsg = 'Операция выполнена';
243
        $result->send();
244
    }
245
246 View Code Duplication
    public function delCategoryAction()
247
    {
248
249
        $request = $this->parseRequest();
250
251
        $dataManager = new Ui\DataManager($request['modelName'], $request['managerName']);
252
253
        if ($dataManager->checkAccess() && !empty($dataManager->managerOptions['categorys'])) {
254
            $categoryModel = $dataManager->managerOptions['categorys']['model'];
255
            $model = $categoryModel::get($request['key'], $categoryModel::index(), $request['params']);
256
            if ($model) {
257
                $model->delete($request['params']);
258
            }
259
        }
260
        $result = new Server\Result();
261
        $result->send();
262
    }
263
264
}
265