|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the fangface/yii2-concord package |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view |
|
6
|
|
|
* the file LICENSE.md that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @package fangface/yii2-concord |
|
9
|
|
|
* @author Fangface <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2014 Fangface <[email protected]> |
|
11
|
|
|
* @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace fangface\base\traits; |
|
16
|
|
|
|
|
17
|
|
|
use fangface\forms\InputField; |
|
18
|
|
|
use fangface\Tools; |
|
19
|
|
|
use fangface\validators\DateValidator; |
|
20
|
|
|
use fangface\validators\FilterValidator; |
|
21
|
|
|
use fangface\validators\StrengthValidator; |
|
22
|
|
|
use yii\helpers\ArrayHelper; |
|
23
|
|
|
use yii\helpers\Html; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
trait AttributeSupport |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var boolean |
|
31
|
|
|
*/ |
|
32
|
|
|
public $allowAutoRule = true; |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the config for an attribute |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $attribute Attribute name |
|
39
|
|
|
* @param string $option [optional] name of the entry to be returned, |
|
40
|
|
|
* otherwise all entries for attribute will be returned |
|
41
|
|
|
* @param string $setting [optional] name of the active field setting to be returned |
|
42
|
|
|
* if $option is 'active' |
|
43
|
|
|
* @param boolean $encode [default false] Should the resulting string be encoded |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getAttributeConfig($attribute, $option = null, $setting = null, $encode = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$config = false; |
|
49
|
|
|
$attributeDefaults = $this->attributeConfig(); |
|
50
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
51
|
|
|
$config = $attributeDefaults[$attribute]; |
|
52
|
|
|
if ($option !== null) { |
|
53
|
|
|
if (array_key_exists($option, $config)) { |
|
54
|
|
|
if ($setting !== null) { |
|
55
|
|
|
if (array_key_exists($setting, $config[$option])) { |
|
56
|
|
|
return ($encode ? Html::encode($config[$option][$setting]) : $config[$option][$setting]); |
|
57
|
|
|
} |
|
58
|
|
|
} else { |
|
59
|
|
|
return ($encode ? Html::encode($config[$option]) : $config[$option]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
return $config; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get array of attribute config suitable for providing fieldConfig, hintBlocks, icons, placeHolders and toolTips |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function attributeConfig() |
|
74
|
|
|
{ |
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the text label for the specified attribute. |
|
80
|
|
|
* @param string $attribute the attribute name |
|
81
|
|
|
* @return string the attribute label |
|
82
|
|
|
* @see generateAttributeLabel() |
|
83
|
|
|
* @see attributeLabels() |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getAttributeLabel($attribute) |
|
86
|
|
|
{ |
|
87
|
|
|
$message = $this->getAttributeConfig($attribute, 'label'); |
|
88
|
|
|
if (!$message) { |
|
89
|
|
|
$message = $this->getAttributeConfig($attribute, 'active', 'label'); |
|
90
|
|
|
if (!$message) { |
|
91
|
|
|
$labels = $this->attributeLabels(); |
|
92
|
|
|
if (array_key_exists($attribute, $labels)) { |
|
93
|
|
|
$message = $labels[$attribute]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
if (!$message) { |
|
98
|
|
|
$message = $this->generateAttributeLabel($attribute); |
|
99
|
|
|
} |
|
100
|
|
|
return $message; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Override default implementation to make use of self::attributeConfig() to determine |
|
105
|
|
|
* rules. If not set then the default parent implementation will be used. |
|
106
|
|
|
* @see \yii\base\Model::rules() |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
public function rules() |
|
110
|
|
|
{ |
|
111
|
|
|
$config = $this->attributeConfig(); |
|
112
|
|
|
if (!$config) { |
|
113
|
|
|
return parent::rules(); |
|
114
|
|
|
} |
|
115
|
|
|
$data = []; |
|
116
|
|
|
foreach ($config as $attribute => $attrConfig) { |
|
117
|
|
|
$rules = []; |
|
118
|
|
|
if (array_key_exists('rules', $attrConfig)) { |
|
119
|
|
|
if ($attrConfig['rules']) { |
|
120
|
|
|
if (array_key_exists('scenarios', $attrConfig['rules'])) { |
|
121
|
|
|
if (array_key_exists($this->getScenario(), $attrConfig['rules']['scenarios'])) { |
|
122
|
|
|
$rules = $attrConfig['rules']['scenarios'][$this->getScenario()]; |
|
123
|
|
|
foreach ($rules as $key => $rule) { |
|
124
|
|
|
$rules[] = $rule; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
foreach ($attrConfig['rules'] as $key => $rule) { |
|
129
|
|
|
if (array_key_exists('except', $rule)) { |
|
130
|
|
|
if (is_array($rule['except']) && in_array($this->getScenario(), $rule['except'])) { |
|
|
|
|
|
|
131
|
|
|
// ignore this rule in this scenario |
|
132
|
|
|
} elseif ($rule['except'] == $this->getScenario()) { |
|
|
|
|
|
|
133
|
|
|
// ignore this rule in this scenario |
|
134
|
|
|
} else { |
|
135
|
|
|
$rules[] = $rule; |
|
136
|
|
|
} |
|
137
|
|
|
} else { |
|
138
|
|
|
$rules[] = $rule; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
if ($this->allowAutoRule) { |
|
145
|
|
|
if (ArrayHelper::getValue($attrConfig, 'autorules', true)) { |
|
146
|
|
|
$extraRules = $this->getAutoRules($rules, $attrConfig, $attribute); |
|
147
|
|
|
if ($extraRules) { |
|
148
|
|
|
foreach ($extraRules as $key => $rule) { |
|
149
|
|
|
$rules[] = $rule; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
foreach ($rules as $key => $rule) { |
|
155
|
|
|
array_unshift($rule, [$attribute]); |
|
156
|
|
|
$data[] = $rule; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
return $data; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get any auto rules base on table schema |
|
164
|
|
|
* @param array $rules |
|
165
|
|
|
* @param array $config |
|
166
|
|
|
* @param string $attribute |
|
167
|
|
|
* @return array |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getAutoRules($rules, $config, $attribute = '') |
|
170
|
|
|
{ |
|
171
|
|
|
static $columns = null; |
|
172
|
|
|
|
|
173
|
|
|
$data = []; |
|
174
|
|
|
|
|
175
|
|
|
switch ($attribute) { |
|
176
|
|
|
case 'id': |
|
177
|
|
|
case 'created_at': |
|
178
|
|
|
case 'createdAt': |
|
179
|
|
|
case 'modified_at': |
|
180
|
|
|
case 'modifiedAt': |
|
181
|
|
|
return $data; |
|
182
|
|
|
break; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$type = (isset($config['active']['type']) ? $config['active']['type'] : ''); |
|
186
|
|
|
if ($type == InputField::INPUT_STATIC) { |
|
187
|
|
|
return $data; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$columns = ($columns === null ? self::getTableSchema()->columns : $columns); |
|
191
|
|
|
|
|
192
|
|
|
if (isset($columns[$attribute])) { |
|
193
|
|
|
$spec = (isset($columns[$attribute]) ? $columns[$attribute] : false); |
|
194
|
|
|
|
|
195
|
|
|
$max = -1; |
|
196
|
|
|
$noTrim = false; |
|
197
|
|
|
$defaultOnEmpty = null; |
|
198
|
|
|
$isString = true; |
|
199
|
|
|
$isMultiple = false; |
|
200
|
|
|
$dateFormat = ''; |
|
201
|
|
|
$addRules = []; |
|
202
|
|
|
|
|
203
|
|
|
$isInteger = false; |
|
204
|
|
|
switch ($spec->type) { |
|
205
|
|
|
case 'int': |
|
206
|
|
|
case 'integer': |
|
207
|
|
|
case 'tinyint': |
|
208
|
|
|
case 'smallint': |
|
209
|
|
|
case 'mediumint': |
|
210
|
|
|
case '__bigint': // not included here as cast as will have been cast as a string |
|
211
|
|
|
$isInteger = true; |
|
212
|
|
|
break; |
|
213
|
|
|
default: |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if ($type == '') { |
|
217
|
|
|
$type = InputField::getDefaultInputFieldType($attribute, $spec, (isset($config['active']) ? $config['active'] : null)); |
|
218
|
|
|
if ($type == InputField::INPUT_STATIC) { |
|
219
|
|
|
return $data; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
switch ($type) { |
|
224
|
|
|
case InputField::INPUT_TEXT: |
|
225
|
|
|
case InputField::INPUT_PASSWORD: |
|
226
|
|
|
$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
|
227
|
|
|
break; |
|
228
|
|
|
case InputField::INPUT_PASSWORD_STRENGTH: |
|
229
|
|
|
//wlchere - alternative or implement correctly again |
|
230
|
|
|
//$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
|
231
|
|
|
//$addRules[] = ['check' => [StrengthValidator::className()], 'rule' => [StrengthValidator::className(), 'preset' => 'normal', 'hasUser' => false, 'hasEmail' => false, 'userAttribute' => false]]; |
|
232
|
|
|
break; |
|
233
|
|
|
case InputField::INPUT_INTEGER: |
|
234
|
|
|
if ($spec) { |
|
235
|
|
|
$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
|
236
|
|
|
$unsigned = $spec->unsigned; |
|
237
|
|
|
} else { |
|
238
|
|
|
$max = $config['active']['options']['maxlength']; |
|
239
|
|
|
$unsigned = (!isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false); |
|
240
|
|
|
} |
|
241
|
|
|
$maxValue = pow(10, $max) - 1; |
|
242
|
|
|
$minValue = ($unsigned ? 0 : -1 * $maxValue); |
|
243
|
|
|
$isString = false; |
|
244
|
|
|
$defaultOnEmpty = 0; |
|
245
|
|
|
if ($maxValue > 2147483646) { |
|
246
|
|
|
// bigint for example is cast as a string |
|
247
|
|
|
} else { |
|
248
|
|
|
$addRules[] = [ |
|
249
|
|
|
'check' => [FilterValidator::className(), 'filter' => 'intval'], |
|
250
|
|
|
'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
|
251
|
|
|
]; |
|
252
|
|
|
} |
|
253
|
|
|
$addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => $maxValue, 'min' => $minValue]]; |
|
254
|
|
|
if (!$unsigned) { |
|
255
|
|
|
$max++; |
|
256
|
|
|
} |
|
257
|
|
|
break; |
|
258
|
|
|
case InputField::INPUT_DECIMAL: |
|
259
|
|
|
if ($spec) { |
|
260
|
|
|
$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size + 1); // allow for decimal point |
|
261
|
|
|
$unsigned = $spec->unsigned; |
|
262
|
|
|
$scale = $spec->scale; |
|
263
|
|
|
} else { |
|
264
|
|
|
$max = $config['active']['options']['maxlength']; |
|
265
|
|
|
$unsigned = (!isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false); |
|
266
|
|
|
$scale = (isset($config['active']['options']['data-inputmask']['digits']) ? $config['active']['options']['data-inputmask']['digits'] : 2); |
|
267
|
|
|
} |
|
268
|
|
|
$maxValue = pow(10, ($max - 1)) - 0.01; |
|
269
|
|
|
$minValue = ($unsigned ? 0 : -1 * $maxValue); |
|
270
|
|
|
$noTrim = true; |
|
271
|
|
|
$defaultOnEmpty = '0.00'; |
|
272
|
|
|
$addRules[] = ['check' => ['double'], 'rule' => ['double', 'max' => $maxValue, 'min' => $minValue]]; |
|
273
|
|
|
$addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'number_format', 'args' => [$scale, '.', '']]]; |
|
274
|
|
|
if (!$unsigned) { |
|
275
|
|
|
$max++; |
|
276
|
|
|
} |
|
277
|
|
|
break; |
|
278
|
|
|
case InputField::INPUT_TEXTAREA: |
|
279
|
|
|
$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
|
280
|
|
|
break; |
|
281
|
|
|
case InputField::INPUT_CHECKBOX: |
|
282
|
|
|
case InputField::INPUT_CHECKBOX_BASIC: |
|
283
|
|
|
case InputField::INPUT_CHECKBOX_SWITCH: |
|
284
|
|
|
case InputField::INPUT_CHECKBOX_ICHECK: |
|
285
|
|
|
$isString = false; |
|
286
|
|
|
$addRules[] = ['check' => ['boolean'], 'rule' => ['boolean']]; |
|
287
|
|
|
$defaultOnEmpty = 0; |
|
288
|
|
|
if ($spec && $isInteger) { |
|
289
|
|
|
if ($spec->size == 1) { |
|
290
|
|
|
$addRules[] = [ |
|
291
|
|
|
'check' => [FilterValidator::className(), 'filter' => 'intval'], |
|
292
|
|
|
'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
|
293
|
|
|
]; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
break; |
|
297
|
|
|
case InputField::INPUT_DATE: |
|
298
|
|
|
$defaultOnEmpty = Tools::DATE_DB_EMPTY; |
|
299
|
|
|
$max = 10; |
|
300
|
|
|
$dateFormat = 'Y-m-d'; |
|
301
|
|
|
break; |
|
302
|
|
|
case InputField::INPUT_DATETIME: |
|
303
|
|
|
$defaultOnEmpty = Tools::DATE_TIME_DB_EMPTY; |
|
304
|
|
|
$max = 19; |
|
305
|
|
|
$dateFormat = 'Y-m-d H:i:s'; |
|
306
|
|
|
break; |
|
307
|
|
|
case InputField::INPUT_TIME: |
|
308
|
|
|
$defaultOnEmpty = Tools::TIME_DB_EMPTY; |
|
309
|
|
|
$max = 8; |
|
310
|
|
|
$dateFormat = 'H:i:s'; |
|
311
|
|
|
break; |
|
312
|
|
|
case InputField::INPUT_YEAR: |
|
313
|
|
|
$defaultOnEmpty = Tools::YEAR_DB_EMPTY; |
|
314
|
|
|
$max = 4; |
|
315
|
|
|
$dateFormat = 'Y'; |
|
316
|
|
|
$addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => Tools::YEAR_DB_MAX, 'min' => Tools::YEAR_DB_EMPTY]]; |
|
317
|
|
|
break; |
|
318
|
|
|
case InputField::INPUT_COLOR: |
|
319
|
|
|
$max = (!$spec || $spec->size > 21 ? 21 : $spec->size); |
|
320
|
|
|
break; |
|
321
|
|
|
case InputField::INPUT_MINI_COLORS: |
|
322
|
|
|
$max = (!$spec || $spec->size > 25 ? 25 : $spec->size); |
|
323
|
|
|
break; |
|
324
|
|
|
case InputField::INPUT_SELECT_PICKER_MULTI: |
|
325
|
|
|
case InputField::INPUT_CHECKBOX_LIST: |
|
326
|
|
|
case InputField::INPUT_CHECKBOX_LIST_ICHECK: |
|
327
|
|
|
case InputField::INPUT_MULTISELECT: |
|
328
|
|
|
case InputField::INPUT_SELECT2_MULTI: |
|
329
|
|
|
$isMultiple = true; |
|
330
|
|
|
break; |
|
331
|
|
|
case InputField::INPUT_SELECT2: |
|
332
|
|
|
case InputField::INPUT_DROPDOWN_LIST: |
|
333
|
|
|
case InputField::INPUT_LIST_BOX: |
|
334
|
|
|
case InputField::INPUT_SELECT_PICKER: |
|
335
|
|
|
case InputField::INPUT_SELECT_SPLITTER: |
|
336
|
|
|
if (isset($config['active']['options']['multiple']) && $config['active']['options']['multiple']) { |
|
337
|
|
|
$isMultiple = true; |
|
338
|
|
|
} else { |
|
339
|
|
|
if ($spec && $isInteger) { |
|
340
|
|
|
$defaultOnEmpty = '0'; |
|
341
|
|
|
$addRules[] = [ |
|
342
|
|
|
'check' => [FilterValidator::className(), 'filter' => 'intval'], |
|
343
|
|
|
'rule' => [FilterValidator::className(), 'filter' => 'intval'], |
|
344
|
|
|
]; |
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
break; |
|
348
|
|
|
case InputField::INPUT_SELECT2_TAGS: |
|
349
|
|
|
// received as a pipe delimited string |
|
350
|
|
|
$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size); |
|
351
|
|
|
break; |
|
352
|
|
|
case InputField::INPUT_RADIO_LIST: |
|
353
|
|
|
case InputField::INPUT_RADIO_LIST_ICHECK: |
|
354
|
|
|
case InputField::INPUT_RADIO: |
|
355
|
|
|
break; |
|
356
|
|
|
case InputField::INPUT_EDITOR_CK: |
|
357
|
|
|
case InputField::INPUT_EDITOR_BS_WYSIHTML5: |
|
358
|
|
|
case InputField::INPUT_EDITOR_BS_SUMMERNOTE: |
|
359
|
|
|
break; |
|
360
|
|
|
default: |
|
361
|
|
|
break; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
if ($isMultiple) { |
|
365
|
|
|
// apply default conversion of multiple selections array into pipe delimited string |
|
366
|
|
|
$addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'implode', 'makeArray' => true, 'argsFirst' => true, 'args' => ['|']]]; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
if ($defaultOnEmpty !== null) { |
|
370
|
|
|
if (!$this->checkHasRule($rules, $attribute, 'default' , 'value')) { |
|
371
|
|
|
$data[] = ['default', 'value' => $defaultOnEmpty]; |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
if ($isString && $max != -1) { |
|
376
|
|
|
if (!$this->checkHasRule($rules, $attribute, 'string', 'max')) { |
|
377
|
|
|
if (isset($config['active']['options']['maxlength'])) { |
|
378
|
|
|
$max = $config['active']['options']['maxlength']; |
|
379
|
|
|
} |
|
380
|
|
|
$data[] = ['string', 'max' => ($max ? intval($max) : $spec->size)]; |
|
381
|
|
|
} |
|
382
|
|
|
if (!$noTrim && !$this->checkHasRule($rules, $attribute, 'filter', 'filter', 'trim')) { |
|
383
|
|
|
$data[] = ['filter', 'filter' => 'trim']; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
if ($dateFormat != '') { |
|
388
|
|
|
if (!$this->checkHasRule($rules, $attribute, 'date')) { |
|
389
|
|
|
$data[] = [DateValidator::className(), 'format' => 'php:' . $dateFormat]; |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
if ($addRules) { |
|
394
|
|
|
foreach ($addRules as $rule) { |
|
395
|
|
|
$skipRule = false; |
|
396
|
|
|
if (isset($rule['check']) && $rule['check']) { |
|
397
|
|
|
if (is_array($rule['check']) && $this->checkHasRule($rules, $attribute, $rule['check'][0] , (isset($rule['check'][1]) ? $rule['check'][1] : ''), (isset($rule['check'][2]) ? $rule['check'][2] : null))) { |
|
398
|
|
|
$skipRule = true; |
|
399
|
|
|
} elseif ($this->checkHasRule($rules, $attribute, $rule['check'])) { |
|
400
|
|
|
$skipRule = true; |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
if (!$skipRule) { |
|
404
|
|
|
$data[] = (isset($rule['rule']) ? $rule['rule'] : $rule); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
} |
|
410
|
|
|
return $data; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
/** |
|
414
|
|
|
* Check rules to see if one matches the type specified |
|
415
|
|
|
* @param array $rules |
|
416
|
|
|
* @param string $attribute |
|
417
|
|
|
* @param string $ruleType |
|
418
|
|
|
* @param string $ruleOption |
|
419
|
|
|
* @param mixed $ruleOptionValue |
|
420
|
|
|
* @return boolean |
|
421
|
|
|
*/ |
|
422
|
|
|
public function checkHasRule($rules, $attribute = '', $ruleType, $ruleOption = '', $ruleOptionValue = null) |
|
423
|
|
|
{ |
|
424
|
|
|
if ($rules) { |
|
425
|
|
|
foreach ($rules as $key => $rule) { |
|
426
|
|
|
if ($rule[0] == $ruleType) { |
|
427
|
|
|
if ($ruleOption != '') { |
|
428
|
|
|
if (isset($rule[$ruleOption])) { |
|
429
|
|
|
if ($ruleOptionValue !== null) { |
|
430
|
|
|
if ($ruleOptionValue == '!') { |
|
431
|
|
|
if (!empty($rule[$ruleOption])) { |
|
432
|
|
|
// not empty |
|
433
|
|
|
return true; |
|
434
|
|
|
} |
|
435
|
|
|
} else { |
|
436
|
|
|
if ($rule[$ruleOption] == $ruleOptionValue) { |
|
437
|
|
|
return true; |
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
return false; |
|
441
|
|
|
} |
|
442
|
|
|
return true; |
|
443
|
|
|
} |
|
444
|
|
|
return false; |
|
445
|
|
|
} |
|
446
|
|
|
return true; |
|
447
|
|
|
} |
|
448
|
|
|
} |
|
449
|
|
|
} |
|
450
|
|
|
|
|
451
|
|
|
return false; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Override default implementation to make use of self::attributeConfig() to determine |
|
456
|
|
|
* scenarios. If not set then the default parent implementation will be used. |
|
457
|
|
|
* @see \yii\base\Model::rules() |
|
458
|
|
|
* @return array |
|
459
|
|
|
*/ |
|
460
|
|
|
public function scenarios() |
|
461
|
|
|
{ |
|
462
|
|
|
$config = $this->attributeConfig(); |
|
463
|
|
|
if (!$config) { |
|
464
|
|
|
return parent::scenarios(); |
|
465
|
|
|
} |
|
466
|
|
|
$data = []; |
|
467
|
|
|
$data[self::SCENARIO_DEFAULT] = []; |
|
468
|
|
|
$data['all'] = []; |
|
469
|
|
|
$byException = []; |
|
470
|
|
|
foreach ($config as $attribute => $attrConfig) { |
|
471
|
|
|
if (array_key_exists('scenarios', $attrConfig)) { |
|
472
|
|
|
if ($attrConfig['scenarios']) { |
|
473
|
|
|
if (is_array($attrConfig['scenarios'])) { |
|
474
|
|
|
foreach ($attrConfig['scenarios'] as $key => $scenario) { |
|
475
|
|
|
if (!array_key_exists($scenario, $data)) { |
|
476
|
|
|
$data[$scenario] = []; |
|
477
|
|
|
$data[$scenario][] = $attribute; |
|
478
|
|
|
} elseif (!in_array($attribute, $data[$scenario])) { |
|
479
|
|
|
$data[$scenario][] = $attribute; |
|
480
|
|
|
} |
|
481
|
|
|
} |
|
482
|
|
|
} else { |
|
483
|
|
|
$scenario = $attrConfig['scenarios']; |
|
484
|
|
|
if (!array_key_exists($scenario, $data)) { |
|
485
|
|
|
$data[$scenario] = []; |
|
486
|
|
|
$data[$scenario][] = $attribute; |
|
487
|
|
|
} elseif (!in_array($attribute, $data[$scenario])) { |
|
488
|
|
|
$data[$scenario][] = $attribute; |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
if (array_key_exists('rules', $attrConfig)) { |
|
494
|
|
|
if ($attrConfig['rules']) { |
|
495
|
|
|
if (array_key_exists('scenarios', $attrConfig['rules'])) { |
|
496
|
|
|
foreach ($attrConfig['rules']['scenarios'] as $scenario => $rules) { |
|
497
|
|
|
if (!array_key_exists($scenario, $data)) { |
|
498
|
|
|
$data[$scenario] = []; |
|
499
|
|
|
$data[$scenario][] = $attribute; |
|
500
|
|
|
} elseif (!in_array($attribute, $data[$scenario])) { |
|
501
|
|
|
$data[$scenario][] = $attribute; |
|
502
|
|
|
} |
|
503
|
|
|
} |
|
504
|
|
|
} else { |
|
505
|
|
|
foreach ($attrConfig['rules'] as $key => $rule) { |
|
506
|
|
|
if (array_key_exists('except', $rule)) { |
|
507
|
|
|
$scenarios = (!is_array($rule['except']) ? [$rule['except']] : $rule['except']); |
|
508
|
|
|
if (!array_key_exists($attribute, $byException)) { |
|
509
|
|
|
$byException[$attribute] = []; |
|
510
|
|
|
} |
|
511
|
|
|
$byException[$attribute] = array_merge($byException[$attribute], $scenarios); |
|
512
|
|
|
} elseif (array_key_exists('on', $rule)) { |
|
513
|
|
|
$scenarios = (!is_array($rule['on']) ? [$rule['on']] : $rule['on']); |
|
514
|
|
|
foreach ($scenarios as $key2 => $scenario) { |
|
515
|
|
|
if (!array_key_exists($scenario, $data)) { |
|
516
|
|
|
$data[$scenario] = []; |
|
517
|
|
|
$data[$scenario][] = $attribute; |
|
518
|
|
|
} elseif (!in_array($attribute, $data[$scenario])) { |
|
519
|
|
|
$data[$scenario][] = $attribute; |
|
520
|
|
|
} |
|
521
|
|
|
} |
|
522
|
|
|
} else { |
|
523
|
|
|
// add to default scenario |
|
524
|
|
|
if (!in_array($attribute, $data[self::SCENARIO_DEFAULT])) { |
|
525
|
|
|
$data[self::SCENARIO_DEFAULT][] = $attribute; |
|
526
|
|
|
} |
|
527
|
|
|
if (false) { |
|
528
|
|
|
// actually add to all scenarios, unless excepted elsewhere |
|
529
|
|
|
if (!array_key_exists($attribute, $byException)) { |
|
530
|
|
|
$byException[$attribute] = []; |
|
531
|
|
|
} |
|
532
|
|
|
} |
|
533
|
|
|
} |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
} else { |
|
538
|
|
|
// add to default scenario |
|
539
|
|
|
if (!in_array($attribute, $data[self::SCENARIO_DEFAULT])) { |
|
540
|
|
|
$data[self::SCENARIO_DEFAULT][] = $attribute; |
|
541
|
|
|
} |
|
542
|
|
|
} |
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
if ($byException) { |
|
546
|
|
|
$scenarios = array_keys($data); |
|
547
|
|
|
foreach ($byException as $attribute => $except) { |
|
548
|
|
|
foreach ($scenarios as $key => $scenario) { |
|
549
|
|
|
if (!$except || !in_array($scenario, $except)) { |
|
550
|
|
|
if (!array_key_exists($scenario, $data)) { |
|
551
|
|
|
$data[$scenario] = []; |
|
552
|
|
|
$data[$scenario][] = $attribute; |
|
553
|
|
|
} elseif (!in_array($attribute, $data[$scenario])) { |
|
554
|
|
|
$data[$scenario][] = $attribute; |
|
555
|
|
|
} |
|
556
|
|
|
} |
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
if (!$data) { |
|
562
|
|
|
return parent::scenarios(); |
|
563
|
|
|
} |
|
564
|
|
|
return $data; |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
/** |
|
568
|
|
|
* Default implementation returns an array of attributes associated with the current or specified scenario |
|
569
|
|
|
* @param string $scenario [optional] |
|
570
|
|
|
* @return array |
|
571
|
|
|
*/ |
|
572
|
|
|
public function getFormAttribs($scenario = null) { |
|
573
|
|
|
$scenario = (is_null($scenario) ? $this->getScenario() : $scenario); |
|
574
|
|
|
return ArrayHelper::getValue($this->scenarios(), $scenario, []); |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
|
|
/** |
|
578
|
|
|
* Get the default active field config for an attribute |
|
579
|
|
|
* |
|
580
|
|
|
* @param string $attribute Attribute name |
|
581
|
|
|
* @return array |
|
582
|
|
|
*/ |
|
583
|
|
|
public function getActiveFieldSettings($attribute) |
|
584
|
|
|
{ |
|
585
|
|
|
$settings = false; |
|
586
|
|
|
$attributeDefaults = $this->attributeActiveFieldSettings(); |
|
587
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
588
|
|
|
$settings = $attributeDefaults[$attribute]; |
|
589
|
|
|
} else { |
|
590
|
|
|
$settings = $this->getAttributeConfig($attribute, 'active'); |
|
591
|
|
|
} |
|
592
|
|
|
return ($settings ? $settings : []); |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* Get array of attributes and their default active field config |
|
597
|
|
|
* |
|
598
|
|
|
* @return array |
|
599
|
|
|
*/ |
|
600
|
|
|
public function attributeActiveFieldSettings() |
|
601
|
|
|
{ |
|
602
|
|
|
return [ |
|
603
|
|
|
]; |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
/** |
|
607
|
|
|
* Get the default hint block text to use for an attribute |
|
608
|
|
|
* |
|
609
|
|
|
* @param string $attribute Attribute name |
|
610
|
|
|
* @param boolean $encode [default false] Should the resulting string be encoded |
|
611
|
|
|
* @return string |
|
612
|
|
|
*/ |
|
613
|
|
|
public function getHintBlock($attribute, $encode = false) |
|
614
|
|
|
{ |
|
615
|
|
|
$message = ''; |
|
616
|
|
|
$attributeDefaults = $this->attributeHintBlocks(); |
|
617
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
618
|
|
|
$message = $attributeDefaults[$attribute]; |
|
619
|
|
|
} else { |
|
620
|
|
|
$message = $this->getAttributeConfig($attribute, 'hint'); |
|
621
|
|
|
} |
|
622
|
|
|
if ($message) { |
|
623
|
|
|
$message = ($encode ? Html::encode($message) : $message); |
|
624
|
|
|
} |
|
625
|
|
|
return $message; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
/** |
|
629
|
|
|
* Get array of attributes and what the default hint block should be |
|
630
|
|
|
* |
|
631
|
|
|
* @return array |
|
632
|
|
|
*/ |
|
633
|
|
|
public function attributeHintBlocks() |
|
634
|
|
|
{ |
|
635
|
|
|
return [ |
|
636
|
|
|
]; |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
/** |
|
640
|
|
|
* Get the default hint block text to use for an attribute when flagged as readonly |
|
641
|
|
|
* |
|
642
|
|
|
* @param string $attribute Attribute name |
|
643
|
|
|
* @param boolean $encode [default false] Should the resulting string be encoded |
|
644
|
|
|
* @return string |
|
645
|
|
|
*/ |
|
646
|
|
|
public function getReadOnlyHintBlock($attribute, $encode = false) |
|
647
|
|
|
{ |
|
648
|
|
|
$message = ''; |
|
649
|
|
|
$attributeDefaults = $this->attributeReadOnlyHintBlocks(); |
|
650
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
651
|
|
|
$message = $attributeDefaults[$attribute]; |
|
652
|
|
|
} else { |
|
653
|
|
|
$message = $this->getAttributeConfig($attribute, 'readonlyhint'); |
|
654
|
|
|
} |
|
655
|
|
|
if ($message) { |
|
656
|
|
|
$message = ($encode ? Html::encode($message) : $message); |
|
657
|
|
|
} |
|
658
|
|
|
return $message; |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Get array of attributes and what the default hint block should be |
|
663
|
|
|
* when flagged as readonly |
|
664
|
|
|
* |
|
665
|
|
|
* @return array |
|
666
|
|
|
*/ |
|
667
|
|
|
public function attributeReadOnlyHintBlocks() |
|
668
|
|
|
{ |
|
669
|
|
|
return [ |
|
670
|
|
|
]; |
|
671
|
|
|
} |
|
672
|
|
|
|
|
673
|
|
|
/** |
|
674
|
|
|
* Get the default icon to use in input boxes for an attribute |
|
675
|
|
|
* |
|
676
|
|
|
* @param string $attribute Attribute name |
|
677
|
|
|
* @return string |
|
678
|
|
|
*/ |
|
679
|
|
|
public function getIcon($attribute) |
|
680
|
|
|
{ |
|
681
|
|
|
$icon = ''; |
|
682
|
|
|
$attributeDefaults = $this->attributeIcons(); |
|
683
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
684
|
|
|
$icon = $attributeDefaults[$attribute]; |
|
685
|
|
|
} else { |
|
686
|
|
|
$icon = $this->getAttributeConfig($attribute, 'icon'); |
|
687
|
|
|
} |
|
688
|
|
|
return ($icon ? $icon : ''); |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
/** |
|
692
|
|
|
* Get array of attributes and what the default icon should be associated with the attribute in forms |
|
693
|
|
|
* |
|
694
|
|
|
* @return array |
|
695
|
|
|
*/ |
|
696
|
|
|
public function attributeIcons() |
|
697
|
|
|
{ |
|
698
|
|
|
return [ |
|
699
|
|
|
]; |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
/** |
|
703
|
|
|
* Get the default place holder text to use in input boxes for an attribute |
|
704
|
|
|
* |
|
705
|
|
|
* @param string $attribute Attribute name |
|
706
|
|
|
* @param boolean $encode [default false] Should the resulting string be encoded |
|
707
|
|
|
* @return string |
|
708
|
|
|
*/ |
|
709
|
|
|
public function getPlaceholder($attribute, $encode = false) |
|
710
|
|
|
{ |
|
711
|
|
|
$text = ''; |
|
712
|
|
|
$attributeDefaults = $this->attributePlaceholders(); |
|
713
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
714
|
|
|
$text = $attributeDefaults[$attribute]; |
|
715
|
|
|
} else { |
|
716
|
|
|
$text = $this->getAttributeConfig($attribute, 'placeholder'); |
|
717
|
|
|
} |
|
718
|
|
|
if ($text) { |
|
719
|
|
|
$text = ($encode ? Html::encode($text) : $text); |
|
720
|
|
|
} |
|
721
|
|
|
return $text; |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
/** |
|
725
|
|
|
* Get array of attributes and what the default help block text should be |
|
726
|
|
|
* |
|
727
|
|
|
* @return array |
|
728
|
|
|
*/ |
|
729
|
|
|
public function attributePlaceholders() |
|
730
|
|
|
{ |
|
731
|
|
|
return [ |
|
732
|
|
|
]; |
|
733
|
|
|
} |
|
734
|
|
|
|
|
735
|
|
|
/** |
|
736
|
|
|
* Get the default tooltip text to use for an attribute |
|
737
|
|
|
* |
|
738
|
|
|
* @param string $attribute Attribute name |
|
739
|
|
|
* @param boolean $encode [default false] Should the resulting string be encoded |
|
740
|
|
|
* @return string |
|
741
|
|
|
*/ |
|
742
|
|
|
public function getTooltip($attribute, $encode = false) |
|
743
|
|
|
{ |
|
744
|
|
|
$message = ''; |
|
745
|
|
|
$attributeDefaults = $this->attributeTooltips(); |
|
746
|
|
|
if (array_key_exists($attribute, $attributeDefaults)) { |
|
747
|
|
|
$message = $attributeDefaults[$attribute]; |
|
748
|
|
|
} else { |
|
749
|
|
|
$message = $this->getAttributeConfig($attribute, 'tooltip'); |
|
750
|
|
|
} |
|
751
|
|
|
if ($message) { |
|
752
|
|
|
$message = ($encode ? Html::encode($message) : $message); |
|
753
|
|
|
} |
|
754
|
|
|
return $message; |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
/** |
|
758
|
|
|
* Get array of attributes and what the default tooltip should be |
|
759
|
|
|
* |
|
760
|
|
|
* @return array |
|
761
|
|
|
*/ |
|
762
|
|
|
public function attributeTooltips() |
|
763
|
|
|
{ |
|
764
|
|
|
return [ |
|
765
|
|
|
]; |
|
766
|
|
|
} |
|
767
|
|
|
|
|
768
|
|
|
/** |
|
769
|
|
|
* Obtain list of fields that need to have string lengths checked as part of beforeSave() |
|
770
|
|
|
* By default look at formConfig when using this trait rather than return an empty array |
|
771
|
|
|
* @return array |
|
772
|
|
|
*/ |
|
773
|
|
|
public function beforeSaveStringFields() |
|
774
|
|
|
{ |
|
775
|
|
|
$config = $this->attributeConfig(); |
|
776
|
|
|
if (!$config) { |
|
777
|
|
|
return []; |
|
778
|
|
|
} |
|
779
|
|
|
$data = []; |
|
780
|
|
|
foreach ($config as $attribute => $attrConfig) { |
|
781
|
|
|
if (ArrayHelper::getValue($attrConfig, 'truncateOnSave', false)) { |
|
782
|
|
|
$data[] = $attribute; |
|
783
|
|
|
} |
|
784
|
|
|
} |
|
785
|
|
|
return $data; |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
|
|
} |
|
789
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.