Skip to content

Instantly share code, notes, and snippets.

@0xPr0xy
Created October 10, 2014 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xPr0xy/4776cdcaa383b552b2f9 to your computer and use it in GitHub Desktop.
Save 0xPr0xy/4776cdcaa383b552b2f9 to your computer and use it in GitHub Desktop.
CanonicalMenuController.php
<?php
namespace BBG\UtrechtScienceParkWebsiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
/**
* A menu class that tries to abstract some menu inputs
* to be a canonical reference of a site structure
* step 1: integrate controller into an existing template
* step 2: integrate controller into a RESTful interface
*/
class CanonicalMenuController extends Controller
{
private $templating;
private $node_repository_name = "KunstmaanNodeBundle:Node";
private $node_translation_repository_name = "KunstmaanNodeBundle:NodeTranslation";
//constructing an templating engine interface
public function __construct(EngineInterface $templating = null) {
//assign a templating engine variable to the controller
$this->templating = $templating;
}
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
//when the container is set, do initialize.
$this->doInitialize();
}
public function doInitialize() {
$this->locale = 'nl';
$this->node_repository = $this->getDoctrine()
->getRepository($this->node_repository_name);
$this->node_translation_repository = $this->getDoctrine()
->getRepository($this->node_translation_repository_name);
}
//get a node translation (containing information pertaining to the node) by it's internal name
public function getNodeTranslationByInternalName( $internalName ) {
$node = $this->node_repository->findOneBy(array('internalName' => $internalName));
if(is_null($node) === false){
$nodeTranslation = $node->getNodeTranslation($this->locale, false);
}
return $nodeTranslation;
}
//get a node translation (containing information pertaining to the node) by it's internal name
public function getNodeTranslationBySlug( $slug ) {
if($slug == "homepage") {
return $this->getNodeTranslationByInternalName($slug);
}
$nodeTranslation = $this->node_translation_repository->findOneBy(array('slug' => $slug));
if(is_null($nodeTranslation) === false){
$nodeTranslation = $nodeTranslation->getNode()->getNodeTranslation($this->locale, false);
}
return $nodeTranslation;
}
//adapted from BBG/PrelumBundle DefaultController's getMenu method
public function getNodeTranslationsByParent( $parent ) {
$parentNode = $this->node_repository->findOneBy(array('internalName' => $parent));
$parentNodeChildren = $parentNode->getChildren();
$menu = array();
foreach($parentNodeChildren as $parentNodeChildKey => $parentNodeChildValue) {
if($parentNodeChildValue->isHiddenFromNav() == true ){
continue;
} elseif ( $parentNodeChildValue->isHiddenFromNav() == false ) {
$parentNodeChildTranslation = $parentNodeChildValue->getNodeTranslation($this->locale, false);
$menu[$parentNodeChildKey] = [$parentNodeChildTranslation];
}
$childNodeChildren = $parentNodeChildValue->getChildren();
foreach ($childNodeChildren as $childNodeChildKey => $childNodeChildValue) {
if( $childNodeChildValue->isHiddenFromNav() == true ) {
continue;
} elseif ( $childNodeChildValue->isHiddenFromNav() == false ){
$childNodeChildTranslation = $childNodeChildValue->getNodeTranslation($this->locale, false);
$menu[$parentNodeChildKey][] = $childNodeChildTranslation;
}
}
}
return $menu;
}
//public render response, decoupled from the index action
//use this method from a template
public function doRenderResponseAction( $parent, $templateLocation, $current ) {
return $this->templating->renderResponse(
$templateLocation,
array(
'parent' => $this->getNodeTranslationBySlug( $parent ),
'current' => $this->getNodeTranslationBySlug( $current ),
'children' => $this->getNodeTranslationsByParent($parent),
)
);
}
//an action that listens to the route defined in routing.yml of BBG\PrelumBundle
public function indexAction( $parent = 'homepage', $templateLocation = '', $current='homepage' ) {
return $this->doRenderResponseAction( $parent, $templateLocation, $current );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment