ice_import - Version 1.8.0

Version Notes

- Updated logic of importing of products and categories
- Speed improvement
- Reduced memory usage

Download this release

Release Info

Developer IceShop
Extension ice_import
Version 1.8.0
Comparing to
See all releases


Code changes from version 1.7.23 to 1.8.0

Files changed (22) hide show
  1. app/code/community/ICEshop/Iceimport/Block/Adminhtml/Dashboard.php +0 -8
  2. app/code/community/ICEshop/Iceimport/Model/Convert/Adapter/Iceimport.php +1259 -0
  3. app/code/community/ICEshop/Iceimport/Model/Convert/Adapter/Product.php +0 -1868
  4. app/code/community/ICEshop/Iceimport/Model/Dataflow/Batch/Import.php +0 -1
  5. app/code/community/ICEshop/Iceimport/Model/Observer.php +62 -46
  6. app/code/community/ICEshop/Iceimport/Model/System/Config/Category.php +33 -0
  7. app/code/community/ICEshop/Iceimport/controllers/Adminhtml/IceimportController.php +0 -9
  8. app/code/community/ICEshop/Iceimport/controllers/Adminhtml/System/Convert/GuiController.php +40 -15
  9. app/code/community/ICEshop/Iceimport/etc/config.xml +4 -9
  10. app/code/community/ICEshop/Iceimport/etc/system.xml +28 -20
  11. app/code/community/ICEshop/Iceimport/include.php +11 -0
  12. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-install-1.7.22.php → mysql4-install-1.8.0.php} +0 -0
  13. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.15-1.7.22.php → mysql4-upgrade-1.7.15-1.8.0.php} +0 -0
  14. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.16-1.7.22.php → mysql4-upgrade-1.7.16-1.8.0.php} +0 -0
  15. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.17-1.7.22.php → mysql4-upgrade-1.7.17-1.8.0.php} +0 -0
  16. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.18-1.7.22.php → mysql4-upgrade-1.7.18-1.8.0.php} +0 -0
  17. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.19-1.7.22.php → mysql4-upgrade-1.7.19-1.8.0.php} +0 -0
  18. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.20-1.7.22.php → mysql4-upgrade-1.7.20-1.8.0.php} +0 -0
  19. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.21-1.7.22.php → mysql4-upgrade-1.7.21-1.8.0.php} +0 -0
  20. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/mysql4-upgrade-1.7.22-1.8.0.php +7 -0
  21. app/code/community/ICEshop/Iceimport/sql/iceimport_setup/{mysql4-upgrade-1.7.8-1.7.22.php → mysql4-upgrade-1.7.8-1.8.0.php} +0 -0
  22. package.xml +8 -15
app/code/community/ICEshop/Iceimport/Block/Adminhtml/Dashboard.php CHANGED
@@ -21,19 +21,11 @@ class ICEshop_Iceimport_Block_Adminhtml_Dashboard extends Mage_Adminhtml_Block_T
21
$last_imported_products_count = $db->getLogEntryByKey('iceimport_count_imported_products');
22
$import_filename = $db->getLogEntryByKey('import_filename');
23
$productivity = $checker->getModulePerformance($last_imported_products_count, $last_started_by_cron, $last_finished_by_cron);
24
- $currently_imported_products = $db->getRowsCount($db->_prefix . "iceshop_iceimport_imported_product_ids");
25
$data_flows = $db->getRowCountByField($db->getTableName('dataflow_batch_import'), 'batch_id', false, ' ORDER BY 1 DESC LIMIT 50');
26
27
$last_run = $db->readQuery("SELECT `performed_at` FROM {$table_name} WHERE `profile_id` = 3 ORDER BY `performed_at` DESC LIMIT 1");
28
$last_deleted_products_count = $db->getLogEntryByKey('iceimport_count_delete_product');
29
30
- if (!empty($currently_imported_products) && ($import_status_cron['log_value'] == 'Running' || $import_status_cron['log_value'] == 'Failed')) {
31
- $additional = array(
32
- 'label' => $helper->__("Currently products imported"),
33
- 'value' => $currently_imported_products . ' from ' . $data_flows[0]['row_count']
34
- );
35
- }
36
-
37
//packing response
38
$response = array(
39
array(
21
$last_imported_products_count = $db->getLogEntryByKey('iceimport_count_imported_products');
22
$import_filename = $db->getLogEntryByKey('import_filename');
23
$productivity = $checker->getModulePerformance($last_imported_products_count, $last_started_by_cron, $last_finished_by_cron);
24
$data_flows = $db->getRowCountByField($db->getTableName('dataflow_batch_import'), 'batch_id', false, ' ORDER BY 1 DESC LIMIT 50');
25
26
$last_run = $db->readQuery("SELECT `performed_at` FROM {$table_name} WHERE `profile_id` = 3 ORDER BY `performed_at` DESC LIMIT 1");
27
$last_deleted_products_count = $db->getLogEntryByKey('iceimport_count_delete_product');
28
29
//packing response
30
$response = array(
31
array(
app/code/community/ICEshop/Iceimport/Model/Convert/Adapter/Iceimport.php ADDED
@@ -0,0 +1,1259 @@
1
+ <?php
2
+
3
+ class Iceimport
4
+ {
5
+
6
+ public $arFields = false;
7
+
8
+ public $db_magento = null;
9
+
10
+ public $db_config = [];
11
+
12
+ public $tablePrefix = false;
13
+
14
+ public $arrayForPostfixes = [];
15
+
16
+ public $stores = [];
17
+
18
+ public $count_all_products = 0;
19
+
20
+ public $batch = 100000;
21
+
22
+ public $end = 0;
23
+
24
+ public $filenameCats = 'file.csv';
25
+
26
+ public $filenameValues = 'category_to_product.csv';
27
+
28
+ public $tempFeed = 'tempFeed.csv';
29
+
30
+ public $tempAttr = 'attr.csv';
31
+
32
+ public function __construct ()
33
+ {
34
+
35
+ $config = Mage::getConfig()->getResourceConnectionConfig("default_setup");
36
+
37
+ $dbinfo = ["host" => $config->host,
38
+ "user" => $config->username,
39
+ "pass" => $config->password,
40
+ "dbname" => $config->dbname
41
+ ];
42
+
43
+ $hostname = $dbinfo["host"];
44
+ $user = $dbinfo["user"];
45
+ $password = $dbinfo["pass"];
46
+ $dbname = $dbinfo["dbname"];
47
+
48
+ $this->db_config = [
49
+ 'host' => $hostname,
50
+ 'username' => $user,
51
+ 'password' => $password,
52
+ 'dbname' => $dbname,
53
+ 'driver_options' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8', PDO::MYSQL_ATTR_LOCAL_INFILE => true]
54
+ ];
55
+
56
+ $this->db_magento = Zend_Db::factory('Pdo_Mysql', $this->db_config);
57
+
58
+ $tablePrefix = (array)Mage::getConfig()->getTablePrefix();
59
+
60
+ if (!empty($tablePrefix)) {
61
+ $this->tablePrefix = $tablePrefix[0];
62
+ }
63
+
64
+ if (!empty(Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/batch_size'))) {
65
+ $this->batch = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/batch_size');
66
+ }
67
+ }
68
+
69
+ public function makeFileFromArrayForManualLaunching ($importProduct)
70
+ {
71
+
72
+ $fp = fopen($this->tempFeed, 'w+');
73
+
74
+ fputcsv($fp, array_keys($importProduct[0]), "\t");
75
+
76
+ foreach ($importProduct as $fields) {
77
+ fputcsv($fp, $fields, "\t");
78
+ }
79
+
80
+ fclose($fp);
81
+ }
82
+
83
+ public function getGuiData ()
84
+ {
85
+
86
+ $profileId = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/iceimport_profile');
87
+
88
+ if (!$profileId) {
89
+ $profileId = 3;
90
+ }
91
+
92
+ $select_gui_data = $this->db_magento->query("SELECT gui_data FROM `{$this->tablePrefix}dataflow_profile` WHERE profile_id = $profileId;");
93
+ $gui_data = $select_gui_data->fetch()['gui_data'];
94
+ $gui_data = unserialize($gui_data);
95
+ return $gui_data;
96
+ }
97
+
98
+ public function getFileByFTP ()
99
+ {
100
+
101
+ $gui_data = $this->getGuiData();
102
+ $file_name = $gui_data['file']['filename'];
103
+ $url = $gui_data['file']['host'];
104
+ $login = $gui_data['file']['user'];
105
+ $password = $gui_data['file']['password'];
106
+ $file_mode = $gui_data['file']['file_mode'];
107
+ $passive_mode = $gui_data['file']['passive'];
108
+ $path = $gui_data['file']['path'];
109
+ $tmp_file = sys_get_temp_dir() . DS . time() . '_' . $file_name;
110
+
111
+ if (file_exists($tmp_file)) {
112
+ unlink($tmp_file);
113
+ }
114
+
115
+ $conn_id = ftp_connect($url);
116
+
117
+ if (empty($conn_id)) {
118
+ throw new \Exception('Cannot connect by ftp for shop');
119
+ }
120
+
121
+ $login_result = ftp_login($conn_id, $login, $password);
122
+
123
+ if (empty($login_result)) {
124
+ ftp_close($conn_id);
125
+ throw new \Exception('Cannot login on ftp server for shop');
126
+ }
127
+ //set passive mode
128
+ if ($passive_mode == 1) {
129
+ ftp_pasv($conn_id, true);
130
+ }
131
+
132
+ $size = ftp_size($conn_id, $path . $file_name);
133
+ if ($size > 0) {
134
+
135
+ $res = ftp_get($conn_id, $tmp_file, $path . $file_name, $file_mode);
136
+
137
+ if (empty($res)) {
138
+ ftp_close($conn_id);
139
+ throw new \Exception('Cannot get file from ftp server for shop');
140
+ }
141
+ } else {
142
+ throw new \Exception('File on FTP not exists or empty');
143
+ }
144
+
145
+ ftp_close($conn_id);
146
+
147
+ return $tmp_file;
148
+ }
149
+
150
+ public function getDelimiterFromProfile ()
151
+ {
152
+
153
+ $gui_data = $this->getGuiData();
154
+ $delimiter = $gui_data['parse']['delimiter'];
155
+
156
+ if ($delimiter == '\t') {
157
+ $delimiter = "\t";
158
+ }
159
+
160
+ return $delimiter;
161
+
162
+ }
163
+
164
+ public function sendToDb ($importProduct = [])
165
+ {
166
+
167
+ $db_helper = Mage::helper('iceimport/db');
168
+
169
+ $gui_data = $this->getGuiData();
170
+ $if_ftp = $gui_data['file']['type'];
171
+ $delimiter = $this->getDelimiterFromProfile();
172
+ $file_name = $gui_data['file']['filename'];
173
+
174
+ $db_helper->insertLogEntry('import_filename', $file_name, 'info');
175
+
176
+ if (!defined('MAGENTO_ROOT')) {
177
+ define('MAGENTO_ROOT', getcwd());
178
+ }
179
+
180
+ $filepath = MAGENTO_ROOT.DS.$gui_data['file']['path'].DS.$file_name;
181
+
182
+ if (!empty($importProduct)) {
183
+
184
+ $this->makeFileFromArrayForManualLaunching($importProduct);
185
+ $filepath = $this->tempFeed;
186
+ }
187
+
188
+ if ($if_ftp == 'ftp') {
189
+ $filepath = $this->getFileByFTP();
190
+ }
191
+
192
+ $fieldsForCreatingTable = '`id` INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, ';
193
+
194
+ $fieldsAr = fgetcsv(fopen($filepath, "r"));
195
+
196
+ if (!$fieldsAr) {
197
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_extensions_logs` (`log_key`, `log_value`, `log_type`, `timecol`) VALUES ('errorFile', 'Can not find file', 'stat', NOW());");
198
+ echo "Can not find file. \n";
199
+ exit;
200
+ }
201
+
202
+ $fields = str_replace($delimiter, ',', $fieldsAr[0]);
203
+
204
+ $arrayForTable = explode(',', $fields);
205
+
206
+ foreach ($arrayForTable as $key => $field) {
207
+
208
+ if ($field == "") {
209
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_extensions_logs` (`log_key`, `log_value`, `log_type`, `timecol`) VALUES ('errorColumn', 'Empty column name', 'stat', NOW());");
210
+ echo "Empty column name. \n";
211
+ exit;
212
+ }
213
+ if (stristr($field, '_id') || stristr($field, 'quantity') || stristr($field, 'is_in_stock') || stristr($field, 'central_stock')) {
214
+ $fieldsForCreatingTable .= "`$field` INT(10) DEFAULT NULL, ";
215
+ $this->arrayForPostfixes['int'][] = $field;
216
+ }
217
+
218
+ elseif ($field == 'sku') {
219
+ $fieldsForCreatingTable .= "`$field` VARCHAR(64) NOT NULL, ";
220
+ }
221
+
222
+ elseif (stristr($field, 'price') || stristr($field, 'weight') || stristr($field, 'cost') || $field == 'qty') {
223
+ $fieldsForCreatingTable .= "`$field` DECIMAL(12,4) DEFAULT NULL, ";
224
+ $this->arrayForPostfixes['decimal'][] = $field;
225
+ }
226
+
227
+ elseif ((stristr($field, 'description') && !stristr($field, 'short_') && !stristr($field, 'long_')) || stristr($field, 'categories') || stristr($field, '_tree')) {
228
+ $fieldsForCreatingTable .= "`$field` TEXT DEFAULT NULL, ";
229
+ $this->arrayForPostfixes['text'][] = $field;
230
+ }
231
+
232
+ elseif (stristr($field, 'tax')) {
233
+ $fieldsForCreatingTable .= "`$field` DECIMAL(12,4) DEFAULT NULL, ";
234
+ $this->arrayForPostfixes['taxes'][] = $field;
235
+ }
236
+
237
+ else {
238
+ $fieldsForCreatingTable .= "`$field` VARCHAR(128) DEFAULT NULL, ";
239
+ $this->arrayForPostfixes['varchar'][] = $field;
240
+ }
241
+
242
+ if ($field == 'visibility' || $field == 'status') {
243
+ $this->arrayForPostfixes['int'][] = $field;
244
+ }
245
+ }
246
+
247
+ $fieldsForCreatingTable .= '`entity_id` int(10) DEFAULT NULL,`attribute_set_id` smallint(5) DEFAULT NULL, `entity_type_id` smallint(5) DEFAULT NULL, `tax_class_id` int(10) DEFAULT NULL, `is_iceimport` VARCHAR(64) DEFAULT 1, `url_key` VARCHAR(255) DEFAULT NULL,
248
+ `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP';
249
+
250
+ $this->arrayForPostfixes['int'][] = 'tax_class_id';
251
+ $this->arrayForPostfixes['varchar'][] = 'is_iceimport';
252
+ $this->arrayForPostfixes['varchar'][] = 'url_key';
253
+
254
+ $this->db_magento->query("DROP TABLE IF EXISTS {$this->tablePrefix}import_feed;");
255
+ $this->db_magento->query("CREATE TABLE IF NOT EXISTS {$this->tablePrefix}import_feed($fieldsForCreatingTable) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8");
256
+ $this->db_magento->query("LOAD DATA LOCAL INFILE '{$filepath}' IGNORE INTO TABLE {$this->tablePrefix}import_feed FIELDS TERMINATED BY '$delimiter' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES ($fields);");
257
+ }
258
+
259
+ public function getDefaultStoreId ()
260
+ {
261
+
262
+ $website_code = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/websites');
263
+
264
+ $select_website_id = $this->db_magento->query("SELECT website_id FROM `{$this->tablePrefix}core_website` WHERE `code` = '$website_code';");
265
+ $website_id = $select_website_id->fetch()['website_id'];
266
+
267
+ $select_default_store_id = $this->db_magento->query("SELECT default_store_id FROM `{$this->tablePrefix}core_store_group` WHERE website_id = $website_id;");
268
+ $default_store_id = $select_default_store_id->fetch()['default_store_id'];
269
+
270
+ return $default_store_id;
271
+ }
272
+
273
+ public function updateFeedTable ($importProduct = [])
274
+ {
275
+
276
+ $this->sendToDb($importProduct);
277
+
278
+ $defaulttaxConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/default_tax');
279
+
280
+ $select_product_entity_type_id = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_product';");
281
+ $product_entity_type_id = $select_product_entity_type_id->fetch()['entity_type_id'];
282
+ $select_attribute_set_id = $this->db_magento->query("SELECT `default_attribute_set_id` FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_product';");
283
+ $attribute_set_id = $select_attribute_set_id->fetch()['default_attribute_set_id'];
284
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` SET `entity_type_id` = $product_entity_type_id;");
285
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` SET `attribute_set_id` = $attribute_set_id;");
286
+ $this->db_magento->query("DELETE FROM `{$this->tablePrefix}import_feed` WHERE sku = '';");
287
+
288
+ foreach ($this->arrayForPostfixes['varchar'] as $i) {
289
+
290
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` SET $i = TRIM(BOTH '\"' FROM $i);");
291
+ }
292
+
293
+ $update_visibility = "UPDATE `{$this->tablePrefix}import_feed` SET `visibility` = CASE WHEN visibility = 'Not Visible Individually' THEN ". Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE. " WHEN visibility = 'Catalog' THEN ". Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG. " WHEN visibility = 'Search' THEN ". Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH. " WHEN visibility = 'Catalog, Search' THEN ". Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH. " ELSE visibility END;";
294
+ $this->db_magento->query($update_visibility);
295
+
296
+ $update_status = "UPDATE `{$this->tablePrefix}import_feed` SET `status` = CASE WHEN status = 'Enabled' THEN ". Mage_Catalog_Model_Product_Status::STATUS_ENABLED ." ELSE ". Mage_Catalog_Model_Product_Status::STATUS_DISABLED ." END;";
297
+ $this->db_magento->query($update_status);
298
+
299
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` SET `tax_class_id` = $defaulttaxConf;");
300
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` SET `url_key` = LOWER(REPLACE(REPLACE(REPLACE(REPLACE(name, ' ', '-'), '/', '-'), '*', ''), ',', ''));");
301
+
302
+ // Update stores
303
+
304
+ $default_store_id = $this->getDefaultStoreId();
305
+ $stores_array = $this->db_magento->fetchAll("SELECT `store_id`, `code` FROM {$this->tablePrefix}core_store WHERE store_id <> 0;");
306
+ $this->stores = $stores_array;
307
+ $query_for_store_update = "UPDATE `{$this->tablePrefix}import_feed` SET `store` = CASE WHEN store = 'default' THEN $default_store_id ";
308
+
309
+ foreach ($stores_array as $store) {
310
+
311
+ $store_code = $store['code'];
312
+ $store_id = $store['store_id'];
313
+ $query_for_store_update .= "WHEN store = '$store_code' THEN $store_id ";
314
+ }
315
+
316
+ $query_for_store_update .= 'ELSE store END;';
317
+
318
+ $this->db_magento->query($query_for_store_update);
319
+ }
320
+
321
+ public function getUnspscToCatId ()
322
+ {
323
+
324
+ $select_category_entity_type = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_category';");
325
+ $category_entity_type = $select_category_entity_type->fetch()['entity_type_id'];
326
+ $select_unspsc_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'unspsc' AND entity_type_id = $category_entity_type;");
327
+ $unspsc_id = $select_unspsc_id->fetch()['attribute_id'];
328
+ $select_unspsc_to_id = $this->db_magento->query("SELECT `entity_id`, `value` FROM `{$this->tablePrefix}catalog_category_entity_varchar` WHERE attribute_id = $unspsc_id AND store_id = 0;");
329
+
330
+ return $select_unspsc_to_id->fetchAll(PDO::FETCH_KEY_PAIR);
331
+ }
332
+
333
+ public function addCatIdToArray ($offset)
334
+ {
335
+
336
+ $unspscToCatId = $this->getUnspscToCatId();
337
+ $unspscToProdId = $this->db_magento->fetchAll("SELECT `unspsc`, `entity_id` FROM `{$this->tablePrefix}import_feed` GROUP BY `entity_id` LIMIT $offset, $this->batch;");
338
+
339
+ $resultArray = [];
340
+ foreach ($unspscToProdId as $key => &$value) {
341
+
342
+ foreach ($unspscToCatId as $k => $item) {
343
+
344
+ if ($value['unspsc'] == $item) {
345
+ $resultArray[$key]['unspsc'] = $k;
346
+ $resultArray[$key]['entity_id'] = $value['entity_id'];
347
+ }
348
+ }
349
+ }
350
+
351
+ return $resultArray;
352
+ }
353
+
354
+ public function makeTempFileForCatalogCategoryProductInsert ($offset)
355
+ {
356
+
357
+ $this->deleteTempFileForCatalogCategoryProductInsert();
358
+
359
+ $arValues = $this->addCatIdToArray($offset);
360
+ $columns = ['category_id', 'product_id'];
361
+ $fp = fopen($this->filenameValues, 'w+');
362
+
363
+ fputcsv($fp, array_unique($columns), "\t");
364
+
365
+ foreach ($arValues as $fields) {
366
+ fputcsv($fp, $fields, "\t");
367
+ }
368
+
369
+ fclose($fp);
370
+
371
+ // delete categories for set new
372
+
373
+ $final = array_map(function ($elements) {
374
+ return $elements['entity_id'];
375
+ }, $arValues);
376
+
377
+ if (!empty($final)) {
378
+ $this->db_magento->query("DELETE FROM {$this->tablePrefix}catalog_category_product WHERE product_id IN (" . implode(',', $final) . ");");
379
+ }
380
+ }
381
+
382
+ public function loadDataToCatalogCategoryProduct()
383
+ {
384
+
385
+ $continue = true;
386
+ $counter = 0;
387
+
388
+ while ($continue) {
389
+
390
+ $offset = $counter * $this->batch;
391
+
392
+ $this->makeTempFileForCatalogCategoryProductInsert($offset);
393
+
394
+ $this->db_magento->query("LOAD DATA LOCAL INFILE '{$this->filenameValues}' IGNORE INTO TABLE {$this->tablePrefix}catalog_category_product FIELDS TERMINATED BY '\t' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES (category_id, product_id);");
395
+ $this->db_magento->query("UPDATE {$this->tablePrefix}catalog_category_product SET position = 1;");
396
+
397
+ if ($counter == $this->end) {
398
+
399
+ $continue = false;
400
+ }
401
+
402
+ $counter++;
403
+ }
404
+ }
405
+
406
+ public function getCategoriesEntityId ()
407
+ {
408
+
409
+ $select_data = $this->db_magento->query("SELECT `entity_id`, `path` FROM `{$this->tablePrefix}catalog_category_entity` WHERE entity_id <> 1 AND entity_id <> 2 ORDER BY entity_id;");
410
+ return $select_data->fetchAll(PDO::FETCH_KEY_PAIR);
411
+ }
412
+
413
+ public function prepareArrayForInsertCategories ()
414
+ {
415
+
416
+ $select_max_qty_of_slashes = $this->db_magento->query("select MAX(LENGTH(`categories`) - LENGTH(REPLACE(`categories`, '/', ''))) as `max_qty_of_slashes` from `{$this->tablePrefix}import_feed`;");
417
+ $max_qty_of_slashes = $select_max_qty_of_slashes->fetch()['max_qty_of_slashes'] + 1;
418
+
419
+ $selectCategories = "";
420
+
421
+ for ($i = 1; $i <= $max_qty_of_slashes; $i++) {
422
+ if ($i == 1) {
423
+ $selectCategories .= "select SUBSTRING_INDEX(categories,'/', $i) category, SUBSTRING_INDEX(unspsc_path,'/', $i) as unspsc_c, unspsc_path, categories path, store
424
+ from {$this->tablePrefix}import_feed
425
+ group by store, unspsc_c union ";
426
+ } else {
427
+ $selectCategories .= "select (SUBSTRING_INDEX(SUBSTRING_INDEX(categories,'/', $i),'/',-1)) category, (SUBSTRING_INDEX(SUBSTRING_INDEX(unspsc_path,'/', $i),'/',-1)) as unspsc_c, unspsc_path, categories path, store
428
+ from {$this->tablePrefix}import_feed
429
+ group by store, unspsc_c union ";
430
+ }
431
+ }
432
+
433
+ $selectCategories = rtrim($selectCategories, 'union ');
434
+ $selectCategories .= " order by unspsc_c;";
435
+
436
+ $categories = $this->db_magento->fetchAll($selectCategories);
437
+
438
+ $unique_categories = [];
439
+
440
+ foreach ($categories as $key => $category) {
441
+
442
+ $path = explode('/', $category['unspsc_path']);
443
+ $category['unspsc_path_c'] = '';
444
+
445
+ foreach ($path as $k => $cat) {
446
+ if ($cat == $category['unspsc_c']) {
447
+ $category['level'] = $k + 2;
448
+ break;
449
+ }
450
+ }
451
+
452
+ $unsps_s = $category['store'].$category['unspsc_c'];
453
+
454
+ if (!array_key_exists($unsps_s, $unique_categories)) {
455
+ $unique_categories[$unsps_s] = $category;
456
+ }
457
+ }
458
+
459
+ foreach ($unique_categories as $key => &$category) {
460
+
461
+ $path = explode('/', $unique_categories[$key]['unspsc_path']);
462
+
463
+ foreach ($path as $cat) {
464
+ foreach ($unique_categories as $k => $item) {
465
+ if ($cat == $item['unspsc_c']) {
466
+ $category['unspsc_path_c'] .= $item['unspsc_c'].'/';
467
+ break;
468
+ }
469
+ }
470
+ if ($cat == $category['unspsc_c']) {
471
+ break;
472
+ }
473
+ }
474
+
475
+ $category['unspsc_path_c'] = rtrim($category['unspsc_path_c'], '/');
476
+ }
477
+
478
+ return $unique_categories;
479
+ }
480
+
481
+ public function makeTempFileForCategories ()
482
+ {
483
+
484
+ $this->deleteTempFileForCategories();
485
+ $arCats = $this->prepareArrayForInsertCategories();
486
+ $columns = [];
487
+
488
+ foreach ($arCats as &$value) {
489
+ foreach ($value as $key => $item) {
490
+ if ($key == 'path') {
491
+ unset($value[$key]);
492
+ }
493
+ else {
494
+ $columns[] = $key;
495
+ }
496
+ }
497
+ }
498
+
499
+ $fp = fopen($this->filenameCats, 'w+');
500
+
501
+ fputcsv($fp, array_unique($columns), "\t");
502
+
503
+ foreach ($arCats as $fields) {
504
+ fputcsv($fp, $fields, "\t");
505
+ }
506
+
507
+ fclose($fp);
508
+
509
+ }
510
+
511
+ public function processTemFileForAttributes ($attributes)
512
+ {
513
+
514
+ $delimiter = $this->getDelimiterFromProfile();
515
+
516
+ $this->deleteTemFileForAttributes();
517
+
518
+ $fp = fopen($this->tempAttr, 'w+');
519
+
520
+ foreach ($attributes as $fields) {
521
+ fputcsv($fp, $fields, $delimiter);
522
+ $fields['store'] = 0;
523
+ fputcsv($fp, $fields, $delimiter);
524
+ }
525
+
526
+ fclose($fp);
527
+ }
528
+
529
+ public function insertIdsInCatalogCategoryEntity ()
530
+ {
531
+
532
+ $now = date("Y-m-d H:i:s");
533
+
534
+ $select_attribute_set_id = $this->db_magento->query("SELECT `default_attribute_set_id` FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_category';");
535
+ $attribute_set_id = $select_attribute_set_id->fetch()['default_attribute_set_id'];
536
+
537
+ $select_category_entity_type = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_category';");
538
+ $category_entity_type = $select_category_entity_type->fetch()['entity_type_id'];
539
+
540
+ $select_unspsc_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'unspsc' AND entity_type_id = $category_entity_type;");
541
+ $unspsc_id = $select_unspsc_id->fetch()['attribute_id'];
542
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}catalog_category_entity (`entity_type_id`, `attribute_set_id`, `path`, `level`, `created_at`, `updated_at`) SELECT $category_entity_type, $attribute_set_id, tc.unspsc_path_c, tc.level, '$now', '$now' FROM {$this->tablePrefix}temp_cats tc WHERE unspsc_c NOT IN (SELECT value FROM {$this->tablePrefix}catalog_category_entity_varchar WHERE attribute_id = $unspsc_id) GROUP BY tc.unspsc_path_c;");
543
+ }
544
+
545
+ public function updateCategoryPath ()
546
+ {
547
+
548
+ $unspsc_ids = $this->db_magento->fetchAll("SELECT id, unspsc_c, unspsc_path_c FROM `{$this->tablePrefix}temp_cats`;");
549
+
550
+ $select_root_category_id = $this->db_magento->query("SELECT entity_id FROM `{$this->tablePrefix}catalog_category_entity` WHERE level = 0;");
551
+ $root_category_id = $select_root_category_id->fetch()['entity_id'];
552
+
553
+ if (!$root_category_id) {
554
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_extensions_logs` (`log_key`, `log_value`, `log_type`, `timecol`) VALUES ('errorRootCategory', 'There is no root category in shop.', 'stat', NOW());");
555
+ echo "There is no root category in your shop. \n";
556
+ exit;
557
+ }
558
+
559
+ $default_category_from_config = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/default_category');
560
+
561
+ if (!$default_category_from_config) {
562
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_extensions_logs` (`log_key`, `log_value`, `log_type`, `timecol`) VALUES ('errorDefaultCategory', 'There is no default category in shop.', 'stat', NOW());");
563
+ echo "There is no default category in your shop. \n";
564
+ exit;
565
+ }
566
+
567
+ foreach ($unspsc_ids as $k => &$unspsc_id) {
568
+
569
+ $unspsc_id['cat_path'] = $root_category_id.'/'.$default_category_from_config.'/';
570
+ $paths = explode('/', $unspsc_id['unspsc_path_c']);
571
+
572
+ foreach ($paths as $path) {
573
+ foreach ($unspsc_ids as $key => $item) {
574
+ if ($path == $item['unspsc_c']) {
575
+ $unspsc_id['cat_path'] .= $item['id'].'/';
576
+ break;
577
+ }
578
+ }
579
+
580
+ if ($path == $unspsc_ids[$k]['unspsc_c']) {
581
+ break;
582
+ }
583
+ }
584
+
585
+ $unspsc_id['cat_path'] = rtrim($unspsc_id['cat_path'], '/');
586
+ $parent = explode('/', $unspsc_id['cat_path']);
587
+ $unspsc_id['parent_id'] = $parent[count($parent) - 2];
588
+ }
589
+
590
+ $query_for_update_path = "UPDATE `{$this->tablePrefix}catalog_category_entity` SET `path` = CASE ";
591
+
592
+ foreach ($unspsc_ids as $k => $v) {
593
+
594
+ $id = $v['id'];
595
+ $path = $v['cat_path'];
596
+ $query_for_update_path .= "WHEN entity_id = '$id' THEN '$path' ";
597
+ }
598
+
599
+ $query_for_update_path .= 'ELSE path END;';
600
+
601
+ $this->db_magento->query($query_for_update_path);
602
+
603
+ $query_for_update_parent_id = "UPDATE `{$this->tablePrefix}catalog_category_entity` SET `parent_id` = CASE ";
604
+
605
+ foreach ($unspsc_ids as $key => $unspsc_id) {
606
+ $id = $unspsc_id['id'];
607
+ $parent_id = $unspsc_id['parent_id'];
608
+ $query_for_update_parent_id .= "WHEN entity_id = '$id' THEN '$parent_id' ";
609
+ }
610
+
611
+ $query_for_update_parent_id .= 'ELSE parent_id END;';
612
+ $this->db_magento->query($query_for_update_parent_id);
613
+ }
614
+
615
+ public function makeTempTableForCategories ()
616
+ {
617
+
618
+ $fieldsForCreatingTable = '`row_id` INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT, ';
619
+
620
+ $fieldsAr = fgetcsv(fopen($this->filenameCats, "r"));
621
+ $fields = str_replace("\t", ',', $fieldsAr[0]);
622
+ $arrayForTable = explode(',', $fields);
623
+
624
+ foreach ($arrayForTable as $key => $field) {
625
+
626
+ if ($field == 'level' || $field == 'parent_id') {
627
+ $fieldsForCreatingTable .= "`$field` INT(10) DEFAULT NULL, ";
628
+ }
629
+ else {
630
+ $fieldsForCreatingTable .= "`$field` VARCHAR(128) DEFAULT NULL, ";
631
+ }
632
+ }
633
+
634
+ $fieldsForCreatingTable = rtrim($fieldsForCreatingTable, ', ');
635
+
636
+ $this->db_magento->query("DROP TABLE IF EXISTS {$this->tablePrefix}temp_cats;");
637
+ $this->db_magento->query("CREATE TABLE IF NOT EXISTS {$this->tablePrefix}temp_cats($fieldsForCreatingTable, id INT(10) DEFAULT NULL, `url_key` VARCHAR(255) DEFAULT NULL) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8");
638
+
639
+ $this->db_magento->query("LOAD DATA LOCAL INFILE '{$this->filenameCats}' IGNORE INTO TABLE {$this->tablePrefix}temp_cats FIELDS TERMINATED BY '\t' ENCLOSED BY '' LINES TERMINATED BY '\n' IGNORE 1 LINES ($fields);");
640
+
641
+ }
642
+
643
+ public function countAllProducts ()
644
+ {
645
+
646
+ $select_count_all_products = $this->db_magento->query("SELECT COUNT(DISTINCT sku) as count_all_prods FROM {$this->tablePrefix}import_feed;");
647
+ $this->count_all_products = $select_count_all_products->fetch()['count_all_prods'];
648
+ }
649
+
650
+ public function importProduct ($importProduct = [])
651
+ {
652
+
653
+ $this->updateFeedTable($importProduct);
654
+
655
+ $this->countAllProducts();
656
+ $this->end = ceil($this->count_all_products / $this->batch);
657
+
658
+ $websites = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/websites');
659
+ $website = Mage::app()->getWebsite(trim($websites));
660
+ $websiteId = $website->getId();
661
+
662
+ $countryCode = Mage::getStoreConfig('general/country/default');
663
+
664
+ // new products
665
+ $newProducts = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_new_products');
666
+
667
+ // product attributes
668
+ $producteanConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_ean');
669
+ $productmpnConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_mpn');
670
+ $productbrandConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_brand_name');
671
+ $productnameConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_name');
672
+ $productshdescriptionConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_short_description');
673
+ $productdescriptionConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_description');
674
+ $deliveryetaConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_delivery_eta');
675
+ $updateStatusFromCsvConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_status_from_csv');
676
+ $updateVisibilityFromCsvConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_visibility_from_csv');
677
+ $updateUrlKeyFromCsvConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_url_key_from_csv');
678
+
679
+ // Images
680
+ $addImages = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_images');
681
+
682
+ // p&a
683
+ $productpricesConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_prices');
684
+ $productstockConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_stock');
685
+ $updateIsInStockFromCsvConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_is_in_stock_from_csv');
686
+
687
+ // categories
688
+ $categoriesConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_categories_from_csv');
689
+ $categoriesToActiveConf = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/category_active');
690
+
691
+ // mapping
692
+ $config_mpn = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_mpn');
693
+ $config_brand = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_brand_name');
694
+ $config_gtin = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_ean');
695
+ $config_delivery_eta = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_delivery_eta');
696
+
697
+ // sorting
698
+ $default_store_id = $this->getDefaultStoreId();
699
+
700
+ // importing products
701
+ if (!$newProducts) {
702
+ $this->db_magento->query("DELETE FROM `{$this->tablePrefix}import_feed` WHERE sku NOT IN (SELECT sku FROM `{$this->tablePrefix}catalog_product_entity`);");
703
+ }
704
+
705
+ //check for empty main table
706
+ $main_table_query = $this->db_magento->query("SELECT COUNT(*) as cnt_rows FROM `{$this->tablePrefix}import_feed` WHERE 1;");
707
+ $main_table_check = $main_table_query->fetch()['cnt_rows'];
708
+
709
+ if (!empty($main_table_check)) {
710
+
711
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}catalog_product_entity` cpe JOIN {$this->tablePrefix}import_feed impf ON cpe.sku = impf.sku SET cpe.`sku` = impf.sku, cpe.`entity_type_id` = impf.entity_type_id, cpe.`attribute_set_id` = impf.attribute_set_id, cpe.`type_id` = 'simple', cpe.`updated_at` = impf.updated_at;");
712
+
713
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}catalog_product_entity` (`sku`, `entity_type_id`, `attribute_set_id`, `type_id`, `created_at`, `updated_at`) SELECT impf.sku, impf.entity_type_id, impf.attribute_set_id, 'simple', impf.created_at, impf.updated_at
714
+ FROM {$this->tablePrefix}import_feed impf WHERE sku NOT IN (SELECT sku FROM {$this->tablePrefix}catalog_product_entity WHERE sku IS NOT NULL) GROUP BY impf.sku;");
715
+
716
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}import_feed` impf JOIN `{$this->tablePrefix}catalog_product_entity` cpe ON impf.sku = cpe.sku SET impf.entity_id = cpe.entity_id;");
717
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_iceimport_imported_product_ids` (`product_id`, `product_sku`) SELECT impf.entity_id, impf.sku FROM `{$this->tablePrefix}import_feed` impf ON DUPLICATE KEY UPDATE product_sku = impf.sku;");
718
+
719
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}catalog_product_website` (`product_id`, `website_id`) SELECT impf.entity_id, $websiteId FROM {$this->tablePrefix}import_feed impf ON DUPLICATE KEY UPDATE product_id = impf.entity_id, website_id = $websiteId;");
720
+
721
+ $select_product_entity_type_id = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_product';");
722
+ $product_entity_type_id = $select_product_entity_type_id->fetch()['entity_type_id'];
723
+
724
+ if (!$config_mpn || !$config_brand || !$config_gtin || !$config_delivery_eta) {
725
+ $this->db_magento->query("INSERT INTO `{$this->tablePrefix}iceshop_extensions_logs` (`log_key`, `log_value`, `log_type`, `timecol`) VALUES ('errorMapping', 'Mapings are incorrect.', 'stat', NOW());");
726
+ echo "Mapings are incorrect. \n";
727
+ exit;
728
+ }
729
+
730
+ if ($addImages) {
731
+ $this->db_magento->query("INSERT IGNORE INTO `{$this->tablePrefix}iceshop_iceimport_image_queue` (`entity_id`, `image_url`) SELECT impf.entity_id, impf.image FROM `{$this->tablePrefix}import_feed` impf ;");
732
+ }
733
+
734
+ foreach ($this->arrayForPostfixes as $key => $value) {
735
+ if ($key != 'taxes') {
736
+
737
+ foreach ($value as $k => $attr) {
738
+ $front_input = false;
739
+ if ($attr == 'mpn' && !$productmpnConf ||
740
+ $attr == 'ean' && !$producteanConf ||
741
+ $attr == 'brand_name' && !$productbrandConf ||
742
+ $attr == 'delivery_eta' && !$deliveryetaConf ||
743
+ $attr == 'description' && !$productdescriptionConf ||
744
+ $attr == 'short_description' && !$productshdescriptionConf ||
745
+ $attr == 'visibility' && !$updateVisibilityFromCsvConf ||
746
+ $attr == 'url_key' && !$updateUrlKeyFromCsvConf ||
747
+ $attr == 'name' && !$productnameConf ||
748
+ $attr == 'price' && !$productpricesConf ||
749
+ $attr == 'status' && !$updateStatusFromCsvConf
750
+ ) {
751
+ continue;
752
+ }
753
+
754
+ $attr_for_select = $attr;
755
+
756
+ switch ($attr_for_select) {
757
+ case 'mpn':
758
+ $attr_for_select = $config_mpn;
759
+ break;
760
+ case 'delivery_eta':
761
+ $attr_for_select = $config_delivery_eta;
762
+ break;
763
+ case 'brand_name':
764
+ $attr_for_select = $config_brand;
765
+ break;
766
+ case 'ean':
767
+ $attr_for_select = $config_gtin;
768
+ break;
769
+ }
770
+
771
+ $select_attribute_id = $this->db_magento->query("SELECT `attribute_id`, `frontend_input`, `source_model` FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = '$attr_for_select' AND entity_type_id = $product_entity_type_id;");
772
+ $tmp_attribute = $select_attribute_id->fetch();
773
+ $attribute_id = (isset($tmp_attribute['attribute_id'])) ? $tmp_attribute['attribute_id'] : null;
774
+ $frontend_input = (isset($tmp_attribute['frontend_input'])) ? $tmp_attribute['frontend_input'] : null;
775
+ $source_model = (isset($tmp_attribute['source_model'])) ? $tmp_attribute['source_model'] : null;
776
+
777
+ if ($attribute_id) {
778
+
779
+ if ($frontend_input == 'select' && ($source_model == 'eav/entity_attribute_source_table' || empty($source_model))) {
780
+ $front_input = true;
781
+
782
+ $brands_in_store = [];
783
+ $elements = [];
784
+ $brands_in_store = $this->db_magento->fetchAll("SELECT DISTINCT `{$attr}` FROM {$this->tablePrefix}import_feed WHERE `{$attr}` NOT IN (SELECT `value` FROM {$this->tablePrefix}eav_attribute_option_value eavov LEFT JOIN {$this->tablePrefix}eav_attribute_option eavo ON eavov.option_id = eavo.option_id WHERE eavo.attribute_id = '{$attribute_id}') AND `{$attr}` <> '';");
785
+
786
+ if (!empty($brands_in_store)) {
787
+ $final = array_map(function ($elements) use ($attr) {
788
+ return $elements[$attr];
789
+ }, $brands_in_store);
790
+
791
+ $attribute_model = Mage::getModel('eav/entity_attribute');
792
+ $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
793
+
794
+ $attribute_code = $attribute_model->getIdByCode('catalog_product', $attr_for_select);
795
+ $attribute = $attribute_model->load($attribute_code);
796
+
797
+ $attribute_options_model->setAttribute($attribute);
798
+ $attribute_options_model->getAllOptions(false);
799
+
800
+ foreach ($final as $brand_item) {
801
+ $value = [];
802
+ $value['option'] = [$brand_item];
803
+ $result = ['value' => $value];
804
+ $attribute->setData('option', $result);
805
+ $attribute->save();
806
+ }
807
+ }
808
+ }
809
+
810
+ $post_fix = $key;
811
+
812
+ $continue = true;
813
+ $counter = 0;
814
+
815
+ if ($front_input) {
816
+ $this->db_magento->query("UPDATE {$this->tablePrefix}import_feed impf LEFT JOIN (SELECT eavov.`option_id` as option_id, eavov.`value` FROM {$this->tablePrefix}eav_attribute_option_value eavov LEFT JOIN {$this->tablePrefix}eav_attribute_option eavo ON eavov.option_id = eavo.option_id WHERE eavo.attribute_id = '{$attribute_id}') tmptable ON tmptable.value = impf.{$attr} SET impf.{$attr} = tmptable.option_id WHERE tmptable.option_id IS NOT NULL;");
817
+ }
818
+
819
+ while ($continue) {
820
+
821
+ $offset = $counter * $this->batch;
822
+
823
+ if ($front_input) {
824
+ $post_fix = 'int';
825
+ $select_attributes = $this->db_magento->query("SELECT impf.entity_type_id, $attribute_id, impf.store, impf.entity_id, $attr FROM {$this->tablePrefix}import_feed impf LIMIT $offset, $this->batch;");
826
+
827
+ } else {
828
+ $select_attributes = $this->db_magento->query("SELECT impf.entity_type_id, $attribute_id, impf.store, impf.entity_id, $attr FROM {$this->tablePrefix}import_feed impf LIMIT $offset, $this->batch;");
829
+ }
830
+
831
+ $attributes = $select_attributes->fetchAll();
832
+ $this->processTemFileForAttributes($attributes);
833
+
834
+ $delimiter = $this->getDelimiterFromProfile();
835
+ $filepath = $this->tempAttr;
836
+
837
+ $this->db_magento->query("LOAD DATA LOCAL INFILE '{$filepath}' IGNORE INTO TABLE {$this->tablePrefix}catalog_product_entity_{$post_fix} FIELDS TERMINATED BY '$delimiter' ENCLOSED BY '' LINES TERMINATED BY '\n' (entity_type_id, attribute_id, store_id, entity_id, value);");
838
+
839
+ if ($counter == $this->end) {
840
+
841
+ $continue = false;
842
+ }
843
+
844
+ $counter++;
845
+ }
846
+
847
+ $this->db_magento->query("UPDATE {$this->tablePrefix}catalog_product_entity_{$post_fix} cpek JOIN {$this->tablePrefix}import_feed impf ON impf.entity_id = cpek.entity_id SET cpek.value = impf.$attr WHERE cpek.attribute_id = $attribute_id AND cpek.value <> impf.$attr AND cpek.store_id = impf.store AND cpek.entity_type_id = impf.entity_type_id;");
848
+ $this->db_magento->query("UPDATE {$this->tablePrefix}catalog_product_entity_{$post_fix} cpek JOIN {$this->tablePrefix}import_feed impf ON impf.entity_id = cpek.entity_id SET cpek.value = impf.$attr WHERE cpek.attribute_id = $attribute_id AND cpek.value <> impf.$attr AND cpek.store_id = 0 AND cpek.entity_type_id = impf.entity_type_id;");
849
+
850
+ }
851
+ }
852
+ }
853
+ }
854
+
855
+ $this->deleteTemFileForAttributes();
856
+
857
+ if (array_key_exists('taxes', $this->arrayForPostfixes)) {
858
+
859
+ foreach ($this->arrayForPostfixes['taxes'] as $tax) {
860
+
861
+ $select_tax_id = $this->db_magento->query("SELECT `attribute_id` FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = '$tax' AND entity_type_id = $product_entity_type_id;");
862
+ $tax_id = $select_tax_id->fetch()['attribute_id'];
863
+
864
+ if ($tax_id) {
865
+ $this->db_magento->query("DELETE FROM `{$this->tablePrefix}weee_tax` WHERE attribute_id = '$tax_id';");
866
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}weee_tax (website_id, entity_id, country, value, state, attribute_id, entity_type_id) SELECT 0, entity_id, '$countryCode', $tax, '*', $tax_id, $product_entity_type_id FROM {$this->tablePrefix}import_feed;");
867
+ }
868
+ }
869
+ }
870
+
871
+ $stock_name = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/stock_inventory');
872
+ $select_stock_id = $this->db_magento->query("SELECT stock_id FROM `{$this->tablePrefix}cataloginventory_stock` WHERE stock_name = '$stock_name';");
873
+ $stock_id = $select_stock_id->fetch()['stock_id'];
874
+
875
+ if ($productstockConf) {
876
+ $this->db_magento->query("UPDATE {$this->tablePrefix}import_feed SET qty = 0 WHERE qty IS NULL;");
877
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}cataloginventory_stock_item (`product_id`, `stock_id`, `qty`, `is_in_stock`) SELECT impf.entity_id, $stock_id, impf.qty, impf.is_in_stock FROM {$this->tablePrefix}import_feed impf
878
+ ON DUPLICATE KEY UPDATE product_id = impf.entity_id, stock_id = $stock_id, qty = impf.qty;");
879
+ }
880
+
881
+ if ($updateIsInStockFromCsvConf) {
882
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}cataloginventory_stock_status (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`) SELECT impf.entity_id, $websiteId, $stock_id, impf.qty, impf.is_in_stock FROM {$this->tablePrefix}import_feed impf
883
+ ON DUPLICATE KEY UPDATE product_id = impf.entity_id, stock_id = $stock_id, website_id = $websiteId, qty = impf.qty;");
884
+ }
885
+
886
+ // Creating temp file for categories
887
+ $this->makeTempFileForCategories();
888
+
889
+ // Creating temp table
890
+ $this->makeTempTableForCategories();
891
+
892
+ // catalog_category_entity
893
+ $select_category_entity_type = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_category';");
894
+ $category_entity_type = $select_category_entity_type->fetch()['entity_type_id'];
895
+
896
+ $select_unspsc_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'unspsc' AND entity_type_id = $category_entity_type;");
897
+ $unspsc_id = $select_unspsc_id->fetch()['attribute_id'];
898
+
899
+ $select_name_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'name' AND entity_type_id = $category_entity_type;");
900
+ $name_id = $select_name_id->fetch()['attribute_id'];
901
+
902
+ $select_url_key_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'url_key' AND entity_type_id = $category_entity_type;");
903
+ $url_key_id = $select_url_key_id->fetch()['attribute_id'];
904
+
905
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}temp_cats` tc JOIN `{$this->tablePrefix}catalog_category_entity_varchar` ccev ON ccev.value = tc.unspsc_c SET tc.id = ccev.entity_id WHERE attribute_id = $unspsc_id AND tc.id IS NULL;");
906
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}temp_cats` SET `category` = TRIM(BOTH '\"' FROM `category`);");
907
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}temp_cats` SET `url_key` = LOWER(REPLACE(REPLACE(REPLACE(category, '&', '-'), '/', '-'), ' ', ''));");
908
+
909
+ $this->insertIdsInCatalogCategoryEntity();
910
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}temp_cats` tc JOIN `{$this->tablePrefix}catalog_category_entity` cce ON cce.path = tc.unspsc_path_c SET tc.id = cce.entity_id;");
911
+
912
+ $this->updateCategoryPath();
913
+
914
+ if ($categoriesConf) {
915
+ // catalog_category_entity_varchar
916
+ $array_for_varchar = ['unspsc_c' => $unspsc_id, 'category' => $name_id, 'url_key' => $url_key_id];
917
+ foreach ($array_for_varchar as $attr_varchar => $attr_varchar_id) {
918
+
919
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}catalog_category_entity_varchar (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) SELECT $category_entity_type, $attr_varchar_id , tc.store, tc.id, tc.$attr_varchar FROM {$this->tablePrefix}temp_cats tc WHERE tc.id <> 0 ON DUPLICATE KEY UPDATE `value` = tc.$attr_varchar;");
920
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}catalog_category_entity_varchar (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) SELECT $category_entity_type, $attr_varchar_id , 0, tc.id, tc.$attr_varchar FROM {$this->tablePrefix}temp_cats tc WHERE store = '$default_store_id' AND tc.id <> 0 ON DUPLICATE KEY UPDATE `value` = tc.$attr_varchar;");
921
+ }
922
+
923
+ // catalog_category_entity_int
924
+ $array_for_int = ['is_active' => $categoriesToActiveConf, 'is_anchor' => 1, 'include_in_menu' => 1];
925
+
926
+ foreach ($array_for_int as $attr_int => $int) {
927
+
928
+ $select_attribute_int = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = '$attr_int' AND entity_type_id = $category_entity_type;");
929
+ $attribute_int = $select_attribute_int->fetch()['attribute_id'];
930
+
931
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}catalog_category_entity_int (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) SELECT $category_entity_type, $attribute_int , tc.store, tc.id, $int FROM {$this->tablePrefix}temp_cats tc WHERE tc.id <> 0 ON DUPLICATE KEY UPDATE `value` = $int;");
932
+ $this->db_magento->query("INSERT INTO {$this->tablePrefix}catalog_category_entity_int (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) SELECT $category_entity_type, $attribute_int , 0, tc.id, $int FROM {$this->tablePrefix}temp_cats tc WHERE store = '$default_store_id' AND tc.id <> 0 ON DUPLICATE KEY UPDATE `value` = $int;");
933
+ }
934
+ }
935
+
936
+ // loading data to catalog_category_product;
937
+ $this->loadDataToCatalogCategoryProduct();
938
+ }
939
+
940
+ // Deleting temp files
941
+ $this->deleteTempFileForCategories();
942
+ $this->deleteTempFileForCatalogCategoryProductInsert();
943
+
944
+ // Deleting temp tables
945
+ $this->deleteTempTableCats();
946
+ $this->deleteTempTableProds();
947
+
948
+ if (!empty($importProduct)) {
949
+ $this->deleteFileFromArrayForManualLaunching();
950
+ }
951
+
952
+ $this->checkAndSetIceField();
953
+ }
954
+
955
+ public function runCategoriesSorting ()
956
+ {
957
+
958
+ $select_number_of_iteration = $this->db_magento->query("SELECT MAX(level) as max FROM `{$this->tablePrefix}catalog_category_entity`;");
959
+ $number_of_iteration = $select_number_of_iteration->fetch()['max'] - 1;
960
+
961
+ $position = 1;
962
+
963
+ if ($number_of_iteration > 0) {
964
+
965
+ for ($i = 1; $i <= $number_of_iteration; $i++) {
966
+
967
+ $query_select_entity_id = "SELECT cce.entity_id FROM `{$this->tablePrefix}catalog_category_entity` cce LEFT JOIN `{$this->tablePrefix}catalog_category_product` ccp ON cce.entity_id = ccp.category_id WHERE ccp.category_id IS NULL AND SUBSTRING_INDEX(cce.path,'/',-1) = cce.entity_id;";
968
+ $empty_categories = $this->db_magento->fetchAll($query_select_entity_id);
969
+
970
+ $final_array = array_map(function ($element) {
971
+ return $element['entity_id'];
972
+ }, $empty_categories);
973
+
974
+ $catCollection = Mage::getModel('catalog/category')
975
+ ->getCollection()
976
+ ->addAttributeToSort('name', 'ASC');
977
+
978
+ $select_category_entity_type = $this->db_magento->query("SELECT entity_type_id FROM `{$this->tablePrefix}eav_entity_type` WHERE entity_type_code = 'catalog_category';");
979
+ $category_entity_type = $select_category_entity_type->fetch()['entity_type_id'];
980
+ $select_attribute_is_active_id = $this->db_magento->query("SELECT attribute_id FROM `{$this->tablePrefix}eav_attribute` WHERE attribute_code = 'is_active' AND entity_type_id = $category_entity_type;");
981
+ $attribute_is_active_id = $select_attribute_is_active_id->fetch()['attribute_id'];
982
+ $update_hide_category = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_hide_category');
983
+
984
+ $array_for_updating_is_active = [];
985
+
986
+ foreach ($catCollection as $key => $category) {
987
+
988
+ $query = "UPDATE `{$this->tablePrefix}catalog_category_entity` SET position = :position WHERE entity_id = :cat_id ";
989
+ $this->db_magento->query($query, array(
990
+ ':position' => $position++,
991
+ ':cat_id' => $category->getId()
992
+ ));
993
+
994
+ if ($update_hide_category) {
995
+
996
+ if (in_array($category->getId(), $final_array)){
997
+ $count = count($category->getChildrenCategories());
998
+
999
+ if ($count == 0){
1000
+ $array_for_updating_is_active[] = $category->getId();
1001
+ }
1002
+ }
1003
+ }
1004
+ }
1005
+
1006
+ $ids_for_update_is_active = implode(',', $array_for_updating_is_active);
1007
+
1008
+ if (!empty($ids_for_update_is_active)) {
1009
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}catalog_category_entity_int` SET `value` = 0 WHERE `attribute_id` = $attribute_is_active_id AND `entity_id` IN ($ids_for_update_is_active);");
1010
+ }
1011
+ }
1012
+ }
1013
+ }
1014
+
1015
+ public function deleteOldProducts($DB_logger)
1016
+ {
1017
+
1018
+ try {
1019
+
1020
+ $this->db_magento->query("SELECT @is_iceimport_id := `attribute_id`
1021
+ FROM {$this->tablePrefix}eav_attribute
1022
+ WHERE attribute_code = 'is_iceimport'");
1023
+
1024
+ $count_prod = $this->db_magento->fetchRow("SELECT count(t2.entity_id) AS count_prod FROM (SELECT cpe.entity_id
1025
+ FROM {$this->tablePrefix}catalog_product_entity AS cpe
1026
+ JOIN {$this->tablePrefix}catalog_product_entity_varchar AS cpev
1027
+ ON cpe.entity_id = cpev.entity_id
1028
+ AND cpev.value = 1
1029
+ AND cpev.attribute_id = @is_iceimport_id
1030
+ GROUP BY cpe.entity_id) t2");
1031
+
1032
+ $count_prod = $count_prod['count_prod'];
1033
+
1034
+ if ($count_prod > 0) {
1035
+ //iceimport products exists, amount > 0
1036
+
1037
+ $count_del_prod = $this->db_magento->fetchRow("SELECT count(t1.entity_id) AS count__del_prod FROM (SELECT cpe.entity_id
1038
+ FROM {$this->tablePrefix}catalog_product_entity AS cpe
1039
+ JOIN {$this->tablePrefix}catalog_product_entity_varchar AS cpev
1040
+ ON cpe.entity_id = cpev.entity_id
1041
+ AND cpev.value = 1
1042
+ AND cpev.attribute_id = @is_iceimport_id
1043
+ LEFT JOIN {$this->tablePrefix}iceshop_iceimport_imported_product_ids AS iip
1044
+ ON cpe.entity_id = iip.product_id
1045
+ WHERE iip.product_id IS NULL GROUP BY cpe.entity_id) t1; ");
1046
+
1047
+ if(!empty($count_del_prod['count__del_prod'])){
1048
+ $count_del_prod = $count_del_prod['count__del_prod'];
1049
+ } else {
1050
+ $count_del_prod = 0;
1051
+ }
1052
+
1053
+ if ($count_del_prod > 0) {
1054
+ //iceimport products to delete exists, amount > 0
1055
+ $delete_old_products_tolerance = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/delete_old_products_tolerance');
1056
+
1057
+ if (round(($count_del_prod / $count_prod * 100), 0) <= $delete_old_products_tolerance) {
1058
+
1059
+ //iceimport products to delete franction is less than allowed tolerance, deletion approved
1060
+ $this->db_magento->query("DELETE cpe
1061
+ FROM {$this->tablePrefix}catalog_product_entity AS cpe
1062
+ JOIN {$this->tablePrefix}catalog_product_entity_varchar AS cpev
1063
+ ON cpe.entity_id = cpev.entity_id
1064
+ AND cpev.value = 1
1065
+ AND cpev.attribute_id = @is_iceimport_id
1066
+ LEFT JOIN {$this->tablePrefix}iceshop_iceimport_imported_product_ids AS iip
1067
+ ON cpe.entity_id = iip.product_id
1068
+ WHERE iip.product_id IS NULL");
1069
+
1070
+ $this->db_magento->query("TRUNCATE TABLE {$this->tablePrefix}iceshop_iceimport_imported_product_ids");
1071
+ } else {
1072
+ $error_message = 'Attempt to delete more old products than allowed in Iceimport configuration. Interruption of the process.';
1073
+ $DB_logger->insertLogEntry('error_try_delete_product', $error_message);
1074
+ $error_message2 = 'Old product percentage: ' . round(($count_del_prod / $count_prod * 100), 2) . '%';
1075
+ $DB_logger->insertLogEntry('error_try_delete_product_percentage', $error_message2);
1076
+
1077
+ //flag to warning notice
1078
+ $DB_logger->insertLogEntry('try_delete_product_percentage_warning_flag', 'SHOW');
1079
+ $DB_logger->insertLogEntry('iceimport_count_delete_product', $count_del_prod);
1080
+ print $error_message;
1081
+ print $error_message2;
1082
+ exit;
1083
+ }
1084
+ }
1085
+ $DB_logger->insertLogEntry('iceimport_count_delete_product', $count_del_prod);
1086
+ }
1087
+ } catch (Exception $e) {
1088
+ throw new Exception($e->getMessage());
1089
+ }
1090
+ }
1091
+
1092
+ public function processImageQueue($logFileName)
1093
+ {
1094
+
1095
+ $queueList = $this->db_magento->fetchAll("SELECT `queue_id`, `entity_id`, `image_url` FROM `{$this->tablePrefix}iceshop_iceimport_image_queue` WHERE `is_downloaded` = 0");
1096
+ if (count($queueList) > 0) {
1097
+ $mediaDir = Mage::getBaseDir('media');
1098
+ foreach ($queueList as $queue) {
1099
+
1100
+ $queueId = $queue['queue_id'];
1101
+ $productId = $queue['entity_id'];
1102
+ $imageUrl = $queue['image_url'];
1103
+
1104
+ try {
1105
+ $preImageName = explode('/', $imageUrl);
1106
+ $imageName = array_pop($preImageName);
1107
+ if (file_exists($mediaDir . DS . $imageName)) {
1108
+ $imageName = rand() . '_' . time() . $imageName;
1109
+ }
1110
+
1111
+ if (file_put_contents($mediaDir . DS . $imageName, file_get_contents($imageUrl))) {
1112
+ $product = Mage::getModel('catalog/product')->load($productId);
1113
+ $product->addImageToMediaGallery($mediaDir . DS . $imageName,
1114
+ ['image', 'small_image', 'thumbnail'],
1115
+ true, true
1116
+ );
1117
+ $product->save();
1118
+ $this->setImageAsDownloaded($queueId);
1119
+ unset($product);
1120
+ } else {
1121
+ $this->setImageAsDownloadedError($queueId);
1122
+ Mage::log('Unable download file to ' . $productId, $logFileName);
1123
+ continue;
1124
+ }
1125
+ } catch(Exception $e) {
1126
+ $this->db_magento->query("UPDATE `{$this->tablePrefix}iceshop_iceimport_image_queue` SET `is_downloaded` = 2 WHERE `queue_id` = '$queueId';");
1127
+ }
1128
+ }
1129
+ }
1130
+ }
1131
+
1132
+ public function setImageAsDownloaded($queueId = false)
1133
+ {
1134
+
1135
+ if ($queueId) {
1136
+ $this->db_magento->query(
1137
+ "UPDATE `{$this->tablePrefix}iceshop_iceimport_image_queue`
1138
+ SET is_downloaded = 1
1139
+ WHERE queue_id = :queue_id",
1140
+ array(':queue_id' => $queueId)
1141
+ );
1142
+ }
1143
+ }
1144
+
1145
+ public function setImageAsDownloadedError($queueId = false)
1146
+ {
1147
+
1148
+ if ($queueId) {
1149
+ $this->db_magento->query(
1150
+ "UPDATE `{$this->tablePrefix}iceshop_iceimport_image_queue`
1151
+ SET is_downloaded = 2
1152
+ WHERE queue_id = :queue_id",
1153
+ array(':queue_id' => $queueId)
1154
+ );
1155
+ }
1156
+ }
1157
+
1158
+ public function checkAndSetIceField()
1159
+ {
1160
+
1161
+ $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'active_ice')->getData();
1162
+ if (isset($attribute['attribute_id'])) {
1163
+
1164
+ $sql = "DROP PROCEDURE IF EXISTS FIELD_EXISTS;";
1165
+ $this->db_magento->query($sql);
1166
+
1167
+ $sql = "CREATE PROCEDURE FIELD_EXISTS(
1168
+ OUT _exists BOOLEAN, -- return value
1169
+ IN tableName CHAR(255) CHARACTER SET 'utf8', -- name of table to look for
1170
+ IN columnName CHAR(255) CHARACTER SET 'utf8', -- name of column to look for
1171
+ IN dbName CHAR(255) CHARACTER SET 'utf8' -- optional specific db
1172
+ ) BEGIN
1173
+ -- try to lookup db if none provided
1174
+ SET @_dbName := IF(dbName IS NULL, database(), dbName);
1175
+
1176
+ IF CHAR_LENGTH(@_dbName) = 0
1177
+ THEN -- no specific or current db to check against
1178
+ SELECT
1179
+ FALSE
1180
+ INTO _exists;
1181
+ ELSE -- we have a db to work with
1182
+ SELECT
1183
+ IF(count(*) > 0, TRUE, FALSE)
1184
+ INTO _exists
1185
+ FROM information_schema.COLUMNS c
1186
+ WHERE
1187
+ c.TABLE_SCHEMA = @_dbName
1188
+ AND c.TABLE_NAME = tableName
1189
+ AND c.COLUMN_NAME = columnName;
1190
+ END IF;
1191
+ END;";
1192
+ $this->db_magento->query($sql);
1193
+
1194
+ $sql = "CALL FIELD_EXISTS(@_exists, '{$this->tablePrefix}catalog_product_entity', 'active_ice', NULL);";
1195
+ $this->db_magento->query($sql);
1196
+
1197
+ $sql = "SELECT @_exists;";
1198
+ $res = $this->db_magento->fetchCol($sql);
1199
+ if (array_shift($res)) {
1200
+ $options = Mage::getModel('eav/config')->getAttribute('catalog_product', 'active_ice')->getSource()->getAllOptions();
1201
+ $optionId = false;
1202
+ foreach ($options as $option) {
1203
+ if ($option['label'] == 'Yes') {
1204
+ $optionId = $option['value'];
1205
+ break;
1206
+ }
1207
+ }
1208
+ if ($optionId) {
1209
+ $sql = "UPDATE `{$this->tablePrefix}catalog_product_entity` SET `active_ice` = '{$optionId}' WHERE `active_ice` IS NULL;";
1210
+ $this->db_magento->query($sql);
1211
+ }
1212
+ }
1213
+ }
1214
+ }
1215
+
1216
+ public function deleteTempTableProds ()
1217
+ {
1218
+
1219
+ $this->db_magento->query("DROP TABLE IF EXISTS {$this->tablePrefix}import_feed;");
1220
+ }
1221
+
1222
+ public function deleteTempTableCats ()
1223
+ {
1224
+
1225
+ $this->db_magento->query("DROP TABLE IF EXISTS {$this->tablePrefix}temp_cats;");
1226
+ }
1227
+
1228
+ public function deleteTempFileForCategories ()
1229
+ {
1230
+
1231
+ if (file_exists($this->filenameCats)) {
1232
+ unlink($this->filenameCats);
1233
+ }
1234
+ }
1235
+
1236
+ public function deleteTempFileForCatalogCategoryProductInsert ()
1237
+ {
1238
+
1239
+ if (file_exists($this->filenameValues)) {
1240
+ unlink($this->filenameValues);
1241
+ }
1242
+ }
1243
+
1244
+ public function deleteFileFromArrayForManualLaunching ()
1245
+ {
1246
+
1247
+ if (file_exists($this->tempFeed)) {
1248
+ unlink($this->tempFeed);
1249
+ }
1250
+ }
1251
+
1252
+ public function deleteTemFileForAttributes ()
1253
+ {
1254
+
1255
+ if (file_exists($this->tempAttr)) {
1256
+ unlink($this->tempAttr);
1257
+ }
1258
+ }
1259
+ }
app/code/community/ICEshop/Iceimport/Model/Convert/Adapter/Product.php DELETED
@@ -1,1868 +0,0 @@
1
- <?php
2
-
3
- /**
4
- * Import Product with additional attributes
5
- *
6
- *
7
- */
8
- class ICEshop_Iceimport_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
9
- {
10
- /**
11
- * @var array
12
- */
13
- protected $_categoryCache = array();
14
-
15
- /**
16
- * @var null
17
- */
18
- protected $_connRes = null;
19
-
20
- /**
21
- * @var string
22
- */
23
- protected $_tablePrefix = '';
24
- protected $_counter = 0;
25
-
26
- /**
27
- * @var
28
- */
29
- protected $_refreshSettings;
30
-
31
- /**
32
- * Import process ID
33
- *
34
- * @var int
35
- */
36
- protected $process_id = 'iceshop_iceimport';
37
-
38
- /**
39
- * Init class
40
- */
41
- public function __construct()
42
- {
43
-
44
- $this->_connRes = Mage::getSingleton('core/resource')->getConnection('core_write');
45
- $tablePrefix = (array)Mage::getConfig()->getTablePrefix();
46
- if (!empty($tablePrefix)) {
47
- $this->_tablePrefix = $tablePrefix[0];
48
- }
49
-
50
- }
51
-
52
- /**
53
- * @param $storeId
54
- */
55
-
56
- private function _initRefreshSettings($storeId)
57
- {
58
- $this->_refreshSettings = new Varien_Object();
59
- $this->_refreshSettings->setData('categories', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_categories_from_csv', $storeId));
60
- $this->_refreshSettings->setData('status', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_status_from_csv', $storeId));
61
- $this->_refreshSettings->setData('visibility', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_visibility_from_csv', $storeId));
62
- $this->_refreshSettings->setData('is_in_stock', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_is_in_stock_from_csv', $storeId));
63
- $this->_refreshSettings->setData('update_hide_category', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_hide_category', $storeId));
64
- $this->_refreshSettings->setData('url_key', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/update_url_key_from_csv', $storeId));
65
- $this->_refreshSettings->setData('import_new_products', (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_new_products', $storeId));
66
- }
67
-
68
- /**
69
- * @param $key
70
- * @return mixed
71
- */
72
- private function _getRefreshSetting($key)
73
- {
74
- $key = Mage::helper('iceimport')->toCamelCase($key, true);
75
- return $this->_refreshSettings->{'get' . $key}();
76
- }
77
-
78
- /**
79
- * @param array $importData
80
- * @return bool
81
- * @throws Mage_Core_Exception
82
- */
83
- public function saveRow(array $importData, $manually = false)
84
- {
85
-
86
- $DB_helper = Mage::helper('iceimport/db');
87
-
88
- $DB_logger = Mage::helper('iceimport/db');
89
-
90
- $transactions_enabled = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/transactions_enabled');
91
-
92
- if (empty($importData[0]['batchId'])) {
93
- $batchId = Mage::getSingleton('core/app')->getRequest()->getPost('batch_id', 0);
94
- } else {
95
- $batchId = (int)$importData[0]['batchId'];
96
- }
97
-
98
- // set store id
99
-
100
- if (empty($importData[0]['store'])) {
101
- if (!is_null($this->getBatchParams('store'))) {
102
- $store = $this->getStoreById($this->getBatchParams('store'));
103
- } else {
104
- $message = Mage::helper('catalog')->__('Skip import, required field "%s" not defined', 'store');
105
- Mage::throwException($message);
106
- }
107
- }
108
-
109
- if (empty($importData[0]['attribute_set'])) {
110
- $message = Mage::helper('catalog')->__('Skip import, required field "%s" not defined', 'attribute_set');
111
- Mage::throwException($message);
112
- }
113
-
114
- if (empty($store)) {
115
- $store = Mage::app()->getStore(trim($importData[0]['store']));
116
- }
117
-
118
- if ($store === false) {
119
- $message = Mage::helper('catalog')->__('Skip import, store "%s" not exists', $importData[0]['store']);
120
- Mage::throwException($message);
121
- }
122
-
123
- $storeId = $store->getId();
124
-
125
- $stock_name = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/stock_inventory');
126
-
127
- $defaulttaxConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/default_tax', $storeId);
128
-
129
- $stockConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_stock', $storeId);
130
- $priceConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_prices', $storeId);
131
-
132
- $categoriesToActiveConf = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/category_active', $storeId);
133
-
134
- $batch_size = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/iceimport_batch_size', $storeId);
135
-
136
- $addImages = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_images');
137
-
138
- //init refresh settings values
139
- $this->_initRefreshSettings($storeId);
140
-
141
- if ($transactions_enabled != 0 && !$manually) {
142
- $this->_connRes->beginTransaction();
143
- }
144
-
145
- foreach($importData as $key => $importDatum) {
146
-
147
- $sortedProductData = $this->_mapAttributes($importDatum);
148
-
149
- $productData = $sortedProductData['productData'];
150
- $iceimportAttributes = $sortedProductData['iceimportAttributes'];
151
-
152
-
153
- $DB_helper->deleteLogEntry('try_delete_product_percentage_warning_flag');
154
- if ($batchId != $DB_helper->getConfigData($this->process_id . '_flag')) {
155
- //set flag, first iteration
156
- $DB_helper->unsetConfigData("import_total");
157
- $DB_helper->unsetConfigData("counter");
158
- $DB_helper->unsetConfigData("skipped_counter");
159
- $DB_helper->setConfigData($this->process_id . '_flag', $batchId);
160
- }
161
-
162
- $import_total = $DB_helper->getConfigData("import_total");
163
- $counter = $DB_helper->getConfigData("counter");
164
- $skipped_counter = $DB_helper->getConfigData("skipped_counter");
165
-
166
- if (empty($import_total)) {
167
- $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
168
- $batchImportModel = $batchModel->getBatchImportModel();
169
- $importIds = $batchImportModel->getIdCollection();
170
- $import_total = count($importIds);
171
- $DB_helper->setConfigData("import_total", (int)$import_total);
172
- }
173
- if (empty($counter)) {
174
- $DB_helper->setConfigData("counter", 1);
175
- $counter = $DB_helper->getConfigData("counter");
176
- }
177
- if (empty($skipped_counter)) {
178
- $DB_helper->setConfigData("skipped_counter", 0);
179
- $skipped_counter = $DB_helper->getConfigData("skipped_counter");
180
- }
181
-
182
- // mark product ice_import generic
183
- $productData['varchar']['is_iceimport'] = 1;
184
-
185
- // set sku
186
- if (empty($iceimportAttributes['sku'])) {
187
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku');
188
- Mage::throwException($message);
189
- }
190
- $sku = $iceimportAttributes['sku'];
191
-
192
- if (!empty($iceimportAttributes['categories'])) {
193
- if (!empty($iceimportAttributes['categories'])) {
194
- $cat_names = explode('/', $iceimportAttributes['categories']);
195
- foreach ($cat_names as $cat_name) {
196
- if (empty($cat_name)) {
197
- $message = Mage::helper('catalog')->__('Skip import row, some of categories does not have name');
198
- Mage::throwException($message);
199
- }
200
- }
201
- }
202
- }
203
- if (!empty($iceimportAttributes['categories'])) {
204
- $category = $iceimportAttributes['categories'];
205
- if (empty($iceimportAttributes['unspsc'])) {
206
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC not defined in store');
207
- Mage::throwException($message);
208
- }
209
- $unspsc = $iceimportAttributes['unspsc'];
210
- $unspsc = preg_replace("/[^a-zA-Z0-9]/", "", $unspsc);
211
- if (empty($iceimportAttributes['unspsc_path'])) {
212
- $message = Mage::helper('catalog')->__('Skip import. Category UNSPSC path not defined in store');
213
- Mage::throwException($message);
214
- }
215
- if (!empty($iceimportAttributes['unspsc_path'])) {
216
- $cat_unspscs = explode('/', $iceimportAttributes['unspsc_path']);
217
- foreach ($cat_unspscs as $cat_unspsc) {
218
- if (empty($cat_unspsc)) {
219
- $message = Mage::helper('catalog')->__('Skip import row, some of categories does not have UNSPSC');
220
- Mage::throwException($message);
221
- }
222
- }
223
- }
224
- $unspscPath = $iceimportAttributes['unspsc_path'];
225
- $unspscPath = preg_replace("/[^a-zA-Z0-9\/]/", "", $unspscPath);
226
- if (!empty($cat_unspscs) && !empty($cat_names) && count($cat_names) != count($cat_unspscs)) {
227
- $message = Mage::helper('catalog')->__('Skip import row, categories names does not match categories UNSPSC');
228
- Mage::throwException($message);
229
- }
230
- }
231
-
232
- // set website id;
233
- $websites = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/websites');
234
- if (empty($websites)) {
235
- $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'websites');
236
- Mage::throwException($message);
237
- }
238
-
239
- $website = Mage::app()->getWebsite(trim($websites));
240
- $websiteId = $website->getId();
241
-
242
- // set in / out of stock
243
- $isInStock = 0;
244
- if (!empty($iceimportAttributes['is_in_stock'])) {
245
- $isInStock = $iceimportAttributes['is_in_stock'];
246
- }
247
-
248
- // set qty
249
- $qty = 0;
250
- if (!empty($iceimportAttributes['qty'])) {
251
- $qty = $iceimportAttributes['qty'];
252
- }
253
-
254
- // set price
255
- $price = 0.00;
256
- if (!empty($iceimportAttributes['price'])) {
257
- $price = $iceimportAttributes['price'];
258
- }
259
-
260
- // set tax_auvibel
261
- $tax_auvibel = 0.00;
262
- if (!empty($iceimportAttributes['tax_auvibel'])) {
263
- $tax_auvibel = $iceimportAttributes['tax_auvibel'];
264
- }
265
-
266
- // set tax_bebat
267
- $tax_bebat = 0.00;
268
- if (!empty($iceimportAttributes['tax_bebat'])) {
269
- $tax_bebat = $iceimportAttributes['tax_bebat'];
270
- }
271
-
272
- // set tax_recupel
273
- $tax_recupel = 0.00;
274
- if (!empty($iceimportAttributes['tax_recupel'])) {
275
- $tax_recupel = $iceimportAttributes['tax_recupel'];
276
- }
277
-
278
- // set tax_reprobel
279
- $tax_reprobel = 0.00;
280
- if (!empty($iceimportAttributes['tax_reprobel'])) {
281
- $tax_reprobel = $iceimportAttributes['tax_reprobel'];
282
- }
283
-
284
- // set status value
285
- $statusValue = (!empty($iceimportAttributes['status']) && $iceimportAttributes['status'] == 'Enabled') ? 1 : 2;
286
- $productData['int']['status'] = $statusValue;
287
-
288
- // set visibility value
289
- $visibilityValue = 1;
290
- if (!empty($iceimportAttributes['visibility'])) {
291
- switch ($iceimportAttributes['visibility']) {
292
- case 'Not Visible Individually':
293
- $visibilityValue = 1;
294
- break;
295
- case 'Catalog':
296
- $visibilityValue = 2;
297
- break;
298
- case 'Search':
299
- $visibilityValue = 3;
300
- break;
301
- case 'Catalog, Search':
302
- $visibilityValue = 4;
303
- break;
304
- }
305
- }
306
- $productData['int']['visibility'] = $visibilityValue;
307
-
308
- // set product image
309
- $productImage = '';
310
- if (!empty($iceimportAttributes['image'])) {
311
- $productImage = $iceimportAttributes['image'];
312
- }
313
-
314
- $initAttributes = '';
315
- // init general attributes query
316
- $initAttributes .= "SELECT @product_entity_type_id := `entity_type_id`
317
- FROM `{$this->_tablePrefix}eav_entity_type`
318
- WHERE entity_type_code = 'catalog_product';";
319
-
320
- $initAttributes .= "SELECT @price_id := `attribute_id`
321
- FROM `{$this->_tablePrefix}eav_attribute`
322
- WHERE `attribute_code` = 'price'
323
- AND entity_type_id = @product_entity_type_id;";
324
-
325
- $initAttributes .= "SELECT @attribute_set_id := `attribute_set_id`
326
- FROM `{$this->_tablePrefix}eav_attribute_set`
327
- WHERE attribute_set_name = :attribute_set
328
- AND entity_type_id = @product_entity_type_id;";
329
-
330
- if (!empty($iceimportAttributes['categories'])) {
331
- $initAttributes .= "SELECT @category_entity_type_id := `entity_type_id`
332
- FROM `{$this->_tablePrefix}eav_entity_type`
333
- WHERE entity_type_code = 'catalog_category';";
334
- }
335
-
336
- if (!empty($iceimportAttributes['categories'])) {
337
- $initAttributes .= "SELECT @unspsc_id := `attribute_id`
338
- FROM `{$this->_tablePrefix}eav_attribute`
339
- WHERE `attribute_code` = 'unspsc'
340
- AND entity_type_id = @category_entity_type_id;";
341
-
342
- $initAttributes .= "SELECT @category_name_id := `attribute_id`
343
- FROM `{$this->_tablePrefix}eav_attribute`
344
- WHERE `attribute_code` = 'name'
345
- AND entity_type_id = @category_entity_type_id;";
346
-
347
- $initAttributes .= "SELECT @category_active_id := `attribute_id`
348
- FROM `{$this->_tablePrefix}eav_attribute`
349
- WHERE `attribute_code` = 'is_active'
350
- AND entity_type_id = @category_entity_type_id;";
351
-
352
- $initAttributes .= "SELECT @include_nav_bar_id := `attribute_id`
353
- FROM `{$this->_tablePrefix}eav_attribute`
354
- WHERE `attribute_code` = 'include_in_menu'
355
- AND entity_type_id = @category_entity_type_id;";
356
-
357
- $initAttributes .= "SELECT @category_is_anchor_id := `attribute_id`
358
- FROM `{$this->_tablePrefix}eav_attribute`
359
- WHERE `attribute_code` = 'is_anchor'
360
- AND entity_type_id = @category_entity_type_id;";
361
- }
362
-
363
- $initAttributes .= "SELECT @tax_auvibel_id := `attribute_id`
364
- FROM `{$this->_tablePrefix}eav_attribute`
365
- WHERE `attribute_code` = 'tax_auvibel'
366
- AND entity_type_id = @product_entity_type_id;";
367
-
368
- $initAttributes .= "SELECT @tax_bebat_id := `attribute_id`
369
- FROM `{$this->_tablePrefix}eav_attribute`
370
- WHERE `attribute_code` = 'tax_bebat'
371
- AND entity_type_id = @product_entity_type_id;";
372
-
373
- $initAttributes .= "SELECT @tax_recupel_id := `attribute_id`
374
- FROM `{$this->_tablePrefix}eav_attribute`
375
- WHERE `attribute_code` = 'tax_recupel'
376
- AND entity_type_id = @product_entity_type_id;";
377
-
378
- $initAttributes .= "SELECT @tax_reprobel_id := `attribute_id`
379
- FROM `{$this->_tablePrefix}eav_attribute`
380
- WHERE `attribute_code` = 'tax_reprobel'
381
- AND entity_type_id = @product_entity_type_id;";
382
-
383
- if (!empty($stock_name)) {
384
- $stock_id = $this->_connRes->fetchRow("SELECT stock_id FROM `{$this->_tablePrefix}cataloginventory_stock` WHERE stock_name = :stock_name LIMIT 1", array(
385
- ':stock_name' => $stock_name
386
- ));
387
- if (!empty($stock_id)) {
388
- $initAttributes .= "SELECT @stock_id := `stock_id` FROM `{$this->_tablePrefix}cataloginventory_stock` WHERE stock_name = '" . $stock_name . "';";
389
- } else {
390
- $message = Mage::helper('catalog')->__('Skip import row, stock name "' . $stock_name . '" does not exists in the shop');
391
- Mage::throwException($message);
392
- }
393
- } else {
394
- $initAttributes .= "SELECT @stock_id := `stock_id` FROM `{$this->_tablePrefix}cataloginventory_stock` WHERE stock_name = 'Default';";
395
- }
396
- $this->_connRes->query($initAttributes, array(':attribute_set' => $iceimportAttributes['attribute_set']));
397
-
398
- // get tax class id
399
-
400
- $productData['int']['tax_class_id'] = $defaulttaxConf;
401
-
402
- if (!empty($iceimportAttributes['categories'])) {
403
- // get category id
404
- $categoryIds = $this->_addCategories($category, $storeId, $unspsc, $unspscPath, $categoriesToActiveConf);
405
- }
406
-
407
- // get url key
408
- $url = '';
409
- if (!empty($productData['varchar']['name'])) {
410
- $preUrl = explode(' ', strtolower($productData['varchar']['name']));
411
- $url = implode('-', $preUrl);
412
- }
413
- $productData['varchar']['url_key'] = $url;
414
-
415
- $prodIdFetch = $this->_connRes->fetchRow("SELECT entity_id FROM `{$this->_tablePrefix}catalog_product_entity` WHERE sku = :sku LIMIT 1", array(
416
- ':sku' => $sku
417
- ));
418
- $productId = $prodIdFetch['entity_id'];
419
-
420
- if (!empty($productId)) {
421
- // check import type (Import only price & qty or all product info)
422
-
423
- if (!empty($iceimportAttributes['categories'])) {
424
- $productId = $this->_coreSave($productData, $productId, $storeId, $sku, $categoryIds);
425
- } else {
426
- $productId = $this->_coreSave($productData, $productId, $storeId, $sku);
427
- }
428
-
429
- $this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock, $stockConf, $priceConf, 0);
430
-
431
- $this->_connRes->query("INSERT INTO {$this->_tablePrefix}iceshop_iceimport_imported_product_ids (product_id, product_sku) VALUES (:prod_id, :sku) ON DUPLICATE KEY UPDATE product_sku = :sku", array(':prod_id' => $productId, ':sku' => $sku));
432
- } else {
433
- if ($this->_getRefreshSetting('import_new_products') == 0) {
434
- $DB_helper->setConfigData("skipped_counter", (int)++$skipped_counter);
435
- return true;
436
- }
437
-
438
- if (!empty($iceimportAttributes['categories'])) {
439
- $productId = $this->_coreSave($productData, $productId, $storeId, $sku, $categoryIds);
440
- } else {
441
- $productId = $this->_coreSave($productData, $productId, $storeId, $sku);
442
- }
443
- // add price & stock
444
- if ($productId) {
445
- $this->_corePriceStock($websiteId, $productId, $price, $qty, $sku, $isInStock);
446
- $this->_connRes->query("INSERT INTO {$this->_tablePrefix}iceshop_iceimport_imported_product_ids (product_id, product_sku) VALUES (:prod_id, :sku) ON DUPLICATE KEY UPDATE product_sku = :sku", array(':prod_id' => $productId, ':sku' => $sku));
447
- }
448
- }
449
-
450
- $this->_connRes->query("DELETE FROM {$this->_tablePrefix}weee_tax WHERE entity_id = :prod_id AND attribute_id = @tax_auvibel_id", array(':prod_id' => $productId));
451
- $this->_connRes->query("DELETE FROM {$this->_tablePrefix}weee_tax WHERE entity_id = :prod_id AND attribute_id = @tax_bebat_id", array(':prod_id' => $productId));
452
- $this->_connRes->query("DELETE FROM {$this->_tablePrefix}weee_tax WHERE entity_id = :prod_id AND attribute_id = @tax_recupel_id", array(':prod_id' => $productId));
453
- $this->_connRes->query("DELETE FROM {$this->_tablePrefix}weee_tax WHERE entity_id = :prod_id AND attribute_id = @tax_reprobel_id", array(':prod_id' => $productId));
454
-
455
- $countryCode = Mage::getStoreConfig('general/country/default');
456
- if ($tax_auvibel > 0) {
457
- $query = "INSERT INTO {$this->_tablePrefix}weee_tax (website_id, entity_id, country, value, state, attribute_id, entity_type_id)"
458
- . "VALUES (0, :prod_id, :country, :value, '*', @tax_auvibel_id, @product_entity_type_id)";
459
- $this->_connRes->query($query, [':prod_id' => $productId, ':country' => $countryCode, ':value' => $tax_auvibel]);
460
- }
461
-
462
- if ($tax_bebat > 0) {
463
- $query = "INSERT INTO {$this->_tablePrefix}weee_tax (website_id, entity_id, country, value, state, attribute_id, entity_type_id)"
464
- . "VALUES (0, :prod_id, :country, :value, '*', @tax_bebat_id, @product_entity_type_id)";
465
- $this->_connRes->query($query, [':prod_id' => $productId, ':country' => $countryCode, ':value' => $tax_bebat]);
466
- }
467
-
468
- if ($tax_recupel > 0) {
469
- $query = "INSERT INTO {$this->_tablePrefix}weee_tax (website_id, entity_id, country, value, state, attribute_id, entity_type_id)"
470
- . "VALUES (0, :prod_id, :country, :value, '*', @tax_recupel_id, @product_entity_type_id)";
471
- $this->_connRes->query($query, [':prod_id' => $productId, ':country' => $countryCode, ':value' => $tax_recupel]);
472
- }
473
-
474
- if ($tax_reprobel > 0) {
475
- $query = "INSERT INTO {$this->_tablePrefix}weee_tax (website_id, entity_id, country, value, state, attribute_id, entity_type_id)"
476
- . "VALUES (0, :prod_id, :country, :value, '*', @tax_reprobel_id, @product_entity_type_id)";
477
- $this->_connRes->query($query, [':prod_id' => $productId, ':country' => $countryCode, ':value' => $tax_reprobel]);
478
- }
479
-
480
- // add product image to queue
481
- if ($addImages) {
482
- $this->_addImageToQueue($productId, $productImage);
483
- }
484
-
485
-
486
- $counter_sum = $counter + $skipped_counter;
487
-
488
- if (isset($counter) && isset($skipped_counter) && isset($import_total) && $counter_sum == $import_total) {
489
-
490
- $this->_runCategoriesSorting();
491
-
492
- $this->deleteOldProducts($DB_logger);
493
-
494
- $DB_logger->insertLogEntry('error' . md5(microtime(true)), 'New products skipped while export according to Iceimport settings: ' . $skipped_counter, 'stat');
495
-
496
- $date = date('m/d/Y H:i:s');
497
- $DB_logger->insertLogEntry('iceimport_import_ended', $date);
498
- }
499
- if ($counter < $import_total) {
500
- $DB_helper->setConfigData("counter", (int)++$counter);
501
- }
502
-
503
- if ($transactions_enabled != 0 && !$manually) {
504
- $this->_counter++;
505
-
506
- if ($this->_counter % $batch_size == 0) {
507
- try {
508
- $this->_connRes->commit();
509
- } catch (Exception $e) {
510
- $this->_connRes->rollBack();
511
- throw $e;
512
- }
513
- $this->_connRes->beginTransaction();
514
- }
515
- if ($this->_counter == count($importData)) {
516
- try {
517
- $this->_connRes->commit();
518
- } catch (Exception $e) {
519
- $this->_connRes->rollBack();
520
- throw $e;
521
- }
522
- }
523
- }
524
- }
525
- return true;
526
- }
527
-
528
- /**
529
- * @param $arg_attribute
530
- * @param $arg_value
531
- * @return bool
532
- */
533
- public function getAttributeOptionValue($arg_attribute, $arg_value)
534
- {
535
- $attribute_model = Mage::getModel('eav/entity_attribute');
536
- $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
537
-
538
- $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
539
- $attribute = $attribute_model->load($attribute_code);
540
-
541
- $attribute_options_model->setAttribute($attribute);
542
- $options = $attribute_options_model->getAllOptions(false);
543
-
544
- foreach ($options as $option) {
545
- if ($option['label'] == $arg_value) {
546
- return $option['value'];
547
- }
548
- }
549
- return false;
550
- }
551
-
552
-
553
- /**
554
- * @param $arg_attribute
555
- * @param $arg_value
556
- * @return bool
557
- * @throws Exception
558
- */
559
- public function addAttributeOption($arg_attribute, $arg_value)
560
- {
561
- $attribute_model = Mage::getModel('eav/entity_attribute');
562
- $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
563
-
564
- $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
565
- $attribute = $attribute_model->load($attribute_code);
566
-
567
- $attribute_options_model->setAttribute($attribute);
568
- $attribute_options_model->getAllOptions(false);
569
-
570
- $value = array();
571
- $value['option'] = array($arg_value, $arg_value);
572
- $result = array('value' => $value);
573
-
574
- $attribute->setData('option', $result);
575
- $attribute->save();
576
-
577
- return $this->getAttributeOptionValue($arg_attribute, $arg_value);
578
- }
579
-
580
-
581
- /**
582
- * @param array $entityData
583
- * @param null $productId
584
- * @param int $storeId
585
- * @param $sku
586
- * @param $categoryIds
587
- * @return null
588
- */
589
- protected function _coreSave(array $entityData, $productId = null, $storeId = 0, $sku, $categoryIds =NULL)
590
- {
591
-
592
- $currentDate = Mage::app()->getLocale()->date(null, null, null, false)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
593
-
594
- $coreSaveSQL = '';
595
- $newProduct = false;
596
- if ($productId === null) {
597
- // add product to store
598
- $coreSaveProduct = "INSERT INTO `{$this->_tablePrefix}catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`, `updated_at`)
599
- VALUES (@product_entity_type_id, @attribute_set_id, 'simple', :sku, :current_date, :current_date);
600
- SELECT @product_id := LAST_INSERT_ID();";
601
-
602
- $this->_connRes->query($coreSaveProduct, array(':sku' => $sku, ':current_date' => $currentDate));
603
- $prodFetch = $this->_connRes->fetchRow("SELECT @product_id AS prod_id");
604
- $productId = $prodFetch['prod_id'];
605
- $newProduct = TRUE;
606
-
607
- } else {
608
- $productId = (int)$productId;
609
- $coreSaveSQL .= "SELECT @product_id := {$productId}; ";
610
- $producteanConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_ean', $storeId);
611
- $productmpnConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_mpn', $storeId);
612
- $productnameConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_name', $storeId);
613
- $productshdescriptionConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_short_description', $storeId);
614
- $productdescriptionConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_description', $storeId);
615
- $productbrandConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_brand_name', $storeId);
616
- $deliveryetaConf = (int)Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/import_product_delivery_eta', $storeId);
617
- $attribute_mapping_mpn = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_mpn');
618
- $attribute_mapping_brand_name = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_brand_name');
619
- $attribute_mapping_ean = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_ean');
620
- $attribute_mapping_delivery_eta = Mage::getStoreConfig('iceshop_iceimport_importprod_root/importprod/attribute_mapping_delivery_eta');
621
-
622
- foreach ($entityData as $type => $typeAttributes) {
623
- foreach ($typeAttributes as $attribute => $value) {
624
- if (
625
- (
626
- ($attribute == $attribute_mapping_mpn && $productmpnConf == 0) ||
627
- ($attribute == $attribute_mapping_brand_name && $productbrandConf == 0) ||
628
- ($attribute == $attribute_mapping_ean && $producteanConf == 0) ||
629
- ($attribute == 'name' && $productnameConf == 0) ||
630
- ($attribute == 'short_description' && $productshdescriptionConf == 0) ||
631
- ($attribute == 'description' && $productdescriptionConf == 0) ||
632
- ($attribute == $attribute_mapping_delivery_eta && $deliveryetaConf == 0)
633
- ) || (
634
- $attribute == 'sku' ||
635
- ($attribute == 'attribute_set') ||
636
- ($attribute == 'categories' && $this->_getRefreshSetting('categories') == 0) ||
637
- ($attribute == 'unspsc') ||
638
- ($attribute == 'price') ||
639
- ($attribute == 'qty') ||
640
- ($attribute == 'status' && $this->_getRefreshSetting('status') == 0) ||
641
- ($attribute == 'visibility' && $this->_getRefreshSetting('visibility') == 0) ||
642
- ($attribute == 'store') ||
643
- ($attribute == 'websites') ||
644
- ($attribute == 'is_in_stock' && $this->_getRefreshSetting('is_in_stock') == 0) ||
645
- ($attribute == 'url_key' && $this->_getRefreshSetting('url_key') == 0) ||
646
- ($attribute == 'image') ||
647
- ($attribute == 'unspsc_path') ||
648
- ($attribute == 'stock_name') ||
649
- ($attribute == 'tax_auvibel') ||
650
- ($attribute == 'tax_bebat') ||
651
- ($attribute == 'tax_recupel') ||
652
- ($attribute == 'tax_reprobel')
653
- )
654
- ) {
655
- unset($entityData[$type][$attribute]);
656
- }
657
- }
658
- }
659
-
660
- $updateDate = "UPDATE `{$this->_tablePrefix}catalog_product_entity` SET `updated_at` = :current_date where entity_id = {$productId}";
661
- $this->_connRes->query($updateDate, array(':current_date' => $currentDate));
662
- }
663
-
664
- $bindArray[':store_id'] = $storeId;
665
-
666
- foreach ($entityData as $type => $typeAttributes) {
667
-
668
- if ($type != 'spec') {
669
- $tailCoreSaveSQL = '';
670
- $attributesInit = '';
671
- if ($type == 'select') {
672
- $type = 'int';
673
- $is_select = 1;
674
- } else {
675
- $is_select = 0;
676
- }
677
- if (!empty($typeAttributes)) {
678
- $tailCoreSaveSQL .= "INSERT INTO `{$this->_tablePrefix}catalog_product_entity_{$type}` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES ";
679
- foreach ($typeAttributes as $attribute => $value) {
680
- if($this->_checkAttributeExist($attribute)){
681
- if ($is_select == 1) {
682
- $option_id = $this->getAttributeOptionValue($attribute, $value);
683
- if (empty($option_id)) {
684
- $option_id = $this->addAttributeOption($attribute, $value);
685
- }
686
- $value = $option_id;
687
- }
688
- $attributesInit .= "SELECT @product_entity_type_id := `entity_type_id`
689
- FROM `{$this->_tablePrefix}eav_entity_type`
690
- WHERE entity_type_code = 'catalog_product';";
691
- $attributesInit .= "SELECT @{$attribute}_id := `attribute_id`
692
- FROM `{$this->_tablePrefix}eav_attribute`
693
- WHERE `attribute_code` = '{$attribute}'
694
- AND entity_type_id = @product_entity_type_id;";
695
-
696
- $tailCoreSaveSQL .= "
697
- (@product_entity_type_id, @{$attribute}_id, 0, @product_id, :{$attribute} ),
698
- (@product_entity_type_id, @{$attribute}_id, :store_id, @product_id, :{$attribute} ), ";
699
- $bindArray[':' . $attribute] = $value;
700
- }
701
- }
702
- $tailCoreSaveSQL = substr($tailCoreSaveSQL, 0, -2);
703
- $tailCoreSaveSQL .= "
704
- ON DUPLICATE KEY UPDATE
705
- `value` = VALUES (`value`);";
706
- }
707
- $coreSaveSQL .= $attributesInit . $tailCoreSaveSQL;
708
- } else {
709
- foreach ($typeAttributes as $attribute => $attributeData) {
710
- if($this->_checkAttributeExist($attribute)){
711
- $prod_id_field = $attributeData['prod_id_field'];
712
- $table = $attributeData['table'];
713
- $field = $attributeData['field'];
714
- $value = $attributeData['value'];
715
- if (!empty($table) && !empty($field)) {
716
- $coreSaveSQL .= "UPDATE `{$this->_tablePrefix}{$table}`
717
- SET `{$field}` = :{$attribute}
718
- WHERE `{$prod_id_field}` = @product_id;";
719
- $bindArray[':' . $attribute] = $value;
720
- }
721
- }
722
- }
723
- }
724
- }
725
- // categories
726
- if (($newProduct || ($productId === null) || ($productId !== null && $this->_getRefreshSetting('categories') == 1)) && !empty($categoryIds)) {
727
- $coreSaveSQL .= "INSERT INTO `{$this->_tablePrefix}catalog_category_product` (`category_id`, `product_id`, `position`) VALUES ";
728
- $counter = 1;
729
-
730
- $mapCategoryIds = array();
731
- $mapCategoryIds[] = array_pop($categoryIds);
732
- $delCategoryIds = array_diff($categoryIds, $mapCategoryIds);
733
-
734
- foreach ($mapCategoryIds as $categoryId) {
735
- if ($counter < count($mapCategoryIds)) {
736
- $coreSaveSQL .= " (" . (int)$categoryId . ", @product_id, 1) , ";
737
- } else if ($counter == count($mapCategoryIds)) {
738
- $coreSaveSQL .= " (" . (int)$categoryId . ", @product_id, 1) ON DUPLICATE KEY UPDATE `position` = 1; ";
739
- }
740
- $counter++;
741
- }
742
-
743
- $not_delete_category = $this->getCategoryIdEmtyUnspsc($storeId);
744
- $noDelCategory = '';
745
- if(!empty($not_delete_category)){
746
- foreach ($not_delete_category as $category_ID ){
747
- $noDelCategory .= ''.$category_ID['entity_id'] . ',';
748
- }
749
- }
750
- if(!empty($noDelCategory)){
751
- $noDelCategory = substr($noDelCategory, 0, -1);
752
- }
753
-
754
- if (!empty($mapCategoryIds)) {
755
- foreach ($mapCategoryIds as $delCategoryId) {
756
- $delCategoryId = (int)$delCategoryId;
757
- $coreSaveSQL .= "DELETE FROM `{$this->_tablePrefix}catalog_category_product`";
758
- if(!empty($noDelCategory)){
759
- $coreSaveSQL .= "WHERE `category_id` NOT IN({$noDelCategory},{$delCategoryId}) AND `product_id` = @product_id;";
760
- } else {
761
- $coreSaveSQL .= "WHERE `category_id`!={$delCategoryId} AND `product_id` = @product_id;";
762
- }
763
- }
764
- }
765
- try {
766
- $this->_connRes->query($coreSaveSQL, $bindArray);
767
- unset($coreSaveSQL, $bindArray);
768
- } catch (Exception $e) {
769
- echo $e->getMessage();
770
- }
771
- } else {
772
- try {
773
- $this->_connRes->query($coreSaveSQL, $bindArray);
774
- unset($coreSaveSQL, $bindArray);
775
- } catch (Exception $e) {
776
- echo $e->getMessage();
777
- }
778
- }
779
- return $productId;
780
- }
781
-
782
- /**
783
- * Verify the existence of an attribute
784
- * @param string $attributeCode
785
- * @return boolean
786
- */
787
- protected function _checkAttributeExist($attributeCode){
788
- $query = "SELECT `attribute_id` FROM `{$this->_tablePrefix}eav_attribute` WHERE attribute_code='{$attributeCode}';";
789
- $attributeCheck = $this->_connRes->fetchRow($query);
790
- $attributeId = $attributeCheck['attribute_id'];
791
- if(empty($attributeId)){
792
- return false;
793
- }
794
- return true;
795
- }
796
-
797
- /**
798
- * @param int $website
799
- * @param bool $productId
800
- * @param float $price
801
- * @param float $qty
802
- * @param bool $sku
803
- * @param int $isInStock
804
- * @param int $stockConf
805
- * @param int $priceConf
806
- * @param int $new_product
807
- * @throws Mage_Core_Exception
808
- */
809
- protected function _corePriceStock($website = 0, $productId = false, $price = 0.00, $qty = 0.00, $sku = false, $isInStock = 0, $stockConf = 1, $priceConf = 1, $new_product = 1)
810
- {
811
-
812
- if (!$productId) {
813
- $message = Mage::helper('catalog')->__('Skip import row, product_id for product "%s" not defined ', $sku);
814
- Mage::throwException($message);
815
- }
816
- $stockSaveSQL = '';
817
- if ($stockConf == 1) {
818
- $stockSaveSQL .= "INSERT INTO `{$this->_tablePrefix}cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `is_in_stock`)
819
- VALUES (:product_id, @stock_id, :qty,1)
820
- ON DUPLICATE KEY UPDATE
821
- `product_id` = :product_id,
822
- `stock_id` = @stock_id,
823
- `qty` = :qty";
824
-
825
- if ($new_product == 1 || $this->_getRefreshSetting('is_in_stock') == 1) {
826
- $stockSaveSQL .= ",
827
- `is_in_stock` = :is_in_stock";
828
- }
829
- $stockSaveSQL .= ";";
830
- $fields_values = array(
831
- ':product_id' => $productId,
832
- ':websiteId' => $website,
833
- ':qty' => $qty
834
- );
835
- if ($new_product == 1 || $this->_getRefreshSetting('is_in_stock') == 1) {
836
- $fields_values[':is_in_stock'] = $isInStock;
837
- }
838
- $this->_connRes->query($stockSaveSQL, $fields_values);
839
- $stockSaveSQL = "";
840
-
841
- $stockSaveSQL .= "INSERT INTO `{$this->_tablePrefix}cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`)
842
- VALUES (:product_id, :websiteId, @stock_id, :qty, 1)
843
- ON DUPLICATE KEY UPDATE
844
- `product_id` = :product_id,
845
- `website_id` = :websiteId,
846
- `stock_id` = @stock_id,
847
- `qty` = :qty";
848
- if ($new_product == 1 || $this->_getRefreshSetting('is_in_stock') == 1) {
849
- $stockSaveSQL .= ",
850
- `stock_status` = :is_in_stock";
851
- }
852
- $stockSaveSQL .= ";";
853
- $fields_values = array(
854
- ':product_id' => $productId,
855
- ':websiteId' => $website,
856
- ':qty' => $qty
857
- );
858
- if ($new_product == 1 || $this->_getRefreshSetting('is_in_stock') == 1) {
859
- $fields_values[':is_in_stock'] = $isInStock;
860
- }
861
- $this->_connRes->query($stockSaveSQL, $fields_values);
862
- $stockSaveSQL = "";
863
-
864
- } elseif ($new_product == 0) {
865
- //existent product
866
- if ($this->_getRefreshSetting('is_in_stock') == 1) {
867
- $stockSaveSQL .= "UPDATE `{$this->_tablePrefix}cataloginventory_stock_item` SET `is_in_stock` = :is_in_stock WHERE `product_id` = :product_id AND `stock_id` = @stock_id;";
868
- $stockSaveSQL .= "UPDATE `{$this->_tablePrefix}cataloginventory_stock_status` SET `stock_status` = :is_in_stock WHERE `product_id` = :product_id AND `website_id` = :websiteId AND `stock_id` = @stock_id;";
869
- $fields_values = array(
870
- ':websiteId' => $website,
871
- ':product_id' => $productId,
872
- ':is_in_stock' => $isInStock
873
- );
874
- $this->_connRes->query($stockSaveSQL, $fields_values);
875
- $stockSaveSQL = '';
876
- }
877
- }
878
-
879
- if ($priceConf == 1) {
880
- $stockSaveSQL .= "INSERT INTO `{$this->_tablePrefix}catalog_product_website` (`product_id`, `website_id`)
881
- VALUES (:product_id, :websiteId)
882
- ON DUPLICATE KEY UPDATE
883
- `product_id` = :product_id,
884
- `website_id` = :websiteId;";
885
-
886
- $fields_values = array(
887
- ':websiteId' => $website,
888
- ':product_id' => $productId
889
- );
890
- $this->_connRes->query($stockSaveSQL, $fields_values);
891
- $stockSaveSQL = "";
892
- $stockSaveSQL .= "INSERT INTO `{$this->_tablePrefix}catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`)
893
- VALUES (@product_entity_type_id, @price_id, 0, :product_id, :price)
894
- ON DUPLICATE KEY UPDATE
895
- `entity_type_id` = @product_entity_type_id,
896
- `attribute_id` = @price_id,
897
- `store_id` = 0,
898
- `entity_id` = :product_id,
899
- `value` = :price;";
900
- $stockSaveSQL .= "INSERT INTO `{$this->_tablePrefix}catalog_product_entity_decimal` (`entity_type_id`,`attribute_id`,`store_id`, `entity_id`, `value`)
901
- VALUES (@product_entity_type_id, @price_id, {$website}, :product_id, :price)
902
- ON DUPLICATE KEY UPDATE
903
- `entity_type_id` = @product_entity_type_id,
904
- `attribute_id` = @price_id,
905
- `store_id` = {$website},
906
- `entity_id` = :product_id,
907
- `value` = :price;";
908
- $fields_values = array(
909
- ':product_id' => $productId,
910
- ':price' => $price,
911
- ':qty' => $qty
912
- );
913
- if ($new_product == 1 || $this->_getRefreshSetting('is_in_stock') == 1) {
914
- $fields_values[':is_in_stock'] = $isInStock;
915
- }
916
- $this->_connRes->query($stockSaveSQL, $fields_values);
917
- $stockSaveSQL = "";
918
- }
919
- if (($priceConf == 1 || $stockConf == 1) && !empty($stockSaveSQL)) {
920
- $fields_values = array(
921
- ':websiteId' => $website,
922
- ':product_id' => $productId,
923
- ':price' => $price,
924
- ':qty' => $qty
925
- );
926
- if ($this->_getRefreshSetting('is_in_stock') == 1) {
927
- $fields_values[':is_in_stock'] = $isInStock;
928
- }
929
- $this->_connRes->query($stockSaveSQL, $fields_values);
930
- unset($stockSaveSQL);
931
- }
932
- unset($fields_values);
933
- }
934
-
935
- /**
936
- * @param bool $productId
937
- * @param $productImageUrl
938
- */
939
- protected function _addImageToQueue($productId = false, $productImageUrl)
940
- {
941
- $productImageUrl = trim($productImageUrl);
942
- if ($productId && !empty($productImageUrl)) {
943
- // add image if not exists to queue
944
- $this->_connRes->query(
945
- "INSERT IGNORE INTO `{$this->_tablePrefix}iceshop_iceimport_image_queue` (`entity_id`, `image_url` )
946
- VALUES (:product_id, :image_url)",
947
- array(
948
- ':product_id' => $productId,
949
- ':image_url' => $productImageUrl
950
- )
951
- );
952
- }
953
- }
954
-
955
- /**
956
- * @return mixed
957
- */
958
- private function getImageQueue()
959
- {
960
- return $this->_connRes->fetchAll("SELECT `queue_id`, `entity_id`, `image_url`
961
- FROM `{$this->_tablePrefix}iceshop_iceimport_image_queue`
962
- WHERE `is_downloaded` = 0");
963
- }
964
-
965
- /**
966
- * @param $logFileName
967
- * @throws Exception
968
- */
969
- public function processImageQueue($logFileName)
970
- {
971
- // download & set product images
972
- $queueList = $this->getImageQueue();
973
- if (count($queueList) > 0) {
974
- $mediaDir = Mage::getBaseDir('media');
975
- foreach ($queueList as $queue) {
976
- $queueId = $queue['queue_id'];
977
- $productId = $queue['entity_id'];
978
- $imageUrl = $queue['image_url'];
979
-
980
- $preImageName = explode('/', $imageUrl);
981
- $imageName = array_pop($preImageName);
982
- if (file_exists($mediaDir . DS . $imageName)) {
983
- $imageName = rand() . '_' . time() . $imageName;
984
- }
985
-
986
- if (file_put_contents($mediaDir . DS . $imageName, file_get_contents($imageUrl))) {
987
- $product = Mage::getModel('catalog/product')->load($productId);
988
- $product->addImageToMediaGallery($mediaDir . DS . $imageName,
989
- array('image', 'small_image', 'thumbnail'),
990
- true, true
991
- );
992
- $product->save();
993
- $this->setImageAsDownloaded($queueId);
994
- unset($product);
995
- } else {
996
- $this->setImageAsDownloadedError($queueId);
997
- Mage::log('Unable download file to ' . $productId, $logFileName);
998
- continue;
999
- }
1000
- }
1001
- }
1002
- }
1003
-
1004
- /**
1005
- * @param bool $queueId
1006
- */
1007
- private function setImageAsDownloaded($queueId = false)
1008
- {
1009
- if ($queueId) {
1010
- $this->_connRes->query(
1011
- "UPDATE `{$this->_tablePrefix}iceshop_iceimport_image_queue`
1012
- SET is_downloaded = 1
1013
- WHERE queue_id = :queue_id",
1014
- array(':queue_id' => $queueId)
1015
- );
1016
- }
1017
- }
1018
-
1019
- /**
1020
- * @param bool $queueId
1021
- */
1022
- private function setImageAsDownloadedError($queueId = false)
1023
- {
1024
- if ($queueId) {
1025
- $this->_connRes->query(
1026
- "UPDATE `{$this->_tablePrefix}iceshop_iceimport_image_queue`
1027
- SET is_downloaded = 2
1028
- WHERE queue_id = :queue_id",
1029
- array(':queue_id' => $queueId)
1030
- );
1031
- }
1032
- }
1033
-
1034
- /**
1035
- * @param $categories
1036
- * @param $storeId
1037
- * @param $unspsc
1038