PageRenderTime 34ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-catalog/Model/AbstractModel.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 377 lines | 156 code | 33 blank | 188 comment | 7 complexity | aa61e055bc7624d947799f38937e8353 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model;
  7. use Magento\Framework\Api\AttributeValueFactory;
  8. /**
  9. * Abstract model for catalog entities
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. abstract class AbstractModel extends \Magento\Framework\Model\AbstractExtensibleModel
  14. {
  15. /**
  16. * Attribute default values
  17. *
  18. * This array contain default values for attributes which was redefine
  19. * value for store
  20. *
  21. * @var array
  22. */
  23. protected $_defaultValues = [];
  24. /**
  25. * This array contains codes of attributes which have value in current store
  26. *
  27. * @var array
  28. */
  29. protected $_storeValuesFlags = [];
  30. /**
  31. * Locked attributes
  32. *
  33. * @var array
  34. */
  35. protected $_lockedAttributes = [];
  36. /**
  37. * Is model deleteable
  38. *
  39. * @var boolean
  40. */
  41. protected $_isDeleteable = true;
  42. /**
  43. * Is model readonly
  44. *
  45. * @var boolean
  46. */
  47. protected $_isReadonly = false;
  48. /**
  49. * Store manager
  50. *
  51. * @var \Magento\Store\Model\StoreManagerInterface
  52. */
  53. protected $_storeManager;
  54. /**
  55. * @param \Magento\Framework\Model\Context $context
  56. * @param \Magento\Framework\Registry $registry
  57. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  58. * @param AttributeValueFactory $customAttributeFactory
  59. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  60. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  61. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  62. * @param array $data
  63. */
  64. public function __construct(
  65. \Magento\Framework\Model\Context $context,
  66. \Magento\Framework\Registry $registry,
  67. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  68. AttributeValueFactory $customAttributeFactory,
  69. \Magento\Store\Model\StoreManagerInterface $storeManager,
  70. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  71. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  72. array $data = []
  73. ) {
  74. $this->_storeManager = $storeManager;
  75. parent::__construct(
  76. $context,
  77. $registry,
  78. $extensionFactory,
  79. $customAttributeFactory,
  80. $resource,
  81. $resourceCollection,
  82. $data
  83. );
  84. }
  85. /**
  86. * Lock attribute
  87. *
  88. * @param string $attributeCode
  89. * @return $this
  90. */
  91. public function lockAttribute($attributeCode)
  92. {
  93. $this->_lockedAttributes[$attributeCode] = true;
  94. return $this;
  95. }
  96. /**
  97. * Unlock attribute
  98. *
  99. * @param string $attributeCode
  100. * @return $this
  101. */
  102. public function unlockAttribute($attributeCode)
  103. {
  104. if ($this->isLockedAttribute($attributeCode)) {
  105. unset($this->_lockedAttributes[$attributeCode]);
  106. }
  107. return $this;
  108. }
  109. /**
  110. * Unlock all attributes
  111. *
  112. * @return $this
  113. */
  114. public function unlockAttributes()
  115. {
  116. $this->_lockedAttributes = [];
  117. return $this;
  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. * Checks that model have locked attributes
  130. *
  131. * @return boolean
  132. */
  133. public function hasLockedAttributes()
  134. {
  135. return !empty($this->_lockedAttributes);
  136. }
  137. /**
  138. * Retrieve locked attributes
  139. *
  140. * @param mixed $attributeCode
  141. * @return boolean
  142. */
  143. public function isLockedAttribute($attributeCode)
  144. {
  145. return isset($this->_lockedAttributes[$attributeCode]);
  146. }
  147. /**
  148. * Overwrite data in the object.
  149. *
  150. * The $key can be string or array.
  151. * If $key is string, the attribute value will be overwritten by $value
  152. *
  153. * If $key is an array, it will overwrite all the data in the object.
  154. *
  155. * $isChanged will specify if the object needs to be saved after an update.
  156. *
  157. * @param string|array $key
  158. * @param mixed $value
  159. * @return \Magento\Framework\DataObject
  160. */
  161. public function setData($key, $value = null)
  162. {
  163. if ($this->hasLockedAttributes()) {
  164. if (is_array($key)) {
  165. foreach ($this->getLockedAttributes() as $attribute) {
  166. if (isset($key[$attribute])) {
  167. unset($key[$attribute]);
  168. }
  169. }
  170. } elseif ($this->isLockedAttribute($key)) {
  171. return $this;
  172. }
  173. } elseif ($this->isReadonly()) {
  174. return $this;
  175. }
  176. return parent::setData($key, $value);
  177. }
  178. /**
  179. * Unset data from the object.
  180. *
  181. * The $key can be a string only. Array will be ignored.
  182. *
  183. * $isChanged will specify if the object needs to be saved after an update.
  184. *
  185. * @param string $key
  186. * @return $this
  187. */
  188. public function unsetData($key = null)
  189. {
  190. if ($key !== null && $this->isLockedAttribute($key) || $this->isReadonly()) {
  191. return $this;
  192. }
  193. return parent::unsetData($key);
  194. }
  195. /**
  196. * Get collection instance
  197. *
  198. * @return \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection
  199. */
  200. public function getResourceCollection()
  201. {
  202. $collection = parent::getResourceCollection()->setStoreId($this->getStoreId());
  203. return $collection;
  204. }
  205. /**
  206. * Load entity by attribute
  207. *
  208. * @param \Magento\Eav\Model\Entity\Attribute\AttributeInterface|integer|string|array $attribute
  209. * @param null|string|array $value
  210. * @param string $additionalAttributes
  211. * @return bool|\Magento\Catalog\Model\AbstractModel
  212. */
  213. public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
  214. {
  215. $collection = $this->getResourceCollection()->addAttributeToSelect(
  216. $additionalAttributes
  217. )->addAttributeToFilter(
  218. $attribute,
  219. $value
  220. )->setPage(
  221. 1,
  222. 1
  223. );
  224. foreach ($collection as $object) {
  225. return $object;
  226. }
  227. return false;
  228. }
  229. /**
  230. * Retrieve sore object
  231. *
  232. * @return \Magento\Store\Model\Store
  233. */
  234. public function getStore()
  235. {
  236. return $this->_storeManager->getStore($this->getStoreId());
  237. }
  238. /**
  239. * Retrieve all store ids of object current website
  240. *
  241. * @return array
  242. */
  243. public function getWebsiteStoreIds()
  244. {
  245. return $this->getStore()->getWebsite()->getStoreIds(true);
  246. }
  247. /**
  248. * Adding attribute code and value to default value registry
  249. *
  250. * Default value existing is flag for using store value in data
  251. *
  252. * @param string $attributeCode
  253. * @param mixed $value
  254. * @return $this
  255. */
  256. public function setAttributeDefaultValue($attributeCode, $value)
  257. {
  258. $this->_defaultValues[$attributeCode] = $value;
  259. return $this;
  260. }
  261. /**
  262. * Retrieve default value for attribute code
  263. *
  264. * @param string $attributeCode
  265. * @return array|boolean
  266. */
  267. public function getAttributeDefaultValue($attributeCode)
  268. {
  269. return array_key_exists($attributeCode, $this->_defaultValues) ? $this->_defaultValues[$attributeCode] : false;
  270. }
  271. /**
  272. * Set attribute code flag if attribute has value in current store and does not use
  273. * value of default store as value
  274. *
  275. * @param string $attributeCode
  276. * @return $this
  277. */
  278. public function setExistsStoreValueFlag($attributeCode)
  279. {
  280. $this->_storeValuesFlags[$attributeCode] = true;
  281. return $this;
  282. }
  283. /**
  284. * Check if object attribute has value in current store
  285. *
  286. * @param string $attributeCode
  287. * @return bool
  288. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  289. */
  290. public function getExistsStoreValueFlag($attributeCode)
  291. {
  292. return array_key_exists($attributeCode, $this->_storeValuesFlags);
  293. }
  294. /**
  295. * Before save unlock attributes
  296. *
  297. * @return \Magento\Catalog\Model\AbstractModel
  298. */
  299. public function beforeSave()
  300. {
  301. $this->unlockAttributes();
  302. return parent::beforeSave();
  303. }
  304. /**
  305. * Checks model is deletable
  306. *
  307. * @return boolean
  308. */
  309. public function isDeleteable()
  310. {
  311. return $this->_isDeleteable;
  312. }
  313. /**
  314. * Set is deletable flag
  315. *
  316. * @param boolean $value
  317. * @return $this
  318. */
  319. public function setIsDeleteable($value)
  320. {
  321. $this->_isDeleteable = (bool)$value;
  322. return $this;
  323. }
  324. /**
  325. * Checks model is deletable
  326. *
  327. * @return boolean
  328. */
  329. public function isReadonly()
  330. {
  331. return $this->_isReadonly;
  332. }
  333. /**
  334. * Set is deletable flag
  335. *
  336. * @param boolean $value
  337. * @return $this
  338. */
  339. public function setIsReadonly($value)
  340. {
  341. $this->_isReadonly = (bool)$value;
  342. return $this;
  343. }
  344. }