<?php
// src/Controller/HomeController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Templating\EngineInterface;
use App\Entity\Events;
use App\Entity\EventSlugs;
use App\Entity\Orders;
use App\Entity\EventTickets;
use App\Entity\Language;
use App\Entity\Sites;
use App\Entity\ShortUrl;
use App\Entity\Pages;
class PageController extends BaseController
{
private $googleData = array();
private $pageData = array();
/**
* @Route("/css/{template}/{file}.{px}x.css", name="css_convert_px")
* @Route("/css/{template}/{file}.css", name="css_convert")
*/
public function cssAction($template,$file,$px=false) {
$response = $this->getResponse();
$response->headers->set('Content-Type', 'text/css');
// replace this example code with whatever you need
return $this->render(
$template.DIRECTORY_SEPARATOR.'css'.DIRECTORY_SEPARATOR.$file.'.css.twig',
[
'pixelratio' => $px
],
$response
);
}
/**
* @Route("/postal/lookup", name="postal_lookup")
*/
public function postalLookupAction() {
$result = $this->getPostalService()->getResults();
return new JsonResponse($result);
}
/**
* @Route("/order/{order}/complete", name = "payment_complete")
* @Route("/order/{event}/complete/{order}", name = "payment_complete_event")
*/
public function completeAction(Orders $order, $event = '')
{
// Welcome back....
// Process Payment state visually to the customer.
//return new Response("test");
$items = $order->getItems();
$cntTickets = 0;
$product = false;
$this->googleData['transactionId'] = $order->getId();
$this->googleData['transactionAffiliation'] = $order->getSites()->getName();
$this->googleData['transactionTotal'] = $order->getTotal();
$this->googleData['transactionTax'] = $order->getTax();
foreach($items as $item) {
if($item->getType() == 'ticket') {
$cntTickets += $item->getAmount();
$product = $item->getProduct();
if($product !== false) {
// Handle all orders....
$productObj = $this->getDoctrine()
->getRepository(EventTickets::class)
->findOneBy(array('Id'=>array($product)),array('Id'=>'ASC'),1,0);
$this->googleData['transactionProducts'][] = array(
'sku' => $productObj->getId(),
'name' => $productObj->getFullIdent(),
'price' => $productObj->getPrice(),
'quantity' => $item->getAmount(),
);
}
}
}
$this->googleData['amountTickets'] = $cntTickets;
$this->googleData['eventDateID'] = '';
if($product !== false) {
// Handle all orders....
$product = $this->getDoctrine()
->getRepository(EventTickets::class)
->findOneBy(array('Id'=>array($product)),array('Id'=>'ASC'),1,0);
$event = $product->getEvents();
$dates = $product->getEventDates();
$gtin = $event->getId().'-'.$dates->getId();
$this->googleData['eventDateID'] = $gtin;
$this->googleData['eventID'] = $event->getId();
}
return $this->home();
}
/**
* @Route("/contact/submit", name = "contact_submit")
*/
public function submitAction()
{
// Handle form submission
return new JsonResponse(array());
}
private function toTrengo() {
}
/**
* @Route("/language/set/{language}", name = "language_set")
*/
public function languageSetAction($language)
{
// Welcome back....
// Process Payment state visually to the customer.
//return new Response("test");
$manager = $this->getDoctrine()->getManager();
$result = $manager->getRepository(Language::class)->findOneBy(array("code"=>$language));
$language = $result->getId();
$session = $this->getSession();
$session->set('language',$language);
return $this->getJsonResponse(array('language'=>$language));
}
/**
* Matches /pages/{id}/info exactly
*
* @Route("/pages/{page_id}/info", name="pages_info")
* @Route("/pages/{page_id}/info/{clean}", name="pages_info_clean")
*/
public function page_info($page_id = false, $clean = false, SessionInterface $session, EngineInterface $twig)
{
$this->session = $session;
$current_language_id = (null !== $session->get('language')) ? $session->get('language') : 1;
$langKey = 'nl';
$langKey = ($current_language_id == 1) ? 'nl' : $langKey;
$langKey = ($current_language_id == 2) ? 'en' : $langKey;
$manager = $this->getDoctrine()->getManager();
$result = $manager->getRepository(Pages::class)->findOneBy(array("Id"=>$page_id));
$site = $manager->getRepository(Sites::class)->findCurrentSite();
$tpl = $site->getTemplateFolder();
$descs = $result->getDescription();
$desc = false;
foreach($descs as $descObj) {
if($descObj->getLanguage()->getId() == $current_language_id) {
// Force current language for now...
$desc = $descObj->getDescription();
}
}
$page_message = '';
// OK now the available dates..
$data = array('page_message'=>$page_message,'page_description'=>$desc, 'lang'=>$langKey, 'clean'=>$clean);
if($twig->exists($tpl.'/page_description'.$page_id.'.html.twig')) {
return $this->render($tpl.'/page_description'.$page_id.'.html.twig',$data);
}
return $this->render($tpl.'/page_description.html.twig',$data);
}
/**
* @Route("/pages/all/page", name = "page_load_all")
*/
public function loadPagesAction()
{
$pageData = array();
$manager = $this->getDoctrine()->getManager();
$site = $manager->getRepository(Sites::class)->findCurrentSite();
if($site instanceof Sites) {
$result = $manager->getRepository(Pages::class)->findBySite(array('site'=>$site));
foreach($result as $page) {
$data = $this->getPageData($page);
$pageData[$page->getId()] = $data[$page->getId()];
}
}
// $pages[$page->getId()]['id'] = $page->getId();;
return new JsonResponse(array('pages'=>$pageData));
}
/**
* @Route("/pages/{id}/page", name = "page_load", requirements={"id"="\d+"})
*/
public function loadPageAction(Pages $page)
{
$pageData = $this->getPageData($page);
// $pages[$page->getId()]['id'] = $page->getId();;
return new JsonResponse(array('pages'=>$pageData));
}
/**
* @Route("/pages/{slug}/page", name = "page_load_slug")
*/
public function loadPageSlugAction(Pages $page)
{
$pageData = $this->getPageData($page);
// $pages[$page->getId()]['id'] = $page->getId();;
return new JsonResponse(array('pages'=>$pageData,'page_id'=>$page->getId()));
}
public function getPageData($page) {
$id = $page->getId();
$pageData[$id]['id'] = $id;
$pageData[$id]['slug'] = $page->getSlug();
$pageData[$id]['slot'] = $page->getSlot();
$pageData[$id]['page_title'] = $page->getDescription()[0]->getTitle();
$pageData[$id]['special_title'] = $page->getDescription()[0]->getSpecialTitle();
if($pageData[$id]['special_title'] === null) {
$pageData[$id]['special_title'] = '';
}
$pageData[$id]['header'] = array();
$headerObj = $page->getPageHeader();
$imageObj = $page->getImagesByType('page');
$image = false;
foreach($imageObj as $x=>$image) {
if($image instanceof \App\Entity\PageImage) {
if(substr($image->getFile(),-5) == '.webm') {
//skip
} elseif(substr($image->getFile(),-4) == '.mp4') {
//skip
} elseif(substr($image->getFile(),-4) == '.pdf') {
//skip
} else {
if(!isSet($pageData[$id]['image'])) {
$pageData[$id]['image'] = $image->getFile();
}
}
}
}
return $pageData;
}
/**
* Matches / exactly
*
* @Route("/", name="home",condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/impersonate", name="page_slug")
* @Route("/{lang}", name="home_en", requirements={"lang"="en|fr|nl|de"},condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/event/{slug}", name="home_event")
* @Route("/event/{slug}/", name="home_event_sl")
* @Route("/{lang}/event/{slug}", name="home_event_sl_lang", requirements={"lang"="en|fr|nl|de"},condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/tour/{slug}", name="home_tour")
* @Route("/tour/{slug}/", name="home_tour_sl")
* @Route("/project/{slug}", name="home_project")
* @Route("/project/{slug}/", name="home_project_sl")
* @Route("/{lang}/tour/{slug}", name="home_tour_sl_lang",requirements={"lang"="en|fr|nl|de"},condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/order/{slug}", name="home_order_event")
* @Route("/order/{slug}/", name="home_order_event_sl")
* @Route("/order/{slug}/{hiddencode}/{event}/", name="order_hidden_show")
* @Route("/{lang}/order/{slug}/", name="home_order_event_sl",requirements={"lang"="en|fr|nl|de"},condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/{short}", name="short_url", methods={"GET"}, condition="request.headers.get('Host') matches '/www\./i'")
* @Route("/page/{short}", name="short_url_page", condition="request.headers.get('Host') matches '/www\./i'")
*/
public function home($slug=false,$event=false,$hiddencode=false,$lang=false, $short=false)
{
$eventUrl = $event;
$event = false;
$page = false;
$result = false;
$pFields = array();
if($short !== false) {
// Nothing else... Check the Short URL Database!
$manager = $this->getDoctrine()->getManager();
$shortObj = $manager->getRepository(ShortUrl::class)->findOneBy(array('url'=>$short));
if($shortObj instanceof ShortUrl) {
$event = $shortObj->getEvents();
$url = $this->generateUrl('home_event',array('slug'=>$event->getSlug()));
return new RedirectResponse($url);
}
// Not a short object... What is it then?
// It can be a page!
$pageObj = $manager->getRepository(Pages::class)->findOneBy(array('slug'=>$short));
if($pageObj instanceof Pages) {
$page = $pageObj->getId();
}
}
if($slug !== false) {
// Fetch Event?
$manager = $this->getDoctrine()->getManager();
$result = $manager->getRepository(Events::class)->findOneBy(array("slug"=>$slug));
if($result) {
// Event found.. Load single event page...
$event = $result->getId(); // Fill with event ID.... Twig does the rest...
}
if(!$result) {
// Old slug maybe??
$result = $manager->getRepository(EventSlugs::class)->findOneBy(array("slug"=>$slug));
if($result) {
// Event found.. Load single event page...
$event = $result->getEvents()->getId(); // Fill with event ID.... Twig does the rest...
}
}
}
$manager = $this->getDoctrine()->getManager();
if($lang !== false) {
// We have a code...
$language = $manager->getRepository(Language::class)->findOneBy(array('code'=>$lang));
if($language) {
$lang = $language->getId();
}
}
$site = $manager->getRepository(Sites::class)->findCurrentSite();
if($site instanceof Sites) {
$site_id = $site->getId();
$tpl = $site->getTemplateFolder();
$sitename = $site->getName();
} else {
$site_id = 0;
$tpl = 'default';
$sitename = 'Default';
}
$session = $this->getSession();
$sesslang = false;
if($session->has('language')) {
$sesslang = $session->get('language');
}
$current_language_id = (false !== ($sesslang)) ? $sesslang : 1;
if($lang !== false && ($current_language_id != $lang || false === $sesslang)) {
$session->set('language',$lang); // Set new language....
$current_language_id = $lang; // Override current language...
} elseif(false === $sesslang) {
$session->set('language',$current_language_id); // Set the language in session....
}
$projects = $manager->getRepository(Events::class)->findUpcomingProjects($site,$current_language_id);
$pFields = $this->generateFields($site_id);
$templateData = array(
'event' => $event,
'page' => $page,
'site_name' => $sitename,
'eventUrl' => $eventUrl,
'hiddencode' => $hiddencode,
'eventdata' => $result,
'personal_fields' => $pFields,
'field_groups' => 3,
'cur_lang' => $current_language_id,
'projects' => $projects,
'googledata' => $this->googleData
);
return $this->render($tpl.'/index.html.twig',$templateData);
}
public function generateFields($site_id) {
$pFields = array();
$container =
$path = $this->get('kernel')->getProjectDir()."/config/fields/";
if(file_exists($path.'site_'.$site_id.'.fields.php')) {
include_once($path.'site_'.$site_id.'.fields.php');
}
return $pFields;
}
}