123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Sitemap\Model\ResourceModel\Cms;
- use Magento\Cms\Api\Data\PageInterface;
- use Magento\Cms\Api\GetUtilityPageIdentifiersInterface;
- use Magento\Cms\Model\Page as CmsPage;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\DB\Select;
- use Magento\Framework\EntityManager\EntityManager;
- use Magento\Framework\EntityManager\MetadataPool;
- use Magento\Framework\Model\AbstractModel;
- use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
- use Magento\Framework\Model\ResourceModel\Db\Context;
- /**
- * Sitemap cms page collection model
- *
- * @api
- * @since 100.0.2
- */
- class Page extends AbstractDb
- {
- /**
- * @var MetadataPool
- * @since 100.1.0
- */
- protected $metadataPool;
- /**
- * @var EntityManager
- * @since 100.1.0
- */
- protected $entityManager;
- /**
- * @var GetUtilityPageIdentifiersInterface
- */
- private $getUtilityPageIdentifiers;
- /**
- * @param Context $context
- * @param MetadataPool $metadataPool
- * @param EntityManager $entityManager
- * @param string $connectionName
- * @param GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers
- */
- public function __construct(
- Context $context,
- MetadataPool $metadataPool,
- EntityManager $entityManager,
- $connectionName = null,
- GetUtilityPageIdentifiersInterface $getUtilityPageIdentifiers = null
- ) {
- $this->metadataPool = $metadataPool;
- $this->entityManager = $entityManager;
- $this->getUtilityPageIdentifiers = $getUtilityPageIdentifiers ?:
- ObjectManager::getInstance()->get(GetUtilityPageIdentifiersInterface::class);
- parent::__construct($context, $connectionName);
- }
- /**
- * Init resource model (catalog/category)
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('cms_page', 'page_id');
- }
- /**
- * @inheritDoc
- * @since 100.1.0
- */
- public function getConnection()
- {
- return $this->metadataPool->getMetadata(PageInterface::class)->getEntityConnection();
- }
- /**
- * Retrieve cms page collection array
- *
- * @param int $storeId
- * @return array
- */
- public function getCollection($storeId)
- {
- $entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
- $linkField = $entityMetadata->getLinkField();
- $select = $this->getConnection()->select()->from(
- ['main_table' => $this->getMainTable()],
- [$this->getIdFieldName(), 'url' => 'identifier', 'updated_at' => 'update_time']
- )->join(
- ['store_table' => $this->getTable('cms_page_store')],
- "main_table.{$linkField} = store_table.$linkField",
- []
- )->where(
- 'main_table.is_active = 1'
- )->where(
- 'main_table.identifier NOT IN (?)',
- $this->getUtilityPageIdentifiers->execute()
- )->where(
- 'store_table.store_id IN(?)',
- [0, $storeId]
- );
- $pages = [];
- $query = $this->getConnection()->query($select);
- while ($row = $query->fetch()) {
- $page = $this->_prepareObject($row);
- $pages[$page->getId()] = $page;
- }
- return $pages;
- }
- /**
- * Prepare page object
- *
- * @param array $data
- * @return \Magento\Framework\DataObject
- */
- protected function _prepareObject(array $data)
- {
- $object = new \Magento\Framework\DataObject();
- $object->setId($data[$this->getIdFieldName()]);
- $object->setUrl($data['url']);
- $object->setUpdatedAt($data['updated_at']);
- return $object;
- }
- /**
- * Load an object
- *
- * @param CmsPage|AbstractModel $object
- * @param mixed $value
- * @param string $field field to load by (defaults to model id)
- * @return $this
- * @since 100.1.0
- */
- public function load(AbstractModel $object, $value, $field = null)
- {
- $entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
- if (!is_numeric($value) && $field === null) {
- $field = 'identifier';
- } elseif (!$field) {
- $field = $entityMetadata->getIdentifierField();
- }
- $isId = true;
- if ($field != $entityMetadata->getIdentifierField() || $object->getStoreId()) {
- $select = $this->_getLoadSelect($field, $value, $object);
- $select->reset(Select::COLUMNS)
- ->columns($this->getMainTable() . '.' . $entityMetadata->getIdentifierField())
- ->limit(1);
- $result = $this->getConnection()->fetchCol($select);
- $value = count($result) ? $result[0] : $value;
- $isId = count($result);
- }
- if ($isId) {
- $this->entityManager->load($object, $value);
- }
- return $this;
- }
- /**
- * @inheritDoc
- * @since 100.1.0
- */
- public function save(AbstractModel $object)
- {
- if ($object->isDeleted()) {
- return $this->delete($object);
- }
- $this->beginTransaction();
- try {
- if (!$this->isModified($object)) {
- $this->processNotModifiedSave($object);
- $this->commit();
- $object->setHasDataChanges(false);
- return $this;
- }
- $object->validateBeforeSave();
- $object->beforeSave();
- if ($object->isSaveAllowed()) {
- $this->_serializeFields($object);
- $this->_beforeSave($object);
- $this->_checkUnique($object);
- $this->objectRelationProcessor->validateDataIntegrity($this->getMainTable(), $object->getData());
- $this->entityManager->save($object);
- $this->unserializeFields($object);
- $this->processAfterSaves($object);
- }
- $this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
- $object->setHasDataChanges(false);
- } catch (\Exception $e) {
- $this->rollBack();
- $object->setHasDataChanges(true);
- throw $e;
- }
- return $this;
- }
- /**
- * @inheritDoc
- * @since 100.1.0
- */
- public function delete(AbstractModel $object)
- {
- $this->entityManager->delete($object);
- return $this;
- }
- }
|