Page.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Model\ResourceModel\Cms;
  7. use Magento\Cms\Api\Data\PageInterface;
  8. use Magento\Cms\Api\GetUtilityPageIdentifiersInterface;
  9. use Magento\Cms\Model\Page as CmsPage;
  10. use Magento\Framework\App\ObjectManager;
  11. use Magento\Framework\DB\Select;
  12. use Magento\Framework\EntityManager\EntityManager;
  13. use Magento\Framework\EntityManager\MetadataPool;
  14. use Magento\Framework\Model\AbstractModel;
  15. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  16. use Magento\Framework\Model\ResourceModel\Db\Context;
  17. /**
  18. * Sitemap cms page collection model
  19. *
  20. * @api
  21. * @since 100.0.2
  22. */
  23. class Page extends AbstractDb
  24. {
  25. /**
  26. * @var MetadataPool
  27. * @since 100.1.0
  28. */
  29. protected $metadataPool;
  30. /**
  31. * @var EntityManager
  32. * @since 100.1.0
  33. */
  34. protected $entityManager;
  35. /**
  36. * @var GetUtilityPageIdentifiersInterface
  37. */
  38. private $getUtilityPageIdentifiers;
  39. /**
  40. * @param Context $context
  41. * @param MetadataPool $metadataPool
  42. * @param EntityManager $entityManager
  43. * @param string $connectionName
  44. * @param GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers
  45. */
  46. public function __construct(
  47. Context $context,
  48. MetadataPool $metadataPool,
  49. EntityManager $entityManager,
  50. $connectionName = null,
  51. GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers = null
  52. ) {
  53. $this->metadataPool = $metadataPool;
  54. $this->entityManager = $entityManager;
  55. $this->getUtilityPageIdentifiers = $getUtilityPageIdentifiers ?:
  56. ObjectManager::getInstance()->get(GetUtilityPageIdentifiersInterface::class);
  57. parent::__construct($context, $connectionName);
  58. }
  59. /**
  60. * Init resource model (catalog/category)
  61. *
  62. * @return void
  63. */
  64. protected function _construct()
  65. {
  66. $this->_init('cms_page', 'page_id');
  67. }
  68. /**
  69. * @inheritDoc
  70. * @since 100.1.0
  71. */
  72. public function getConnection()
  73. {
  74. return $this->metadataPool->getMetadata(PageInterface::class)->getEntityConnection();
  75. }
  76. /**
  77. * Retrieve cms page collection array
  78. *
  79. * @param int $storeId
  80. * @return array
  81. */
  82. public function getCollection($storeId)
  83. {
  84. $entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
  85. $linkField = $entityMetadata->getLinkField();
  86. $select = $this->getConnection()->select()->from(
  87. ['main_table' => $this->getMainTable()],
  88. [$this->getIdFieldName(), 'url' => 'identifier', 'updated_at' => 'update_time']
  89. )->join(
  90. ['store_table' => $this->getTable('cms_page_store')],
  91. "main_table.{$linkField} = store_table.$linkField",
  92. []
  93. )->where(
  94. 'main_table.is_active = 1'
  95. )->where(
  96. 'main_table.identifier NOT IN (?)',
  97. $this->getUtilityPageIdentifiers->execute()
  98. )->where(
  99. 'store_table.store_id IN(?)',
  100. [0, $storeId]
  101. );
  102. $pages = [];
  103. $query = $this->getConnection()->query($select);
  104. while ($row = $query->fetch()) {
  105. $page = $this->_prepareObject($row);
  106. $pages[$page->getId()] = $page;
  107. }
  108. return $pages;
  109. }
  110. /**
  111. * Prepare page object
  112. *
  113. * @param array $data
  114. * @return \Magento\Framework\DataObject
  115. */
  116. protected function _prepareObject(array $data)
  117. {
  118. $object = new \Magento\Framework\DataObject();
  119. $object->setId($data[$this->getIdFieldName()]);
  120. $object->setUrl($data['url']);
  121. $object->setUpdatedAt($data['updated_at']);
  122. return $object;
  123. }
  124. /**
  125. * Load an object
  126. *
  127. * @param CmsPage|AbstractModel $object
  128. * @param mixed $value
  129. * @param string $field field to load by (defaults to model id)
  130. * @return $this
  131. * @since 100.1.0
  132. */
  133. public function load(AbstractModel $object, $value, $field = null)
  134. {
  135. $entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
  136. if (!is_numeric($value) && $field === null) {
  137. $field = 'identifier';
  138. } elseif (!$field) {
  139. $field = $entityMetadata->getIdentifierField();
  140. }
  141. $isId = true;
  142. if ($field != $entityMetadata->getIdentifierField() || $object->getStoreId()) {
  143. $select = $this->_getLoadSelect($field, $value, $object);
  144. $select->reset(Select::COLUMNS)
  145. ->columns($this->getMainTable() . '.' . $entityMetadata->getIdentifierField())
  146. ->limit(1);
  147. $result = $this->getConnection()->fetchCol($select);
  148. $value = count($result) ? $result[0] : $value;
  149. $isId = count($result);
  150. }
  151. if ($isId) {
  152. $this->entityManager->load($object, $value);
  153. }
  154. return $this;
  155. }
  156. /**
  157. * @inheritDoc
  158. * @since 100.1.0
  159. */
  160. public function save(AbstractModel $object)
  161. {
  162. if ($object->isDeleted()) {
  163. return $this->delete($object);
  164. }
  165. $this->beginTransaction();
  166. try {
  167. if (!$this->isModified($object)) {
  168. $this->processNotModifiedSave($object);
  169. $this->commit();
  170. $object->setHasDataChanges(false);
  171. return $this;
  172. }
  173. $object->validateBeforeSave();
  174. $object->beforeSave();
  175. if ($object->isSaveAllowed()) {
  176. $this->_serializeFields($object);
  177. $this->_beforeSave($object);
  178. $this->_checkUnique($object);
  179. $this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
  180. $this->entityManager->save($object);
  181. $this->unserializeFields($object);
  182. $this->processAfterSaves($object);
  183. }
  184. $this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
  185. $object->setHasDataChanges(false);
  186. } catch (\Exception $e) {
  187. $this->rollBack();
  188. $object->setHasDataChanges(true);
  189. throw $e;
  190. }
  191. return $this;
  192. }
  193. /**
  194. * @inheritDoc
  195. * @since 100.1.0
  196. */
  197. public function delete(AbstractModel $object)
  198. {
  199. $this->entityManager->delete($object);
  200. return $this;
  201. }
  202. }