PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Abstract.php

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