Passed
Push — master ( 227fa5...84d0c3 )
by Alexey
06:13
created

Input::readOnly()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 18
nop 0
dl 0
loc 25
rs 4.909
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 = isset($this->colParams['default']) ? $this->colParams['default'] : '';
59
        if ($this->activeForm) {
60
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
61
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
62
        }
63
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
64
        return $value;
65
    }
66
67
    public function preset() {
68
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
69
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
70
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
71
        }
72
        if ($preset) {
73
            $value = '';
74
            if (!empty($preset['value'])) {
75
                $value = $preset['value'];
76
            } elseif (!empty($preset['userCol'])) {
77
                if (strpos($preset['userCol'], ':')) {
78
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
79
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
80
                    $value = \Users\User::$cur->$rel->$param;
81
                }
82
            }
83
            return $value;
84
        }
85
        return null;
86
    }
87
88
    public function colName() {
89
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
90
    }
91
92
    public function colLabel() {
93
        $modelName = $this->modelName;
94
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
95
    }
96
97
    public function readOnly() {
98
        if (!empty($this->colParams['readonly'])) {
99
            if (is_bool($this->colParams['readonly'])) {
100
                return true;
101
            }
102
            $readonly = true;
103
            if (is_array($this->colParams['readonly'])) {
104
                switch ($this->colParams['readonly']['cond']) {
105
                    case 'colValue':
106
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
107
                        if (!empty($this->colParams['readonly']['reverse'])) {
108
                            $readonly = !$readonly;
109
                        }
110
                        break;
111
                    case 'itemMethod':
112
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
113
                        break;
114
                }
115
            }
116
            if ($readonly) {
117
                return true;
118
            }
119
        }
120
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
121
    }
122
123
    public function validate(&$request) {
124
        if (empty($request[$this->colName]) && !empty($this->colParams['required'])) {
125
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
126
        }
127
        if (!empty($this->colParams['validator'])) {
128
            $modelName = $this->modelName;
129
            $validator = $modelName::validator($this->colParams['validator']);
130
            $validator($this->activeForm, $request);
131
        }
132
    }
133
}