Block.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model;
  7. use Magento\Cms\Api\Data\BlockInterface;
  8. use Magento\Framework\DataObject\IdentityInterface;
  9. use Magento\Framework\Model\AbstractModel;
  10. /**
  11. * CMS block model
  12. *
  13. * @method Block setStoreId(array $storeId)
  14. * @method array getStoreId()
  15. */
  16. class Block extends AbstractModel implements BlockInterface, IdentityInterface
  17. {
  18. /**
  19. * CMS block cache tag
  20. */
  21. const CACHE_TAG = 'cms_b';
  22. /**#@+
  23. * Block's statuses
  24. */
  25. const STATUS_ENABLED = 1;
  26. const STATUS_DISABLED = 0;
  27. /**#@-*/
  28. /**#@-*/
  29. protected $_cacheTag = self::CACHE_TAG;
  30. /**
  31. * Prefix of model events names
  32. *
  33. * @var string
  34. */
  35. protected $_eventPrefix = 'cms_block';
  36. /**
  37. * @return void
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init(\Magento\Cms\Model\ResourceModel\Block::class);
  42. }
  43. /**
  44. * Prevent blocks recursion
  45. *
  46. * @return AbstractModel
  47. * @throws \Magento\Framework\Exception\LocalizedException
  48. */
  49. public function beforeSave()
  50. {
  51. if ($this->hasDataChanges()) {
  52. $this->setUpdateTime(null);
  53. }
  54. $needle = 'block_id="' . $this->getId() . '"';
  55. if (false == strstr($this->getContent(), $needle)) {
  56. return parent::beforeSave();
  57. }
  58. throw new \Magento\Framework\Exception\LocalizedException(
  59. __('Make sure that static block content does not reference the block itself.')
  60. );
  61. }
  62. /**
  63. * Get identities
  64. *
  65. * @return array
  66. */
  67. public function getIdentities()
  68. {
  69. return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getIdentifier()];
  70. }
  71. /**
  72. * Retrieve block id
  73. *
  74. * @return int
  75. */
  76. public function getId()
  77. {
  78. return $this->getData(self::BLOCK_ID);
  79. }
  80. /**
  81. * Retrieve block identifier
  82. *
  83. * @return string
  84. */
  85. public function getIdentifier()
  86. {
  87. return (string)$this->getData(self::IDENTIFIER);
  88. }
  89. /**
  90. * Retrieve block title
  91. *
  92. * @return string
  93. */
  94. public function getTitle()
  95. {
  96. return $this->getData(self::TITLE);
  97. }
  98. /**
  99. * Retrieve block content
  100. *
  101. * @return string
  102. */
  103. public function getContent()
  104. {
  105. return $this->getData(self::CONTENT);
  106. }
  107. /**
  108. * Retrieve block creation time
  109. *
  110. * @return string
  111. */
  112. public function getCreationTime()
  113. {
  114. return $this->getData(self::CREATION_TIME);
  115. }
  116. /**
  117. * Retrieve block update time
  118. *
  119. * @return string
  120. */
  121. public function getUpdateTime()
  122. {
  123. return $this->getData(self::UPDATE_TIME);
  124. }
  125. /**
  126. * Is active
  127. *
  128. * @return bool
  129. */
  130. public function isActive()
  131. {
  132. return (bool)$this->getData(self::IS_ACTIVE);
  133. }
  134. /**
  135. * Set ID
  136. *
  137. * @param int $id
  138. * @return BlockInterface
  139. */
  140. public function setId($id)
  141. {
  142. return $this->setData(self::BLOCK_ID, $id);
  143. }
  144. /**
  145. * Set identifier
  146. *
  147. * @param string $identifier
  148. * @return BlockInterface
  149. */
  150. public function setIdentifier($identifier)
  151. {
  152. return $this->setData(self::IDENTIFIER, $identifier);
  153. }
  154. /**
  155. * Set title
  156. *
  157. * @param string $title
  158. * @return BlockInterface
  159. */
  160. public function setTitle($title)
  161. {
  162. return $this->setData(self::TITLE, $title);
  163. }
  164. /**
  165. * Set content
  166. *
  167. * @param string $content
  168. * @return BlockInterface
  169. */
  170. public function setContent($content)
  171. {
  172. return $this->setData(self::CONTENT, $content);
  173. }
  174. /**
  175. * Set creation time
  176. *
  177. * @param string $creationTime
  178. * @return BlockInterface
  179. */
  180. public function setCreationTime($creationTime)
  181. {
  182. return $this->setData(self::CREATION_TIME, $creationTime);
  183. }
  184. /**
  185. * Set update time
  186. *
  187. * @param string $updateTime
  188. * @return BlockInterface
  189. */
  190. public function setUpdateTime($updateTime)
  191. {
  192. return $this->setData(self::UPDATE_TIME, $updateTime);
  193. }
  194. /**
  195. * Set is active
  196. *
  197. * @param bool|int $isActive
  198. * @return BlockInterface
  199. */
  200. public function setIsActive($isActive)
  201. {
  202. return $this->setData(self::IS_ACTIVE, $isActive);
  203. }
  204. /**
  205. * Receive page store ids
  206. *
  207. * @return int[]
  208. */
  209. public function getStores()
  210. {
  211. return $this->hasData('stores') ? $this->getData('stores') : $this->getData('store_id');
  212. }
  213. /**
  214. * Prepare block's statuses.
  215. *
  216. * @return array
  217. */
  218. public function getAvailableStatuses()
  219. {
  220. return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
  221. }
  222. }