Test Failed
Push — master ( 2e584a...5aebc0 )
by Alexey
10:12
created

Input::parseRequest()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 5
nop 1
dl 12
loc 12
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
class Input extends \Object {
15
16
    public $form = null;
17
    public $activeForm = null;
18
    public $activeFormParams = [];
19
    public $modelName = '';
20
    public $colName = '';
21
    public $colParams = [];
22
    public $options = [];
23
24
    public function draw() {
25
        $inputName = $this->colName();
26
        $inputLabel = $this->colLabel();
27
28
        $inputOptions = $this->options;
29
        $inputOptions['value'] = $this->value();
30
        $inputOptions['disabled'] = $this->readOnly();
31
32
        $preset = $this->preset();
33
        if ($preset !== null) {
34
            $inputOptions['disabled'] = true;
35
            $this->form->input('hidden', $inputName, '', $inputOptions);
36
            return true;
37
        }
38
        $classPath = explode('\\', get_called_class());
39
        $inputType = lcfirst(array_pop($classPath));
40
        $this->form->input($inputType, $inputName, $inputLabel, $inputOptions);
41
        return true;
42
    }
43
44 View Code Duplication
    public function parseRequest($request) {
45
        if ($this->readOnly()) {
46
            return false;
47
        }
48
        $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
49
        if (isset($request[$this->colName])) {
50
            $this->activeForm->model->{$colName} = $request[$this->colName];
51
        } else {
52
            $this->activeForm->model->{$colName} = 0;
53
            $this->activeForm->model->{$colName} = '';
54
        }
55
    }
56
57
    public function value() {
58
        $value = '';
59
        if (isset($this->colParams['default'])) {
60
            if (is_array($this->colParams['default'])) {
61
                switch ($this->colParams['default']['type']) {
62
                    case 'relPath':
63
                        $val = $this->activeForm->model;
64
                        foreach (explode(':', $this->colParams['default']['relPath']) as $path) {
65
                            if ($val->$path) {
66
                                $val = $val->$path;
67
                            } else {
68
                                break 2;
69
                            }
70
                            $value = $val;
71
                        }
72
                        break;
73
                }
74
            } else {
75
                $value = $this->colParams['default'];
76
            }
77
        }
78
        if ($this->activeForm) {
79
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
80
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
81
        }
82
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
83
        return $value;
84
    }
85
86
    public function preset() {
87
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
88
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
89
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
90
        }
91
        if ($preset) {
92
            $value = '';
93
            if (!empty($preset['value'])) {
94
                $value = $preset['value'];
95
            } elseif (!empty($preset['userCol'])) {
96
                if (strpos($preset['userCol'], ':')) {
97
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
98
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
99
                    $value = \Users\User::$cur->$rel->$param;
100
                }
101
            }
102
            return $value;
103
        }
104
        return null;
105
    }
106
107
    public function colName() {
108
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
109
    }
110
111
    public function colLabel() {
112
        $modelName = $this->modelName;
113
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
114
    }
115
116
    public function readOnly() {
117
        if (!empty($this->colParams['readonly'])) {
118
            if (is_bool($this->colParams['readonly'])) {
119
                return true;
120
            }
121
            $readonly = true;
122
            if (is_array($this->colParams['readonly'])) {
123
                switch ($this->colParams['readonly']['cond']) {
124
                    case 'colValue':
125
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
126
                        if (!empty($this->colParams['readonly']['reverse'])) {
127
                            $readonly = !$readonly;
128
                        }
129
                        break;
130
                    case 'itemMethod':
131
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
132
                        break;
133
                }
134
            }
135
            if ($readonly) {
136
                return true;
137
            }
138
        }
139
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
140
    }
141
142
    public function validate(&$request) {
143
        if (empty($request[$this->colName]) && !empty($this->colParams['required'])) {
144
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
145
        }
146
        if (!empty($this->colParams['validator'])) {
147
            $modelName = $this->modelName;
148
            $validator = $modelName::validator($this->colParams['validator']);
149
            $validator($this->activeForm, $request);
150
        }
151
    }
152
}