src/Controller/Document/DivisionPageController.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Document;
  3. use App\Form\SearchFormType;
  4. use App\Helper\DataMapper\NavigationDataMapper;
  5. use Pimcore\Model\Document;
  6. use Pimcore\Navigation\Builder;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class DivisionPageController extends AbstractDocumentController
  10. {
  11.     protected $navigationBuilder;
  12.     public function __construct(Builder $navigationBuilder)
  13.     {
  14.         parent::__construct($navigationBuilder);
  15.     }
  16.     /**
  17.      * @param Request $request
  18.      * @return void
  19.      */
  20.     #[Template()]
  21.     public function defaultAction(Request $request)
  22.     {
  23.     }
  24.     #[Template()]
  25.     public function headerAction(Request $request)
  26.     {
  27.         parent::headerAction($request);
  28.         $this->view->mainNavigation = [];
  29.         $masterRequest $this->get('request_stack')->getMasterRequest();
  30.         $parentDocument $masterRequest->get('contentDocument');
  31.         $division $masterRequest->get('division') ?? explode('/'$masterRequest->getRequestUri())[1];
  32.         if (!$parentDocument) {
  33.             $parentDocument Document::getByPath('/'.$division.'/ErrorPage');
  34.             if (!$parentDocument) {
  35.                 $parentDocument Document::getById(1);
  36.             }
  37.         }
  38.         $menuStartDocument $parentDocument->getProperty('menu_start_page');
  39.         if ($this->isRootDocument($parentDocument)) {
  40.             $parentDocument $this->getDivisionDocumentFromRequest($masterRequest);
  41.             $menuStartDocument $this->getDivisionDocumentFromRequest($masterRequest);
  42.             $division null;
  43.             if($parentDocument) {
  44.                 $division $parentDocument->getKey();
  45.             }
  46.             $this->view->division $division;
  47.         }
  48.         $mainNavigation $this->navigationBuilder->getNavigation($parentDocument$menuStartDocument);
  49.         $navigationMapped NavigationDataMapper::list(
  50.             $mainNavigation->getPages()
  51.         )->all($request);
  52.         $this->view->mainNavigation $this->groupNavigationByProperty($navigationMapped'menu_group');
  53.         $form $this->createForm(SearchFormType::class);
  54.         $this->view->form $form->createView();
  55.         if (!$division) {
  56.             $explodedPath array_values(array_filter(explode('/'$masterRequest->getPathInfo())));
  57.             if (count($explodedPath)) {
  58.                 $division $explodedPath[0];
  59.             }
  60.         }
  61.         $this->view->resourceMenuLinks $this->getResourceLinks($division'resources');
  62.         $this->view->researchMenuLinks $this->getResourceLinks($division'research');
  63.         $this->view->supportMenuLinks $this->getResourceLinks($division'support');
  64.         $this->view->educationMenuLinks $this->getResourceLinks($division'education');
  65.         $this->view->aboutMenuLinks $this->getResourceLinks($division'about');
  66.     }
  67.     /**
  68.      * Return nested links for document with given name
  69.      *
  70.      * @param string $division
  71.      * @param string $resourceName
  72.      *
  73.      * @return array
  74.      */
  75.     private function getResourceLinks(?string $divisionstring $resourceName)
  76.     {
  77.         $resources Document\Link::getByPath('/' $division '/'.$resourceName);
  78.         if (!$resources) {
  79.             return [];
  80.         }
  81.         $links = [];
  82.         foreach ($resources->getChildren() as $child) {
  83.             if ($child instanceof Document\Page) {
  84.                 $links[] = [
  85.                     'name' => $child->getTitle(),
  86.                     'link' => $child->getHref()
  87.                 ];
  88.             }
  89.         }
  90.         return $links;
  91.     }
  92.     public function getDivisionDocumentFromRequest($request)
  93.     {
  94.         $id $request->request->get('division');
  95.         return Document::getById($id);
  96.     }
  97.     protected function isRootDocument($document)
  98.     {
  99.         return $document && $document->getId() == 1;
  100.     }
  101.     protected function groupNavigationByProperty(array $navigationstring $propertyKey)
  102.     {
  103.         return array_reduce($navigation, function ($collection$item) use ($propertyKey) {
  104.             if ($value $item[$propertyKey]) {
  105.                 $collection[$value] = $collection[$value] ?? [];
  106.                 $collection[$value][] = $item;
  107.             }
  108.             return $collection;
  109.         }, []);
  110.     }
  111. }