<?php
namespace TheatreCore\Repository;
use TheatreCore\Entity\Keywords;
use TheatreCore\Traits\TheatreTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* KeywodsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class KeywordsRepository extends ServiceEntityRepository
{
use TheatreTrait;
private $em;
public $__table;
public $__table_object;
public $__idtable;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Keywords::class);
$this->__table = $this->getClassMetadata()->getTableName();
$this->__table_object = $this->getClassMetadata()->name;
$this->__idtable = $this->getClassMetadata()->getSingleIdentifierFieldName();
}
// {{{ findData()
/** Recherche d'keyword
*
* Permet de rechercher des keywords 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['idkeyword']) && my_is_int($_REQUEST['idkeyword'])) {
$idkeyword = (int)$_REQUEST['idkeyword'];
}
$query = $this->createQueryBuilder('e');
if ($this->getCount()) {
$query->select('COUNT(e.idkeyword)');
$this->setCount(false);
return $query;
}
// query perso
// $query = 'SELECT * FROM '.$this->__table.'
// WHERE 1';
//
// //recherche stricte par id
// if(!empty($idkeyword)) {
// $query .= ' AND cl.idkeyword='.(int)$idkeyword;
// } else {
// // mots cles
// if (!empty($keywords)) {
// $query .= ' AND title LIKE \'%'.$this->escape($keywords).'%\' ';
// }
// }
// on lance la recherche
return $query->getQuery();
}
// }}}
// {{{ setToArray()
/** Liste des keywords dans un tableau associatif
*
* Tableau associatif notament utilisé pour lister les informations sur des keywords
* Utile pour générer les tableaux HTML de listes de keywords
*
* @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->getInfosKeyword();
$values[$nb]['id'] = $this->idkeyword;
// couleur de ligne alternative
$color = $this->colorAlternate($color);
$values[$nb]['colorline'] = $color;
$nb++;
}
return $values;
}
// }}}
// {{{ getInfosKeyword()
/** Informations sur une keyword
*
* @access public
* @param int $idkeyword Identifiant de l'keyword
* @return array
*/
public function getInfosKeyword($idkeyword = null, $params = array())
{
$infos = array();
////////////// infos de la table
if (empty($this->idkeyword)) {
$data = $this->get($idkeyword);
$this->loadResult($data);
}
$infos += array(
'idkeyword' => $this->idkeyword,
'title' => $this->title,
'url_clean' => $this->url_clean,
'dateupdate' => $this->dateupdate,
'dateinsert' => $this->dateinsert,
'publish' => $this->publish,
);
$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->idkeyword)) $idobject = $this->idkeyword;
// champs
$fields = array('idkeyword', 'title', 'url_clean');
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'];
}
// }}}
// {{{ getListSearch()
/** Liste des keywords
*
* 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 k.idkeyword, k.title
FROM ' . $this->__table . ' k
WHERE 1 ';
// selection par ordre alpha
if (!empty($lettre) && $lettre != 'tous') {
$sql_where[] = array('LOWER(LEFT(LTRIM(title),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('idkeyword IN (' . implode(',', $list[$this->IDNUM]) . ')', 'OR');
}
}
if (!empty($sql_where)) {
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->idkeyword] = $this->title;
// si trouvé par id, on l'affiche en fin de titre
if (!empty($list[$this->IDNUM]) && in_array($this->idkeyword, $list[$this->IDNUM])) {
$r[$this->idkeyword] .= ' - n°' . $this->idkeyword;
}
}
$this->free();
return $r;
}
// }}}
// {{{ getListKeywords()
/** Retourne des listes des keywords
*
* Retourne des listes de keywords 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 getListKeywords($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->getSQLKeywords($params_sql);
// 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);
$keywords = array();
// on boucle
while ($this->fetch()) {
$idkeyword = $this->idkeyword;
if (!empty($params_sql['fields'])) {
foreach ($params_sql['fields'] as $f) {
$keywords[$idkeyword][$f] = $this->$f;
}
} else {
$keywords[$idkeyword] = $this->getInfosKeyword($idkeyword);
}
}
// debug de la boucle
if ($this->isTraceMode('list_object')) {
$this->setTraceInfos(array(
'execution_time_boucle' => microtime(true) - $start_boucle,
'nb_occurence' => count($keywords),
'dbg' => $dbg,
));
}
$this->free();
return $keywords;
}
// }}}
// {{{ countListKeywords()
/** Compter la liste des keywords
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @param bool $debug mode débugage
* @return int
*/
public function countListKeywords($params_sql = null, $debug = false)
{
// si pas de limite => tout
if (empty($params_sql['limit'])) {
$params_sql['limit'] = array('all');
}
$params_sql['special']['count_only'] = true;
// construction de la requête avec le passage de paramètres
return $this->queryOne($this->getSQLKeywords($params_sql, $debug));
}
// }}}
// {{{ getSQLKeywords()
/** Générer la requete standard pour des keywords
*
* @access public
* @param array $aueru Les paramètres pour la requete
* @param bool $debug afficher la requête
* @return string
*/
public function getSQLKeywords($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('keywords' => '*')
));
// publié ou pas
if (empty($query['publish']) && $_ENV['CHECK_IF_ONLINE']) {
$this->addSQLParams('where', $query, array(
array('keywords', 'publish', 1)
));
} elseif (!empty($query['publish'])) {
$this->addSQLParams('where', $query, array(
array('keywords', 'publish', $query['publish'])
));
}
// liaison avec multimedia_keyword
if (!empty($query['special']['idmultimedia'])) {
$this->addSQLParams('from', $query, array(
'multimedia_keyword',
));
// jointure entre les 3 tables
$this->addSQLParams('join', $query, array(
'keywords.`idkeyword`' => 'multimedia_keyword.`idkeyword`',
));
$this->addSQLParams('where', $query, array(
array('multimedia_keyword', 'idmultimedia', $query['special']['idmultimedia']),
));
}
return $this->getSQLObject($query);
}
// }}}
}