src/Controller/ProjekteController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\DataObject;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. class ProjekteController extends FrontendController {
  13.     // -----------------------------------
  14.     // GET DETAIL
  15.     // -----------------------------------
  16.     /**
  17.      * @Route(
  18.      *      "/{path}/{title}_p{id}",
  19.      *      name="projekt",
  20.      *      requirements={
  21.      *          "path"=".*",
  22.      *          "title"="[\w-]+",
  23.      *          "id"="\d+"
  24.      *      }
  25.      * )
  26.      */
  27.     public function getDetail(Request $requestTranslatorInterface $translator): Response
  28.     {
  29.         $documentId $this->document->getId();
  30.         // preview
  31.         $preview $request->get('preview');
  32.         // parameters
  33.         $routeParameters $request->attributes->get('_route_params');
  34.         $id $routeParameters['id'];
  35.         //$title = $routeParameters['title'];
  36.         // news object
  37.         $detail DataObject\Projekt::getById($id);
  38.         // view
  39.         if ($detail && ($preview == 'true' || $detail->getPublished())) {
  40.             $title HelperController::toUrl($detail->getMaintitle());
  41.             $detailLink $this->generateUrl('projekt', [
  42.                 'id' => $id,
  43.                 'title' => $title,
  44.                 'path' => $translator->trans('projekt-url-path')
  45.             ]);
  46.             return $this->render('projekte/detail.html.twig', [
  47.                 'projektDetail' => $detail,
  48.                 'projektDetailLink' => $detailLink,
  49.                 'projektTeaserList' => $this->getList($request),
  50.                 'documentId' => $documentId
  51.             ]);
  52.         } else {
  53.             throw new NotFoundHttpException("Projekte object not found");
  54.         }
  55.     }
  56.     // -----------------------------------
  57.     // GET LIST
  58.     // -----------------------------------
  59.     public static function getList(Request $request): DataObject\Projekt\Listing
  60.     {
  61.         $list = new DataObject\Projekt\Listing();
  62.         $list->setOrderKey("date");
  63.         $list->setOrder("desc");
  64.         //$list->setLimit(2);
  65.         if ($request->get('filter')) {
  66.             $list->setCondition('category__id LIKE ?', [$request->get('filter')]);
  67.         }
  68.         $limit NULL;
  69.         if ($limit) {
  70.             $list->setLimit($limit);
  71.         }
  72.         return $list;
  73.     }
  74.     // --------------------------------
  75.     // GET LIST AJAX
  76.     // --------------------------------
  77.     /**
  78.      * @Route(
  79.      *      "/{_locale}/projekt-list",
  80.      *      name="projekt-list"
  81.      * )
  82.      */
  83.     public function listAjax(Request $requestPaginatorInterface $paginator): Response
  84.     {
  85.         $list = new DataObject\Projekt\Listing();
  86.         $list->setOrderKey('date');
  87.         $list->setOrder('desc');
  88.         //$list->load();
  89.         if ($request->get('filter')) {
  90.             $list->setCondition('category__id LIKE ?', [$request->get('filter')]);
  91.         }
  92.         // paginator
  93.         $paginator $paginator->paginate($list$request->get('page'1), 2);
  94.         return $this->render('projekte/projekt-list-ajax.html.twig', [
  95.             'projektList' => $paginator,
  96.             'paginationVariables' => $paginator->getPaginationData()
  97.         ]);
  98.     }
  99.     // -----------------------------------
  100.     // GET FILTER
  101.     // -----------------------------------
  102.     public static function getFilter(): array
  103.     {
  104.         $list = new DataObject\Projekt\Listing();
  105.         $categories = [];
  106.         foreach ($list as $detail) {
  107.             foreach ($detail->getCategory() as $category) {
  108.                 $categories[$category->getId()] = $category->getName();
  109.             }
  110.         }
  111.         return $categories;
  112.     }
  113.     /**
  114.      * @Route("/api/projekt-list-api", name="projekt-api-list")
  115.      * @param Request $request
  116.      * @param TranslatorInterface $translator
  117.      * @return JsonResponse
  118.      */
  119.     public function projektApiAction(Request $requestTranslatorInterface $translator): JsonResponse
  120.     {
  121.         // set empty json array
  122.         $json = [];
  123.         // get a list of news objects and order them by date
  124.         $apiList = new DataObject\Projekt\Listing();
  125.         $apiList->setOrderKey('date');
  126.         $apiList->setOrder('DESC');
  127.         $apiList->setOffset($request->get('offset'0));
  128.         $apiList->setLimit($request->get('limit'500));
  129.         foreach ($apiList as $item) {
  130.             $categories = [];
  131.             foreach ($item->getCategory() as $category) {
  132.                 $categories[] = $category->getName();
  133.             }
  134.             $title HelperController::toUrl($item->getMaintitle());
  135.             $detailLink $this->generateUrl('projekt', [
  136.                 'id' => $item->getId(),
  137.                 'title' => $title,
  138.                 'path' => $translator->trans('projekt-url-path')
  139.             ]);
  140.             $subjectVal = ["ö","-","/"," ","ä","&","ü"];
  141.             $toReplace = ["oe","","","","ae","","ue"];
  142.             $formatedCategories = ["project-teaser"];
  143.             foreach ($categories as $category ) {
  144.                 $formatedCategories[] = strtolower(str_replace($subjectVal,  $toReplace$category));
  145.             }
  146.             $json[] = [
  147.                 'id' => $item->getId(),
  148.                 'categories' => $categories,
  149.                 'formatedCategories' => $formatedCategories,
  150.                 'key'   => $item->getKey(),
  151.                 'maintitle' => $item->getMaintitle(),
  152.                 'image' => $item->getImage()->getThumbnail('ProjectListTeaser')->getPath(),
  153.                 'detailLink' => $detailLink,
  154.                 //'objectSelect' => $item->getObjektSelect(),
  155.             ];
  156.         }
  157.         return $this->json($json);
  158.     }
  159. }