Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

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

Labels
Severity

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\Entity\Node;
14
use Kunstmaan\NodeBundle\Helper\NodeMenuItem;
15
use Kunstmaan\NodeBundle\Helper\PagesConfiguration;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * The Page Menu Adaptor
20
 */
21
class PageMenuAdaptor implements MenuAdaptorInterface
22
{
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    private $em;
27
28
    /**
29
     * @var AclNativeHelper
30
     */
31
    private $aclNativeHelper;
32
33
    /**
34
     * @var array
35
     */
36
    private $treeNodes = null;
37
38
    /**
39
     * @var array
40
     */
41
    private $activeNodeIds = null;
42
43
    /**
44
     * @var PagesConfiguration
45
     */
46
    private $pagesConfiguration;
47
48
    /**
49
     * @var DomainConfigurationInterface
50
     */
51
    private $domainConfiguration;
52
53
    /**
54
     * @param EntityManagerInterface       $em                  The entity manager
55
     * @param AclNativeHelper              $aclNativeHelper     The acl helper
56
     * @param PagesConfiguration           $pagesConfiguration
57
     * @param DomainConfigurationInterface $domainConfiguration
58
     */
59
    public function __construct(
60
        EntityManagerInterface $em,
61
        AclNativeHelper $aclNativeHelper,
62
        PagesConfiguration $pagesConfiguration,
63
        DomainConfigurationInterface $domainConfiguration
64
    ) {
65
        $this->em = $em;
66
        $this->aclNativeHelper = $aclNativeHelper;
67
        $this->pagesConfiguration = $pagesConfiguration;
68
        $this->domainConfiguration = $domainConfiguration;
69
    }
70
71
    /**
72
     * In this method you can add children for a specific parent, but also
73
     * remove and change the already created children
74
     *
75
     * @param MenuBuilder $menu      The menu builder
76
     * @param MenuItem[]  &$children The children array that may be adapted
77
     * @param MenuItem    $parent    The parent menu item
78
     * @param Request     $request   The request
79
     */
80
    public function adaptChildren(
81
        MenuBuilder $menu,
82
        array &$children,
83
        MenuItem $parent = null,
84
        Request $request = null
85
    ) {
86
        if (null === $parent) {
87
            $menuItem = new TopMenuItem($menu);
88
            $menuItem
89
                ->setRoute('KunstmaanNodeBundle_nodes')
90
                ->setUniqueId('pages')
91
                ->setLabel('pages.title')
92
                ->setParent($parent);
93
            if (stripos($request->attributes->get('_route'), $menuItem->getRoute()) === 0) {
94
                $menuItem->setActive(true);
95
            }
96
            $children[] = $menuItem;
97
        } elseif (stripos($request->attributes->get('_route'), 'KunstmaanNodeBundle_nodes') === 0) {
98
            $treeNodes = $this->getTreeNodes(
99
                $request->getLocale(),
100
                PermissionMap::PERMISSION_VIEW,
101
                $this->aclNativeHelper,
102
                true
103
            );
104
            $activeNodeIds = $this->getActiveNodeIds($request);
105
106
            if (isset($treeNodes[0]) && 'KunstmaanNodeBundle_nodes' === $parent->getRoute()) {
107
                $this->processNodes(
108
                    $menu,
109
                    $children,
110
                    $treeNodes[0],
111
                    $parent,
112
                    $activeNodeIds
113
                );
114
            } elseif ('KunstmaanNodeBundle_nodes_edit' === $parent->getRoute()) {
115
                $parentRouteParams = $parent->getRouteParams();
116
                $parent_id = $parentRouteParams['id'];
117
                if (array_key_exists($parent_id, $treeNodes)) {
118
                    $this->processNodes(
119
                        $menu,
120
                        $children,
121
                        $treeNodes[$parent_id],
122
                        $parent,
123
                        $activeNodeIds
124
                    );
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * Get the list of nodes that is used in the admin menu.
132
     *
133
     * @param string          $lang
134
     * @param string          $permission
135
     * @param AclNativeHelper $aclNativeHelper
136
     * @param bool            $includeHiddenFromNav
137
     *
138
     * @return array
139
     */
140
    private function getTreeNodes(
141
        $lang,
142
        $permission,
143
        AclNativeHelper $aclNativeHelper,
144
        $includeHiddenFromNav
145
    ) {
146
        if (null === $this->treeNodes) {
147
            $repo = $this->em->getRepository('KunstmaanNodeBundle:Node');
148
            $this->treeNodes = [];
149
150
            $rootNode = $this->domainConfiguration->getRootNode();
151
152
            // Get all nodes that should be shown in the menu
153
            $allNodes = $repo->getAllMenuNodes(
154
                $lang,
155
                $permission,
156
                $aclNativeHelper,
157
                $includeHiddenFromNav,
158
                $rootNode
159
            );
160
            /** @var Node $nodeInfo */
161
            foreach ($allNodes as $nodeInfo) {
162
                $refEntityName = $nodeInfo['ref_entity_name'];
163
                if ($this->pagesConfiguration->isHiddenFromTree($refEntityName)) {
164
                    continue;
165
                }
166
                $parent_id = is_null($nodeInfo['parent']) ? 0 : $nodeInfo['parent'];
167
                unset($nodeInfo['parent']);
168
                $this->treeNodes[$parent_id][] = $nodeInfo;
169
            }
170
            unset($allNodes);
171
        }
172
173
        return $this->treeNodes;
174
    }
175
176
    /**
177
     * Get an array with the id's off all nodes in the tree that should be
178
     * expanded.
179
     *
180
     * @param $request
181
     *
182
     * @return array
183
     */
184
    private function getActiveNodeIds($request)
185
    {
186
        if (null === $this->activeNodeIds) {
187
            if (stripos($request->attributes->get('_route'), 'KunstmaanNodeBundle_nodes_edit') === 0) {
188
                $repo = $this->em->getRepository('KunstmaanNodeBundle:Node');
189
190
                $currentNode = $repo->findOneById($request->attributes->get('id'));
0 ignored issues
show
The method findOneById() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
191
                $parentNodes = $repo->getAllParents($currentNode);
192
                $this->activeNodeIds = [];
193
                foreach ($parentNodes as $parentNode) {
194
                    $this->activeNodeIds[] = $parentNode->getId();
195
                }
196
            }
197
        }
198
199
        return is_null($this->activeNodeIds) ? [] : $this->activeNodeIds;
200
    }
201
202
    /**
203
     * @param MenuBuilder    $menu          The menu builder
204
     * @param MenuItem[]     &$children     The children array that may be
205
     *                                      adapted
206
     * @param NodeMenuItem[] $nodes         The nodes
207
     * @param MenuItem       $parent        The parent menu item
208
     * @param array          $activeNodeIds List with id's of all nodes that
209
     *                                      should be expanded in the tree
210
     */
211
    private function processNodes(
212
        MenuBuilder $menu,
213
        array &$children,
214
        array $nodes,
215
        MenuItem $parent = null,
216
        array $activeNodeIds
217
    ) {
218
        foreach ($nodes as $child) {
219
            $menuItem = new MenuItem($menu);
220
            $refName = $child['ref_entity_name'];
221
222
            $menuItem
223
                ->setRoute('KunstmaanNodeBundle_nodes_edit')
224
                ->setRouteParams(['id' => $child['id']])
225
                ->setUniqueId('node-'.$child['id'])
226
                ->setLabel($child['title'])
227
                ->setParent($parent)
228
                ->setOffline(!$child['online'] && !$this->pagesConfiguration->isStructureNode($refName))
229
                ->setHiddenFromNav($child['hidden'])
230
                ->setFolder($this->pagesConfiguration->isStructureNode($refName))
231
                ->setRole('page')
232
                ->setWeight($child['weight'])
233
                ->addAttributes(
234
                    [
235
                        'page' => [
236
                            'class' => $refName,
237
                            'children' => $this->pagesConfiguration->getPossibleChildTypes($refName),
238
                            'icon' => $this->pagesConfiguration->getIcon($refName) ?? ($this->pagesConfiguration->isHomePage($refName) ? 'fa fa-home' : null),
239
                        ],
240
                    ]
241
                );
242
243
            if (\in_array($child['id'], $activeNodeIds, false)) {
244
                $menuItem->setActive(true);
245
            }
246
            $children[] = $menuItem;
247
        }
248
    }
249
}
250