<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use App\Service\SessionManagerLMDV;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraints\DateTime;
/**
* Service class that declares some twig helpers in order to retrieve information in an easy way.
*
* @author jra
*
*/
class TwigExtension extends AbstractExtension
{
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* @var \Symfony\Component\Asset\Packages
*/
protected $assets;
protected $projectDir;
public function __construct(RequestStack $request_stack, Packages $assets, string $projectDir) {
$this->request = $request_stack->getCurrentRequest();
$this->assets = $assets;
$this->projectDir = $projectDir;
}
public function getFunctions()
{
return [
new TwigFunction('LMDV_getTitle', [$this, 'getTitle']),
new TwigFunction('LMDV_getStartDate', [$this, 'getDateDepart']),
new TwigFunction('LMDV_getEndDate', [$this, 'getEndDate']),
new TwigFunction('LMDV_getDepartureCity', [$this, 'getDepartureCity']),
new TwigFunction('LMDV_getDestinationCity', [$this, 'getDestinationCity']),
new TwigFunction('LMDV_getTotalPrice', [$this, 'getTotalPrice']),
new TwigFunction('LMDV_getTelephone', [$this, 'getTelephone']),
new TwigFunction('LMDV_getImage', [$this, 'getImage']),
new TwigFunction('LMDV_getTotalDay', [$this, 'getTotalDay']),
new TwigFunction('LMDV_getTotalNight', [$this, 'getTotalNight']),
new TwigFunction('LMDV_getProcess', [$this, 'getProcess']),
new TwigFunction('LMDV_hasPDFVoyage', [$this, 'hasPDFVoyage'])
];
}
/**
* Gets a random image from an static list. Width an height parameters are calculated
* because it allows browsers to calculate an aspect ratio and make place for the image
* in the layout *before* actually downloading the image
*
* @return array $output
* An array with the url, width & height
*/
public function getImage() {
// Static images array
$images = [
'build/images/femalehiker.jpg',
];
$key = array_rand($images, 1);
$filename = $this->assets->getUrl($images[$key]);
list($width, $height) = getimagesize($this->projectDir . '/public/' . $filename);
$output = [
'url' => $filename,
'width' => $width,
'height' => $height
];
return $output;
}
public function getTitle()
{
if ($this->request->getSession()->get('TWIG_title') != '') {
return $this->request->getSession()->get('TWIG_title');
}
else {
return 'N/A';
}
}
public function getDateDepart()
{
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8');
return $this->request->getSession()->get('departureDate') ? strftime("%d %B %Y", strtotime($this->request->getSession()->get('departureDate'))) : 'N/A';
}
public function getEndDate()
{
setlocale(LC_TIME, 'fr_FR.UTF8', 'fr.UTF8');
return $this->request->getSession()->get('endDate') ? strftime("%d %B %Y", strtotime($this->request->getSession()->get('endDate'))) : 'N/A';
}
public function getDepartureCity()
{
return $this->request->getSession()->get('GETINFO_departureCity') ? $this->request->getSession()->get('GETINFO_departureCity') : ($this->request->getSession()->get('departureCity') ? $this->request->getSession()->get('departureCity') : 'N/A');
}
public function getTelephone() {
if($this->request->getSession()->get('GETINFO_supportPhone') != '') {
return $this->request->getSession()->get('GETINFO_supportPhone');
}
else {
return '';
}
}
public function getTotalPrice()
{
if ($this->request->getSession()->get('TWIG_totalPrice') != '') {
$money = $this->request->getSession()->get('TWIG_totalPrice');
$str = $money->getDisplayValue(TRUE);
$str = str_replace('EUR', '€', $str);
return $str;
}
else {
return 'N/A';
}
}
public function getTotalNight() {
$start = $this->request->getSession()->get('departureDate');
$end = $this->request->getSession()->get('endDate');
if($start && $end) {
$date1 = new \DateTime($start);
$date2 = new \DateTime($end);
$diff = $date2->diff($date1)->format("%a");
return $diff;
}
else {
return 'N/A';
}
}
public function getTotalDay() {
$start = $this->request->getSession()->get('departureDate');
$end = $this->request->getSession()->get('endDate');
if($start && $end) {
$date1 = new \DateTime($start);
$date2 = new \DateTime($end);
$diff = $date2->diff($date1)->format("%a");
return $diff + 1;
}
else {
return 'N/A';
}
}
/**
* NOTE : This value is extracted from the URL so we introduce a dependency between
* a className and the URL structure. This should be avoided but as the PDF controller
* is shared between all user-cases-paths, this is currently the fastest way to identify
* what tunnel we are on. If URL structure changes, a mapping between the URL and the needed
* Process class name could be implemented here.
*
* After an HiPay paiement, we can not look at the URL so we trust the previously stored session value.
*
* @return string
* The string that represents the last namespace part of the null
*/
public function getProcess() {
$aux = $this->request->getPathInfo();
$aux = explode('/', $aux);
$process = $aux[1];
if($process == 'payment') {
return $this->request->getSession()->get('TWIG_process');
}
else {
$this->request->getSession()->set('TWIG_process', $process);
return $process;
}
}
public function hasPDFVoyage() {
return $this->request->getSession()->get('hasPDFVoyage');
}
}