File: /var/www/vhost/disk-apps/magento.bikenow.co/vendor/magento/module-cms/Model/ResourceModel/Block.php
<?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 = $this->_storeManager->isSingleStoreMode()
? [Store::DEFAULT_STORE_ID]
: (array)$object->getData('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'))
->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;
}
}