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:
28: /**
29: * Entity/Attribute/Model - attribute backend abstract
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_Eav_Model_Entity_Attribute_Backend_Abstract
36: implements Mage_Eav_Model_Entity_Attribute_Backend_Interface
37: {
38: /**
39: * Reference to the attribute instance
40: *
41: * @var Mage_Eav_Model_Entity_Attribute_Abstract
42: */
43: protected $_attribute;
44:
45: /**
46: * PK value_id for loaded entity (for faster updates)
47: *
48: * @var integer
49: */
50: protected $_valueId;
51:
52: /**
53: * PK value_ids for each loaded entity
54: *
55: * @var array
56: */
57: protected $_valueIds = array();
58:
59: /**
60: * Table name for this attribute
61: *
62: * @var string
63: */
64: protected $_table;
65:
66: /**
67: * Name of the entity_id field for the value table of this attribute
68: *
69: * @var string
70: */
71: protected $_entityIdField;
72:
73: /**
74: * Default value for the attribute
75: *
76: * @var mixed
77: */
78: protected $_defaultValue = null;
79:
80: /**
81: * Set attribute instance
82: *
83: * @param Mage_Eav_Model_Entity_Attribute_Abstract $attribute
84: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
85: */
86: public function setAttribute($attribute)
87: {
88: $this->_attribute = $attribute;
89: return $this;
90: }
91:
92: /**
93: * Get attribute instance
94: *
95: * @return Mage_Eav_Model_Entity_Attribute_Abstract
96: */
97: public function getAttribute()
98: {
99: return $this->_attribute;
100: }
101:
102: /**
103: * Get backend type of the attribute
104: *
105: * @return string
106: */
107: public function getType()
108: {
109: return $this->getAttribute()->getBackendType();
110: }
111:
112: /**
113: * Check whether the attribute is a real field in the entity table
114: *
115: * @return boolean
116: */
117: public function isStatic()
118: {
119: return $this->getAttribute()->isStatic();
120: }
121:
122: /**
123: * Get table name for the values of the attribute
124: *
125: * @return string
126: */
127: public function getTable()
128: {
129: if (empty($this->_table)) {
130: if ($this->isStatic()) {
131: $this->_table = $this->getAttribute()->getEntityType()->getValueTablePrefix();
132: } elseif ($this->getAttribute()->getBackendTable()) {
133: $this->_table = $this->getAttribute()->getBackendTable();
134: } else {
135: $entity = $this->getAttribute()->getEntity();
136: $tableName = sprintf('%s_%s', $entity->getValueTablePrefix(), $this->getType());
137: $this->_table = $tableName;
138: }
139: }
140:
141: return $this->_table;
142: }
143:
144: /**
145: * Get entity_id field in the attribute values tables
146: *
147: * @return string
148: */
149: public function getEntityIdField()
150: {
151: if (empty($this->_entityIdField)) {
152: if ($this->getAttribute()->getEntityIdField()) {
153: $this->_entityIdField = $this->getAttribute()->getEntityIdField();
154: } else {
155: $this->_entityIdField = $this->getAttribute()->getEntityType()->getValueEntityIdField();
156: }
157: }
158:
159: return $this->_entityIdField;
160: }
161:
162: /**
163: * Set value id
164: *
165: * @param int $valueId
166: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
167: */
168: public function setValueId($valueId)
169: {
170: $this->_valueId = $valueId;
171: return $this;
172: }
173:
174: /**
175: * Set entity value id
176: *
177: * @param Varien_Object $entity
178: * @param int $valueId
179: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
180: */
181: public function setEntityValueId($entity, $valueId)
182: {
183: if (!$entity || !$entity->getId()) {
184: return $this->setValueId($valueId);
185: }
186:
187: $this->_valueIds[$entity->getId()] = $valueId;
188: return $this;
189: }
190:
191: /**
192: * Retrieve value id
193: *
194: * @return int
195: */
196: public function getValueId()
197: {
198: return $this->_valueId;
199: }
200:
201: /**
202: * Get entity value id
203: *
204: * @param Varien_Object $entity
205: * @return int
206: */
207: public function getEntityValueId($entity)
208: {
209: if (!$entity || !$entity->getId() || !array_key_exists($entity->getId(), $this->_valueIds)) {
210: return $this->getValueId();
211: }
212:
213: return $this->_valueIds[$entity->getId()];
214: }
215:
216: /**
217: * Retrieve default value
218: *
219: * @return mixed
220: */
221: public function getDefaultValue()
222: {
223: if ($this->_defaultValue === null) {
224: if ($this->getAttribute()->getDefaultValue()) {
225: $this->_defaultValue = $this->getAttribute()->getDefaultValue();
226: } else {
227: $this->_defaultValue = "";
228: }
229: }
230:
231: return $this->_defaultValue;
232: }
233:
234: /**
235: * Validate object
236: *
237: * @param Varien_Object $object
238: * @throws Mage_Eav_Exception
239: * @return boolean
240: */
241: public function validate($object)
242: {
243: $attrCode = $this->getAttribute()->getAttributeCode();
244: $value = $object->getData($attrCode);
245: if ($this->getAttribute()->getIsRequired() && $this->getAttribute()->isValueEmpty($value)) {
246: return false;
247: }
248:
249: if ($this->getAttribute()->getIsUnique()
250: && !$this->getAttribute()->getIsRequired()
251: && ($value == '' || $this->getAttribute()->isValueEmpty($value)))
252: {
253: return true;
254: }
255:
256: if ($this->getAttribute()->getIsUnique()) {
257: if (!$this->getAttribute()->getEntity()->checkAttributeUniqueValue($this->getAttribute(), $object)) {
258: $label = $this->getAttribute()->getFrontend()->getLabel();
259: throw Mage::exception('Mage_Eav',
260: Mage::helper('eav')->__('The value of attribute "%s" must be unique', $label)
261: );
262: }
263: }
264:
265: return true;
266: }
267:
268: /**
269: * After load method
270: *
271: * @param Varien_Object $object
272: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
273: */
274: public function afterLoad($object)
275: {
276: return $this;
277: }
278:
279: /**
280: * Before save method
281: *
282: * @param Varien_Object $object
283: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
284: */
285: public function beforeSave($object)
286: {
287: $attrCode = $this->getAttribute()->getAttributeCode();
288: if (!$object->hasData($attrCode) && $this->getDefaultValue()) {
289: $object->setData($attrCode, $this->getDefaultValue());
290: }
291:
292: return $this;
293: }
294:
295: /**
296: * After save method
297: *
298: * @param Varien_Object $object
299: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
300: */
301: public function afterSave($object)
302: {
303: return $this;
304: }
305:
306: /**
307: * Before delete method
308: *
309: * @param Varien_Object $object
310: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
311: */
312: public function beforeDelete($object)
313: {
314: return $this;
315: }
316: /**
317: * After delete method
318: *
319: * @param Varien_Object $object
320: * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
321: */
322: public function afterDelete($object)
323: {
324: return $this;
325: }
326: }
327: