src/Controller/DefaultController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\GroupBoxItems;
  4. use App\Entity\PostPublishes;
  5. use App\Entity\Tags;
  6. use App\Entity\Videos;
  7. use App\Service\CryptUtils;
  8. use App\Service\DataExchange;
  9. use App\Service\Contact;
  10. use App\Utils\CacheProvider;
  11. use App\Utils\Constants;
  12. use App\Utils\Lib;
  13. use App\Utils\Input;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Translation\TranslatorInterface;
  17. use Symfony\Component\VarDumper\Cloner\Data;
  18. class DefaultController extends BaseController
  19. {
  20.     /**
  21.      * @param Request $request
  22.      * @param DataExchange $exchangeService
  23.      * @return \Symfony\Component\HttpFoundation\Response
  24.      * @throws \Exception
  25.      */
  26.     public function index(Request $requestDataExchange $exchangeService)
  27.     {
  28.         $cacheService $this->getCacheProvider(Constants::SERVER_CACHE_ARTICLE);
  29.         $featuredPosts $this->getFeaturedPosts($cacheService$exchangeService);
  30.         $travelPositions $this->getTravelPositions($cacheService$exchangeService$featuredPosts['existingPostIds']);
  31.         $hotelPosts $this->getHotelPosts($cacheService$exchangeService$featuredPosts['existingPostIds']);
  32.         $handBookPosts $this->getHandBookPosts($cacheService$exchangeService);
  33.         $mostViewPosts $this->getMostViewPosts($cacheService$exchangeService);
  34.         $tagPostsBox $this->getTagPosts($cacheService$exchangeService);
  35.         $homeUrl $this->generateUrl('index');
  36.         $seo $this->buildPagingMeta($homeUrl$this->getParameter('home_title'), $this->getParameter('site_desc'));
  37.         $seo['is_home'] = true;
  38.         $amp $request->get('is_amp') ? '-amp' '';
  39.         if (!$amp){
  40.             $seo['amp'] = true;
  41.             $seo['ampUrl'] = $this->generateUrl('index_amp');
  42.         }
  43. //        var_dump($seo);
  44.         return $this->renderHtml('default/index' $amp '.html.twig', [
  45.             'featuredPosts' => $featuredPosts,
  46.             'travelPositions' => $travelPositions,
  47.             'hotelPosts' => $hotelPosts,
  48.             'handBookPosts' => $handBookPosts,
  49.             'mostViewPosts' => $mostViewPosts,
  50.             'tagPostsBox' => $tagPostsBox,
  51.             'cateSlug' => 'home',
  52.             'seo' => $seo
  53.         ]);
  54. //        $this->addCachePage($request, $response);
  55. //        return $response;
  56.     }
  57.     
  58.     /**
  59.      * @return \Symfony\Component\HttpFoundation\Response
  60.      */
  61.     public function page404()
  62.     {
  63.         $page404Url $this->generateUrl('page404');
  64.         $seo $this->buildPagingMeta($page404Url'Page not found');
  65.         $response $this->renderHtml('default/404.html.twig', ['metaData' => $seo]);
  66.         return $response;
  67.     }
  68.     
  69.     /**
  70.      * @return \Symfony\Component\HttpFoundation\Response
  71.      */
  72.     public function aboutUs()
  73.     {
  74.         $pageUrl $this->generateUrl('about_us');
  75.         $seo $this->buildPagingMeta($pageUrl'Giới thiệu');
  76.         $response $this->renderHtml('default/about_us.html.twig', ['metaData' => $seo]);
  77.         return $response;
  78.     }
  79.     
  80.     /**
  81.      * @return \Symfony\Component\HttpFoundation\Response
  82.      */
  83.     public function siteMap()
  84.     {
  85.         $pageUrl $this->generateUrl('site_map');
  86.         $seo $this->buildPagingMeta($pageUrl'Site map');
  87.         $response $this->renderHtml('default/site_map.html.twig', ['metaData' => $seo]);
  88.         return $response;
  89.     }
  90.     
  91.     /**
  92.      * @return \Symfony\Component\HttpFoundation\Response
  93.      */
  94.     public function termCondition()
  95.     {
  96.         $pageTermUrl $this->generateUrl('term_condition');
  97.         $seo $this->buildPagingMeta($pageTermUrl'Điều khoản sử dụng');
  98.         $response $this->renderHtml('default/terms_of_use.html.twig', ['metaData' => $seo]);
  99.         return $response;
  100.     }
  101.     
  102.     /**
  103.      * @param Request $request
  104.      * @param Contact $contact
  105.      * @param TranslatorInterface $translator
  106.      * @param Breadcrumb $breadcrumb
  107.      * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
  108.      */
  109.     public function contact(Request $requestContact $contact)
  110.     {
  111.         if ($request->isMethod('POST')) {
  112.             $captcha $request->get('g-recaptcha-response');
  113.             $captchakey $this->getParameter('captcha')['secret_key'];
  114.             if (!Lib::verifyCaptcha($captcha$captchakey)) {
  115.                 $response['success'] = 0;
  116.                 $response['errors'] = null;
  117.                 $response['message'] = "Xin vui lòng xác thực bảo mật";
  118.                 return new JsonResponse($response);
  119.             }
  120.             
  121.             $requestData = [
  122.                 'name' => Input::filter($request->get('name')),
  123.                 'email' => Input::filter($request->get('email')),
  124.                 'phone' => Input::filter($request->get('phone')),
  125.                 'content' => Input::filter($request->get('content'))
  126.             ];
  127.             
  128.             $contact->setFromInput($requestData)->validate($errors);
  129.             
  130.             if (empty($errors)) {
  131.                 $contact->save();
  132.                 return new JsonResponse(
  133.                     [
  134.                         'success' => 1,
  135.                         'message' => 'Quý khách đã gửi liên hệ thành công. Nhân viên của chúng tôi sẽ gọi lại để xác nhận!'
  136.                     ]
  137.                 );
  138.             }
  139.             
  140.             return new JsonResponse(
  141.                 [
  142.                     'success' => 0,
  143.                     'errors' => $errors
  144.                 ]
  145.             );
  146.         }//end if
  147.         
  148.         $pageTermUrl $this->generateUrl('contact');
  149.         $seo $this->buildPagingMeta($pageTermUrl'Liên hệ');
  150.         return $this->renderHtml(
  151.             'default/contact.html.twig',
  152.             [
  153.                 'errors' => isset($errors) ? $errors null,
  154.                 'data' => isset($requestData) ? $requestData null,
  155.                 'captcha' => $this->getParameter('captcha'),
  156.                 'metaData' => $seo
  157.             ]
  158.         );
  159.     }//end actionContact()
  160.     
  161.     /**
  162.      * @param Request $request
  163.      * @return \Symfony\Component\HttpFoundation\Response
  164.      * @throws \Exception
  165.      */
  166.     public function register(Request $request)
  167.     {
  168.         $email $request->get('email');
  169.         $isRegistered false;
  170.         
  171.         if (preg_match('/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/i'$email)) {
  172.             $redis $this->getCacheProvider(Constants::SERVER_CACHE_QUEUE);
  173.             $isRegistered $redis->lpush(Constants::REGISTERED_QUEUE$email '|' time());
  174.         }
  175.         
  176.         return $this->returnJsonResponse(
  177.             [
  178.                 'success' => $isRegistered true false,
  179.             ]
  180.         );
  181.         
  182.     }
  183.     
  184.     /**
  185.      * @param $cacheService
  186.      * @param DataExchange $exchangeService
  187.      * @return array|bool
  188.      */
  189.     private function getFeaturedPosts($cacheServiceDataExchange $exchangeService)
  190.     {
  191.         $focusCacheKey Constants::CACHE_HOME_FEATURED_POST;
  192.         $featuredPosts $cacheService->get($focusCacheKey);
  193.         
  194.         if ($featuredPosts === false) {
  195.             $em $this->getDoctrine()->getManager();
  196.             $sourcePosts $em->getRepository(PostPublishes::class)
  197.                 ->getTopPostsInGroupBox(Constants::GROUP_BOX_HOME_FEATUREConstants::HOME_TOP_FEATURED_POSTS_LIMIT);
  198.             if ($sourcePosts) {
  199.                 $firstFeaturedPost array_shift($sourcePosts);
  200.                 $firstFeaturedPost $exchangeService->exchangeRowPost(
  201.                     $firstFeaturedPost,
  202.                     Constants::FIRST_FEATURED_SIZE,
  203.                     null,
  204.                     null,
  205.                     Constants::FIRST_FEATURED_MOBILE_SIZE
  206.                 
  207.                 );
  208.                 $existPostIds[] = $firstFeaturedPost['id'];
  209.                 
  210.                 if (!empty($sourcePosts)) {
  211.                     foreach ($sourcePosts as $postItem) {
  212.                         $otherFeaturedPosts[] = $exchangeService->exchangeRowPost(
  213.                             $postItem,
  214.                             Constants::FEATURED_SIZE,
  215.                             null,
  216.                             null,
  217.                             Constants::FEATURED_MOBILE_SIZE
  218.                         
  219.                         );
  220.                         $existPostIds[] = $postItem['postId'];
  221.                     }
  222.                 }
  223.                 
  224.                 $featuredPosts = [
  225.                     'centerFeaturedPost' => $firstFeaturedPost,
  226.                     'rightBoxFeaturedPosts' => array_slice($otherFeaturedPosts02),
  227.                     'leftBoxFeaturedPosts' => array_slice($otherFeaturedPosts22),
  228.                     'existingPostIds' => $existPostIds
  229.                 ];
  230.             }
  231.             
  232.             $cacheService->set($focusCacheKey$featuredPosts$this->getParameter('cache_time')['hour']);
  233.         }
  234.         
  235.         return $featuredPosts;
  236.     }// End getFeaturedPosts();
  237.     
  238.     /**
  239.      * @param $cacheService
  240.      * @param DataExchange $exchangeService
  241.      * @param $exceptIds
  242.      * @return array
  243.      */
  244.     private function getTravelPositions($cacheServiceDataExchange $exchangeService$exceptIds)
  245.     {
  246.         $cacheKey Constants::CACHE_TRAVEL_POSITIONS;
  247.         
  248.         if (($data $cacheService->get($cacheKey)) === false) {
  249.             $em $this->getDoctrine()->getManager();
  250.             $sourcePosts $em->getRepository(PostPublishes::class)
  251.                 ->getLatestPostsByCate([Constants::CATE_TRAVEL_POSITIONS], Constants::CATE_TRAVEL_POSITIONS_LIMIT$exceptIds);
  252.             
  253.             if ($sourcePosts) {
  254.                 foreach ($sourcePosts as $postItem) {
  255.                     $data[] = $exchangeService->exchangeRowPost(
  256.                         $postItem,
  257.                         Constants::BOX_POSITION_IMAGE_SIZE,
  258.                         null,
  259.                         null,
  260.                         Constants::BOX_POSITION_IMAGE_MOBILE_SIZE
  261.                     );
  262.                 }
  263.             }
  264.             
  265.             $cacheService->set($cacheKey$data$this->getParameter('cache_time')['hour']);
  266.         }
  267.         
  268.         return $data;
  269.     }
  270.     
  271.     /**
  272.      * @param $cacheService
  273.      * @param DataExchange $exchangeService
  274.      * @param $exceptIds
  275.      * @return array
  276.      */
  277.     private function getHotelPosts($cacheServiceDataExchange $exchangeService$exceptIds)
  278.     {
  279.         $cacheKey Constants::CACHE_HOTEL_POSTS;
  280.         
  281.         if (($data $cacheService->get($cacheKey)) === false) {
  282.             $em $this->getDoctrine()->getManager();
  283.             $sourcePosts $em->getRepository(PostPublishes::class)
  284.                 ->getLatestPostsByCate([Constants::CATE_HOTEL_ID], Constants::CATE_HOTEL_LIMIT$exceptIds);
  285.             if ($sourcePosts) {
  286.                 $firstPost array_shift($sourcePosts);
  287.                 $firstPost $exchangeService->exchangeRowPost(
  288.                     $firstPost,
  289.                     Constants::BOX_HOTEL_FIRST_POST_IMAGE_SIZE,
  290.                     null,
  291.                     null,
  292.                     Constants::BOX_HOTEL_FIRST_POST_IMAGE_MOBILE_SIZE
  293.                 );
  294.                 $otherPosts = [];
  295.                 if (!empty($sourcePosts)) {
  296.                     foreach ($sourcePosts as $postItem) {
  297.                         $otherPosts[] = $exchangeService->exchangeRowPost(
  298.                             $postItem,
  299.                             Constants::BOX_HOTEL_IMAGE_SIZE,
  300.                             null,
  301.                             null,
  302.                             Constants::BOX_HOTEL_IMAGE_MOBILE_SIZE
  303.                         );
  304.                     }
  305.                 }
  306.                 
  307.                 $data = [
  308.                     'firstPosts' => $firstPost,
  309.                     'otherPosts' => $otherPosts
  310.                 ];
  311.             }
  312.             
  313.             $cacheService->set($cacheKey$data$this->getParameter('cache_time')['hour']);
  314.         }
  315.         
  316.         return $data;
  317.     }
  318.     
  319.     /**
  320.      * @param $cacheService
  321.      * @param DataExchange $exchangeService
  322.      * @return array
  323.      */
  324.     private function getHandBookPosts($cacheServiceDataExchange $exchangeService)
  325.     {
  326.         $cacheKey Constants::CACHE_HANDBOOK_POSTS;
  327.         
  328.         if (($data $cacheService->get($cacheKey)) === false) {
  329.             $em $this->getDoctrine()->getManager();
  330.             $sourcePosts $em->getRepository(PostPublishes::class)
  331.                 ->getLatestPostsByCate([Constants::CATE_HANDBOOK_ID], Constants::CATE_HANDBOOK_LIMIT);
  332.             if ($sourcePosts) {
  333.                 foreach ($sourcePosts as $postItem) {
  334.                     $data[] = $exchangeService->exchangeRowPost(
  335.                         $postItem,
  336.                         Constants::BOX_HANDBOOK_IMAGE_SIZE,
  337.                         null,
  338.                         null,
  339.                         Constants::BOX_HANDBOOK_IMAGE_MOBILE_SIZE
  340.                     );
  341.                 }
  342.                 
  343.                 $cacheService->set($cacheKey$data$this->getParameter('cache_time')['hour']);
  344.             }
  345.         }
  346.         
  347.         return $data;
  348.     }
  349.     
  350.     /**
  351.      * @param $cacheService
  352.      * @param DataExchange $exchangeService
  353.      * @return array|bool
  354.      */
  355.     private function getTagPosts($cacheServiceDataExchange $exchangeService)
  356.     {
  357.         $focusCacheKey Constants::CACHE_SPECIAL_POSTS;
  358.         $data $cacheService->get($focusCacheKey);
  359.         
  360.         if ($data === false) {
  361.             $em $this->getDoctrine()->getManager();
  362.             $sourcePosts $em->getRepository(PostPublishes::class)
  363.                 ->getTopPostsInGroupBox(Constants::GROUP_BOX_HOT_TOPICSConstants::BOX_POST_TAG_LIMIT);
  364.             if ($sourcePosts) {
  365.                 if (!empty($sourcePosts)) {
  366.                     foreach ($sourcePosts as $postItem) {
  367.                         $data[] = $exchangeService->exchangeRowPost(
  368.                             $postItem,
  369.                             Constants::BOX_POST_IMAGE_SIZE,
  370.                             null,
  371.                             null,
  372.                             Constants::BOX_POST_IMAGE_MOBILE_SIZE
  373.                         );
  374.                     }
  375.                 }
  376.             }
  377.             
  378.             $cacheService->set($focusCacheKey$data$this->getParameter('cache_time')['hour']);
  379.         }
  380.         
  381.         return $data;
  382.     }// End getFeaturedPosts();
  383. }