Test Failed
Push — master ( e9a009...455e4b )
by Alexey
04:25
created

UiController::formPopUpAction()   F

Complexity

Conditions 18
Paths 768

Size

Total Lines 49
Code Lines 40

Duplication

Lines 3
Ratio 6.12 %

Importance

Changes 0
Metric Value
cc 18
eloc 40
nc 768
nop 0
dl 3
loc 49
rs 2.8539
c 0
b 0
f 0

How to fix   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
 * Ui 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 UiController extends Controller {
12
13
    public function formPopUpAction() {
14
        if (strpos($_GET['item'], ':')) {
15
            $raw = explode(':', $_GET['item']);
16
            $modelName = $raw[0];
17
            $id = $raw[1];
18
            $model = $modelName::get($id, $modelName::index(), !empty($_GET['params']['dataManagerParams']) ? $_GET['params']['dataManagerParams'] : []);
19
        } else {
20
            $modelName = $_GET['item'];
21
            $model = new $modelName();
22
        }
23
        $params = [];
24
        if (!empty($_GET['params'])) {
25
            $params = $_GET['params'];
26
            if (!empty($params['preset'])) {
27
                $model->setParams($params['preset']);
28
            }
29
        }
30 View Code Duplication
        if (!empty($_GET['params']['dataManagerParams']['appType'])) {
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...
31
            $params['appType'] = $_GET['params']['dataManagerParams']['appType'];
32
        }
33
34
        $formName = !empty($_GET['formName']) ? $_GET['formName'] : (!empty($_GET['params']['formName']) ? $_GET['params']['formName'] : 'manager');
35
        $form = new Ui\ActiveForm($model, $formName);
36
        if (!empty($_GET['_']) || !empty($_POST['_'])) {
37
            $return = new Server\Result();
38
            ob_start();
39
            $form->checkRequest($params, true);
40
            $_GET['item'] = get_class($form->model) . ($model->pk() ? ':' . $model->pk() : '');
41
            $get = $_GET;
42
            if (isset($get['notSave'])) {
43
                unset($get['notSave']);
44
            }
45
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($get);
46
            $form->draw($params, true);
47
            $return->content = ob_get_contents();
48
            ob_end_clean();
49
            $return->send();
50
        } else {
51
            $form->checkRequest($params);
52
            $_GET['item'] = get_class($form->model) . ($model->pk() ? ':' . $model->pk() : '');
53
            $get = $_GET;
54
            if (isset($get['notSave'])) {
55
                unset($get['notSave']);
56
            }
57
            $form->action = (App::$cur->system ? '/' . App::$cur->name : '') . '/ui/formPopUp/?' . http_build_query($get);
58
            $this->view->setTitle(($model && $model->pk() ? 'Изменить ' : 'Создать ') . $form->header);
59
            $this->view->page(['content' => 'form', 'data' => compact('form', 'params')]);
60
        }
61
    }
62
63
    public function fastEditAction() {
64
        $model = $_POST['model']::get($_POST['key']);
65
        if ($model && $model->checkAccess()) {
66
            $model->$_POST['col'] = $_POST['data'];
67
            $model->save();
68
        }
69
    }
70
71
    public function autocompleteAction() {
72
        $snippets = $this->module->getSnippets('autocomplete');
73
        if (!is_string($_GET['snippet']) || !isset($snippets[$_GET['snippet']])) {
74
            exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method autocompleteAction() 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...
75
        }
76
        $snippet = $snippets[$_GET['snippet']];
77
        $result = new \Server\Result();
78
        $result->content = $snippet['find']($_GET['search'], $_GET['snippetParams']);
79
        $result->send();
80
    }
81
}
82