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
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function load($data) |
43
|
|
|
{ |
44
|
|
|
$this->setAttributes($data[$this->formName()]); |
45
|
|
|
$this->setResources($data[(new DomainResource())->formName()]); |
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return DomainService[] |
52
|
|
|
*/ |
53
|
|
|
public function getServices() |
54
|
|
|
{ |
55
|
|
|
return $this->createServices($this->tariff->resources); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return DomainService[] |
60
|
|
|
*/ |
61
|
|
|
public function getBaseServices() |
62
|
|
|
{ |
63
|
|
|
return $this->createServices($this->baseTariff->resources); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $resources |
68
|
|
|
* @return DomainService[] |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
|
protected function createServices($resources) |
72
|
|
|
{ |
73
|
|
|
$result = []; |
74
|
|
|
$resource = reset($resources); |
75
|
|
|
|
76
|
|
|
foreach ($resource->getServiceTypes() as $type => $name) { |
77
|
|
|
$service = new DomainService([ |
78
|
|
|
'name' => $name, |
79
|
|
|
'type' => $type, |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
foreach ($resources as $resource) { |
83
|
|
|
if ($service->tryResourceAssignation($resource) && $service->isFulfilled()) { |
84
|
|
|
$result[$type] = $service; |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setResources($resources) |
94
|
|
|
{ |
95
|
|
|
$result = []; |
96
|
|
|
foreach ($resources as $resource) { |
97
|
|
|
if ($resource instanceof DomainResource) { |
98
|
|
|
$result[] = $resource; |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$model = new DomainResource(['scenario' => 'create']); |
103
|
|
|
|
104
|
|
|
if ($model->load($resource, '') && $model->validate()) { |
105
|
|
|
$result[] = $model; |
106
|
|
|
} else { |
107
|
|
|
throw new UnprocessableEntityHttpException('Failed to load resource model'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->_resources = $result; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
public function getZoneResources($zone) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$id = $this->zones[$zone]; |
119
|
|
|
|
120
|
|
|
$result = []; |
121
|
|
|
|
122
|
|
|
foreach ($this->tariff->resources as $resource) { |
123
|
|
|
if ($resource->object_id == $id && $resource->isTypeCorrect()) { |
124
|
|
|
$result[$resource->type] = $resource; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// sorts $result by order of $resource->getAvailableTypes() |
129
|
|
|
$result = array_merge($resource->getAvailableTypes(), $result); |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $result; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function getZoneBaseResources($zone) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$id = $this->zones[$zone]; |
137
|
|
|
|
138
|
|
|
$result = []; |
139
|
|
|
|
140
|
|
|
foreach ($this->baseTariff->resources as $resource) { |
141
|
|
|
if ($resource->object_id == $id && $resource->isTypeCorrect()) { |
142
|
|
|
$result[$resource->type] = $resource; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// sorts $result by order of $resource->getAvailableTypes() |
147
|
|
|
$result = array_merge($resource->getAvailableTypes(), $result); |
|
|
|
|
148
|
|
|
|
149
|
|
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getZones() |
153
|
|
|
{ |
154
|
|
|
return $this->zones; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.