Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Block.php
Go to the documentation of this file.
1 <?php
7 
18 
23 class Block extends AbstractDb
24 {
30  protected $_storeManager;
31 
35  protected $entityManager;
36 
40  protected $metadataPool;
41 
49  public function __construct(
50  Context $context,
54  $connectionName = null
55  ) {
56  $this->_storeManager = $storeManager;
57  $this->entityManager = $entityManager;
58  $this->metadataPool = $metadataPool;
59  parent::__construct($context, $connectionName);
60  }
61 
67  protected function _construct()
68  {
69  $this->_init('cms_block', 'block_id');
70  }
71 
75  public function getConnection()
76  {
77  return $this->metadataPool->getMetadata(BlockInterface::class)->getEntityConnection();
78  }
79 
87  protected function _beforeSave(AbstractModel $object)
88  {
89  if (!$this->getIsUniqueBlockToStores($object)) {
90  throw new LocalizedException(
91  __('A block identifier with the same properties already exists in the selected store.')
92  );
93  }
94  return $this;
95  }
96 
105  private function getBlockId(AbstractModel $object, $value, $field = null)
106  {
107  $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
108  if (!is_numeric($value) && $field === null) {
109  $field = 'identifier';
110  } elseif (!$field) {
111  $field = $entityMetadata->getIdentifierField();
112  }
113  $entityId = $value;
114  if ($field != $entityMetadata->getIdentifierField() || $object->getStoreId()) {
115  $select = $this->_getLoadSelect($field, $value, $object);
116  $select->reset(Select::COLUMNS)
117  ->columns($this->getMainTable() . '.' . $entityMetadata->getIdentifierField())
118  ->limit(1);
119  $result = $this->getConnection()->fetchCol($select);
120  $entityId = count($result) ? $result[0] : false;
121  }
122  return $entityId;
123  }
124 
133  public function load(AbstractModel $object, $value, $field = null)
134  {
135  $blockId = $this->getBlockId($object, $value, $field);
136  if ($blockId) {
137  $this->entityManager->load($object, $blockId);
138  }
139  return $this;
140  }
141 
150  protected function _getLoadSelect($field, $value, $object)
151  {
152  $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
153  $linkField = $entityMetadata->getLinkField();
154 
155  $select = parent::_getLoadSelect($field, $value, $object);
156 
157  if ($object->getStoreId()) {
158  $stores = [(int)$object->getStoreId(), Store::DEFAULT_STORE_ID];
159 
160  $select->join(
161  ['cbs' => $this->getTable('cms_block_store')],
162  $this->getMainTable() . '.' . $linkField . ' = cbs.' . $linkField,
163  ['store_id']
164  )
165  ->where('is_active = ?', 1)
166  ->where('cbs.store_id in (?)', $stores)
167  ->order('store_id DESC')
168  ->limit(1);
169  }
170 
171  return $select;
172  }
173 
181  public function getIsUniqueBlockToStores(AbstractModel $object)
182  {
183  $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
184  $linkField = $entityMetadata->getLinkField();
185 
186  if ($this->_storeManager->isSingleStoreMode()) {
188  } else {
189  $stores = (array)$object->getData('store_id');
190  }
191 
192  $select = $this->getConnection()->select()
193  ->from(['cb' => $this->getMainTable()])
194  ->join(
195  ['cbs' => $this->getTable('cms_block_store')],
196  'cb.' . $linkField . ' = cbs.' . $linkField,
197  []
198  )
199  ->where('cb.identifier = ?', $object->getData('identifier'))
200  ->where('cbs.store_id IN (?)', $stores);
201 
202  if ($object->getId()) {
203  $select->where('cb.' . $entityMetadata->getIdentifierField() . ' <> ?', $object->getId());
204  }
205 
206  if ($this->getConnection()->fetchRow($select)) {
207  return false;
208  }
209 
210  return true;
211  }
212 
219  public function lookupStoreIds($id)
220  {
221  $connection = $this->getConnection();
222 
223  $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
224  $linkField = $entityMetadata->getLinkField();
225 
226  $select = $connection->select()
227  ->from(['cbs' => $this->getTable('cms_block_store')], 'store_id')
228  ->join(
229  ['cb' => $this->getMainTable()],
230  'cbs.' . $linkField . ' = cb.' . $linkField,
231  []
232  )
233  ->where('cb.' . $entityMetadata->getIdentifierField() . ' = :block_id');
234 
235  return $connection->fetchCol($select, ['block_id' => (int)$id]);
236  }
237 
243  public function save(AbstractModel $object)
244  {
245  $this->entityManager->save($object);
246  return $this;
247  }
248 
252  public function delete(AbstractModel $object)
253  {
254  $this->entityManager->delete($object);
255  return $this;
256  }
257 }
_getLoadSelect($field, $value, $object)
Definition: Block.php:150
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$id
Definition: fieldset.phtml:14
$storeManager
__()
Definition: __.php:13
__construct(Context $context, StoreManagerInterface $storeManager, EntityManager $entityManager, MetadataPool $metadataPool, $connectionName=null)
Definition: Block.php:49
$value
Definition: gender.phtml:16
const COLUMNS
Definition: Select.php:48
save(AbstractModel $object)
Definition: Block.php:243
getIsUniqueBlockToStores(AbstractModel $object)
Definition: Block.php:181
load(AbstractModel $object, $value, $field=null)
Definition: Block.php:133
$connection
Definition: bulk.php:13
_beforeSave(AbstractModel $object)
Definition: Block.php:87