Completed
Push — master ( e999e7...604db9 )
by Alexey
10:06
created

DataManagerController::groupActionAction()   D

Complexity

Conditions 21
Paths 216

Size

Total Lines 75
Code Lines 56

Duplication

Lines 28
Ratio 37.33 %
Metric Value
dl 28
loc 75
rs 4.6077
cc 21
eloc 56
nc 216
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    function indexAction()
1 ignored issue
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...
Coding Style introduced by
indexAction 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...
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;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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()
1 ignored issue
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...
Coding Style introduced by
loadRowsAction 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...
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'], ':')) {
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...
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;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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) {
1 ignored issue
show
Bug introduced by
The variable $endRow does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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();
1 ignored issue
show
Coding Style Compatibility introduced by
The method loadRowsAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
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()
1 ignored issue
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...
Coding Style introduced by
loadCategorysAction 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...
159
    {
160
        $result = new Server\Result();
161
        ob_start();
162 View Code Duplication
        if (strpos($_GET['modelName'], ':')) {
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...
163
            $raw = explode(':', $_GET['modelName']);
164
            $modelName = $raw[0];
165
            $id = $raw[1];
166
            $model = $modelName::get($id);
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167
        } else {
168
            $modelName = $_GET['modelName'];
169
            $id = null;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
            $model = null;
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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()
1 ignored issue
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...
Coding Style introduced by
delRowAction 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...
192
    {
193
194 View Code Duplication
        if (strpos($_GET['modelName'], ':')) {
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...
195
            $raw = explode(':', $_GET['modelName']);
196
            $modelName = $raw[0];
197
            $id = $raw[1];
198
            $model = $modelName::get($id);
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
199
        } else {
200
            $modelName = $_GET['modelName'];
201
            $id = null;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
202
            $model = null;
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
203
        }
204 View Code Duplication
        if (!empty($_GET['params'])) {
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...
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 = [];
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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()
1 ignored issue
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...
Coding Style introduced by
updateRowAction 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...
235
    {
236
237 View Code Duplication
        if (strpos($_GET['modelName'], ':')) {
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...
238
            $raw = explode(':', $_GET['modelName']);
239
            $modelName = $raw[0];
240
            $id = $raw[1];
241
            $model = $modelName::get($id);
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
242
        } else {
243
            $modelName = $_GET['modelName'];
244
            $id = null;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
245
            $model = null;
0 ignored issues
show
Unused Code introduced by
$model is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
246
        }
247
        $params = [];
248 View Code Duplication
        if (!empty($_GET['params'])) {
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...
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()
1 ignored issue
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...
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...
277
    {
278
279
280 View Code Duplication
        if (strpos($_GET['modelName'], ':')) {
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...
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;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
288
            $model = null;
289
        }
290 View Code Duplication
        if (!empty($_GET['params'])) {
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...
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 = [];
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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':
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...
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()
1 ignored issue
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...
Coding Style introduced by
delCategoryAction 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...
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