1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\widgets; |
12
|
|
|
|
13
|
|
|
use hipanel\models\Ref; |
14
|
|
|
use hiqdev\hiart\ActiveRecord; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\base\Widget; |
17
|
|
|
use yii\helpers\ArrayHelper; |
18
|
|
|
use yii\helpers\Html; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class RefFilter widget. |
22
|
|
|
* |
23
|
|
|
* Usage: |
24
|
|
|
* RefFilter::widget([ |
25
|
|
|
* 'attribute' => 'state', |
26
|
|
|
* 'model' => $searchModel, |
27
|
|
|
* 'gtype' => 'state,domain', |
28
|
|
|
* ]); |
29
|
|
|
*/ |
30
|
|
|
class RefFilter extends Widget |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ActiveRecord |
34
|
|
|
*/ |
35
|
|
|
public $model; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $gtype; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array additional find options that will be passed to [[Ref]] model |
44
|
|
|
*/ |
45
|
|
|
public $findOptions = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $attribute; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string Dictionary name for i18n module to translate refs |
54
|
|
|
*/ |
55
|
|
|
public $i18nDictionary; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array of elements that will be merged with elements extracted from [[Ref]] |
59
|
|
|
* and passed to [[ArrayHelper::merge()]] as a second attribute |
60
|
|
|
* |
61
|
|
|
* @see ArrayHelper::merge() |
62
|
|
|
*/ |
63
|
|
|
public $elementOverrides = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
public $options = []; |
69
|
|
|
|
70
|
|
|
public static function widget($config = []) |
71
|
|
|
{ |
72
|
|
|
if (isset($config['options'])) { |
73
|
|
|
$options = &$config['options']; |
74
|
|
|
} |
75
|
|
|
$vars = get_class_vars(get_class()); |
76
|
|
|
foreach ($config as $k => $v) { |
77
|
|
|
if (array_key_exists($k, $vars)) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
$options[$k] = $v; |
|
|
|
|
81
|
|
|
unset($config[$k]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return parent::widget($config); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function run() |
88
|
|
|
{ |
89
|
|
|
parent::run(); |
90
|
|
|
|
91
|
|
|
return $this->renderInput(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function renderInput() |
95
|
|
|
{ |
96
|
|
|
return Html::activeDropDownList($this->model, $this->attribute, $this->getRefs(), ArrayHelper::merge([ |
97
|
|
|
'class' => 'form-control', |
98
|
|
|
'prompt' => Yii::t('hipanel', '----------'), |
99
|
|
|
], $this->options)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getRefs() |
103
|
|
|
{ |
104
|
|
|
$elements = Ref::getList($this->gtype, $this->i18nDictionary, $this->findOptions); |
105
|
|
|
|
106
|
|
|
return ArrayHelper::merge($elements, $this->elementOverrides); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: