1 | <?php |
||||
2 | /** |
||||
3 | * Server module for HiPanel |
||||
4 | * |
||||
5 | * @link https://github.com/hiqdev/hipanel-module-server |
||||
6 | * @package hipanel-module-server |
||||
7 | * @license BSD-3-Clause |
||||
8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||||
9 | */ |
||||
10 | |||||
11 | namespace hipanel\modules\server\cart; |
||||
12 | |||||
13 | use hipanel\modules\finance\cart\Calculation; |
||||
14 | use hipanel\modules\server\helpers\ServerHelper; |
||||
15 | use hipanel\modules\server\models\Osimage; |
||||
16 | use hipanel\modules\server\models\Package; |
||||
17 | use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget; |
||||
18 | use Yii; |
||||
19 | use yii\base\InvalidConfigException; |
||||
20 | use yii\helpers\ArrayHelper; |
||||
21 | |||||
22 | class ServerOrderProduct extends AbstractServerProduct |
||||
23 | { |
||||
24 | /** {@inheritdoc} */ |
||||
25 | protected $_purchaseModel = ServerOrderPurchase::class; |
||||
26 | |||||
27 | /** @var Package */ |
||||
28 | protected $_model; |
||||
29 | |||||
30 | /** {@inheritdoc} */ |
||||
31 | protected $_calculationModel = Calculation::class; |
||||
32 | |||||
33 | /** |
||||
34 | * @var Osimage the selected OS image detailed information |
||||
35 | */ |
||||
36 | protected $_image; |
||||
37 | |||||
38 | /** |
||||
39 | * @var integer |
||||
40 | */ |
||||
41 | public $tariff_id; |
||||
42 | |||||
43 | /** |
||||
44 | * @var integer id of cluster. See [[hipanel\modules\server\models\Package::getLocations()]] for details |
||||
45 | * @see \hipanel\modules\server\models\Package::getLocations() |
||||
46 | */ |
||||
47 | public $cluster_id; |
||||
48 | |||||
49 | /** |
||||
50 | * @var string the purpose for the server |
||||
51 | */ |
||||
52 | public $purpose; |
||||
53 | |||||
54 | /** |
||||
55 | * @var string link to any kind of social network |
||||
56 | */ |
||||
57 | public $social; |
||||
58 | |||||
59 | /** |
||||
60 | * @var string osimage name. Is used to load [[_image]] on product initialisation |
||||
61 | */ |
||||
62 | public $osimage; |
||||
63 | |||||
64 | /** |
||||
65 | * @var string software package name |
||||
66 | */ |
||||
67 | public $panel_soft; |
||||
68 | |||||
69 | /** {@inheritdoc} */ |
||||
70 | public function load($data, $formName = null) |
||||
71 | { |
||||
72 | if ($result = parent::load($data, '')) { |
||||
73 | $this->ensureRelatedData(); |
||||
74 | } |
||||
75 | |||||
76 | return $result; |
||||
77 | } |
||||
78 | |||||
79 | /** {@inheritdoc} */ |
||||
80 | private function ensureRelatedData() |
||||
81 | { |
||||
82 | $this->_model = ServerHelper::getAvailablePackages(null, $this->tariff_id); |
||||
83 | if ($this->_model === null) { |
||||
84 | throw new InvalidConfigException('Failed to find tariff'); |
||||
85 | } |
||||
86 | |||||
87 | $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => $this->_model->getType()])->one(); |
||||
88 | if ($this->_image === null) { |
||||
89 | throw new InvalidConfigException('Failed to find osimage'); |
||||
90 | } |
||||
91 | |||||
92 | $this->name = $this->_model->getName(); |
||||
93 | $this->description = Yii::t('hipanel:server:order', 'Order'); |
||||
94 | } |
||||
95 | |||||
96 | /** {@inheritdoc} */ |
||||
97 | public function getId() |
||||
98 | { |
||||
99 | if ($this->_id === null) { |
||||
100 | $this->_id = hash('crc32b', implode('_', ['server', 'order', $this->_model->id])); |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
101 | } |
||||
102 | |||||
103 | return $this->_id; |
||||
104 | } |
||||
105 | |||||
106 | /** {@inheritdoc} */ |
||||
107 | public function getCalculationModel($options = []) |
||||
108 | { |
||||
109 | return parent::getCalculationModel(array_merge([ |
||||
110 | 'tariff_id' => $this->tariff_id, |
||||
111 | 'object' => 'server', |
||||
112 | ], $options)); |
||||
113 | } |
||||
114 | |||||
115 | /** {@inheritdoc} */ |
||||
116 | public function getPurchaseModel($options = []) |
||||
117 | { |
||||
118 | $this->ensureRelatedData(); // To get fresh domain expiration date |
||||
119 | |||||
120 | $options = array_merge([ |
||||
121 | 'osimage' => $this->osimage, |
||||
122 | 'panel' => $this->_image->getPanelName(), |
||||
123 | 'cluster_id' => $this->cluster_id, |
||||
124 | 'social' => $this->social, |
||||
125 | 'purpose' => $this->purpose, |
||||
126 | 'tariff_id' => $this->tariff_id, |
||||
127 | ], $options); |
||||
128 | |||||
129 | if ($options['panel'] === Osimage::NO_PANEL) { |
||||
130 | unset($options['panel']); |
||||
131 | } |
||||
132 | |||||
133 | return parent::getPurchaseModel($options); |
||||
134 | } |
||||
135 | |||||
136 | /** {@inheritdoc} */ |
||||
137 | public function rules() |
||||
138 | { |
||||
139 | return array_merge(parent::rules(), [ |
||||
140 | [['cluster_id', 'tariff_id'], 'integer'], |
||||
141 | [['social', 'osimage'], 'safe'], |
||||
142 | [['tariff_id', 'purpose', 'osimage'], 'required'], |
||||
143 | [['cluster_id'], 'validateClusterId'], |
||||
144 | ]); |
||||
145 | } |
||||
146 | |||||
147 | public function validateClusterId($value) |
||||
0 ignored issues
–
show
The parameter
$value is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||
148 | { |
||||
149 | return in_array($this->cluster_id, array_keys($this->_model->getLocations()), true); |
||||
150 | } |
||||
151 | |||||
152 | /** {@inheritdoc} */ |
||||
153 | public function attributeLabels() |
||||
154 | { |
||||
155 | return ArrayHelper::merge(parent::attributeLabels(), [ |
||||
156 | 'cluster_id' => Yii::t('hipanel:server:order', 'Server location'), |
||||
157 | 'purpose' => Yii::t('hipanel:server:order', 'Purpose'), |
||||
158 | 'social' => Yii::t('hipanel:server:order', 'Social network'), |
||||
159 | ]); |
||||
160 | } |
||||
161 | |||||
162 | /** {@inheritdoc} */ |
||||
163 | public function attributeHints() |
||||
164 | { |
||||
165 | return ArrayHelper::merge(parent::attributeHints(), [ |
||||
166 | 'purpose' => Yii::t('hipanel:server:order', 'How are you going to use the server?'), |
||||
167 | 'social' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'), |
||||
168 | ]); |
||||
169 | } |
||||
170 | |||||
171 | /** {@inheritdoc} */ |
||||
172 | public function renderDescription() |
||||
173 | { |
||||
174 | return OrderPositionDescriptionWidget::widget(['position' => $this]); |
||||
175 | } |
||||
176 | |||||
177 | /** |
||||
178 | * @return Osimage |
||||
179 | */ |
||||
180 | public function getImage() |
||||
181 | { |
||||
182 | return $this->_image; |
||||
183 | } |
||||
184 | |||||
185 | protected function serializationMap() |
||||
186 | { |
||||
187 | $parent = parent::serializationMap(); |
||||
188 | $parent['cluster_id'] = $this->cluster_id; |
||||
189 | $parent['osimage'] = $this->osimage; |
||||
190 | $parent['panel_soft'] = $this->panel_soft; |
||||
191 | $parent['purpose'] = $this->purpose; |
||||
192 | $parent['social'] = $this->social; |
||||
193 | $parent['tariff_id'] = $this->tariff_id; |
||||
194 | $parent['_image'] = $this->_image; |
||||
195 | |||||
196 | return $parent; |
||||
197 | } |
||||
198 | } |
||||
199 |