<?php
namespace App\Frontend\Controller;
use App\AMZ\Core\Controller\FrontendController;
use App\AMZ\Core\DataType\PostStatusType;
use App\AMZ\Core\DataType\PostType;
use App\AMZ\Core\Entity\Category;
use App\AMZ\Core\Form\Block\ContactType;
use App\Backend\Article\Entity\Article;
use App\Backend\TicketBundle\Entity\TicketRequest;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Contracts\Translation\TranslatorInterface;
class SiteController extends FrontendController
{
const ITEM_PER_SEARCHING_PAGE = 10;
public function index(Request $request): Response
{
$slug = 'trang-chu';
$locale = $request->getLocale();
$slug = ($locale == 'en')?'homepage':$slug;
$page = $this->doctrine->getRepository('Core:Page')
->getPageBySlug($slug );
$post = $page->getPost();
return $this->render('themes/'.self::THEME.'/page/index.html.twig',array(
'page' => $page,
'post' => $post
));
}
public function post(Request $request, $slug):Response{
$post = $this->doctrine->getRepository('Core:Post')
->findOneBy([ 'slug' => $slug ]);
$entity = $post;
// kiểm tra tình trạng của post
$deleted = $post->getDeleted();
$published = $post->getPublished();
if($deleted == PostStatusType::POST_STATUS_NOT_DELETED && $published == PostType::POST_TYPE_PUBLISHED){
if($post->getPostType() == PostType::POST_TYPE_PAGE)
return $this->page($slug);
$template = $this->getTemplateByPost($post);
return $this->render($template, array(
'post'=>$post,
'entity' => $entity
));
}else{
return $this->redirectToRoute('frontend_site_default');
throw new NotFoundHttpException();
}
}
public function page($slug): Response
{
$post = $this->doctrine->getRepository('Core:Post')
->findOneBy(array('slug' => $slug)) ;
$page = $this->doctrine->getRepository('Core:Page')
->findOneBy(array('post' => $post));
// kiểm tra template xem có template phù hợp slug không?
// nếu có thì sử dụng template theo slug
// nếu không có thì sử dụng template mặc định themes/phyto/post/page/detail.html.twig
$template = "themes/".self::THEME."/post/page/detail.html.twig";
$pageTemplate = "themes/".self::THEME."/post/page/".$post->getSlug().".html.twig";
if($this->twig->getLoader()->exists($pageTemplate)){
$template = $pageTemplate;
}
return $this->render($template, array(
'page' => $page,
'post' => $post
));
}
public function category(Request $request, PaginatorInterface $paginator, $slug){
$paginationParams = $this->getParameter('pagination');
$length = $paginationParams['post']['article']['default'];
$category = $this->doctrine->getRepository(Category::class)
->findOneBy(array('slug'=>$slug));
$qb = $this->doctrine->getRepository(Article::class)
->getArticleByCategory($category,true);
$pagination = $paginator->paginate( $qb, $request->query->get('page', 1), $length );
$template = $this->getTemplateByCategory($category);
return $this->render($template,
array( 'pagination' => $pagination,'category'=>$category )
);
}
public function contact(Request $request, TranslatorInterface $translator, FlashBagInterface $flashBag){
$ticket = new TicketRequest();
$contactForm = $this->createForm(ContactType::class, $ticket);
$contactForm->handleRequest($request);
if($contactForm->isSubmitted() && $contactForm->isValid()){
$this->doctrine->getManager()->persist($ticket);
$this->doctrine->getManager()->flush();
$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'));
unset($ticket);
unset($contactForm);
$ticket = new TicketRequest();
$contactForm = $this->createForm(ContactType::class, $ticket);
}
$form = $contactForm->createView();
return $this->render('themes/phuongtrinh/post/page/lien-he.html.twig',[
'form' => $form
]);
}
public function search(Request $request, PaginatorInterface $paginator){
$keyword = $request->query->get("keyword","");
$page = $request->query->get('page',1);
if(empty($keyword) || strlen($keyword) < 4){
return $this->render('themes/phyto/post/page/search.html.twig');
}
$qb = $this->doctrine->getRepository(Article::class)
->createQueryBuilder('article');
$qb->leftJoin('article.post','post');
$keywordExpr = $qb->expr()->literal('%'.$keyword."%");
$qb->andWhere(
$qb->expr()->like('post.title', $keywordExpr)
);
$pagination = $paginator->paginate($qb->getQuery(), $page, self::ITEM_PER_SEARCHING_PAGE);
return $this->render('themes/phyto/post/page/search.html.twig',['pagination' => $pagination]);
}
public function handleContactForm(Request $request, TranslatorInterface $translator){
$ticket = new TicketRequest();
$form = $this->createForm(\App\Frontend\Form\ContactType::class, $ticket);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$this->doctrine->getManager()->persist($ticket);
$this->doctrine->getManager()->flush();
return new Response(json_encode(array(
'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')
)), 200);
}elseif(!$form->isValid()){
return new Response( $translator->trans('Vui lòng để lại các thông tin bắt buộc'), 403);
}
}
}