|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Demo |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\demo\handlers; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\models\File as FileModel, |
|
13
|
|
|
gplcart\core\models\Product as ProductModel, |
|
14
|
|
|
gplcart\core\models\Category as CategoryModel, |
|
15
|
|
|
gplcart\core\models\Collection as CollectionModel, |
|
16
|
|
|
gplcart\core\models\CollectionItem as CollectionItemModel, |
|
17
|
|
|
gplcart\core\models\CategoryGroup as CategoryGroupModel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Handler for Demo module |
|
21
|
|
|
*/ |
|
22
|
|
|
class Demo |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* File model instance |
|
27
|
|
|
* @var \gplcart\core\models\File $file |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $file; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Category model instance |
|
33
|
|
|
* @var \gplcart\core\models\Category $category |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $category; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Category group model instance |
|
39
|
|
|
* @var \gplcart\core\models\CategoryGroup $category_group |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $category_group; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Product model instance |
|
45
|
|
|
* @var \gplcart\core\models\Product $product |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $product; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Collection model instance |
|
51
|
|
|
* @var \gplcart\core\models\Collection $collection |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $collection; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Collection item model instance |
|
57
|
|
|
* @var \gplcart\core\models\CollectionItem $collection_item |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $collection_item; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* An array of created entity IDs |
|
63
|
|
|
* Entity keys are preserved to guarantee their order upon deletion |
|
64
|
|
|
* E.g, collection items must be deleted before the related collection |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $created = array( |
|
68
|
|
|
'file' => array(), |
|
69
|
|
|
'product' => array(), |
|
70
|
|
|
'category' => array(), |
|
71
|
|
|
'category_group' => array(), |
|
72
|
|
|
'collection_item' => array(), |
|
73
|
|
|
'collection' => array() |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* The store ID |
|
78
|
|
|
* @var integer |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $store_id; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param FileModel $file |
|
84
|
|
|
* @param ProductModel $product |
|
85
|
|
|
* @param CategoryModel $category |
|
86
|
|
|
* @param CollectionModel $collection |
|
87
|
|
|
* @param CategoryGroupModel $category_group |
|
88
|
|
|
* @param CollectionItemModel $collection_item |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct(FileModel $file, ProductModel $product, |
|
91
|
|
|
CategoryModel $category, CollectionModel $collection, |
|
92
|
|
|
CategoryGroupModel $category_group, |
|
93
|
|
|
CollectionItemModel $collection_item) |
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
|
|
$this->file = $file; |
|
97
|
|
|
$this->product = $product; |
|
98
|
|
|
$this->category = $category; |
|
99
|
|
|
$this->collection = $collection; |
|
100
|
|
|
$this->category_group = $category_group; |
|
101
|
|
|
$this->collection_item = $collection_item; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Creates all demo content |
|
106
|
|
|
* @param integer $store_id |
|
107
|
|
|
* @param \gplcart\modules\demo\models\Demo $model |
|
108
|
|
|
* @return boolean |
|
109
|
|
|
*/ |
|
110
|
|
|
public function create($store_id, \gplcart\modules\demo\models\Demo $model) |
|
111
|
|
|
{ |
|
112
|
|
|
set_time_limit(0); |
|
113
|
|
|
|
|
114
|
|
|
$this->store_id = $store_id; |
|
115
|
|
|
|
|
116
|
|
|
$this->createCategoryGroups(); |
|
117
|
|
|
$this->createCategories(); |
|
118
|
|
|
$this->createProducts(); |
|
119
|
|
|
$this->createProductImages(); |
|
120
|
|
|
|
|
121
|
|
|
$this->createCollections(); |
|
122
|
|
|
$this->createFiles(); |
|
123
|
|
|
$this->createCollectionItems(); |
|
124
|
|
|
|
|
125
|
|
|
$model->setCreatedEntityId('default', $store_id, $this->created); |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Creates collections |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function createCollections() |
|
133
|
|
|
{ |
|
134
|
|
|
$data = include __DIR__ . '/../config/default/collection.php'; |
|
135
|
|
|
foreach ($data as $collection) { |
|
136
|
|
|
$this->created['collection'][$collection['title']] = $this->collection->add($collection); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Creates files |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function createFiles() |
|
144
|
|
|
{ |
|
145
|
|
|
$data = include __DIR__ . '/../config/default/file.php'; |
|
146
|
|
|
|
|
147
|
|
|
foreach ($data as $file) { |
|
148
|
|
|
|
|
149
|
|
|
$destination = $this->copyFile(realpath($file['path']), GC_IMAGE_DIR); |
|
150
|
|
|
|
|
151
|
|
|
if (empty($destination)) { |
|
152
|
|
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$data = array( |
|
156
|
|
|
'title' => $file['title'], |
|
157
|
|
|
'path' => gplcart_file_relative_path($destination) |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
$this->created['file'][$file['title']] = $this->file->add($data); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Creates collection items |
|
166
|
|
|
*/ |
|
167
|
|
|
protected function createCollectionItems() |
|
168
|
|
|
{ |
|
169
|
|
|
$data = include __DIR__ . '/../config/default/collection_item.php'; |
|
170
|
|
|
foreach ($data as $item) { |
|
171
|
|
|
$this->created['collection_item'][] = $this->collection_item->add($item); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Create category groups |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function createCategoryGroups() |
|
179
|
|
|
{ |
|
180
|
|
|
$data = include __DIR__ . '/../config/default/category_group.php'; |
|
181
|
|
|
foreach ($data as $category_group) { |
|
182
|
|
|
$this->created['category_group'][$category_group['title']] = $this->category_group->add($category_group); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Creates demo categories |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function createCategories() |
|
190
|
|
|
{ |
|
191
|
|
|
$data = include __DIR__ . '/../config/default/category.php'; |
|
192
|
|
|
foreach ($data as $category) { |
|
193
|
|
|
$this->created['category'][$category['title']] = $this->category->add($category); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Creates demo products |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function createProducts() |
|
201
|
|
|
{ |
|
202
|
|
|
$data = include __DIR__ . '/../config/default/product.php'; |
|
203
|
|
|
foreach ($data as $product) { |
|
204
|
|
|
$this->created['product'][$product['sku']] = $this->product->add($product); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Creates product images |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function createProductImages() |
|
212
|
|
|
{ |
|
213
|
|
|
foreach ($this->created['product'] as $sku => $product_id) { |
|
214
|
|
|
|
|
215
|
|
|
$directory = __DIR__ . "/../image/default/product/$sku"; |
|
216
|
|
|
|
|
217
|
|
|
if (!is_dir($directory)) { |
|
218
|
|
|
continue; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
foreach (glob("$directory/*.{jpg,png,gif}", GLOB_BRACE) as $file) { |
|
222
|
|
|
|
|
223
|
|
|
$destination = $this->copyFile($file, $this->product->getImagePath(true)); |
|
224
|
|
|
|
|
225
|
|
|
if (empty($destination)) { |
|
226
|
|
|
continue; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$data = array( |
|
230
|
|
|
'id_key' => 'product_id', |
|
231
|
|
|
'id_value' => $product_id, |
|
232
|
|
|
'path' => gplcart_file_relative_path($destination) |
|
233
|
|
|
); |
|
234
|
|
|
|
|
235
|
|
|
$this->created['file'][$destination] = $this->file->add($data); |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Copy files from module to file directory |
|
242
|
|
|
* @param string $source |
|
243
|
|
|
* @param string $directory |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function copyFile($source, $directory) |
|
247
|
|
|
{ |
|
248
|
|
|
if (file_exists($directory) || mkdir($directory, 0755, true)) { |
|
249
|
|
|
$destination = gplcart_file_unique("$directory/" . basename($source)); |
|
250
|
|
|
return copy($source, $destination) ? $destination : ''; |
|
251
|
|
|
} |
|
252
|
|
|
return ''; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Deletes all created demo content |
|
257
|
|
|
* @param integer $store_id |
|
258
|
|
|
* @param \gplcart\modules\demo\models\Demo $model |
|
259
|
|
|
*/ |
|
260
|
|
|
public function delete($store_id, $model) |
|
261
|
|
|
{ |
|
262
|
|
|
set_time_limit(0); |
|
263
|
|
|
|
|
264
|
|
|
foreach ($model->getCreatedEntityId('default', $store_id) as $entity => $ids) { |
|
265
|
|
|
foreach ($ids as $id) { |
|
266
|
|
|
// Don't call methods dynamically $this->{$entity} |
|
267
|
|
|
// to make them visible in IDE |
|
268
|
|
|
switch ($entity) { |
|
269
|
|
|
case 'product': |
|
270
|
|
|
$this->product->delete($id); |
|
271
|
|
|
break; |
|
272
|
|
|
case 'category': |
|
273
|
|
|
$this->category->delete($id); |
|
274
|
|
|
break; |
|
275
|
|
|
case 'category_group': |
|
276
|
|
|
$this->category_group->delete($id); |
|
277
|
|
|
break; |
|
278
|
|
|
case 'collection': |
|
279
|
|
|
$this->collection->delete($id); |
|
280
|
|
|
break; |
|
281
|
|
|
case 'collection_item': |
|
282
|
|
|
$this->collection_item->delete($id); |
|
283
|
|
|
break; |
|
284
|
|
|
case 'file': |
|
285
|
|
|
$this->file->deleteAll($id); |
|
286
|
|
|
break; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
$model->resetCreatedEntityId('default', $store_id); |
|
292
|
|
|
return true; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
|