<?php
namespace TheatreCore\Repository;
use TheatreCore\Entity\Aidecontacts;
use TheatreCore\Traits\TheatreTrait;
use TheatreCore\Entity\Aides;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* AidecontactsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class AidecontactsRepository extends ServiceEntityRepository
{
use TheatreTrait;
private $em;
public $__table;
public $__table_object;
public $__idtable;
public function __construct(ManagerRegistry $registry, TranslatorInterface $translator)
{
parent::__construct($registry, Aidecontacts::class);
$this->__table = $this->getClassMetadata()->getTableName();
$this->__table_object = $this->getClassMetadata()->name;
$this->__idtable = $this->getClassMetadata()->getSingleIdentifierFieldName();
$this->translator = $translator;
}
// {{{ findData()
/** Recherche d'aide
*
* Permet de rechercher des aides selon différents critères
*
* @access public
* @see Theatre::find()
*/
public function findData($idclassification = null)
{
$query = $this->createQueryBuilder('e');
// parametres
if (isset($_REQUEST['keywords']) && $_REQUEST['keywords'] != 'nom') {
$keywords = filter_var(trim($_REQUEST['keywords']));
}
if (isset($_REQUEST['idaidecontact']) && my_is_int($_REQUEST['idaidecontact'])) {
$idaide = (int)$_REQUEST['idaidecontact'];
}
//recherche stricte par id
if (!empty($idaide)) {
$query->andWhere(' e.idaidecontact=' . (int)$idaide);
} else {
// mots cles
if (!empty($keywords)) {
$query->andWhere(' e.title LIKE \'%' . $this->escape($keywords) . '%\' ');
}
}
// on lance la recherche
return $query->getQuery();
}
// }}}
// {{{ setToArray()
/** Liste des aides dans un tableau associatif
*
* Tableau associatif notament utilisé pour lister les informations sur des aides
* Utile pour générer les tableaux HTML de listes de aides
*
* @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->getInfosAidecontact($this->idaidecontact);
$values[$nb]['id'] = $this->idaidecontact;
// couleur de ligne alternative
$color = $this->colorAlternate($color);
$values[$nb]['colorline'] = $color;
$nb++;
}
return $values;
}
// }}}
// {{{ getInfosAidecontact()
/** Informations sur une catégorie d'aide
*
* @access public
* @param int $idaidecontact Identifiant de la catégorie d'aide
* @return array
*/
public function getInfosAidecontact($idaidecontact = null, $params = array())
{
$infos = array();
////////////// infos de la table
if (empty($this->idaidecontact) || $this->idaidecontact != $idaidecontact) {
$this->loadResult($this->get($idaidecontact));
}
$infos += array(
'idaidecontact' => $this->idaidecontact,
'title' => $this->title,
'url_clean' => (!empty($this->urlClean)) ? $this->urlClean : $this->url_clean,
'dateupdate' => $this->dateupdate,
'dateinsert' => $this->dateinsert,
'publish' => $this->publish,
);
$infos['nameview'] = $this->title;
return $infos;
}
// }}}
// {{{ getAidecontacts()
/** Retourne des listes des genre d'aide
*
* Retourne des listes
*
* @access public
* @return array
*/
public function getAidecontacts()
{
$sql = 'SELECT idaidecontact,title
FROM ' . $this->__table . ' WHERE 1 ';
return $this->queryAll($sql, array(), null, true);
}
// }}}
// {{{ getListAidecontacts()
/** les catégories d'aide
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @return array
*/
public function getListAidecontacts($params_sql = null, $params_display = array())
{
$conn = $this->getEntityManager();
// 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->getSQLAidecontacts($params_sql);
// paramètre de debug
$dbg = (empty($params_sql['dbg'])) ? array() : $dbg = $params_sql['dbg'];
// executer la requete
$datas = $this->query($sql, $dbg);
// debug
$start_boucle = microtime(true);
$aidecontacts = array();
$aides = $conn->getRepository(Aides::class);
// on boucle
foreach ($datas as $value) {
$this->loadResult($value);
$aidecontacts[$this->idaidecontact] = $this->getInfosAidecontact();
$params_sql_aide = array(
'special' => array(
'idaidecontact' => $this->idaidecontact,
),
'fields' => array(
'aide', 'url_clean', 'idaide', 'mention_contacts'
),
'limit' => array(0, 100),
'dbg' => array('aides_aidescontacts', 'Les aides associés à aidecontact')
);
if (!empty($params_sql['special']['aides']['type_aides'])) {
$params_sql_aide['special']['type_aide'] = $params_sql['special']['aides']['type_aides'];
}
if (!empty($params_sql['special']['aides']['idcontacts'])) {
$params_sql_aide['special']['aides']['idcontacts'] = $params_sql['special']['aides']['idcontacts'];
}
if (!empty($params_sql['special']['object'])) {
$params_sql_aide['special'][$params_sql['special']['object'] . '_aide'] = true;
}
// trouver également les aides associées
$aidecontacts[$this->idaidecontact]['aides'] = $aides->getListAides($params_sql_aide);
}
// debug de la boucle
if ($this->isTraceMode('list_object')) {
$this->setTraceInfos(array(
'execution_time_boucle' => microtime(true) - $start_boucle,
'nb_occurence' => count($aidecontacts),
'dbg' => $dbg,
));
}
$this->free();
return $aidecontacts;
}
// }}}
// {{{ countListAidecontacts()
/** Compter la liste des catégories d'aide
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @param bool $debug mode débugage
* @return int
*/
public function countListAidecontacts($params_sql = null, $debug = false)
{
// si pas de limite => tout
if (empty($params_sql['limit'])) {
$params_sql['limit'] = array('all');
}
$params_sql['count_only'] = true;
// construction de la requête avec le passage de paramètres
return $this->db->queryOne($this->getSQLAidecontacts($params_sql, $debug));
}
// }}}
// {{{ getSQLAidecontacts()
/** Générer la requete standard pour des catégories d'aide
*
* @access public
* @param array $aueru Les paramètres pour la requete
* @param bool $debug afficher la requête
* @return string
*/
public function getSQLAidecontacts($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();
}
}
$this->addSQLParams('distinct', $query, 'no_primary');
if (empty($query['group'])) {
$this->addSQLParams('group', $query, 'no');
}
// champs par défaut
$this->addSQLParams('select', $query, array(
array('aidecontacts' => '*'),
array('aides' => '*')
));
$this->addSQLParams('from', $query, array(
'aides',
));
$this->addSQLParams('join', $query, array(
'aides.`idaidecontact`' => 'aidecontacts.`idaidecontact`',
));
// uniquement pour certains objets ?
if (!empty($query['special']['object'])) {
$object = $query['special']['object'];
$this->addSQLParams('from', $query, array(
$object . '_aide',
));
$this->addSQLParams('join', $query, array(
'aides.`idaide`' => $object . '_aide.`idaide`',
));
} else {
$object = 'text';
}
// publié ou pas
if (empty($query['publish']) && $_ENV['CHECK_IF_ONLINE']) {
$this->addSQLParams('where', $query, array(
array('aides', 'publish', 1)
));
} elseif (!empty($query['publish'])) {
$this->addSQLParams('where', $query, array(
array('aides', 'publish', $query['publish'])
));
}
// recherche par contact
if (!empty($query['special']['aides'])) {
if (!empty($query['special']['aides']['idcontacts'])) {
$this->addSQLParams('where', $query, array(
array('aides', 'idcontact', $query['special']['aides']['idcontacts']),
));
}
if (!empty($query['special']['idaidecontact'])) {
$this->addSQLParams('where', $query, array(
array('aides', 'idaidecontact', $query['special']['aides']['idaidecontact']),
));
}
if (!empty($query['special']['aides']['type_aides'])) {
$this->addSQLParams('where', $query, array(
array('aides', 'type_aide', $query['special']['aides']['type_aides']),
));
}
if (!empty($query['aide_is_null'])) {
$query['where'][] = ' AND (aides.aide != "" OR aides.aide IS NULL)';
} else {
$this->addSQLParams('where', $query, array(
array('aides', 'aide', '', 'AND', '!='),
));
}
}
// pour un texte
if (!empty($query['idtext'])) {
$this->addSQLParams('where', $query, array(
array('text_aide', 'idtext', $query['idtext'])
));
}
// recherche par année
if (!empty($query['special']['period'])) {
if (!empty($query['special']['period']['year'])) {
$this->addSQLParams('where_more', $query, ' AND YEAR(' . $object . '_aide.`date_start`)=' . $query['special']['period']['year']);
}
if (!empty($query['special']['period']['years'])) {
$where_more = ' AND YEAR(' . $object . '_aide.`date_start`) BETWEEN ' . $query['special']['period']['years']['start'] . ' AND ' . $query['special']['period']['years']['end'];
$this->addSQLParams('where_more', $query, $where_more);
}
}
if (empty($query['special']['no_order'])) {
$this->addSQLParams('order', $query, array(
array($object . '_aide' => 'date_start'),
));
}
return $this->getSQLObject($query);
}
// }}}
}