PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 801 lines | 546 code | 94 blank | 161 comment | 95 complexity | 6bde20a5d15591bef8508d675bf2f060 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Catalog_Model_Convert_Adapter_Product
  27. extends Mage_Eav_Model_Convert_Adapter_Entity
  28. {
  29. const MULTI_DELIMITER = ' , ';
  30. const ENTITY = 'catalog_product_import';
  31. /**
  32. * Product model
  33. *
  34. * @var Mage_Catalog_Model_Product
  35. */
  36. protected $_productModel;
  37. /**
  38. * product types collection array
  39. *
  40. * @var array
  41. */
  42. protected $_productTypes;
  43. /**
  44. * Product Type Instances singletons
  45. *
  46. * @var array
  47. */
  48. protected $_productTypeInstances = array();
  49. /**
  50. * product attribute set collection array
  51. *
  52. * @var array
  53. */
  54. protected $_productAttributeSets;
  55. protected $_stores;
  56. protected $_attributes = array();
  57. protected $_configs = array();
  58. protected $_requiredFields = array();
  59. protected $_ignoreFields = array();
  60. /**
  61. * @deprecated after 1.5.0.0-alpha2
  62. *
  63. * @var array
  64. */
  65. protected $_imageFields = array();
  66. /**
  67. * Inventory Fields array
  68. *
  69. * @var array
  70. */
  71. protected $_inventoryFields = array();
  72. /**
  73. * Inventory Fields by product Types
  74. *
  75. * @var array
  76. */
  77. protected $_inventoryFieldsProductTypes = array();
  78. protected $_toNumber = array();
  79. /**
  80. * Load product collection Id(s)
  81. *
  82. */
  83. public function load()
  84. {
  85. $attrFilterArray = array();
  86. $attrFilterArray ['name'] = 'like';
  87. $attrFilterArray ['sku'] = 'startsWith';
  88. $attrFilterArray ['type'] = 'eq';
  89. $attrFilterArray ['attribute_set'] = 'eq';
  90. $attrFilterArray ['visibility'] = 'eq';
  91. $attrFilterArray ['status'] = 'eq';
  92. $attrFilterArray ['price'] = 'fromTo';
  93. $attrFilterArray ['qty'] = 'fromTo';
  94. $attrFilterArray ['store_id'] = 'eq';
  95. $attrToDb = array(
  96. 'type' => 'type_id',
  97. 'attribute_set' => 'attribute_set_id'
  98. );
  99. $filters = $this->_parseVars();
  100. if ($qty = $this->getFieldValue($filters, 'qty')) {
  101. $qtyFrom = isset($qty['from']) ? (float) $qty['from'] : 0;
  102. $qtyTo = isset($qty['to']) ? (float) $qty['to'] : 0;
  103. $qtyAttr = array();
  104. $qtyAttr['alias'] = 'qty';
  105. $qtyAttr['attribute'] = 'cataloginventory/stock_item';
  106. $qtyAttr['field'] = 'qty';
  107. $qtyAttr['bind'] = 'product_id=entity_id';
  108. $qtyAttr['cond'] = "{{table}}.qty between '{$qtyFrom}' AND '{$qtyTo}'";
  109. $qtyAttr['joinType'] = 'inner';
  110. $this->setJoinField($qtyAttr);
  111. }
  112. parent::setFilter($attrFilterArray, $attrToDb);
  113. if ($price = $this->getFieldValue($filters, 'price')) {
  114. $this->_filter[] = array(
  115. 'attribute' => 'price',
  116. 'from' => $price['from'],
  117. 'to' => $price['to']
  118. );
  119. $this->setJoinAttr(array(
  120. 'alias' => 'price',
  121. 'attribute' => 'catalog_product/price',
  122. 'bind' => 'entity_id',
  123. 'joinType' => 'LEFT'
  124. ));
  125. }
  126. return parent::load();
  127. }
  128. /**
  129. * Retrieve product model cache
  130. *
  131. * @return Mage_Catalog_Model_Product
  132. */
  133. public function getProductModel()
  134. {
  135. if (is_null($this->_productModel)) {
  136. $productModel = Mage::getModel('catalog/product');
  137. $this->_productModel = Mage::objects()->save($productModel);
  138. }
  139. return Mage::objects()->load($this->_productModel);
  140. }
  141. /**
  142. * Retrieve eav entity attribute model
  143. *
  144. * @param string $code
  145. * @return Mage_Eav_Model_Entity_Attribute
  146. */
  147. public function getAttribute($code)
  148. {
  149. if (!isset($this->_attributes[$code])) {
  150. $this->_attributes[$code] = $this->getProductModel()->getResource()->getAttribute($code);
  151. }
  152. if ($this->_attributes[$code] instanceof Mage_Catalog_Model_Resource_Eav_Attribute) {
  153. $applyTo = $this->_attributes[$code]->getApplyTo();
  154. if ($applyTo && !in_array($this->getProductModel()->getTypeId(), $applyTo)) {
  155. return false;
  156. }
  157. }
  158. return $this->_attributes[$code];
  159. }
  160. /**
  161. * Retrieve product type collection array
  162. *
  163. * @return array
  164. */
  165. public function getProductTypes()
  166. {
  167. if (is_null($this->_productTypes)) {
  168. $this->_productTypes = array();
  169. $options = Mage::getModel('catalog/product_type')
  170. ->getOptionArray();
  171. foreach ($options as $k => $v) {
  172. $this->_productTypes[$k] = $k;
  173. }
  174. }
  175. return $this->_productTypes;
  176. }
  177. /**
  178. * ReDefine Product Type Instance to Product
  179. *
  180. * @param Mage_Catalog_Model_Product $product
  181. * @return Mage_Catalog_Model_Convert_Adapter_Product
  182. */
  183. public function setProductTypeInstance(Mage_Catalog_Model_Product $product)
  184. {
  185. $type = $product->getTypeId();
  186. if (!isset($this->_productTypeInstances[$type])) {
  187. $this->_productTypeInstances[$type] = Mage::getSingleton('catalog/product_type')
  188. ->factory($product, true);
  189. }
  190. $product->setTypeInstance($this->_productTypeInstances[$type], true);
  191. return $this;
  192. }
  193. /**
  194. * Retrieve product attribute set collection array
  195. *
  196. * @return array
  197. */
  198. public function getProductAttributeSets()
  199. {
  200. if (is_null($this->_productAttributeSets)) {
  201. $this->_productAttributeSets = array();
  202. $entityTypeId = Mage::getModel('eav/entity')
  203. ->setType('catalog_product')
  204. ->getTypeId();
  205. $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
  206. ->setEntityTypeFilter($entityTypeId);
  207. foreach ($collection as $set) {
  208. $this->_productAttributeSets[$set->getAttributeSetName()] = $set->getId();
  209. }
  210. }
  211. return $this->_productAttributeSets;
  212. }
  213. /**
  214. * Init stores
  215. */
  216. protected function _initStores ()
  217. {
  218. if (is_null($this->_stores)) {
  219. $this->_stores = Mage::app()->getStores(true, true);
  220. foreach ($this->_stores as $code => $store) {
  221. $this->_storesIdCode[$store->getId()] = $code;
  222. }
  223. }
  224. }
  225. /**
  226. * Retrieve store object by code
  227. *
  228. * @param string $store
  229. * @return Mage_Core_Model_Store
  230. */
  231. public function getStoreByCode($store)
  232. {
  233. $this->_initStores();
  234. /**
  235. * In single store mode all data should be saved as default
  236. */
  237. if (Mage::app()->isSingleStoreMode()) {
  238. return Mage::app()->getStore(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
  239. }
  240. if (isset($this->_stores[$store])) {
  241. return $this->_stores[$store];
  242. }
  243. return false;
  244. }
  245. /**
  246. * Retrieve store object by code
  247. *
  248. * @param string $store
  249. * @return Mage_Core_Model_Store
  250. */
  251. public function getStoreById($id)
  252. {
  253. $this->_initStores();
  254. /**
  255. * In single store mode all data should be saved as default
  256. */
  257. if (Mage::app()->isSingleStoreMode()) {
  258. return Mage::app()->getStore(Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID);
  259. }
  260. if (isset($this->_storesIdCode[$id])) {
  261. return $this->getStoreByCode($this->_storesIdCode[$id]);
  262. }
  263. return false;
  264. }
  265. public function parse()
  266. {
  267. $batchModel = Mage::getSingleton('dataflow/batch');
  268. /* @var $batchModel Mage_Dataflow_Model_Batch */
  269. $batchImportModel = $batchModel->getBatchImportModel();
  270. $importIds = $batchImportModel->getIdCollection();
  271. foreach ($importIds as $importId) {
  272. //print '<pre>'.memory_get_usage().'</pre>';
  273. $batchImportModel->load($importId);
  274. $importData = $batchImportModel->getBatchData();
  275. $this->saveRow($importData);
  276. }
  277. }
  278. protected $_productId = '';
  279. /**
  280. * Initialize convert adapter model for products collection
  281. *
  282. */
  283. public function __construct()
  284. {
  285. $fieldset = Mage::getConfig()->getFieldset('catalog_product_dataflow', 'admin');
  286. foreach ($fieldset as $code => $node) {
  287. /* @var $node Mage_Core_Model_Config_Element */
  288. if ($node->is('inventory')) {
  289. foreach ($node->product_type->children() as $productType) {
  290. $productType = $productType->getName();
  291. $this->_inventoryFieldsProductTypes[$productType][] = $code;
  292. if ($node->is('use_config')) {
  293. $this->_inventoryFieldsProductTypes[$productType][] = 'use_config_' . $code;
  294. }
  295. }
  296. $this->_inventoryFields[] = $code;
  297. if ($node->is('use_config')) {
  298. $this->_inventoryFields[] = 'use_config_'.$code;
  299. }
  300. }
  301. if ($node->is('required')) {
  302. $this->_requiredFields[] = $code;
  303. }
  304. if ($node->is('ignore')) {
  305. $this->_ignoreFields[] = $code;
  306. }
  307. if ($node->is('to_number')) {
  308. $this->_toNumber[] = $code;
  309. }
  310. }
  311. $this->setVar('entity_type', 'catalog/product');
  312. if (!Mage::registry('Object_Cache_Product')) {
  313. $this->setProduct(Mage::getModel('catalog/product'));
  314. }
  315. if (!Mage::registry('Object_Cache_StockItem')) {
  316. $this->setStockItem(Mage::getModel('cataloginventory/stock_item'));
  317. }
  318. }
  319. /**
  320. * Retrieve not loaded collection
  321. *
  322. * @param string $entityType
  323. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
  324. */
  325. protected function _getCollectionForLoad($entityType)
  326. {
  327. $collection = parent::_getCollectionForLoad($entityType)
  328. ->setStoreId($this->getStoreId())
  329. ->addStoreFilter($this->getStoreId());
  330. return $collection;
  331. }
  332. public function setProduct(Mage_Catalog_Model_Product $object)
  333. {
  334. $id = Mage::objects()->save($object);
  335. //$this->_product = $object;
  336. Mage::register('Object_Cache_Product', $id);
  337. }
  338. public function getProduct()
  339. {
  340. return Mage::objects()->load(Mage::registry('Object_Cache_Product'));
  341. }
  342. public function setStockItem(Mage_CatalogInventory_Model_Stock_Item $object)
  343. {
  344. $id = Mage::objects()->save($object);
  345. //$this->_product = $object;
  346. Mage::register('Object_Cache_StockItem', $id);
  347. //$this->_stockItem = $object;
  348. }
  349. public function getStockItem()
  350. {
  351. return Mage::objects()->load(Mage::registry('Object_Cache_StockItem'));
  352. //return $this->_stockItem;
  353. }
  354. public function save()
  355. {
  356. $stores = array();
  357. foreach (Mage::getConfig()->getNode('stores')->children() as $storeNode) {
  358. $stores[(int)$storeNode->system->store->id] = $storeNode->getName();
  359. }
  360. $collections = $this->getData();
  361. if ($collections instanceof Mage_Catalog_Model_Entity_Product_Collection) {
  362. $collections = array($collections->getEntity()->getStoreId()=>$collections);
  363. } elseif (!is_array($collections)) {
  364. $this->addException(
  365. Mage::helper('catalog')->__('No product collections found.'),
  366. Mage_Dataflow_Model_Convert_Exception::FATAL
  367. );
  368. }
  369. //$stockItems = $this->getInventoryItems();
  370. $stockItems = Mage::registry('current_imported_inventory');
  371. if ($collections) foreach ($collections as $storeId=>$collection) {
  372. $this->addException(Mage::helper('catalog')->__('Records for "'.$stores[$storeId].'" store found.'));
  373. if (!$collection instanceof Mage_Catalog_Model_Entity_Product_Collection) {
  374. $this->addException(
  375. Mage::helper('catalog')->__('Product collection expected.'),
  376. Mage_Dataflow_Model_Convert_Exception::FATAL
  377. );
  378. }
  379. try {
  380. $i = 0;
  381. foreach ($collection->getIterator() as $model) {
  382. $new = false;
  383. // if product is new, create default values first
  384. if (!$model->getId()) {
  385. $new = true;
  386. $model->save();
  387. // if new product and then store is not default
  388. // we duplicate product as default product with store_id -
  389. if (0 !== $storeId ) {
  390. $data = $model->getData();
  391. $default = Mage::getModel('catalog/product');
  392. $default->setData($data);
  393. $default->setStoreId(0);
  394. $default->save();
  395. unset($default);
  396. } // end
  397. #Mage::getResourceSingleton('catalog_entity/convert')->addProductToStore($model->getId(), 0);
  398. }
  399. if (!$new || 0!==$storeId) {
  400. if (0!==$storeId) {
  401. Mage::getResourceSingleton('catalog_entity/convert')->addProductToStore(
  402. $model->getId(),
  403. $storeId
  404. );
  405. }
  406. $model->save();
  407. }
  408. if (isset($stockItems[$model->getSku()]) && $stock = $stockItems[$model->getSku()]) {
  409. $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($model->getId());
  410. $stockItemId = $stockItem->getId();
  411. if (!$stockItemId) {
  412. $stockItem->setData('product_id', $model->getId());
  413. $stockItem->setData('stock_id', 1);
  414. $data = array();
  415. } else {
  416. $data = $stockItem->getData();
  417. }
  418. foreach($stock as $field => $value) {
  419. if (!$stockItemId) {
  420. if (in_array($field, $this->_configs)) {
  421. $stockItem->setData('use_config_'.$field, 0);
  422. }
  423. $stockItem->setData($field, $value?$value:0);
  424. } else {
  425. if (in_array($field, $this->_configs)) {
  426. if ($data['use_config_'.$field] == 0) {
  427. $stockItem->setData($field, $value?$value:0);
  428. }
  429. } else {
  430. $stockItem->setData($field, $value?$value:0);
  431. }
  432. }
  433. }
  434. $stockItem->save();
  435. unset($data);
  436. unset($stockItem);
  437. unset($stockItemId);
  438. }
  439. unset($model);
  440. $i++;
  441. }
  442. $this->addException(Mage::helper('catalog')->__("Saved %d record(s)", $i));
  443. } catch (Exception $e) {
  444. if (!$e instanceof Mage_Dataflow_Model_Convert_Exception) {
  445. $this->addException(
  446. Mage::helper('catalog')->__(
  447. 'An error occurred while saving the collection, aborting. Error message: %s',
  448. $e->getMessage()
  449. ),
  450. Mage_Dataflow_Model_Convert_Exception::FATAL
  451. );
  452. }
  453. }
  454. }
  455. //unset(Zend::unregister('imported_stock_item'));
  456. unset($collections);
  457. return $this;
  458. }
  459. /**
  460. * Save product (import)
  461. *
  462. * @param array $importData
  463. * @throws Mage_Core_Exception
  464. * @return bool
  465. */
  466. public function saveRow(array $importData)
  467. {
  468. $product = $this->getProductModel()
  469. ->reset();
  470. if (empty($importData['store'])) {
  471. if (!is_null($this->getBatchParams('store'))) {
  472. $store = $this->getStoreById($this->getBatchParams('store'));
  473. } else {
  474. $message = Mage::helper('catalog')->__(
  475. 'Skipping import row, required field "%s" is not defined.',
  476. 'store'
  477. );
  478. Mage::throwException($message);
  479. }
  480. }
  481. else {
  482. $store = $this->getStoreByCode($importData['store']);
  483. }
  484. if ($store === false) {
  485. $message = Mage::helper('catalog')->__(
  486. 'Skipping import row, store "%s" field does not exist.',
  487. $importData['store']
  488. );
  489. Mage::throwException($message);
  490. }
  491. if (empty($importData['sku'])) {
  492. $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'sku');
  493. Mage::throwException($message);
  494. }
  495. $product->setStoreId($store->getId());
  496. $productId = $product->getIdBySku($importData['sku']);
  497. if ($productId) {
  498. $product->load($productId);
  499. }
  500. else {
  501. $productTypes = $this->getProductTypes();
  502. $productAttributeSets = $this->getProductAttributeSets();
  503. /**
  504. * Check product define type
  505. */
  506. if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) {
  507. $value = isset($importData['type']) ? $importData['type'] : '';
  508. $message = Mage::helper('catalog')->__(
  509. 'Skip import row, is not valid value "%s" for field "%s"',
  510. $value,
  511. 'type'
  512. );
  513. Mage::throwException($message);
  514. }
  515. $product->setTypeId($productTypes[strtolower($importData['type'])]);
  516. /**
  517. * Check product define attribute set
  518. */
  519. if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) {
  520. $value = isset($importData['attribute_set']) ? $importData['attribute_set'] : '';
  521. $message = Mage::helper('catalog')->__(
  522. 'Skip import row, the value "%s" is invalid for field "%s"',
  523. $value,
  524. 'attribute_set'
  525. );
  526. Mage::throwException($message);
  527. }
  528. $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]);
  529. foreach ($this->_requiredFields as $field) {
  530. $attribute = $this->getAttribute($field);
  531. if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
  532. $message = Mage::helper('catalog')->__(
  533. 'Skipping import row, required field "%s" for new products is not defined.',
  534. $field
  535. );
  536. Mage::throwException($message);
  537. }
  538. }
  539. }
  540. $this->setProductTypeInstance($product);
  541. if (isset($importData['category_ids'])) {
  542. $product->setCategoryIds($importData['category_ids']);
  543. }
  544. foreach ($this->_ignoreFields as $field) {
  545. if (isset($importData[$field])) {
  546. unset($importData[$field]);
  547. }
  548. }
  549. if ($store->getId() != 0) {
  550. $websiteIds = $product->getWebsiteIds();
  551. if (!is_array($websiteIds)) {
  552. $websiteIds = array();
  553. }
  554. if (!in_array($store->getWebsiteId(), $websiteIds)) {
  555. $websiteIds[] = $store->getWebsiteId();
  556. }
  557. $product->setWebsiteIds($websiteIds);
  558. }
  559. if (isset($importData['websites'])) {
  560. $websiteIds = $product->getWebsiteIds();
  561. if (!is_array($websiteIds) || !$store->getId()) {
  562. $websiteIds = array();
  563. }
  564. $websiteCodes = explode(',', $importData['websites']);
  565. foreach ($websiteCodes as $websiteCode) {
  566. try {
  567. $website = Mage::app()->getWebsite(trim($websiteCode));
  568. if (!in_array($website->getId(), $websiteIds)) {
  569. $websiteIds[] = $website->getId();
  570. }
  571. }
  572. catch (Exception $e) {}
  573. }
  574. $product->setWebsiteIds($websiteIds);
  575. unset($websiteIds);
  576. }
  577. foreach ($importData as $field => $value) {
  578. if (in_array($field, $this->_inventoryFields)) {
  579. continue;
  580. }
  581. if (is_null($value)) {
  582. continue;
  583. }
  584. $attribute = $this->getAttribute($field);
  585. if (!$attribute) {
  586. continue;
  587. }
  588. $isArray = false;
  589. $setValue = $value;
  590. if ($attribute->getFrontendInput() == 'multiselect') {
  591. $value = explode(self::MULTI_DELIMITER, $value);
  592. $isArray = true;
  593. $setValue = array();
  594. }
  595. if ($value && $attribute->getBackendType() == 'decimal') {
  596. $setValue = $this->getNumber($value);
  597. }
  598. if ($attribute->usesSource()) {
  599. $options = $attribute->getSource()->getAllOptions(false);
  600. if ($isArray) {
  601. foreach ($options as $item) {
  602. if (in_array($item['label'], $value)) {
  603. $setValue[] = $item['value'];
  604. }
  605. }
  606. } else {
  607. $setValue = false;
  608. foreach ($options as $item) {
  609. if ($item['label'] == $value) {
  610. $setValue = $item['value'];
  611. }
  612. }
  613. }
  614. }
  615. $product->setData($field, $setValue);
  616. }
  617. if (!$product->getVisibility()) {
  618. $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
  619. }
  620. $stockData = array();
  621. $inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()])
  622. ? $this->_inventoryFieldsProductTypes[$product->getTypeId()]
  623. : array();
  624. foreach ($inventoryFields as $field) {
  625. if (isset($importData[$field])) {
  626. if (in_array($field, $this->_toNumber)) {
  627. $stockData[$field] = $this->getNumber($importData[$field]);
  628. }
  629. else {
  630. $stockData[$field] = $importData[$field];
  631. }
  632. }
  633. }
  634. $product->setStockData($stockData);
  635. $mediaGalleryBackendModel = $this->getAttribute('media_gallery')->getBackend();
  636. $arrayToMassAdd = array();
  637. foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
  638. if (isset($importData[$mediaAttributeCode])) {
  639. $file = trim($importData[$mediaAttributeCode]);
  640. if (!empty($file) && !$mediaGalleryBackendModel->getImage($product, $file)) {
  641. $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
  642. }
  643. }
  644. }
  645. $addedFilesCorrespondence = $mediaGalleryBackendModel->addImagesWithDifferentMediaAttributes(
  646. $product,
  647. $arrayToMassAdd, Mage::getBaseDir('media') . DS . 'import',
  648. false,
  649. false
  650. );
  651. foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
  652. $addedFile = '';
  653. if (isset($importData[$mediaAttributeCode . '_label'])) {
  654. $fileLabel = trim($importData[$mediaAttributeCode . '_label']);
  655. if (isset($importData[$mediaAttributeCode])) {
  656. $keyInAddedFile = array_search($importData[$mediaAttributeCode],
  657. $addedFilesCorrespondence['alreadyAddedFiles']);
  658. if ($keyInAddedFile !== false) {
  659. $addedFile = $addedFilesCorrespondence['alreadyAddedFilesNames'][$keyInAddedFile];
  660. }
  661. }
  662. if (!$addedFile) {
  663. $addedFile = $product->getData($mediaAttributeCode);
  664. }
  665. if ($fileLabel && $addedFile) {
  666. $mediaGalleryBackendModel->updateImage($product, $addedFile, array('label' => $fileLabel));
  667. }
  668. }
  669. }
  670. $product->setIsMassupdate(true);
  671. $product->setExcludeUrlRewrite(true);
  672. $product->save();
  673. return true;
  674. }
  675. /**
  676. * Silently save product (import)
  677. *
  678. * @param array $
  679. * @return bool
  680. */
  681. public function saveRowSilently(array $importData)
  682. {
  683. try {
  684. $result = $this->saveRow($importData);
  685. return $result;
  686. }
  687. catch (Exception $e) {
  688. return false;
  689. }
  690. }
  691. /**
  692. * Process after import data
  693. * Init indexing process after catalog product import
  694. *
  695. */
  696. public function finish()
  697. {
  698. /**
  699. * Back compatibility event
  700. */
  701. Mage::dispatchEvent('catalog_product_import_after', array());
  702. $entity = new Varien_Object();
  703. Mage::getSingleton('index/indexer')->processEntityAction(
  704. $entity, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
  705. );
  706. }
  707. }