/app/code/Magento/Catalog/Model/AbstractModel.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 427 lines · 184 code · 37 blank · 206 comment · 10 complexity · 72493f980d8e0429be5a336fec9c15b7 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. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  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|null
  22. */
  23. protected $_defaultValues;
  24. /**
  25. * This array contains codes of attributes which have value in current store
  26. *
  27. * @var array|null
  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. * @var \Magento\Catalog\Model\Attribute\ScopeOverriddenValue
  56. */
  57. private $scopeOverriddenValue;
  58. /**
  59. * @param \Magento\Framework\Model\Context $context
  60. * @param \Magento\Framework\Registry $registry
  61. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  62. * @param AttributeValueFactory $customAttributeFactory
  63. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  64. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  65. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  66. * @param array $data
  67. */
  68. public function __construct(
  69. \Magento\Framework\Model\Context $context,
  70. \Magento\Framework\Registry $registry,
  71. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  72. AttributeValueFactory $customAttributeFactory,
  73. \Magento\Store\Model\StoreManagerInterface $storeManager,
  74. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  75. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  76. array $data = []
  77. ) {
  78. $this->_storeManager = $storeManager;
  79. parent::__construct(
  80. $context,
  81. $registry,
  82. $extensionFactory,
  83. $customAttributeFactory,
  84. $resource,
  85. $resourceCollection,
  86. $data
  87. );
  88. }
  89. /**
  90. * Lock attribute
  91. *
  92. * @param string $attributeCode
  93. * @return $this
  94. */
  95. public function lockAttribute($attributeCode)
  96. {
  97. $this->_lockedAttributes[$attributeCode] = true;
  98. return $this;
  99. }
  100. /**
  101. * Unlock attribute
  102. *
  103. * @param string $attributeCode
  104. * @return $this
  105. */
  106. public function unlockAttribute($attributeCode)
  107. {
  108. if ($this->isLockedAttribute($attributeCode)) {
  109. unset($this->_lockedAttributes[$attributeCode]);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Unlock all attributes
  115. *
  116. * @return $this
  117. */
  118. public function unlockAttributes()
  119. {
  120. $this->_lockedAttributes = [];
  121. return $this;
  122. }
  123. /**
  124. * Retrieve locked attributes
  125. *
  126. * @return array
  127. */
  128. public function getLockedAttributes()
  129. {
  130. return array_keys($this->_lockedAttributes);
  131. }
  132. /**
  133. * Checks that model have locked attributes
  134. *
  135. * @return boolean
  136. */
  137. public function hasLockedAttributes()
  138. {
  139. return !empty($this->_lockedAttributes);
  140. }
  141. /**
  142. * Retrieve locked attributes
  143. *
  144. * @param mixed $attributeCode
  145. * @return boolean
  146. */
  147. public function isLockedAttribute($attributeCode)
  148. {
  149. return isset($this->_lockedAttributes[$attributeCode]);
  150. }
  151. /**
  152. * Overwrite data in the object.
  153. *
  154. * The $key can be string or array.
  155. * If $key is string, the attribute value will be overwritten by $value
  156. *
  157. * If $key is an array, it will overwrite all the data in the object.
  158. *
  159. * $isChanged will specify if the object needs to be saved after an update.
  160. *
  161. * @param string|array $key
  162. * @param mixed $value
  163. * @return \Magento\Framework\DataObject
  164. */
  165. public function setData($key, $value = null)
  166. {
  167. if ($this->hasLockedAttributes()) {
  168. if (is_array($key)) {
  169. foreach ($this->getLockedAttributes() as $attribute) {
  170. if (isset($key[$attribute])) {
  171. unset($key[$attribute]);
  172. }
  173. }
  174. } elseif ($this->isLockedAttribute($key)) {
  175. return $this;
  176. }
  177. } elseif ($this->isReadonly()) {
  178. return $this;
  179. }
  180. return parent::setData($key, $value);
  181. }
  182. /**
  183. * Unset data from the object.
  184. *
  185. * The $key can be a string only. Array will be ignored.
  186. *
  187. * $isChanged will specify if the object needs to be saved after an update.
  188. *
  189. * @param string $key
  190. * @return $this
  191. */
  192. public function unsetData($key = null)
  193. {
  194. if ($key !== null && $this->isLockedAttribute($key) || $this->isReadonly()) {
  195. return $this;
  196. }
  197. return parent::unsetData($key);
  198. }
  199. /**
  200. * Get collection instance
  201. *
  202. * @return \Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection
  203. */
  204. public function getResourceCollection()
  205. {
  206. $collection = parent::getResourceCollection()->setStoreId($this->getStoreId());
  207. return $collection;
  208. }
  209. /**
  210. * Load entity by attribute
  211. *
  212. * @param \Magento\Eav\Model\Entity\Attribute\AttributeInterface|integer|string|array $attribute
  213. * @param null|string|array $value
  214. * @param string $additionalAttributes
  215. * @return bool|\Magento\Catalog\Model\AbstractModel
  216. */
  217. public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
  218. {
  219. $collection = $this->getResourceCollection()->addAttributeToSelect(
  220. $additionalAttributes
  221. )->addAttributeToFilter(
  222. $attribute,
  223. $value
  224. )->setPage(
  225. 1,
  226. 1
  227. );
  228. foreach ($collection as $object) {
  229. return $object;
  230. }
  231. return false;
  232. }
  233. /**
  234. * Retrieve sore object
  235. *
  236. * @return \Magento\Store\Model\Store
  237. */
  238. public function getStore()
  239. {
  240. return $this->_storeManager->getStore($this->getStoreId());
  241. }
  242. /**
  243. * Retrieve all store ids of object current website
  244. *
  245. * @return array
  246. */
  247. public function getWebsiteStoreIds()
  248. {
  249. return $this->getStore()->getWebsite()->getStoreIds(true);
  250. }
  251. /**
  252. * Adding attribute code and value to default value registry
  253. *
  254. * Default value existing is flag for using store value in data
  255. *
  256. * @param string $attributeCode
  257. * @param mixed $value
  258. * @return $this
  259. *
  260. * @deprecated
  261. */
  262. public function setAttributeDefaultValue($attributeCode, $value)
  263. {
  264. $this->_defaultValues[$attributeCode] = $value;
  265. return $this;
  266. }
  267. /**
  268. * Get attribute scope overridden value instance
  269. *
  270. * @return \Magento\Catalog\Model\Attribute\ScopeOverriddenValue
  271. *
  272. * @deprecated
  273. */
  274. private function getAttributeScopeOverriddenValue()
  275. {
  276. if ($this->scopeOverriddenValue === null) {
  277. $this->scopeOverriddenValue = \Magento\Framework\App\ObjectManager::getInstance()
  278. ->get(\Magento\Catalog\Model\Attribute\ScopeOverriddenValue::class);
  279. }
  280. return $this->scopeOverriddenValue;
  281. }
  282. /**
  283. * Retrieve default value for attribute code
  284. *
  285. * @param string $attributeCode
  286. * @return array|boolean
  287. *
  288. * @deprecated
  289. */
  290. public function getAttributeDefaultValue($attributeCode)
  291. {
  292. if ($this->_defaultValues === null) {
  293. $entityType = [
  294. \Magento\Catalog\Model\Product::ENTITY => \Magento\Catalog\Api\Data\ProductInterface::class,
  295. \Magento\Catalog\Model\Category::ENTITY => \Magento\Catalog\Api\Data\CategoryInterface::class,
  296. ][$this->getResource()->getEntityType()->getEntityTypeCode()];
  297. $this->_defaultValues = $this->getAttributeScopeOverriddenValue()->getDefaultValues($entityType, $this);
  298. }
  299. return array_key_exists($attributeCode, $this->_defaultValues) ? $this->_defaultValues[$attributeCode] : false;
  300. }
  301. /**
  302. * Set attribute code flag if attribute has value in current store and does not use
  303. * value of default store as value
  304. *
  305. * @param string $attributeCode
  306. * @return $this
  307. *
  308. * @deprecated
  309. */
  310. public function setExistsStoreValueFlag($attributeCode)
  311. {
  312. $this->_storeValuesFlags[$attributeCode] = true;
  313. return $this;
  314. }
  315. /**
  316. * Check if object attribute has value in current store
  317. *
  318. * @param string $attributeCode
  319. * @return bool
  320. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  321. *
  322. * @deprecated
  323. */
  324. public function getExistsStoreValueFlag($attributeCode)
  325. {
  326. if ($this->_storeValuesFlags === null) {
  327. $entityType = [
  328. \Magento\Catalog\Model\Product::ENTITY => \Magento\Catalog\Api\Data\ProductInterface::class,
  329. \Magento\Catalog\Model\Category::ENTITY => \Magento\Catalog\Api\Data\CategoryInterface::class,
  330. ][$this->getResource()->getEntityType()->getEntityTypeCode()];
  331. return $this->getAttributeScopeOverriddenValue()->containsValue(
  332. $entityType,
  333. $this,
  334. $attributeCode,
  335. $this->getStore()->getId()
  336. );
  337. }
  338. return array_key_exists($attributeCode, $this->_storeValuesFlags);
  339. }
  340. /**
  341. * Before save unlock attributes
  342. *
  343. * @return \Magento\Catalog\Model\AbstractModel
  344. */
  345. public function beforeSave()
  346. {
  347. $this->unlockAttributes();
  348. return parent::beforeSave();
  349. }
  350. /**
  351. * Checks model is deletable
  352. *
  353. * @return boolean
  354. */
  355. public function isDeleteable()
  356. {
  357. return $this->_isDeleteable;
  358. }
  359. /**
  360. * Set is deletable flag
  361. *
  362. * @param boolean $value
  363. * @return $this
  364. */
  365. public function setIsDeleteable($value)
  366. {
  367. $this->_isDeleteable = (bool)$value;
  368. return $this;
  369. }
  370. /**
  371. * Checks model is deletable
  372. *
  373. * @return boolean
  374. */
  375. public function isReadonly()
  376. {
  377. return $this->_isReadonly;
  378. }
  379. /**
  380. * Set is deletable flag
  381. *
  382. * @param boolean $value
  383. * @return $this
  384. */
  385. public function setIsReadonly($value)
  386. {
  387. $this->_isReadonly = (bool)$value;
  388. return $this;
  389. }
  390. }