<?php
namespace App\Controller;
use App\Entity\General;
use App\Entity\Sidebar;
use App\Entity\Sponsors;
use App\Entity\Usuari;
use App\Entity\Meteo;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContext;
use App\Entity\Temps;
use Symfony\Component\Translation\LocaleSwitcher;
use Symfony\Contracts\Translation\TranslatorInterface;
class InfoneuController extends AbstractController
{
private $em;
private $objUtil;
private $session;
private $mailer;
private $translator;
private $sPathUploads;
public function __construct(private LocaleSwitcher $localeSwitcher, EntityManagerInterface $em,Security $security,TranslatorInterface $translator,RequestStack $requestStack,MailerInterface $mailer)
{
$this->em = $em;
$this->objUtil = new \App\Util\Util($this->em);
$this->mailer = $mailer;
$this->session = $requestStack->getSession();
$this->translator = $translator;
$this->sPathUploads = realpath(dirname(__FILE__)) . '/../../public/uploads/';
}
public function indexAction(Request $peticion)
{
$aRequest = $peticion->request;
/////////////////////////
// Recoger dades generals
////////////////////////
$enGeneral = $this->em->getRepository(General::class)->findAll();
$query = $this->em->createQuery(
'SELECT p
FROM App:Pistes p
WHERE p.estatDia = :estat_dia
AND p.pista_relacionada is null
ORDER BY p.zones ASC
'
)->setParameter('estat_dia', '1');
$enPistes = $query->getResult();
$iTotalLongitudPises = 0;
$aZones = array();
$aPistes = array();
foreach ($enPistes as $key => $val)
{
$iTotalLongitudPises = $iTotalLongitudPises + $val->getLongitud();
$sFuntion = 'getNom' . ucfirst($peticion->attributes->get('_locale'));
$iIndex = 0;
//echo '-' . $val->getZones() . '-';
if (array_key_exists($val->getZones()->getNom() . '', $aPistes))
{
$iIndex = count($aPistes[$val->getZones()->getNom() . '']);
} else {
$aZones[] = $val->getZones()->getNom();
}
$aPistes[$val->getZones()->getNom() . ''][$iIndex]['nom'] = $val->$sFuntion();
$aPistes[$val->getZones()->getNom() . ''][$iIndex]['color'] = (method_exists($val->getColors(), 'getCodi')?$val->getColors()->getCodi():'000');
}
$query = $this->em->createQuery(
'SELECT p
FROM App:Remuntadors p
WHERE p.estatDia = :estat_dia
ORDER BY p.remuntadorsTipus ASC
'
)->setParameter('estat_dia', '1');
$enRemuntadors = $query->getResult();
$aTipus = array();
$aRemuntadors = array();
foreach ($enRemuntadors as $key => $val)
{
$sFuntion = 'getNom' . ucfirst($peticion->attributes->get('_locale'));
$iIndex = 0;
//echo '-' . $val->getRemuntadorsTipus() . '-';
if (array_key_exists($val->getRemuntadorsTipus()->$sFuntion() . '', $aRemuntadors))
{
$iIndex = count($aRemuntadors[$val->getRemuntadorsTipus()->$sFuntion() . '']);
} else {
$aTipus[] = $val->getRemuntadorsTipus()->$sFuntion();
}
$aRemuntadors[$val->getRemuntadorsTipus()->$sFuntion() . ''][$iIndex] = $val->getNom();
// echo $val->getNom();
}
$sFecha = $this->translator->trans(date("l")) . ', ' . date("d") . ' ' . $this->translator->trans('of ' . date("F")) . ' ' . $this->translator->trans('de') . ' ' . date("Y");
if (
(date("G") < 8
&& (date('W') == 0
|| date('W') == 6)
)
||
(
date("G") < 8
// ||
// (
// date("G") == 8
// && date("i") < 30
// )
&&
(
date('W') != 0
&& date('W') != 6
)
)
|| date("G") > 15
)
{
if (date("G") > 15)
{
$date = strtotime("+1 day");
$sFecha = $this->translator->trans(date("l", $date)) . ', ' . date("d", $date) . ' ' . $this->translator->trans('of ' . date("F", $date)) . ' ' . $this->translator->trans('de') . ' ' . date("Y", $date);
}
$sFecha = $this->translator->trans("PREVISIÓ PEL") . ' ' . $sFecha;
}
$enTemps = $this->em->getRepository(Temps::class)->findBy(
array(),
array( 'data' => 'DESC'),
3
);
if (date("G") <= 15)
{
unset($enTemps[2]);
}
$fecha = date('y/m/d');
$sQuery = "SELECT p "
. " FROM App:PrediccioMeteo p "
. " WHERE ";
/*
if (date('h') >= 18)
{
$sQuery .= " p.data > :now ";
} else {
$sQuery .= " p.data >= :now ";
}
*
*/
$sQuery .= " p.data = :now ";
$sQuery .= " ORDER BY p.data ASC";
$query = $this->em->createQuery($sQuery);
$query->setParameter('now', $fecha);
$enPrediccioMeteo = $query->getResult();
$enMeteo = $this->em->getRepository(Meteo::class)->findAll();
foreach ($enMeteo as $aMeteoPartial)
{
$aMeteo[$aMeteoPartial->getCodi()] = $aMeteoPartial;
//$sHora = ($aMeteoPartial->getDataExtrem() != ''?' ' . $this->translator->trans("a les") . ' ' . $aMeteoPartial->getDataExtrem()->format('H:i') . ' h ':'');
$sHora = '';
$aMeteo[$aMeteoPartial->getCodi()] = number_format( $aMeteoPartial->getValor(), $aMeteoPartial->getDecimals(), ",", ".") . ' ' . $aMeteoPartial->getUnitats() . $sHora;
}
//dump($aMeteo);
//die();
$aGeneral['iMin'] = $aGeneral['iMax'] = $enGeneral[0]->getMasellaCm();
if ($enGeneral[0]->getBoscCm() != '')
{
if ($enGeneral[0]->getBoscCm() > $aGeneral['iMax']) $aGeneral['iMax'] = $enGeneral[0]->getBoscCm();
if ($enGeneral[0]->getBoscCm() < $aGeneral['iMin']) $aGeneral['iMin'] = $enGeneral[0]->getBoscCm();
}
if ($enGeneral[0]->getTosaCm() != '')
{
if ($enGeneral[0]->getTosaCm() > $aGeneral['iMax']) $aGeneral['iMax'] = $enGeneral[0]->getTosaCm();
if ($enGeneral[0]->getTosaCm() < $aGeneral['iMin']) $aGeneral['iMin'] = $enGeneral[0]->getTosaCm();
}
if ($enGeneral[0]->getComaOriolaCm() != '')
{
if ($enGeneral[0]->getComaOriolaCm() > $aGeneral['iMax']) $aGeneral['iMax'] = $enGeneral[0]->getComaOriolaCm();
if ($enGeneral[0]->getComaOriolaCm() < $aGeneral['iMin']) $aGeneral['iMin'] = $enGeneral[0]->getComaOriolaCm();
}
if ($enGeneral[0]->getLaPletaCm() != '')
{
if ($enGeneral[0]->getLaPletaCm() > $aGeneral['iMax']) $aGeneral['iMax'] = $enGeneral[0]->getLaPletaCm();
if ($enGeneral[0]->getLaPletaCm() < $aGeneral['iMin']) $aGeneral['iMin'] = $enGeneral[0]->getLaPletaCm();
}
return $this->render('infoneu.html.twig', array(
'enGeneral' => $enGeneral,
'general' => $enGeneral[0],
'enPistes' => $enPistes,
'enRemuntadors' => $enRemuntadors,
'iTotalLongitudPises' => $iTotalLongitudPises,
'aZones' => $aZones,
'aPistes' => $aPistes,
'sFecha' => $sFecha,
'aTipus' => $aTipus,
'aRemuntadors' => $aRemuntadors,
'enTemps' => $enTemps,
'enPrediccioMeteo' => key_exists(0, $enPrediccioMeteo)?$enPrediccioMeteo[0]:arraY(),
'aMeteo' => $aMeteo,
'aGeneral' => $aGeneral
));
}
public function instagramAction(Request $peticion)
{
$aRequest = $peticion->request;
/////////////////////////
// Recoger dades generals
/////////////////////////
$enGeneral = $this->em->getRepository(General::class)->findAll();
$query = $this->em->createQuery(
'SELECT p
FROM App:Pistes p
WHERE p.estatDia = :estat_dia
AND p.pista_relacionada is null
ORDER BY p.zones ASC
'
)->setParameter('estat_dia', '1');
$enPistes = $query->getResult();
$iTotalLongitudPises = 0;
$aZones = array();
$aPistes = array();
foreach ($enPistes as $key => $val)
{
$iTotalLongitudPises = $iTotalLongitudPises + $val->getLongitud();
$sFuntion = 'getNom' . ucfirst($peticion->attributes->get('_locale'));
$iIndex = 0;
//echo '-' . $val->getZones() . '-';
if (array_key_exists($val->getZones()->getNom() . '', $aPistes))
{
$iIndex = count($aPistes[$val->getZones()->getNom() . '']);
} else {
$aZones[] = $val->getZones()->getNom();
}
$aPistes[$val->getZones()->getNom() . ''][$iIndex]['nom'] = $val->$sFuntion();
$aPistes[$val->getZones()->getNom() . ''][$iIndex]['color'] = (method_exists($val->getColors(), 'getCodi')?$val->getColors()->getCodi():'000');
}
$query = $this->em->createQuery(
'SELECT p
FROM App:Remuntadors p
WHERE p.estatDia = :estat_dia
ORDER BY p.remuntadorsTipus ASC
'
)->setParameter('estat_dia', '1');
$enRemuntadors = $query->getResult();
$aTipus = array();
$aRemuntadors = array();
foreach ($enRemuntadors as $key => $val)
{
$sFuntion = 'getNom' . ucfirst($peticion->attributes->get('_locale'));
$iIndex = 0;
//echo '-' . $val->getRemuntadorsTipus() . '-';
if (array_key_exists($val->getRemuntadorsTipus()->$sFuntion() . '', $aRemuntadors))
{
$iIndex = count($aRemuntadors[$val->getRemuntadorsTipus()->$sFuntion() . '']);
} else {
$aTipus[] = $val->getRemuntadorsTipus()->$sFuntion();
}
$aRemuntadors[$val->getRemuntadorsTipus()->$sFuntion() . ''][$iIndex] = $val->getNom();
// echo $val->getNom();
}
$sFecha = $this->translator->trans(date("l")) . ', ' . date("d") . ' ' . $this->translator->trans('of ' . date("F")) . ' ' . $this->translator->trans('de') . ' ' . date("Y");
if (date("G") > 15 || date("G") < 7)
{
$date = strtotime("+1 day");
$sFecha = $this->translator->trans(date("l", $date)) . ', ' . date("d", $date) . ' ' . $this->translator->trans('of ' . date("F", $date)) . ' ' . $this->translator->trans('de') . ' ' . date("Y", $date);
$sFecha = $this->translator->trans("PREVISIÓ PEL") . ' ' . $sFecha;
}
$enTemps = $this->em->getRepository(Temps::class)->findBy(
array(),
array( 'data' => 'DESC'),
3
);
if (date("G") <= 15)
{
unset($enTemps[2]);
}
$fecha = date('y/m/d');
$sQuery = "SELECT p "
. " FROM App:PrediccioMeteo p "
. " WHERE ";
/*
if (date('h') >= 18)
{
$sQuery .= " p.data > :now ";
} else {
$sQuery .= " p.data >= :now ";
}
*
*/
$sQuery .= " p.data = :now ";
$sQuery .= " ORDER BY p.data ASC";
$query = $this->em->createQuery($sQuery);
$query->setParameter('now', $fecha);
$enPrediccioMeteo = $query->getResult();
$enMeteo = $this->em->getRepository(Meteo::class)->findAll();
foreach ($enMeteo as $aMeteoPartial)
{
$aMeteo[$aMeteoPartial->getCodi()] = $aMeteoPartial;
//$sHora = ($aMeteoPartial->getDataExtrem() != ''?' ' . $this->translator->trans("a les") . ' ' . $aMeteoPartial->getDataExtrem()->format('H:i') . ' h ':'');
$sHora = '';
$aMeteo[$aMeteoPartial->getCodi()] = number_format( $aMeteoPartial->getValor(), $aMeteoPartial->getDecimals(), ",", ".") . ' ' . $aMeteoPartial->getUnitats() . $sHora;
}
//dump($aMeteo);
//die();
$aZonaManual[0][0] = 'TSC | 4';
$aZonaManual[0][1] = 'TSD | 4';
$aZonaManual[0][2] = 'TSD | 6';
$aZonaManual[1][0] = 'TQ | 1';
$aZonaManual[2][0] = 'CT | 1';
$aZonaManual[2][1] = 'TCD | 1';
//dump($aRemuntadors);
//die();
return $this->render('infoneuInstagram.html.twig', array(
'enGeneral' => $enGeneral,
'general' => $enGeneral[0],
'enPistes' => $enPistes,
'enRemuntadors' => $enRemuntadors,
'iTotalLongitudPises' => $iTotalLongitudPises,
'aZones' => $aZones,
'aPistes' => $aPistes,
'sFecha' => $sFecha,
'aTipus' => $aTipus,
'aRemuntadors' => $aRemuntadors,
'enTemps' => $enTemps,
'enPrediccioMeteo' => (array_key_exists(0, $enPrediccioMeteo)? $enPrediccioMeteo[0]:array()),
'aMeteo' => $aMeteo,
'aZonaManual' => $aZonaManual
));
}
public function pdfAction(Request $peticion)
{
$sIdioma = $peticion->attributes->get('_locale');
$sSubName = 'INFONEU';
if ($sIdioma != 'ca')
{
$sIdioma = 'es';
$sSubName = 'INFONIEVE';
}
$dData = date('Y-m-d');
$sName = $dData . '_' . $sSubName . '.pdf';
$sUrlDesti = $this->sPathUploads . 'infoneu/' . $sName;
$sUrl = 'https://www.masella.com/' . $sIdioma . '/infoneu';
// echo $sName;
// echo '<br>';
// echo $sUrl;
// die();
// $sUrl = 'http://masella15.developer.entorno.es/app_dev.php/' . $peticion->attributes->get('_locale') . '/infoneu';
// mail ('manuel@entorno.es','resultados/controller/resultats_pdf', $sUrl . '<br>' . "wkhtmltopdf $sUrl " . $sUrlDesti);
/*
* Important: l'usuari ha de ser apache per que pugui executar el wkhtmltopdf
*/
//$blah = shell_exec("/bin/wkhtmltopdf $sUrl " . $sUrlDesti . ' --footer-center "Masella infoneu" --footer-right "[page] de [toPage]"');
// echo $sUrlDesti;
// echo '<br>';
$blah = shell_exec("/usr/local/bin/wkhtmltopdf --margin-top 5 --margin-bottom 0 --margin-left 5 --margin-right 5 $sUrl " . $sUrlDesti);
// echo $blah;
// echo 'hola';
// die();
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header ("Content-disposition: attachment; filename=" . $sName);
header ("Content-type: application/pdf");
header('Content-Length: ' . filesize($sUrlDesti));
readfile ($sUrlDesti);
unlink($sUrlDesti);
die();
}
}