Skip to content

Instantly share code, notes, and snippets.

@0xPr0xy
Created September 5, 2014 09:05
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/8d12cd0011a7e5c0b6bf to your computer and use it in GitHub Desktop.
Save 0xPr0xy/8d12cd0011a7e5c0b6bf to your computer and use it in GitHub Desktop.
DefaultController.php
<?php
namespace BBG\PrelumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use BBG\PrelumBundle\Controller\CanonicalMenuController;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\ArrayAdapter;
class DefaultController extends Controller
{
private static $vacature_template = "BBGPrelumBundle:Page:vacature-detail.html.twig";
private static $medewerker_template = "BBGPrelumBundle:Page:medewerker-detail.html.twig";
private static $bijeenkomsten_template = "BBGPrelumBundle:Pages:EntityOverview/bijeenkomsten.html.twig";
private static $bijeenkomst_template = "BBGPrelumBundle:Page:bijeenkomst-detail.html.twig";
private static $product_template = "BBGPrelumBundle:Page:product-detail.html.twig";
private static $products_template = "BBGPrelumBundle:Pages:EntityOverview/products.html.twig";
private static $auteur_template = "BBGPrelumBundle:Page:auteur-detail.html.twig";
private static $vacature_repository_name = "BBGPrelumBundle:Vacature";
private static $medewerker_repository_name = "BBGPrelumBundle:Medewerkers";
private static $bijeenkomst_repository_name = "BBGPrelumBundle:Bijeenkomst";
private static $product_repository_name = "BBGPrelumBundle:Product";
private static $category_repository_name = "BBGPrelumBundle:ProductCategory";
private static $vakgebied_repository_name = "BBGPrelumBundle:ProductVakgebied";
private static $auteur_repository_name = "BBGPrelumBundle:Auteur";
private static $node_repository_name = "KunstmaanNodeBundle:Node";
private static $products_ajax_template = "BBGPrelumBundle:Pages:EntityOverview/product-list.html.twig";
private $vacature_repository;
private $medewerker_repository;
private $bijeenkomst_repository;
private $product_repository;
private $product_category_repository;
private $product_vakgebied_repository;
private $about_repository;
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
$this->bijeenkomst_repository = $this->getDoctrine()
->getRepository(self::$bijeenkomst_repository_name);
$this->medewerker_repository = $this->getDoctrine()
->getRepository(self::$medewerker_repository_name);
$this->vacature_repository = $this->getDoctrine()
->getRepository(self::$vacature_repository_name);
$this->product_repository = $this->getDoctrine()
->getRepository(self::$product_repository_name);
$this->auteur_repository = $this->getDoctrine()
->getRepository(self::$auteur_repository_name);
$this->product_category_repository = $this->getDoctrine()
->getRepository(self::$category_repository_name);
$this->product_vakgebied_repository = $this->getDoctrine()
->getRepository(self::$vakgebied_repository_name);
$this->node_repository = $this->getDoctrine()
->getRepository(self::$node_repository_name);
$this->about_repository = $this->getDoctrine()
->getRepository(self::$node_repository_name);
}
public function getMenu()
{
$aboutNode = $this->node_repository->findOneBy(array('internalName' => 'about'));
$children = $aboutNode->getChildren();
$menu = array();
foreach($children as $child){
if($child->isHiddenFromNav()){
continue;
}
$translation = $child->getNodeTranslation('en', false);
$menu[] = $translation;
}
//sort the menu like the overview pages
usort($menu, function($a, $b)
{
if( !is_null($a) && !is_null($b)){
return strcmp($a->getTitle(), $b->getTitle());
}
});
return $menu;
}
public function getNodeTranslation($slug) {
$canonicalMenuController = new CanonicalMenuController( );
$canonicalMenuController->setContainer( $this->container );
$canonicalMenuController->doInitialize();
$nodeTranslation = $canonicalMenuController->getNodeTranslationByInternalName( $slug );
return $nodeTranslation;
}
//for inclusion of the parent level of the 'about' menu
private function getAboutNode() {
$aboutNode = $this->node_repository->findOneBy(array('internalName' => 'about'));
$menuParent = $aboutNode->getNodeTranslation('en', false);
return $menuParent;
}
// Vacature Detail
public function vacatureAction($slug)
{
$vacature = $this->vacature_repository->findOneBySlug($slug);
return $this->render(self::$vacature_template,
array(
'slug' => 'Vacature '.$vacature->getName(),
'vacature' => $vacature,
'menu' => $this->getMenu(),
'parentNodeTranslation' => $this->getAboutNode(),
'nodetranslation' => $this->getNodeTranslation('vacatures'),
)
);
}
// Medewerker Detail
public function medewerkerAction($slug)
{
$medewerker = $this->medewerker_repository->findOneBySlug($slug);
return $this->render(self::$medewerker_template,
array(
'slug' => 'Medewerker '.$medewerker->getNaam(),
'medewerker' => $medewerker,
'menu' => $this->getMenu(),
'parentNodeTranslation' => $this->getAboutNode(),
'nodetranslation' => $this->getNodeTranslation('medewerkers'),
)
);
}
// Bijeenkomst Detail
public function bijeenkomstAction($slug)
{
$bijeenkomst = $this->bijeenkomst_repository->findOneBySlug($slug);
return $this->render(self::$bijeenkomst_template,
array(
'slug' => 'Bijeenkomst '.$bijeenkomst->getNaam(),
'bijeenkomst' => $bijeenkomst,
'nodetranslation' => $this->getNodeTranslation('bijeenkomsten'),
)
);
}
// Product Detail
public function productAction($slug)
{
$product = $this->product_repository->findOneBySlug($slug);
return $this->render(self::$product_template,
array(
'slug' => 'Product '.$product->getTitel(),
'product' => $product,
'menu' => $this->getMenu(),
'nodetranslation' => $this->getNodeTranslation('producten')
)
);
}
public function getRouteAction($internalName)
{
$node = $this->node_repository->findOneBy(array('internalName' => $internalName));
if($node){
$nodeTranslation = $node->getNodeTranslation('en',false);
if($nodeTranslation){
$nodeUrl = $nodeTranslation->getUrl();
$baseUrl = $this->getRequest()->getSchemeAndHttpHost();
if($baseUrl && $nodeUrl){
return $this->redirect($baseUrl . '/' . $nodeUrl);
}
}
}
throw new NotFoundHttpException('Pagina niet gevonden');
}
// Products sorted
public function productsAction($categorie_titel=null)
{
if($categorie_titel != null){
// AJAX POST
if($this->getRequest()->isXmlHttpRequest() && $this->getRequest()->isMethod('POST'))
{
$vakgebieden = $this->get('request')->request->get('vakgebieden');
$searchString = $this->get('request')->request->get('searchString');
$product_selection = $this->product_repository->findProductsInCategoryAndVakgebiedenAndSearchString($categorie_titel, $vakgebieden, $searchString);
return $this->render(self::$products_ajax_template,
array(
'product' => $product_selection,
'categorie_titel' => $categorie_titel
)
);
// Categorized GET
} else {
$category = $this->product_category_repository->findByTitle($categorie_titel);
$products = $this->product_repository->findByCategorie($category);
}
// Uncategorized GET
} else {
$products = $this->product_repository->findAll();
}
$pag = $this->setProductPagerfanta($products);
$pagV = $this->setProductPaginationView($products);
return $this->render(self::$products_template,
array(
'slug' => 'Producten '.$categorie_titel,
'product' => $products,
'categorie_titel' => $categorie_titel,
'pagerfanta' => $pag,
'paginationView' => $pagV,
'nodetranslation' => $this->getNodeTranslation('producten'),
'menu' => $this->getMenu(),
)
);
}
// Auteur Detail
public function auteurAction($slug)
{
if($slug != null){
$auteur = $this->auteur_repository->findOneBySlug($slug);
$categorie_titel = 'Boek';
// AJAX POST
if($this->getRequest()->isXmlHttpRequest() && $this->getRequest()->isMethod('POST'))
{
$vakgebieden = $this->get('request')->request->get('vakgebieden');
$searchString = $this->get('request')->request->get('searchString');
$product_selection = $this->product_repository->findProductsInCategoryAndVakgebiedenAndSearchStringWithAuthor($auteur->getName(), $vakgebieden, $searchString);
return $this->render(self::$products_ajax_template,
array(
'product' => $product_selection,
'categorie_titel' => $categorie_titel
)
);
// Categorized GET
} else {
$products = $auteur->getProducts()->toArray();
}
return $this->render(self::$auteur_template,
array(
'slug' => 'Auteur '.$auteur->getName(),
'auteur' => $auteur,
'product' => $products,
'categorie_titel' => $categorie_titel,
'pagerfanta' => $this->setProductPagerfanta($products),
'paginationView' => $this->setProductPaginationView($products),
'nodetranslation' => $this->getNodeTranslation('auteurs')
)
);
} else {
// should return 404 since we need the auteur
}
}
public function setGatheringPagerfanta($array) {
$parameterList = 'gathering_list';
//make a new pagination controller
$this->gatheringPagination = new PaginationController();
//pass the container of this class to the pagination controller
$this->gatheringPagination->setContainer( $this->container );
$this->gatheringPagination->setParameters( $parameterList );
$pagination = $this->gatheringPagination->setPagerfanta(
$array,
$this->get( 'request' ),
$parameterList
);
return $pagination;
}
public function setGatheringPaginationView( $array ) {
// $pagerFanta, $currentPage, $options
if( isset($this->gatheringPagination) ) {
$paginationView = $this->gatheringPagination->generateView( $this->setGatheringPagerfanta( $array ) , $this->gatheringPagination->currentPage, $this->gatheringPagination->options );
return $paginationView;
}
return null;
}
//function receives a node, which normally should be a parent node
public function goBackAction( $nodeName ) {
$nodeInstance = $this->about_repository->findOneBy( array('internalName' => $nodeName) );
$nodeTranslation = $nodeInstance->getNodeTranslation('en', false);
// var_dump($nodeTranslation);
return $this->render('BBGPrelumBundle:Layout:anchor-back-to.html.twig', array(
'node' => $nodeTranslation,
) );
}
public function setProductPagerfanta($array) {
$parameterList = 'product_list';
//make a new pagination controller
$this->productPagination = new PaginationController();
//pass the container of this class to the pagination controller
$this->productPagination->setContainer( $this->container );
$this->productPagination->setParameters( $parameterList );
$pagination = $this->productPagination->setPagerfanta(
$array,
$this->get( 'request' ),
$parameterList
);
return $pagination;
}
public function setProductPaginationView( $array ) {
// $pagerFanta, $currentPage, $options
if( isset($this->productPagination) ) {
$paginationView = $this->productPagination->generateView( $this->setProductPagerfanta( $array ) , $this->productPagination->currentPage, $this->productPagination->options );
return $paginationView;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment