<?php
namespace App\Controller\web;
use App\Entity\Contact;
use App\Entity\Product;
use App\Entity\WeddingCalculator;
use App\Form\web\ContactType;
use App\Form\web\WeddingCalculatorType2;
use App\Repository\ArticleRepository;
use App\Repository\CategoryRepository;
use App\Repository\InfoTableRepository;
use App\Repository\PhotoAlbumRepository;
use App\Repository\ProductRepository;
use App\Repository\VideoRepository;
use App\Service\AdminUtils;
use App\Service\MailingService;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
private $categoryRepo;
private $productRepo;
private $infoRepo;
private $requestStack;
private $mailer;
private $mailing;
private $em;
private $articleRepo;
private $paginator;
private $photoAlbumRepo;
private $adminUtils;
private $videoRepo;
public function __construct(
CategoryRepository $categoryRepository,
ProductRepository $productRepository,
InfoTableRepository $infoRepo,
RequestStack $requestStack,
\Swift_Mailer $mailer,
MailingService $mailing,
EntityManagerInterface $em,
ArticleRepository $articleRepository,
PaginatorInterface $paginator,
PhotoAlbumRepository $photoAlbumRepository,
AdminUtils $adminUtils,
VideoRepository $videoRepository
)
{
$this->infoRepo = $infoRepo;
$this->categoryRepo = $categoryRepository;
$this->productRepo = $productRepository;
$this->requestStack = $requestStack;
$this->mailer = $mailer;
$this->mailing = $mailing;
$this->em = $em;
$this->articleRepo = $articleRepository;
$this->paginator = $paginator;
$this->photoAlbumRepo = $photoAlbumRepository;
$this->adminUtils = $adminUtils;
$this->videoRepo = $videoRepository;
}
/**
* @Route("/", name="web_home")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexPage(){
return $this->render('web/index.html.twig', [
"category" => $this->categoryRepo->findBy(["isActive" => true]),
"promoted" => $this->productRepo->findBy(["isActive" => true, "promoted" => true]),
"latestVideos" => $this->videoRepo->findBy(["isActive" => true], ["createdAt" => "DESC"], 3),
]);
}
/**
* @Route("/info/{slug}", name="web_info")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function infoPage($slug){
$info = $this->infoRepo->findOneBy(["slug" => $slug, "isActive" => true]);
if(!$info) {
return $this->redirectToRoute("web_home");
}
return $this->render('web/info.html.twig', [
"info" => $info
]);
}
/**
* @Route("/proizvodi", name="web_products")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function productsPage(){
return $this->render('web/products.html.twig', [
"category" => $this->categoryRepo->findBy(["isActive" => true]),
]);
}
/**
* @Route("/proizvod/{slug}/{id}", name="web_product_single")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function productSinglePage($slug, Product $id){
$categoryProducts = $this->productRepo->findBy(["category" => $id->getCategory(), "isActive" => 1]);
if(!$id->getIsActive()){
return $this->redirectToRoute("web_home");
}
return $this->render('web/product-details.html.twig', [
"product" => $id,
"related" => $categoryProducts
]);
}
/**
* @Route("/usluge", name="web_services")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function servicesPage(){
return $this->render('web/services.html.twig');
}
/**
* @Route("/novosti", name="web_blog")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function blogPage(Request $request){
$qb = $this->articleRepo->findActiveQueryBuilder();
$pagination = $this->paginator->paginate(
$qb,
$request->query->getInt('page', 1),
6
);
return $this->render('web/blog.html.twig', [
'articles' => $pagination,
]);
}
/**
* @Route("/novosti/{slug}", name="web_article")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function articlePage(string $slug){
$article = $this->articleRepo->findBySlug($slug);
if (!$article) {
return $this->redirectToRoute('web_blog');
}
return $this->render('web/blog-detail.html.twig', [
'article' => $article,
]);
}
/**
* @Route("/videoteka", name="web_videos")
*/
public function videosPage()
{
$videos = $this->videoRepo->findBy(['isActive' => true], ['sortOrder' => 'ASC', 'createdAt' => 'DESC']);
return $this->render('web/videos.html.twig', [
'videos' => $videos,
]);
}
/**
* @Route("/pitaj-sladanu", name="web_chatbot_page")
*/
public function chatbotPage()
{
return $this->render('web/chatbot-page.html.twig');
}
/**
* @Route("/galerija", name="web_gallery")
*/
public function galleryPage()
{
$albums = $this->photoAlbumRepo->findBy(['isActive' => true], ['sortOrder' => 'ASC', 'createdAt' => 'DESC']);
return $this->render('web/gallery.html.twig', [
'albums' => $albums,
]);
}
/**
* @Route("/galerija/{slug}", name="web_gallery_album")
*/
public function galleryAlbumPage(string $slug)
{
$album = $this->photoAlbumRepo->findOneBy(['slug' => $slug, 'isActive' => true]);
if (!$album) {
return $this->redirectToRoute('web_gallery');
}
return $this->render('web/gallery-detail.html.twig', [
'album' => $album,
]);
}
/**
* @Route("/kontakt", name="web_contact")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function contactPage(){
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact);
$request = $this->requestStack->getCurrentRequest();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid() && $this->mailing->verifyRecaptcha($request->get('g-recaptcha-response'))){
$contact->setType('contact');
$uploadedFiles = $request->files->get('contact_photos', []);
if ($uploadedFiles) {
$paths = $this->adminUtils->handleInquiryPhotos(is_array($uploadedFiles) ? $uploadedFiles : [$uploadedFiles]);
if ($paths) {
$contact->setPhotos($paths);
}
}
$this->em->persist($contact);
$this->em->flush();
$this->mailing->sendContact($contact);
$this->addFlash('success', 'Vaša poruka je poslana!');
return $this->redirectToRoute('web_contact');
}elseif($form->isSubmitted() && $form->isValid() && !$this->mailing->verifyRecaptcha($request->get('g-recaptcha-response'))){
$this->addFlash(
'error_rec',
'Potvrdite da niste robot!'
);
}
return $this->render('web/contact.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/svadbeni-kalkulator", name="web_calculator")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function calculatorPage2(){
$weddingCalculator = new WeddingCalculator();
$form = $this->createForm(WeddingCalculatorType2::class, $weddingCalculator);
$form->handleRequest($this->requestStack->getCurrentRequest());
if($form->isSubmitted() && $form->isValid() && $this->mailing->verifyRecaptcha($this->requestStack->getCurrentRequest()->get('g-recaptcha-response'))){
$weddingCalculator->setAdditionalCake(null);
if($form->get('eventDate')->getData()){
$eventTime = strtotime($form->get('eventDate')->getData());
$eventDate = date('Y-m-d H:i:s',$eventTime);
$weddingCalculator->setEventDate(new \DateTime($eventDate));
}
$this->em->persist($weddingCalculator);
$this->em->flush();
$this->mailing->sendCalculator($weddingCalculator);
$this->addFlash('success', 'Vaša zahtjev je poslan!');
return $this->redirectToRoute('web_calculator');
}elseif($form->isSubmitted() && $form->isValid() && !$this->mailing->verifyRecaptcha($this->requestStack->getCurrentRequest()->get('g-recaptcha-response'))){
$this->addFlash(
'error_rec',
'Potvrdite da niste robot!'
);
}
return $this->render('web/calculator-2.html.twig', [
'form' => $form->createView()
]);
}
}