Test Failed
Push — master ( 390c0f...bea1ad )
by Alexey
05:07
created

Input::colLabel()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 8
nop 0
dl 0
loc 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Active form input
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
12
namespace Ui\ActiveForm;
13
14
15
class Input extends \Object {
16
17
    public $form = null;
18
    /**
19
     * @var \Ui\ActiveForm
20
     */
21
    public $activeForm = null;
22
    public $activeFormParams = [];
23
    public $modelName = '';
24
    public $colName = '';
25
    public $colParams = [];
26
    public $options = [];
27
28
    public function draw() {
29
        $inputName = $this->colName();
30
        $inputLabel = $this->colLabel();
31
        $inputOptions = $this->options;
32
        $inputOptions['value'] = $this->value();
33
        $inputOptions['disabled'] = $this->readOnly();
34
        if (!empty($this->colParams['required']) || (!empty($this->colParams['requiredOnNew']) && !$this->activeForm->model->pk())) {
35
            $inputOptions['required'] = true;
36
        }
37
38
        $preset = $this->preset();
39
        if ($preset !== null) {
40
            $inputOptions['disabled'] = true;
41
            $this->form->input('hidden', $inputName, '', $inputOptions);
42
            return true;
43
        }
44
        $classPath = explode('\\', get_called_class());
45
        $inputType = lcfirst(array_pop($classPath));
46
        $this->form->input($inputType, $inputName, $inputLabel, $inputOptions);
47
        return true;
48
    }
49
50
    public function parseRequest($request) {
51
        $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
52
        if ($this->readOnly()) {
53
            if ($this->activeForm->model->pk()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
54
            } else {
55
                $this->activeForm->model->{$colName} = $this->defaultValue();
56
            }
57
            return true;
58
        }
59
60
        if (isset($request[$this->colName])) {
61
            $this->activeForm->model->{$colName} = $request[$this->colName];
62
        } else {
63
            $this->activeForm->model->{$colName} = 0;
64
            $this->activeForm->model->{$colName} = '';
65
        }
66
        return true;
67
    }
68
69
    public function value() {
70
        $value = $this->defaultValue();
71
        if ($this->activeForm) {
72
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
73
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
74
        }
75
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
76
        return $value;
77
    }
78
79
    public function defaultValue($value = '') {
80
        if (isset($this->colParams['default'])) {
81
            if (is_array($this->colParams['default'])) {
82
                switch ($this->colParams['default']['type']) {
83
                    case 'relPath':
84
                        $val = $this->activeForm->model;
85
                        foreach (explode(':', $this->colParams['default']['relPath']) as $path) {
86
                            if ($val->$path) {
87
                                $val = $val->$path;
88
                            } else {
89
                                break 2;
90
                            }
91
                            $value = $val;
92
                        }
93
                        break;
94
                }
95
            } else {
96
                $value = $this->colParams['default'];
97
            }
98
        }
99
        return $value;
100
    }
101
102
    public function preset() {
103
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
104
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
105
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
106
        }
107
        if ($preset) {
108
            $value = '';
109
            if (!empty($preset['value'])) {
110
                $value = $preset['value'];
111
            } elseif (!empty($preset['userCol'])) {
112
                if (strpos($preset['userCol'], ':')) {
113
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
114
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
115
                    $value = \Users\User::$cur->$rel->$param;
116
                }
117
            }
118
            return $value;
119
        }
120
        return null;
121
    }
122
123
    public function colName() {
124
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
125
    }
126
127
    public function colLabel() {
128
        $modelName = $this->modelName;
129
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
130
    }
131
132
    public function readOnly() {
133
        if (!empty($this->colParams['readonly'])) {
134
            if (is_bool($this->colParams['readonly'])) {
135
                return true;
136
            }
137
            $readonly = true;
138
            if (is_array($this->colParams['readonly'])) {
139
                switch ($this->colParams['readonly']['cond']) {
140
                    case 'colValue':
141
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
142
                        if (!empty($this->colParams['readonly']['reverse'])) {
143
                            $readonly = !$readonly;
144
                        }
145
                        break;
146
                    case 'itemMethod':
147
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
148
                        break;
149
                }
150
            }
151
            if ($readonly) {
152
                return true;
153
            }
154
        }
155
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
156
    }
157
158
    public function validate(&$request) {
159
        if (!empty($this->colParams['required']) && empty($request[$this->colName])) {
160
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
161
        }
162
        if (!empty($this->colParams['requiredOnNew']) && !$this->activeForm->model->pk() && empty($request[$this->colName])) {
163
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
164
        }
165
        if (!empty($this->colParams['unique']) && is_string($request[$this->colName])) {
166
            $modelName = $this->activeForm->modelName;
167
            $item = $modelName::get($request[$this->colName], $this->colName);
168
            if ($item && $item->id != $this->activeForm->model->id) {
169
                throw new \Exception($modelName::objectName() . ' с ' . $this->colLabel() . ' "' . $request[$this->colName] . '" уже существует');
170
            }
171
        }
172 View Code Duplication
        if (!empty($this->colParams['validator'])) {
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...
173
            $modelName = $this->modelName;
174
            $validator = $modelName::validator($this->colParams['validator']);
175
            $validator($this->activeForm, $request);
176
        }
177
    }
178
}