<?php
namespace App\Controller;
use App\Model\Sector;
use Exception;
use Pimcore\Model\DataObject\Folder;
use Pimcore\Model\WebsiteSetting;
use Pimcore\Tool;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class SectorController extends DefaultFrontendController
{
/**
* @Template
* @param Request $request
* @return array
* @throws Exception
*/
public function singleAction(Request $request): array
{
// Check if the current page has a related page
return $this->data($request, [
'sector' => $this->getDocumentEditable('relation', 'sector')?->getElement() ?? null
]);
}
/**
* @Template
* @param Request $request
* @return array
* @throws Exception
*/
public function archiveAction(Request $request): array
{
/* the sector archive page lists all sectors hierachially.
* a navigation bar is present on the left which displays the parent items
* each sector displays a tile with text, if a page is connected to this sector a link is present
* 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.
* If No children are present the first tile is set as a large tile
*/
/*
* First we find the sector folder and retrieve all children.
* Should the sectors folder be renamed then a sectorFolder option needs to be set in the website Settings.
*/
$sectorFolder = WebsiteSetting::getByName('sectorFolder')
?? Folder::getByPath('/sectors/');
$sectorObjects = $sectorFolder->getChildren(['object']);
/*
* Remove all non-sectors that may be accidentally be placed in the sector folder.
*/
foreach ($sectorObjects as $key => $object) {
/** @var Sector $object */
if ($object->getClassName() !== "Sector") {
unset($sectorObjects[$key]);
}
}
/*
* Now we sort the Sector Objects by Amount of children
*/
usort($sectorObjects, function($a, $b){
return $b->getChildAmount(['object']) > $a->getChildAmount(['object']);
});
return $this->data($request, [
'sectorObjects' => $sectorObjects,
]);
}
}