PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Catalog/Model/Resource/Eav/Attribute.php

https://github.com/rgranadino/magento-mirror
PHP | 400 lines | 178 code | 31 blank | 191 comment | 48 complexity | 16ef42b2ba4740a01feb6839fb575395 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_Catalog
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog attribute model
  28. *
  29. * @method Mage_Catalog_Model_Resource_Attribute _getResource()
  30. * @method Mage_Catalog_Model_Resource_Attribute getResource()
  31. * @method Mage_Catalog_Model_Resource_Eav_Attribute getFrontendInputRenderer()
  32. * @method string setFrontendInputRenderer(string $value)
  33. * @method int setIsGlobal(int $value)
  34. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsVisible()
  35. * @method int setIsVisible(int $value)
  36. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsSearchable()
  37. * @method int setIsSearchable(int $value)
  38. * @method Mage_Catalog_Model_Resource_Eav_Attribute getSearchWeight()
  39. * @method int setSearchWeight(int $value)
  40. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsFilterable()
  41. * @method int setIsFilterable(int $value)
  42. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsComparable()
  43. * @method int setIsComparable(int $value)
  44. * @method int setIsVisibleOnFront(int $value)
  45. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsHtmlAllowedOnFront()
  46. * @method int setIsHtmlAllowedOnFront(int $value)
  47. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsUsedForPriceRules()
  48. * @method int setIsUsedForPriceRules(int $value)
  49. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsFilterableInSearch()
  50. * @method int setIsFilterableInSearch(int $value)
  51. * @method Mage_Catalog_Model_Resource_Eav_Attribute getUsedInProductListing()
  52. * @method int setUsedInProductListing(int $value)
  53. * @method Mage_Catalog_Model_Resource_Eav_Attribute getUsedForSortBy()
  54. * @method int setUsedForSortBy(int $value)
  55. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsConfigurable()
  56. * @method int setIsConfigurable(int $value)
  57. * @method string setApplyTo(string $value)
  58. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsVisibleInAdvancedSearch()
  59. * @method int setIsVisibleInAdvancedSearch(int $value)
  60. * @method Mage_Catalog_Model_Resource_Eav_Attribute getPosition()
  61. * @method int setPosition(int $value)
  62. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsWysiwygEnabled()
  63. * @method int setIsWysiwygEnabled(int $value)
  64. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsUsedForPromoRules()
  65. * @method int setIsUsedForPromoRules(int $value)
  66. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsUsedForCustomerSegment()
  67. * @method int setIsUsedForCustomerSegment(int $value)
  68. * @method Mage_Catalog_Model_Resource_Eav_Attribute getIsUsedForTargetRules()
  69. * @method int setIsUsedForTargetRules(int $value)
  70. *
  71. * @category Mage
  72. * @package Mage_Catalog
  73. * @author Magento Core Team <core@magentocommerce.com>
  74. */
  75. class Mage_Catalog_Model_Resource_Eav_Attribute extends Mage_Eav_Model_Entity_Attribute
  76. {
  77. const SCOPE_STORE = 0;
  78. const SCOPE_GLOBAL = 1;
  79. const SCOPE_WEBSITE = 2;
  80. const MODULE_NAME = 'Mage_Catalog';
  81. const ENTITY = 'catalog_eav_attribute';
  82. /**
  83. * Event prefix
  84. *
  85. * @var string
  86. */
  87. protected $_eventPrefix = 'catalog_entity_attribute';
  88. /**
  89. * Event object name
  90. *
  91. * @var string
  92. */
  93. protected $_eventObject = 'attribute';
  94. /**
  95. * Array with labels
  96. *
  97. * @var array
  98. */
  99. static protected $_labels = null;
  100. protected function _construct()
  101. {
  102. $this->_init('catalog/attribute');
  103. }
  104. /**
  105. * Processing object before save data
  106. *
  107. * @throws Mage_Core_Exception
  108. * @return Mage_Core_Model_Abstract
  109. */
  110. protected function _beforeSave()
  111. {
  112. $this->setData('modulePrefix', self::MODULE_NAME);
  113. if (isset($this->_origData['is_global'])) {
  114. if (!isset($this->_data['is_global'])) {
  115. $this->_data['is_global'] = self::SCOPE_GLOBAL;
  116. }
  117. if (($this->_data['is_global'] != $this->_origData['is_global'])
  118. && $this->_getResource()->isUsedBySuperProducts($this)) {
  119. Mage::throwException(Mage::helper('catalog')->__('Scope must not be changed, because the attribute is used in configurable products.'));
  120. }
  121. }
  122. if ($this->getFrontendInput() == 'price') {
  123. if (!$this->getBackendModel()) {
  124. $this->setBackendModel('catalog/product_attribute_backend_price');
  125. }
  126. }
  127. if ($this->getFrontendInput() == 'textarea') {
  128. if ($this->getIsWysiwygEnabled()) {
  129. $this->setIsHtmlAllowedOnFront(1);
  130. }
  131. }
  132. return parent::_beforeSave();
  133. }
  134. /**
  135. * Processing object after save data
  136. *
  137. * @return Mage_Core_Model_Abstract
  138. */
  139. protected function _afterSave()
  140. {
  141. /**
  142. * Fix saving attribute in admin
  143. */
  144. Mage::getSingleton('eav/config')->clear();
  145. return parent::_afterSave();
  146. }
  147. /**
  148. * Init indexing process after attribute data commit
  149. *
  150. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  151. */
  152. public function afterCommitCallback()
  153. {
  154. parent::afterCommitCallback();
  155. Mage::getSingleton('index/indexer')->processEntityAction(
  156. $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
  157. );
  158. return $this;
  159. }
  160. /**
  161. * Register indexing event before delete catalog eav attribute
  162. *
  163. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  164. */
  165. protected function _beforeDelete()
  166. {
  167. if ($this->_getResource()->isUsedBySuperProducts($this)) {
  168. Mage::throwException(Mage::helper('catalog')->__('This attribute is used in configurable products.'));
  169. }
  170. Mage::getSingleton('index/indexer')->logEvent(
  171. $this, self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE
  172. );
  173. return parent::_beforeDelete();
  174. }
  175. /**
  176. * Init indexing process after catalog eav attribute delete commit
  177. *
  178. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  179. */
  180. protected function _afterDeleteCommit()
  181. {
  182. parent::_afterDeleteCommit();
  183. Mage::getSingleton('index/indexer')->indexEvents(
  184. self::ENTITY, Mage_Index_Model_Event::TYPE_DELETE
  185. );
  186. return $this;
  187. }
  188. /**
  189. * Return is attribute global
  190. *
  191. * @return integer
  192. */
  193. public function getIsGlobal()
  194. {
  195. return $this->_getData('is_global');
  196. }
  197. /**
  198. * Retrieve attribute is global scope flag
  199. *
  200. * @return bool
  201. */
  202. public function isScopeGlobal()
  203. {
  204. return $this->getIsGlobal() == self::SCOPE_GLOBAL;
  205. }
  206. /**
  207. * Retrieve attribute is website scope website
  208. *
  209. * @return bool
  210. */
  211. public function isScopeWebsite()
  212. {
  213. return $this->getIsGlobal() == self::SCOPE_WEBSITE;
  214. }
  215. /**
  216. * Retrieve attribute is store scope flag
  217. *
  218. * @return bool
  219. */
  220. public function isScopeStore()
  221. {
  222. return !$this->isScopeGlobal() && !$this->isScopeWebsite();
  223. }
  224. /**
  225. * Retrieve store id
  226. *
  227. * @return int
  228. */
  229. public function getStoreId()
  230. {
  231. $dataObject = $this->getDataObject();
  232. if ($dataObject) {
  233. return $dataObject->getStoreId();
  234. }
  235. return $this->getData('store_id');
  236. }
  237. /**
  238. * Retrieve apply to products array
  239. * Return empty array if applied to all products
  240. *
  241. * @return array
  242. */
  243. public function getApplyTo()
  244. {
  245. if ($this->getData('apply_to')) {
  246. if (is_array($this->getData('apply_to'))) {
  247. return $this->getData('apply_to');
  248. }
  249. return explode(',', $this->getData('apply_to'));
  250. } else {
  251. return array();
  252. }
  253. }
  254. /**
  255. * Retrieve source model
  256. *
  257. * @return Mage_Eav_Model_Entity_Attribute_Source_Abstract
  258. */
  259. public function getSourceModel()
  260. {
  261. $model = $this->getData('source_model');
  262. if (empty($model)) {
  263. if ($this->getBackendType() == 'int' && $this->getFrontendInput() == 'select') {
  264. return $this->_getDefaultSourceModel();
  265. }
  266. }
  267. return $model;
  268. }
  269. /**
  270. * Check is allow for rule condition
  271. *
  272. * @return bool
  273. */
  274. public function isAllowedForRuleCondition()
  275. {
  276. $allowedInputTypes = array('text', 'multiselect', 'textarea', 'date', 'datetime', 'select', 'boolean', 'price');
  277. return $this->getIsVisible() && in_array($this->getFrontendInput(), $allowedInputTypes);
  278. }
  279. /**
  280. * Retrieve don't translated frontend label
  281. *
  282. * @return string
  283. */
  284. public function getFrontendLabel()
  285. {
  286. return $this->_getData('frontend_label');
  287. }
  288. /**
  289. * Get Attribute translated label for store
  290. *
  291. * @deprecated
  292. * @return string
  293. */
  294. protected function _getLabelForStore()
  295. {
  296. return $this->getFrontendLabel();
  297. }
  298. /**
  299. * Initialize store Labels for attributes
  300. *
  301. * @deprecated
  302. * @param int $storeId
  303. */
  304. public static function initLabels($storeId = null)
  305. {
  306. if (is_null(self::$_labels)) {
  307. if (is_null($storeId)) {
  308. $storeId = Mage::app()->getStore()->getId();
  309. }
  310. $attributeLabels = array();
  311. $attributes = Mage::getResourceSingleton('catalog/product')->getAttributesByCode();
  312. foreach ($attributes as $attribute) {
  313. if (strlen($attribute->getData('frontend_label')) > 0) {
  314. $attributeLabels[] = $attribute->getData('frontend_label');
  315. }
  316. }
  317. self::$_labels = Mage::app()->getTranslator()->getResource()->getTranslationArrayByStrings($attributeLabels, $storeId);
  318. }
  319. }
  320. /**
  321. * Get default attribute source model
  322. *
  323. * @return string
  324. */
  325. public function _getDefaultSourceModel()
  326. {
  327. return 'eav/entity_attribute_source_table';
  328. }
  329. /**
  330. * Check is an attribute used in EAV index
  331. *
  332. * @return bool
  333. */
  334. public function isIndexable()
  335. {
  336. // exclude price attribute
  337. if ($this->getAttributeCode() == 'price') {
  338. return false;
  339. }
  340. if (!$this->getIsFilterableInSearch() && !$this->getIsVisibleInAdvancedSearch() && !$this->getIsFilterable()) {
  341. return false;
  342. }
  343. $backendType = $this->getBackendType();
  344. $frontendInput = $this->getFrontendInput();
  345. if ($backendType == 'int' && $frontendInput == 'select') {
  346. return true;
  347. } else if ($backendType == 'varchar' && $frontendInput == 'multiselect') {
  348. return true;
  349. } else if ($backendType == 'decimal') {
  350. return true;
  351. }
  352. return false;
  353. }
  354. /**
  355. * Retrieve index type for indexable attribute
  356. *
  357. * @return string|false
  358. */
  359. public function getIndexType()
  360. {
  361. if (!$this->isIndexable()) {
  362. return false;
  363. }
  364. if ($this->getBackendType() == 'decimal') {
  365. return 'decimal';
  366. }
  367. return 'source';
  368. }
  369. }