123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Cms\Model\ResourceModel;
- use Magento\Cms\Api\Data\BlockInterface;
- use Magento\Framework\DB\Select;
- use Magento\Framework\EntityManager\EntityManager;
- use Magento\Framework\EntityManager\MetadataPool;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Model\AbstractModel;
- use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
- use Magento\Framework\Model\ResourceModel\Db\Context;
- use Magento\Store\Model\Store;
- use Magento\Store\Model\StoreManagerInterface;
- /**
- * CMS block model
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Block extends AbstractDb
- {
- /**
- * Store manager
- *
- * @var StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var EntityManager
- */
- protected $entityManager;
- /**
- * @var MetadataPool
- */
- protected $metadataPool;
- /**
- * @param Context $context
- * @param StoreManagerInterface $storeManager
- * @param EntityManager $entityManager
- * @param MetadataPool $metadataPool
- * @param string $connectionName
- */
- public function __construct(
- Context $context,
- StoreManagerInterface $storeManager,
- EntityManager $entityManager,
- MetadataPool $metadataPool,
- $connectionName = null
- ) {
- $this->_storeManager = $storeManager;
- $this->entityManager = $entityManager;
- $this->metadataPool = $metadataPool;
- parent::__construct($context, $connectionName);
- }
- /**
- * Initialize resource model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('cms_block', 'block_id');
- }
- /**
- * @inheritDoc
- */
- public function getConnection()
- {
- return $this->metadataPool->getMetadata(BlockInterface::class)->getEntityConnection();
- }
- /**
- * Perform operations before object save
- *
- * @param AbstractModel $object
- * @return $this
- * @throws LocalizedException
- */
- protected function _beforeSave(AbstractModel $object)
- {
- if (!$this->getIsUniqueBlockToStores($object)) {
- throw new LocalizedException(
- __('A block identifier with the same properties already exists in the selected store.')
- );
- }
- return $this;
- }
- /**
- * Get block id.
- *
- * @param AbstractModel $object
- * @param mixed $value
- * @param string $field
- * @return bool|int|string
- * @throws LocalizedException
- * @throws \Exception
- */
- private function getBlockId(AbstractModel $object, $value, $field = null)
- {
- $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
- if (!is_numeric($value) && $field === null) {
- $field = 'identifier';
- } elseif (!$field) {
- $field = $entityMetadata->getIdentifierField();
- }
- $entityId = $value;
- 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);
- $entityId = count($result) ? $result[0] : false;
- }
- return $entityId;
- }
- /**
- * Load an object
- *
- * @param \Magento\Cms\Model\Block|AbstractModel $object
- * @param mixed $value
- * @param string $field field to load by (defaults to model id)
- * @return $this
- */
- public function load(AbstractModel $object, $value, $field = null)
- {
- $blockId = $this->getBlockId($object, $value, $field);
- if ($blockId) {
- $this->entityManager->load($object, $blockId);
- }
- return $this;
- }
- /**
- * Retrieve select object for load object data
- *
- * @param string $field
- * @param mixed $value
- * @param \Magento\Cms\Model\Block|AbstractModel $object
- * @return Select
- */
- protected function _getLoadSelect($field, $value, $object)
- {
- $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
- $linkField = $entityMetadata->getLinkField();
- $select = parent::_getLoadSelect($field, $value, $object);
- if ($object->getStoreId()) {
- $stores = [(int)$object->getStoreId(), Store::DEFAULT_STORE_ID];
- $select->join(
- ['cbs' => $this->getTable('cms_block_store')],
- $this->getMainTable() . '.' . $linkField . ' = cbs.' . $linkField,
- ['store_id']
- )
- ->where('is_active = ?', 1)
- ->where('cbs.store_id in (?)', $stores)
- ->order('store_id DESC')
- ->limit(1);
- }
- return $select;
- }
- /**
- * Check for unique of identifier of block to selected store(s).
- *
- * @param AbstractModel $object
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getIsUniqueBlockToStores(AbstractModel $object)
- {
- $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
- $linkField = $entityMetadata->getLinkField();
- $stores = (array)$object->getData('store_id');
- $isDefaultStore = $this->_storeManager->isSingleStoreMode()
- || array_search(Store::DEFAULT_STORE_ID, $stores) !== false;
- if (!$isDefaultStore) {
- $stores[] = Store::DEFAULT_STORE_ID;
- }
- $select = $this->getConnection()->select()
- ->from(['cb' => $this->getMainTable()])
- ->join(
- ['cbs' => $this->getTable('cms_block_store')],
- 'cb.' . $linkField . ' = cbs.' . $linkField,
- []
- )
- ->where('cb.identifier = ? ', $object->getData('identifier'));
- if (!$isDefaultStore) {
- $select->where('cbs.store_id IN (?)', $stores);
- }
- if ($object->getId()) {
- $select->where('cb.' . $entityMetadata->getIdentifierField() . ' <> ?', $object->getId());
- }
- if ($this->getConnection()->fetchRow($select)) {
- return false;
- }
- return true;
- }
- /**
- * Get store ids to which specified item is assigned
- *
- * @param int $id
- * @return array
- */
- public function lookupStoreIds($id)
- {
- $connection = $this->getConnection();
- $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
- $linkField = $entityMetadata->getLinkField();
- $select = $connection->select()
- ->from(['cbs' => $this->getTable('cms_block_store')], 'store_id')
- ->join(
- ['cb' => $this->getMainTable()],
- 'cbs.' . $linkField . ' = cb.' . $linkField,
- []
- )
- ->where('cb.' . $entityMetadata->getIdentifierField() . ' = :block_id');
- return $connection->fetchCol($select, ['block_id' => (int)$id]);
- }
- /**
- * Save an object.
- *
- * @param AbstractModel $object
- * @return $this
- * @throws \Exception
- */
- public function save(AbstractModel $object)
- {
- $this->entityManager->save($object);
- return $this;
- }
- /**
- * @inheritDoc
- */
- public function delete(AbstractModel $object)
- {
- $this->entityManager->delete($object);
- return $this;
- }
- }
|