1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\forms; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\DomainResource; |
6
|
|
|
use hipanel\modules\finance\models\DomainService; |
7
|
|
|
use hipanel\modules\finance\models\Tariff; |
8
|
|
|
use yii\web\UnprocessableEntityHttpException; |
9
|
|
|
|
10
|
|
|
class DomainTariffForm extends AbstractTariffForm |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array Domain zones |
14
|
|
|
* Key - zone name (com, net, ...) |
15
|
|
|
* Value - zone id |
16
|
|
|
* @see getZones |
17
|
|
|
*/ |
18
|
|
|
protected $zones; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param array $zones |
22
|
|
|
* @param Tariff $baseTariff |
23
|
|
|
* @param Tariff $tariff |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function fill($zones, Tariff $baseTariff, Tariff $tariff = null) |
27
|
|
|
{ |
28
|
|
|
$this->tariff = isset($tariff) ? $tariff : $baseTariff; |
29
|
|
|
$this->baseTariff = $baseTariff; |
30
|
|
|
$this->zones = array_flip($zones); |
31
|
|
|
|
32
|
|
|
if (isset($tariff)) { |
33
|
|
|
$this->id = $this->tariff->id ?: null; |
|
|
|
|
34
|
|
|
$this->name = $this->tariff->name; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->parent_id = $this->baseTariff->id; |
|
|
|
|
38
|
|
|
|
39
|
|
|
foreach ($this->tariff->resources as $resource) { |
40
|
|
|
$resource->scenario = $this->scenario; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function load($data) |
47
|
|
|
{ |
48
|
|
|
$this->setAttributes($data[$this->formName()]); |
49
|
|
|
$this->setResources($data[(new DomainResource())->formName()]); |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return DomainService[] |
56
|
|
|
*/ |
57
|
|
|
public function getServices() |
58
|
|
|
{ |
59
|
|
|
return $this->createServices($this->tariff->resources); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return DomainService[] |
64
|
|
|
*/ |
65
|
|
|
public function getBaseServices() |
66
|
|
|
{ |
67
|
|
|
return $this->createServices($this->baseTariff->resources); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $resources |
72
|
|
|
* @return DomainService[] |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
|
|
protected function createServices($resources) |
76
|
|
|
{ |
77
|
|
|
$result = []; |
78
|
|
|
$resource = reset($resources); |
79
|
|
|
|
80
|
|
|
foreach ($resource->getServiceTypes() as $type => $name) { |
81
|
|
|
$service = new DomainService([ |
82
|
|
|
'name' => $name, |
83
|
|
|
'type' => $type, |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
foreach ($resources as $resource) { |
87
|
|
|
if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) { |
88
|
|
|
$result[$type] = $service; |
89
|
|
|
break; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $result; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setResources($resources) |
98
|
|
|
{ |
99
|
|
|
$result = []; |
100
|
|
|
foreach ($resources as $resource) { |
101
|
|
|
if ($resource instanceof DomainResource) { |
102
|
|
|
$result[] = $resource; |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$model = new DomainResource(['scenario' => $this->scenario]); |
107
|
|
|
|
108
|
|
|
if ($model->load($resource, '') && $model->validate()) { |
109
|
|
|
$result[] = $model; |
110
|
|
|
} else { |
111
|
|
|
throw new UnprocessableEntityHttpException('Failed to load resource model: ' . reset($model->getFirstErrors())); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->_resources = $result; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function getZoneResources($zone) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$id = $this->zones[$zone]; |
123
|
|
|
|
124
|
|
|
$result = []; |
125
|
|
|
|
126
|
|
|
foreach ($this->tariff->resources as $resource) { |
127
|
|
|
if ($resource->object_id == $id && $resource->isTypeCorrect()) { |
128
|
|
|
$result[$resource->type] = $resource; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// sorts $result by order of $resource->getAvailableTypes() |
133
|
|
|
$result = array_merge($resource->getAvailableTypes(), $result); |
|
|
|
|
134
|
|
|
|
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
View Code Duplication |
public function getZoneBaseResources($zone) |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
$id = $this->zones[$zone]; |
141
|
|
|
|
142
|
|
|
$result = []; |
143
|
|
|
|
144
|
|
|
foreach ($this->baseTariff->resources as $resource) { |
145
|
|
|
if ($resource->object_id == $id && $resource->isTypeCorrect()) { |
146
|
|
|
$result[$resource->type] = $resource; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// sorts $result by order of $resource->getAvailableTypes() |
151
|
|
|
$result = array_merge($resource->getAvailableTypes(), $result); |
|
|
|
|
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getZones() |
157
|
|
|
{ |
158
|
|
|
return $this->zones; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.