File size: 9,355 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace Mautic\CoreBundle\Menu;

use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use Symfony\Component\HttpFoundation\RequestStack;

class MenuHelper
{
    /**
     * Stores items that are assigned to another parent outside it's bundle.
     */
    private array $orphans = [];

    public function __construct(
        protected CorePermissions $security,
        protected RequestStack $requestStack,
        private CoreParametersHelper $coreParametersHelper,
        protected IntegrationHelper $integrationHelper
    ) {
    }

    /**
     * Converts menu config into something KNP menus expects.
     *
     * @param int    $depth
     * @param int    $defaultPriority
     * @param string $type
     */
    public function createMenuStructure(&$items, $depth = 0, $defaultPriority = 9999, $type = 'main'): void
    {
        foreach ($items as $k => &$i) {
            if (!is_array($i) || empty($i)) {
                continue;
            }

            // Remove the item if the checks fail
            if (false === $this->handleChecks($i)) {
                unset($items[$k]);
                continue;
            }

            // Set ID to route name
            if (!isset($i['id'])) {
                if (!empty($i['route'])) {
                    $i['id'] = $i['route'];
                } else {
                    $i['id'] = 'menu-item-'.uniqid();
                }
            }

            // Set link attributes
            if (!isset($i['linkAttributes'])) {
                $i['linkAttributes'] = [
                    'data-menu-link' => $i['id'],
                    'id'             => $i['id'],
                ];
            } elseif (!isset($i['linkAttributes']['id'])) {
                $i['linkAttributes']['id']             = $i['id'];
                $i['linkAttributes']['data-menu-link'] = $i['id'];
            } elseif (!isset($i['linkAttributes']['data-menu-link'])) {
                $i['linkAttributes']['data-menu-link'] = $i['id'];
            }

            $i['extras']          = [];
            $i['extras']['depth'] = $depth;

            // Note a divider
            if (!empty($i['divider'])) {
                $i['extras']['divider'] = true;
            }

            // Note a header
            if (!empty($i['header'])) {
                $i['extras']['header'] = $i['header'];
            }

            // Set the icon class for the menu item
            if (!empty($i['iconClass'])) {
                $i['extras']['iconClass'] = $i['iconClass'];
            }

            // Set the actual route name so that it's available to the menu template
            if (isset($i['route'])) {
                $i['extras']['routeName'] = $i['route'];
            }

            // Repeat for sub items
            if (isset($i['children'])) {
                $this->createMenuStructure($i['children'], $depth + 1, $defaultPriority);
            }

            // Determine if this item needs to be listed in a bundle outside it's own
            if (isset($i['parent'])) {
                if (!isset($this->orphans[$type])) {
                    $this->orphans[$type] = [];
                }

                if (!isset($this->orphans[$type][$i['parent']])) {
                    $this->orphans[$type][$i['parent']] = [];
                }

                $this->orphans[$type][$i['parent']][$k] = $i;

                unset($items[$k]);

            // Don't set a default priority here as it'll assume that of it's parent
            } elseif (!isset($i['priority'])) {
                // Ensure a priority for non-orphans
                $i['priority'] = $defaultPriority;
            }
        }
    }

    /**
     * Get and reset orphaned menu items.
     *
     * @param string $type
     *
     * @return mixed
     */
    public function resetOrphans($type = 'main')
    {
        $orphans              = $this->orphans[$type] ?? [];
        $this->orphans[$type] = [];

        return $orphans;
    }

    /**
     * Give orphaned menu items a home.
     *
     * @param bool $appendOrphans
     * @param int  $depth
     */
    public function placeOrphans(array &$menuItems, $appendOrphans = false, $depth = 1, $type = 'main'): void
    {
        foreach ($menuItems as $key => &$items) {
            if (isset($this->orphans[$type]) && isset($this->orphans[$type][$key])) {
                $priority = $items['priority'] ?? 9999;
                foreach ($this->orphans[$type][$key] as &$orphan) {
                    if (!isset($orphan['extras'])) {
                        $orphan['extras'] = [];
                    }
                    $orphan['extras']['depth'] = $depth;
                    if (!isset($orphan['priority'])) {
                        $orphan['priority'] = $priority;
                    }
                }

                $items['children'] =
                    (!isset($items['children']))
                    ?
                    $this->orphans[$type][$key]
                    :
                    array_merge($items['children'], $this->orphans[$type][$key]);
                unset($this->orphans[$type][$key]);
            } elseif (isset($items['children'])) {
                foreach ($items['children'] as $subItems) {
                    $this->placeOrphans($subItems, false, $depth + 1, $type);
                }
            }
        }

        // Append orphans that couldn't find a home
        if ($appendOrphans && !empty($this->orphans[$type])) {
            $menuItems            = array_merge($menuItems, $this->orphans[$type]);
            $this->orphans[$type] = [];
        }
    }

    /**
     * Sort menu items by priority.
     */
    public function sortByPriority(&$menuItems, $defaultPriority = 9999): void
    {
        foreach ($menuItems as &$items) {
            $parentPriority = $items['priority'] ?? $defaultPriority;
            if (isset($items['children'])) {
                $this->sortByPriority($items['children'], $parentPriority);
            }
        }

        uasort(
            $menuItems,
            function ($a, $b) use ($defaultPriority): int {
                $ap = (isset($a['priority']) ? (int) $a['priority'] : $defaultPriority);
                $bp = (isset($b['priority']) ? (int) $b['priority'] : $defaultPriority);

                return $bp <=> $ap;
            }
        );
    }

    /**
     * @return mixed
     */
    protected function getParameter($name)
    {
        return $this->coreParametersHelper->get($name, false);
    }

    /**
     * @param string $integrationName
     */
    protected function handleIntegrationChecks($integrationName, array $config): bool
    {
        $integration = $this->integrationHelper->getIntegrationObject($integrationName);

        if (!$integration) {
            return false;
        }

        $settings = $integration->getIntegrationSettings();

        $passChecks = true;

        foreach ($config as $key => $value) {
            switch ($key) {
                case 'enabled':
                    $passChecks = $settings->getIsPublished() == $value;
                    break;
                case 'features':
                    $supportedFeatures = $settings->getSupportedFeatures();

                    foreach ($value as $featureName) {
                        if (!in_array($featureName, $supportedFeatures)) {
                            $passChecks = false;
                            break;
                        }
                    }
                    break;
            }
        }

        return $passChecks;
    }

    /**
     * @param string $name
     * @param mixed  $value
     */
    protected function handleParametersChecks($name, $value): bool
    {
        return $this->getParameter($name) == $value;
    }

    /**
     * @param string $name
     * @param mixed  $value
     */
    protected function handleRequestChecks($name, $value): bool
    {
        return $this->requestStack->getCurrentRequest()->get($name) == $value;
    }

    /**
     * @return bool
     */
    protected function handleAccessCheck($accessLevel)
    {
        return match ($accessLevel) {
            'admin' => $this->security->isAdmin(),
            default => $this->security->isGranted($accessLevel, 'MATCH_ONE'),
        };
    }

    /**
     * Handle access check and other checks for menu items.
     *
     * @return bool Returns false if the item fails the access check or any other checks
     */
    protected function handleChecks(array $menuItem): bool
    {
        if (isset($menuItem['access']) && false === $this->handleAccessCheck($menuItem['access'])) {
            return false;
        }

        if (isset($menuItem['checks']) && is_array($menuItem['checks'])) {
            foreach ($menuItem['checks'] as $checkGroup => $checkConfig) {
                $checkMethod = 'handle'.ucfirst($checkGroup).'Checks';

                if (!method_exists($this, $checkMethod)) {
                    continue;
                }

                foreach ($checkConfig as $name => $value) {
                    if (false === $this->$checkMethod($name, $value)) {
                        return false;
                    }
                }
            }
        }

        return true;
    }
}