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_Catalog
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: * Abstract model for catalog entities
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Catalog_Model_Abstract extends Mage_Core_Model_Abstract
35: {
36: /**
37: * Identifuer of default store
38: * used for loading default data for entity
39: */
40: const DEFAULT_STORE_ID = 0;
41:
42: /**
43: * Attribute default values
44: *
45: * This array contain default values for attributes which was redefine
46: * value for store
47: *
48: * @var array
49: */
50: protected $_defaultValues = array();
51:
52: /**
53: * This array contains codes of attributes which have value in current store
54: *
55: * @var array
56: */
57: protected $_storeValuesFlags = array();
58:
59: /**
60: * Locked attributes
61: *
62: * @var array
63: */
64: protected $_lockedAttributes = array();
65:
66: /**
67: * Is model deleteable
68: *
69: * @var boolean
70: */
71: protected $_isDeleteable = true;
72:
73: /**
74: * Is model readonly
75: *
76: * @var boolean
77: */
78: protected $_isReadonly = false;
79:
80:
81: /**
82: * Lock attribute
83: *
84: * @param string $attributeCode
85: * @return Mage_Catalog_Model_Abstract
86: */
87: public function lockAttribute($attributeCode)
88: {
89: $this->_lockedAttributes[$attributeCode] = true;
90: return $this;
91: }
92:
93: /**
94: * Unlock attribute
95: *
96: * @param string $attributeCode
97: * @return Mage_Catalog_Model_Abstract
98: */
99: public function unlockAttribute($attributeCode)
100: {
101: if ($this->isLockedAttribute($attributeCode)) {
102: unset($this->_lockedAttributes[$attributeCode]);
103: }
104:
105: return $this;
106: }
107:
108: /**
109: * Unlock all attributes
110: *
111: * @return Mage_Catalog_Model_Abstract
112: */
113: public function unlockAttributes()
114: {
115: $this->_lockedAttributes = array();
116: return $this;
117: }
118:
119: /**
120: * Retrieve locked attributes
121: *
122: * @return array
123: */
124: public function getLockedAttributes()
125: {
126: return array_keys($this->_lockedAttributes);
127: }
128:
129: /**
130: * Checks that model have locked attributes
131: *
132: * @return boolean
133: */
134: public function hasLockedAttributes()
135: {
136: return !empty($this->_lockedAttributes);
137: }
138:
139: /**
140: * Retrieve locked attributes
141: *
142: * @return boolean
143: */
144: public function isLockedAttribute($attributeCode)
145: {
146: return isset($this->_lockedAttributes[$attributeCode]);
147: }
148:
149: /**
150: * Overwrite data in the object.
151: *
152: * $key can be string or array.
153: * If $key is string, the attribute value will be overwritten by $value
154: *
155: * If $key is an array, it will overwrite all the data in the object.
156: *
157: * $isChanged will specify if the object needs to be saved after an update.
158: *
159: * @param string|array $key
160: * @param mixed $value
161: * @param boolean $isChanged
162: * @return Varien_Object
163: */
164: public function setData($key, $value = null)
165: {
166: if ($this->hasLockedAttributes()) {
167: if (is_array($key)) {
168: foreach ($this->getLockedAttributes() as $attribute) {
169: if (isset($key[$attribute])) {
170: unset($key[$attribute]);
171: }
172: }
173: } elseif ($this->isLockedAttribute($key)) {
174: return $this;
175: }
176: } elseif ($this->isReadonly()) {
177: return $this;
178: }
179:
180: return parent::setData($key, $value);
181: }
182:
183: /**
184: * Unset data from the object.
185: *
186: * $key can be a string only. Array will be ignored.
187: *
188: * $isChanged will specify if the object needs to be saved after an update.
189: *
190: * @param string $key
191: * @param boolean $isChanged
192: * @return Mage_Catalog_Model_Abstract
193: */
194: public function unsetData($key = null)
195: {
196: if ((!is_null($key) && $this->isLockedAttribute($key)) ||
197: $this->isReadonly()) {
198: return $this;
199: }
200:
201: return parent::unsetData($key);
202: }
203:
204: /**
205: * Get collection instance
206: *
207: * @return Mage_Catalog_Model_Resource_Collection_Abstract
208: */
209: public function getResourceCollection()
210: {
211: $collection = parent::getResourceCollection()
212: ->setStoreId($this->getStoreId());
213: return $collection;
214: }
215:
216: /**
217: * Load entity by attribute
218: *
219: * @param Mage_Eav_Model_Entity_Attribute_Interface|integer|string|array $attribute
220: * @param null|string|array $value
221: * @param string $additionalAttributes
222: * @return bool|Mage_Catalog_Model_Abstract
223: */
224: public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
225: {
226: $collection = $this->getResourceCollection()
227: ->addAttributeToSelect($additionalAttributes)
228: ->addAttributeToFilter($attribute, $value)
229: ->setPage(1,1);
230:
231: foreach ($collection as $object) {
232: return $object;
233: }
234: return false;
235: }
236:
237: /**
238: * Retrieve sore object
239: *
240: * @return Mage_Core_Model_Store
241: */
242: public function getStore()
243: {
244: return Mage::app()->getStore($this->getStoreId());
245: }
246:
247: /**
248: * Retrieve all store ids of object current website
249: *
250: * @return array
251: */
252: public function getWebsiteStoreIds()
253: {
254: return $this->getStore()->getWebsite()->getStoreIds(true);
255: }
256:
257: /**
258: * Adding attribute code and value to default value registry
259: *
260: * Default value existing is flag for using store value in data
261: *
262: * @param string $attributeCode
263: * @value mixed $value
264: * @return Mage_Catalog_Model_Abstract
265: */
266: public function setAttributeDefaultValue($attributeCode, $value)
267: {
268: $this->_defaultValues[$attributeCode] = $value;
269: return $this;
270: }
271:
272: /**
273: * Retrieve default value for attribute code
274: *
275: * @param string $attributeCode
276: * @return array|boolean
277: */
278: public function getAttributeDefaultValue($attributeCode)
279: {
280: return array_key_exists($attributeCode, $this->_defaultValues) ? $this->_defaultValues[$attributeCode] : false;
281: }
282:
283: /**
284: * Set attribute code flag if attribute has value in current store and does not use
285: * value of default store as value
286: *
287: * @param string $attributeCode
288: * @return Mage_Catalog_Model_Abstract
289: */
290: public function setExistsStoreValueFlag($attributeCode)
291: {
292: $this->_storeValuesFlags[$attributeCode] = true;
293: return $this;
294: }
295:
296: /**
297: * Check if object attribute has value in current store
298: *
299: * @param string $attributeCode
300: * @return bool
301: */
302: public function getExistsStoreValueFlag($attributeCode)
303: {
304: return array_key_exists($attributeCode, $this->_storeValuesFlags);
305: }
306:
307: /**
308: * Before save unlock attributes
309: *
310: * @return Mage_Catalog_Model_Abstract
311: */
312: protected function _beforeSave()
313: {
314: $this->unlockAttributes();
315: return parent::_beforeSave();
316: }
317:
318: /**
319: * Checks model is deletable
320: *
321: * @return boolean
322: */
323: public function isDeleteable()
324: {
325: return $this->_isDeleteable;
326: }
327:
328: /**
329: * Set is deletable flag
330: *
331: * @param boolean $value
332: * @return Mage_Catalog_Model_Abstract
333: */
334: public function setIsDeleteable($value)
335: {
336: $this->_isDeleteable = (bool) $value;
337: return $this;
338: }
339:
340: /**
341: * Checks model is deletable
342: *
343: * @return boolean
344: */
345: public function isReadonly()
346: {
347: return $this->_isReadonly;
348: }
349:
350: /**
351: * Set is deletable flag
352: *
353: * @param boolean $value
354: * @return Mage_Catalog_Model_Abstract
355: */
356: public function setIsReadonly($value)
357: {
358: $this->_isReadonly = (bool)$value;
359: return $this;
360: }
361:
362: }
363: