<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\TagCategory;
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\Routing\Annotation\Route;
class ServiceController extends FrontendController
{
public function defaultAction(Request $request)
{
$slug = trim($request->get('slug'), '/');
if(str_contains($slug, 'sid-')){
$service = DataObject\ServiceClass::getById(str_replace('sid-', '', $slug), 1);
}else{
$service = DataObject\ServiceClass::getByURL($slug, 1);
}
if(is_null($service->getSnipped())){
}
$this->document->setValue('title', $service->getMetaTitle());
$this->document->setValue('description', $service->getMetaDescription());
return $this->render('digitalwandler/DocumentType/snippedPage.html.twig', ['snipped' => $service->getSnipped()->getId()]);
}
public function menuAction(Request $request)
{
$services = DataObject\ServiceClass::getList([
'order' => 'desc'
]);
foreach ($services as $service){
foreach (explode('|', $service->getTags()) as $tag){
if(isset($tags[$tag])){
$tags[$tag]++;
}else{
$tags[$tag] = 1;
}
}
}
return $this->render('digitalwandler/DocumentType/menuSnippet.html.twig', ['tags' => $tags]);
}
/**
* @Route("/api/success-stories", name="success_stories", methods="POST")
*/
public function getFilteredSuccessStories(Request $request) {
try {
$requestAsArray = $request->toArray();
} catch (JsonException $e) {
$requestAsArray = [];
}
$frontendSelectedTags = [];
foreach ($requestAsArray as $tagCategoryId => $tagCategory) {
foreach ($tagCategory["tags"] as $tagId => $tag) {
if ($tag["selected"] == true) {
$frontendSelectedTags[$tagCategoryId][] = $tagId;
}
}
}
$successStories = DataObject\SuccessStories::getList([
'order' => 'desc'
]);
// add joins to filter for tags
// one join per tagCategories that filters with IN (tags) -->
$successStories->onCreateQueryBuilder(
function (\Doctrine\DBAL\Query\QueryBuilder $qb) use ($successStories, $frontendSelectedTags) {
foreach ($frontendSelectedTags as $tagCategoryId => $tags) {
if($tags && count($tags) > 0) {
$rel = "rel" . $tagCategoryId;
$qb->join("object_2", "object_relations_2", $rel , "object_2.oo_id = $rel.src_id AND $rel.fieldname = 'realTags' AND $rel.dest_id IN(" . implode(",",$tags) . ")");
}
}
$qb->groupBy("object_2.oo_id");
}
);
$allowedTags = [];
// build filtered stories array
// also build $allowedTags while we're looping trough all filtered stories
$stories = [];
foreach ($successStories as $story) {
$allowedTags = array_unique(array_merge($allowedTags, array_map(fn($tag) => $tag->getId(), $story->getRealTags())));
$stories[] = [
"id" => $story->getId(),
"title" => $story->getTitle(),
"url" => $story->getUrl(),
"teaser" => $story->getTeaser(),
"teaserImage" => $story->getTeaserImage() ? $story->getTeaserImage()->getThumbnail('jumpstartImage')->getPath() : "",
"tags" => array_map(fn($tag) => ['id' => $tag->getId(), 'name' => $tag->getName()], $story->getRealTags())
];
}
$TagCategories = TagCategory::getList();
$tags = [];
foreach ($TagCategories as $TagCat) {
$TagCatTags = $TagCat->getTags();
$tagsArr = [];
foreach ($TagCatTags as $Tag) {
if(in_array($Tag->getId(), $allowedTags)) {
$selected = isset($frontendSelectedTags[$TagCat->getId()]) ? in_array($Tag->getId(), $frontendSelectedTags[$TagCat->getId()]) : false;
$tagsArr[$Tag->getId()] = [
"name" => $Tag->getName(),
"selected" => $selected,
];
}
}
$tags[$TagCat->getId()] = [
"name" => $TagCat->getName(),
"id" => $TagCat->getId(),
"icon" => $TagCat->getIcon()->__toString(),
"tags" => $tagsArr
];
}
return $this->json([
"tagCategories" => $tags,
"stories" => $stories
]);
}
}