1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\forms; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\Tariff; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
abstract class AbstractTariffForm extends \yii\base\Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int Tariff ID |
14
|
|
|
*/ |
15
|
|
|
public $id; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string Tariff name |
19
|
|
|
*/ |
20
|
|
|
public $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int Parent tariff ID |
24
|
|
|
*/ |
25
|
|
|
public $parent_id; |
26
|
|
|
/** @var Tariff */ |
27
|
|
|
public $baseTariff; |
28
|
|
|
/** @var Tariff[] */ |
29
|
|
|
public $baseTariffs; |
30
|
|
|
/** @var Tariff */ |
31
|
|
|
protected $tariff; |
32
|
|
|
/** @var Resource[] */ |
33
|
|
|
protected $_resources; |
34
|
|
|
|
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
$this->initTariff(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function initTariff() |
41
|
|
|
{ |
42
|
|
|
$this->selectBaseTariff(); |
43
|
|
|
$this->ensureTariff(); |
44
|
|
|
$this->ensureScenario(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function ensureTariff() |
48
|
|
|
{ |
49
|
|
|
if ($this->getTariff() instanceof Tariff) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->setDefaultTariff(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function ensureScenario() |
57
|
|
|
{ |
58
|
|
|
foreach ($this->tariff->resources as $resource) { |
59
|
|
|
$resource->scenario = $this->scenario; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function setDefaultTariff() |
64
|
|
|
{ |
65
|
|
|
$this->setTariff($this->baseTariff); |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function rules() |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
[['name'], 'required', 'on' => ['create', 'update']], |
75
|
|
|
[['parent_id', 'id'], 'integer', 'on' => ['create', 'update']], |
76
|
|
|
[['parent_id'], 'required', 'on' => ['create']], |
77
|
|
|
[['id'], 'required', 'on' => ['update']], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @inheritdoc */ |
82
|
|
|
public function fields() |
83
|
|
|
{ |
84
|
|
|
return ArrayHelper::merge(array_combine($this->attributes(), $this->attributes()), [ |
85
|
|
|
'resources' => '_resources' |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @inheritdoc */ |
90
|
|
|
public function attributes() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'id', |
94
|
|
|
'parent_id', |
95
|
|
|
'name', |
96
|
|
|
]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getResources() |
100
|
|
|
{ |
101
|
|
|
return $this->_resources; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $resources |
106
|
|
|
* @throws InvalidConfigException when not implemented |
107
|
|
|
*/ |
108
|
|
|
public function setResources($resources) |
109
|
|
|
{ |
110
|
|
|
throw new InvalidConfigException('Method "setResources" must be implemented'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getResourceTypes() |
114
|
|
|
{ |
115
|
|
|
return reset($this->baseTariff->resources)->getTypes(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function attributeLabels() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
'parent_id' => Yii::t('hipanel/finance/tariff', 'Parent tariff'), |
122
|
|
|
'name' => Yii::t('hipanel/finance/tariff', 'Name'), |
123
|
|
|
'label' => Yii::t('hipanel/finance/tariff', 'Label'), |
124
|
|
|
'note' => Yii::t('hipanel', 'Note'), |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $data to be loaded |
130
|
|
|
* @return bool |
131
|
|
|
* @throws InvalidConfigException when not implemented |
132
|
|
|
*/ |
133
|
|
|
public function load($data) |
134
|
|
|
{ |
135
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function selectBaseTariff() |
139
|
|
|
{ |
140
|
|
|
if (!isset($this->parent_id)) { |
141
|
|
|
if (isset($this->tariff)) { |
142
|
|
|
$this->parent_id = $this->tariff->parent_id; |
|
|
|
|
143
|
|
|
} else { |
144
|
|
|
$this->parent_id = ArrayHelper::getValue(reset($this->baseTariffs), 'id'); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$filtered = array_filter($this->baseTariffs, function ($model) { |
149
|
|
|
return $model->id == $this->parent_id; |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
if (count($filtered) !== 1) { |
153
|
|
|
Yii::error('Found ' . count($filtered) . ' base tariffs. Must be exactly one'); |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->baseTariff = reset($filtered); |
158
|
|
|
$this->parent_id = $this->baseTariff->id; |
159
|
|
|
|
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getBaseTariffsList() |
164
|
|
|
{ |
165
|
|
|
return array_combine( |
166
|
|
|
ArrayHelper::getColumn($this->baseTariffs, 'id'), |
167
|
|
|
ArrayHelper::getColumn($this->baseTariffs, 'name') |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function insert($runValidation = true, $attributes = null, $options = []) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function update($runValidation = true, $attributes = null, $options = []) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
throw new InvalidConfigException("Method load must be implemented"); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return Tariff |
183
|
|
|
*/ |
184
|
|
|
public function getTariff() |
185
|
|
|
{ |
186
|
|
|
return $this->tariff; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function setTariff($tariff) |
190
|
|
|
{ |
191
|
|
|
if ($tariff === null) { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->tariff = $tariff; |
196
|
|
|
|
197
|
|
|
$this->id = $tariff->id; |
198
|
|
|
$this->name = $tariff->name; |
199
|
|
|
|
200
|
|
|
return true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.