Skip to content

Commit

Permalink
Merge pull request #581 from krispypen/feature/bringnodemenuback
Browse files Browse the repository at this point in the history
[NodeBundle] Re-add the nodemenu again and make it lazy loading
  • Loading branch information
Roderik van der Veer committed Jul 23, 2015
2 parents 8fd4193 + 8ffdd82 commit 516793f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
10 changes: 0 additions & 10 deletions UPGRADE-3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ and the -adminpwd- placeholder will be replaced by the generated password.
Note that all Kunstmaan fixtures have been relocated from the AdminBundle to the GeneratorBundle. An extra 'AdminLoginFirstTime.feature' has been added
to the admintests.

## Nodemenu parameter no longer available in twigfiles

the nodemenu variable has been removed from the renderContext, so to acces it in a twig file just add

{% set node = null %}
{% if nodetranslation is defined %}
{% set node = nodetranslation.node %}
{% endif %}
{% set nodemenu = get_node_menu(app.request.locale, node) %}

## Refactored search to seperate service

A lot of the functionality from the AbstractSearchPageController has been moved to the SearchService class. So if you have extended the AbstractSearchPageController
Expand Down
8 changes: 8 additions & 0 deletions src/Kunstmaan/NodeBundle/Controller/SlugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Kunstmaan\NodeBundle\Controller;

use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Event\Events;
use Kunstmaan\NodeBundle\Event\SlugEvent;
use Kunstmaan\NodeBundle\Helper\NodeMenu;
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -80,7 +82,12 @@ public function slugAction(Request $request, $url = null, $preview = false)
throw new AccessDeniedException('You do not have sufficient rights to access this page.');
}

/* @var AclHelper $aclHelper */
$aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
$includeOffline = $preview;
$nodeMenu = new NodeMenu($em, $securityContext, $aclHelper, $locale, $node, PermissionMap::PERMISSION_VIEW, $includeOffline);
unset($securityContext);
unset($aclHelper);

//render page
$renderContext = new RenderContext(
Expand All @@ -89,6 +96,7 @@ public function slugAction(Request $request, $url = null, $preview = false)
'slug' => $url,
'page' => $entity,
'resource' => $entity,
'nodemenu' => $nodeMenu,
)
);
if (method_exists($entity, 'getDefaultView')) {
Expand Down
51 changes: 38 additions & 13 deletions src/Kunstmaan/NodeBundle/Helper/NodeMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class NodeMenu
*/
private $nodesByInternalName = array();

/**
* @var bool
*/
private $initialized = false;

/**
* @param EntityManagerInterface $em The entity manager
* @param SecurityContextInterface $securityContext The security context
Expand Down Expand Up @@ -110,24 +115,32 @@ public function __construct(
$this->includeHiddenFromNav = $includeHiddenFromNav;
$this->permission = $permission;
$this->currentNode = $currentNode;
}

/* @var NodeRepository $repo */
$repo = $this->em->getRepository('KunstmaanNodeBundle:Node');
/**
* This method initializes the nodemenu only once, the method may be executed multiple times
*/
private function init() {
if (!$this->initialized) {
/* @var NodeRepository $repo */
$repo = $this->em->getRepository('KunstmaanNodeBundle:Node');

// Get all possible menu items in one query (also fetch offline nodes)
$nodes = $repo->getChildNodes(false, $this->lang, $permission, $this->aclHelper, $includeHiddenFromNav, true);
foreach ($nodes as $node) {
$this->allNodes[$node->getId()] = $node;
// Get all possible menu items in one query (also fetch offline nodes)
$nodes = $repo->getChildNodes(false, $this->lang, $this->permission, $this->aclHelper, $this->includeHiddenFromNav, true);
foreach ($nodes as $node) {
$this->allNodes[$node->getId()] = $node;

if ($node->getParent()) {
$this->childNodes[$node->getParent()->getId()][] = $node;
} else {
$this->childNodes[0][] = $node;
}
if ($node->getParent()) {
$this->childNodes[$node->getParent()->getId()][] = $node;
} else {
$this->childNodes[0][] = $node;
}

if ($node->getInternalName()) {
$this->nodesByInternalName[$node->getInternalName()][] = $node;
if ($node->getInternalName()) {
$this->nodesByInternalName[$node->getInternalName()][] = $node;
}
}
$this->initialized = true;
}
}

Expand All @@ -136,6 +149,7 @@ public function __construct(
*/
public function getTopNodes()
{
$this->init();
if (!is_array($this->topNodeMenuItems)) {
$this->topNodeMenuItems = array();

Expand Down Expand Up @@ -229,6 +243,7 @@ public function getActiveForDepth($depth)
*/
public function getChildren(Node $node, $includeHiddenFromNav = true)
{
$this->init();
$children = array();

if (array_key_exists($node->getId(), $this->childNodes)) {
Expand Down Expand Up @@ -260,6 +275,7 @@ public function getChildren(Node $node, $includeHiddenFromNav = true)
*/
public function getParent(Node $node)
{
$this->init();
if ($node->getParent() && array_key_exists($node->getParent()->getId(), $this->allNodes)) {
return $this->allNodes[$node->getParent()->getId()];
}
Expand Down Expand Up @@ -287,6 +303,7 @@ public function getNodeBySlug(NodeTranslation $parentNode, $slug)
*/
public function getNodeByInternalName($internalName, $parent = null, $includeOffline = null)
{
$this->init();
$resultNode = null;

if (is_null($includeOffline)) {
Expand Down Expand Up @@ -435,4 +452,12 @@ public function getActive($slug)
return false;
}

/**
* @return bool
*/
public function isInitialized()
{
return $this->initialized;
}

}

0 comments on commit 516793f

Please sign in to comment.