<?php
namespace App\Controller;
use App\Model\ProductCategory;
use Exception;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Page;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ProductCategoryController extends DefaultFrontendController
{
/**
* @Template
* @param Request $request
* @return array
* @throws Exception
*
*/
public function singleAction(Request $request): array
{
$relatedCategoryObject = $this->getDocumentEditable('relation', 'productCategory')->getElement();
$this->addAreaBrickData('faq-brick', [
/** @see Relation::getElement() can also return `false`; ** SMACK **, bad Pimcore! */
'faqs' => $relatedCategoryObject ? $relatedCategoryObject->getFaqs() : null,
'faqs_source' => "Product Categorie",
]);
$parentPageController = $this->document->getParent()->getController();
return $this->data($request, [
'layout' => $parentPageController == "App\Controller\ProductCategoryController::singleAction" ? "sub" :"main",
'productCategory' => $relatedCategoryObject,
'childCategoriesObjects' =>$this->getChildCategoriesObjects(),
'childCategories' => $this->getChildCategories(),
'childProducts' => $this->getChildProducts(),
/** @see Relation::getElement() can also return `false`; ** SMACK **, bad Pimcore! */
'overrideImage' => $relatedCategoryObject ? $relatedCategoryObject->getThumbnailImage() : null,
/** @see Relation::getElement() can also return `false`; ** SMACK **, bad Pimcore! */
'productCategoryProperties' => $relatedCategoryObject ? $relatedCategoryObject?->formatPropertyItems() : null,
]);
}
/**
* @Template
* @param Request $request
* @return array
* @throws Exception
*/
public function archiveAction(Request $request): array
{
return $this->data($request);
}
private function getChildCategoriesObjects()
{
$return = [];
foreach ($this->document->getChildren() as $childPage) {
if ("App\Controller\ProductCategoryController::singleAction" == $childPage->getObjectVars()['controller']) {
/** @var Page $childDocument */
$childDocument = Document::getById($childPage->getId());
if ($relatedCategoryObject = $childDocument->getEditable('productCategory')?->getElement()) {
$return[] = $relatedCategoryObject;
}
}
}
return $return;
}
private function getChildCategories()
{
foreach ($this->document->getChildren() as $childPage) {
if ("App\Controller\ProductCategoryController::singleAction" == $childPage->getObjectVars()['controller']) {
$return[] = $childPage;
}
}
return $return ?? false;
}
private function getChildProducts()
{
foreach ($this->document->getChildren() as $childPage) {
if ("App\Controller\ProductController::singleAction" == $childPage->getObjectVars()['controller']) {
$return[] = $childPage;
}
}
return $return ?? false;
}
}