Page.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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\PageInterface;
  8. use Magento\Cms\Helper\Page as PageHelper;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\DataObject\IdentityInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\Framework\Model\AbstractModel;
  13. /**
  14. * Cms Page Model
  15. *
  16. * @api
  17. * @method Page setStoreId(array $storeId)
  18. * @method array getStoreId()
  19. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  20. * @since 100.0.2
  21. */
  22. class Page extends AbstractModel implements PageInterface, IdentityInterface
  23. {
  24. /**
  25. * No route page id
  26. */
  27. const NOROUTE_PAGE_ID = 'no-route';
  28. /**#@+
  29. * Page's Statuses
  30. */
  31. const STATUS_ENABLED = 1;
  32. const STATUS_DISABLED = 0;
  33. /**#@-*/
  34. /**
  35. * CMS page cache tag
  36. */
  37. const CACHE_TAG = 'cms_p';
  38. /**
  39. * @var string
  40. */
  41. protected $_cacheTag = self::CACHE_TAG;
  42. /**
  43. * Prefix of model events names
  44. *
  45. * @var string
  46. */
  47. protected $_eventPrefix = 'cms_page';
  48. /**
  49. * @var ScopeConfigInterface
  50. */
  51. private $scopeConfig;
  52. /**
  53. * Initialize resource model
  54. *
  55. * @return void
  56. */
  57. protected function _construct()
  58. {
  59. $this->_init(\Magento\Cms\Model\ResourceModel\Page::class);
  60. }
  61. /**
  62. * Load object data
  63. *
  64. * @param int|null $id
  65. * @param string $field
  66. * @return $this
  67. */
  68. public function load($id, $field = null)
  69. {
  70. if ($id === null) {
  71. return $this->noRoutePage();
  72. }
  73. return parent::load($id, $field);
  74. }
  75. /**
  76. * Load No-Route Page
  77. *
  78. * @return \Magento\Cms\Model\Page
  79. */
  80. public function noRoutePage()
  81. {
  82. return $this->load(self::NOROUTE_PAGE_ID, $this->getIdFieldName());
  83. }
  84. /**
  85. * Receive page store ids
  86. *
  87. * @return int[]
  88. */
  89. public function getStores()
  90. {
  91. return $this->hasData('stores') ? $this->getData('stores') : (array)$this->getData('store_id');
  92. }
  93. /**
  94. * Check if page identifier exist for specific store
  95. * return page id if page exists
  96. *
  97. * @param string $identifier
  98. * @param int $storeId
  99. * @return int
  100. */
  101. public function checkIdentifier($identifier, $storeId)
  102. {
  103. return $this->_getResource()->checkIdentifier($identifier, $storeId);
  104. }
  105. /**
  106. * Prepare page's statuses.
  107. * Available event cms_page_get_available_statuses to customize statuses.
  108. *
  109. * @return array
  110. */
  111. public function getAvailableStatuses()
  112. {
  113. return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
  114. }
  115. /**
  116. * Get identities
  117. *
  118. * @return array
  119. */
  120. public function getIdentities()
  121. {
  122. return [self::CACHE_TAG . '_' . $this->getId()];
  123. }
  124. /**
  125. * Get ID
  126. *
  127. * @return int
  128. */
  129. public function getId()
  130. {
  131. return parent::getData(self::PAGE_ID);
  132. }
  133. /**
  134. * Get identifier
  135. *
  136. * @return string
  137. */
  138. public function getIdentifier()
  139. {
  140. return $this->getData(self::IDENTIFIER);
  141. }
  142. /**
  143. * Get title
  144. *
  145. * @return string
  146. */
  147. public function getTitle()
  148. {
  149. return $this->getData(self::TITLE);
  150. }
  151. /**
  152. * Get page layout
  153. *
  154. * @return string
  155. */
  156. public function getPageLayout()
  157. {
  158. return $this->getData(self::PAGE_LAYOUT);
  159. }
  160. /**
  161. * Get meta title
  162. *
  163. * @return string|null
  164. * @since 101.0.0
  165. */
  166. public function getMetaTitle()
  167. {
  168. return $this->getData(self::META_TITLE);
  169. }
  170. /**
  171. * Get meta keywords
  172. *
  173. * @return string
  174. */
  175. public function getMetaKeywords()
  176. {
  177. return $this->getData(self::META_KEYWORDS);
  178. }
  179. /**
  180. * Get meta description
  181. *
  182. * @return string
  183. */
  184. public function getMetaDescription()
  185. {
  186. return $this->getData(self::META_DESCRIPTION);
  187. }
  188. /**
  189. * Get content heading
  190. *
  191. * @return string
  192. */
  193. public function getContentHeading()
  194. {
  195. return $this->getData(self::CONTENT_HEADING);
  196. }
  197. /**
  198. * Get content
  199. *
  200. * @return string
  201. */
  202. public function getContent()
  203. {
  204. return $this->getData(self::CONTENT);
  205. }
  206. /**
  207. * Get creation time
  208. *
  209. * @return string
  210. */
  211. public function getCreationTime()
  212. {
  213. return $this->getData(self::CREATION_TIME);
  214. }
  215. /**
  216. * Get update time
  217. *
  218. * @return string
  219. */
  220. public function getUpdateTime()
  221. {
  222. return $this->getData(self::UPDATE_TIME);
  223. }
  224. /**
  225. * Get sort order
  226. *
  227. * @return string
  228. */
  229. public function getSortOrder()
  230. {
  231. return $this->getData(self::SORT_ORDER);
  232. }
  233. /**
  234. * Get layout update xml
  235. *
  236. * @return string
  237. */
  238. public function getLayoutUpdateXml()
  239. {
  240. return $this->getData(self::LAYOUT_UPDATE_XML);
  241. }
  242. /**
  243. * Get custom theme
  244. *
  245. * @return string
  246. */
  247. public function getCustomTheme()
  248. {
  249. return $this->getData(self::CUSTOM_THEME);
  250. }
  251. /**
  252. * Get custom root template
  253. *
  254. * @return string
  255. */
  256. public function getCustomRootTemplate()
  257. {
  258. return $this->getData(self::CUSTOM_ROOT_TEMPLATE);
  259. }
  260. /**
  261. * Get custom layout update xml
  262. *
  263. * @return string
  264. */
  265. public function getCustomLayoutUpdateXml()
  266. {
  267. return $this->getData(self::CUSTOM_LAYOUT_UPDATE_XML);
  268. }
  269. /**
  270. * Get custom theme from
  271. *
  272. * @return string
  273. */
  274. public function getCustomThemeFrom()
  275. {
  276. return $this->getData(self::CUSTOM_THEME_FROM);
  277. }
  278. /**
  279. * Get custom theme to
  280. *
  281. * @return string
  282. */
  283. public function getCustomThemeTo()
  284. {
  285. return $this->getData(self::CUSTOM_THEME_TO);
  286. }
  287. /**
  288. * Is active
  289. *
  290. * @return bool
  291. */
  292. public function isActive()
  293. {
  294. return (bool)$this->getData(self::IS_ACTIVE);
  295. }
  296. /**
  297. * Set ID
  298. *
  299. * @param int $id
  300. * @return \Magento\Cms\Api\Data\PageInterface
  301. */
  302. public function setId($id)
  303. {
  304. return $this->setData(self::PAGE_ID, $id);
  305. }
  306. /**
  307. * Set identifier
  308. *
  309. * @param string $identifier
  310. * @return \Magento\Cms\Api\Data\PageInterface
  311. */
  312. public function setIdentifier($identifier)
  313. {
  314. return $this->setData(self::IDENTIFIER, $identifier);
  315. }
  316. /**
  317. * Set title
  318. *
  319. * @param string $title
  320. * @return \Magento\Cms\Api\Data\PageInterface
  321. */
  322. public function setTitle($title)
  323. {
  324. return $this->setData(self::TITLE, $title);
  325. }
  326. /**
  327. * Set page layout
  328. *
  329. * @param string $pageLayout
  330. * @return \Magento\Cms\Api\Data\PageInterface
  331. */
  332. public function setPageLayout($pageLayout)
  333. {
  334. return $this->setData(self::PAGE_LAYOUT, $pageLayout);
  335. }
  336. /**
  337. * Set meta title
  338. *
  339. * @param string $metaTitle
  340. * @return \Magento\Cms\Api\Data\PageInterface
  341. * @since 101.0.0
  342. */
  343. public function setMetaTitle($metaTitle)
  344. {
  345. return $this->setData(self::META_TITLE, $metaTitle);
  346. }
  347. /**
  348. * Set meta keywords
  349. *
  350. * @param string $metaKeywords
  351. * @return \Magento\Cms\Api\Data\PageInterface
  352. */
  353. public function setMetaKeywords($metaKeywords)
  354. {
  355. return $this->setData(self::META_KEYWORDS, $metaKeywords);
  356. }
  357. /**
  358. * Set meta description
  359. *
  360. * @param string $metaDescription
  361. * @return \Magento\Cms\Api\Data\PageInterface
  362. */
  363. public function setMetaDescription($metaDescription)
  364. {
  365. return $this->setData(self::META_DESCRIPTION, $metaDescription);
  366. }
  367. /**
  368. * Set content heading
  369. *
  370. * @param string $contentHeading
  371. * @return \Magento\Cms\Api\Data\PageInterface
  372. */
  373. public function setContentHeading($contentHeading)
  374. {
  375. return $this->setData(self::CONTENT_HEADING, $contentHeading);
  376. }
  377. /**
  378. * Set content
  379. *
  380. * @param string $content
  381. * @return \Magento\Cms\Api\Data\PageInterface
  382. */
  383. public function setContent($content)
  384. {
  385. return $this->setData(self::CONTENT, $content);
  386. }
  387. /**
  388. * Set creation time
  389. *
  390. * @param string $creationTime
  391. * @return \Magento\Cms\Api\Data\PageInterface
  392. */
  393. public function setCreationTime($creationTime)
  394. {
  395. return $this->setData(self::CREATION_TIME, $creationTime);
  396. }
  397. /**
  398. * Set update time
  399. *
  400. * @param string $updateTime
  401. * @return \Magento\Cms\Api\Data\PageInterface
  402. */
  403. public function setUpdateTime($updateTime)
  404. {
  405. return $this->setData(self::UPDATE_TIME, $updateTime);
  406. }
  407. /**
  408. * Set sort order
  409. *
  410. * @param string $sortOrder
  411. * @return \Magento\Cms\Api\Data\PageInterface
  412. */
  413. public function setSortOrder($sortOrder)
  414. {
  415. return $this->setData(self::SORT_ORDER, $sortOrder);
  416. }
  417. /**
  418. * Set layout update xml
  419. *
  420. * @param string $layoutUpdateXml
  421. * @return \Magento\Cms\Api\Data\PageInterface
  422. */
  423. public function setLayoutUpdateXml($layoutUpdateXml)
  424. {
  425. return $this->setData(self::LAYOUT_UPDATE_XML, $layoutUpdateXml);
  426. }
  427. /**
  428. * Set custom theme
  429. *
  430. * @param string $customTheme
  431. * @return \Magento\Cms\Api\Data\PageInterface
  432. */
  433. public function setCustomTheme($customTheme)
  434. {
  435. return $this->setData(self::CUSTOM_THEME, $customTheme);
  436. }
  437. /**
  438. * Set custom root template
  439. *
  440. * @param string $customRootTemplate
  441. * @return \Magento\Cms\Api\Data\PageInterface
  442. */
  443. public function setCustomRootTemplate($customRootTemplate)
  444. {
  445. return $this->setData(self::CUSTOM_ROOT_TEMPLATE, $customRootTemplate);
  446. }
  447. /**
  448. * Set custom layout update xml
  449. *
  450. * @param string $customLayoutUpdateXml
  451. * @return \Magento\Cms\Api\Data\PageInterface
  452. */
  453. public function setCustomLayoutUpdateXml($customLayoutUpdateXml)
  454. {
  455. return $this->setData(self::CUSTOM_LAYOUT_UPDATE_XML, $customLayoutUpdateXml);
  456. }
  457. /**
  458. * Set custom theme from
  459. *
  460. * @param string $customThemeFrom
  461. * @return \Magento\Cms\Api\Data\PageInterface
  462. */
  463. public function setCustomThemeFrom($customThemeFrom)
  464. {
  465. return $this->setData(self::CUSTOM_THEME_FROM, $customThemeFrom);
  466. }
  467. /**
  468. * Set custom theme to
  469. *
  470. * @param string $customThemeTo
  471. * @return \Magento\Cms\Api\Data\PageInterface
  472. */
  473. public function setCustomThemeTo($customThemeTo)
  474. {
  475. return $this->setData(self::CUSTOM_THEME_TO, $customThemeTo);
  476. }
  477. /**
  478. * Set is active
  479. *
  480. * @param int|bool $isActive
  481. * @return \Magento\Cms\Api\Data\PageInterface
  482. */
  483. public function setIsActive($isActive)
  484. {
  485. return $this->setData(self::IS_ACTIVE, $isActive);
  486. }
  487. /**
  488. * {@inheritdoc}
  489. * @since 101.0.0
  490. */
  491. public function beforeSave()
  492. {
  493. $originalIdentifier = $this->getOrigData('identifier');
  494. $currentIdentifier = $this->getIdentifier();
  495. if ($this->hasDataChanges()) {
  496. $this->setUpdateTime(null);
  497. }
  498. if (!$this->getId() || $originalIdentifier === $currentIdentifier) {
  499. return parent::beforeSave();
  500. }
  501. switch ($originalIdentifier) {
  502. case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_NO_ROUTE_PAGE):
  503. throw new LocalizedException(
  504. __('This identifier is reserved for "CMS No Route Page" in configuration.')
  505. );
  506. case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_HOME_PAGE):
  507. throw new LocalizedException(__('This identifier is reserved for "CMS Home Page" in configuration.'));
  508. case $this->getScopeConfig()->getValue(PageHelper::XML_PATH_NO_COOKIES_PAGE):
  509. throw new LocalizedException(
  510. __('This identifier is reserved for "CMS No Cookies Page" in configuration.')
  511. );
  512. }
  513. return parent::beforeSave();
  514. }
  515. /**
  516. * @return ScopeConfigInterface
  517. */
  518. private function getScopeConfig()
  519. {
  520. if (null === $this->scopeConfig) {
  521. $this->scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(ScopeConfigInterface::class);
  522. }
  523. return $this->scopeConfig;
  524. }
  525. }