1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use app\modules\image\models\Image; |
4
|
|
|
use app\modules\shop\models\Category; |
5
|
|
|
use app\modules\shop\models\Product; |
6
|
|
|
use app\models\PropertyGroup; |
7
|
|
|
use app\models\Property; |
8
|
|
|
use app\models\PropertyStaticValues; |
9
|
|
|
use app\models\BaseObject; |
10
|
|
|
use app\models\ObjectStaticValues; |
11
|
|
|
use app\models\ObjectPropertyGroup; |
12
|
|
|
use app\models\PropertyHandler; |
13
|
|
|
use app\components\Helper; |
14
|
|
|
use yii\db\Migration; |
15
|
|
|
|
16
|
|
|
class m150605_094805_demo_data extends Migration |
17
|
|
|
{ |
18
|
|
|
protected $products = []; |
19
|
|
|
protected $properties = []; |
20
|
|
|
protected $values = []; |
21
|
|
|
protected $textHandlerId; |
22
|
|
|
protected $selectHandlerId; |
23
|
|
|
|
24
|
|
|
protected function getKey($name, $length = 20, $delimiter = '_') |
25
|
|
|
{ |
26
|
|
|
if ($delimiter == '_') { |
27
|
|
|
$name = str_replace('-', '_', Helper::createSlug($name)); |
28
|
|
|
} |
29
|
|
|
return mb_strlen($name) > $length |
30
|
|
|
? mb_substr($name, 0, $length - 5). $delimiter . mb_substr(md5($name), 0, 4) |
31
|
|
|
: $name; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function saveEav($id, $groupId, $name, $value) |
35
|
|
|
{ |
36
|
|
|
$key = $this->getKey($name); |
37
|
|
View Code Duplication |
if (!isset($this->properties[$key])) { |
38
|
|
|
$this->insert( |
39
|
|
|
Property::tableName(), |
40
|
|
|
[ |
41
|
|
|
'property_group_id' => $groupId, |
42
|
|
|
'name' => $name, |
43
|
|
|
'key' => $key, |
44
|
|
|
'property_handler_id' => $this->textHandlerId, |
45
|
|
|
'handler_additional_params' => '{}', |
46
|
|
|
'is_eav' => 1, |
47
|
|
|
'has_slugs_in_values' => 0, |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
$this->properties[$key] = $this->db->lastInsertID; |
51
|
|
|
} |
52
|
|
|
$this->insert( |
53
|
|
|
'{{%product_eav}}', |
54
|
|
|
[ |
55
|
|
|
'object_model_id' => $id, |
56
|
|
|
'property_group_id' => $groupId, |
57
|
|
|
'key' => $key, |
58
|
|
|
'value' => $value, |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function saveStatic($id, $key, $value) |
64
|
|
|
{ |
65
|
|
|
if (!isset($this->values[$value])) { |
66
|
|
|
$value = trim($value, '/ '); |
67
|
|
|
$this->insert( |
68
|
|
|
PropertyStaticValues::tableName(), |
69
|
|
|
[ |
70
|
|
|
'property_id' => $this->properties[$key], |
71
|
|
|
'name' => $value, |
72
|
|
|
'value' => $value, |
73
|
|
|
'slug' => Helper::createSlug($value), |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
$this->values[$value] = $this->db->lastInsertID; |
77
|
|
|
} |
78
|
|
|
$this->insert( |
79
|
|
|
ObjectStaticValues::tableName(), |
80
|
|
|
[ |
81
|
|
|
'object_id' => BaseObject::getForClass(Product::className())->id, |
|
|
|
|
82
|
|
|
'object_model_id' => $id, |
83
|
|
|
'property_static_value_id' => $this->values[$value], |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function up() |
89
|
|
|
{ |
90
|
|
|
mb_internal_encoding(Yii::$app->getModule('core')->internalEncoding); |
91
|
|
|
$data = include __DIR__ . DIRECTORY_SEPARATOR . 'demo-data.php'; |
92
|
|
|
$productObject = BaseObject::getForClass(Product::className()); |
|
|
|
|
93
|
|
|
/** @var PropertyHandler $handler */ |
94
|
|
|
$handler = PropertyHandler::findOne(['handler_class_name' => 'app\properties\handlers\text\TextProperty']); |
95
|
|
|
if (!is_null($handler)) { |
96
|
|
|
$this->textHandlerId = $handler->id; |
97
|
|
|
} |
98
|
|
|
$handler = PropertyHandler::findOne(['handler_class_name' => 'app\properties\handlers\select\SelectProperty']); |
99
|
|
|
if (!is_null($handler)) { |
100
|
|
|
$this->selectHandlerId = $handler->id; |
101
|
|
|
} |
102
|
|
|
$this->insert( |
103
|
|
|
PropertyGroup::tableName(), |
104
|
|
|
[ |
105
|
|
|
'object_id' => $productObject->id, |
106
|
|
|
'name' => 'Общая группа свойств', |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
$commonGroupId = $this->db->lastInsertID; |
110
|
|
|
$this->insert( |
111
|
|
|
Property::tableName(), |
112
|
|
|
[ |
113
|
|
|
'property_group_id' => $commonGroupId, |
114
|
|
|
'name' => 'Производитель', |
115
|
|
|
'key' => 'vendor', |
116
|
|
|
'property_handler_id' => $this->selectHandlerId, |
117
|
|
|
'handler_additional_params' => '{}', |
118
|
|
|
'has_static_values' => 1, |
119
|
|
|
'has_slugs_in_values' => 1, |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
$this->properties['vendor'] = $this->db->lastInsertID; |
123
|
|
|
$staticProperties = [ |
124
|
|
|
'Тип крепления бура', |
125
|
|
|
'Макс. энергия удара', |
126
|
|
|
'Количество скоростей работы', |
127
|
|
|
'Питание', |
128
|
|
|
'Тип процессора', |
129
|
|
|
'Тип памяти', |
130
|
|
|
'Частота памяти: 1600 МГц', |
131
|
|
|
'Количество слотов памяти', |
132
|
|
|
'Максимальный размер памяти', |
133
|
|
|
'Размер экрана: 15.6 "', |
134
|
|
|
'Тип экрана', |
135
|
|
|
'Тип видеоадаптера', |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
foreach ($data as $category) { |
140
|
|
|
$this->insert( |
141
|
|
|
Category::tableName(), |
142
|
|
|
[ |
143
|
|
|
'category_group_id' => 1, |
144
|
|
|
'parent_id' => 1, |
145
|
|
|
'name' => $category['name'], |
146
|
|
|
'h1' => $category['name'], |
147
|
|
|
'title' => $category['name'] . ' с доставкой в любой город России и СНГ', |
148
|
|
|
'breadcrumbs_label' => $category['name'], |
149
|
|
|
'slug' => Helper::createSlug($category['name']), |
150
|
|
|
'announce' => $category['content'], |
151
|
|
|
'content' => $category['content'], |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
$categoryId = $this->db->lastInsertID; |
155
|
|
|
$this->insert( |
156
|
|
|
PropertyGroup::tableName(), |
157
|
|
|
[ |
158
|
|
|
'object_id' => $productObject->id, |
159
|
|
|
'name' => $category['name'], |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
$groupId = $this->db->lastInsertID; |
163
|
|
|
foreach ($category['products'] as $product) { |
164
|
|
|
// product |
165
|
|
|
$slug = Helper::createSlug($product['name']); |
166
|
|
|
if (isset($this->products[$slug])) { |
167
|
|
|
$slug = mb_substr($slug, 0, 66) . '-' . uniqid(); |
168
|
|
|
} |
169
|
|
|
$this->insert( |
170
|
|
|
Product::tableName(), |
171
|
|
|
[ |
172
|
|
|
'parent_id' => 0, |
173
|
|
|
'measure_id' => 1, |
174
|
|
|
'currency_id' => 1, |
175
|
|
|
'sku' => $product['id'], |
176
|
|
|
'main_category_id' => $categoryId, |
177
|
|
|
'name' => $product['name'], |
178
|
|
|
'title' => $product['name'], |
179
|
|
|
'breadcrumbs_label' => $product['name'], |
180
|
|
|
'h1' => $product['name'], |
181
|
|
|
'slug' => $slug, |
182
|
|
|
'announce' => Helper::trimPlain($product['description']), |
183
|
|
|
'content' => $product['description'], |
184
|
|
|
'price' => $product['prices']['min'], |
185
|
|
|
'old_price' => $product['prices']['max'], |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
$productId = $this->db->lastInsertID; |
189
|
|
|
$this->products[$slug] = $productId; |
190
|
|
|
// categories |
191
|
|
|
$this->batchInsert( |
192
|
|
|
'{{%product_category}}', |
193
|
|
|
['category_id', 'object_model_id'], |
194
|
|
|
[ |
195
|
|
|
[1, $productId], |
196
|
|
|
[$categoryId, $productId], |
197
|
|
|
] |
198
|
|
|
); |
199
|
|
|
// property groups |
200
|
|
|
$this->batchInsert( |
201
|
|
|
ObjectPropertyGroup::tableName(), |
202
|
|
|
['object_id', 'object_model_id', 'property_group_id'], |
203
|
|
|
[ |
204
|
|
|
[$productObject->id, $productId, $commonGroupId], |
205
|
|
|
[$productObject->id, $productId, $groupId], |
206
|
|
|
] |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
// properties |
210
|
|
|
if (isset($product['vendor'])) { |
211
|
|
|
$this->saveStatic($productId, 'vendor', $product['vendor']); |
212
|
|
|
} |
213
|
|
|
foreach ($product['details']['modelDetails'] as $group) { |
214
|
|
|
foreach ($group['params'] as $property) { |
215
|
|
|
$property['name'] = trim($property['name'], '/ '); |
216
|
|
|
if (in_array($property['name'], $staticProperties)) { |
217
|
|
|
$key = $this->getKey($property['name']); |
218
|
|
View Code Duplication |
if (!isset($this->properties[$key])) { |
219
|
|
|
$this->insert( |
220
|
|
|
Property::tableName(), |
221
|
|
|
[ |
222
|
|
|
'property_group_id' => $groupId, |
223
|
|
|
'name' => $property['name'], |
224
|
|
|
'key' => $key, |
225
|
|
|
'property_handler_id' => $this->selectHandlerId, |
226
|
|
|
'handler_additional_params' => '{}', |
227
|
|
|
'has_static_values' => 1, |
228
|
|
|
'has_slugs_in_values' => 1, |
229
|
|
|
] |
230
|
|
|
); |
231
|
|
|
$this->properties[$key] = $this->db->lastInsertID; |
232
|
|
|
} |
233
|
|
|
$this->saveStatic( |
234
|
|
|
$productId, |
235
|
|
|
$this->getKey($property['name']), |
236
|
|
|
str_replace($property['name'] . ': ', '', $property['value']) |
237
|
|
|
); |
238
|
|
|
} else { |
239
|
|
|
$this->saveEav( |
240
|
|
|
$productId, |
241
|
|
|
$groupId, |
242
|
|
|
$property['name'], |
243
|
|
|
str_replace($property['name'] . ': ', '', $property['value']) |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
// images |
249
|
|
|
$prodPhotos = []; |
250
|
|
View Code Duplication |
if (isset($product['photos'])) { |
251
|
|
|
foreach ($product['photos'] as $photo) { |
252
|
|
|
$prodPhotos[] = [ |
253
|
|
|
$productObject->id, |
254
|
|
|
$productId, |
255
|
|
|
$photo['name'], |
256
|
|
|
$product['name'], |
257
|
|
|
$product['name'], |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
View Code Duplication |
if (isset($product['mainPhoto']['name'])) { |
262
|
|
|
$prodPhotos[] = [ |
263
|
|
|
$productObject->id, |
264
|
|
|
$productId, |
265
|
|
|
$product['mainPhoto']['name'], |
266
|
|
|
$product['name'], |
267
|
|
|
$product['name'], |
268
|
|
|
]; |
269
|
|
|
} |
270
|
|
|
if (count($prodPhotos) > 0) { |
271
|
|
|
$this->batchInsert( |
272
|
|
|
Image::tableName(), |
273
|
|
|
[ |
274
|
|
|
'object_id', |
275
|
|
|
'object_model_id', |
276
|
|
|
'filename', |
277
|
|
|
'image_alt', |
278
|
|
|
'image_title', |
279
|
|
|
], |
280
|
|
|
$prodPhotos |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
srand(); |
286
|
|
|
$cdnNumber = rand(1, 2); |
287
|
|
|
$imgUrl = "http://static-{$cdnNumber}.dotplant.ru/demo-photos.zip"; |
288
|
|
|
$imagesPath = Yii::getAlias('@webroot/files/'); |
289
|
|
|
$imgsFile = $imagesPath.DIRECTORY_SEPARATOR .'imgs.zip'; |
290
|
|
|
if (file_exists($imgsFile) === false) { |
291
|
|
|
$fp = fopen($imgsFile, 'w+'); |
292
|
|
|
$ch = curl_init($imgUrl); |
293
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 600); |
294
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
295
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
296
|
|
|
curl_exec($ch); |
297
|
|
|
curl_close($ch); |
298
|
|
|
fclose($fp); |
299
|
|
|
} |
300
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
301
|
|
|
echo "\n\nWow! You are running windows! Please unzip $imgsFile to $imagesPath \n\n"; |
302
|
|
|
} else { |
303
|
|
|
passthru('/usr/bin/env unzip -n "' . $imgsFile . '" -d "' . $imagesPath . '"'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function down() |
309
|
|
|
{ |
310
|
|
|
echo "Sorry...\n"; |
311
|
|
|
return false; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.