-
Notifications
You must be signed in to change notification settings - Fork 21
/
Menu.php
217 lines (185 loc) · 6.87 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
<?php
/**
* ScandiPWA_MenuOrganizerGraphQl
*
* @category ScandiPWA
* @package ScandiPWA_MenuOrganizer
* @author Raivis Dejus <info@scandiweb.com>
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com)
*/
declare(strict_types=1);
namespace ScandiPWA\MenuOrganizer\Model\Resolver;
use Magento\Catalog\Model\Category;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface;
use ScandiPWA\MenuOrganizer\Api\Data\ItemInterface;
use ScandiPWA\MenuOrganizer\Model\MenuFactory;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CatCollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory as ItemCollectionFactory;
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Menu as MenuResourceModel;
/**
* Class Menu
*
* @package ScandiPWA\MenumanagerGraphQl\Model\Resolver
*/
class Menu implements ResolverInterface
{
public const CATEGORY_ID_KEY = 'category_id';
public const CMS_PAGE_ID = 'cms_page_id';
/** @var MenuFactory */
protected $menuFactory;
/** @var MenuResourceModel */
protected $menuResourceModel;
/** @var ItemCollectionFactory */
protected $itemCollectionFactory;
/** @var StoreManagerInterface */
protected $storeManager;
/** @var CatCollectionFactory */
protected $catCollectionFactory;
/** @var PageCollectionFactory */
protected $pageCollectionFactory;
/** @var UrlInterface */
protected $urlBuilder;
/**
* Menu constructor.
* @param StoreManagerInterface $storeManager
* @param MenuFactory $menuFactory
* @param MenuResourceModel $menuResourceModel
* @param ItemCollectionFactory $itemCollectionFactory
* @param CatCollectionFactory $catCollectionFactory
* @param PageCollectionFactory $pageCollectionFactory
* @param UrlInterface $urlBuilder
*/
public function __construct(
StoreManagerInterface $storeManager,
MenuFactory $menuFactory,
MenuResourceModel $menuResourceModel,
ItemCollectionFactory $itemCollectionFactory,
CatCollectionFactory $catCollectionFactory,
PageCollectionFactory $pageCollectionFactory,
UrlInterface $urlBuilder
) {
$this->storeManager = $storeManager;
$this->menuFactory = $menuFactory;
$this->menuResourceModel = $menuResourceModel;
$this->itemCollectionFactory = $itemCollectionFactory;
$this->catCollectionFactory = $catCollectionFactory;
$this->pageCollectionFactory = $pageCollectionFactory;
$this->urlBuilder = $urlBuilder;
}
/**
* Menu organizer resolver (lines concerning menu id are changed from core scandipwa menumanager)
*
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
*
* @throws NoSuchEntityException
* @throws LocalizedException
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$identifier = $args['identifier'];
$menu = $this->menuFactory->create()->setStoreId(
$this->storeManager->getStore()->getId()
);
$this->menuResourceModel->load($menu, $identifier);
if ($menu->getId() === null) {
throw new \InvalidArgumentException(sprintf("Could not find menu with identifier '%s'", $identifier));
}
return array_merge(
$menu->getData(),
[
'items' => $this->getMenuItems($menu['menu_id'])
]
);
}
/**
* @param string $menuId
* @return array
* @throws LocalizedException
*/
private function getMenuItems(string $menuId): array
{
$menuItems = $this->itemCollectionFactory
->create()
->addMenuFilter($menuId)
->addStatusFilter()
->setParentIdOrder()
->setPositionOrder()
->getData();
$categoryIds = [];
$pageIds = [];
$itemsMap = [];
foreach ($menuItems as $item) {
$itemId = $item[ItemInterface::ITEM_ID];
if (isset($item[self::CATEGORY_ID_KEY])) {
$catId = $item[self::CATEGORY_ID_KEY];
if (isset($categoryIds[$catId])) {
$categoryIds[$catId][] = $itemId;
} else {
$categoryIds[$catId] = [$itemId];
}
} else if (isset($item[self::CMS_PAGE_ID])) {
$pageId = $item[self::CMS_PAGE_ID];
if (isset($pageIds[$pageId])) {
$pageIds[$pageId][] = $itemId;
} else {
$pageIds[$pageId] = [$itemId];
}
}
$itemsMap[$itemId] = $item;
}
$catCollection = $this->catCollectionFactory->create();
$categories = $catCollection
->addAttributeToSelect('*')
->addFieldToFilter('entity_id', ['in' => array_keys($categoryIds)])
->addFieldToFilter('is_active', 1)
->getItems();
foreach ($categories as $category) {
$catId = $category->getId();
$itemIds = $categoryIds[$catId];
foreach ($itemIds as $itemId) {
/** @var $category Category */
$itemsMap[$itemId]['url'] = parse_url($category->getUrl(), PHP_URL_PATH);
}
}
$pageCollection = $this->pageCollectionFactory->create();
$pages = $pageCollection
->addFieldToFilter('page_id', ['in' => array_keys($pageIds)])
->addFieldToFilter('is_active', 1)
->addStoreFilter($this->storeManager->getStore()->getId())
->getItems();
foreach ($pages as $page) {
$pageIdentifier = $page->getIdentifier();
$pageId = $page->getId();
$itemIds = $pageIds[$pageId];
foreach ($itemIds as $itemId) {
$url = $this->urlBuilder->getUrl(null, ['_direct' => $pageIdentifier]);
$itemsMap[$itemId]['url'] = parse_url($url, PHP_URL_PATH);
}
}
foreach ($itemsMap as $itemId => $item) {
// do not include items which URL can not be handled URL
if (!$item['url']) {
unset($itemsMap[$itemId]);
}
}
return array_values($itemsMap);
}
}