<?php
namespace App\Document\Areabrick;
use Pimcore\Model\Document;
use Pimcore\Model\Document\Editable\Area\Info;
use Symfony\Component\HttpFoundation\Response;
use ToolboxBundle\Document\Areabrick\AbstractAreabrick;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\TagCategory;
class SsFilterGrid extends AbstractAreabrick
{
public function action(Document\Editable\Area\Info $info): ?Response
{
$successStories = DataObject\SuccessStories::getList([
'order' => 'desc'
]);
$allowedTags = [];
foreach ($successStories as $story) {
$tags = array_map(fn($tag) => $tag->getId(), $story->getRealTags());
$allowedTags = array_unique(array_merge($allowedTags, $tags));
}
$TagCategories = TagCategory::getList();
// build tags array with allowedTags (all tags that are linked to at least one successStory)
$tags = [];
foreach ($TagCategories as $TagCat) {
$TagCatTags = $TagCat->getTags();
$tagsArr = [];
foreach ($TagCatTags as $Tag) {
if(in_array($Tag->getId(), $allowedTags)) {
$tagsArr[$Tag->getId()] = [
"name" => $Tag->getName(),
"selected" => false,
];
}
}
$tags[$TagCat->getId()] = [
"name" => $TagCat->getName(),
"id" => $TagCat->getId(),
"tags" => $tagsArr
];
}
$info->setParams([
'tags' => $tags, // tags are set once here and re-evaluated with each filter call to ServiceController::getFilteredSuccessStories
'filters' => $tags, // filters are set once when loading the brick and track the inputs in the frontend
'allowedTags' => $allowedTags
]);
return parent::action($info); // TODO: Change the autogenerated stub
}
public function getTemplateDirectoryName():string
{
// this method is only required if your brick name (e.g. my_brick or myBrick)
// differs from the view template name (e.g. ss_filter_grid)
return 'ss_filter_grid';
}
public function getTemplate(): string
{
// this method is only required if your brick name (e.g. my_brick or myBrick)
// differs from the view template name (e.g. ss_filter_grid)
return sprintf('areas/%s/view.%s', $this->getTemplateDirectoryName(), $this->getTemplateSuffix());
}
public function getName():string
{
return 'Success Story Filter Grid';
}
public function getDescription():string
{
return 'Success Story Filter Grid';
}
public function getIcon():string
{
return '/static/areas/ss_filter_grid/icon.svg';
}
}