-
Notifications
You must be signed in to change notification settings - Fork 21
/
Menu.php
305 lines (260 loc) · 8.8 KB
/
Menu.php
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
<?php
namespace ScandiPWA\MenuOrganizer\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
/**
* @category ScandiPWA
* @package ScandiPWA\MenuOrganizer\Model\ResourceModel
* @author Dmitrijs Sitovs <info@scandiweb.com / dmitrijssh@scandiweb.com / dsitovs@gmail.com>
* @copyright Copyright (c) 2015 Scandiweb, Ltd (http://scandiweb.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*
* Class Menu
*/
class Menu extends AbstractDb
{
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
protected $_date;
/**
* Construct
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
* @param string|null $resourcePrefix
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
$resourcePrefix = null
) {
parent::__construct($context, $resourcePrefix);
$this->_date = $date;
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('scandiweb_menumanager_menu', 'menu_id');
}
/**
* Validate data before saving
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
$this->_setStoreIds($object);
if (!$this->isValidMenuIdentifier($object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Menu identifier contains capital letters or disallowed symbols.')
);
}
if ($this->isNumericMenuIdentifier($object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Menu identifier cannot be made of only numbers.')
);
}
if (!$this->getIsUniqueMenuToStores($object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('A menu identifier with the same properties already exists in one of the selected stores.')
);
}
return parent::_beforeSave($object);
}
/**
* Check if menu identifier is unique in store(s).
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return bool
*/
public function getIsUniqueMenuToStores(\Magento\Framework\Model\AbstractModel $object)
{
$stores = (array)$object->getData('stores');
if (!$stores) {
return true;
}
$select = $this->getConnection()->select()
->from(['menu' => $this->getMainTable()])
->join(
['menu_stores' => $this->getTable('scandiweb_menumanager_menu_store')],
'menu.menu_id = menu_stores.menu_id',
[]
)
->where('menu.identifier = ?', $object->getData('identifier'));
if ($stores && !in_array(0, $stores)) {
$select->where('menu_stores.store_id IN (?)', $stores);
}
if ($object->getId()) {
$select->where('menu.menu_id <> ?', $object->getId());
}
if ($this->getConnection()->fetchOne($select)) {
return false;
}
return true;
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
*/
protected function _setStoreIds(\Magento\Framework\Model\AbstractModel $object)
{
$om = \Magento\Framework\App\ObjectManager::getInstance();
$request = $om->get('Magento\Framework\App\RequestInterface');
$menuData = $request->getParam('menu');
if ($menuData && isset($menuData['store_id'])) {
if (is_string($menuData['store_id']) || isset($menuData['store_id'][0])) {
$object->setData('stores', [0]);
} else {
$object->setData('stores', $menuData['store_id']);
}
}
}
/**
* @param \Magento\Framework\Model\AbstractModel $object
*/
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
{
$oldStores = $this->lookupStoreIds($object->getId());
$newStores = (array)$object->getStores();
$table = $this->getTable('scandiweb_menumanager_menu_store');
$insert = array_diff($newStores, $oldStores);
$delete = array_diff($oldStores, $newStores);
if ($delete) {
$where = [
'menu_id = ?' => (int) $object->getId(),
'store_id IN (?)' => $delete
];
$this->getConnection()->delete($table, $where);
}
if ($insert) {
$data = [];
foreach ($insert as $storeId) {
$data[] = [
'menu_id' => (int) $object->getId(),
'store_id' => (int) $storeId
];
}
$this->getConnection()->insertMultiple($table, $data);
}
}
/**
* Get store IDs to which menu is assigned
*
* @param int $id
* @return array
*/
public function lookupStoreIds($id)
{
$adapter = $this->getConnection();
$select = $adapter->select()
->from($this->getTable('scandiweb_menumanager_menu_store'), 'store_id')
->where('menu_id = :menu_id');
$binds = [
':menu_id' => (int) $id
];
return $adapter->fetchCol($select, $binds);
}
/**
* Load an object using 'identifier' field if there's no field specified and value is not numeric
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param mixed $value
* @param string $field
* @return $this
*/
public function load(\Magento\Framework\Model\AbstractModel $object, $value, $field = null)
{
if (!is_numeric($value) && is_null($field)) {
$field = 'identifier';
}
return parent::load($object, $value, $field);
}
/**
* Retrieve select object for load object data
*
* @param string $field
* @param mixed $value
* @param \ScandiPWA\MenuOrganizer\Model\Menu $object
* @return \Zend_Db_Select
*/
protected function _getLoadSelect($field, $value, $object)
{
$select = parent::_getLoadSelect($field, $value, $object);
if ($object->getStoreId()) {
$stores = [
(int) $object->getStoreId(),
0
];
$select->join(
['menu_store' => $this->getTable('scandiweb_menumanager_menu_store')],
$this->getMainTable() . '.menu_id = menu_store.menu_id',
['store_id']
)
->where('menu_store.store_id in (?) ', $stores)
->where('is_active = ?', 1)
->order('store_id DESC')
->limit(1);
}
return $select;
}
/**
* Perform operations after object load - add stores data
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
{
if ($object->getId()) {
$stores = $this->lookupStoreIds($object->getId());
$object->setData('store_id', $stores);
$object->setData('stores', $stores);
}
return parent::_afterLoad($object);
}
/**
* Retrieve load select with filter by identifier and activity
*
* @param string $identifier
* @param int $isActive
* @return \Magento\Framework\DB\Select
*/
protected function _getLoadByUrlKeySelect($identifier, $isActive = null)
{
$select = $this->getConnection()->select()->from(
['menus' => $this->getMainTable()]
)->where(
'menus.identifier = ?',
$identifier
);
if (!is_null($isActive)) {
$select->where('menus.is_active = ?', $isActive);
}
return $select;
}
/**
* Check whether menu identifier is numeric
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return bool
*/
protected function isNumericMenuIdentifier(\Magento\Framework\Model\AbstractModel $object)
{
return preg_match('/^[0-9]+$/', $object->getData('identifier'));
}
/**
* Check whether menu identifier is valid
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return bool
*/
protected function isValidMenuIdentifier(\Magento\Framework\Model\AbstractModel $object)
{
return preg_match('/^[a-z0-9][a-z0-9_\/-]+(\.[a-z0-9_-]+)?$/', $object->getData('identifier'));
}
}