1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Eav_Model_Entity_Attribute extends Mage_Eav_Model_Entity_Attribute_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_eventPrefix = 'eav_entity_attribute';
43:
44: CONST ATTRIBUTE_CODE_MAX_LENGTH = 30;
45:
46: 47: 48: 49: 50: 51: 52:
53: protected $_eventObject = 'attribute';
54:
55: const CACHE_TAG = 'EAV_ATTRIBUTE';
56: protected $_cacheTag = 'EAV_ATTRIBUTE';
57:
58: 59: 60: 61: 62:
63: protected function _getDefaultBackendModel()
64: {
65: switch ($this->getAttributeCode()) {
66: case 'created_at':
67: return 'eav/entity_attribute_backend_time_created';
68:
69: case 'updated_at':
70: return 'eav/entity_attribute_backend_time_updated';
71:
72: case 'store_id':
73: return 'eav/entity_attribute_backend_store';
74:
75: case 'increment_id':
76: return 'eav/entity_attribute_backend_increment';
77: }
78:
79: return parent::_getDefaultBackendModel();
80: }
81:
82: 83: 84: 85: 86:
87: protected function _getDefaultFrontendModel()
88: {
89: return parent::_getDefaultFrontendModel();
90: }
91:
92: 93: 94: 95: 96:
97: protected function _getDefaultSourceModel()
98: {
99: if ($this->getAttributeCode() == 'store_id') {
100: return 'eav/entity_attribute_source_store';
101: }
102: return parent::_getDefaultSourceModel();
103: }
104:
105: 106: 107: 108: 109:
110: public function deleteEntity()
111: {
112: return $this->_getResource()->deleteEntity($this);
113: }
114:
115: 116: 117: 118: 119:
120: public function loadEntityAttributeIdBySet()
121: {
122:
123: $filteredAttributes = $this->getResourceCollection()
124: ->setAttributeSetFilter($this->getAttributeSetId())
125: ->addFieldToFilter('entity_attribute.attribute_id', $this->getId())
126: ->load();
127: if (count($filteredAttributes) > 0) {
128:
129: $this->setEntityAttributeId($filteredAttributes->getFirstItem()->getEntityAttributeId());
130: }
131: return $this;
132: }
133:
134: 135: 136: 137: 138:
139: protected function _beforeSave()
140: {
141:
142: if (isset($this->_data['attribute_code'])
143: && Mage::getModel('catalog/product')->isReservedAttribute($this))
144: {
145: 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']));
146: }
147:
148: 149: 150:
151: if (isset($this->_data['attribute_code']) &&
152: !Zend_Validate::is(
153: $this->_data['attribute_code'],
154: 'StringLength',
155: array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH)
156: )
157: ) {
158: throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Maximum length of attribute code must be less then %s symbols', self::ATTRIBUTE_CODE_MAX_LENGTH));
159: }
160:
161: $defaultValue = $this->getDefaultValue();
162: $hasDefaultValue = ((string)$defaultValue != '');
163:
164: if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
165: $locale = Mage::app()->getLocale()->getLocaleCode();
166: if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => $locale))) {
167: throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
168: }
169:
170: try {
171: $filter = new Zend_Filter_LocalizedToNormalized(
172: array('locale' => Mage::app()->getLocale()->getLocaleCode())
173: );
174: $this->setDefaultValue($filter->filter($defaultValue));
175: } catch (Exception $e) {
176: throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
177: }
178: }
179:
180: if ($this->getBackendType() == 'datetime') {
181: if (!$this->getBackendModel()) {
182: $this->setBackendModel('eav/entity_attribute_backend_datetime');
183: }
184:
185: if (!$this->getFrontendModel()) {
186: $this->setFrontendModel('eav/entity_attribute_frontend_datetime');
187: }
188:
189:
190: if ($hasDefaultValue) {
191: $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
192: try {
193: $defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue();
194: $this->setDefaultValue($defaultValue);
195: } catch (Exception $e) {
196: throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default date'));
197: }
198: }
199: }
200:
201: if ($this->getBackendType() == 'gallery') {
202: if (!$this->getBackendModel()) {
203: $this->setBackendModel('eav/entity_attribute_backend_media');
204: }
205: }
206:
207: return parent::_beforeSave();
208: }
209:
210: 211: 212: 213: 214:
215: protected function _afterSave()
216: {
217: $this->_getResource()->saveInSetIncluding($this);
218: return parent::_afterSave();
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function getBackendTypeByInput($type)
228: {
229: $field = null;
230: switch ($type) {
231: case 'text':
232: case 'gallery':
233: case 'media_image':
234: case 'multiselect':
235: $field = 'varchar';
236: break;
237:
238: case 'image':
239: case 'textarea':
240: $field = 'text';
241: break;
242:
243: case 'date':
244: $field = 'datetime';
245: break;
246:
247: case 'select':
248: case 'boolean':
249: $field = 'int';
250: break;
251:
252: case 'price':
253: $field = 'decimal';
254: break;
255: }
256:
257: return $field;
258: }
259:
260: 261: 262: 263: 264: 265:
266: public function getDefaultValueByInput($type)
267: {
268: $field = '';
269: switch ($type) {
270: case 'select':
271: case 'gallery':
272: case 'media_image':
273: break;
274: case 'multiselect':
275: $field = null;
276: break;
277:
278: case 'text':
279: case 'price':
280: case 'image':
281: $field = 'default_value_text';
282: break;
283:
284: case 'textarea':
285: $field = 'default_value_textarea';
286: break;
287:
288: case 'date':
289: $field = 'default_value_date';
290: break;
291:
292: case 'boolean':
293: $field = 'default_value_yesno';
294: break;
295: }
296:
297: return $field;
298: }
299:
300: 301: 302: 303: 304: 305:
306: public function getAttributeCodesByFrontendType($type)
307: {
308: return $this->getResource()->getAttributeCodesByFrontendType($type);
309: }
310:
311: 312: 313: 314: 315:
316: public function getStoreLabels()
317: {
318: if (!$this->getData('store_labels')) {
319: $storeLabel = $this->getResource()->getStoreLabelsByAttributeId($this->getId());
320: $this->setData('store_labels', $storeLabel);
321: }
322: return $this->getData('store_labels');
323: }
324:
325: 326: 327: 328: 329:
330: public function getStoreLabel($storeId = null)
331: {
332: if ($this->hasData('store_label')) {
333: return $this->getData('store_label');
334: }
335: $store = Mage::app()->getStore($storeId);
336: $label = false;
337: if (!$store->isAdmin()) {
338: $labels = $this->getStoreLabels();
339: if (isset($labels[$store->getId()])) {
340: return $labels[$store->getId()];
341: }
342: }
343: return $this->getFrontendLabel();
344: }
345: }
346: