src/Controller/SectorController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Model\Sector;
  4. use Exception;
  5. use Pimcore\Model\DataObject\Folder;
  6. use Pimcore\Model\WebsiteSetting;
  7. use Pimcore\Tool;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. class SectorController extends DefaultFrontendController
  11. {
  12.     /**
  13.      * @Template
  14.      * @param Request $request
  15.      * @return array
  16.      * @throws Exception
  17.      */
  18.     public function singleAction(Request $request): array
  19.     {
  20.         // Check if the current page has a related page
  21.         return $this->data($request, [
  22.             'sector' => $this->getDocumentEditable('relation''sector')?->getElement() ?? null
  23.         ]);
  24.     }
  25.     /**
  26.      * @Template
  27.      * @param Request $request
  28.      * @return array
  29.      * @throws Exception
  30.      */
  31.     public function archiveAction(Request $request): array
  32.     {
  33.         /* the sector archive page lists all sectors hierachially.
  34.          * a navigation bar is present on the left which displays the parent items
  35.          * each sector displays a tile with text, if a page is connected to this sector a link is present
  36.          * if the sector has child sectors, display those in the subsequent tile, again: if a page is connected to that specific sector then a link is present.
  37.          * If No children are present the first tile is set as a large tile
  38.          */
  39.         /*
  40.          * First we find the sector folder and retrieve all children.
  41.          * Should the sectors folder be renamed then a sectorFolder option needs to be set in the website Settings.
  42.          */
  43.         $sectorFolder WebsiteSetting::getByName('sectorFolder')
  44.             ?? Folder::getByPath('/sectors/');
  45.         $sectorObjects $sectorFolder->getChildren(['object']);
  46.         /*
  47.          * Remove all non-sectors that may be accidentally be placed in the sector folder.
  48.          */
  49.         foreach ($sectorObjects as $key => $object) {
  50.             /** @var Sector $object */
  51.             if ($object->getClassName() !== "Sector") {
  52.                 unset($sectorObjects[$key]);
  53.             }
  54.         }
  55.         /*
  56.          * Now we sort the Sector Objects by Amount of children
  57.          */
  58.         usort($sectorObjects, function($a$b){
  59.             return $b->getChildAmount(['object']) > $a->getChildAmount(['object']);
  60.         });
  61.         return $this->data($request, [
  62.             'sectorObjects' => $sectorObjects,
  63.         ]);
  64.     }
  65. }