src/Controller/ServiceController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Model\DataObject\TagCategory;
  7. use Symfony\Component\HttpClient\Exception\JsonException;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class ServiceController extends FrontendController
  10. {
  11.     public function defaultAction(Request $request)
  12.     {
  13.         $slug trim($request->get('slug'), '/');
  14.         if(str_contains($slug'sid-')){
  15.             $service DataObject\ServiceClass::getById(str_replace('sid-'''$slug), 1);
  16.         }else{
  17.             $service DataObject\ServiceClass::getByURL($slug1);
  18.         }
  19.         if(is_null($service->getSnipped())){
  20.         }
  21.         $this->document->setValue('title'$service->getMetaTitle());
  22.         $this->document->setValue('description'$service->getMetaDescription());
  23.         return $this->render('digitalwandler/DocumentType/snippedPage.html.twig', ['snipped' => $service->getSnipped()->getId()]);
  24.     }
  25.     public function menuAction(Request $request)
  26.     {
  27.         $services DataObject\ServiceClass::getList([
  28.             'order' => 'desc'
  29.         ]);
  30.         foreach ($services as $service){
  31.             foreach (explode('|'$service->getTags()) as $tag){
  32.                 if(isset($tags[$tag])){
  33.                     $tags[$tag]++;
  34.                 }else{
  35.                     $tags[$tag] = 1;
  36.                 }
  37.             }
  38.         }
  39.         return $this->render('digitalwandler/DocumentType/menuSnippet.html.twig', ['tags' => $tags]);
  40.     }
  41.    /**
  42.     * @Route("/api/success-stories", name="success_stories", methods="POST")
  43.     */
  44.     public function getFilteredSuccessStories(Request $request) {
  45.         try {
  46.             $requestAsArray $request->toArray();
  47.         } catch (JsonException $e) {
  48.             $requestAsArray = [];
  49.         }
  50.         
  51.         $frontendSelectedTags = [];
  52.         foreach ($requestAsArray as $tagCategoryId => $tagCategory) {
  53.             foreach ($tagCategory["tags"] as $tagId => $tag) {
  54.                 if ($tag["selected"] == true) {
  55.                     $frontendSelectedTags[$tagCategoryId][] = $tagId;
  56.                 }
  57.             }
  58.         }
  59.         $successStories DataObject\SuccessStories::getList([
  60.             'order' => 'desc'
  61.         ]);
  62.         
  63.         // add joins to filter for tags
  64.         // one join per tagCategories that filters with IN (tags) --> 
  65.         $successStories->onCreateQueryBuilder(
  66.             function (\Doctrine\DBAL\Query\QueryBuilder $qb) use ($successStories$frontendSelectedTags) {
  67.                 foreach ($frontendSelectedTags as $tagCategoryId => $tags) {
  68.                     if($tags && count($tags) > 0) {
  69.                         $rel "rel" $tagCategoryId;
  70.                         $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) . ")");
  71.                     }
  72.                 }
  73.                 $qb->groupBy("object_2.oo_id");
  74.             }
  75.         );
  76.         
  77.         $allowedTags = [];
  78.         
  79.         // build filtered stories array 
  80.         // also build $allowedTags while we're looping trough all filtered stories
  81.         $stories = [];
  82.         foreach ($successStories as $story) {
  83.             $allowedTags array_unique(array_merge($allowedTagsarray_map(fn($tag) => $tag->getId(), $story->getRealTags())));
  84.             $stories[] = [
  85.                 "id" => $story->getId(),
  86.                 "title" => $story->getTitle(),
  87.                 "url" => $story->getUrl(),
  88.                 "teaser" => $story->getTeaser(),
  89.                 "teaserImage" => $story->getTeaserImage() ? $story->getTeaserImage()->getThumbnail('jumpstartImage')->getPath() : "",
  90.                 "tags" => array_map(fn($tag) => ['id' => $tag->getId(), 'name' => $tag->getName()], $story->getRealTags())
  91.             ];
  92.         }
  93.         
  94.         $TagCategories TagCategory::getList();
  95.         $tags = [];
  96.         foreach ($TagCategories as $TagCat) {
  97.             $TagCatTags $TagCat->getTags();
  98.             $tagsArr = [];
  99.             foreach ($TagCatTags as $Tag) {
  100.                 if(in_array($Tag->getId(), $allowedTags)) {
  101.                     $selected = isset($frontendSelectedTags[$TagCat->getId()]) ? in_array($Tag->getId(), $frontendSelectedTags[$TagCat->getId()]) : false;
  102.                     $tagsArr[$Tag->getId()] = [
  103.                         "name" => $Tag->getName(),
  104.                         "selected" => $selected,
  105.                     ];
  106.                 }
  107.             }
  108.             $tags[$TagCat->getId()] = [
  109.                 "name" => $TagCat->getName(),
  110.                 "id" => $TagCat->getId(),
  111.                 "icon" => $TagCat->getIcon()->__toString(),
  112.                 "tags" => $tagsArr
  113.             ];
  114.         }
  115.         return $this->json([
  116.             "tagCategories" => $tags,
  117.             "stories" => $stories
  118.         ]);
  119.     }
  120. }