<?php
namespace TheatreCore\Repository;
use TheatreCore\Entity\Bannieres;
use TheatreCore\Traits\TheatreTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* BannieresRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class BannieresRepository extends ServiceEntityRepository
{
use TheatreTrait;
private $em;
public $__table;
public $__table_object;
public $__idtable;
public function __construct(ManagerRegistry $registry, TranslatorInterface $tr)
{
parent::__construct($registry, Bannieres::class);
$this->__table = $this->getClassMetadata()->getTableName();
$this->__table_object = $this->getClassMetadata()->name;
$this->__idtable = $this->getClassMetadata()->getSingleIdentifierFieldName();
$this->translator = $tr;
}
// {{{ findData()
/** Recherche d'banniere
*
* Permet de rechercher des bannieres selon différents critères
*
* @access public
* @see Theatre::find()
*/
public function findData($idclassification = null)
{
// parametres
if (isset($_REQUEST['keywords']) && $_REQUEST['keywords'] != 'nom') {
$keywords = filter_var(trim($_REQUEST['keywords']));
}
if (isset($_REQUEST['idbanniere']) && $this->my_is_int($_REQUEST['idbanniere'])) {
$idbanniere = (int)$_REQUEST['idbanniere'];
}
$query = $this->createQueryBuilder('e');
if ($this->getCount()) {
$query->select('COUNT(e.idbanniere)');
$this->setCount(false);
return $query;
}
// query perso
// $query = 'SELECT * FROM '.$this->__table.'
// WHERE 1';
//recherche stricte par id
if (!empty($idbanniere)) {
//$query .= ' AND cl.idbanniere='.(int)$idbanniere;
$query->andWhere('e.idbanniere = :idbanniere')->setParameter(':idbanniere', $idbanniere);
} else {
//mots cles
if (!empty($keywords)) {
//$query .= ' AND title LIKE \'%'.$this->escape($keywords).'%\' ';
$query->andWhere('e.title LIKE \'%:keywords%\'')->setParameter(':keywords', $keywords);
}
}
$query->orderBy('e.idbanniere', 'desc');
// on lance la recherche
return $query->getQuery();
}
// }}}
// {{{ setToArray()
/** Liste des bannieres dans un tableau associatif
*
* Tableau associatif notament utilisé pour lister les informations sur des bannieres
* Utile pour générer les tableaux HTML de listes de bannieres
*
* @access public
* @return array
* @see findData()
*/
public function setToArray($spec = true)
{
if ($spec == true) {
$this->findData();
}
$values = array();
$color = null;
$nb = 0;
while ($this->fetch()) {
$values[$nb] = $this->getInfosBanniere(null);
$values[$nb]['id'] = $this->idbanniere;
// couleur de ligne alternative
$color = $this->colorAlternate($color);
$values[$nb]['colorline'] = $color;
$nb++;
}
return $values;
}
// }}}
// {{{ orderByDefaut()
/** Classement par défaut
*
* Défini le tri par défaut pour les requêtes de type SELECT
*
* @access public
* @return string
*/
public function orderByDefaut($order = null)
{
// tri par défaut
if (empty($order)) {
$order = 'idbanniere';
}
return $order;
}
// }}}
// {{{ getInfosBanniere()
/** Informations sur une banniere
*
* @access public
* @param int $idbanniere Identifiant de l'banniere
* @return array
*/
public function getInfosBanniere($idbanniere = null, $params = array())
{
$infos = array();
////////////// infos de la table
if (empty($this->idbanniere)) {
$this->get($idbanniere);
}
$infos += array(
'idbanniere' => $this->idbanniere,
'title' => $this->title,
'imgfile' => $this->imgfile,
'url_redirect' => $this->url_redirect,
'description' => $this->description,
'date_start' => $this->date_start,
'date_end' => $this->date_end,
'dateupdate' => $this->dateupdate,
'dateinsert' => $this->dateinsert,
'publish' => $this->publish,
'is_thnet' => $this->is_thnet,
'is_bottom' => $this->is_bottom,
'is_region' => $this->is_region,
'is_sponsored' => $this->is_sponsored,
'order' => $this->order,
'region' => $this->region,
'is_swf' => $this->is_swf,
);
$infos['nameview'] = $this->title;
return $infos;
}
// }}}
// {{{ getObjectTitle()
/** Infos de l'item recherché
*
*
* @access public
* @param int $idobject Identifiant de l'item
* @param array $infos_object Informations pour construire le titre
* @param array $params Paramétres supplémentaires
* @return string|array
*/
public function getObjectTitle($idobject = null, $infos_object = array(), $params = array())
{
if (!$idobject && isset($this->idbanniere)) $idobject = $this->idbanniere;
// champs
$fields = array('idbanniere', 'title', 'url_redirect');
if (!empty($infos_object)) {
foreach ($fields as $k => $v) {
if (array_key_exists($v, $infos_object)) {
$infos[$v] = $infos_object[$v];
} else {
$infos[$v] = '';
}
}
} else {
//charger les infos que si necessaire
if (empty($this->title)) {
$sql = 'SELECT ' . implode(',', $fields) . '
FROM ' . $this->__table . '
WHERE ' . $this->__idtable . '=' . $idobject;
$infos = $this->queryRow($sql);
} else {
foreach ($fields as $k => $v) {
if (isset($this->$v) && $this->$v != '') {
$infos[$v] = trim($this->$v);
} else {
$infos[$v] = '';
}
}
}
}
// renvoyer un array plutôt que le titre (utile si on veux l'url)
if (array_key_exists('return_array', $params)) {
return $infos;
}
return $infos['title'];
}
// }}}
// {{{ getRelationsArray()
/** Décrit les relations de l'objet
*
* @access public
* @param string|int $type si on souhaite retourner uniquement un type precis
* @see Theatre::getLinkedObject()
*/
public function getRelationsArray($type)
{
// récupérer les relations communes
$array_types = parent::getRelationsArray('all');
// retourne uniquement un type précis
if ($type == 'all') {
return $array_types;
}
if (!empty($array_types[$type])) {
$array_types[$type]['type'] = $type;
return $array_types[$type];
} else {
return null;
}
}
// }}}
// {{{ getListSearch()
/** Liste des bannieres
*
* Pour les champs Ajax de relations et les recherches diverses
*
* @access public
* @param string $lettres Pour la recherche alphabetique
* @param string $type Pour le type recherche (editeurs, revues...)
* @param string $keywords Recherche par mots cles
* @return array
*/
public function getListSearch($lettre, $type = null, $keywords = null)
{
$sql = 'SELECT idbanniere, title
FROM ' . $this->__table . '
WHERE 1 ';
// selection par ordre alpha
if (!empty($lettre) && $lettre != 'tous') {
$sql_where[] = array('LOWER(LEFT(LTRIM(banniere),1))=\'' . $this->escape($lettre) . '\'');
}
// recherche avec un mot cle
$list = $this->splitKeywords($keywords);
if ($list) {
if (!empty($list[$this->IDSTR])) {
// mots clés
foreach ($list[$this->IDSTR] as $s) {
$sql_more_name[] = 'title LIKE \'%' . $s . '%\'';
}
$sql_where[] = array('(' . join(' OR ', $sql_more_name) . ')');
}
if (!empty($list[$this->IDNUM])) {
// recherche par numéro d'id pour les mots clés numériques
$sql_where[] = array('idbanniere IN (' . implode(',', $list[$this->IDNUM]) . ')', 'OR');
}
}
foreach ($sql_where as $k => $v) {
if (!empty($v[1])) {
$sql .= ' ' . $v[1] . ' ' . $v[0];
} else {
$sql .= ' AND ' . $v[0];
}
}
$sql .= ' ORDER BY title';
$this->query($sql);
$r = array();
while ($this->fetch()) {
$r[$this->idbanniere] = $this->banniere;
// si trouvé par id, on l'affiche en fin de titre
if (!empty($list[$this->IDNUM]) && in_array($this->idbanniere, $list[$this->IDNUM])) {
$r[$this->idbanniere] .= ' - n°' . $this->idbanniere;
}
}
$this->free();
return $r;
}
// }}}
// {{{ getListBannieres()
/** Retourne des listes des bannieres
*
* Retourne des listes de bannieres au format "standard""
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @param array $format_vignette de la vignette si pas standard
* @param array $params_display paramètre pour l'affichage
* @return array
*/
public function getListBannieres($params_sql = null, $format_vignette = array(), $params_display = array())
{
// si pas de tableau
if (!isset($params_sql['special'])) {
$params_sql['special'] = array();
}
if (!isset($params_sql['params'])) {
$params_sql['params'] = array();
}
// construction de la requête avec le passage de paramètres
$sql = $this->getSQLBannieres($params_sql);
// vignettes
if (empty($params['format_vignette'])) {
$format_vignette = array('width' => 130, 'height' => 110, 'direction' => 'thumb');
}
// paramètre de debug
$dbg = (empty($params_sql['dbg'])) ? array() : $dbg = $params_sql['dbg'];
// executer la requete
$this->query($sql, $dbg);
$videos = array();
// donnée entières ?
if (empty($params_display['alldata'])) {
$params_display['alldata'] = false;
}
// debug
$start_boucle = microtime(true);
$bannieres = array();
// on boucle
while ($this->fetch()) {
$idbanniere = $this->idbanniere;
if (!empty($params_sql['fields'])) {
foreach ($params_sql['fields'] as $f) {
$bannieres[$idbanniere][$f] = $this->$f;
}
} else {
$bannieres[$idbanniere] = $this->getInfosBanniere($idbanniere);
$bannieres[$idbanniere]['description_courte'] = $this->cutSentence(strip_tags($bannieres[$idbanniere]['description']), 45, true);
}
}
// debug de la boucle
if ($this->isTraceMode('list_object')) {
$this->setTraceInfos(array(
'execution_time_boucle' => microtime(true) - $start_boucle,
'nb_occurence' => count($bannieres),
'dbg' => $dbg,
));
}
$this->free();
return $bannieres;
}
// }}}
// {{{ getSQLBannieres()
/** Générer la requete standard pour des bannieres
*
* @access public
* @param array $aueru Les paramètres pour la requete
* @param bool $debug afficher la requête
* @return string
*/
public function getSQLBannieres($query = array(), $debug = false)
{
// on prepare les tableaux
foreach (array('select', 'from', 'where', 'order', 'group', 'limit') as $t) {
if (!empty($query[$t])) {
if (!is_array($query[$t])) {
$query[$t][] = $query[$t];
} else {
$query[$t] = $query[$t];
}
} else {
$query[$t] = array();
}
}
// champs par défaut
$this->addSQLParams('select', $query, array(
array('bannieres' => '*')
));
// publié ou pas
if (empty($query['publish']) && $_ENV['CHECK_IF_ONLINE']) {
$this->addSQLParams('where', $query, array(
array('bannieres', 'publish', 1)
));
} elseif (!empty($query['publish'])) {
$this->addSQLParams('where', $query, array(
array('bannieres', 'publish', $query['publish'])
));
}
return $this->getSQLObject($query);
}
// }}}
// {{{ getCurrentBanniere()
/** Récupère la bannière courante */
public function getCurrentBanniere($exclude = array()): array
{
$sql = 'SELECT * FROM ' . $this->__table . '
WHERE publish=1
AND valid=1
AND (is_bottom=0 OR is_bottom IS NULL)
AND (is_region=0 OR is_region IS NULL)
AND (is_national=0 OR is_national IS NULL)
AND date_start<=NOW()
AND date_end>=NOW()
LIMIT 0,1';
return $this->queryRow($sql) ?: [];
}
// }}}
// {{{ getRegionBanniere()
/** Récupèrer la bannière courante des regions
*
* @access public
* @return array
*/
public function getRegionBanniere($idbigregion = null)
{
$dateNow = new \DateTime();
$sql = 'SELECT * FROM ' . $this->__table . '
WHERE publish=1
AND valid=1
AND is_region=1
AND date_start<=\'' . $dateNow->format('Y-m-d') . '\'
AND date_end>=\'' . $dateNow->format('Y-m-d') . '\'';
//setDebug($sql);
if (!is_null($idbigregion)) {
if (is_numeric($idbigregion)) {
$sql .= ' AND region = ' . (int)$idbigregion . '';
} else {
$sql .= ' AND country = "' . $idbigregion . '"';
}
} else {
$sql .= ' AND (region IS NULL AND country IS NULL)';
}
// $sql .= ' LIMIT 0,2';
$sql .= ' ORDER BY bannieres.order ASC ';
$res = $this->queryAll($sql);
if ($res) {
return $res;
} else {
return array();
}
}
// }}}
// {{{ getNationalBanniere()
/** Récupèrer la bannière courante des regions au national
*
* @access public
* @return array
*/
public function getNationalBanniere()
{
$dateNow = new \DateTime();
$sql = 'SELECT * FROM ' . $this->__table . '
WHERE publish=1
AND valid=1
AND is_national=1
AND date_start<=\'' . $dateNow->format('Y-m-d') . '\'
AND date_end>=\'' . $dateNow->format('Y-m-d') . '\'
ORDER BY bannieres.order ASC
LIMIT 0,10';
//setDebug($sql);
$res = $this->queryAll($sql);
if ($res) {
return $res;
} else {
return array();
}
}
// }}}
// {{{ getCurrentBanniere()
/** Récupère la bannière courante du bas */
public function getBottomBanniere(): array
{
$sql = 'SELECT * FROM ' . $this->__table . '
WHERE publish=1
AND valid=1
AND is_bottom=1
AND date_start<=NOW()
AND date_end>=NOW()
ORDER BY bannieres.order ASC
LIMIT 0,10';
return $this->queryRow($sql) ?: [];
}
// }}}
}