1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Model\ResourceModel;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Model\Page as CmsPage;
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\Framework\Stdlib\DateTime;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
/**
* Cms page mysql resource
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Page extends AbstractDb
{
/**
* Store model
*
* @var null|Store
*/
protected $_store = null;
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @var DateTime
*/
protected $dateTime;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var MetadataPool
*/
protected $metadataPool;
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param DateTime $dateTime
* @param EntityManager $entityManager
* @param MetadataPool $metadataPool
* @param string $connectionName
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
DateTime $dateTime,
EntityManager $entityManager,
MetadataPool $metadataPool,
$connectionName = null
) {
parent::__construct($context, $connectionName);
$this->_storeManager = $storeManager;
$this->dateTime = $dateTime;
$this->entityManager = $entityManager;
$this->metadataPool = $metadataPool;
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('cms_page', 'page_id');
}
/**
* @inheritDoc
*/
public function getConnection()
{
return $this->metadataPool->getMetadata(PageInterface::class)->getEntityConnection();
}
/**
* Process page data before saving
*
* @param AbstractModel $object
* @return $this
* @throws LocalizedException
*/
protected function _beforeSave(AbstractModel $object)
{
/*
* For two attributes which represent timestamp data in DB
* we should make converting such as:
* If they are empty we need to convert them into DB
* type NULL so in DB they will be empty and not some default value
*/
foreach (['custom_theme_from', 'custom_theme_to'] as $field) {
$value = !$object->getData($field) ? null : $this->dateTime->formatDate($object->getData($field));
$object->setData($field, $value);
}
if (!$this->isValidPageIdentifier($object)) {
throw new LocalizedException(
__(
"The page URL key can't use capital letters or disallowed symbols. "
. "Remove the letters and symbols and try again."
)
);
}
if ($this->isNumericPageIdentifier($object)) {
throw new LocalizedException(
__("The page URL key can't use only numbers. Add letters or words and try again.")
);
}
return parent::_beforeSave($object);
}
/**
* @param AbstractModel $object
* @param string $value
* @param string|null $field
* @return bool|int|string
* @throws LocalizedException
* @throws \Exception
*/
private function getPageId(AbstractModel $object, $value, $field = null)
{
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
if (!is_numeric($value) && $field === null) {
$field = 'identifier';
} elseif (!$field) {
$field = $entityMetadata->getIdentifierField();
}
$pageId = $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);
$pageId = count($result) ? $result[0] : false;
}
return $pageId;
}
/**
* Load an object
*
* @param CmsPage|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)
{
$pageId = $this->getPageId($object, $value, $field);
if ($pageId) {
$this->entityManager->load($object, $pageId);
}
return $this;
}
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @param CmsPage|AbstractModel $object
* @return Select
*/
protected function _getLoadSelect($field, $value, $object)
{
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$linkField = $entityMetadata->getLinkField();
$select = parent::_getLoadSelect($field, $value, $object);
if ($object->getStoreId()) {
$storeIds = [
Store::DEFAULT_STORE_ID,
(int)$object->getStoreId(),
];
$select->join(
['cms_page_store' => $this->getTable('cms_page_store')],
$this->getMainTable() . '.' . $linkField . ' = cms_page_store.' . $linkField,
[]
)
->where('is_active = ?', 1)
->where('cms_page_store.store_id IN (?)', $storeIds)
->order('cms_page_store.store_id DESC')
->limit(1);
}
return $select;
}
/**
* Retrieve load select with filter by identifier, store and activity
*
* @param string $identifier
* @param int|array $store
* @param int $isActive
* @return Select
*/
protected function _getLoadByIdentifierSelect($identifier, $store, $isActive = null)
{
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$linkField = $entityMetadata->getLinkField();
$select = $this->getConnection()->select()
->from(['cp' => $this->getMainTable()])
->join(
['cps' => $this->getTable('cms_page_store')],
'cp.' . $linkField . ' = cps.' . $linkField,
[]
)
->where('cp.identifier = ?', $identifier)
->where('cps.store_id IN (?)', $store);
if ($isActive !== null) {
$select->where('cp.is_active = ?', $isActive);
}
return $select;
}
/**
* Check whether page identifier is numeric
*
* @param AbstractModel $object
* @return bool
*/
protected function isNumericPageIdentifier(AbstractModel $object)
{
return preg_match('/^[0-9]+$/', $object->getData('identifier'));
}
/**
* Check whether page identifier is valid
*
* @param AbstractModel $object
* @return bool
*/
protected function isValidPageIdentifier(AbstractModel $object)
{
return preg_match('/^[a-z0-9][a-z0-9_\/-]+(\.[a-z0-9_-]+)?$/', $object->getData('identifier'));
}
/**
* Check if page identifier exist for specific store
* return page id if page exists
*
* @param string $identifier
* @param int $storeId
* @return int
*/
public function checkIdentifier($identifier, $storeId)
{
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$stores = [Store::DEFAULT_STORE_ID, $storeId];
$select = $this->_getLoadByIdentifierSelect($identifier, $stores, 1);
$select->reset(Select::COLUMNS)
->columns('cp.' . $entityMetadata->getIdentifierField())
->order('cps.store_id DESC')
->limit(1);
return $this->getConnection()->fetchOne($select);
}
/**
* Retrieves cms page title from DB by passed identifier.
*
* @param string $identifier
* @return string|false
*/
public function getCmsPageTitleByIdentifier($identifier)
{
$stores = [Store::DEFAULT_STORE_ID];
if ($this->_store) {
$stores[] = (int)$this->getStore()->getId();
}
$select = $this->_getLoadByIdentifierSelect($identifier, $stores);
$select->reset(Select::COLUMNS)
->columns('cp.title')
->order('cps.store_id DESC')
->limit(1);
return $this->getConnection()->fetchOne($select);
}
/**
* Retrieves cms page title from DB by passed id.
*
* @param string $id
* @return string|false
*/
public function getCmsPageTitleById($id)
{
$connection = $this->getConnection();
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$select = $connection->select()
->from($this->getMainTable(), 'title')
->where($entityMetadata->getIdentifierField() . ' = :page_id');
return $connection->fetchOne($select, ['page_id' => (int)$id]);
}
/**
* Retrieves cms page identifier from DB by passed id.
*
* @param string $id
* @return string|false
*/
public function getCmsPageIdentifierById($id)
{
$connection = $this->getConnection();
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$select = $connection->select()
->from($this->getMainTable(), 'identifier')
->where($entityMetadata->getIdentifierField() . ' = :page_id');
return $connection->fetchOne($select, ['page_id' => (int)$id]);
}
/**
* Get store ids to which specified item is assigned
*
* @param int $pageId
* @return array
*/
public function lookupStoreIds($pageId)
{
$connection = $this->getConnection();
$entityMetadata = $this->metadataPool->getMetadata(PageInterface::class);
$linkField = $entityMetadata->getLinkField();
$select = $connection->select()
->from(['cps' => $this->getTable('cms_page_store')], 'store_id')
->join(
['cp' => $this->getMainTable()],
'cps.' . $linkField . ' = cp.' . $linkField,
[]
)
->where('cp.' . $entityMetadata->getIdentifierField() . ' = :page_id');
return $connection->fetchCol($select, ['page_id' => (int)$pageId]);
}
/**
* Set store model
*
* @param Store $store
* @return $this
*/
public function setStore($store)
{
$this->_store = $store;
return $this;
}
/**
* Retrieve store model
*
* @return Store
*/
public function getStore()
{
return $this->_storeManager->getStore($this->_store);
}
/**
* @inheritDoc
*/
public function save(AbstractModel $object)
{
$this->entityManager->save($object);
return $this;
}
/**
* @inheritDoc
*/
public function delete(AbstractModel $object)
{
$this->entityManager->delete($object);
return $this;
}
}