-
Notifications
You must be signed in to change notification settings - Fork 21
/
Item.php
196 lines (173 loc) · 5.01 KB
/
Item.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
<?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 Item
*/
class Item extends AbstractDb
{
/**
* 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);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('scandiweb_menumanager_item', 'item_id');
}
/**
* 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);
}
/**
* 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)
{
if (!$this->_isValidMenu($object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('This menu does not exists anymore.')
);
}
if (!$this->_isValidParentItem($object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Menu item can not be parent to itself.')
);
}
if (!$object->getIdentifier()) {
$object->setIdentifier('menu_' . $object->getMenuId() . '_item_' . date('Y_m_d_H_i_s'));
}
$this->_processUrlSave($object);
return parent::_beforeSave($object);
}
/**
* 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);
$select->limit(1);
return $select;
}
/**
* @param $menuId
*
* @return \Magento\Framework\DB\Select
*/
protected function _getMenuSelect($menuId)
{
$select = $this->getConnection()->select()->from(
['menus' => $this->getTable('scandiweb_menumanager_menu')]
)->where(
'menus.menu_id = ?',
$menuId
)->limit(1);
return $select;
}
/**
* @param $object
*/
protected function _processUrlSave($object)
{
switch ($object->getUrlType()) {
case 0:
default:
$object->setData('cms_page_id', null);
$object->setData('category_id', null);
break;
case 1:
$object->setData('url', null);
$object->setData('category_id', null);
break;
case 2:
$object->setData('url', null);
$object->setData('cms_page_id', null);
break;
}
}
/**
* @param $itemId
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _getItemSelect($itemId)
{
$select = $this->getConnection()->select()->from(
['items' => $this->getMainTable()]
)->where(
'items.item_id = ?',
$itemId
)->limit(1);
return $select;
}
/**
* @param $object
*
* @return bool|string
*/
protected function _isValidMenu($object)
{
if (!$menuId = $object->getMenuId()) {
return false;
}
$select = $this->_getMenuSelect($menuId);
$menu = $this->getConnection()->fetchOne($select);
return $menu ? true : false;
}
/**
* @param $object
*
* @return bool
*/
protected function _isValidParentItem($object)
{
if ($object->getId() && $object->getId() == $object->getParentId()) {
return false;
}
return true;
}
}