<?php
namespace TheatreCore\Repository;
use TheatreCore\Classes\Images\Images;
use TheatreCore\Entity\ContactPerson;
use TheatreCore\Entity\Directories;
use TheatreCore\Entity\Extranetusers;
use TheatreCore\Entity\Persons;
use TheatreCore\Traits\TheatreTrait;
use DateInterval;
use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Contracts\Translation\TranslatorInterface;
use TheatreCore\Entity\Texts;
/**
* PersonsRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PersonsRepository extends ServiceEntityRepository
{
use TheatreTrait;
private $em;
public $__table;
public $__table_object;
public $__idtable;
public $idperson;
// style de texte
protected $stylemore_text = array('imagep' => 'width:110px;', 'desctxt' => 'width:170px;');
// style de vignette
protected $format_vignette = array('width' => 130, 'height' => 100, 'direction' => 'thumb');
public function __construct(ManagerRegistry $registry, TranslatorInterface $tr, Images $images)
{
parent::__construct($registry, Persons::class);
$this->__table = $this->getClassMetadata()->getTableName();
$this->__table_object = $this->getClassMetadata()->name;
$this->__idtable = $this->getClassMetadata()->getSingleIdentifierFieldName();
$this->idperson = null;
$this->translator = $tr;
$this->images = $images;
}
/**
* Prepare attributes for entity
* replace foreign keys with entity instances
*
* @param array $attributes entity attributes
* @return array modified attributes values
*/
public function prepareAttributes(array $attributes)
{
foreach ($attributes as $fieldName => &$fieldValue) {
if (!$this->getClassMetadata()->hasAssociation($fieldName)) {
continue;
}
$association = $this->getClassMetadata()
->getAssociationMapping($fieldName);
if (is_null($fieldValue)) {
continue;
}
$fieldValue = $this->getEntityManager()
->getReference($association['targetEntity'], $fieldValue);
unset($fieldValue);
}
return $attributes;
}
// {{{ findData()
/** Recherche des personnes
*
* Permet de rechercher des personnes en fonction de certains critères :
* la première lettre, un bout du nom, le type de personne, si il est en ligne ou pas
*
* @access public
* @see Theatre::find()
*/
public function findData($idperson = null, $lettre = null, $keywords = null, $search = null)
{
$query = $this->createQueryBuilder('e');
// parametres
if (isset($_REQUEST['idperson']) && is_numeric($_REQUEST['idperson'])) {
$idperson = $_REQUEST['idperson'];
}
if (isset($_REQUEST['idcontact_ecole']) && is_numeric($_REQUEST['idcontact_ecole'])) {
$idcontact_ecole = $_REQUEST['idcontact_ecole'];
$search = 'idcontact_ecole';
}
if (isset($_REQUEST['lettre']) && !empty($_REQUEST['lettre'])) {
$lettre = $_REQUEST['lettre'];
}
if (isset($_REQUEST['keywords']) && $_REQUEST['keywords'] != 'nom' && !empty($_REQUEST['keywords'])) {
$keywords = $_REQUEST['keywords'];
// si keywords est un entier => recherche par id
if ($this->my_is_int($keywords)) {
$idperson = (int)$keywords;
$keywords = null;
}
}
if (isset($_REQUEST['search']) && !empty($_REQUEST['search'])) {
$search = $_REQUEST['search'];
}
// select ou select as
if (empty($search) || isset($_REQUEST['typeobj'])) {
// $this->selectAdd();
// $this->selectAdd('idperson,lastname,firstname,country,url_clean,language,language2,publish,valid');
} else {
// $this->selectAs();
// $this->selectAs(array('idperson,lastname,firstname,country,url_clean,language,language2,publish,valid'));
}
//recherche stricte par id
if (!empty($idperson)) {
// $this->whereAdd('persons.idperson='.$idperson);
$query->andWhere('e.idperson = ' . $idperson);
}
// recherche par mot cles
if (!empty($lettre)) {
// $this->whereAdd('lastname REGEXP \'^'.$lettre.'\' ');
// $query->andWhere('e.idperson = '.$idperson);
$query->andWhere(' ( (e.lastname LIKE \'%' . $this->escape($lettre) . '%\')');
$query->andWhere(' ( (e.idperson = ' . $idperson);
}
if (!empty($keywords)) {
$list = $this->splitKeywords($keywords);
if ($list) {
// si la liste est > 1 alors 0 = prénom et 1 = nom ou le contraire
if (count($list[$this->IDSTR]) == 2) {
// $this->whereAdd('(lastname LIKE \'%'.$list[$this->IDSTR][0].'%\'
// AND firstname LIKE \'%'.$list[$this->IDSTR][1].'%\')
// OR (firstname LIKE \'%'.$list[$this->IDSTR][0].'%\'
// AND lastname LIKE \'%'.$list[$this->IDSTR][1].'%\')');
$query->andWhere(' (e.lastname LIKE \'%' . $list[$this->IDSTR][0] . '%\'
AND e.firstname LIKE \'%' . $list[$this->IDSTR][1] . '%\')
OR (e.firstname LIKE \'%' . $list[$this->IDSTR][0] . '%\'
AND e.lastname LIKE \'%' . $list[$this->IDSTR][1] . '%\') ');
} else {
// mots clés
foreach ($list[$this->IDSTR] as $s) {
$query->orWhere('e.lastname LIKE \'%' . $s . '%\'');
}
}
/*if (!empty($list[$this->IDNUM])) {
// recherche par numéro d'id pour les mots clés numériques
$sql_where[] = array('idperson IN ('.implode(',', $list[$this->IDNUM]).')', 'OR');
}*/
}
}
// selectionne uniquement les auteurs
if ($search == 'author') {
// $ta = DB_DataObject::factory('text_author');
// $this->joinAdd($ta);
// $this->selectAs();
// $this->selectAs($ta,'ta_%s');
// $query->addSelect('ta');
}
// // selectionne uniquement les traducteurs
if ($search == 'translator') {
// $tt = DB_DataObject::factory('text_translator');
// $this->joinAdd($tt);
// $this->selectAs();
// $this->selectAs($tt,'tt_%s');
$query->join('e.textTranslatortext', 'ttt');
}
// selectionne uniquement les metteurs en scenes
if ($search == 'director') {
// $sp = DB_DataObject::factory('spectacle_person');
// $this->joinAdd($sp);
// $this->selectAs();
// $this->selectAs($sp,'tt_%s');
}
// // selectionne les élèves d'une école
// if (!empty($idcontact_ecole)) {
// $pe = DB_DataObject::factory('person_ecole');
// $this->joinAdd($pe);
// $this->selectAs();
// $this->selectAs($pe,'pe_%s');
// $this->whereAdd('idcontact='.$idcontact_ecole);
// }
//
// // selectionne les fiches hors ligne
// if (isset($_REQUEST['publish']) && $_REQUEST['publish']=='no') {
// $this->whereAdd('publish=0');
// }
// $this->groupBy('persons.idperson');
$query->orderBy('e.idperson', 'DESC');
// on lance la recherche
return $query->getQuery();
}
// }}}
// {{{ setToArray()
/** Liste des personnes dans un tableau associatif
*
* Tableau associatif notament utilisé pour lister les informations sur des personnes
* Utile pour générer les tableaux HTML de listes de personnes
*
* @access public
* @return array
* @see findData()
*/
public function setToArray($spec = true)
{
// recherche
if ($spec == true) {
$this->findData();
}
$values = array();
$color = null;
$nb = 0;
while ($this->fetch()) {
$values[$nb] = $this->getInfosPerson();
$values[$nb]['id'] = $this->idperson;
// couleur de ligne alternative
$color = $this->colorAlternate($color);
$values[$nb]['colorline'] = $color;
$nb++;
}
return $values;
}
// }}}
// {{{ getInfosPerson()
/** Informations sur une personne
*
* Permet de récupérer sous forme de tableau associatif
* les informations complete sur une personne (sauf contenu associe)
*
* @access public
* @param int $idperson id de la personne pour une recherche hors contexte
* @return array
*/
public function getInfosPerson($idperson = null, $params = array())
{
$infos = array();
// infos de la table
if (!empty($idperson)) {
$query = $this->createQueryBuilder('u')
->select('u')
->where('u.' . $this->__idtable . ' =:value')->setParameter('value', $idperson);
$datas = $query->getQuery()
->getOneOrNullResult();
$this->idperson = $datas->getIdperson();
} else {
$idperson = $this->idperson;
}
$lastname = (!empty($this->lastname)) ? $this->lastname : $datas->getLastname();
$firstname = (!empty($this->firstname)) ? $this->firstname : $datas->getFirstname();
$infos += array(
'lastname' => (!empty($this->lastname)) ? $this->lastname : $datas->getLastname(),
'firstname' => (!empty($this->firstname)) ? $this->firstname : $datas->getFirstname(),
'sexe' => (!empty($this->sexe)) ? $this->sexe : $datas->getSexe(),
'idperson' => (!empty($this->idperson)) ? $this->idperson : $datas->getIdperson(),
'is_author' => $this->isAuthor(),
'is_translator' => $this->isTranslator(),
//'is_adaptateur' => $this->isAdaptateur(),
'is_director' => $this->isDirector(),
'publish' => (!empty($this->publish)) ? $this->publish : $datas->getPublish(),
'valid' => (!empty($this->valid)) ? $this->valid : $datas->getValid(),
'url_clean' => (!empty($this->urlClean)) ? $this->urlClean : $datas->getUrlClean(),
'datestart' => (!empty($this->datestart)) ? $this->datestart : $datas->getDatestart(),
'dateend' => (!empty($this->dateend)) ? $this->dateend : $datas->getDateend(),
'idcorvin' => $datas->getIdCorvin(),
);
if (!empty($this->country)) {
$infos['country'] = $this->getNameCountry($this->country);
$infos['country_code'] = $this->country;
} else if ($datas->getCountry()) {
$infos['country'] = $this->getNameCountry($datas->getCountry()->getId());
$infos['country_code'] = $datas->getCountry()->getId();
}
if (!empty($this->language)) {
$infos['language'] = $this->getNameLanguage($this->language, $GLOBALS['lng']);
$infos['language_code'] = $this->language;
} else if (!empty($datas->getLanguage())) {
$infos['language'] = $this->getNameLanguage($datas->getLanguage(), $GLOBALS['lng']);
$infos['language_code'] = $datas->getLanguage();
}
// nom complet
$has_lastname = !empty($lastname);
$has_firstname = !empty($firstname);
if ($has_lastname && $has_firstname) {
$infos['fullname'] = $firstname . ' ' . $lastname;
} else {
if ($has_lastname) {
$infos['fullname'] = $lastname;
} else if ($has_firstname) {
$infos['fullname'] = $firstname;
} else {
$infos['fullname'] = ''; // ne devrait pas se produire
}
}
// retrouver les comptes mon théâtre associés
if (array_key_exists('get_extranetusers', $params)) {
$infos['extranetusers'] = $this->getEntityManager()->getRepository(Extranetusers::class)->getObjectExtranetusers('person', $infos['idperson']);
$infos['nb_extranetusers'] = count($infos['extranetusers']);
}
// retrouver les fiches annuaires associées
if (array_key_exists('get_directories', $params)) {
$infos['directories'] = $this->getEntityManager()->getRepository(Directories::class)->getDirectoriesByObject('person', $infos['idperson']);
$infos['nb_directories'] = count($infos['directories']);
}
// retrouver les contacts associés
if (array_key_exists('get_contact_persons', $params)) {
$infos['contact_persons'] = $this->getEntityManager()->getRepository(ContactPerson::class)->getContacts($infos['idperson']);
$infos['nb_contact_persons'] = count($infos['contact_persons']);
}
if (!empty($params_display['carrer_couleurs'])) {
$infos['carrer'] = $this->getObjectsCarrerDetails($infos['idperson'], 'persons');
}
return $infos;
}
// }}}
// {{{ getTextsPerson()
/** Textes d'une personne
*
* Permet de récupérer sous forme de tableau associatif
* les enregistrements de la table 'texts' liés à l'auteur
*
* @access public
* @param string $type Type de texte (original, traduction...)
* @param int $idtextoriginal id du texte original pour la recherche d'un traduction
* @param int $idperson id de la personne pour une recherche hors contexte
* @param int $publish textes publie ou pas ?
* @return array
*/
public function getTextsPerson($idperson = null, $type = 'original', $publish = null, $language = null, $idtextoriginal = null, $std_result = false)
{
$conn = $this->getEntityManager();
$texts = array();
// cast
$idperson = ((int)$idperson);
$idtextoriginal = ((int)$idtextoriginal);
$sql = null;
$select_fields_tabs = array('idtext', 'title', 'date', 'type', 'language', 'country', 'idcontact_publisher', 'isbn', 'ean', 'publication_year', 'nbpages', 'idgenretext', 'textorder', 'url_clean');
foreach ($select_fields_tabs as $k => $v) {
$select_fields_tabs_sql[] = 't.`' . $v . '`';
}
$select_fields = join(',', $select_fields_tabs_sql) . ',contacts.`organisation`, contacts.`url_clean` as url_clean_publisher ';
// les textes originaux
if ($type == 'original') {
$sql = 'SELECT ' . $select_fields . '
FROM text_author ta, texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND t.idtext=ta.idtext
AND ta.idperson=' . $idperson . '
AND t.type=0';
// filtre sur le genre de texte original
if (isset($this->byGenreText) && $this->byGenreText == 1) {
$sql .= ' AND (t.idgenretext=1 OR t.idgenretext IS NULL)';
} elseif (isset($this->byGenreText) && $this->byGenreText > 1) {
$sql .= ' AND t.idgenretext=' . $this->byGenreText;
}
} // toutes les adaptations de texte faites par l'auteur
elseif ($type == 'adaptation') {
$sql = 'SELECT ' . $select_fields . ', tad.`idtextadaptation`
FROM text_adaptation tad
JOIN texts t ON t.idtext=tad.idtextadaptation
JOIN text_author ta ON t.idtext=ta.idtext
LEFT JOIN contacts contacts ON contacts.idcontact=t.idcontact_publisher
WHERE ta.idperson=' . (int)$idperson . '
AND t.type=2';
} // toutes les adaptations d'un texte faites par la langue du spectacle
elseif ($type == 'adaptation_by_language_spectacle') {
$sql = 'SELECT ' . $select_fields . '
FROM texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE (
t.idtext IN(
SELECT t.idtext
FROM texts t
LEFT JOIN text_adaptation ta ON ta.idtext = ' . $idtextoriginal . '
LEFT JOIN text_spectacle ts ON ts.idtext = ta.idtextadaptation
LEFT JOIN spectacles s ON s.idspectacle = ts.idspectacle
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE t.idtext = ta.idtextadaptation';
// on ignore les textes dans une langue
if (!empty($language)) {
$sql .= ' AND s.language != \'' . $language . '\'';
}
$sql .= '
)
OR t.idtext IN(
SELECT t.idtext
FROM spectacles s, text_spectacle ts, text_traduction tt, texts t
WHERE 1
AND ts.idtext = t.idtext
AND s.idspectacle = ts.idspectacle
AND t.idtext=tt.idtexttraduction
AND tt.idtext=' . $idtextoriginal . '
)
)';
} // toutes les traductions d'un texte (page 26 du CDC) TODO = voir a mettre dans la classe texte ?
elseif ($type == 'translation') {
$sql = 'SELECT ' . $select_fields . '
FROM text_traduction tt, texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND t.idtext=tt.idtexttraduction
AND tt.idtext=' . $idtextoriginal . '
AND t.type=1';
// uniquement les traductions dans une langue
if (!empty($language)) {
$sql .= ' AND t.language=\'' . $language . '\'';
}
} // recuperer les six dernieres traductions (page 59 du CDC)
elseif ($type == 'search_translation') {
$sql = 'SELECT ' . $select_fields . '
FROM text_traduction tt, texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND t.idtext=tt.idtexttraduction
AND t.type=1
AND tt.idtext IN (
SELECT DISTINCT(t.idtext) FROM texts t,text_author ta
WHERE 1';
if (!empty($publish)) {
$sql .= ' AND t.publish=1';
}
$sql .= '
AND t.idtext=ta.idtext
AND ta.idperson=' . $idperson . '
)';
// uniquement les traductions dans une langue
if (!empty($language)) {
$sql .= ' AND t.language=\'' . $language . '\' ';
}
//piece recherchee
if (!empty($idtextoriginal)) {
$idtextsearch = $idtextoriginal;
$sql .= ' AND tt.idtext=' . $idtextsearch;
}
} // recherche les traductions d'une personne
elseif ($type == 'translator_translation') {
$sql = 'SELECT ' . $select_fields . '
FROM text_traduction tt, text_translator ttr, texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND t.idtext=tt.idtexttraduction
AND t.idtext=ttr.idtext
AND ttr.idperson=' . $idperson . '
AND t.type=1';
} elseif ($type == 'translator_translation_count') {
$sql = 'SELECT COUNT(t.`idtext`) AS total
FROM text_traduction tt, text_translator ttr, texts t
LEFT JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND t.idtext=tt.idtexttraduction
AND t.idtext=ttr.idtext
AND ttr.idperson=' . $idperson . '
AND t.type=1';
return $this->queryOne($sql);
} elseif ($type == 'count_all_texts_translations') {
$sql = 'SELECT COUNT(DISTINCT(t.idtext))
FROM text_traduction tt, texts t
LEFT OUTER JOIN contacts ON contacts.idcontact=t.idcontact_publisher
WHERE 1
AND tt.idtexttraduction=t.idtext
AND t.publish=1
AND tt.idtext IN (
SELECT idtext
FROM text_author
WHERE idperson=' . $idperson . '
)
';
return $this->queryOne($sql);
}
// uniquement les textes publies
if (!empty($publish)) {
$sql .= ' AND t.publish=1';
}
// pas les fictifs
$sql .= ' AND t.textfictif=0';
// classement
$order = ' ORDER BY date DESC';
// groupement
$sql .= ' GROUP BY t.idtext';
// tri
$sql .= $order;
//objet texts
$txt = $conn->getRepository(Texts::class);
// on lance la recherche
$txt->query($sql);
while ($txt->fetch()) {
$vignette = array('width' => $_ENV['VIGN_TEXT_WIDTH'], 'height' => $_ENV['VIGN_TEXT_HEIGHT'], 'direction' => 'thumb');
if ($std_result) {
$texts[$txt->idtext] = $txt->getInfosTextRow(true, $vignette, true, true);
continue;
}
$infotxt = array();
// s'agit-il d'une adaptation
if ($type == 'adaptation') {
$idtext = $txt->idtextoriginal;
} else {
$idtext = $txt->idtext;
}
// infos communes
if ($GLOBALS['module'] == 'theduc') {
$pb_publisher = true;
} else {
$pb_publisher = false;
}
$infotxt = $txt->getInfosTextRow(false, false, true, $pb_publisher);
$infotxt['language'] = $txt->getNameLanguage($txt->language);
$infotxt['country'] = $txt->getNameCountry($txt->country);
//pour les adapations
if ($type == 'adaptation') {
$infotxt['idtextoriginal'] = $txt->idtextoriginal;
$infotxt['idtextadaptation'] = $txt->idtextadaptation;
// recuperer les auteurs du text adapte
$infotxt['textadaptationauthors'] = $this->getAuthors($txt->idtextoriginal);
// recuperer le titre du text adapte et son identifiant (array)
$infotxt['textadaptation'] = $this->getInfoText($txt->idtextoriginal);
}
// $infotxt = $txt->getInfosTextRow($txt->idtext,$this->format_vignette);
$infotxt['stylemore'] = array('imagep' => 'width:60px;', 'desctxt' => 'width:400px;');
$infotxt['publisher_first'] = true;
$texts[$idtext] = $infotxt;
}
// retourner les enregistrements
return $texts;
}
// }}}
// {{{ isAuthor()
/** Voir si c'est un auteur de texte original (type=0)
*
* @access public
* @return bool
*/
public function isAuthor($idperson = null, $idcontact_publisher = null)
{
if (empty($idperson)) {
$idperson = $this->idperson;
}
$sql = 'SELECT COUNT(DISTINCT(t.idtext))
FROM text_author ta, texts t
WHERE t.idtext=ta.idtext AND t.textfictif=0
AND ta.idperson=' . (int)$idperson;
if ($_ENV['CHECK_IF_ONLINE']) {
$sql .= ' AND t.publish=1';
}
// si on recherche spécifiquement chez un editeur
if ($idcontact_publisher && $this->my_is_int($idcontact_publisher)) {
$sql .= ' AND t.idcontact_publisher=' . $idcontact_publisher;
// on trouve aussi le nombre de traductions des textes de cet auteur chez cet éditeur
$sql_trads = 'SELECT COUNT(DISTINCT(t.idtext)) as total
FROM text_traduction tt, texts t
WHERE 1
AND t.idtext=tt.idtexttraduction
AND t.idcontact_publisher=' . $idcontact_publisher . '
AND t.textfictif=0
AND tt.idtext IN (
SELECT idtext FROM text_author WHERE idperson=' . $idperson . '
)';
if ($_ENV['CHECK_IF_ONLINE']) {
$sql .= ' AND t.publish=1';
}
$nb_trads = $this->queryOne($sql_trads);
} else {
// TODO
$sql .= ' AND t.type IN (0,2) ';
}
$nb_textes = $this->queryOne($sql);
if (!empty($nb_trads)) {
return $nb_textes + $nb_trads;
} else {
return $nb_textes;
}
}
// }}}
// {{{ isAdaptateur()
/** Voir si c'est un adaptateur (type=2)
*
* @access public
* @return bool
*/
public function isAdaptateur($idperson = null)
{
if (empty($idperson)) {
$idperson = $this->idperson;
}
$sql = 'SELECT COUNT(DISTINCT(t.idtext)) as total
FROM text_author ta, texts
WHERE 1
AND t.idtext=ta.idtext
AND t.textfictif=0
AND ta.idperson=' . $idperson . '
AND t.type=2';
if ($_ENV['CHECK_IF_ONLINE']) {
$sql .= ' AND t.publish=1';
}
return $this->queryOne($sql);
}
// }}}
// {{{ isTranslator()
/** Voir si c'est un traducteur
*
* @access public
* @return bool
*/
public function isTranslator($idperson = null, $idcontact_publisher = null)
{
if (empty($idperson)) {
$idperson = $this->idperson;
}
$sql = 'SELECT COUNT(DISTINCT(tt.idtext)) as total
FROM text_translator tt, texts t
WHERE 1
AND t.idtext=tt.idtext
AND t.textfictif=0
AND tt.idperson=' . $idperson;
if ($_ENV['CHECK_IF_ONLINE']) {
$sql .= ' AND t.publish=1';
}
// si on recherche spécifiquement chez un editeur
if ($idcontact_publisher) {
$sql .= ' AND t.idcontact_publisher=' . ((int)$idcontact_publisher);
}
return $this->queryOne($sql);
}
// }}}
// {{{ isDirector()
/** Voir si c'est un metteur en scene
*
* @access public
* @return bool
*/
public function isDirector($idperson = null)
{
if (empty($idperson)) {
$idperson = $this->idperson;
}
$sql = 'SELECT COUNT(DISTINCT(sp.idspectacle)) as total
FROM spectacle_person sp, spectacles s
WHERE 1
AND sp.idspectacle=s.idspectacle
AND sp.idperson=' . $idperson . '
AND sp.idrole=1';
if ($_ENV['CHECK_IF_ONLINE']) {
$sql .= ' AND s.publish=1';
}
return $this->queryOne($sql);
}
// }}}
// {{{ getAuthors()
/** Récupération des auteurs d'un texte
*
* @access public
* @return array
*/
public function getAuthors($idtext = null)
{
if (!empty($idtext)) {
// objet texts
$text = $this->getEntityManager()->getRepository(Texts::class);
return $text->getAuthors($idtext);
}
}
// }}}
// {{{ getTextTitle()
/** Récupération du titre d'un texte
*
* @access public
* @return array
*/
public function getInfoText($idtext = null)
{
if (empty($idtext)) $idtext = $this->idtext;
$texts = $this->getEntityManager()->getRepository(Texts::class);
return $texts->getObjectTitle($idtext, array(), array('noauthor' => true, 'return_array' => true));
}
// }}}
// {{{ 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)
{
if (empty($order)) $order = 'persons.idperson';
return $order;
}
// }}}
// {{{ getListSearch()
/** Liste de personnes
*
* 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 (traducteur, auteur...)
* @param string $keywords Recherche par mots cles
* @return array
*/
public function getListSearch($lettre, $type = null, $keywords = null)
{
$sql = 'SELECT p.`idperson`, p.`firstname`, p.`lastname`
FROM ' . $this->__table . ' p
WHERE 1 ';
// selection par ordre alpha
if (!empty($lettre)) {
$sql_where[] = array('LOWER(LEFT(LTRIM(p.`lastname`),1))=\'' . $this->escape($lettre) . '\'');
}
// recherche avec un mot cle
$list = $this->splitKeywords($keywords);
if ($list) {
if (!empty($list[$this->IDSTR])) {
// si la liste est > 1 alors 0 = prénom et 1 = nom ou le contraire
if (count($list[$this->IDSTR]) == 2) {
$sql_where[] = array('(p.`lastname` LIKE \'%' . $list[$this->IDSTR][0] . '%\' AND p.`firstname` LIKE \'%' . $list[$this->IDSTR][1] . '%\')
OR (p.`firstname` LIKE \'%' . $list[$this->IDSTR][0] . '%\' AND p.`lastname` LIKE \'%' . $list[$this->IDSTR][1] . '%\')');
} else {
// mots clés
foreach ($list[$this->IDSTR] as $s) {
$sql_more_name[] = 'p.`lastname` 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('p.`idperson` IN (' . implode(',', $list[$this->IDNUM]) . ')', 'OR');
}
}
// on recherche par type
if ($type == 'translator') {
$sql_more = 'SELECT DISTINCT(idperson) FROM text_translator';
$sql_where[] = array('p.`idperson` IN (' . $sql_more . ')', 'AND');
}
if ($type == 'author') {
$sql_more = 'SELECT DISTINCT(idperson) FROM text_author';
$sql_where[] = array('p.`idperson` IN (' . $sql_more . ')', 'AND');
}
// uniquement associé à un multimedia
if ($type == 'multimedia') {
$sql_more = 'SELECT DISTINCT(idobject) FROM object_multimedia WHERE object=\'persons\'';
$sql_where[] = array('p.`idperson` IN (' . $sql_more . ')', 'AND');
}
// recherche AND ou autre
foreach ($sql_where as $k => $v) {
if (!empty($v[1])) {
$sql .= ' ' . $v[1] . ' ' . $v[0];
} else {
$sql .= ' AND ' . $v[0];
}
}
$sql .= ' ORDER BY p.`lastname`, p.`firstname`';
$this->query($sql);
$r = array();
while ($this->fetch()) {
$r[$this->idperson] = mb_strtoupper($this->lastname) . ' ' . $this->firstname;
// si trouvé par id, on l'affiche en fin de titre
if (!empty($list[$this->IDNUM]) && in_array($this->idperson, $list[$this->IDNUM])) {
$r[$this->idperson] .= ' - n°' . $this->idperson;
}
}
$this->free();
return $r;
}
// }}}
// {{{ 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->idperson)) {
$idobject = $this->idperson;
}
if (is_object($idobject)) {
$idobject = $idobject->getIdperson();
}
// champs
$fields = array('idperson', 'lastname', 'firstname');
if (!empty($infos_object)) {
// Pour les données chargés grace à Doctrine
$fields[] = 'urlClean';
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->lastname)) {
// Pour les données chargés grace à du SQL native
$fields[] = 'url_clean';
$sql = 'SELECT ' . implode(',', $fields) . '
FROM ' . $this->__table . '
WHERE ' . $this->__idtable . '=' . $idobject;
$infos = $this->queryRow($sql);
} else {
// Pour les données chargés grace à Doctrine
$fields[] = 'urlClean';
foreach ($fields as $k => $v) {
if (isset($this->$v) && $this->$v != '') {
$infos[$v] = trim($this->$v);
} else {
$infos[$v] = '';
}
}
}
}
// changer l'ordre si besoin
if (in_array('inverse', $params)) {
if (!empty($infos['firstname'])) {
$infos['title'] = $infos['firstname'] . ' ' . $infos['lastname'];
} else {
$infos['title'] = $infos['lastname'];
}
} else {
$infos = $infos ?: [];
$infos['lastname_oupper'] = mb_strtoupper($infos['lastname']);
if (!empty($infos['firstname'])) {
$infos['title'] = $infos['lastname_oupper'] . ' ' . $infos['firstname'];
} else {
$infos['title'] = $infos['lastname_oupper'];
}
}
// 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'];
}
// }}}
// {{{ byGenre()
/** Genre du texte pour une recherche
*
* chercher le titre traduit du texte dans une langue donnée
*
* @access public
*/
public function byGenre($idgenretext)
{
$this->byGenreText = $idgenretext;
}
// }}}
// {{{ getInfosObject()
/** Alias de getInfosPerson
*
* @access public
* @param int $idobject identifiant
* @return array
* @see getInfosPerson()
*/
public function getInfosObject($idobject = null)
{
return $this->getInfosPerson($idobject);
}
// }}}
// {{{ getVignettePerson()
/** Récupére la vignette d'une personne ou la cree si besoin
*
* @access public
* param int $idperson identifiant de la fiche persons
* param array $format format de l'image
* param bool $return_only Ne pas retourner l'image par défaut
* @return string
*/
public function getVignettePerson($idperson = null, $format = array(), $return_only = true)
{
if (!$idperson) $idperson = $this->idperson;
// par default : image normal 100px x 100px qualite 75
if ($format) {
$set_format = $format;
} else {
$set_format = array('width' => 70, 'height' => 70, 'direction' => 'thumb');
}
// generation par la classe Images
$images = $this->images;
$images->setVignetteFormat($set_format);
return $images->getVignetteObject($idperson, 'persons', 'class_photo_person', null, 1, $return_only);
}
// }}}
// {{{ getVignette()
/** Récupére la vignette d'une personne ou la cree si besoin
*
* @see getVignettePerson
*/
public function getVignette($idperson = null, $format = array())
{
return $this->getVignettePerson($idperson, $format);
}
// }}}
// {{{ 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 = null)
{
$array_types = array(
'person_classification' => array(// classifications associées
'type_join' => 'multiple',
'dest_table' => 'classifications',
'join_table' => 'person_classification',
'fields' => array('idclassification', 'classification', 'type'),
),
'text_person' => array(// persons liées à un texte
'type_join' => 'multiple',
'dest_table' => 'texts',
'join_table' => 'text_person',
'field_link' => 'idrole', //TODO
'fields' => array('title', 'url_clean', 'valid'),
),
// contact_person idcontact/idperson/idrole
'contact_person' => array(// contact associé à des personnes (direction d'une structure...)
'type_join' => 'multiple',
'dest_table' => 'contacts',
'join_table' => 'contact_person',
'fields' => array('idcontact', 'organisation', 'url_clean'),
'fields_join_table' => array('idoffice', 'date_start', 'date_end'),
'description' => 'Personne(s) associée(s) au contact',
),
// person annuaire
'directories' => array(// contact associé à des personnes (direction d'une structure...)
'type_join' => 'simple',
'dest_table' => 'directories',
'fields' => array('idperson', 'idcontact'),
'description' => 'Fiche(s) annuaire(s)',
),
/*bookmark_extranetuser
contact_person
dvd_person
files
contents
new_person
object_content > object/idobject
object_file => object/idobject
object_multimedia => object/idobject
persons
person_classification
person_ecole
person_extranetuser
spectacle_person
stats => object/idobject
text_author
text_person
text_translator*/
);
// retourne uniquement un type précis
if (!empty($array_types[$type])) {
$array_types[$type]['type'] = $type;
return $array_types[$type];
} else {
return $array_types;
}
}
// }}}
// {{{ getSQLPersons()
/** Générer la requete standard pour une person
*
* $params = array(
* 'select' => champs en plus pour le select,
* 'from' => champs en plus pour le from,
* 'where' => paramètres en plus le where,
* 'order' => classement des résultats,
* 'limit' => limiter le nb de résultats array(debut,fin), (all pour pas de limit)
* )
* $special = array(
* 'alpha' => recherche alphabétique sur le nom
* 'search' => recherche par mots-clés sur le nom
* )
*
*
* @access public
* @param array $params Les paramètres pour la requete
* @param array $special Les paramètres supplémentaires
* @param bool $debug afficher la requête TODO
* @return string
*/
public function getSQLPersons($params = array(), $special = array(), $debug = false)
{
$sql_join = '';
if (empty($params['where'])) {
$params['where'] = '';
}
if (empty($params['from'])) {
$params['from'] = '';
}
if (empty($params['select'])) {
$params['select'] = '';
}
// selon une classification
if (!empty($special['idclassification'])) {
// commun
$params['from'] .= ' person_classification c1';
$params['where'] .= ' AND T1.idperson= c1.idperson
AND c1.idclassification=' . (int)$special['idclassification'];
}
// selon une personne
if (!empty($special['idperson'])) {
$params['where'] .= ' AND c1.idperson = ' . (int)$special['idperson'];
}
// role special dans un spectacle
if (!empty($special['idrole'])) {
// commun
$params['from'] .= ' spectacle_person sp, spectacles s';
$params['where'] .= ' AND T1.idperson=sp.idperson
AND s.publish=1
AND s.idspectacle=sp.idspectacle ';
// si recherche table role ou direct id
if (!$this->my_is_int($special['idrole'])) {
$params['from'] .= ', roles r';
$params['where'] .= ' AND sp.idrole=r.idrole
AND r.role=\'' . $this->escape($special['idrole']) . '\' ';
} else {
$params['where'] .= ' AND sp.idrole=' . (int)$special['idrole'];
}
}
// personnes avec photo
if (!empty($special['with_photo'])) {
$sql_join .= '
JOIN object_file of ON T1.`idperson`=of.`idobject`
JOIN files f ON of.`idfile`=f.`idfile`
JOIN file_classification fc ON f.`idfile`=fc.`idfile` ';
// class_photo_person
$params['where'] .= '
AND fc.`idclassification`=14
';
}
// sexe de la personne
if (!empty($special['sexe'])) {
$params['where'] .= ' AND sexe=' . $this->getSexeValue($special['sexe']) . ' ';
}
// personne ayant un texte édité chez un éditeur
if (!empty($special['contacts']['publishers'])) {
//Les traducteurs
if (!empty($special['translator'])) {
$sql_join .= '
JOIN text_author T3 ON T2.`idperson` = T3.`idperson`
JOIN texts t ON t.`idtext`=T3.`idtext`
';
} //Les auteurs
else if (empty($special['contacts']['desactive_jointure_author'])) {
$sql_join .= '
JOIN text_author T2 ON T2.`idperson` = T1.`idperson`
JOIN texts t ON t.`idtext`=T2.`idtext`
';
}
$params['where'] .= ' AND t.`idcontact_publisher` IN (' . $this->arrayToIn($special['contacts']['publishers']) . ') ';
}
// personnes à l'affiche
if (!empty($special['spectacle'])) {
$params['where'] .= '
AND s.`valid`=1
AND s.`publish`=1
AND s.`textfictif`=0
AND T1.`publish`=1 ';
$roles_types = array(
'director' => array(1),
'actors' => array(5)
);
if (!empty($special['spectacle']['type'])) {
if ($special['spectacle']['type'] == 'author') {
$sql_join .= '
JOIN spectacle_author sa ON T1.`idperson`=sa.`idperson`
JOIN spectacles s ON sa.`idspectacle`=s.`idspectacle`
JOIN text_spectacle ts ON s.`idspectacle`=ts.`idspectacle`
JOIN texts t ON t.`idtext`=ts.`idtext` ';
$params['where'] .= ' AND t.`type` IN (0,1) ';
} elseif (array_key_exists($special['spectacle']['type'], $roles_types)) { // autres roles
$sql_join .= '
JOIN spectacle_person sp ON T1.`idperson`=sp.`idperson`
JOIN spectacles s ON sp.`idspectacle`=s.`idspectacle`
JOIN text_spectacle ts ON s.`idspectacle`=ts.`idspectacle`
JOIN texts t ON t.`idtext`=ts.`idtext` ';
$params['where'] .= ' AND sp.`idrole` IN (' . join(',', $roles_types[$special['spectacle']['type']]) . ') ';
} elseif ($special['spectacle']['type'] == 'all') {
$sql_join .= '
LEFT JOIN spectacle_author sa ON T1.`idperson`=sa.`idperson`
LEFT JOIN spectacle_person sp ON T1.`idperson`=sp.`idperson`
JOIN spectacles s ON s.idspectacle=sp.idspectacle
JOIN text_spectacle ts ON s.`idspectacle`=ts.`idspectacle`
JOIN texts t ON t.`idtext`=ts.`idtext` ';
}
} else {
$sql_join .= '
JOIN spectacle_author sa ON T1.`idperson`=sa.`idperson`
JOIN spectacles s ON sa.`idspectacle`=s.`idspectacle`
JOIN text_spectacle ts ON s.`idspectacle`=ts.`idspectacle`
JOIN texts t ON t.`idtext`=ts.`idtext` ';
}
if (!empty($special['spectacle']['count_date'])) {
$params['select'] .= ' COUNT( sc.`idschedule` ) AS nb_dates ';
$params['groupby'] = ' T1.`idperson`, t.`idtext` ';
$params['order'] = ' nb_dates DESC ';
}
// uniquement certain type de text
if (!empty($special['spectacle']['text']['type'])) {
$params['where'] .= ' AND t.`type` IN (' . $this->arrayToIn($special['spectacle']['text']['type']) . ') ';
}
$sql_join .= '
JOIN schedules sc ON s.`idspectacle`=sc.`idspectacle` ';
if (!empty($special['spectacle']['prochains'])) {
$date = new \DateTime('now');
$date->add(new DateInterval('P' . $special['spectacle']['prochains']));
$params['where'] .= ' AND sc.`date` BETWEEN \'' . date('Y-m-d') . '\' AND \'' . $date->format('Y-m-d') . '\' ';
}
if (!empty($special['spectacle']['saison'])) {
$s = explode('-', $special['spectacle']['saison']);
$params['where'] .= ' AND sc.`date` BETWEEN \'' . $s[0] . '-09-01\' AND \'' . $s[1] . '-08-31\' ';
}
if (!empty($special['spectacle']['year'])) {
$params['where'] .= ' AND sc.`date` BETWEEN \'' . (int)$special['spectacle']['year'] . '-01-01\' AND \'' . $special['spectacle']['year'] . '-12-31\' ';
}
if (!empty($special['spectacle']['person_periode'])) {
$date = new \DateTime('now');
$date->sub(new DateInterval('P70Y'));
if ($special['spectacle']['person_periode'] == 'public') {
$params['where'] .= ' AND (T1.`dateend`!=0 AND T1.`dateend`<=\'' . $date->format('Y-m-d') . '\') ';
} elseif ($special['spectacle']['person_periode'] == 'contemporain') {
$params['where'] .= ' AND (T1.`dateend` IS NULL OR T1.`dateend` = 0 OR T1.`dateend`>=\'' . $date->format('Y') . '\' ) ';
} else {
$params['where'] .= ' AND (T1.`dateend` IN (0,\'\') OR T1.`dateend` IS NULL) ';
}
}
if (!empty($special['spectacle']['text_edite'])) {
//$params['where'] .= ' AND T1.`idperson` IN (SELECT idperson FROM text_author JOIN texts WHERE text_author.idtext=texts.idtext AND texts.idcontact_publisher IS NOT NULL) ';
$params['where'] .= ' AND t.`idcontact_publisher` IS NOT NULL ';
}
if (!empty($special['spectacle']['periode'])) {
if (strpos($special['spectacle']['periode']['start'], '/') !== false) {
$datestart = DateTime::createFromFormat('d/m/Y', $special['spectacle']['periode']['start']);
$dateend = DateTime::createFromFormat('d/m/Y', $special['spectacle']['periode']['end']);
} else {
$datestart = DateTime::createFromFormat('Y-m-d', $special['spectacle']['periode']['start']);
$dateend = DateTime::createFromFormat('Y-m-d', $special['spectacle']['periode']['end']);
}
$params['where'] .= ' AND sc.`date` BETWEEN \'' . $datestart->format('Y-m-d') . '\'
AND \'' . $dateend->format('Y-m-d') . '\' ';
}
if (!empty($special['spectacle']['idtypestructure'])) {
$sql_join .= ' JOIN contacts ON sc.`idcontact` = contacts.`idcontact` ';
$join_contact = true;
if (is_array($special['spectacle']['idtypestructure'])) {
foreach ($special['spectacle']['idtypestructure'] as $k => $v) {
$idtypestructures[$k] = '\'' . $v . '\'';
}
$params['where'] .= ' AND contacts.`idtypestructure` IN (' . join(',', $idtypestructures) . ') ';
} else {
$params['where'] .= ' AND contacts.`idtypestructure`=\'' . $special['spectacle']['idtypestructure'] . '\' ';
}
}
if (!empty($special['spectacle']['country'])) {
if (empty($join_contact)) {
$sql_join .= ' JOIN contacts ON sc.`idcontact`= contacts.`idcontact` ';
}
$params['where'] .= ' AND contacts.`country`=\'' . $special['spectacle']['country'] . '\' ';
}
}
// étudiants d'une école
if (!empty($special['person_ecole'])) {
foreach (array('url_clean', 'idcontact', 'organisation') as $f) {
$params['select'] .= ' cte.`' . $f . '` as contact_' . $f . ', ';
}
$params['select'] .= ' pe.*, YEAR(pe.`date_start`) as year_start, YEAR(pe.`date_end`) as year_end ';
$sql_join .= ' JOIN person_ecole pe ON T1.`idperson`=pe.`idperson`
JOIN contacts cte ON pe.`idcontact`=cte.`idcontact` ';
// pour un contact précis
if (!empty($special['person_ecole']['idcontact'])) {
$params['where'] .= ' AND pe.`idcontact` IN (' . $this->arrayToIn($special['person_ecole']['idcontact']) . ') ';
}
// pour un role
if (!empty($special['person_ecole']['idrole'])) {
$sql_join .= ' JOIN ecole_role er ON pe.`idperson`=er.`idperson` ';
$params['where'] .= ' AND er.`idrole` IN (' . $this->arrayToIn($special['person_ecole']['idrole']) . ') ';
}
if (!empty($special['person_ecole']['exclude_jtn'])) { // pas le JTN
$params['where'] .= ' AND pe.`idcontact`!=3914 ';
}
if (!empty($special['person_ecole']['periode'])) { // uniquement les actifs => date de fin vide
if ($special['person_ecole']['periode'] == 'actual') {
$params['where'] .= '
AND (
(pe.`date_end` IS NULL OR pe.`date_end`=\'0000-00-00 00:00:00\' OR pe.`date_end`>=\'' . date('Y-m-d') . '\')
AND pe.`date_start`<=\'' . date('Y-m-d') . '\'
)';
}
if ($this->my_is_int($special['person_ecole']['periode']) && strlen($special['person_ecole']['periode']) == 4) {
$params['where'] .= ' AND YEAR(pe.`date_end`)=\'' . $special['person_ecole']['periode'] . '\' ';
}
}
}
// directeur de structure en particulier
if (!empty($special['contact_person'])) {
$sql_join .= ' JOIN contact_person cp ON T1.`idperson`=cp.`idperson`
JOIN contacts c1 ON c1.`idcontact`=cp.`idcontact` ';
$roles_types = array(
'directeur' => array(1),
'actors' => array(5)
);
// auteurs
if (array_key_exists($special['contact_person']['idoffice'], $roles_types)) { // autres roles
$params['where'] .= ' AND cp.`idoffice` IN (' . join(',', $roles_types[$special['contact_person']['idoffice']]) . ') ';
}
if (array_key_exists('actual', $special['contact_person'])) { // uniquement les actifs
$params['where'] .= ' AND ((cp.`date_end` IS NULL OR cp.`date_end`=\'0000-00-00 00:00:00\' OR cp.`date_end`>=\'' . date('Y-m-d') . '\') AND cp.`date_start`<=\'' . date('Y-m-d') . '\')';
}
if (array_key_exists('idtypestructure', $special['contact_person'])) {
$params['where'] .= ' AND c1.`idtypestructure`=\'' . $special['contact_person']['idtypestructure'] . '\' ';
}
}
// recherche auteurs
if (!empty($special['author'])) {
$params['where'] = ' AND (
(T1.dateend=0 OR T1.dateend IS NULL OR T1.dateend>1970)
AND (
T1.idperson IN
(
SELECT persons.idperson FROM persons, text_author, text_spectacle
WHERE persons.idperson=text_author.idperson AND text_spectacle.idtext=text_author.idtext
AND text_spectacle.idtext NOT IN (SELECT idtextadaptation FROM text_adaptation)
)
OR T1.idperson IN
(
SELECT persons.idperson FROM persons, text_author
WHERE persons.idperson=text_author.idperson AND text_author.idtext IN (SELECT idtext FROM text_traduction)
)
)
OR T1.idrole=21) ';
if (!empty($special['contacts']['publishers'])) {
$params['where'] .= ' AND t.`idcontact_publisher` IN (' . $this->arrayToIn($special['contacts']['publishers']) . ') ';
}
}
// rechecher traducteurs
if (!empty($special['translator'])) {
$params['from'] = array('text_translator' => 'join');
}
// francophone
if (isset($special['is_francophone'])) {
$params['where'] .= ' AND T1.`language`=\'fr\' ';
}
if (isset($special['is_non_francophone'])) {
$params['where'] .= ' AND T1.`language`!=\'fr\' ';
}
// publié
if (isset($special['publish'])) {
$params['where'] .= ' AND T1.`publish`=' . (int)$special['publish'];
}
// tables et champs
$tables_list = array(
'persons' => array(
'fields' => array('lastname', 'firstname', 'url_clean'),
),
);
$al = 1;
foreach ($tables_list as $k => $v) {
$tables_list[$k]['table_alias'] = $k . ' T' . $al;
foreach ($v['fields'] as $f) {
$select_fields_array[] = 'T' . $al . '.' . $f;
}
$al++;
}
// select de base
$sql_select = 'SELECT DISTINCT(T1.idperson), ' . join(',', $select_fields_array) . ' ';
// select en plus
if (array_key_exists('select', $params) && !empty($params['select'])) {
$sql_select .= ',' . $params['select'];
}
// compte seulement
if (array_key_exists('count_only', $special)) {
$sql_select = 'SELECT COUNT(DISTINCT(T1.idperson)) as total ';
}
// récupère les lettres des prénoms
if (array_key_exists('firstname_only', $special)) {
$sql_select = 'SELECT DISTINCT(UPPER(LEFT(T1.`lastname`, 1))) as letter ';
}
// from
foreach ($tables_list as $k => $v) {
$sql_from_tables[] = $v['table_alias'];
}
$sql_from = ' FROM ' . join(',', $sql_from_tables);
// déclaration si besoin de jointer avec la table persons
$sql_join_more = array();
// selectionde table en plus
if (array_key_exists('from', $params) && !empty($params['from'])) {
if (is_array($params['from'])) {
foreach ($params['from'] as $k => $v) {
$sql_from .= ',' . $k . ' T' . $al;
// join avec la table principale ?
if ($v == 'join') {
$sql_join_more[] = ' T1.idperson=T' . $al . '.idperson';
}
}
$al++;
} else {
$sql_from .= ',' . $params['from'];
}
}
// where standard
$sql_where = ' WHERE 1 ';
if (!empty($sql_join_more)) {
$sql_where .= ' AND ' . join(' AND ', $sql_join_more);
}
// paramètres en plus
if (array_key_exists('where', $params)) {
$sql_where .= $params['where'];
}
// recherche alpha
if (array_key_exists('alpha', $special)) {
$sql_where .= ' AND T1.lastname REGEXP \'^' . $special['alpha'] . '\' ';
}
// recherche lastname
if (array_key_exists('search', $special)) {
// on split et on recherche sur les mots de plus de 2 cractères
$keywords = explode(' ', trim($special['search']));
if (!empty($keywords)) {
foreach ($keywords as $k) {
$sql_keywords[] = ' T1.lastname LIKE \'' . $this->escape('%' . trim($k) . '%') . '\' ';
}
$sql_where .= ' AND (' . join(' OR ', $sql_keywords) . ') ';
} else {
$sql_where .= ' AND T1.lastname LIKE \'' . $this->escape('%' . $special['search'] . '%') . '\' ';
}
}
if (empty($special['count_only'])) {
// groupement par défaut
if (!array_key_exists('groupby', $params)) {
$groupby = ' GROUP BY T1.idperson ';
} else {
$groupby = ' GROUP BY ' . $params['groupby'];
}
// classement par défaut
if (!array_key_exists('order', $params)) {
// si ancien, classement DESC
if (array_key_exists('desc', $special)) {
$classement = 'DESC';
} else {
$classement = 'ASC';
}
if (array_key_exists('classement', $params)) {
$classement = $params['classement'];
}
$order = ' ORDER BY T1.lastname ' . $classement;
} else {
$order = ' ORDER BY ' . $params['order'];
}
// classement aléatoire
if (array_key_exists('random', $special)) {
$order = ' ORDER BY RAND() ' . $special['random'];
}
} else {
$order = null;
$groupby = null;
}
// limite par défaut
if (!array_key_exists('limit', $params)) {
$limit = ' LIMIT 0,10';
} else {
if ($params['limit'] != 'all') {
$limit = ' LIMIT ' . $params['limit'][0] . ',' . $params['limit'][1];
} else {
$limit = ' ';
}
}
$sql_global = $sql_select . $sql_from . $sql_join . $sql_where . $groupby . $order . $limit;
return $sql_select . $sql_from . $sql_join . $sql_where . $groupby . $order . $limit;
}
// }}}
// {{{ countListPersons()
/** Compter une requête préparée
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @param bool $debug mode débugage
* @return int
*/
public function countListPersons($params_sql = array(), $debug = false)
{
// si pas de tableau
if (!isset($params_sql['special'])) {
$params_sql['special'] = array();
}
if (!isset($params_sql['params'])) {
$params_sql['params'] = array();
}
// si pas de limite => tout
if (empty($params_sql['params']['limit'])) {
$params_sql['params']['limit'] = 'all';
}
$params_sql['special']['count_only'] = true;
// construction de la requête avec le passage de paramètres
return $this->queryOne($this->getSQLPersons($params_sql['params'], $params_sql['special'], $debug));
}
// }}}
// {{{ getListPersons()
/** Récupére la liste des persons
*
* Récupére les persons de manière standard
*
* @access public
* @param array $params_sql paramètre pour la requete SQL
* @param array $format de la vignette si pas standard
* @param bool $debug mode débugage
* @return array
*/
public function getListPersons($params_sql = array(), $params = array(), $debug = false)
{
// 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->getSQLPersons($params_sql['params'], $params_sql['special'], $debug);
// paramètre de debug
$dbg = (empty($params_sql['dbg'])) ? array() : $dbg = $params_sql['dbg'];
// executer la requete
$this->query($sql, $dbg);
$pers = array();
$params_row = array();
// debug
$start_boucle = microtime(true);
// style more ?
if (!empty($params['stylemore'])) {
$params_row['stylemore'] = $params['stylemore'];
} else {
$params_row['stylemore'] = array('imagep' => 'width:80px;', 'desctxt' => 'width:200px;');
}
// format de la vignette
if (!empty($params['vignette_format'])) {
$vignette_format_flou = $vignette_format = $params['vignette_format'];
} else {
$vignette_format_flou = $vignette_format = array('width' => $_ENV['VIGN_PERSONS_WIDTH'], 'height' => $_ENV['VIGN_PERSONS_HEIGHT'], 'direction' => 'crop');
}
$vignette_format_flou['blur'] = true;
// mettre en forme chaque reponse
$entity = new Persons();
while ($attributes = $this->fetch()) {
// uniquement les années d'étude
if (!empty($params_sql['special']['person_ecole']['get_promos'])) {
$pers[$this->year_end]['annee'] = $this->year_end;
continue;
}
// infos sur la personne
$pers[$this->idperson] = $this->getInfosPersonRow($vignette_format, $params_row);
$pers[$this->idperson]['photo'] = $this->getVignettePerson($this->idperson, $vignette_format);
$pers[$this->idperson]['photo_flou'] = $this->getVignettePerson($this->idperson, $vignette_format_flou);
// dates
if (isset($this->nb_dates)) {
$pers[$this->idperson]['nb_dates'] = $this->nb_dates;
}
// écoles
if (!empty($params_sql['special']['person_ecole'])) {
$pers[$this->idperson]['person_ecole'] = array(
'date_start' => $this->date_start,
'date_end' => $this->date_end,
'year_start' => $this->year_start,
'year_end' => $this->year_end,
'comments' => $this->comments,
'contact' => array(
'url_clean' => $this->contact_url_clean,
'idcontact' => $this->contact_idcontact,
'organisation' => $this->contact_organisation,
)
);
}
}
// libère
$this->free();
// debug de la boucle
if ($this->isTraceMode('list_object')) {
$this->setTraceInfos(array(
'execution_time_boucle' => (microtime(true) - $start_boucle),
'nb_occurence' => count($pers),
'dbg' => $dbg,
));
}
return $pers;
}
// }}}
// {{{ getInfosPersonRow()
/** Retrouner un tableau standard dans les listes
*
*
* @param bool $vignette trouver les vignettes
* @param array $params paramètres en plus
* @access public
* @return array
*/
public function getInfosPersonRow($vignette = true, $params = array())
{
$pers = array();
$idperson = $this->idperson;
// id, nom, prenom, url_clean
$pers = array(
'idperson' => $this->idperson,
'lastname' => mb_strtoupper($this->lastname, 'utf8'),
'firstname' => $this->firstname,
'url_clean' => $this->url_clean,
);
return $pers;
}
// }}}
// {{{ getAssociateTexts()
/** Récupére des liste de texte en fonction de classification
*
* @access public
* @param int $idperson identifiant de la personne
* @param int $idrole son role
* @param int $classification la classification
* @param array $params_link paramètre en plus
* @return array
*/
public function getAssociateTexts($idperson, $idrole = null, $classification = null, $params_link = array())
{
if (!empty($classification)) {
/*$classifications = Theatre::factory('classifications');
$idlist = $classifications->classificationsToList($classification);
$where = $classifications->listClassificationsWhere($idlist);*/
if (is_array($classification)) {
$constraint_dest = 'idclassification IN (' . implode(',', $classification) . ')';
} else {
$constraint_dest = 'idclassification=' . (int)$classification;
}
}
$sql = 'SELECT DISTINCT(t.idtext)
FROM text_classification tc, text_person tp, classification cl, texts t
WHERE tc.idclassification=cl.idclassification
AND tp.idtext=tc.idtext
AND tp.idtext=t.idtext AND tc.idtext=t.idtext
AND tp.idperson=' . ((int)$idperson) . ' ';
$infos_link = array( // liaison avec la table text_classification via la table text_author
'type_join' => 'multiple',
'dest_table' => 'text_classification',
'join_table' => 'text_person',
'fields' => array('idtext', 'idclassification'),
'constraint_dest' => (!empty($constraint_dest)) ? $constraint_dest : '',
);
// si c'est pour l'auteur
if (!empty($params_link['author'])) {
$infos_link['join_table'] = 'text_author';
}
if (!empty($params['link'])) {
$params_link = $params['link'];
}
// $params_link['debug'] = true;
if (!empty($infos_link)) {
$items = $this->getLinkedObject($infos_link, $idperson, null, 'persons', $params_link);
}
if (!empty($params_link['count_only'])) {
return $items;
}
// grouper par classification
if (!empty($classification) && !empty($params['by_classification'])) {
if (is_array($classification)) {
foreach ($classification as $k => $v) {
$by_classification[$v] = array(
'classification' => $v,
);
}
} else {
$by_classification[$classification] = array(
'classification' => $classification,
);
}
}
if (!empty($items)) {
// reourner uniquement le nb
if (!empty($params_link['count_only'])) {
return $items;
}
// liste des texts
foreach ($items as $k => $v) {
$idtext_list[] = $v['idtext'];
}
$texts = $this->getEntityManager()->getRepository(Texts::class);
$params_sql = array(
'params' => array(
'where' => ' AND T1.idtext IN (' . join(',', $idtext_list) . ') ',
'limit' => array(0, 100)
)
);
if (!empty($params_link['order'])) {
$params_sql['params']['order'] = $params_link['order'];
}
$sql_texts = $texts->getSQLTexts($params_sql['params']);
//$texts_list = $texts->getListTexts($params_sql);
$texts->query($sql_texts);
while ($texts->fetch($sql)) {
$text = $texts->getInfosTextRow($texts->idtext, $this->format_vignette);
$text['publisher_first'] = true;
$text['stylemore'] = $this->stylemore_text;
if (!empty($params['by_classification'])) {
foreach ($items as $item) {
if ($item['idtext'] == $texts->idtext) {
$by_classification[$item['idclassification']]['texts'][$texts->idtext] = $text;
}
}
} else {
$infos[$texts->idtext] = $text;
}
}
//print_r($by_classification);
if (!empty($by_classification)) {
return $by_classification;
} else {
//print_r($infos);
return $infos;
}
}
}
// }}}
// {{{ countAuthors()
/** Nombre de persons ayant au moins un texte original
*
* @param bool $sql_only On veut retourner la requête
* @return int|string la valeur ou la requete
*/
public function countAuthors($sql_only = false)
{
// compter avers getSQLPersons
$params_sql = array(
'limit' => 'all'
);
$params_special = array(
'count_only' => true,
'author' => true,
);
$sql = $this->getSQLPersons($params_sql, $params_special);
if (!$sql_only) {
return $this->queryOne($sql);
} else {
return $sql;
}
}
// }}}
// {{{ countDirectors()
/** Nombre de persons ayant au moins mis en scène
*
* @param bool $sql_only On veut retourner la requête
* @return int|string la valeur ou la requete
*/
public function countDirectors($sql_only = false)
{
// compter avers getSQLPersons
$params_sql = array(
'limit' => 'all'
);
$params_special = array(
'count_only' => true,
'idrole' => 1,
);
$sql = $this->getSQLPersons($params_sql, $params_special);
if (!$sql_only) {
return $this->queryOne($sql);
} else {
return $sql;
}
}
// }}}
// {{{ countTranslators()
/** Nombre de persons ayant au moins un texte traduit
*
* @param bool $sql_only On veut retourner la requête
* @return int|string la valeur ou la requete
*/
public function countTranslators($sql_only = false)
{
// compter avers getSQLPersons
$params_sql = array(
'limit' => 'all'
);
$params_special = array(
'count_only' => true,
'translator' => true,
);
$sql = $this->getSQLPersons($params_sql, $params_special);
if (!$sql_only) {
return $this->queryOne($sql);
} else {
return $sql;
}
}
// }}}
// {{{ countActors()
/** Nombre de persons ayant au moins un joué un spectacle
*
* @param bool $sql_only On veut retourner la requête
* @return int|string la valeur ou la requete
*/
public function countActors($sql_only = false)
{
// compter avers getSQLPersons
$params_sql = array(
'limit' => 'all'
);
$params_special = array(
'count_only' => true,
'idrole' => 5,
);
$sql = $this->getSQLPersons($params_sql, $params_special);
if (!$sql_only) {
return $this->queryOne($sql);
} else {
return $sql;
}
}
// }}}
// {{{ countPersons()
/** Nombre de persons
*
* @param bool $sql_only On veut retourner la requête
* @param string $type On veut compter un type de personne en particulier
* @return int|string la valeur ou la requete
*/
public function countPersons($type = null, $sql_only = false)
{
if (!empty($type)) {
$method_name = 'count' . ucfirst($type);
if (method_exists($this, $method_name)) {
return $this->$method_name($sql_only);
}
} else {
$sql = 'SELECT COUNT(*) as total FROM ' . $this->__table;
$this->queryOne($sql);
}
}
// }}}
// {{{ setStylemoreText()
/** Pour l'affichage des listes de texte
*
*/
public function setStylemoreText($stylemore_text)
{
$this->stylemore_text = $stylemore_text;
}
// }}}
// {{{ setFormatVignette()
/** Pour l'affichage des listes
*
*/
public function setFormatVignette($format_vignette)
{
$this->format_vignette = $format_vignette;
}
// }}}
// {{{ getAssociateContacts()
/** Contacts aossociés à la personne
*
* @access public
* @param int $idperson identifiant de la personne
* @param int $idoffice role (exemple : direction)
* @param bool $actual_only uniquement les actuels (contrainte sur la date)
* @return array
*/
public function getAssociateContacts($idperson, $idoffice = null, $actual_only = null)
{
// infos sur la liaison
$infos_link = $this->getRelationsArray('contact_person');
// select egalement l'idrole
if ($idoffice) {
$infos_link['where'] = ' AND j2.idoffice=' . $idoffice;
}
// qui n'a pas fini son mandat
if ($actual_only) {
$infos_link['where'] .= ' AND (j2.date_end IS NULL OR j2.date_end>\'' . date('Y-m-d') . '\')';
}
// avec ou sans virgule avec le noPunct
if (!empty($infos_link)) {
return $this->noPunct($this->getLinkedObject($infos_link, $idperson, $infos_link['type'], 'persons'));
}
}
// }}}
// {{{ getSexeValue()
/** Trouver le sexe à partir de H/F
*
* @access public
* @param string $sexe sexe
* @return int
*/
public function getSexeValue($sexe)
{
$sexes = array(
'H' => 1,
'F' => 2,
);
if (!empty($sexes[$sexe])) {
return $sexes[$sexe];
}
return 0;
}
// }}}
// {{{ getPromos()
/** Trouver les promos
*
* @access public
* @param array $params_sql
* @return array
*/
public function getPromos($params_sql)
{
$conn = $this->getEntityManager();
$params_sql['special']['person_ecole']['get_promos'] = true;
$params_sql['dbg'] = array('get_promos', 'Les années de promos');
$annees = $conn->getRepository(Persons::class)->getListPersons($params_sql);
ksort($annees);
return $annees;
}
// }}}
// {{{ getSpectacleRoles()
/** Trouver les rôles joués par une personne dans un spectacle
*
* @access public
* @param int $idperson Identifiant personne
* @return array
*/
public function getSpectacleRoles($idperson)
{
$sql = 'SELECT r.idrole, r.role, r.role_male, r.role_female, lg.fr
FROM
spectacle_person sp
JOIN roles r ON sp.idrole=r.idrole
JOIN langterms lg ON lg.id=r.role
WHERE sp.idperson=' . $idperson . '
GROUP BY r.idrole ';
return $this->queryAll($sql);
}
// }}}
}