Passed
Push — master ( 52863f...37ba64 )
by Alexey
04:34
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
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
        $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
46
        if (isset($request[$this->colName])) {
47
            $this->activeForm->model->{$colName} = $request[$this->colName];
48
        } else {
49
            $this->activeForm->model->{$colName} = 0;
50
            $this->activeForm->model->{$colName} = '';
51
        }
52
    }
53
54
    public function value() {
55
        $value = isset($this->colParams['default']) ? $this->colParams['default'] : '';
56
        if ($this->activeForm) {
57
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
58
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
59
        }
60
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
61
        return $value;
62
    }
63
64
    public function preset() {
65
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
66
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
67
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
68
        }
69
        if ($preset) {
70
            $value = '';
71
            if (!empty($preset['value'])) {
72
                $value = $preset['value'];
73
            } elseif (!empty($preset['userCol'])) {
74
                if (strpos($preset['userCol'], ':')) {
75
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
76
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
77
                    $value = \Users\User::$cur->$rel->$param;
78
                }
79
            }
80
            return $value;
81
        }
82
        return null;
83
    }
84
85
    public function colName() {
86
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
87
    }
88
89
    public function colLabel() {
90
        $modelName = $this->modelName;
91
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
92
    }
93
94
    public function readOnly() {
95
        if (!empty($this->colParams['readonly'])) {
96
            if (is_bool($this->colParams['readonly'])) {
97
                return true;
98
            }
99
            $readonly = true;
100
            if (is_array($this->colParams['readonly'])) {
101
                switch ($this->colParams['readonly']['cond']) {
102
                    case 'colValue':
103
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
104
                        if (!empty($this->colParams['readonly']['reverse'])) {
105
                            $readonly = !$readonly;
106
                        }
107
                        break;
108
                }
109
            }
110
            if ($readonly) {
111
                return true;
112
            }
113
        }
114
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
115
    }
116
117
    public function validate(&$request) {
118
        if (empty($request[$this->colName]) && !empty($this->colParams['required'])) {
119
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
120
        }
121
        if (!empty($this->colParams['validator'])) {
122
            $modelName = $this->modelName;
123
            $validator = $modelName::validator($this->colParams['validator']);
124
            $validator($this->activeForm, $request);
125
        }
126
    }
127
}