-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathMenu.php
192 lines (167 loc) · 4.42 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
<?php
namespace Snowdog\Menu\Model;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Snowdog\Menu\Api\Data\MenuInterface;
use Snowdog\Menu\Helper\MenuHelper;
/**
* @method string getCssClass()
*/
class Menu extends AbstractModel implements MenuInterface, IdentityInterface
{
const CACHE_TAG = 'snowdog_menu_menu';
protected $_cacheTag = self::CACHE_TAG;
/**
* @var MenuHelper
*/
public $menuHelper;
public function __construct(
MenuHelper $menuHelper,
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
$this->menuHelper = $menuHelper;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
protected function _construct()
{
$this->_init(\Snowdog\Menu\Model\ResourceModel\Menu::class);
}
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
public function getStores()
{
$connection = $this->getResource()->getConnection();
$select = $connection->select()->from($this->getResource()->getTable('snowmenu_store'), ['store_id'])->where(
$this->menuHelper->getLinkField() . ' = ?',
$this->menuHelper->getLinkValue($this)
);
return $connection->fetchCol($select);
}
/**
* @return bool
*/
public function saveStores(array $stores)
{
if ($stores == $this->getStores()) {
return false;
}
$connection = $this->getResource()->getConnection();
$connection->beginTransaction();
$table = $this->getResource()->getTable('snowmenu_store');
$connection->delete($table, [$this->menuHelper->getLinkField() . ' = ?' => $this->menuHelper->getLinkValue($this)]);
foreach ($stores as $store) {
$connection->insert($table, [$this->menuHelper->getLinkField() => $this->menuHelper->getLinkValue($this), 'store_id' => $store]);
}
$connection->commit();
return true;
}
/**
* @inheritdoc
*/
public function getMenuId()
{
return $this->_getData(MenuInterface::MENU_ID);
}
/**
* @inheritdoc
*/
public function setMenuId($menuId)
{
return $this->setData(MenuInterface::MENU_ID, $menuId);
}
/**
* @inheritdoc
*/
public function getTitle()
{
return $this->_getData(MenuInterface::TITLE);
}
/**
* @inheritdoc
*/
public function setTitle($title)
{
return $this->setData(MenuInterface::TITLE, $title);
}
/**
* @inheritdoc
*/
public function getIdentifier()
{
return $this->_getData(MenuInterface::IDENTIFIER);
}
/**
* @inheritdoc
*/
public function setIdentifier($identifier)
{
return $this->setData(MenuInterface::IDENTIFIER, $identifier);
}
/**
* @inheritdoc
*/
public function getCreationTime()
{
return $this->_getData(MenuInterface::CREATION_TIME);
}
/**
* @inheritdoc
*/
public function setCssClass($cssClass)
{
return $this->setData(MenuInterface::CSS_CLASS, $cssClass);
}
/**
* @inheritdoc
*/
public function getCssClass()
{
return $this->_getData(MenuInterface::CSS_CLASS);
}
/**
* @inheritdoc
*/
public function setCreationTime($creationTime)
{
return $this->setData(MenuInterface::CREATION_TIME, $creationTime);
}
/**
* @inheritdoc
*/
public function getUpdateTime()
{
return $this->_getData(MenuInterface::UPDATE_TIME);
}
/**
* @inheritdoc
*/
public function setUpdateTime($updateTime)
{
return $this->setData(MenuInterface::UPDATE_TIME, $updateTime);
}
/**
* @inheritdoc
*/
public function getIsActive()
{
return $this->_getData(MenuInterface::IS_ACTIVE);
}
/**
* @inheritdoc
*/
public function setIsActive($isActive)
{
return $this->setData(MenuInterface::IS_ACTIVE, $isActive);
}
}