PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Eav/Model/Entity/Attribute.php

https://bitbucket.org/dnejedly/eaparts
PHP | 345 lines | 192 code | 40 blank | 113 comment | 26 complexity | 6767004ad4d075e23e07f22e0f8d5b12 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_Eav
  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. /**
  27. * EAV Entity attribute model
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Eav_Model_Entity_Attribute extends Mage_Eav_Model_Entity_Attribute_Abstract
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'eav_entity_attribute';
  41. CONST ATTRIBUTE_CODE_MAX_LENGTH = 30;
  42. /**
  43. * Parameter name in event
  44. *
  45. * In observe method you can use $observer->getEvent()->getAttribute() in this case
  46. *
  47. * @var string
  48. */
  49. protected $_eventObject = 'attribute';
  50. const CACHE_TAG = 'EAV_ATTRIBUTE';
  51. protected $_cacheTag = 'EAV_ATTRIBUTE';
  52. /**
  53. * Retreive default attribute backend model by attribute code
  54. *
  55. * @return string
  56. */
  57. protected function _getDefaultBackendModel()
  58. {
  59. switch ($this->getAttributeCode()) {
  60. case 'created_at':
  61. return 'eav/entity_attribute_backend_time_created';
  62. case 'updated_at':
  63. return 'eav/entity_attribute_backend_time_updated';
  64. case 'store_id':
  65. return 'eav/entity_attribute_backend_store';
  66. case 'increment_id':
  67. return 'eav/entity_attribute_backend_increment';
  68. }
  69. return parent::_getDefaultBackendModel();
  70. }
  71. /**
  72. * Retreive default attribute frontend model
  73. *
  74. * @return string
  75. */
  76. protected function _getDefaultFrontendModel()
  77. {
  78. return parent::_getDefaultFrontendModel();
  79. }
  80. /**
  81. * Retreive default attribute source model
  82. *
  83. * @return string
  84. */
  85. protected function _getDefaultSourceModel()
  86. {
  87. if ($this->getAttributeCode() == 'store_id') {
  88. return 'eav/entity_attribute_source_store';
  89. }
  90. return parent::_getDefaultSourceModel();
  91. }
  92. /**
  93. * Delete entity
  94. *
  95. * @return Mage_Eav_Model_Resource_Entity_Attribute
  96. */
  97. public function deleteEntity()
  98. {
  99. return $this->_getResource()->deleteEntity($this);
  100. }
  101. /**
  102. * Load entity_attribute_id into $this by $this->attribute_set_id
  103. *
  104. * @return Mage_Core_Model_Abstract
  105. */
  106. public function loadEntityAttributeIdBySet()
  107. {
  108. // load attributes collection filtered by attribute_id and attribute_set_id
  109. $filteredAttributes = $this->getResourceCollection()
  110. ->setAttributeSetFilter($this->getAttributeSetId())
  111. ->addFieldToFilter('entity_attribute.attribute_id', $this->getId())
  112. ->load();
  113. if (count($filteredAttributes) > 0) {
  114. // getFirstItem() can be used as we can have one or zero records in the collection
  115. $this->setEntityAttributeId($filteredAttributes->getFirstItem()->getEntityAttributeId());
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Prepare data for save
  121. *
  122. * @return Mage_Eav_Model_Entity_Attribute
  123. */
  124. protected function _beforeSave()
  125. {
  126. // prevent overriding product data
  127. if (isset($this->_data['attribute_code'])
  128. && Mage::getModel('catalog/product')->isReservedAttribute($this))
  129. {
  130. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('The attribute code \'%s\' is reserved by system. Please try another attribute code', $this->_data['attribute_code']));
  131. }
  132. /**
  133. * Check for maximum attribute_code length
  134. */
  135. if (isset($this->_data['attribute_code']) &&
  136. !Zend_Validate::is(
  137. $this->_data['attribute_code'],
  138. 'StringLength',
  139. array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH)
  140. )
  141. ) {
  142. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Maximum length of attribute code must be less then %s symbols', self::ATTRIBUTE_CODE_MAX_LENGTH));
  143. }
  144. $defaultValue = $this->getDefaultValue();
  145. $hasDefaultValue = ((string)$defaultValue != '');
  146. if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
  147. $locale = Mage::app()->getLocale()->getLocaleCode();
  148. if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => $locale))) {
  149. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
  150. }
  151. try {
  152. $filter = new Zend_Filter_LocalizedToNormalized(
  153. array('locale' => Mage::app()->getLocale()->getLocaleCode())
  154. );
  155. $this->setDefaultValue($filter->filter($defaultValue));
  156. } catch (Exception $e) {
  157. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
  158. }
  159. }
  160. if ($this->getBackendType() == 'datetime') {
  161. if (!$this->getBackendModel()) {
  162. $this->setBackendModel('eav/entity_attribute_backend_datetime');
  163. }
  164. if (!$this->getFrontendModel()) {
  165. $this->setFrontendModel('eav/entity_attribute_frontend_datetime');
  166. }
  167. // save default date value as timestamp
  168. if ($hasDefaultValue) {
  169. $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
  170. try {
  171. $defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue();
  172. $this->setDefaultValue($defaultValue);
  173. } catch (Exception $e) {
  174. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default date'));
  175. }
  176. }
  177. }
  178. if ($this->getBackendType() == 'gallery') {
  179. if (!$this->getBackendModel()) {
  180. $this->setBackendModel('eav/entity_attribute_backend_media');
  181. }
  182. }
  183. return parent::_beforeSave();
  184. }
  185. /**
  186. * Save additional data
  187. *
  188. * @return Mage_Eav_Model_Entity_Attribute
  189. */
  190. protected function _afterSave()
  191. {
  192. $this->_getResource()->saveInSetIncluding($this);
  193. return parent::_afterSave();
  194. }
  195. /**
  196. * Detect backend storage type using frontend input type
  197. *
  198. * @return string backend_type field value
  199. * @param string $type frontend_input field value
  200. */
  201. public function getBackendTypeByInput($type)
  202. {
  203. $field = null;
  204. switch ($type) {
  205. case 'text':
  206. case 'gallery':
  207. case 'media_image':
  208. case 'multiselect':
  209. $field = 'varchar';
  210. break;
  211. case 'image':
  212. case 'textarea':
  213. $field = 'text';
  214. break;
  215. case 'date':
  216. $field = 'datetime';
  217. break;
  218. case 'select':
  219. case 'boolean':
  220. $field = 'int';
  221. break;
  222. case 'price':
  223. $field = 'decimal';
  224. break;
  225. }
  226. return $field;
  227. }
  228. /**
  229. * Detect default value using frontend input type
  230. *
  231. * @return string default_value field value
  232. * @param string $type frontend_input field name
  233. */
  234. public function getDefaultValueByInput($type)
  235. {
  236. $field = '';
  237. switch ($type) {
  238. case 'select':
  239. case 'gallery':
  240. case 'media_image':
  241. break;
  242. case 'multiselect':
  243. $field = null;
  244. break;
  245. case 'text':
  246. case 'price':
  247. case 'image':
  248. $field = 'default_value_text';
  249. break;
  250. case 'textarea':
  251. $field = 'default_value_textarea';
  252. break;
  253. case 'date':
  254. $field = 'default_value_date';
  255. break;
  256. case 'boolean':
  257. $field = 'default_value_yesno';
  258. break;
  259. }
  260. return $field;
  261. }
  262. /**
  263. * Retreive attribute codes by frontend type
  264. *
  265. * @param string $type
  266. * @return array
  267. */
  268. public function getAttributeCodesByFrontendType($type)
  269. {
  270. return $this->getResource()->getAttributeCodesByFrontendType($type);
  271. }
  272. /**
  273. * Return array of labels of stores
  274. *
  275. * @return array
  276. */
  277. public function getStoreLabels()
  278. {
  279. if (!$this->getData('store_labels')) {
  280. $storeLabel = $this->getResource()->getStoreLabelsByAttributeId($this->getId());
  281. $this->setData('store_labels', $storeLabel);
  282. }
  283. return $this->getData('store_labels');
  284. }
  285. /**
  286. * Return store label of attribute
  287. *
  288. * @return string
  289. */
  290. public function getStoreLabel($storeId = null)
  291. {
  292. if ($this->hasData('store_label')) {
  293. return $this->getData('store_label');
  294. }
  295. $store = Mage::app()->getStore($storeId);
  296. $label = false;
  297. if (!$store->isAdmin()) {
  298. $labels = $this->getStoreLabels();
  299. if (isset($labels[$store->getId()])) {
  300. return $labels[$store->getId()];
  301. }
  302. }
  303. return $this->getFrontendLabel();
  304. }
  305. }