<?php
namespace App\Controller;
use App\Entity\GroupBoxItems;
use App\Entity\PostPublishes;
use App\Entity\Tags;
use App\Entity\Videos;
use App\Service\CryptUtils;
use App\Service\DataExchange;
use App\Service\Contact;
use App\Utils\CacheProvider;
use App\Utils\Constants;
use App\Utils\Lib;
use App\Utils\Input;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\VarDumper\Cloner\Data;
class DefaultController extends BaseController
{
/**
* @param Request $request
* @param DataExchange $exchangeService
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
*/
public function index(Request $request, DataExchange $exchangeService)
{
$cacheService = $this->getCacheProvider(Constants::SERVER_CACHE_ARTICLE);
$featuredPosts = $this->getFeaturedPosts($cacheService, $exchangeService);
$travelPositions = $this->getTravelPositions($cacheService, $exchangeService, $featuredPosts['existingPostIds']);
$hotelPosts = $this->getHotelPosts($cacheService, $exchangeService, $featuredPosts['existingPostIds']);
$handBookPosts = $this->getHandBookPosts($cacheService, $exchangeService);
$mostViewPosts = $this->getMostViewPosts($cacheService, $exchangeService);
$tagPostsBox = $this->getTagPosts($cacheService, $exchangeService);
$homeUrl = $this->generateUrl('index');
$seo = $this->buildPagingMeta($homeUrl, $this->getParameter('home_title'), $this->getParameter('site_desc'));
$seo['is_home'] = true;
$amp = $request->get('is_amp') ? '-amp' : '';
if (!$amp){
$seo['amp'] = true;
$seo['ampUrl'] = $this->generateUrl('index_amp');
}
// var_dump($seo);
return $this->renderHtml('default/index' . $amp . '.html.twig', [
'featuredPosts' => $featuredPosts,
'travelPositions' => $travelPositions,
'hotelPosts' => $hotelPosts,
'handBookPosts' => $handBookPosts,
'mostViewPosts' => $mostViewPosts,
'tagPostsBox' => $tagPostsBox,
'cateSlug' => 'home',
'seo' => $seo
]);
// $this->addCachePage($request, $response);
// return $response;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function page404()
{
$page404Url = $this->generateUrl('page404');
$seo = $this->buildPagingMeta($page404Url, 'Page not found');
$response = $this->renderHtml('default/404.html.twig', ['metaData' => $seo]);
return $response;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function aboutUs()
{
$pageUrl = $this->generateUrl('about_us');
$seo = $this->buildPagingMeta($pageUrl, 'Giới thiệu');
$response = $this->renderHtml('default/about_us.html.twig', ['metaData' => $seo]);
return $response;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function siteMap()
{
$pageUrl = $this->generateUrl('site_map');
$seo = $this->buildPagingMeta($pageUrl, 'Site map');
$response = $this->renderHtml('default/site_map.html.twig', ['metaData' => $seo]);
return $response;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
*/
public function termCondition()
{
$pageTermUrl = $this->generateUrl('term_condition');
$seo = $this->buildPagingMeta($pageTermUrl, 'Điều khoản sử dụng');
$response = $this->renderHtml('default/terms_of_use.html.twig', ['metaData' => $seo]);
return $response;
}
/**
* @param Request $request
* @param Contact $contact
* @param TranslatorInterface $translator
* @param Breadcrumb $breadcrumb
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response
*/
public function contact(Request $request, Contact $contact)
{
if ($request->isMethod('POST')) {
$captcha = $request->get('g-recaptcha-response');
$captchakey = $this->getParameter('captcha')['secret_key'];
if (!Lib::verifyCaptcha($captcha, $captchakey)) {
$response['success'] = 0;
$response['errors'] = null;
$response['message'] = "Xin vui lòng xác thực bảo mật";
return new JsonResponse($response);
}
$requestData = [
'name' => Input::filter($request->get('name')),
'email' => Input::filter($request->get('email')),
'phone' => Input::filter($request->get('phone')),
'content' => Input::filter($request->get('content'))
];
$contact->setFromInput($requestData)->validate($errors);
if (empty($errors)) {
$contact->save();
return new JsonResponse(
[
'success' => 1,
'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!'
]
);
}
return new JsonResponse(
[
'success' => 0,
'errors' => $errors
]
);
}//end if
$pageTermUrl = $this->generateUrl('contact');
$seo = $this->buildPagingMeta($pageTermUrl, 'Liên hệ');
return $this->renderHtml(
'default/contact.html.twig',
[
'errors' => isset($errors) ? $errors : null,
'data' => isset($requestData) ? $requestData : null,
'captcha' => $this->getParameter('captcha'),
'metaData' => $seo
]
);
}//end actionContact()
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Exception
*/
public function register(Request $request)
{
$email = $request->get('email');
$isRegistered = false;
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)) {
$redis = $this->getCacheProvider(Constants::SERVER_CACHE_QUEUE);
$isRegistered = $redis->lpush(Constants::REGISTERED_QUEUE, $email . '|' . time());
}
return $this->returnJsonResponse(
[
'success' => $isRegistered ? true : false,
]
);
}
/**
* @param $cacheService
* @param DataExchange $exchangeService
* @return array|bool
*/
private function getFeaturedPosts($cacheService, DataExchange $exchangeService)
{
$focusCacheKey = Constants::CACHE_HOME_FEATURED_POST;
$featuredPosts = $cacheService->get($focusCacheKey);
if ($featuredPosts === false) {
$em = $this->getDoctrine()->getManager();
$sourcePosts = $em->getRepository(PostPublishes::class)
->getTopPostsInGroupBox(Constants::GROUP_BOX_HOME_FEATURE, Constants::HOME_TOP_FEATURED_POSTS_LIMIT);
if ($sourcePosts) {
$firstFeaturedPost = array_shift($sourcePosts);
$firstFeaturedPost = $exchangeService->exchangeRowPost(
$firstFeaturedPost,
Constants::FIRST_FEATURED_SIZE,
null,
null,
Constants::FIRST_FEATURED_MOBILE_SIZE
);
$existPostIds[] = $firstFeaturedPost['id'];
if (!empty($sourcePosts)) {
foreach ($sourcePosts as $postItem) {
$otherFeaturedPosts[] = $exchangeService->exchangeRowPost(
$postItem,
Constants::FEATURED_SIZE,
null,
null,
Constants::FEATURED_MOBILE_SIZE
);
$existPostIds[] = $postItem['postId'];
}
}
$featuredPosts = [
'centerFeaturedPost' => $firstFeaturedPost,
'rightBoxFeaturedPosts' => array_slice($otherFeaturedPosts, 0, 2),
'leftBoxFeaturedPosts' => array_slice($otherFeaturedPosts, 2, 2),
'existingPostIds' => $existPostIds
];
}
$cacheService->set($focusCacheKey, $featuredPosts, $this->getParameter('cache_time')['hour']);
}
return $featuredPosts;
}// End getFeaturedPosts();
/**
* @param $cacheService
* @param DataExchange $exchangeService
* @param $exceptIds
* @return array
*/
private function getTravelPositions($cacheService, DataExchange $exchangeService, $exceptIds)
{
$cacheKey = Constants::CACHE_TRAVEL_POSITIONS;
if (($data = $cacheService->get($cacheKey)) === false) {
$em = $this->getDoctrine()->getManager();
$sourcePosts = $em->getRepository(PostPublishes::class)
->getLatestPostsByCate([Constants::CATE_TRAVEL_POSITIONS], Constants::CATE_TRAVEL_POSITIONS_LIMIT, $exceptIds);
if ($sourcePosts) {
foreach ($sourcePosts as $postItem) {
$data[] = $exchangeService->exchangeRowPost(
$postItem,
Constants::BOX_POSITION_IMAGE_SIZE,
null,
null,
Constants::BOX_POSITION_IMAGE_MOBILE_SIZE
);
}
}
$cacheService->set($cacheKey, $data, $this->getParameter('cache_time')['hour']);
}
return $data;
}
/**
* @param $cacheService
* @param DataExchange $exchangeService
* @param $exceptIds
* @return array
*/
private function getHotelPosts($cacheService, DataExchange $exchangeService, $exceptIds)
{
$cacheKey = Constants::CACHE_HOTEL_POSTS;
if (($data = $cacheService->get($cacheKey)) === false) {
$em = $this->getDoctrine()->getManager();
$sourcePosts = $em->getRepository(PostPublishes::class)
->getLatestPostsByCate([Constants::CATE_HOTEL_ID], Constants::CATE_HOTEL_LIMIT, $exceptIds);
if ($sourcePosts) {
$firstPost = array_shift($sourcePosts);
$firstPost = $exchangeService->exchangeRowPost(
$firstPost,
Constants::BOX_HOTEL_FIRST_POST_IMAGE_SIZE,
null,
null,
Constants::BOX_HOTEL_FIRST_POST_IMAGE_MOBILE_SIZE
);
$otherPosts = [];
if (!empty($sourcePosts)) {
foreach ($sourcePosts as $postItem) {
$otherPosts[] = $exchangeService->exchangeRowPost(
$postItem,
Constants::BOX_HOTEL_IMAGE_SIZE,
null,
null,
Constants::BOX_HOTEL_IMAGE_MOBILE_SIZE
);
}
}
$data = [
'firstPosts' => $firstPost,
'otherPosts' => $otherPosts
];
}
$cacheService->set($cacheKey, $data, $this->getParameter('cache_time')['hour']);
}
return $data;
}
/**
* @param $cacheService
* @param DataExchange $exchangeService
* @return array
*/
private function getHandBookPosts($cacheService, DataExchange $exchangeService)
{
$cacheKey = Constants::CACHE_HANDBOOK_POSTS;
if (($data = $cacheService->get($cacheKey)) === false) {
$em = $this->getDoctrine()->getManager();
$sourcePosts = $em->getRepository(PostPublishes::class)
->getLatestPostsByCate([Constants::CATE_HANDBOOK_ID], Constants::CATE_HANDBOOK_LIMIT);
if ($sourcePosts) {
foreach ($sourcePosts as $postItem) {
$data[] = $exchangeService->exchangeRowPost(
$postItem,
Constants::BOX_HANDBOOK_IMAGE_SIZE,
null,
null,
Constants::BOX_HANDBOOK_IMAGE_MOBILE_SIZE
);
}
$cacheService->set($cacheKey, $data, $this->getParameter('cache_time')['hour']);
}
}
return $data;
}
/**
* @param $cacheService
* @param DataExchange $exchangeService
* @return array|bool
*/
private function getTagPosts($cacheService, DataExchange $exchangeService)
{
$focusCacheKey = Constants::CACHE_SPECIAL_POSTS;
$data = $cacheService->get($focusCacheKey);
if ($data === false) {
$em = $this->getDoctrine()->getManager();
$sourcePosts = $em->getRepository(PostPublishes::class)
->getTopPostsInGroupBox(Constants::GROUP_BOX_HOT_TOPICS, Constants::BOX_POST_TAG_LIMIT);
if ($sourcePosts) {
if (!empty($sourcePosts)) {
foreach ($sourcePosts as $postItem) {
$data[] = $exchangeService->exchangeRowPost(
$postItem,
Constants::BOX_POST_IMAGE_SIZE,
null,
null,
Constants::BOX_POST_IMAGE_MOBILE_SIZE
);
}
}
}
$cacheService->set($focusCacheKey, $data, $this->getParameter('cache_time')['hour']);
}
return $data;
}// End getFeaturedPosts();
}