<?php
namespace App\Controller;
use App\ClassTheatre\asides;
use App\ClassTheatre\TheatreController;
use App\Service\Functions;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use TheatreCore\Entity\Contacts;
use TheatreCore\Entity\Spectacles;
class EditionsController 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'] = 'editions';
parent::__construct($translator, $tokenStorage);
$this->em = $em;
$this->asides = new asides();
$this->func = $func;
$this->spectacles = $em->getRepository(Spectacles::class);
$this->contacts = $em->getRepository(Contacts::class);
$this->perPage = 1000;
}
/**
*
* @Route("/editions", name="editions")
*
*/
public function action_view(Request $request)
{
// alphabet
$alphabet_simple = $this->func->getAlphaUpper();
// recherche par lettre soit on a le parametre,
// soit une lettre au hasard
if(isset($_GET['lettre']) && strlen($_GET['lettre'])==1) {
$alpha = strtoupper($_GET['lettre']);
} else {
$aff = array_rand($alphabet_simple);
if(isset($alphabet_simple[$aff])) {
$alpha = $alphabet_simple[$aff];
} else {
$alpha = 'a';
}
}
// the lettre
$this->view->alpha = strtoupper($alpha);
foreach($alphabet_simple as $lettre=>$upperlettre) {
$alphabet[$lettre]['nom'] = $upperlettre;
if($upperlettre==$alpha) {
$alphabet[$lettre]['active'] = 'active';
}
}
$this->view->alphabet = $alphabet;
// liste des éditeurs
$query_normal = 'SELECT DISTINCT(c.idcontact), c.organisation, c.country, c.city, c.url_clean FROM contacts c';
$query = ' WHERE 1
AND c.idcontact IN
(SELECT DISTINCT(texts.idcontact_publisher) FROM texts WHERE texts.publish=1)';
$total_editeurs = $this->contacts->queryAll($query_normal.$query);
$this->view->nb_editeurs = count($total_editeurs);
// franco
$nb_editeurs_francophones = 0;
foreach($total_editeurs as $k=>$v) {
if($v['country']=='FR') {
$nb_editeurs_francophones++;
}
}
$this->view->nb_editeurs_francophones = $nb_editeurs_francophones;
if(!empty($alpha)) {
$query .= ' AND c.organisation REGEXP \'^'.$alpha.'\' ';
}
$query .= ' ORDER BY c.organisation';
$this->contacts->query($query_normal.$query);
while($this->contacts->fetch()) {
// nb de texte
$nb_texts = $this->contacts->countObjects('text','idcontact_publisher',$this->contacts->idcontact);
$editeur_infos = array(
'organisation' => $this->contacts->cutSentence($this->contacts->organisation,40),
'city' => $this->contacts->city,
'country' => $this->contacts->getNameCountry($this->contacts->country,$this->lng),
'url_clean' => $this->contacts->url_clean,
'nb_texts' => $nb_texts,
);
if($nb_texts>1) {
$editeur_infos['nb_texts_pluriel'] = 's';
}
// on sépare FR et Inter
if($this->contacts->country=='FR') {
$liste_editeurs_alpha_fr[$this->contacts->idcontact] = $editeur_infos;
} else {
$liste_editeurs_alpha_int[$this->contacts->idcontact] = $editeur_infos;
}
// tableau avec tous
$liste_editeurs_alpha[$this->contacts->idcontact] = $editeur_infos;
}
$this->contacts->free();
// liste des éditeurs à la vue
if(!empty($liste_editeurs_alpha)) {
if (!empty($liste_editeurs_alpha_fr)) $this->view->liste_editeurs_fr = $liste_editeurs_alpha_fr;
if (!empty($liste_editeurs_alpha_int)) $this->view->liste_editeurs_int = $liste_editeurs_alpha_int;
} else {
$this->view->no_editeur_alpha = true;
}
// ==> moteur de recherche edition
// formulaire de saisie recherche par titre
$this->context['formAsideSearch'] = $this->get('form.factory')->createNamedBuilder('', FormType::class);
$this->context['formAsideSearch']->add('organisation', TextType::class, [
'label' => 'Mots du titre',
'attr' => [
'size' => '35',
'placeholder' =>'Rechercher un éditeur'
]
]);
$this->context['formAsideSearch']->add('search_editeur', SubmitType::class, [
'label' => 'OK',
'attr' => [
'class'=>'boutform'
]
]);
if(isset($_GET['aide-recherche'])) {
$this->view->recherche_texts = true;
$this->view->aide_recherche = true;
$this->view->resultat = true;
}
$type_recherche = false;
$this->getCommonSearchView($request);
if($this->context['formAsideSearch']->isSubmitted() || isset($_GET['page'])) {
$this->view->liste_editeurs = array();
if (isset($_REQUEST['search_editeur']) || isset($_REQUEST['search'])) {
// on recherche un éditeur
if(isset($_REQUEST['search_editeur'])) {
// ids éditeurs
foreach($total_editeurs as $k=>$v) {
$edit[] = $v['idcontact'];
}
$organisation = $this->context['formAsideSearch']->get('organisation')->getData();
if(empty($organisation) && isset($_GET['organisation'])) {
$organisation = $_GET['organisation'];
}
$sql_count = 'SELECT COUNT(DISTINCT(c.idcontact)) as total ';
$sql_normal = 'SELECT c.idcontact,c.url_clean,c.organisation,cty.'.$GLOBALS['lng'].', c.country ';
$sql = 'FROM contacts c
LEFT JOIN countries cty ON c.country=cty.id
WHERE c.organisation LIKE "%'.$this->contacts->escape($organisation).'%"
AND c.type=1
AND c.idcontact IN ('.join(',',$edit).') ORDER BY c.organisation';
//echo $sql_normal.$sql;
$type_recherche = 'recherche_editeurs';
$this->view->organisation = $organisation;
$this->view->autre_editeur = 'autre';
}
// nombre d'enregistrement
$totalItems = $this->contacts->queryOne($sql_count.$sql);
if(!empty($totalItems) && $totalItems>0) {
$pagerOptions['totalItems'] = $totalItems;
// pagination
$pagerOptions['delta'] = 2;
$pagerOptions['perPage'] = 30;
$paged_data = $this->getPagination($totalItems,$this->perPage,$pagerOptions);
// ajout des criteres de tri apres avoir fait le count
if(isset($tri)) {
$sql .= $tri;
}
//limite
$limit = ' LIMIT '.$this->getLimitStart().','.$this->perPage;
// executer la requete et mettre en forme chaque reponse
$this->contacts->query($sql_normal.$sql.$limit);
$liste_editeurs_fr = [];
$liste_editeurs_int = [];
while($this->contacts->fetch()) {
$idcontact = $this->contacts->idcontact;
$nb_texts = $this->contacts->countObjects('text','idcontact_publisher',$idcontact);
$editeur_infos = array(
'organisation' => $this->contacts->organisation,
'country' => $this->contacts->{$this->lng},
'url_clean' => $this->contacts->url_clean,
'nb_texts' => $nb_texts,
);
if($nb_texts>1) {
$editeur_infos['nb_texts_pluriel'] = 's';
}
// on sépare FR et Inter
if($this->contacts->country=='FR') {
$liste_editeurs_fr[$idcontact] = $editeur_infos;
} else {
$liste_editeurs_int[$idcontact] = $editeur_infos;
}
// tableau avec tous
$liste_editeurs[$idcontact] = $editeur_infos;
}
//passer les resultats au template
if(!empty($liste_editeurs)) {
// $this->view->liste_editeurs = $liste_editeurs;
$this->view->liste_editeurs_fr = $liste_editeurs_fr;
$this->view->liste_editeurs_int = $liste_editeurs_int;
$this->view->alpha = false;
}
// liens du pager
// $paged_data['links'] = str_replace('editions/view/index.php?controller=editions&action=view&', 'editeurs?', $paged_data['links']);
}
$this->view->$type_recherche = $type_recherche;
$this->view->resultat = true;
}
}
// titre de la page
$this->title_page = 'Les maisons d\'édition de théâtre';
// situation
$this->locate = array(0 => 'Maisons d\'édition de théâtre');
$this->rub_courante = 'editions';
$this->asides->load(['recherche_editeur.inc' => array(
'exec' => ['htmlstart' => true, 'color'=>'context'],
'action' => '/editions'
)]);
$this->asides->load(['recherche_alphabetique' => array(
'exec' => ['htmlend' => true, 'color' => 'alternative'],
'action' => '/editions/'
)]);
$this->asides->load('bio_editeur');
$this->context['view']->aside = $this->asides->view();
return $this->view('editions/view.html.twig', [
'controller_name' => 'EditionsController',
]);
}
private function getTextsInfoR($sql,$texts) {
$liste_texts = array();
$texts->query($sql);
$format_flou = $format = array('width'=>getenv('VIGN_TEXT_WIDTH'),'height'=>getenv('VIGN_TEXT_HEIGHT'),'direction'=>'thumb');
$format_flou['blur'] = true;
while($texts->fetch()) {
$idtext = $texts->idtext;
$liste_texts[$idtext] = $texts->getInfosTextRow();
$liste_texts[$idtext]['publisher_first'] = true;
$liste_texts[$idtext]['stylemore'] = array('imagep' => 'width:60px;', 'desctxt' => 'width:195px;');
if(count($liste_texts[$idtext]['allauthors'])>5) {
$liste_texts[$idtext]['collectif'] = true;
}
$liste_texts[$idtext]['vignette'] = $texts->getVignetteText($idtext,$format);
$liste_texts[$idtext]['vignette_flou'] = $texts->getVignetteText($idtext,$format_flou);
$liste_texts[$idtext]['publisher']['country'] = $texts->getNameCountry($texts->publisher_country,$GLOBALS['lng']);
}
$texts->free();
return $liste_texts;
}
}