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\server\models\Config; |
||
14 | use hipanel\modules\server\models\Osimage; |
||
15 | use hipanel\modules\server\models\Package; |
||
16 | use hipanel\modules\server\widgets\cart\OrderPositionDescriptionWidget; |
||
17 | use Yii; |
||
18 | use yii\base\InvalidConfigException; |
||
19 | use yii\helpers\ArrayHelper; |
||
20 | |||
21 | class ServerOrderDedicatedProduct extends AbstractServerProduct |
||
22 | { |
||
23 | /** {@inheritdoc} */ |
||
24 | protected $_purchaseModel = ServerOrderDedicatedPurchase::class; |
||
25 | |||
26 | /** @var Package */ |
||
27 | protected $_model; |
||
28 | |||
29 | /** {@inheritdoc} */ |
||
30 | protected $_calculationModel = ConfigCalculation::class; |
||
31 | |||
32 | /** {@inheritdoc} */ |
||
33 | protected $duration = [1]; |
||
34 | |||
35 | /** |
||
36 | * @var Osimage the selected OS image detailed information |
||
37 | */ |
||
38 | protected $_image; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | public $location; |
||
44 | |||
45 | /** |
||
46 | * @var integer |
||
47 | */ |
||
48 | public $object_id; |
||
49 | |||
50 | /** |
||
51 | * @var integer |
||
52 | */ |
||
53 | public $tariff_id; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | public $label; |
||
59 | |||
60 | /** |
||
61 | * @var string link to any kind of social network |
||
62 | */ |
||
63 | public $administration; |
||
64 | |||
65 | /** |
||
66 | * @var string osimage name. Is used to load [[_image]] on product initialisation |
||
67 | */ |
||
68 | public $osimage; |
||
69 | |||
70 | /** |
||
71 | * @var string software package name |
||
72 | */ |
||
73 | public $softpack; |
||
74 | |||
75 | /** {@inheritdoc} */ |
||
76 | public function load($data, $formName = null) |
||
77 | { |
||
78 | if ($result = parent::load($data, '')) { |
||
79 | $this->ensureRelatedData(); |
||
80 | } |
||
81 | |||
82 | return $result; |
||
83 | } |
||
84 | |||
85 | /** {@inheritdoc} */ |
||
86 | protected function ensureRelatedData() |
||
87 | { |
||
88 | $configs = Config::find(['batch' => true])->getAvailable()->withSellerOptions()->andWhere(['id' => $this->object_id])->all(); |
||
89 | if (empty($configs)) { |
||
90 | throw new InvalidConfigException('Failed to find config'); |
||
91 | } |
||
92 | $this->_model = reset($configs); |
||
93 | |||
94 | $this->_image = Osimage::find()->where(['osimage' => $this->osimage, 'type' => 'dedicated'])->one(); |
||
95 | if ($this->_image === null) { |
||
96 | throw new InvalidConfigException('Failed to find osimage'); |
||
97 | } |
||
98 | $this->name = $this->_model->name; |
||
99 | $this->description = $this->_model->descr; |
||
100 | } |
||
101 | |||
102 | /** {@inheritdoc} */ |
||
103 | public function getId() |
||
104 | { |
||
105 | if ($this->_id === null) { |
||
106 | $this->_id = hash('crc32b', implode('_', ['server', 'order', 'dedicated', $this->_model->id, mt_rand()])); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
107 | } |
||
108 | |||
109 | return $this->_id; |
||
110 | } |
||
111 | |||
112 | /** {@inheritdoc} */ |
||
113 | public function getCalculationModel($options = []) |
||
114 | { |
||
115 | return parent::getCalculationModel(array_merge([ |
||
116 | 'tariff_id' => $this->tariff_id, |
||
117 | 'object_id' => $this->object_id, |
||
118 | 'location' => $this->location, |
||
119 | 'image' => $this->osimage, |
||
120 | ], $options)); |
||
121 | } |
||
122 | |||
123 | /** {@inheritdoc} */ |
||
124 | public function getPurchaseModel($options = []) |
||
125 | { |
||
126 | $this->ensureRelatedData(); |
||
127 | |||
128 | $options = array_merge([ |
||
129 | 'osimage' => $this->osimage, |
||
130 | 'object_id' => $this->object_id, |
||
131 | 'label' => $this->label, |
||
132 | 'administration' => $this->administration, |
||
133 | 'softpack' => $this->softpack, |
||
134 | 'tariff_id' => $this->tariff_id, |
||
135 | ], $options); |
||
136 | |||
137 | return parent::getPurchaseModel($options); |
||
138 | } |
||
139 | |||
140 | /** {@inheritdoc} */ |
||
141 | public function rules() |
||
142 | { |
||
143 | return array_merge(parent::rules(), [ |
||
144 | [['object_id', 'tariff_id'], 'integer'], |
||
145 | [['administration', 'osimage', 'label', 'location'], 'string'], |
||
146 | [['tariff_id', 'object_id', 'osimage', 'location'], 'required'], |
||
147 | ]); |
||
148 | } |
||
149 | |||
150 | /** {@inheritdoc} */ |
||
151 | public function attributeLabels() |
||
152 | { |
||
153 | return ArrayHelper::merge(parent::attributeLabels(), [ |
||
154 | 'object_id' => Yii::t('hipanel:server:order', 'Server config'), |
||
155 | 'label' => Yii::t('hipanel:server:order', 'Label'), |
||
156 | ]); |
||
157 | } |
||
158 | |||
159 | /** {@inheritdoc} */ |
||
160 | public function attributeHints() |
||
161 | { |
||
162 | return ArrayHelper::merge(parent::attributeHints(), [ |
||
163 | 'label' => Yii::t('hipanel:server:order', 'How are you going to use the server?'), |
||
164 | 'administration' => Yii::t('hipanel:server:order', 'Any social network link. Will be used in case of emergency contact'), |
||
165 | ]); |
||
166 | } |
||
167 | |||
168 | /** {@inheritdoc} */ |
||
169 | public function renderDescription() |
||
170 | { |
||
171 | return OrderPositionDescriptionWidget::widget(['position' => $this]); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return Osimage |
||
176 | */ |
||
177 | public function getImage() |
||
178 | { |
||
179 | return $this->_image; |
||
180 | } |
||
181 | |||
182 | protected function serializationMap() |
||
183 | { |
||
184 | $parent = parent::serializationMap(); |
||
185 | $parent['object_id'] = $this->object_id; |
||
186 | $parent['osimage'] = $this->osimage; |
||
187 | $parent['label'] = $this->label; |
||
188 | $parent['administration'] = $this->administration; |
||
189 | $parent['softpack'] = $this->softpack; |
||
190 | $parent['tariff_id'] = $this->tariff_id; |
||
191 | $parent['location'] = $this->location; |
||
192 | $parent['_image'] = $this->_image; |
||
193 | |||
194 | return $parent; |
||
195 | } |
||
196 | } |
||
197 |