|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace import_export\classes; |
|
4
|
|
|
|
|
5
|
|
|
use CI_DB_active_record; |
|
6
|
|
|
use CI_DB_result; |
|
7
|
|
|
use CI_Model; |
|
8
|
|
|
use Core; |
|
9
|
|
|
use core\models\Route; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
use import_export\classes\ProductsImport as ProductsHandler; |
|
12
|
|
|
use MY_Controller; |
|
13
|
|
|
use SPropertyValue; |
|
14
|
|
|
use SPropertyValueQuery; |
|
15
|
|
|
|
|
16
|
|
|
(defined('BASEPATH')) OR exit('No direct script access allowed'); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @property Core $core |
|
20
|
|
|
* @property CI_DB_active_record $db |
|
21
|
|
|
*/ |
|
22
|
|
|
class BaseImport extends CI_Model |
|
23
|
|
|
{ |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class BaseImport |
|
27
|
|
|
* @var BaseImport |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $_instance; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Id currency |
|
33
|
|
|
* @var Int |
|
34
|
|
|
*/ |
|
35
|
|
|
public $currency = 2; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Charset |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $encoding = 'utf-8'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* language |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
public $languages = 'ru'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* language |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
public $mainLanguages; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Path to file |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
public $CSVsource = ''; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* CSV delimiter |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public $delimiter = ';'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* CSV enclosure |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
public $enclosure = '"'; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Import type |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
public $importType = ''; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Attributes |
|
81
|
|
|
* @var array |
|
82
|
|
|
*/ |
|
83
|
|
|
public $attributes = ''; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The maximum number of fields |
|
87
|
|
|
* @var int |
|
88
|
|
|
*/ |
|
89
|
|
|
public $maxRowLength = 0; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Content |
|
93
|
|
|
* @var array |
|
94
|
|
|
*/ |
|
95
|
|
|
public $content = []; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Settings |
|
99
|
|
|
* @var array |
|
100
|
|
|
*/ |
|
101
|
|
|
public $settings = []; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Possible attributes |
|
105
|
|
|
* @var array |
|
106
|
|
|
*/ |
|
107
|
|
|
public $possibleAttributes = []; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Count products in CSV file |
|
111
|
|
|
* @var int |
|
112
|
|
|
*/ |
|
113
|
|
|
public $countProduct; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @var array |
|
117
|
|
|
*/ |
|
118
|
|
|
public $allLanguages = []; |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @var int |
|
122
|
|
|
*/ |
|
123
|
|
|
public $maxRowLegth; |
|
124
|
|
|
|
|
125
|
|
|
public function __construct() { |
|
126
|
|
|
parent::__construct(); |
|
127
|
|
|
$this->languages = MY_Controller::getCurrentLocale(); |
|
128
|
|
|
$this->languages = $this->input->post('language') ?: $this->languages; |
|
129
|
|
|
$this->getLangs(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function getLangs() { |
|
133
|
|
|
$langs = $this->db->get('languages')->result(); |
|
134
|
|
|
foreach ($langs as $val) { |
|
135
|
|
|
$this->allLanguages[] = $val->identif; |
|
136
|
|
|
if ($val->default == 1) { |
|
137
|
|
|
$this->mainLanguages = $val->identif; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* BaseImport Singleton |
|
144
|
|
|
* @return BaseImport |
|
145
|
|
|
* @access public |
|
146
|
|
|
* @author Kaero |
|
147
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function create() { |
|
150
|
|
|
(null !== self::$_instance) OR self::$_instance = new self(); |
|
151
|
|
|
return self::$_instance; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Start CSV Import |
|
156
|
|
|
* @param integer $offers The final position |
|
157
|
|
|
* @param integer $limit Step |
|
158
|
|
|
* @param integer $countProd count products |
|
159
|
|
|
* @param $EmptyFields |
|
|
|
|
|
|
160
|
|
|
* @return false|null |
|
161
|
|
|
* @access public |
|
162
|
|
|
* @author Kaero |
|
163
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
164
|
|
|
*/ |
|
165
|
|
|
public function makeImport($offers, $limit, $countProd, $EmptyFields) { |
|
166
|
|
|
|
|
167
|
|
|
$this->makeAttributesList(); |
|
168
|
|
|
if ($offers == 0) { |
|
169
|
|
|
$this->validateFile($offers, $limit); |
|
170
|
|
|
} else { |
|
171
|
|
|
$this->parseFile($offers, $limit); |
|
172
|
|
|
$this->loadCategories($EmptyFields); |
|
173
|
|
|
ProductsHandler::create()->make($EmptyFields); |
|
174
|
|
|
$this->runProperties(); |
|
175
|
|
|
} |
|
176
|
|
|
if (ImportBootstrap::noErrors()) { |
|
177
|
|
|
ImportBootstrap::create()->addMessage(Factor::SuccessImportCompleted . '<b>' . $countProd . '</b>', Factor::MessageTypeSuccess); |
|
178
|
|
|
} else { |
|
179
|
|
|
return FALSE; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get attributes list. |
|
185
|
|
|
* @return BaseImport |
|
186
|
|
|
* @access public |
|
187
|
|
|
* @author Kaero |
|
188
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
189
|
|
|
*/ |
|
190
|
|
|
public function makeAttributesList() { |
|
191
|
|
|
if (!count($this->possibleAttributes)) { |
|
192
|
|
|
$this->possibleAttributes = [ |
|
193
|
|
|
'skip' => lang('Skip column', 'import_export'), |
|
194
|
|
|
'name' => lang('Product Name', 'import_export'), |
|
195
|
|
|
'archive' => lang('Archive', 'import_export'), |
|
196
|
|
|
'url' => lang('URL', 'import_export'), |
|
197
|
|
|
'prc' => lang('Price', 'import_export'), |
|
198
|
|
|
'oldprc' => lang('Old Price', 'import_export'), |
|
199
|
|
|
'stk' => lang('Amount', 'import_export'), |
|
200
|
|
|
'num' => lang('Article', 'import_export'), |
|
201
|
|
|
'var' => lang('Variant name', 'import_export'), |
|
202
|
|
|
'act' => lang('Active', 'import_export'), |
|
203
|
|
|
'hit' => lang('Hit', 'import_export'), |
|
204
|
|
|
'hot' => lang('Hot', 'import_export'), |
|
205
|
|
|
'action' => lang('Action', 'import_export'), |
|
206
|
|
|
'brd' => lang('Brand', 'import_export'), |
|
207
|
|
|
'cat' => lang('Category', 'import_export'), |
|
208
|
|
|
'addcats' => lang('Additional categories', 'import_export'), |
|
209
|
|
|
'relp' => lang('Related products', 'import_export'), |
|
210
|
|
|
'vimg' => lang('Main image variant', 'import_export'), |
|
211
|
|
|
'cur' => lang('Currencies', 'import_export'), |
|
212
|
|
|
'imgs' => lang('Additional images', 'import_export'), |
|
213
|
|
|
'shdesc' => lang('Short description', 'import_export'), |
|
214
|
|
|
'desc' => lang('Full description', 'import_export'), |
|
215
|
|
|
'mett' => lang('Meta Title', 'import_export'), |
|
216
|
|
|
'metd' => lang('Meta Description', 'import_export'), |
|
217
|
|
|
'metk' => lang('Meta Keywords', 'import_export'), |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
$properties = $this->db->query( |
|
221
|
|
|
' |
|
222
|
|
|
SELECT shop_product_properties.id, shop_product_properties.csv_name, shop_product_properties_i18n.name |
|
223
|
|
|
FROM `shop_product_properties` |
|
224
|
|
|
LEFT OUTER JOIN `shop_product_properties_i18n` ON shop_product_properties.id = shop_product_properties_i18n.id |
|
225
|
|
|
WHERE `csv_name` != "" AND shop_product_properties_i18n.locale = ? |
|
226
|
|
|
', |
|
227
|
|
|
$this->languages |
|
228
|
|
|
)->result(); |
|
229
|
|
|
|
|
230
|
|
|
foreach ($properties as $property) { |
|
231
|
|
|
$this->possibleAttributes[$property->csv_name] = $property->name; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Validate Information and parse CSV. As a goal we have $content variable with file information. |
|
239
|
|
|
* @param integer $offers The final position |
|
240
|
|
|
* @param integer $limit Step |
|
241
|
|
|
* @return false|null |
|
242
|
|
|
* @access public |
|
243
|
|
|
* @author Kaero |
|
244
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
245
|
|
|
*/ |
|
246
|
|
|
public function validateFile($offers, $limit) { |
|
247
|
|
|
|
|
248
|
|
|
if (substr(sprintf('%o', fileperms(ImportBootstrap::getUploadDir())), -4) != '0777') { |
|
249
|
|
|
ImportBootstrap::addMessage(Factor::ErrorFolderPermission); |
|
250
|
|
|
return FALSE; |
|
251
|
|
|
} |
|
252
|
|
|
if (!$file = @fopen($this->CSVsource, 'r')) { |
|
253
|
|
|
ImportBootstrap::addMessage(Factor::ErrorFileReadError); |
|
254
|
|
|
return FALSE; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
$row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure); |
|
258
|
|
|
|
|
259
|
|
View Code Duplication |
if (!in_array('prc', $row) && !$this->attributeExist('prc')) { |
|
260
|
|
|
ImportBootstrap::addMessage(Factor::ErrorPriceAttribute); |
|
261
|
|
|
return FALSE; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
View Code Duplication |
if (!in_array('num', $row) && !$this->attributeExist('prc') && $this->importType == Factor::ImportProducts) { |
|
265
|
|
|
ImportBootstrap::addMessage(Factor::ErrorNumberAttribute); |
|
266
|
|
|
return FALSE; |
|
267
|
|
|
} |
|
268
|
|
|
if ((count($this->possibleAttributes) - count(array_diff($this->possibleAttributes, $row))) == count($this->attributes)) { |
|
269
|
|
|
$this->attributes = $row; |
|
270
|
|
|
} elseif (count($row) === count($this->attributes)) { |
|
271
|
|
|
rewind($file); |
|
272
|
|
|
} else { |
|
273
|
|
|
ImportBootstrap::addMessage(Factor::ErrorPossibleAttrValues); |
|
274
|
|
|
return FALSE; |
|
275
|
|
|
} |
|
276
|
|
|
$this->parseFile($offers, $limit, $file); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param string $attribute |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
|
|
public function attributeExist($attribute) { |
|
284
|
|
|
$attributes = \CI::$APP->input->post('attributes'); |
|
285
|
|
|
if (!$attributes) { |
|
286
|
|
|
return TRUE; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$attributes = explode(',', $attributes); |
|
290
|
|
|
return in_array($attribute, $attributes) ? TRUE : FALSE; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* File parsing |
|
295
|
|
|
* @param integer $offers The final position |
|
296
|
|
|
* @param integer $limit Step |
|
297
|
|
|
* @param $file |
|
|
|
|
|
|
298
|
|
|
* @return boolean |
|
299
|
|
|
*/ |
|
300
|
|
|
public function parseFile($offers, $limit, $file = false) { |
|
301
|
|
|
if (!$file) { |
|
302
|
|
|
$file = @fopen($this->CSVsource, 'r'); |
|
303
|
|
|
} |
|
304
|
|
|
if ($offers > 0) { |
|
305
|
|
|
$positionStart = $offers - $limit; |
|
306
|
|
|
$cnt = 0; |
|
307
|
|
|
$iOffer = 0; |
|
308
|
|
|
while (($row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure)) !== false) { |
|
309
|
|
|
|
|
310
|
|
|
if ($cnt != 0) { |
|
311
|
|
|
if ($iOffer < $positionStart) { |
|
312
|
|
|
$iOffer++; |
|
313
|
|
|
} else { |
|
314
|
|
|
$this->content[] = array_combine($this->attributes, array_map('trim', $row)); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
if ($cnt >= $offers) { |
|
318
|
|
|
break; |
|
319
|
|
|
} |
|
320
|
|
|
$cnt++; |
|
321
|
|
|
} |
|
322
|
|
|
} else { |
|
323
|
|
|
$cnt = 0; |
|
324
|
|
|
while (($row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure)) !== false) { |
|
325
|
|
|
if ($cnt != 0) { |
|
326
|
|
|
$this->countProduct++; |
|
327
|
|
|
} |
|
328
|
|
|
$cnt = 1; |
|
329
|
|
|
} |
|
330
|
|
|
$_SESSION['countProductsInFile'] = $this->countProduct; |
|
331
|
|
|
} |
|
332
|
|
|
fclose($file); |
|
333
|
|
|
return TRUE; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Process Categories |
|
338
|
|
|
* @access public |
|
339
|
|
|
* @author Kaero |
|
340
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
341
|
|
|
*/ |
|
342
|
|
|
public function loadCategories() { |
|
343
|
|
|
if (ImportBootstrap::hasErrors()) { |
|
344
|
|
|
return FALSE; |
|
345
|
|
|
} |
|
346
|
|
|
$this->load->helper('translit'); |
|
347
|
|
|
foreach ($this->content as $key => $node) { |
|
348
|
|
|
if ($node['cat'] == '') { |
|
349
|
|
|
continue; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
if (trim($node['addcats'])) { |
|
353
|
|
|
$cats = explode('|', $node['addcats']); |
|
354
|
|
|
foreach ($cats as $cat) { |
|
355
|
|
|
$this->content(['cat' => $cat], $key); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
$this->content($node, $key); |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @param string $node |
|
364
|
|
|
* @param string $key |
|
365
|
|
|
*/ |
|
366
|
|
|
private function content($node, $key) { |
|
367
|
|
|
|
|
368
|
|
|
$parts = $this->parseCategoryName($node['cat']); |
|
369
|
|
|
$pathIds = $pathNames = []; |
|
370
|
|
|
$parentId = $line = 0; |
|
|
|
|
|
|
371
|
|
|
foreach ($parts as $part) { |
|
372
|
|
|
|
|
373
|
|
|
/* Find existing category */ |
|
374
|
|
|
$binds = [ |
|
375
|
|
|
$part, |
|
376
|
|
|
$this->languages, |
|
377
|
|
|
$parentId, |
|
378
|
|
|
]; |
|
379
|
|
|
// $binds = array($part, $this->mainLanguages, $parentId); |
|
380
|
|
|
$result = $this->db->query( |
|
381
|
|
|
' |
|
382
|
|
|
SELECT SCategory.id AS CategoryId |
|
383
|
|
|
FROM `shop_category_i18n` AS SCategoryI18n |
|
384
|
|
|
RIGHT OUTER JOIN `shop_category` AS SCategory ON SCategory.id = SCategoryI18n.id |
|
385
|
|
|
WHERE SCategoryI18n.name = ? AND SCategoryI18n.locale = ? AND SCategory.parent_id = ?', |
|
386
|
|
|
$binds |
|
387
|
|
|
); |
|
388
|
|
|
if ($result) { |
|
389
|
|
|
$result = $result->row(); |
|
390
|
|
|
} else { |
|
391
|
|
|
Logger::create()->set('Error $result in CategoryImport.php - IMPORT'); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
if (!($result instanceof \stdClass)) { |
|
395
|
|
|
/* Create new category */ |
|
396
|
|
|
$lastPosition = $this->db->query('SELECT max(position) AS maxPos FROM `shop_category`')->row()->maxPos; |
|
397
|
|
|
$binds = [ |
|
398
|
|
|
'parent_id' => $parentId, |
|
399
|
|
|
'full_path_ids' => serialize($pathIds), |
|
400
|
|
|
'active' => 1, |
|
401
|
|
|
'position' => $lastPosition + 1, |
|
402
|
|
|
]; |
|
403
|
|
|
|
|
404
|
|
|
$this->db->insert('shop_category', $binds); |
|
405
|
|
|
$newCategoryId = $this->db->insert_id(); |
|
406
|
|
|
|
|
407
|
|
|
if ($newCategoryId) { |
|
408
|
|
|
$route = [ |
|
409
|
|
|
'parent_url' => implode('/', array_map('translit_url', $pathNames)), |
|
410
|
|
|
'url' => translit_url($part), |
|
411
|
|
|
'entity_id' => $newCategoryId, |
|
412
|
|
|
'type' => Route::TYPE_SHOP_CATEGORY, |
|
413
|
|
|
]; |
|
414
|
|
|
|
|
415
|
|
|
$this->db->insert('route', $route); |
|
416
|
|
|
|
|
417
|
|
|
$newRouteId = $this->db->insert_id(); |
|
418
|
|
|
|
|
419
|
|
|
$this->db->update('shop_category', ['route_id' => $newRouteId], ['id' => $newCategoryId]); |
|
420
|
|
|
} |
|
421
|
|
|
if (!$newCategoryId) { |
|
422
|
|
|
Logger::create()->set('Error INSERT category or SELECT id new category in CategoryImport.php - IMPORT'); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/* Add translation data for new category */ |
|
426
|
|
View Code Duplication |
foreach ($this->allLanguages as $val) { |
|
427
|
|
|
$this->db->insert('shop_category_i18n', ['id' => $newCategoryId, 'locale' => $val, 'name' => trim($part)]); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
$this->content[$key]['CategoryId'] = $pathIds[] = $parentId = $newCategoryId; |
|
431
|
|
|
$this->content[$key]['CategoryIds'] = $pathIds; |
|
432
|
|
|
} else { |
|
433
|
|
|
$this->content[$key]['CategoryId'] = $pathIds[] = $parentId = $result->CategoryId; |
|
434
|
|
|
$this->content[$key]['CategoryIds'] = $pathIds; |
|
435
|
|
|
} |
|
436
|
|
|
$pathNames[] = $part; |
|
437
|
|
|
|
|
438
|
|
|
} |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* Parse Category Name by slashes |
|
443
|
|
|
* @param string $name |
|
444
|
|
|
* @return array |
|
445
|
|
|
* @access private |
|
446
|
|
|
* @author Kaero |
|
447
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
448
|
|
|
*/ |
|
449
|
|
|
private function parseCategoryName($name) { |
|
450
|
|
|
$result = array_map('trim', array_map('stripcslashes', preg_split('/\\REPLACE((?:[^\\\\\REPLACE]|\\\\.)*)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY))); |
|
451
|
|
|
return explode('/', $result[0]); |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* FROM PropertiesImport |
|
456
|
|
|
*/ |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Process Properties Handling |
|
460
|
|
|
* @access public |
|
461
|
|
|
* @author Kaero |
|
462
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
463
|
|
|
*/ |
|
464
|
|
|
public function runProperties() { |
|
465
|
|
|
if (ImportBootstrap::hasErrors()) { |
|
466
|
|
|
return FALSE; |
|
467
|
|
|
} |
|
468
|
|
|
$properties = $this->db->query('SELECT `id`, `csv_name` FROM `shop_product_properties`')->result(); |
|
469
|
|
|
foreach ($properties as $property) { |
|
470
|
|
|
$properyAlias[$property->csv_name] = $property->id; |
|
|
|
|
|
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
foreach ($this->content as $node) { |
|
474
|
|
|
foreach ($node as $nodeKey => $nodeElement) { |
|
475
|
|
|
|
|
476
|
|
|
if (array_key_exists($nodeKey, $properyAlias)) { |
|
477
|
|
|
$result = $this->db->query('SELECT * FROM `shop_product_properties_data` WHERE `product_id` = ? AND `property_id` = ?', [$node['ProductId'], $properyAlias[$nodeKey]])->row(); |
|
478
|
|
|
|
|
479
|
|
|
if ($result instanceof \stdClass) { |
|
480
|
|
|
$this->db->delete( |
|
481
|
|
|
'shop_product_properties_data', |
|
482
|
|
|
[ |
|
483
|
|
|
'product_id' => $node['ProductId'], |
|
484
|
|
|
'property_id' => $properyAlias[$nodeKey], |
|
485
|
|
|
'locale' => $this->languages, |
|
486
|
|
|
] |
|
487
|
|
|
); |
|
488
|
|
|
} |
|
489
|
|
|
$values = array_map('trim', explode('|', $nodeElement)); |
|
490
|
|
|
foreach ($values as $v) { |
|
491
|
|
|
$v = htmlspecialchars($v); |
|
492
|
|
|
if ($v !== '') { |
|
493
|
|
|
|
|
494
|
|
|
$property_value = SPropertyValueQuery::create() |
|
495
|
|
|
->useSPropertyValueI18nQuery() |
|
496
|
|
|
->filterByLocale($this->languages) |
|
497
|
|
|
->filterByValue($v) |
|
498
|
|
|
->endUse() |
|
499
|
|
|
->findOneByPropertyId($properyAlias[$nodeKey]); |
|
500
|
|
|
|
|
501
|
|
|
if (!$property_value) { |
|
502
|
|
|
|
|
503
|
|
|
$property_value = new SPropertyValue(); |
|
504
|
|
|
$property_value->setPropertyId($properyAlias[$nodeKey]); |
|
505
|
|
|
$property_value->setLocale($this->languages); |
|
506
|
|
|
$property_value->setValue($v); |
|
507
|
|
|
$property_value->save(); |
|
508
|
|
|
|
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
$this->checkPropertiesData($properyAlias[$nodeKey], $node['ProductId'], $property_value->getId()); |
|
512
|
|
|
|
|
513
|
|
|
} |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
foreach ($node['CategoryIds'] as $categoryId) { |
|
517
|
|
|
$result = $this->db->query('SELECT * FROM `shop_product_properties_categories` WHERE `category_id` = ? AND `property_id` = ?', [$categoryId, $properyAlias[$nodeKey]])->row(); |
|
518
|
|
|
if (!($result instanceof \stdClass) && !empty($nodeElement)) { |
|
519
|
|
|
$this->db->insert('shop_product_properties_categories', ['property_id' => $properyAlias[$nodeKey], 'category_id' => $categoryId]); |
|
520
|
|
|
} |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
$propery = $this->db->query( |
|
524
|
|
|
' |
|
525
|
|
|
SELECT `id`, `name` |
|
526
|
|
|
FROM `shop_product_properties_i18n` |
|
527
|
|
|
WHERE id = ? AND locale = ?', |
|
528
|
|
|
[ |
|
529
|
|
|
$properyAlias[$nodeKey], |
|
530
|
|
|
$this->languages, |
|
531
|
|
|
] |
|
532
|
|
|
)->row(); |
|
533
|
|
|
$data = (!empty($propery->data)) ? unserialize($propery->data) : []; |
|
534
|
|
|
$changed = false; |
|
535
|
|
|
foreach ($values as $v) { |
|
536
|
|
|
if (!in_array($v, $data, true)) { |
|
537
|
|
|
$changed = true; |
|
538
|
|
|
$data[] = $v; |
|
539
|
|
|
} |
|
540
|
|
|
} |
|
541
|
|
|
if ($changed) { |
|
542
|
|
|
$this->db->update('shop_product_properties_i18n', ['data' => serialize($data)], ['id' => $properyAlias[$nodeKey], 'locale' => $this->languages]); |
|
543
|
|
|
} |
|
544
|
|
|
} |
|
545
|
|
|
} |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* @param int $property_id |
|
552
|
|
|
* @param int $product_id |
|
553
|
|
|
* @param int $value_id |
|
554
|
|
|
* @return bool |
|
|
|
|
|
|
555
|
|
|
*/ |
|
556
|
|
|
private function checkPropertiesData($property_id, $product_id, $value_id) { |
|
557
|
|
|
|
|
558
|
|
|
$import_data = [ |
|
559
|
|
|
'property_id' => $property_id, |
|
560
|
|
|
'product_id' => $product_id, |
|
561
|
|
|
'value_id' => $value_id, |
|
562
|
|
|
]; |
|
563
|
|
|
|
|
564
|
|
|
/** @var CI_DB_result $test */ |
|
565
|
|
|
$test = $this->db->get_where('shop_product_properties_data', $import_data); |
|
566
|
|
|
|
|
567
|
|
|
if ($test->num_rows() > 0) { |
|
568
|
|
|
return false; |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
$this->db->insert('shop_product_properties_data', $import_data); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* Set Import Type. Must be setted before import start. |
|
576
|
|
|
* @param $type |
|
|
|
|
|
|
577
|
|
|
* @return BaseImport |
|
578
|
|
|
* @access public |
|
579
|
|
|
* @author Kaero |
|
580
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
581
|
|
|
*/ |
|
582
|
|
|
public function setImportType($type) { |
|
583
|
|
|
$this->importType = $type; |
|
584
|
|
|
return $this; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* FROM CategoryImport |
|
589
|
|
|
*/ |
|
590
|
|
|
|
|
591
|
|
|
/** |
|
592
|
|
|
* Set Import Settings. Must be setted before import start. |
|
593
|
|
|
* @param $settings |
|
|
|
|
|
|
594
|
|
|
* @return BaseImport |
|
595
|
|
|
* @access public |
|
596
|
|
|
* @author Kaero |
|
597
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
598
|
|
|
*/ |
|
599
|
|
|
public function setSettings($settings) { |
|
600
|
|
|
$this->settings = $settings; |
|
601
|
|
|
$this->attributes = array_diff(explode(',', $this->settings['attributes']), [null]); |
|
602
|
|
|
return $this; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
/** |
|
606
|
|
|
* Set Import file name. Must be setted before import start. |
|
607
|
|
|
* @param string $fileName |
|
608
|
|
|
* @return BaseImport |
|
|
|
|
|
|
609
|
|
|
* @access public |
|
610
|
|
|
* @author Kaero |
|
611
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
612
|
|
|
*/ |
|
613
|
|
|
public function setFileName($fileName) { |
|
614
|
|
|
try { |
|
615
|
|
|
if (FALSE === file_exists($fileName)) { |
|
616
|
|
|
throw new Exception(Factor::ErrorEmptySlot); |
|
617
|
|
|
} |
|
618
|
|
|
$this->CSVsource = $fileName; |
|
619
|
|
|
return $this; |
|
620
|
|
|
} catch (Exception $exc) { |
|
621
|
|
|
$result[Factor::MessageTypeSuccess] = FALSE; |
|
|
|
|
|
|
622
|
|
|
$result[Factor::MessageTypeError] = FALSE; |
|
623
|
|
|
$result['message'] = $exc->getMessage(); |
|
624
|
|
|
echo json_encode($result); |
|
625
|
|
|
exit(); |
|
626
|
|
|
} |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* Add new value to custom field and save it. |
|
631
|
|
|
* @param mixed $name |
|
632
|
|
|
* @param mixed $value |
|
633
|
|
|
* @access public |
|
634
|
|
|
* @return void |
|
|
|
|
|
|
635
|
|
|
* @author Kaero |
|
636
|
|
|
* @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
|
637
|
|
|
*/ |
|
638
|
|
|
public function addCustomFieldValue($name, $value) { |
|
639
|
|
|
if (array_key_exists($name, $this->customFieldsCache)) { |
|
640
|
|
|
$fieldDataArray = $this->customFieldsCache[$name]->getDataArray(); |
|
641
|
|
|
|
|
642
|
|
|
if ($fieldDataArray === null) { |
|
643
|
|
|
$fieldDataArray = []; |
|
644
|
|
|
} |
|
645
|
|
|
|
|
646
|
|
|
if (!in_array($value, $fieldDataArray)) { |
|
647
|
|
|
array_push($fieldDataArray, $value); |
|
648
|
|
|
$newData = implode("\n", $fieldDataArray); |
|
649
|
|
|
$this->customFieldsCache[$name]->setData($newData); |
|
650
|
|
|
$this->customFieldsCache[$name]->save(); |
|
651
|
|
|
$this->customFieldsCache[$name]->setVirtualColumn('dataArray', $fieldDataArray); |
|
652
|
|
|
$this->customFieldsCache[$name]->setData($newData); |
|
653
|
|
|
} |
|
654
|
|
|
} |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
} |