Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

NodeBundle/Helper/Menu/PageMenuAdaptor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Helper\Menu;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
8
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder;
9
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem;
10
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem;
11
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper;
12
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
13
use Kunstmaan\NodeBundle\Helper\NodeMenuItem;
14
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * The Page Menu Adaptor
19
 */
20
class PageMenuAdaptor implements MenuAdaptorInterface
21
{
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $em;
26
27
    /**
28
     * @var AclNativeHelper
29
     */
30
    private $aclNativeHelper;
31
32
    /**
33
     * @var array
34
     */
35
    private $treeNodes;
36
37
    /**
38
     * @var array
39
     */
40
    private $activeNodeIds;
41
42
    /**
43
     * @var PagesConfiguration
44
     */
45
    private $pagesConfiguration;
46
47
    /**
48
     * @var DomainConfigurationInterface
49
     */
50
    private $domainConfiguration;
51
52
    /**
53
     * @param EntityManagerInterface       $em                  The entity manager
54
     * @param AclNativeHelper              $aclNativeHelper     The acl helper
55
     * @param PagesConfiguration           $pagesConfiguration
56
     * @param DomainConfigurationInterface $domainConfiguration
57
     */
58
    public function __construct(
59
        EntityManagerInterface $em,
60
        AclNativeHelper $aclNativeHelper,
61
        PagesConfiguration $pagesConfiguration,
62
        DomainConfigurationInterface $domainConfiguration
63
    ) {
64
        $this->em = $em;
65
        $this->aclNativeHelper = $aclNativeHelper;
66
        $this->pagesConfiguration = $pagesConfiguration;
67
        $this->domainConfiguration = $domainConfiguration;
68
    }
69
70
    /**
71
     * In this method you can add children for a specific parent, but also
72
     * remove and change the already created children
73
     *
74
     * @param MenuBuilder $menu      The menu builder
75
     * @param MenuItem[]  &$children The children array that may be adapted
76
     * @param MenuItem    $parent    The parent menu item
0 ignored issues
show
Should the type for parameter $parent not be null|MenuItem?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
77
     * @param Request     $request   The request
78
     */
79
    public function adaptChildren(
80
        MenuBuilder $menu,
81
        array &$children,
82
        MenuItem $parent = null,
83
        Request $request = null
84
    ) {
85
        if (null === $parent) {
86
            $menuItem = new TopMenuItem($menu);
87
            $menuItem
88
                ->setRoute('KunstmaanNodeBundle_nodes')
89
                ->setUniqueId('pages')
90
                ->setLabel('pages.title')
91
                ->setParent($parent);
92
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
93
                $menuItem->setActive(true);
94
            }
95
            $children[] = $menuItem;
96
        } elseif (strncasecmp($request->attributes->get('_route'), 'KunstmaanNodeBundle_nodes', 25) === 0) {
97
            $treeNodes = $this->getTreeNodes(
98
                $request->getLocale(),
99
                PermissionMap::PERMISSION_VIEW,
100
                $this->aclNativeHelper,
101
                true
102
            );
103
            $activeNodeIds = $this->getActiveNodeIds($request);
104
105
            if (isset($treeNodes[0]) && 'KunstmaanNodeBundle_nodes' === $parent->getRoute()) {
106
                $this->processNodes(
107
                    $menu,
108
                    $children,
109
                    $treeNodes[0],
110
                    $parent,
111
                    $activeNodeIds
112
                );
113
            } elseif ('KunstmaanNodeBundle_nodes_edit' === $parent->getRoute()) {
114
                $parentRouteParams = $parent->getRouteParams();
115
                $parent_id = $parentRouteParams['id'];
116
                if (\array_key_exists($parent_id, $treeNodes)) {
117
                    $this->processNodes(
118
                        $menu,
119
                        $children,
120
                        $treeNodes[$parent_id],
121
                        $parent,
122
                        $activeNodeIds
123
                    );
124
                }
125
            }
126
        }
127
    }
128
129
    /**
130
     * Get the list of nodes that is used in the admin menu.
131
     *
132
     * @param string          $lang
133
     * @param string          $permission
134
     * @param AclNativeHelper $aclNativeHelper
135
     * @param bool            $includeHiddenFromNav
136
     *
137
     * @return array
138
     */
139
    private function getTreeNodes(
140
        $lang,
141
        $permission,
142
        AclNativeHelper $aclNativeHelper,
143
        $includeHiddenFromNav
144
    ) {
145
        if (null === $this->treeNodes) {
146
            $repo = $this->em->getRepository(Node::class);
147
            $this->treeNodes = [];
148
149
            $rootNode = $this->domainConfiguration->getRootNode();
150
151
            // Get all nodes that should be shown in the menu
152
            $allNodes = $repo->getAllMenuNodes(
153
                $lang,
154
                $permission,
155
                $aclNativeHelper,
156
                $includeHiddenFromNav,
157
                $rootNode
158
            );
159
            foreach ($allNodes as $nodeInfo) {
160
                $refEntityName = $nodeInfo['ref_entity_name'];
161
                if ($this->pagesConfiguration->isHiddenFromTree($refEntityName)) {
162
                    continue;
163
                }
164
                $parent_id = \is_null($nodeInfo['parent']) ? 0 : $nodeInfo['parent'];
165
                unset($nodeInfo['parent']);
166
                $this->treeNodes[$parent_id][] = $nodeInfo;
167
            }
168
            unset($allNodes);
169
        }
170
171
        return $this->treeNodes;
172
    }
173
174
    /**
175
     * Get an array with the id's off all nodes in the tree that should be
176
     * expanded.
177
     *
178
     * @param $request
179
     *
180
     * @return array
181
     */
182
    private function getActiveNodeIds($request)
183
    {
184
        if ((null === $this->activeNodeIds) && strncasecmp($request->attributes->get('_route'), 'KunstmaanNodeBundle_nodes_edit', 30) === 0) {
185
            $repo = $this->em->getRepository(Node::class);
186
187
            $currentNode = $repo->findOneById($request->attributes->get('id'));
188
            $parentNodes = $repo->getAllParents($currentNode);
189
            $this->activeNodeIds = [];
190
            foreach ($parentNodes as $parentNode) {
191
                $this->activeNodeIds[] = $parentNode->getId();
192
            }
193
        }
194
195
        return \is_null($this->activeNodeIds) ? [] : $this->activeNodeIds;
196
    }
197
198
    /**
199
     * @param MenuBuilder    $menu          The menu builder
200
     * @param MenuItem[]     &$children     The children array that may be
201
     *                                      adapted
202
     * @param NodeMenuItem[] $nodes         The nodes
203
     * @param MenuItem       $parent        The parent menu item
204
     * @param array          $activeNodeIds List with id's of all nodes that
205
     *                                      should be expanded in the tree
206
     */
207
    private function processNodes(
208
        MenuBuilder $menu,
209
        array &$children,
210
        array $nodes,
211
        MenuItem $parent = null,
212
        array $activeNodeIds
213
    ) {
214
        foreach ($nodes as $child) {
215
            $menuItem = new MenuItem($menu);
216
            $refName = $child['ref_entity_name'];
217
218
            $menuItem
219
                ->setRoute('KunstmaanNodeBundle_nodes_edit')
220
                ->setRouteParams(['id' => $child['id']])
221
                ->setUniqueId('node-'.$child['id'])
222
                ->setLabel($child['title'])
223
                ->setParent($parent)
224
                ->setOffline(!$child['online'] && !$this->pagesConfiguration->isStructureNode($refName))
225
                ->setHiddenFromNav($child['hidden'])
226
                ->setFolder($this->pagesConfiguration->isStructureNode($refName))
227
                ->setRole('page')
228
                ->setWeight($child['weight'])
229
                ->addAttributes(
230
                    [
231
                        'page' => [
232
                            'class' => $refName,
233
                            'children' => $this->pagesConfiguration->getPossibleChildTypes($refName),
234
                            'icon' => $this->pagesConfiguration->getIcon($refName) ?? ($this->pagesConfiguration->isHomePage($refName) ? 'fa fa-home' : null),
235
                        ],
236
                    ]
237
                );
238
239
            if (\in_array($child['id'], $activeNodeIds, false)) {
240
                $menuItem->setActive(true);
241
            }
242
            $children[] = $menuItem;
243
        }
244
    }
245
}
246