src/Controller/NewsController.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 NewsController extends FrontendController {
  13.     // -----------------------------------
  14.     // GET DETAIL
  15.     // -----------------------------------
  16.     /**
  17.      * @Route(
  18.      *      "/{path}/{headline}_n{id}",
  19.      *      name="newslist",
  20.      *      requirements={
  21.      *          "path"=".*",
  22.      *          "headline"="[\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\News::getById($id);
  38.         // view
  39.         if ($detail && ($preview == 'true' || $detail->getPublished())) {
  40.             $headline HelperController::toUrl($detail->getHeadline());
  41.             $detailLink $this->generateUrl('newslist', [
  42.                 'id' => $id,
  43.                 'headline' => $headline,
  44.                 'path' => $translator->trans('news-url-path')
  45.             ]);
  46.             return $this->render('news/detail.html.twig', [
  47.                 'newsDetail' => $detail,
  48.                 'newsDetailLink' => $detailLink,
  49.                 'newsTeaserList' => $this->getList($request),
  50.                 'documentId' => $documentId
  51.             ]);
  52.         } else {
  53.             throw new NotFoundHttpException("News object not found");
  54.         }
  55.     }
  56.     // -----------------------------------
  57.     // GET LIST
  58.     // -----------------------------------
  59.     public static function getList(Request $request): DataObject\News\Listing
  60.     {
  61.         $list = new DataObject\News\Listing();
  62.         $list->setOrderKey("date");
  63.         $list->setOrder("desc");
  64.         $list->setLimit(500);
  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.      *      "/news-list",
  80.      *      name="news-list"
  81.      * )
  82.      */
  83.     public function listAjax(Request $requestPaginatorInterface $paginator): Response
  84.     {
  85.         $list = new DataObject\News\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), 4);
  94.         return $this->render('news/news-list-ajax.html.twig', [
  95.             'newsList' => $paginator,
  96.             'paginationVariables' => $paginator->getPaginationData()
  97.         ]);
  98.     }
  99.     // -----------------------------------
  100.     // GET FILTER
  101.     // -----------------------------------
  102.     public static function getFilter(): array
  103.     {
  104.         $list = new DataObject\News\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("{_locale}/api/news-list", name="news-api-list")
  115.      * @param Request $request
  116.      * @param TranslatorInterface $translator
  117.      * @return JsonResponse
  118.      */
  119.     public function newsApiAction(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\News\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->getHeadline());
  135.             $detailLink $this->generateUrl('news-list', [
  136.                 'id' => $item->getId(),
  137.                 'title' => $title,
  138.                 'path' => $translator->trans('news-url-path')
  139.             ]);
  140.             $json[] = [
  141.                 'id' => $item->getId(),
  142.                 'categories' => $categories,
  143.                 'key'   => $item->getKey(),
  144.                 //'title' => $item->getTitle(),
  145.                 'intro' => $item->getIntro(),
  146.                 //'pages' => $item->getPages(),
  147.                 //'year'  => $item->getYear(),
  148.                 //'image' => $item->getImage()->getThumbnail('NewsOverview')->getPath(),
  149.                 'detailLink' => $detailLink
  150.             ];
  151.         }
  152.         return $this->json($json);
  153.     }
  154. }