-
Notifications
You must be signed in to change notification settings - Fork 16
/
Attribute.php
345 lines (305 loc) · 10.4 KB
/
Attribute.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Eav
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* EAV Entity attribute model
*
* @category Mage
* @package Mage_Eav
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Eav_Model_Entity_Attribute extends Mage_Eav_Model_Entity_Attribute_Abstract
{
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'eav_entity_attribute';
CONST ATTRIBUTE_CODE_MAX_LENGTH = 30;
/**
* Parameter name in event
*
* In observe method you can use $observer->getEvent()->getAttribute() in this case
*
* @var string
*/
protected $_eventObject = 'attribute';
const CACHE_TAG = 'EAV_ATTRIBUTE';
protected $_cacheTag = 'EAV_ATTRIBUTE';
/**
* Retreive default attribute backend model by attribute code
*
* @return string
*/
protected function _getDefaultBackendModel()
{
switch ($this->getAttributeCode()) {
case 'created_at':
return 'eav/entity_attribute_backend_time_created';
case 'updated_at':
return 'eav/entity_attribute_backend_time_updated';
case 'store_id':
return 'eav/entity_attribute_backend_store';
case 'increment_id':
return 'eav/entity_attribute_backend_increment';
}
return parent::_getDefaultBackendModel();
}
/**
* Retreive default attribute frontend model
*
* @return string
*/
protected function _getDefaultFrontendModel()
{
return parent::_getDefaultFrontendModel();
}
/**
* Retreive default attribute source model
*
* @return string
*/
protected function _getDefaultSourceModel()
{
if ($this->getAttributeCode() == 'store_id') {
return 'eav/entity_attribute_source_store';
}
return parent::_getDefaultSourceModel();
}
/**
* Delete entity
*
* @return Mage_Eav_Model_Resource_Entity_Attribute
*/
public function deleteEntity()
{
return $this->_getResource()->deleteEntity($this);
}
/**
* Load entity_attribute_id into $this by $this->attribute_set_id
*
* @return Mage_Core_Model_Abstract
*/
public function loadEntityAttributeIdBySet()
{
// load attributes collection filtered by attribute_id and attribute_set_id
$filteredAttributes = $this->getResourceCollection()
->setAttributeSetFilter($this->getAttributeSetId())
->addFieldToFilter('entity_attribute.attribute_id', $this->getId())
->load();
if (count($filteredAttributes) > 0) {
// getFirstItem() can be used as we can have one or zero records in the collection
$this->setEntityAttributeId($filteredAttributes->getFirstItem()->getEntityAttributeId());
}
return $this;
}
/**
* Prepare data for save
*
* @return Mage_Eav_Model_Entity_Attribute
*/
protected function _beforeSave()
{
// prevent overriding product data
if (isset($this->_data['attribute_code'])
&& Mage::getModel('catalog/product')->isReservedAttribute($this))
{
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']));
}
/**
* Check for maximum attribute_code length
*/
if (isset($this->_data['attribute_code']) &&
!Zend_Validate::is(
$this->_data['attribute_code'],
'StringLength',
array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH)
)
) {
throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Maximum length of attribute code must be less then %s symbols', self::ATTRIBUTE_CODE_MAX_LENGTH));
}
$defaultValue = $this->getDefaultValue();
$hasDefaultValue = ((string)$defaultValue != '');
if ($this->getBackendType() == 'decimal' && $hasDefaultValue) {
$locale = Mage::app()->getLocale()->getLocaleCode();
if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => $locale))) {
throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
}
try {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$this->setDefaultValue($filter->filter($defaultValue));
} catch (Exception $e) {
throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value'));
}
}
if ($this->getBackendType() == 'datetime') {
if (!$this->getBackendModel()) {
$this->setBackendModel('eav/entity_attribute_backend_datetime');
}
if (!$this->getFrontendModel()) {
$this->setFrontendModel('eav/entity_attribute_frontend_datetime');
}
// save default date value as timestamp
if ($hasDefaultValue) {
$format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
try {
$defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue();
$this->setDefaultValue($defaultValue);
} catch (Exception $e) {
throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default date'));
}
}
}
if ($this->getBackendType() == 'gallery') {
if (!$this->getBackendModel()) {
$this->setBackendModel('eav/entity_attribute_backend_media');
}
}
return parent::_beforeSave();
}
/**
* Save additional data
*
* @return Mage_Eav_Model_Entity_Attribute
*/
protected function _afterSave()
{
$this->_getResource()->saveInSetIncluding($this);
return parent::_afterSave();
}
/**
* Detect backend storage type using frontend input type
*
* @return string backend_type field value
* @param string $type frontend_input field value
*/
public function getBackendTypeByInput($type)
{
$field = null;
switch ($type) {
case 'text':
case 'gallery':
case 'media_image':
case 'multiselect':
$field = 'varchar';
break;
case 'image':
case 'textarea':
$field = 'text';
break;
case 'date':
$field = 'datetime';
break;
case 'select':
case 'boolean':
$field = 'int';
break;
case 'price':
$field = 'decimal';
break;
}
return $field;
}
/**
* Detect default value using frontend input type
*
* @return string default_value field value
* @param string $type frontend_input field name
*/
public function getDefaultValueByInput($type)
{
$field = '';
switch ($type) {
case 'select':
case 'gallery':
case 'media_image':
break;
case 'multiselect':
$field = null;
break;
case 'text':
case 'price':
case 'image':
$field = 'default_value_text';
break;
case 'textarea':
$field = 'default_value_textarea';
break;
case 'date':
$field = 'default_value_date';
break;
case 'boolean':
$field = 'default_value_yesno';
break;
}
return $field;
}
/**
* Retreive attribute codes by frontend type
*
* @param string $type
* @return array
*/
public function getAttributeCodesByFrontendType($type)
{
return $this->getResource()->getAttributeCodesByFrontendType($type);
}
/**
* Return array of labels of stores
*
* @return array
*/
public function getStoreLabels()
{
if (!$this->getData('store_labels')) {
$storeLabel = $this->getResource()->getStoreLabelsByAttributeId($this->getId());
$this->setData('store_labels', $storeLabel);
}
return $this->getData('store_labels');
}
/**
* Return store label of attribute
*
* @return string
*/
public function getStoreLabel($storeId = null)
{
if ($this->hasData('store_label')) {
return $this->getData('store_label');
}
$store = Mage::app()->getStore($storeId);
$label = false;
if (!$store->isAdmin()) {
$labels = $this->getStoreLabels();
if (isset($labels[$store->getId()])) {
return $labels[$store->getId()];
}
}
return $this->getFrontendLabel();
}
}