vendor/theatre/core/src/Repository/NewsRepository.php line 262

Open in your IDE?
  1. <?php
  2. namespace TheatreCore\Repository;
  3. use AmazonCloudFront;
  4. use AmazonS3;
  5. use TheatreCore\Classes\Images\Images;
  6. use TheatreCore\Entity\Bigregions;
  7. use TheatreCore\Entity\Files;
  8. use TheatreCore\Entity\News;
  9. use TheatreCore\Entity\Newschedules;
  10. use TheatreCore\Traits\TheatreTrait;
  11. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. /**
  14.  * NewsRepository
  15.  *
  16.  * This class was generated by the Doctrine ORM. Add your own custom
  17.  * repository methods below.
  18.  */
  19. class NewsRepository extends ServiceEntityRepository
  20. {
  21.     use TheatreTrait {
  22.         deleteData as protected deleteDataTrait;
  23.     }
  24.     private $em;
  25.     public $__table;
  26.     public $__table_object;
  27.     public $__idtable;
  28.     public function __construct(ManagerRegistry $registryImages $images)
  29.     {
  30.         parent::__construct($registryNews::class);
  31.         $this->__table $this->getClassMetadata()->getTableName();
  32.         $this->__table_object $this->getClassMetadata()->name;
  33.         $this->__idtable $this->getClassMetadata()->getSingleIdentifierFieldName();
  34.         $this->images $images;
  35.     }
  36.     /** Recherche des actualités
  37.      *
  38.      * recherche des actualités postées par les utilisateurs
  39.      *
  40.      * @access  public
  41.      * @see     Theatre::find()
  42.      */
  43.     public function findData($idnew null$params = array())
  44.     {
  45.         $query $this->createQueryBuilder('e');
  46.         if ($this->getCount()) {
  47.             $query->select('COUNT(e.idnew)');
  48.             $this->setCount(false);
  49.             return $query;
  50.         }
  51.         // parametre
  52.         if (!empty($_REQUEST[$this->__idtable]) && my_is_int($_REQUEST[$this->__idtable])) {
  53.             $idnew = (int)$_REQUEST[$this->__idtable];
  54.         }
  55.         if (isset($_REQUEST['keywords']) && $_REQUEST['keywords'] != 'titre') {
  56.             $keywords filter_var(trim($_REQUEST['keywords']));
  57.         }
  58.         if (isset($_REQUEST['city']) && $_REQUEST['city'] != 'ville' && !empty($_REQUEST['city'])) {
  59.             $city filter_var(trim($_REQUEST['city']));
  60.         }
  61. //
  62. //        $query = 'SELECT DISTINCT(n.idnew), n.*, e.login, ns.*,n.idnew as idnew
  63. //                  FROM  '.$this->__table.' n
  64. //                  INNER JOIN extranetusers e ON n.idextranetuser=e.idextranetuser
  65. //                  LEFT JOIN newschedules ns ON n.idnew=ns.idnew ';
  66. //        // également par personnes
  67. //        if(!empty($params['persons_list'])) {
  68. //            $query .= ' INNER JOIN new_person np ON np.idnew=n.idnew ';
  69. //        }
  70. //
  71. //        // également par spectacles
  72. //        if(!empty($params['spectacles_list'])) {
  73. //            $query .= ' INNER JOIN new_spectacle ns ON ns.idnew=n.idnew ';
  74. //        }
  75. //
  76. //        $query .= ' WHERE 1 ';
  77. //
  78. //        //recherche stricte par id
  79. //        if(!empty($idnew)) {
  80. //            $query .= ' AND n.idnew='.$idnew;
  81. //        }
  82. //        // mots cles
  83. //        if (!empty($keywords)) {
  84. //            $query .= ' AND n.title LIKE \'%'.$this->escape($keywords).'%\' ';
  85. //        }
  86. //        // ville
  87. //        if (!empty($city)) {
  88. //            $query .= ' AND n.city LIKE \'%'.$this->escape($city).'%\' ';
  89. //        }
  90. //
  91. //
  92. //        // recherche par person
  93. //        if(!empty($params['persons_list'])) {
  94. //            $query .= ' AND np.idperson IN ('.$params['persons_list'].') ';
  95. //        }
  96. //
  97. //        // recherche par spectacle
  98. //        if(!empty($params['spectacles_list'])) {
  99. //            $query .= ' AND ns.idspectacle IN ('.$params['spectacles_list'].') ';
  100. //        }
  101. //
  102. //        // recherche par extranetuser
  103. //        if(!empty($params['idextranetuser'])) {
  104. //            $query .= ' AND n.idextranetuser='.(int)$params['idextranetuser'];
  105. //        }
  106. //
  107. //        // selectionne les fiches hors ligne et ou valide
  108. //        foreach(array('publish','valid','idregion','country') as $f) {
  109. //            if (isset($_REQUEST[$f]) && my_is_int($_REQUEST[$f]) && $_REQUEST[$f]<2) {
  110. //                $query .= ' AND n.'.$f.'='.(int)$_REQUEST[$f];
  111. //            }
  112. //            if (!empty($params[$f]) && my_is_int($params[$f])) {
  113. //                $query .= ' AND n.'.$f.'='.(int)$params[$f];
  114. //            }
  115. //        }
  116. //
  117. //        $query .= ' GROUP BY n.idnew';
  118.         // on lance la recherche
  119.         return $query->getQuery();
  120.     }
  121.     // }}}
  122.     //  {{{ setToArray()
  123.     /** Liste des à propos dans un tableau associatif
  124.      *
  125.      * Tableau associatif notamment utilisé pour lister les informations sur les à proposs
  126.      *
  127.      * @access  public
  128.      * @param bool $spec recherche ou non
  129.      * @return  array
  130.      * @see     findData()
  131.      */
  132.     public function setToArray($spec true)
  133.     {
  134.         // recherche
  135.         if ($spec == true) {
  136.             $this->findData();
  137.         }
  138.         $values = array();
  139.         $color null;
  140.         $nb 0;
  141.         while ($this->fetch()) {
  142.             $values[$nb] = $this->getInfosNews(null, array('schedule_detail' => true));
  143.             $values[$nb]['id'] = $this->idnew;
  144.             $values[$nb]['nameview'] = $this->cutSentence($this->title150);
  145.             // couleur de ligne alternative
  146.             $color $this->colorAlternate($color);
  147.             $values[$nb]['colorline'] = $color;
  148.             $nb++;
  149.         }
  150.         return $values;
  151.     }
  152.     // }}}
  153.     //  {{{ getInfosNews()
  154.     /** Infos sur une actualite
  155.      *
  156.      * Retourne un tableau associatif avec les principales
  157.      * informations sur une annonce
  158.      *
  159.      * @access  public
  160.      * @param int $idnew Identifiant de la fiche actualite
  161.      * @return  array
  162.      * @see     findData()
  163.      */
  164.     public function getInfosNews($idnew null$params_data = array())
  165.     {
  166.         $infos = array();
  167.         $con $this->getEntityManager();
  168.         ////////////// infos de la table
  169.         if (!empty($idnew)) {
  170.             $data $this->find($idnew);
  171.             if (empty($data)) {
  172.                 return $data;
  173.             }
  174.         } else {
  175.             return false;
  176.         }
  177.         $infos += array(
  178.             'idnew' => $data->getIdNew(),
  179.             'title' => $data->getTitle(),
  180.             'description' => $data->getDescription(),
  181.             'markdown' => $data->getMarkdown(),
  182.             'url' => $data->getUrl(),
  183.             'type_news' => $data->getTypeNews(),
  184.             'url_more' => $data->getUrlMore(),
  185.             'url_file' => $data->getUrlFile(),
  186.             'idextranetuser' => $data->getIdextranetuser()->getIdextranetuser(),
  187.             'publish' => $data->getPublish(),
  188.             'valid' => $data->getValid(),
  189.             'statut' => $data->getStatut(),
  190.             'dateinsert' => $data->getDateInsert(),
  191.             'dateupdate' => $data->getDateUpdate()
  192.         );
  193.         $this->loadResult($infos);
  194.         // récupèrer le calendrier de la news
  195.         if (!empty($params_data['schedule_detail'])) {
  196.             $infos['schedule_detail'] = $con->getRepository(Newschedules::class)->getDateLieuCommon('tous'$infos['idnew'], null'ASC');
  197.         }
  198.         // date d'insertion
  199.         if (!empty($data->getDateInsert())) {
  200.             $infos['created'] = $this->getFormeDate($data->getDateInsert(), $GLOBALS['format_date_heure'][$GLOBALS['lng']]);
  201.             $infos['date_creation'] = $this->getFormeDate($data->getDateInsert(), '%d/%m/%y à %H:%m'true);
  202.         }
  203.         // date de modification
  204.         if (!empty($data->getDateupdate()) && $data->getDateupdate() != '0000-00-00 00:00:00') {
  205.             $infos['updated'] = $this->getFormeDate($data->getDateupdate(), $GLOBALS['format_date_heure'][$GLOBALS['lng']]);
  206.             $infos['date_modification'] = $this->getFormeDate($data->getDateupdate(), '%d/%m/%y à %H:%m'true);
  207.         }
  208.         // est-ce qu'il faut rechercher la prochaine date à partir d'une date donnée (datestart) ?
  209.         if (!empty($params_data['special']['depuis_le'])) {
  210.             $date_debut_recherche $this->getFormeDate($params_data['special']['depuis_le']);
  211.         } else {
  212.             $date_debut_recherche null;
  213.         }
  214.         // trouver la date et le lieu le plus proche
  215.         if (!empty($params_data['special']['anciens'])) {
  216.             $periode = array('anciens' => true);
  217.         } elseif (!empty($params_data['special']['plus_proche'])) { // la date la plus proche passée ou future
  218.             $periode = array('plus_proche' => true);
  219.         } elseif (!empty($params_data['special']['date_creation'])) {
  220.         } else { // la premiere date future
  221.             $periode = array('prochains' => true);
  222.         }
  223.         $infos['vignette'] = $this->getVignetteNew($infos['idnew']);
  224.         // definir un début de recherche
  225.         if ($date_debut_recherche) {
  226.             $periode['depuis_le'] = $date_debut_recherche;
  227.         }
  228.         //$periode['plus_proche'] = $date_debut_recherche;
  229.         // ajoute les infos sur les dates et lieux prochains
  230.         $infos['schedule'] = $this->getProchDateLieu($periode$infos['idnew'], null);
  231.         if (empty($infos['schedule'])) {
  232.             $periode = array('plus_proche' => true);
  233.             $infos['schedule'] = $this->getProchDateLieu($periode$infos['idnew'], null);
  234.         }
  235.         // insérer par l'administrateur ou par un autre user
  236.         if (empty($this->idextranetuser)) {
  237.             $infos['extranetuser']['login'] = 'Administrateur';
  238.             $infos['extranetuser']['is_admin'] = true;
  239.         } else {
  240.             $infos['extranetuser']['idextranetuser'] = $this->idextranetuser;
  241.             $extranetusers_fields = array(
  242.                 'lastname',
  243.                 'firstname',
  244.                 'signature',
  245.                 'login',
  246.                 'city',
  247.                 'enseignant_etablissement'
  248.             );
  249.             foreach ($extranetusers_fields as $f) {
  250.                 if (isset($this->$f)) {
  251.                     if ($f == 'city_extranetuser') {
  252.                         $infos['extranetuser']['city'] = $this->$f;
  253.                     } else {
  254.                         $infos['extranetuser'][$f] = $this->$f;
  255.                     }
  256.                 }
  257.             }
  258.         }
  259.         // les contacts, les personnes et les spectacles liées
  260.         $infos['persons'] = $this->noPunct($this->getLinkedObject($this->getRelationsArray('new_person'), $this->idnew'new_person''news'));
  261.         $infos['contacts'] = $this->noPunct($this->getLinkedObject($this->getRelationsArray('new_contact'), $this->idnew'new_contact''news'));
  262.         $infos['spectacles'] = $this->noPunct($this->getLinkedObject($this->getRelationsArray('new_spectacle'), $this->idnew'new_spectacle''news'));
  263.         $infos['texts'] = $this->noPunct($this->getLinkedObject($this->getRelationsArray('new_text'), $this->idnew'new_text''news'));
  264.         /*$wiki->setOpt('active_quote', 1);
  265.         $wiki->setOpt('active_fix_word_entities',1);
  266.         $wiki->setOpt('active_fr_syntax',1);*/
  267.         $markdown = ($infos['markdown'] || !empty($params_data['markdown_force'])) ? true false;
  268.         //conversion des champs (wiki, valeurs par défaut)
  269.         if (empty($params_data['no_wiki_convert'])) {
  270.             $keys_to_convert = array('description' => 200);
  271.             if (!empty($keys_to_convert)) {
  272.                 foreach ($keys_to_convert as $f => $v) {
  273.                     // texte court
  274.                     $t strip_tags($this->wikiTransform($this->$f$this->markdown));
  275.                     $infos[$f '_cut'] = $this->cutSentence($t$v);
  276.                     // texte normal
  277.                     $infos[$f] = $this->wikiTransform($this->$f$markdown);
  278.                     // ne pas afficher un autre texte si c'est la même taille
  279.                     if (!empty($v) && (strlen($this->$f) == strlen($infos[$f '_cut']))) {
  280.                         $infos['no_long_text'] = true;
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.         // infos sur l'actualite
  286.         return $infos;
  287.     }
  288.     // }}}
  289.     // {{{ orderByDefaut()
  290.     /** Classement par défaut
  291.      *
  292.      * Défini le tri par défaut pour les requêtes de type SELECT
  293.      *
  294.      * @access public
  295.      * @param string $order ordre
  296.      * @return string
  297.      */
  298.     public function orderByDefaut($order null)
  299.     {
  300.         // tri par défaut
  301.         if (empty($order)) {
  302.             $order 'n.dateinsert';
  303.         }
  304.         return $order;
  305.     }
  306.     // }}}
  307.     //  {{{ getInfosNewsRow()
  308.     /** Infos sur une actualite
  309.      *
  310.      * les principales
  311.      * informations sur une news dans une boucle
  312.      *
  313.      * @access  public
  314.      * @param int $idnew Identifiant de la fiche actualite
  315.      * @return  array
  316.      * @see     findData()
  317.      */
  318.     public function getInfosNewsRow($idnew null$params_data = array())
  319.     {
  320.         $infos $this->getInfosNews($idnew$params_data);
  321.         return $infos;
  322.     }
  323.     // }}}
  324.     //  {{{ getLastNews()
  325.     /** récupérer les dernières news
  326.      *
  327.      * @access  public
  328.      * @param int $idnew Identifiant de la fiche actualite
  329.      * @return  array
  330.      * @see     findData()
  331.      */
  332.     public function getListNewsObject($object$idobject$limit$periode 'next'$date_suivante)
  333.     {
  334.         $obj Theatre::factory('new_' $object);
  335.         // on recup aussi le login de l'utilisateur
  336.         $sql_news 'SELECT n.*,e.login,o.* FROM ' $this->__table ' n, extranetusers e, ' $obj->__table ' o
  337.                      WHERE e.idextranetuser=n.idextranetuser
  338.                      AND o.' $this->__idtable '=n.' $this->__idtable '
  339.                      AND o.id' $object '=' . ((int)$idobject) . '
  340.                      AND n.publish=1 AND n.valid=1 ';
  341.         // limite dans le future
  342.         if ($periode == 'next') {
  343.             $sql_news .= ' AND n.date_start>=\'' date('Y-m-d') . '\' ';
  344.         }
  345.         $sql_news .= ' ORDER BY n.date_start LIMIT 0,' . (int)$limit;
  346.         $this->query($sql_news);
  347.         $news = array();
  348.         // mise en forme des donnees
  349.         while ($this->fetch()) {
  350.             $news[] = $this->getInfosNewsRow();
  351.         }
  352.         return $news;
  353.     }
  354.     // }}}
  355.     //  {{{ getRelationsArray()
  356.     /** Tableau des relations
  357.      *
  358.      * @access  public
  359.      * @param string|int $type le type de champ
  360.      */
  361.     public function getRelationsArray($type null)
  362.     {
  363.         $array_types = array(
  364.             // extranetusers idextranetuser
  365.             'idextranetuser' => array(
  366.                 'type_join' => 'simple',
  367.                 'dest_table' => 'extranetusers',
  368.                 'join_field_src' => 'idextranetuser',
  369.                 'fields' => array('idextranetuser''lastname''firstname''login'),
  370.                 'description' => 'Utilisateur(s) associé(s) à cet évènement',
  371.                 'silent_delete_relation' => true,
  372.             ),
  373.             // new_person idnew
  374.             'new_person' => array(
  375.                 'type_join' => 'multiple',
  376.                 'dest_table' => 'persons',
  377.                 'join_table' => 'new_person',
  378.                 'fields' => array('idperson''lastname''firstname''url_clean''valid'),
  379.                 'description' => 'Personne(s) associée(s) à cet évènement',
  380.                 'silent_delete_relation' => true,
  381.             ),
  382.             // new_contact idnew
  383.             'new_contact' => array(
  384.                 'type_join' => 'multiple',
  385.                 'dest_table' => 'contacts',
  386.                 'join_table' => 'new_contact',
  387.                 'fields' => array('idcontact''lastname''firstname''organisation''type''url_clean'),
  388.                 'description' => 'Contact(s) associé(s) à cet évènement',
  389.                 'silent_delete_relation' => true,
  390.             ),
  391.             // new_spectacle idnew
  392.             'new_spectacle' => array(
  393.                 'type_join' => 'multiple',
  394.                 'dest_table' => 'spectacles',
  395.                 'join_table' => 'new_spectacle',
  396.                 'fields' => array('idspectacle''title''valid''publish''url_clean'),
  397.                 'description' => 'Spectacle(s) associé(s) à cet évènement',
  398.                 'silent_delete_relation' => true,
  399.             ),
  400.             // new_text idnew
  401.             'new_text' => array(
  402.                 'type_join' => 'multiple',
  403.                 'dest_table' => 'texts',
  404.                 'join_table' => 'new_text',
  405.                 'fields' => array('idtext''title''valid''publish''url_clean'),
  406.                 'description' => 'texte(s) associé(s) à cet évènement',
  407.                 'silent_delete_relation' => true,
  408.             ),
  409.         );
  410.         if (!empty($array_types[$type])) {
  411.             $array_types[$type]['type'] = $type;
  412.             return $array_types[$type];
  413.         } else {
  414.             return $array_types;
  415.         }
  416.     }
  417.     // }}}
  418.     //  {{{ getObjectTitle()
  419.     /** Infos de l'item recherché
  420.      *
  421.      *
  422.      * @access public
  423.      * @param int $idobject Identifiant de l'item
  424.      * @param array $infos_object Informations pour construire le titre
  425.      * @param array $params Paramétres supplémentaires
  426.      * @return string|array
  427.      */
  428.     public function getObjectTitle($idobject null$infos_object = array(), $params = array())
  429.     {
  430.         if (!$idobject && isset($this->idperson)) $idobject $this->idperson;
  431.         // champs
  432.         $fields = array('idnew''title');
  433.         if (!empty($infos_object)) {
  434.             foreach ($fields as $k => $v) {
  435.                 if (array_key_exists($v$infos_object)) {
  436.                     $infos[$v] = $infos_object[$v];
  437.                 } else {
  438.                     $infos[$v] = '';
  439.                 }
  440.             }
  441.         } else {
  442.             //charger les infos que si necessaire
  443.             if (empty($this->title)) {
  444.                 $sql 'SELECT ' implode(','$fields) . '
  445.                         FROM ' $this->__table '
  446.                         WHERE ' $this->__idtable '=' $idobject;
  447.                 $infos $this->queryRow($sql);
  448.             } else {
  449.                 foreach ($fields as $k => $v) {
  450.                     if (isset($this->$v) && $this->$v != '') {
  451.                         $infos[$v] = trim($this->$v);
  452.                     } else {
  453.                         $infos[$v] = '';
  454.                     }
  455.                 }
  456.             }
  457.         }
  458.         // renvoyer un array plutôt que le titre (utile si on veux l'url)
  459.         if (array_key_exists('return_array'$params)) {
  460.             return $infos;
  461.         }
  462.         return $infos['title'];
  463.     }
  464.     // }}}
  465.     //  {{{ getSQLNews()
  466.     /** Générer la requete standard pour les news
  467.      *
  468.      * $params = array(
  469.      *  'select' => champs en plus pour le select,
  470.      *  'from' => champs en plus pour le from,
  471.      *  'where' => paramètres en plus le where,
  472.      *  'order' => classement des résultats,
  473.      *  'limit' => limiter le nb de résultats array(debut,fin), (all pour pas de limit)
  474.      * )
  475.      * $special = array(
  476.      *  'prochains' => true = date dans le future, un entier = fait un between
  477.      *  'anciens' =>
  478.      *  'join' => jointure gauche sur contacts et schedules
  479.      * )
  480.      *
  481.      *
  482.      * @access   public
  483.      * @param array $params Les paramètres pour la requete
  484.      * @param array $special Les paramètres supplémentaires
  485.      * @param bool $debug afficher la requête TODO
  486.      * @return   string
  487.      */
  488.     public function getSQLNews($params = array(), $special = array(), $debug false)
  489.     {
  490.         $conn $this->getEntityManager();
  491.         // tables et champs
  492.         $tables_list = array(
  493.             'news' => array(
  494.                 'fields' => array('*'),
  495.             ),
  496.             'extranetusers' => array(
  497.                 'fields' => array('idextranetuser''lastname''firstname''signature''city as city_extranetuser''enseignant_etablissement''enseignant_active'),
  498.             ),
  499.             'newschedules' => array(
  500.                 'fields' => array('idnewschedule''date''hour''idcontact''note'),
  501.             ),
  502.         );
  503.         // définition de l'alias
  504.         $al 1;
  505.         foreach ($tables_list as $k => $v) {
  506.             $tables_list[$k]['table_alias'] = $k ' T' $al;
  507.             foreach ($v['fields'] as $f) {
  508.                 $select_fields_array[] = 'T' $al '.' $f;
  509.             }
  510.             $al++;
  511.         }
  512.         // select de base
  513.         $sql_select 'SELECT DISTINCT(T1.' $this->__idtable '), ' join(','$select_fields_array) . ' ';
  514.         // select en plus sous forme de string
  515.         if (array_key_exists('select'$params)) {
  516.             $sql_select .= ',' $params['select'];
  517.         }
  518.         // compte seulement les résulats
  519.         if (array_key_exists('count_only'$special)) {
  520.             $sql_select 'SELECT COUNT(DISTINCT(T1.' $this->__idtable ')) as total ';
  521.         }
  522.         // from avec jointure entre news et extranetusers
  523.         if (!array_key_exists('no_join'$special)) {
  524.             $sql_from ' FROM ' $this->__table ' T1 ';
  525.             // seulement avec calendrier ?
  526.             if (array_key_exists('no_schedule'$special)) {
  527.                 $join_direction 'LEFT';
  528.             } else {
  529.                 $join_direction 'INNER';
  530.             }
  531.             // seulement avec calendrier et contact ?
  532.             if (array_key_exists('no_schedule_contact'$special)) {
  533.                 $join_direction_contact 'INNER';
  534.             } else {
  535.                 $join_direction_contact 'LEFT';
  536.             }
  537.             $sql_from .= $join_direction ' JOIN newschedules T3
  538.                             ON T3.idnew=T1.idnew
  539.                             INNER JOIN extranetusers T2 ON T2.idextranetuser=T1.idextranetuser
  540.                             ' $join_direction_contact ' JOIN contacts T4 ON T4.idcontact=T3.idcontact ';
  541.         } else {
  542.             foreach ($tables_list as $k => $v) {
  543.                 $sql_from_tables[] = $v['table_alias'];
  544.             }
  545.             $sql_from ' FROM ' join(','$sql_from_tables);
  546.         }
  547.         // jointure sur un objet => faire un foreach pour gérer plusieurs contraintes => liaison dans cet object OU avec cet object (cas du calendrier)
  548.         if (array_key_exists('by_object'$special)) {
  549.             $alias_bo 'T' . ($al 1);
  550.             foreach ($special['by_object'] as $k => $v) {
  551.                 $object_by str_replace('id'''$k);
  552.                 $sql_from .= ' LEFT JOIN new_' $object_by ' ' $alias_bo ' ON ' $alias_bo '.' $this->__idtable '=T1.' $this->__idtable ' ';
  553.             }
  554.         }
  555.         // selection de table en plus sius forme de string
  556.         if (array_key_exists('from'$params)) {
  557.             $sql_from .= ',' $params['from'];
  558.         }
  559.         // where standard
  560.         $sql_where ' WHERE 1 ';
  561.         // recherche par objet
  562.         if (array_key_exists('by_object'$special)) {
  563.             foreach ($special['by_object'] as $k => $v) {
  564.                 $sql_where .= ' AND (' $alias_bo '.' $k '=' . (int)$v ' )';
  565.                 //$sql_where .= ' OR T3.idcontact='.$v.')';
  566.                 //$sql_where .= ''
  567.             }
  568.         }
  569.         // paramètres where en plus sous forme de string
  570.         if (array_key_exists('where'$params)) {
  571.             $sql_where .= $params['where'];
  572.         }
  573.         // publie ou pas ?
  574.         if (!array_key_exists('publish'$special)) {
  575.             $sql_where .= ' AND T1.publish=1';
  576.         } else {
  577.             if ($this->my_is_int($special['publish'])) {
  578.                 $sql_where .= ' AND T1.publish=' . (int)$special['publish'];
  579.             }
  580.         }
  581.         if (array_key_exists('is_homepage'$special)) {
  582.             $sql_where .= ' AND T1.`is_homepage`=' . (int)$special['is_homepage'];
  583.         }
  584.         // valid et publié uniquement
  585.         if (array_key_exists('publish_valid'$special)) {
  586.             $sql_where .= ' AND T1.publish=1 AND T1.valid=1 ';
  587.         }
  588.         // type de news ? (spectacle ou évènement)
  589.         if (array_key_exists('type_news'$special)) {
  590.             if ($this->my_is_int($special['type_news'])) {
  591.                 $sql_where .= ' AND T1.type_news=' $special['type_news'] . ' ';
  592.             } else {
  593.                 // 2 types de news
  594.                 $types_news = array(
  595.                     'news' => 0,
  596.                     'spectacles' => 1
  597.                 );
  598.                 // on checke
  599.                 if (array_key_exists($special['type_news'], $types_news)) {
  600.                     $sql_where .= ' AND T1.type_news=' $types_news[$special['type_news']];
  601.                 }
  602.             }
  603.         }
  604.         // par un utuilisateur
  605.         if (array_key_exists('idextranetuser'$special)) {
  606.             if (is_array($special['idextranetuser'])) {
  607.                 $sql_where .= ' AND T1.idextranetuser IN (' join(','$special['idextranetuser']) . ') ';
  608.             } else {
  609.                 $sql_where .= ' AND T1.idextranetuser=' . (int)$special['idextranetuser'] . ' ';
  610.             }
  611.         }
  612.         // dans une structure
  613.         if (array_key_exists('idtypestructure'$special)) {
  614.             $sql_where .= ' AND T4.idtypestructure=\'' $special['idtypestructure'] . '\' ';
  615.         }
  616.         // dans une grande région
  617.         if (array_key_exists('idbigregion'$special)) {
  618.             if (is_array($special['idbigregion'])) {
  619.                 $bigregions $conn->getRepository(Bigregions::class);
  620.                 $sql_region $bigregions->getZipCode(end($special['idbigregion']), 'T4');
  621.             } else {
  622.                 $bigregions $conn->getRepository(Bigregions::class);
  623.                 $sql_region $bigregions->getZipCode($special['idbigregion'], 'T4');
  624.             }
  625.             $sql_where_and[] = str_replace(' AND T4.country''T4.country'$sql_region);
  626.         }
  627.         // dans un pays
  628.         if (array_key_exists('country'$special)) {
  629.             foreach ($special['country'] as $c) {
  630.                 $country_tabs[] = '\'' $c '\'';
  631.             }
  632.             $sql_where_and[] = ' T4.country IN (' join(','$country_tabs) . ') ';
  633.         }
  634.         // prochains uniquement => date comprise entre le début et la fin
  635.         if (array_key_exists('prochains'$special)) {
  636.             // convertir à date du jour + le params
  637.             $sql_where_and[] = ' T3.date>=\'' date('Y-m-d') . '\' ';
  638.         }
  639.         // anciens
  640.         if (array_key_exists('anciens'$special)) {
  641.             // évènements terminé
  642.             $sql_where_and[] = ' T3.date<\'' date('Y-m-d') . '\' ';
  643.         }
  644.         // nouveau code
  645.         if (!empty($sql_where_and)) {
  646.             $sql_where_lieu_date[] = '(' join(' AND '$sql_where_and) . ')';
  647.         }
  648.         $old_code false;
  649.         if ($old_code) {
  650.             // dans une région
  651.             if (array_key_exists('idregion'$special)) {
  652.                 $sql_where_old[] = 'T1.idregion IN (' join(','$special['idregion']) . ') ';
  653.             }
  654.             // dans un pays
  655.             if (array_key_exists('country'$special)) {
  656.                 foreach ($special['country'] as $c) {
  657.                     $country_tabs[] = '\'' $c '\'';
  658.                 }
  659.                 $sql_where_old[] = 'T1.country IN (' join(','$country_tabs) . ') ';
  660.             }
  661.             // prochains uniquement => date comprise entre le début et la fin
  662.             if (array_key_exists('prochains'$special)) {
  663.                 if ($this->my_is_int($special['prochains'])) {
  664.                     // convertir à date du jour + le params
  665.                     $sql_where_old[] = 'T1.date_start>NOW() ';
  666.                 } else {
  667.                     // évènements commencé mais non terminé
  668.                     $sql_where_old_date '(';
  669.                     $sql_where_old_date .= ' (date_start<=NOW() AND (date_end IS NOT NULL AND date_end>=NOW()) ) ';
  670.                     $sql_where_old_date .= ' OR ';
  671.                     //évènement qui vont commencer
  672.                     $sql_where_old_date .= ' (date_start>=\'' date('Y-m-d') . '\') ';
  673.                     $sql_where_old_date .= ') ';
  674.                     $sql_where_old[] = $sql_where_old_date;
  675.                 }
  676.             }
  677.             // anciens
  678.             if (array_key_exists('anciens'$special)) {
  679.                 // évènements terminé
  680.                 $sql_where_old_date '(';
  681.                 $sql_where_old_date .= ' (date_start<NOW() AND (date_end IS NOT NULL AND date_end<NOW()) ) ';
  682.                 $sql_where_old_date .= ' OR ';
  683.                 //évènement qui vont commencer
  684.                 $sql_where_old_date .= ' (date_start<NOW()) ';
  685.                 $sql_where_old_date .= ') ';
  686.                 $sql_where_old[] = $sql_where_old_date;
  687.             }
  688.             // ancien code
  689.             if (!empty($sql_where_old)) {
  690.                 $sql_where_lieu_date[] = '(' join(' AND '$sql_where_old) . ')';
  691.             }
  692.         } // fin ancien code
  693.         if (!empty($sql_where_lieu_date)) $sql_where .= ' AND (' join(' OR '$sql_where_lieu_date) . ') ';
  694.         $order ' ';
  695.         $groupby ' ';
  696.         $limit ' ';
  697.         if (!array_key_exists('count_only'$special)) {
  698.             // groupement par défaut
  699.             if (!array_key_exists('groupby'$params)) {
  700.                 $groupby ' GROUP BY T1.' $this->__idtable ' ';
  701.             } else {
  702.                 $groupby ' GROUP BY ' $params['groupby'];
  703.             }
  704.             // classement par défaut
  705.             if (!array_key_exists('order'$params)) {
  706.                 // si ancien, classement DESC
  707.                 if (array_key_exists('anciens'$special)) {
  708.                     $classement 'DESC';
  709.                 } else {
  710.                     $classement 'ASC';
  711.                 }
  712.                 if (array_key_exists('classement'$params)) {
  713.                     $classement $params['classement'];
  714.                 }
  715.                 if (!empty($sql_where_old)) {
  716.                     $order ' ORDER BY T3.date, T1.date_start ' $classement;
  717.                 } else {
  718.                     $order ' ORDER BY T3.date ' $classement;
  719.                 }
  720.             } else {
  721.                 $order ' ORDER BY ' $params['order'];
  722.             }
  723.         }
  724.         // classement aléatoire
  725.         if (array_key_exists('random'$special)) {
  726.             $order ' ORDER BY RAND() ' $special['random'];
  727.         }
  728.         // limite par défaut
  729.         if (!array_key_exists('limit'$params)) {
  730.             $limit ' LIMIT 0,10';
  731.         } else {
  732.             if ($params['limit'] != 'all') {
  733.                 $limit ' LIMIT ' $params['limit'][0] . ',' $params['limit'][1];
  734.             } else {
  735.                 $limit ' ';
  736.             }
  737.         }
  738.         // compte le tout
  739.         if (array_key_exists('count_only'$special) && $special['count_only'] == 'all') {
  740.             $limit ' ';
  741.         }
  742.         // random sur les résultats ?
  743.         $sql_global $sql_select $sql_from $sql_where $groupby $order $limit;
  744.         return $sql_select $sql_from $sql_where $groupby $order $limit;
  745.     }
  746.     // }}}
  747.     //  {{{ getListNews()
  748.     /** Récupére la liste des news
  749.      *
  750.      * Récupére les news de manière standard
  751.      *
  752.      * @access   public
  753.      * @param array $params_sql paramètre pour la requete SQL
  754.      * @param array $format de la vignette si pas standard
  755.      * @param bool $debug mode débugage
  756.      * @return   array
  757.      */
  758.     public function getListNews($params_sql = array(), $params = array(), $debug false)
  759.     {
  760.         // si pas de tableau
  761.         if (!isset($params_sql['special'])) {
  762.             $params_sql['special'] = array();
  763.         }
  764.         if (!isset($params_sql['params'])) {
  765.             $params_sql['params'] = array();
  766.         }
  767.         if (!empty($params['display'])) {
  768.             $display $params['display'];
  769.             unset($params['display']);
  770.         } else {
  771.             $display = array();
  772.         }
  773.         // pagination ?
  774.         if (!empty($params_sql['special']['pagerOptionsFromController'])) {
  775.             $limit $params_sql['special']['pagerOptionsFromController']['limit'];
  776.             $perPage $params_sql['special']['pagerOptionsFromController']['perPage'];
  777.             $params_sql['params']['limit'] = array($limit$perPage);
  778.         }
  779.         // construction de la requête avec le passage de paramètres
  780.         $sql $this->getSQLNews($params_sql['params'], $params_sql['special'], $debug);
  781.         // paramètre de debug
  782.         $dbg = (empty($params_sql['dbg'])) ? array() : $dbg $params_sql['dbg'];
  783.         // executer la requete
  784.         $datas $this->query($sql$dbg);
  785.         $nws = array();
  786.         $params_row = array();
  787.         // format de la vignette
  788.         if (!empty($params['vignette_format'])) {
  789.             $vignette_format $params['vignette_format'];
  790.         } else {
  791.             $vignette_format = array('width' => 100'height' => 70'direction' => 'thumb');
  792.         }
  793.         // debug
  794.         $start_boucle microtime(true);
  795.         // mettre en forme chaque reponse
  796.         $conn $this->getEntityManager();
  797.         $x $conn->getRepository(News::class);
  798.         foreach ($datas as $data) {
  799.             $this->loadResult($data);
  800.             $y $this->getInfosNewsRow($this->{$this->__idtable}, $params_sql);
  801.             $idnew $y['idnew'];
  802.             // il faut classer par type
  803.             if (!empty($params['par_type'])) {
  804.                 if ($this->type_news == 0) {
  805.                     $nws['news'][$idnew] = $y;
  806.                 } else {
  807.                     $nws['spectacles'][$idnew] = $y;
  808.                 }
  809.             } else {
  810.                 $nws[$idnew] = $y;
  811.             }
  812.             $nws[$idnew]['display'] = $display;
  813.         }
  814.         // libère
  815.         $this->free();
  816.         // debug de la boucle
  817.         if ($this->isTraceMode('list_object')) {
  818.             $this->setTraceInfos(array(
  819.                 'execution_time_boucle' => (microtime(true) - $start_boucle),
  820.                 'dbg' => $dbg,
  821.             ));
  822.         }
  823.         return $nws;
  824.     }
  825.     // }}}
  826.     //  {{{ countNews()
  827.     /** compter le nombre de news
  828.      *
  829.      * @access   public
  830.      * @param array $params paramètre (exemple : by_object => idperson)
  831.      * @return   int
  832.      */
  833.     public function countNews($news_params = array())
  834.     {
  835.         $news_params['special']['count_only'] = true;
  836.         if (empty($news_params['sql'])) {
  837.             $news_params['sql'] = array();
  838.         }
  839.         $sql $this->getSQLNews($news_params['sql'], $news_params['special']);
  840.         $nb_news $this->queryOne($sql);
  841.         if ($nb_news) {
  842.             return $nb_news;
  843.         } else {
  844.             return null;
  845.         }
  846.     }
  847.     // }}}
  848.     //  {{{ getTypeNew()
  849.     /** Les types de news
  850.      *
  851.      * @access   public
  852.      * @return   array
  853.      */
  854.     public function getTypesNews($type_news null)
  855.     {
  856.         $types_news = array(
  857.             => 'news',
  858.             => 'spectacles',
  859.         );
  860.         return $types_news;
  861.     }
  862.     //
  863.     //  {{{ getVignetteNew()
  864.     /** Récupère la vignette d'une news ou la cree si besoin
  865.      *
  866.      * @access   public
  867.      * @param int idspectacle identifiant du spectacle
  868.      * @param array format $format de la vignette
  869.      * @return   string
  870.      */
  871.     public function getVignetteNew($idnew null$format = array(), $url_file null)
  872.     {
  873.         // id de la new
  874.         if (empty($idnew)) {
  875.             $idnew $this->idnew;
  876.         }
  877.         $idnew = (int)$idnew;
  878.         // par default : image arrondie crop 100px x 100px qualite 75
  879.         if (!empty($format)) {
  880.             $set_format $format;
  881.         } else {
  882.             $set_format = array('width' => 80'height' => 80'direction' => 'crop''forme' => 'round''valround' => 100'default' => '../../default/news-def.png');
  883.         }
  884.         // generation par la classe Images
  885.         if (!empty($url_file)) {
  886.             $url_file $_ENV['PATH_DIRECTORYUPLOAD'] . 'news/src/' $this->images->getHashDir($url_filetrue) . $url_file;
  887.         } else {
  888.             $sql 'SELECT url_file FROM ' $this->__table ' WHERE idnew=' . (int)$idnew;
  889.             $url_file $this->queryOne($sql);
  890. //            if(empty($url_file) || is_null($url_file)) {
  891. //                $url_file = null;
  892. //            } else {
  893.             $url_file $_ENV['PATH_DIRECTORYUPLOAD'] . 'news/src/' $this->images->getHashDir($url_filetrue) . $url_file;
  894. //            }
  895.         }
  896.         $this->images->setVignetteFormat($set_format);
  897.         return $this->images->getVignetteObject($idnew'news''unknow_classification'$url_file);
  898.     }
  899.     // }}}
  900.     //  {{{ delete()
  901.     /** Supprimer une news et ses images
  902.      *
  903.      * @access   public
  904.      * @param int idnew identifiant du new
  905.      * @param array format $format de la vignette
  906.      * @return   string
  907.      */
  908.     public function deleteData($idnew null$url_file null)
  909.     {
  910.         $conn $this->getEntityManager();
  911.         // si on a l'URL du fichier, on peux le supprimer ainsi que ses vignettes
  912.         if (empty($url_file)) {
  913.             $sql 'SELECT url_file FROM ' $this->__table ' WHERE idnew=' . (int)$idnew;
  914.             $url_file $this->queryOne($sql);
  915.         }
  916.         if (!empty($url_file)) {
  917.             // classe des images
  918.             $url_file_src $_ENV['PATH_DIRECTORYUPLOAD'] . 'news/src/' $this->images->getHashDir($url_filetrue) . $url_file;
  919.             // supprimer les vignettes
  920.             $files $conn->getRepository(Files::class);
  921.             $files->deleteVignettes(null'news'$idnew, array('file' => $url_file));
  922.             // supprimer le fichier
  923.             if (file_exists($url_file_src)) {
  924.                 @unlink($url_file_src);
  925.             }
  926.         }
  927.         // supprimer la news
  928.         $this->deleteDataTrait($idnew);
  929.         return true;
  930.     }
  931.     // }}}
  932.     //  {{{ getProchDateLieu()
  933.     /** Lieu et date d'une news
  934.      *
  935.      * Pour une news, la prochaine date et ville pour (au choix) :
  936.      * - un contact donné
  937.      * - une date passée ou future
  938.      * - une zone géographique donnée
  939.      *
  940.      * @access   public
  941.      * @param string $periode futur ou passé ?
  942.      * @param int $idnew identifiant de la news
  943.      * @param int $idcontact date spécifiquement dans un lieu ?
  944.      * @param int $zone date spécifiquement dans une zone géographique ?
  945.      * @return   array
  946.      */
  947.     public function getProchDateLieu($periode 'prochains'$idnew$idcontact null$zone null$special_schedule null)
  948.     {
  949.         // trouver la date et le lieu le plus proche dans le futur ou le passé
  950.         if (isset($this->lastdate) || array_key_exists('anciens'$periode)) {
  951.             $eq '<';
  952.             $tri 'DESC';
  953.         } elseif (array_key_exists('plus_proche'$periode)) { // la premiere date future
  954.             $tri 'ASC';
  955.             if ($periode['plus_proche'] == 'all') {
  956.                 $eq '>=';
  957.             }
  958.         } else {
  959.             $eq '>=';
  960.             $tri 'ASC';
  961.         }
  962.         // voir si on doit partir d'une date précise pour la recherche de la date la plus proche dans le futur ou le passé
  963.         if (array_key_exists('depuis_le'$periode)) {
  964.             $date_debut_recherche $periode['depuis_le'];
  965.         } else {
  966.             $date_debut_recherche date('Y-m-d');
  967.         }
  968.         // on veux exclure un contact en créeant une table temporaire
  969.         if ($special_schedule) {
  970.             $from_schedules '(SELECT idcontact,idnew,date
  971.                                  FROM newschedules
  972.                                  WHERE idnew=' . ((int)$idnew) . '
  973.                                  AND idcontact !=' $idcontact '
  974.                                  AND date' $eq '\'' $date_debut_recherche '\') AS';
  975.         } else {
  976.             $from_schedules 'newschedules';
  977.         }
  978.         // le select pour trouver la première date future
  979.         $select_prochdate 'sc.date, c.city, c.country, c.zip, c.organisation, c.url_clean, c.idcontact';
  980.         // le select pour trouver la periode
  981.         $select_periode 'MIN(sc.date) as datestart, MAX(sc.date) as dateend, c.city, c.country';
  982.         // le from commun pour prochaine date et periode
  983.         $from_commun ' FROM ' $from_schedules ' sc, contacts c
  984.                          WHERE sc.idcontact=c.idcontact
  985.                          AND sc.idnew=' . ((int)$idnew);
  986.         // recherche de la date la plus proche passé ou future
  987.         $sql_prochdate 'SELECT ' $select_prochdate $from_commun;
  988.         // init
  989.         $sql_more null;
  990.         if ($from_schedules == 'newschedules' && !empty($eq)) {
  991.             $sql_more .= ' AND sc.date' $eq '\'' $date_debut_recherche '\' ';
  992.         }
  993.         // uniquement dans un lieu
  994.         if ($idcontact) {
  995.             if (is_array($idcontact)) {
  996.                 $sql_more .= ' AND c.idcontact IN (' join(','$idcontact) . ')';
  997.             } else {
  998.                 $sql_more .= ' AND c.idcontact=' . ((int)$idcontact);
  999.             }
  1000.         }
  1001.         // sur une zone geographique précise
  1002.         if (isset($this->zone_recherche['zip'])) {
  1003.             $sql_more .= $this->zone_recherche['zip'];
  1004.         }
  1005.         // on tri par date et on ne prends que le premier pour le début de la période
  1006.         $sql_prochdate_limit ' ORDER BY sc.date ' $tri ' LIMIT 0,1';
  1007.         $sql_prochdate_construct $sql_prochdate $sql_more $sql_prochdate_limit;
  1008.         $prochdate $this->queryRow($sql_prochdate_construct);
  1009.         // si il n'y a pas de prochaine date, rechercher la précédente
  1010.         if (empty($prochdate['date']) && array_key_exists('plus_proche'$periode)) {
  1011.             $sql_prochdate_construct str_replace('>=''<='$sql_prochdate_construct);
  1012.             $sql_more str_replace('>=''<='$sql_more);
  1013.             $prochdate $this->queryRow($sql_prochdate_construct);
  1014.         }
  1015.         if (!empty($prochdate['date'])) {
  1016.             // recherche la suite de la période dans ce lieu (date début et date de fin dans le lieu)
  1017.             $sql_periode 'SELECT ' $select_periode $from_commun;
  1018.             //$sql_periode_construct = $sql_periode.$sql_more.' GROUP BY c.idcontact';
  1019.             $sql_periode_construct $sql_periode $sql_more ' AND sc.idcontact=' $prochdate['idcontact'] . ' GROUP BY sc.idcontact';
  1020.             $prochedate_periode $this->queryAll($sql_periode_construct);
  1021.             $prochedate_periode $prochedate_periode[0];
  1022.             // debut et fin de la période en timestamp
  1023.             $prochedate_periode['datestart_std'] = $prochedate_periode['datestart'];
  1024.             $prochedate_periode['dateend_std'] = $prochedate_periode['dateend'];
  1025.             // début et fin de la période en français
  1026.             $prochedate_periode['datestart'] = $this->getFormeDate($prochedate_periode['datestart'], "%a %d/%m/%y"true);
  1027.             $prochedate_periode['dateend'] = $this->getFormeDate($prochedate_periode['dateend'], "%a %d/%m/%y"true);
  1028.             // si date start est égale a dateend, alors on a qu'une date
  1029.             if ($prochedate_periode['datestart_std'] == $prochedate_periode['dateend_std']) {
  1030.                 $prochedate_periode['datestart'] = null;
  1031.             }
  1032.             if (empty($prochedate_periode['datestart'])) {
  1033.                 $spec['prochdate'] = $prochedate_periode['dateend'];
  1034.             } else {
  1035.                 $spec['periode'] = $prochedate_periode;
  1036.                 $spec['prochdate'] = null;
  1037.             }
  1038.             // la ville et le pays de la prochaine date ou de la période
  1039.             $spec['city'] = $prochedate_periode['city'];
  1040.             $spec['country'] = $prochedate_periode['country'];
  1041.             // date standard
  1042.             $spec['date_std'] = $prochdate['date'];
  1043.             // infos sur le contact
  1044.             if (!empty($prochdate['organisation'])) {
  1045.                 $spec['contact'] = array(
  1046.                     'organisation' => $prochdate['organisation'],
  1047.                     'idcontact' => $prochdate['idcontact'],
  1048.                     'url_clean' => $prochdate['url_clean']
  1049.                 );
  1050.             }
  1051.             return $spec;
  1052.         }
  1053.         return array();
  1054.     }
  1055.     // }}}
  1056.     //  {{{ setZoneRecherche()
  1057.     /** Zone de recherche d'un new
  1058.      *
  1059.      * @access   public
  1060.      * @param array $zone Zone de recherche
  1061.      */
  1062.     public function setZoneRecherche($zone)
  1063.     {
  1064.         $this->zone_recherche['zip'] = $zone;
  1065.     }
  1066.     // }}}
  1067. }