00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Eav_Model_Entity_Attribute extends Mage_Eav_Model_Entity_Attribute_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_eventPrefix = 'eav_entity_attribute';
00043
00044
00045
00046
00047
00048
00049
00050
00051 protected $_eventObject = 'attribute';
00052
00053 const CACHE_TAG = 'EAV_ATTRIBUTE';
00054 protected $_cacheTag = 'EAV_ATTRIBUTE';
00055
00056 protected function _getDefaultBackendModel()
00057 {
00058 switch ($this->getAttributeCode()) {
00059 case 'created_at':
00060 return 'eav/entity_attribute_backend_time_created';
00061
00062 case 'updated_at':
00063 return 'eav/entity_attribute_backend_time_updated';
00064
00065 case 'store_id':
00066 return 'eav/entity_attribute_backend_store';
00067
00068 case 'increment_id':
00069 return 'eav/entity_attribute_backend_increment';
00070 }
00071
00072
00073
00074 return parent::_getDefaultBackendModel();
00075 }
00076
00077 protected function _getDefaultFrontendModel()
00078 {
00079 return parent::_getDefaultFrontendModel();
00080 }
00081
00082 protected function _getDefaultSourceModel()
00083 {
00084 switch ($this->getAttributeCode()) {
00085 case 'store_id':
00086 return 'eav/entity_attribute_source_store';
00087 }
00088 return parent::_getDefaultSourceModel();
00089 }
00090
00091 public function deleteEntity()
00092 {
00093 return $this->_getResource()->deleteEntity($this);
00094 }
00095
00096 protected function _beforeSave()
00097 {
00098
00099 if (isset($this->_data['attribute_code'])
00100 && Mage::getModel('catalog/product')->isReservedAttribute($this)) {
00101 Mage::throwException(Mage::helper('eav')->__('The attribute code \'%s\' is reserved by system. Please, try another attribute code.', $this->_data['attribute_code']));
00102 }
00103
00104
00105 if (isset($this->_origData['is_global'])) {
00106 if (!isset($this->_data['is_global'])) {
00107 Mage::throwException('0_o');
00108 }
00109 if (($this->_data['is_global'] != $this->_origData['is_global'])
00110 && $this->_getResource()->isUsedBySuperProducts($this)) {
00111 Mage::throwException(Mage::helper('eav')->__('Scope must not be changed, because the attribute is used in configurable products.'));
00112 }
00113 }
00114
00115 if ($this->getBackendType() == 'datetime') {
00116 if (!$this->getBackendModel()) {
00117 $this->setBackendModel('eav/entity_attribute_backend_datetime');
00118 }
00119
00120 if (!$this->getFrontendModel()) {
00121 $this->setFrontendModel('eav/entity_attribute_frontend_datetime');
00122 }
00123
00124
00125 if ($defaultValue = $this->getDefaultValue()) {
00126 $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00127 $this->setDefaultValue(Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue());
00128 }
00129 }
00130
00131 if ($this->getBackendType() == 'gallery') {
00132 if (!$this->getBackendModel()) {
00133 $this->setBackendModel('eav/entity_attribute_backend_media');
00134 }
00135 }
00136
00137 if ($this->getFrontendInput() == 'price') {
00138 if (!$this->getBackendModel()) {
00139 $this->setBackendModel('catalog/product_attribute_backend_price');
00140 }
00141 }
00142
00143 return parent::_beforeSave();
00144 }
00145
00146 protected function _afterSave()
00147 {
00148 $this->_getResource()->saveInSetIncluding($this);
00149
00150 return parent::_afterSave();
00151 }
00152
00153 protected function _beforeDelete()
00154 {
00155 if ($this->_getResource()->isUsedBySuperProducts($this)) {
00156 Mage::throwException(Mage::helper('eav')->__('Attribute used in configurable products.'));
00157 }
00158 return parent::_beforeDelete();
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 public function getBackendTypeByInput($type)
00168 {
00169 switch ($type) {
00170 case 'text':
00171 case 'gallery':
00172 case 'media_image':
00173 case 'multiselect':
00174 return 'varchar';
00175
00176 case 'image':
00177 case 'textarea':
00178 return 'text';
00179
00180 case 'date':
00181 return 'datetime';
00182
00183 case 'select':
00184 case 'boolean':
00185 return 'int';
00186
00187
00188 case 'price':
00189 return 'decimal';
00190
00191
00192
00193
00194
00195
00196
00197
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 public function getDefaultValueByInput($type)
00208 {
00209 $field = '';
00210 switch ($type) {
00211 case 'select':
00212 case 'gallery':
00213 case 'media_image':
00214 case 'multiselect':
00215 return '';
00216
00217 case 'text':
00218 case 'price':
00219 case 'image':
00220 $field = 'default_value_text';
00221 break;
00222
00223 case 'textarea':
00224 $field = 'default_value_textarea';
00225 break;
00226
00227 case 'date':
00228 $field = 'default_value_date';
00229 break;
00230
00231 case 'boolean':
00232 $field = 'default_value_yesno';
00233 break;
00234
00235
00236
00237
00238
00239
00240
00241
00242 }
00243
00244 return $field;
00245 }
00246 public function getAttributeCodesByFrontendType($type)
00247 {
00248 return $this->getResource()->getAttributeCodesByFrontendType($type);
00249 }
00250 }