src/Frontend/Controller/SiteController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Frontend\Controller;
  3. use App\AMZ\Core\Controller\FrontendController;
  4. use App\AMZ\Core\DataType\PostStatusType;
  5. use App\AMZ\Core\DataType\PostType;
  6. use App\AMZ\Core\Entity\Category;
  7. use App\AMZ\Core\Form\Block\ContactType;
  8. use App\Backend\Article\Entity\Article;
  9. use App\Backend\TicketBundle\Entity\TicketRequest;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class SiteController extends FrontendController
  17. {
  18.     const ITEM_PER_SEARCHING_PAGE 10;
  19.     public function index(Request $request): Response
  20.     {
  21.         $slug 'trang-chu';
  22.         $locale $request->getLocale();
  23.         $slug = ($locale == 'en')?'homepage':$slug;
  24.         $page $this->doctrine->getRepository('Core:Page')
  25.             ->getPageBySlug($slug );
  26.         $post $page->getPost();
  27.         return $this->render('themes/'.self::THEME.'/page/index.html.twig',array(
  28.             'page' => $page,
  29.             'post' => $post
  30.         ));
  31.     }
  32.     public function post(Request $request$slug):Response{
  33.         $post $this->doctrine->getRepository('Core:Post')
  34.             ->findOneBy([ 'slug' => $slug ]);
  35.         $entity $post;
  36. //        kiểm tra tình trạng của post
  37.         $deleted $post->getDeleted();
  38.         $published $post->getPublished();
  39.         if($deleted == PostStatusType::POST_STATUS_NOT_DELETED && $published == PostType::POST_TYPE_PUBLISHED){
  40.             if($post->getPostType() == PostType::POST_TYPE_PAGE)
  41.                 return $this->page($slug);
  42.             $template $this->getTemplateByPost($post);
  43.             return $this->render($template, array(
  44.                 'post'=>$post,
  45.                 'entity' => $entity
  46.             ));
  47.         }else{
  48.             return $this->redirectToRoute('frontend_site_default');
  49.             throw new NotFoundHttpException();
  50.         }
  51.     }
  52.     public function page($slug): Response
  53.     {
  54.         $post $this->doctrine->getRepository('Core:Post')
  55.             ->findOneBy(array('slug' => $slug)) ;
  56.         $page $this->doctrine->getRepository('Core:Page')
  57.             ->findOneBy(array('post' => $post));
  58. //        kiểm tra template xem có template phù hợp slug không?
  59. //        nếu có thì sử dụng template theo slug
  60. //        nếu không có thì sử dụng template mặc định themes/phyto/post/page/detail.html.twig
  61.         $template "themes/".self::THEME."/post/page/detail.html.twig";
  62.         $pageTemplate "themes/".self::THEME."/post/page/".$post->getSlug().".html.twig";
  63.         if($this->twig->getLoader()->exists($pageTemplate)){
  64.             $template $pageTemplate;
  65.         }
  66.         return $this->render($template, array(
  67.             'page' => $page,
  68.             'post' => $post
  69.         ));
  70.     }
  71.     public function category(Request $requestPaginatorInterface $paginator$slug){
  72.         $paginationParams $this->getParameter('pagination');
  73.         $length $paginationParams['post']['article']['default'];
  74.         $category $this->doctrine->getRepository(Category::class)
  75.             ->findOneBy(array('slug'=>$slug));
  76.         $qb $this->doctrine->getRepository(Article::class)
  77.             ->getArticleByCategory($category,true);
  78.         $pagination $paginator->paginate$qb$request->query->get('page'1), $length );
  79.         $template $this->getTemplateByCategory($category);
  80.         return $this->render($template,
  81.             array( 'pagination' => $pagination,'category'=>$category )
  82.         );
  83.     }
  84.     public function contact(Request $requestTranslatorInterface $translatorFlashBagInterface $flashBag){
  85.         $ticket = new TicketRequest();
  86.         $contactForm $this->createForm(ContactType::class, $ticket);
  87.         $contactForm->handleRequest($request);
  88.         if($contactForm->isSubmitted() && $contactForm->isValid()){
  89.             $this->doctrine->getManager()->persist($ticket);
  90.             $this->doctrine->getManager()->flush();
  91.             $flashBag->set('contact.form.notice',$translator->trans('Cám ơn bạn đã để lại thông tin, chúng tôi sẽ liên hệ lại sớm nhất'));
  92.             unset($ticket);
  93.             unset($contactForm);
  94.             $ticket = new TicketRequest();
  95.             $contactForm $this->createForm(ContactType::class, $ticket);
  96.         }
  97.         $form $contactForm->createView();
  98.         return $this->render('themes/phuongtrinh/post/page/lien-he.html.twig',[
  99.             'form' => $form
  100.         ]);
  101.     }
  102.     public function search(Request $requestPaginatorInterface $paginator){
  103.         $keyword $request->query->get("keyword","");
  104.         $page $request->query->get('page',1);
  105.         if(empty($keyword) || strlen($keyword) < 4){
  106.             return $this->render('themes/phyto/post/page/search.html.twig');
  107.         }
  108.         $qb $this->doctrine->getRepository(Article::class)
  109.             ->createQueryBuilder('article');
  110.         $qb->leftJoin('article.post','post');
  111.         $keywordExpr $qb->expr()->literal('%'.$keyword."%");
  112.         $qb->andWhere(
  113.             $qb->expr()->like('post.title'$keywordExpr)
  114.         );
  115.         $pagination $paginator->paginate($qb->getQuery(), $pageself::ITEM_PER_SEARCHING_PAGE);
  116.         return $this->render('themes/phyto/post/page/search.html.twig',['pagination' => $pagination]);
  117.     }
  118.     public function handleContactForm(Request $requestTranslatorInterface $translator){
  119.         $ticket = new TicketRequest();
  120.         $form $this->createForm(\App\Frontend\Form\ContactType::class, $ticket);
  121.         $form->handleRequest($request);
  122.         if($form->isSubmitted() && $form->isValid()){
  123.             $this->doctrine->getManager()->persist($ticket);
  124.             $this->doctrine->getManager()->flush();
  125.             return new Response(json_encode(array(
  126.                 'message' => $translator->trans('Chúng tôi đã tiếp nhận thông tin và sẽ liên hệ lại bạn sớm nhất')
  127.             )), 200);
  128.         }elseif(!$form->isValid()){
  129.             return new Response$translator->trans('Vui lòng để lại các thông tin bắt buộc'), 403);
  130.         }
  131.     }
  132. }