PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/FiveDigital/magento2
PHP | 394 lines | 248 code | 45 blank | 101 comment | 40 complexity | 24994b5feff21e253493d31ecb1ad4e5 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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) 2012 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_Parser_Product
  27. extends Mage_Eav_Model_Convert_Parser_Abstract
  28. {
  29. const MULTI_DELIMITER = ' , ';
  30. protected $_resource;
  31. /**
  32. * Product collections per store
  33. *
  34. * @var array
  35. */
  36. protected $_collections;
  37. /**
  38. * Product Type Instances object cache
  39. *
  40. * @var array
  41. */
  42. protected $_productTypeInstances = array();
  43. /**
  44. * Product Type cache
  45. *
  46. * @var array
  47. */
  48. protected $_productTypes;
  49. protected $_inventoryFields = array();
  50. protected $_imageFields = array();
  51. protected $_systemFields = array();
  52. protected $_internalFields = array();
  53. protected $_externalFields = array();
  54. protected $_inventoryItems = array();
  55. protected $_productModel;
  56. protected $_setInstances = array();
  57. protected $_store;
  58. protected $_storeId;
  59. protected $_attributes = array();
  60. public function __construct()
  61. {
  62. foreach (Mage::getConfig()->getFieldset('catalog_product_dataflow', 'admin') as $code=>$node) {
  63. if ($node->is('inventory')) {
  64. $this->_inventoryFields[] = $code;
  65. if ($node->is('use_config')) {
  66. $this->_inventoryFields[] = 'use_config_'.$code;
  67. }
  68. }
  69. if ($node->is('internal')) {
  70. $this->_internalFields[] = $code;
  71. }
  72. if ($node->is('system')) {
  73. $this->_systemFields[] = $code;
  74. }
  75. if ($node->is('external')) {
  76. $this->_externalFields[$code] = $code;
  77. }
  78. if ($node->is('img')) {
  79. $this->_imageFields[] = $code;
  80. }
  81. }
  82. }
  83. /**
  84. * @return Mage_Catalog_Model_Resource_Convert
  85. */
  86. public function getResource()
  87. {
  88. if (!$this->_resource) {
  89. $this->_resource = Mage::getResourceSingleton('Mage_Catalog_Model_Resource_Convert');
  90. #->loadStores()
  91. #->loadProducts()
  92. #->loadAttributeSets()
  93. #->loadAttributeOptions();
  94. }
  95. return $this->_resource;
  96. }
  97. public function getCollection($storeId)
  98. {
  99. if (!isset($this->_collections[$storeId])) {
  100. $this->_collections[$storeId] = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Collection');
  101. $this->_collections[$storeId]->getEntity()->setStore($storeId);
  102. }
  103. return $this->_collections[$storeId];
  104. }
  105. /**
  106. * Retrieve product type options
  107. *
  108. * @return array
  109. */
  110. public function getProductTypes()
  111. {
  112. if (is_null($this->_productTypes)) {
  113. $this->_productTypes = Mage::getSingleton('Mage_Catalog_Model_Product_Type')
  114. ->getOptionArray();
  115. }
  116. return $this->_productTypes;
  117. }
  118. /**
  119. * Retrieve Product type name by code
  120. *
  121. * @param string $code
  122. * @return string
  123. */
  124. public function getProductTypeName($code)
  125. {
  126. $productTypes = $this->getProductTypes();
  127. if (isset($productTypes[$code])) {
  128. return $productTypes[$code];
  129. }
  130. return false;
  131. }
  132. /**
  133. * Retrieve product type code by name
  134. *
  135. * @param string $name
  136. * @return string
  137. */
  138. public function getProductTypeId($name)
  139. {
  140. $productTypes = $this->getProductTypes();
  141. if ($code = array_search($name, $productTypes)) {
  142. return $code;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Retrieve product model cache
  148. *
  149. * @return Mage_Catalog_Model_Product
  150. */
  151. public function getProductModel()
  152. {
  153. if (is_null($this->_productModel)) {
  154. $productModel = Mage::getModel('Mage_Catalog_Model_Product');
  155. $this->_productModel = Mage::objects()->save($productModel);
  156. }
  157. return Mage::objects()->load($this->_productModel);
  158. }
  159. /**
  160. * Retrieve current store model
  161. *
  162. * @return Mage_Core_Model_Store
  163. */
  164. public function getStore()
  165. {
  166. if (is_null($this->_store)) {
  167. try {
  168. $store = Mage::app()->getStore($this->getVar('store'));
  169. } catch (Exception $e) {
  170. $this->addException(
  171. Mage::helper('Mage_Catalog_Helper_Data')->__('Invalid store specified'),
  172. Varien_Convert_Exception::FATAL
  173. );
  174. throw $e;
  175. }
  176. $this->_store = $store;
  177. }
  178. return $this->_store;
  179. }
  180. /**
  181. * Retrieve store ID
  182. *
  183. * @return int
  184. */
  185. public function getStoreId()
  186. {
  187. if (is_null($this->_storeId)) {
  188. $this->_storeId = $this->getStore()->getId();
  189. }
  190. return $this->_storeId;
  191. }
  192. /**
  193. * ReDefine Product Type Instance to Product
  194. *
  195. * @param Mage_Catalog_Model_Product $product
  196. * @return Mage_Catalog_Model_Convert_Parser_Product
  197. */
  198. public function setProductTypeInstance(Mage_Catalog_Model_Product $product)
  199. {
  200. $type = $product->getTypeId();
  201. if (!isset($this->_productTypeInstances[$type])) {
  202. $this->_productTypeInstances[$type] = Mage::getSingleton('Mage_Catalog_Model_Product_Type')
  203. ->factory($product);
  204. }
  205. $product->setTypeInstance($this->_productTypeInstances[$type]);
  206. return $this;
  207. }
  208. public function getAttributeSetInstance()
  209. {
  210. $productType = $this->getProductModel()->getType();
  211. $attributeSetId = $this->getProductModel()->getAttributeSetId();
  212. if (!isset($this->_setInstances[$productType][$attributeSetId])) {
  213. $this->_setInstances[$productType][$attributeSetId] =
  214. Mage::getSingleton('Mage_Catalog_Model_Product_Type')->factory($this->getProductModel());
  215. }
  216. return $this->_setInstances[$productType][$attributeSetId];
  217. }
  218. /**
  219. * Retrieve eav entity attribute model
  220. *
  221. * @param string $code
  222. * @return Mage_Eav_Model_Entity_Attribute
  223. */
  224. public function getAttribute($code)
  225. {
  226. if (!isset($this->_attributes[$code])) {
  227. $this->_attributes[$code] = $this->getProductModel()->getResource()->getAttribute($code);
  228. }
  229. return $this->_attributes[$code];
  230. }
  231. public function setInventoryItems($items)
  232. {
  233. $this->_inventoryItems = $items;
  234. }
  235. public function getInventoryItems()
  236. {
  237. return $this->_inventoryItems;
  238. }
  239. /**
  240. * Unparse (prepare data) loaded products
  241. *
  242. * @return Mage_Catalog_Model_Convert_Parser_Product
  243. */
  244. public function unparse()
  245. {
  246. $entityIds = $this->getData();
  247. foreach ($entityIds as $i => $entityId) {
  248. $product = $this->getProductModel()
  249. ->setStoreId($this->getStoreId())
  250. ->load($entityId);
  251. $this->setProductTypeInstance($product);
  252. /* @var $product Mage_Catalog_Model_Product */
  253. $position = Mage::helper('Mage_Catalog_Helper_Data')->__('Line %d, SKU: %s', ($i+1), $product->getSku());
  254. $this->setPosition($position);
  255. $row = array(
  256. 'store' => $this->getStore()->getCode(),
  257. 'websites' => '',
  258. 'attribute_set' => $this->getAttributeSetName($product->getEntityTypeId(),
  259. $product->getAttributeSetId()),
  260. 'type' => $product->getTypeId(),
  261. 'category_ids' => join(',', $product->getCategoryIds())
  262. );
  263. if ($this->getStore()->getCode() == Mage_Core_Model_Store::ADMIN_CODE) {
  264. $websiteCodes = array();
  265. foreach ($product->getWebsiteIds() as $websiteId) {
  266. $websiteCode = Mage::app()->getWebsite($websiteId)->getCode();
  267. $websiteCodes[$websiteCode] = $websiteCode;
  268. }
  269. $row['websites'] = join(',', $websiteCodes);
  270. } else {
  271. $row['websites'] = $this->getStore()->getWebsite()->getCode();
  272. if ($this->getVar('url_field')) {
  273. $row['url'] = $product->getProductUrl(false);
  274. }
  275. }
  276. foreach ($product->getData() as $field => $value) {
  277. if (in_array($field, $this->_systemFields) || is_object($value)) {
  278. continue;
  279. }
  280. $attribute = $this->getAttribute($field);
  281. if (!$attribute) {
  282. continue;
  283. }
  284. if ($attribute->usesSource()) {
  285. $option = $attribute->getSource()->getOptionText($value);
  286. if ($value && empty($option) && $option != '0') {
  287. $this->addException(
  288. Mage::helper('Mage_Catalog_Helper_Data')->__('Invalid option ID specified for %s (%s), skipping the record.', $field, $value),
  289. Mage_Dataflow_Model_Convert_Exception::ERROR
  290. );
  291. continue;
  292. }
  293. if (is_array($option)) {
  294. $value = join(self::MULTI_DELIMITER, $option);
  295. } else {
  296. $value = $option;
  297. }
  298. unset($option);
  299. } elseif (is_array($value)) {
  300. continue;
  301. }
  302. $row[$field] = $value;
  303. }
  304. if ($stockItem = $product->getStockItem()) {
  305. foreach ($stockItem->getData() as $field => $value) {
  306. if (in_array($field, $this->_systemFields) || is_object($value)) {
  307. continue;
  308. }
  309. $row[$field] = $value;
  310. }
  311. }
  312. foreach ($this->_imageFields as $field) {
  313. if (isset($row[$field]) && $row[$field] == 'no_selection') {
  314. $row[$field] = null;
  315. }
  316. }
  317. $batchExport = $this->getBatchExportModel()
  318. ->setId(null)
  319. ->setBatchId($this->getBatchModel()->getId())
  320. ->setBatchData($row)
  321. ->setStatus(1)
  322. ->save();
  323. $product->reset();
  324. }
  325. return $this;
  326. }
  327. /**
  328. * Retrieve accessible external product attributes
  329. *
  330. * @return array
  331. */
  332. public function getExternalAttributes()
  333. {
  334. $productAttributes = Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Attribute_Collection')
  335. ->load();
  336. $attributes = $this->_externalFields;
  337. foreach ($productAttributes as $attr) {
  338. $code = $attr->getAttributeCode();
  339. if (in_array($code, $this->_internalFields) || $attr->getFrontendInput() == 'hidden') {
  340. continue;
  341. }
  342. $attributes[$code] = $code;
  343. }
  344. foreach ($this->_inventoryFields as $field) {
  345. $attributes[$field] = $field;
  346. }
  347. return $attributes;
  348. }
  349. }