src/Controller/Mvc/NewsController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Mvc;
  3. use App\Entity\News\Entry;
  4. use App\Form\News\ListType;
  5. use App\Repository\News\CategoryRepository;
  6. use App\Repository\News\EntryRepository;
  7. use App\Service\CmsHelperService;
  8. use App\Service\FileUploader\UploadService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * @Route("/news")
  16.  */
  17. class NewsController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="news_index", methods={"GET"})
  21.      */
  22.     public function index(
  23.         Request $request,
  24.         EntryRepository $repository,
  25.         CategoryRepository $categoryRepository,
  26.         CmsHelperService $helper
  27.     ): Response {
  28.         $form $this->createForm(ListType::class);
  29.         $criteria $request->query->all();
  30.         $criteria['limit'] = $criteria['limit'] ?? 10;
  31.         $form->submit($criteria);
  32.         $paginate $helper->listHandle($repository$form->getViewData(), function($qb$criteria) use ($repository) {
  33.             $repository->addPublicTo($qbEntryRepository::CORPORATE);
  34.         });
  35.         return $this->render("pages/news/index.html.twig", [
  36.             "paginate" => $paginate,
  37.             "data" => $paginate->getPaginator()->getIterator(),
  38.             "categories" => $categoryRepository->getCategoriesHaveEntry(),
  39.             "current_category" => $form->getViewData()['category']
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/{id}", name="news_detail", methods={"GET"}, requirements={"id" = "\d+"})
  44.      */
  45.     public function detail(
  46.         EntryRepository $repository,
  47.         $id
  48.     ) {
  49.         $Entry $repository->getEntry($id);
  50.         if(!$Entry) {
  51.             throw new NotFoundHttpException('page not found');
  52.         }
  53.         if($Entry->getLinkUrl()) {
  54.             return $this->redirect($Entry->getLinkUrl());
  55.         }
  56.         return $this->render("pages/news/detail.html.twig", [
  57.             "entry" => $Entry
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/image/{filename}", name="news_image", methods={"GET"})
  62.      */
  63.     public function image(UploadService $uploader$filename) {
  64.         $config $uploader->getUploadConfig(new Entry"main_img");
  65.         return $uploader->outputResponseOrWebpResponse($filename$config);
  66.     }
  67.     /**
  68.      * @Route("/headline", name="news_headline", methods={"GET"})
  69.      */
  70.     public function headline(Request $requestEntryRepository $repository) {
  71.         $criteria $request->query->all();
  72.         $qb $repository->getListQuery($criteria);
  73.         $repository->addPublicTo($qbEntryRepository::CORPORATE);
  74.         $qb->setMaxResults(isset($criteria['limit']) && $criteria['limit']? $criteria['limit']: 3);
  75.         return $this->render('pages/news/headline.html.twig', [
  76.             "data" => $qb->getQuery()->getResult()
  77.         ]);
  78.     }
  79. }