vendor/theatre/core/src/Repository/NewschedulesRepository.php line 306

Open in your IDE?
  1. <?php
  2. namespace TheatreCore\Repository;
  3. use TheatreCore\Classes\Date\Calendar;
  4. use TheatreCore\Entity\Contacts;
  5. use TheatreCore\Entity\Newschedules;
  6. use TheatreCore\Traits\TheatreTrait;
  7. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. /**
  10.  * NewschedulesRepository
  11.  *
  12.  * This class was generated by the Doctrine ORM. Add your own custom
  13.  * repository methods below.
  14.  */
  15. class NewschedulesRepository extends ServiceEntityRepository
  16. {
  17.     use TheatreTrait;
  18.     private $em;
  19.     public $__table;
  20.     public $__table_object;
  21.     public $__idtable;
  22.     public function __construct(ManagerRegistry $registry)
  23.     {
  24.         parent::__construct($registryNewschedules::class);
  25.         $this->__table $this->getClassMetadata()->getTableName();
  26.         $this->__table_object $this->getClassMetadata()->name;
  27.         $this->__idtable $this->getClassMetadata()->getSingleIdentifierFieldName();
  28. //        $this->__table = 'persons';
  29. //        $this->__table_object = '\\App\\Entity\\Persons';
  30.     }
  31.     //  {{{ getHours()
  32.     /** heures du spectacle
  33.      *
  34.      * @access  public
  35.      * @return  array
  36.      * @param   string  $date La date du spectacle
  37.      * @param   int     $idspectacle identifiant du spectacle
  38.      * @param   int     $idcontact identifiant du contact
  39.      */
  40.     public function getHours($date,$idnew,$idcontact)
  41.     {
  42.         $query 'SELECT hour,note 
  43.                   FROM newschedules 
  44.                   WHERE date="'.$date.'" 
  45.                   AND idnew='.$idnew.
  46.                   AND idcontact='.$idcontact;
  47.         $datas $this->query($query);
  48.         foreach($datas AS $row) {
  49.             if($row['hour']!='00:00') {
  50.                 $hour[]['hour'] = $row['hour'];
  51.             }
  52.             $hour[]['note'] = $row['note'];
  53.         }
  54.         unset($datas);
  55.         return $hour;
  56.     }
  57.     // }}}
  58.     //  {{{ getItemsList()
  59.     /** Liste des items pour les champs relationnels
  60.      *
  61.      * Retourne un tableau associatif contenant la liste des items d'une relation
  62.      *
  63.      * @access  public
  64.      * @return  array
  65.      * @param   string $type        Ce qu'on recherche
  66.      * @param   string $idtext      L'id de l'objet
  67.      */
  68.     public function getItemsList($idcontact,$type)
  69.     {
  70.         $conn $this->getEntityManager();
  71.         // on doit recherche l'id contact qui correspond
  72.         if($type=='idcontact') {
  73.             $contact $conn->getRepository(Contacts::class)->getObjectTitle($idcontact,array(),array('return_array' => true));
  74.             return array($contact['idcontact'] => array(
  75.                 'id' => $contact['idcontact'],
  76.                 'view_name' => $contact['title']
  77.             ));
  78.         }  else {
  79.             return $idnewschedule.' Aucune fonction de recherche pour le type <strong>'.$type.'</strong> dans la méthode !';
  80.         }
  81.     }
  82.     // }}}
  83.     //  {{{ getDatesHours()
  84.     /** Dates et heures de l'évènement
  85.      *
  86.      * @access  public
  87.      * @return  array
  88.      * @param   int     $idnew identifiant du spectacle
  89.      * @param   int     $idcontact identifiant du contact
  90.      */
  91.     public function getDatesHours($idnew,$idcontact,$type=null)
  92.     {
  93.         $sql 'SELECT DISTINCT(date), note 
  94.                 FROM newschedules 
  95.                 WHERE idnew='.$idnew.'
  96.                 AND idcontact='.$idcontact.' ';
  97.         // pour les dates futurs, commencer au début du mois
  98.         if(!empty($type) && $type!='tous') {
  99.             if ($type == 'demain') {
  100.                 $expr '>=';
  101.                 $date_search date('Y-m-01');
  102.             } else {
  103.                 $expr '<=';
  104.                 $date_search date('Y-m-d');
  105.             }
  106.             $sql .= ' AND date'.$expr.'\''.$date_search.'\' ';
  107.         }
  108.         // classement
  109.         $sql .= ' ORDER BY date';
  110.         $res $this->query($sql);
  111.         //setDebug('SQL datehour : '.$sql);
  112.         $nbresult count($res);
  113.         if($nbresult>1) {
  114.             $dateshours['periode'] = true;
  115.         }
  116.         $i 0;
  117.         foreach($res AS $row) {
  118.             $this->loadResult($row);
  119.             // année mois
  120.             $y $this->getFormeDate($row['date'],"%y",true);
  121.             $m $this->getFormeDate($row['date'],"%m",true);
  122.             // format string ou classique
  123.             $date_str $this->getFormeDate($row['date'],"%a %d/%m/%y",true);
  124.             $date_orig $this->getFormeDate($row['date'],$GLOBALS['format_date'][$GLOBALS['lng']]);
  125.             // une période
  126.             if($nbresult>1) {
  127.                 $dateshours[$date_orig]['hours'] = $this->getHours($row['date'],$idnew,$idcontact);
  128.                 if($type=='demain' && empty($date_debut_ok)) {
  129.                     if($row['date']>=date('Y-m-d')) {
  130.                         $dateshours[$date_orig]['datedebut'] = $date_str;
  131.                         $date_debut_ok true;
  132.                     }
  133.                 } else {
  134.                     if($i==0) { // première date = date de début
  135.                         $dateshours[$date_orig]['datedebut'] = $date_str;
  136.                     }
  137.                 }
  138.                 if($i==$nbresult-1) {
  139.                     $dateshours[$date_orig]['datefin'] = $date_str;
  140.                 }
  141.             } else { // inde date unique
  142.                 $dateshours['date'] = $date_orig;
  143.                 $dateshours['hours'] = $this->getHours($row['date'],$idnew,$idcontact);
  144.             }
  145.             $hour_string = array();
  146.             if(!empty($dateshours[$date_orig]['hours'])) {
  147.                 foreach($dateshours[$date_orig]['hours'] as $k=>$v) {
  148.                     if(!empty($v['hour'])) {
  149.                         $hour_string[] = $v['hour'];
  150.                     }
  151.                 }
  152.             }
  153.             // plusieurs heures ou une seule
  154.             if(!empty($hour_string)) {
  155.                 $hour_string join(' / '$hour_string);
  156.             } else {
  157.                 $hour_string null;
  158.             }
  159.             $dateshours['event'][$row['date']] = array(
  160.                 'date' => $row['date'],
  161.                 'hour' => $hour_string,
  162.             );
  163.             $dateshours['months'][$y.$m] = array('y' => $y'm' => $m);
  164.             if(!empty($row['note'])) {
  165.                 $dateshours['event'][$row['date']]['note'] = $row['note'];
  166.             }
  167.             $i++;
  168.         }
  169.         unset($res);
  170.         // classement months
  171.         ksort($dateshours['months']);
  172.         ksort($dateshours['event']);
  173.         return $dateshours;
  174.     }
  175.     // }}}
  176.     //  {{{ getRelationsArray()
  177.     /** Tableau des relations
  178.      *
  179.      * @access  public
  180.      * @param string|int $type le type de champ
  181.      */
  182.     public function getRelationsArray($type=null)
  183.     {
  184.         $array_types = array(
  185.             'idcontact' => array(
  186.                 'type_join' => 'simple',
  187.                 'dest_table' => 'contacts',
  188.                 'join_field_src' => 'idcontact',
  189.                 'fields' => array('idcontact','organisation','firstname','lastname','url_clean'),
  190.                 'silent_delete_relation' => true,
  191.                 'description' => 'Lieu de représentation associé à cette date',
  192.             )
  193.         );
  194.         if(!empty($array_types[$type])) {
  195.             $array_types[$type]['type'] = $type;
  196.             return $array_types[$type];
  197.         } else {
  198.             return $array_types;
  199.         }
  200.     }
  201.     // }}}
  202.     // {{{ getDateLieuCommon()
  203.     /** Code commun à getDateLieuDemain() et getDateLieuHier(),
  204.      *  Liste des dates pour un spectacle
  205.      *
  206.      *
  207.      * @param  string $type demain|hier
  208.      * @param  int    $idnew
  209.      * @param  string $limit
  210.      * @param  string $order ASC|DESC
  211.      * @access protected
  212.      * @return array
  213.      */
  214.     public function getDateLieuCommon($type='demain',$idnew,$limit,$order)
  215.     {
  216.         if ($type=='demain') {
  217.             $expr '>=';
  218.         } elseif($type=='hier') {
  219.             $expr '<=';
  220.         } else {
  221.             $expr 'tous';
  222.         }
  223.         //récupérer les informations et la date de début
  224.         $sql 'SELECT DISTINCT(s.idnewschedule),s.note,s.hour,c.idcontact,c.organisation,c.phone1,c.phone2,c.city,c.url_clean,MIN(s.date) AS datedebut
  225.                   FROM newschedules s,contacts c 
  226.                   WHERE s.idcontact=c.idcontact
  227.                   AND s.idnew='.((int)$idnew);
  228.         if($expr!='tous') {
  229.             $sql .= ' AND s.date'.$expr.'\''.date('Y-m-d').'\'';
  230.         }
  231.         $sql .= ' GROUP BY s.idcontact ORDER BY s.date '.$order;
  232.         //$sql .= ' GROUP BY s.idcontact ORDER BY SIGN(DATEDIFF(NOW(), s.`date`)) ASC, ABS (DATEDIFF(NOW(), s.`date`)) ';
  233.         if(!empty($limit)) {
  234.             $sql .= ' LIMIT 0,'.((int)$limit);
  235.         }
  236.         //setDebug($sql);
  237.         //executer la requete
  238.         $datas $this->query($sql);
  239.         //tableau de retour
  240.         $r = array();
  241.         // génération du calendrier
  242.         $fields_contact_select = array('city','url_clean','idcontact','organisation','phone1','phone2' );
  243.         //trouver la date de fin pour chaque spectacle
  244.         foreach($datas AS $data) {
  245.             $this->loadResult($data);
  246.             foreach($fields_contact_select as $f) {
  247.                 if(isset($this->$f))  {
  248.                     $r[$this->idnewschedule][$f] = $this->$f;
  249.                 }
  250.             }
  251.             $r[$this->idnewschedule]['dateshours'] = $this->getDatesHours($idnew,$this->idcontact,$type);
  252.             $r[$this->idnewschedule]['event'] = $r[$this->idnewschedule]['dateshours']['event'];
  253.             $r[$this->idnewschedule]['months'] = $r[$this->idnewschedule]['dateshours']['months'];
  254.             // ne pas encombrer le tableau
  255.             unset($r[$this->idnewschedule]['dateshours']['months']);
  256.             unset($r[$this->idnewschedule]['dateshours']['event']);
  257.             // générer le calendrier
  258.             // première date
  259.             foreach($r[$this->idnewschedule]['event'] as $k=>$v) {
  260.                 $calendar = new Calendar($v['date']);
  261.                 break;
  262.             }
  263.             // highlight les dates
  264.             $i 0;
  265.             foreach($r[$this->idnewschedule]['months'] as $k=>$v) {
  266.                 if($i>0) {
  267.                     $calendar = new Calendar($v['y'].'-'.$v['m'].'-01');
  268.                 }
  269.                 $calendar->highlighted_dates $r[$this->idnewschedule]['event'];
  270.                 $calendar->link_days 2;
  271.                 $calendar->formatted_link_to "javascript:;";
  272.                 $r[$this->idnewschedule]['calendar'][] = $calendar->output_calendar();
  273.                 $i++;
  274.             }
  275.         }
  276.         $this->free();
  277.         //retourner le tableau
  278.         return $r;
  279.     }
  280.     // }}}
  281.     // {{{ deleteSchedule()
  282.     /** Supprimer toutes les dates pour une news et/ou un lieu
  283.      *
  284.      *
  285.      * @param  int $idnew Identifiant de la news
  286.      * @param  int    $idcontact Identifiant du contact
  287.      * @access public
  288.      * @return array
  289.      */
  290.     public function deleteSchedule($idnew,$idcontact=null)
  291.     {
  292.         $sql 'DELETE FROM '.$this->__table.
  293.                 WHERE idnew='.(int)$idnew;
  294.         if(!empty($idcontact)) {
  295.             $sql .= ' AND idcontact='.(int)$idcontact;
  296.         }
  297.         return $this->execute($sql);
  298.     }
  299.     // }}}
  300. }