<?php
namespace App\Controller;
use App\ClassTheatre\asides;
use App\ClassTheatre\TheatreController;
use App\Service\Functions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class NewsletterController extends TheatreController
{
// {{{ _construct()
/** constructeur hérité
*
*/
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, TokenStorageInterface $tokenStorage, Functions $func)
{
// surbrillance du menu principal (thnet)
$GLOBALS['rub_courante'] = 'index';
parent::__construct($translator, $tokenStorage);
$GLOBALS['editset'] = 'newsletter';
$this->context['controller'] = 'newsletter';
$this->em = $em;
$this->func = $func;
$this->asides = new asides();
}
// }}}
/**
* @Route("/newsletter/", name="newsletter_view")
*/
public function action_view()
{
$liste = array();
$finder = new Finder();
$finder->files()
->in($this->getParameter('webDir') . '/newsletters/date')
->sortByName(true)
->reverseSorting();
if ($finder->hasResults()) {
foreach ($finder as $myFile) {
if (preg_match('/([0-9]{4}[^0-9][0-9]{2}[^0-9][0-9]{2})\.htm[l]?$/', $myFile, $regs)) {
if (isset($_REQUEST['show'])) {
$show = &$_REQUEST['show'];
if ($show == '' || $show == $regs[1]) {
/* redirige vers le dernier message ou celui spécifié (ex: 2008-10-24)*/
return $this->redirect('/newsletter/date/' . $regs[0]);
}
/* restriction de la liste au préfixe donné (ex: 2008-10)*/
if (strpos($regs[1], $show) !== 0) continue;
}
/* ajout à la liste */
$liste[] = array($regs[0], $regs[1]);
}
}
}
$this->view->liste = $liste;
// situation
$locate = array(0 => 'Historique des messages');
// title page
$title_page = 'Historique des messages de theatre-contemporain.net';
return $this->view('newsletter/view.html.twig');
}
/**
* @Route("/newsletter/date/{fileName}", name="newsletter_serve_file")
*/
public function action_serveFile($fileName)
{
$filePath = $this->getParameter('webDir') . "/newsletters/date/$fileName";
if (! file_exists($filePath)) {
throw new NotFoundHttpException();
}
$response = new Response(
/** @todo convertir les mauvais encodage, bon chance */
(file_get_contents($filePath))
);
return $response;
}
}