PageRenderTime 32ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/ImportExport/Model/Import/Entity/Product/Type/Abstract.php

https://bitbucket.org/jokusafet/magento2
PHP | 324 lines | 142 code | 27 blank | 155 comment | 27 complexity | ac2f9bf8437d7a0850e3a3ac84babfd1 MD5 | raw file
  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_ImportExport
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Import entity abstract product type model
  28. *
  29. * @category Mage
  30. * @package Mage_ImportExport
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract
  34. {
  35. /**
  36. * Product type attribute sets and attributes parameters.
  37. *
  38. * [attr_set_name_1] => array(
  39. * [attr_code_1] => array(
  40. * 'options' => array(),
  41. * 'type' => 'text', 'price', 'textarea', 'select', etc.
  42. * 'id' => ..
  43. * ),
  44. * ...
  45. * ),
  46. * ...
  47. *
  48. * @var array
  49. */
  50. protected $_attributes = array();
  51. /**
  52. * Attributes' codes which will be allowed anyway, independently from its visibility property.
  53. *
  54. * @var array
  55. */
  56. protected $_forcedAttributesCodes = array();
  57. /**
  58. * Attributes with index (not label) value.
  59. *
  60. * @var array
  61. */
  62. protected $_indexValueAttributes = array();
  63. /**
  64. * Validation failure message template definitions
  65. *
  66. * @var array
  67. */
  68. protected $_messageTemplates = array();
  69. /**
  70. * Column names that holds values with particular meaning.
  71. *
  72. * @var array
  73. */
  74. protected $_specialAttributes = array();
  75. /**
  76. * Product entity object.
  77. *
  78. * @var Mage_ImportExport_Model_Import_Entity_Product
  79. */
  80. protected $_entityModel;
  81. /**
  82. * Product type (simple, configurable, etc.).
  83. *
  84. * @var string
  85. */
  86. protected $_type;
  87. /**
  88. * Object constructor.
  89. *
  90. * @param array $params
  91. * @param string $type Product type (simple, configurable, etc.)
  92. * @throws Exception
  93. * @return void
  94. */
  95. final public function __construct(array $params)
  96. {
  97. if ($this->isSuitable()) {
  98. if (!isset($params[0]) || !isset($params[1])
  99. || !is_object($params[0]) || !($params[0] instanceof Mage_ImportExport_Model_Import_Entity_Product)) {
  100. Mage::throwException(Mage::helper('Mage_ImportExport_Helper_Data')->__('Invalid parameters'));
  101. }
  102. $this->_entityModel = $params[0];
  103. $this->_type = $params[1];
  104. foreach ($this->_messageTemplates as $errorCode => $message) {
  105. $this->_entityModel->addMessageTemplate($errorCode, $message);
  106. }
  107. $this->_initAttributes();
  108. }
  109. }
  110. /**
  111. * Add attribute parameters to appropriate attribute set.
  112. *
  113. * @param string $attrSetName Name of attribute set.
  114. * @param array $attrParams Refined attribute parameters.
  115. * @return Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract
  116. */
  117. protected function _addAttributeParams($attrSetName, array $attrParams)
  118. {
  119. if (!$attrParams['apply_to'] || in_array($this->_type, $attrParams['apply_to'])) {
  120. $this->_attributes[$attrSetName][$attrParams['code']] = $attrParams;
  121. }
  122. return $this;
  123. }
  124. /**
  125. * Return product attributes for its attribute set specified in row data.
  126. *
  127. * @param array|string $attrSetData Product row data or simply attribute set name.
  128. * @return array
  129. */
  130. protected function _getProductAttributes($attrSetData)
  131. {
  132. if (is_array($attrSetData)) {
  133. return $this->_attributes[$attrSetData[Mage_ImportExport_Model_Import_Entity_Product::COL_ATTR_SET]];
  134. } else {
  135. return $this->_attributes[$attrSetData];
  136. }
  137. }
  138. /**
  139. * Initialize attributes parameters for all attributes' sets.
  140. *
  141. * @return Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract
  142. */
  143. protected function _initAttributes()
  144. {
  145. // temporary storage for attributes' parameters to avoid double querying inside the loop
  146. $attributesCache = array();
  147. foreach (Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection')
  148. ->setEntityTypeFilter($this->_entityModel->getEntityTypeId()) as $attributeSet) {
  149. foreach (Mage::getResourceModel('Mage_Catalog_Model_Resource_Product_Attribute_Collection')
  150. ->setAttributeSetFilter($attributeSet->getId()) as $attribute) {
  151. $attributeCode = $attribute->getAttributeCode();
  152. $attributeId = $attribute->getId();
  153. if ($attribute->getIsVisible() || in_array($attributeCode, $this->_forcedAttributesCodes)) {
  154. if (!isset($attributesCache[$attributeId])) {
  155. $attributesCache[$attributeId] = array(
  156. 'id' => $attributeId,
  157. 'code' => $attributeCode,
  158. 'for_configurable' => $attribute->getIsConfigurable(),
  159. 'is_global' => $attribute->getIsGlobal(),
  160. 'is_required' => $attribute->getIsRequired(),
  161. 'is_unique' => $attribute->getIsUnique(),
  162. 'frontend_label' => $attribute->getFrontendLabel(),
  163. 'is_static' => $attribute->isStatic(),
  164. 'apply_to' => $attribute->getApplyTo(),
  165. 'type' => Mage_ImportExport_Model_Import::getAttributeType($attribute),
  166. 'default_value' => strlen($attribute->getDefaultValue())
  167. ? $attribute->getDefaultValue() : null,
  168. 'options' => $this->_entityModel
  169. ->getAttributeOptions($attribute, $this->_indexValueAttributes)
  170. );
  171. }
  172. $this->_addAttributeParams($attributeSet->getAttributeSetName(), $attributesCache[$attributeId]);
  173. }
  174. }
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Have we check attribute for is_required? Used as last chance to disable this type of check.
  180. *
  181. * @param string $attrCode
  182. * @return bool
  183. */
  184. protected function _isAttributeRequiredCheckNeeded($attrCode)
  185. {
  186. return true;
  187. }
  188. /**
  189. * Validate particular attributes columns.
  190. *
  191. * @param array $rowData
  192. * @param int $rowNum
  193. * @return bool
  194. */
  195. protected function _isParticularAttributesValid(array $rowData, $rowNum)
  196. {
  197. return true;
  198. }
  199. /**
  200. * Check price correction value validity (signed integer or float with or without percentage sign).
  201. *
  202. * @param string $value
  203. * @return int
  204. */
  205. protected function _isPriceCorr($value)
  206. {
  207. return preg_match('/^-?\d+\.?\d*%?$/', $value);
  208. }
  209. /**
  210. * Particular attribute names getter.
  211. *
  212. * @return array
  213. */
  214. public function getParticularAttributes()
  215. {
  216. return $this->_specialAttributes;
  217. }
  218. /**
  219. * Validate row attributes. Pass VALID row data ONLY as argument.
  220. *
  221. * @param array $rowData
  222. * @param int $rowNum
  223. * @param boolean $checkRequiredAttributes OPTIONAL Flag which can disable validation required values.
  224. * @return boolean
  225. */
  226. public function isRowValid(array $rowData, $rowNum, $checkRequiredAttributes = true)
  227. {
  228. $error = false;
  229. $rowScope = $this->_entityModel->getRowScope($rowData);
  230. if (Mage_ImportExport_Model_Import_Entity_Product::SCOPE_NULL != $rowScope) {
  231. foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
  232. // check value for non-empty in the case of required attribute?
  233. if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
  234. $error |= !$this->_entityModel->isAttributeValid($attrCode, $attrParams, $rowData, $rowNum);
  235. } elseif (
  236. $this->_isAttributeRequiredCheckNeeded($attrCode)
  237. && $checkRequiredAttributes
  238. && Mage_ImportExport_Model_Import_Entity_Product::SCOPE_DEFAULT == $rowScope
  239. && $attrParams['is_required']
  240. ) {
  241. $this->_entityModel->addRowError(
  242. Mage_ImportExport_Model_Import_Entity_Product::ERROR_VALUE_IS_REQUIRED, $rowNum, $attrCode
  243. );
  244. $error = true;
  245. }
  246. }
  247. }
  248. $error |= !$this->_isParticularAttributesValid($rowData, $rowNum);
  249. return !$error;
  250. }
  251. /**
  252. * Additional check for model availability. If method returns FALSE - model is not suitable for data processing.
  253. *
  254. * @return bool
  255. */
  256. public function isSuitable()
  257. {
  258. return true;
  259. }
  260. /**
  261. * Prepare attributes values for save: exclude non-existent, static or with empty values attributes;
  262. * set default values if needed
  263. *
  264. * @param array $rowData
  265. * @param bool $withDefaultValue
  266. *
  267. * @return array
  268. */
  269. public function prepareAttributesWithDefaultValueForSave(array $rowData, $withDefaultValue = true)
  270. {
  271. $resultAttrs = array();
  272. foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
  273. if (!$attrParams['is_static']) {
  274. if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
  275. $resultAttrs[$attrCode] =
  276. ('select' == $attrParams['type'] || 'multiselect' == $attrParams['type'])
  277. ? $attrParams['options'][strtolower($rowData[$attrCode])]
  278. : $rowData[$attrCode];
  279. } elseif (array_key_exists($attrCode, $rowData)) {
  280. $resultAttrs[$attrCode] = $rowData[$attrCode];
  281. } elseif ($withDefaultValue && null !== $attrParams['default_value']) {
  282. $resultAttrs[$attrCode] = $attrParams['default_value'];
  283. }
  284. }
  285. }
  286. return $resultAttrs;
  287. }
  288. /**
  289. * Save product type specific data.
  290. *
  291. * @return Mage_ImportExport_Model_Import_Entity_Product_Type_Abstract
  292. */
  293. public function saveData()
  294. {
  295. return $this;
  296. }
  297. }