vendor/theatre/core/src/Repository/PersonEcoleRepository.php line 345

Open in your IDE?
  1. <?php
  2. namespace TheatreCore\Repository;
  3. use TheatreCore\Entity\EcoleRole;
  4. use TheatreCore\Entity\PersonEcole;
  5. use TheatreCore\Entity\Persons;
  6. use TheatreCore\Traits\TheatreTrait;
  7. use DateTime;
  8. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. /**
  11.  * TextAideRepository
  12.  *
  13.  * This class was generated by the Doctrine ORM. Add your own custom
  14.  * repository methods below.
  15.  */
  16. class PersonEcoleRepository extends ServiceEntityRepository
  17. {
  18.     use TheatreTrait;
  19.     private $em;
  20.     public $__table;
  21.     public $__table_object;
  22.     public $__idtable;
  23.     public function __construct(ManagerRegistry $registry)
  24.     {
  25.         parent::__construct($registryPersonEcole::class);
  26.         $this->__table $this->getClassMetadata()->getTableName();
  27.         $this->__table_object $this->getClassMetadata()->name;
  28.         $this->__idtable $this->getClassMetadata()->getSingleIdentifierFieldName();
  29. //        $this->__table = 'persons';
  30. //        $this->__table_object = '\\App\\Entity\\Persons';
  31.     }
  32.     //  {{{ countEleves()
  33.     /** Compter le nombre d'élèves
  34.      *
  35.      * @access   public
  36.      * param     int idcontact identifiant du contact
  37.      * @return   int
  38.      */
  39.     public function countEleves($idcontact null$params = array())
  40.     {
  41.         $sql 'SELECT COUNT(DISTINCT(idperson)) 
  42.                 FROM ' $this->__table ' WHERE 1 ';
  43.         if (!empty($idcontact)) {
  44.             if (is_array($idcontact)) {
  45.                 $sql .= ' AND idcontact IN (' $this->arrayToIn($idcontact) . ') ';
  46.             } else {
  47.                 $sql .= ' AND idcontact=' . (int)$idcontact;
  48.             }
  49.         }
  50.         // exclure des structures
  51.         if (!empty($params['exclude']['idcontacts'])) {
  52.             $sql .= ' AND idcontact NOT IN (' $this->arrayToIn($params['exclude']['idcontacts']) . ') ';
  53.         }
  54.         // uniquement les anciens
  55.         if (!empty($params['old'])) {
  56.             if ($idcontact == 3914) {
  57.                 $sql .= ' AND date_end < \'' date('Y') . '-06-30 00:00:00\' ';
  58.             } else {
  59.                 $sql .= ' AND date_end < \'' date('Y-m-d') . ' 00:00:00\' ';
  60.             }
  61.         }
  62.         // uniquement les courants
  63.         if (!empty($params['current'])) {
  64.             if ($idcontact == 3914) {
  65.                 $sql .= ' AND date_end > \'' date('Y-m-d') . '-06-30 00:00:00\' ';
  66.             } else {
  67.                 $sql .= ' AND (date_end >= \'' date('Y-m-d') . '\' OR date_end=\'0000-00-00 00:00:00\' OR date_end IS NULL)';
  68.             }
  69.         }
  70.         $nb_eleves $this->queryOne($sql);
  71.         if ($nb_eleves) {
  72.             return $nb_eleves;
  73.         }
  74.     }
  75.     // }}}
  76.     //  {{{ getInfosPerson_ecole()
  77.     /** R&écupèrer les infos
  78.      *
  79.      * @access   public
  80.      * param     int idcontact identifiant du contact
  81.      * param     int idperson identifiant de la personne
  82.      * @return   array
  83.      */
  84.     public function getInfosPerson_ecole($idcontact$idperson)
  85.     {
  86.         try {
  87.             $sql 'SELECT * FROM ' $this->__table ' WHERE idcontact=' . (int)$idcontact ' AND idperson=' . (int)$idperson;
  88.             $infos $this->queryRow($sql);
  89.             if (empty($infos)) {
  90.                 return [];
  91.             }
  92.             // formater les dates
  93.             foreach (array('start''end') as $f) {
  94.                 $day substr($infos['date_' $f], 82);
  95.                 $month substr($infos['date_' $f], 52);
  96.                 $year substr($infos['date_' $f], 04);
  97.                 $infos['date_' $f '_day'] = $day;
  98.                 $infos['date_' $f '_month'] = $month;
  99.                 $infos['date_' $f '_year'] = $year;
  100.                 $infos['date_' $f '_str'] = $day '/' $month '/' $year;
  101.             }
  102.             if (empty($infos)) {
  103.                 return [];
  104.             }
  105.                 return $infos;
  106.         } catch (\Throwable $e) {
  107.             return [];
  108.         }
  109.     }
  110.     // }}}
  111.     //  {{{ getItemsList()
  112.     /** Liste des items pour les champs relationnels
  113.      *
  114.      * Retourne un tableau associatif contenant la liste des items
  115.      * d'une relation. Exemple : getItemsList(4,'text_author'); retourne les noms
  116.      * et prénom des auteurs du texte numéro 4
  117.      *
  118.      * @access  public
  119.      * @param string $type Ce qu'on recherche
  120.      * @param string $idtext L'id de l'objet
  121.      * @return  array
  122.      */
  123.     public function getItemsList($idobject$type)
  124.     {
  125.         // splitter idcontact;idperson
  126.         $ids explode(';'$idobject);
  127.         $idcontact $ids[0];
  128.         $idperson $ids[1];
  129.         // les personnes associées
  130.         if ($type == 'idperson') {
  131.             // on veut seulement une personne qui n'est pas encore inscrite
  132.             if ($idcontact == 'idperson') {
  133.                 $infos['idperson'] = $idperson;
  134.                 $infos['code'] = $idperson;
  135.                 $infos['view_name'] = $this->getEntityManager()->getRepository(Persons::class)->getObjectTitle($idperson);
  136.                 return array($idperson => $infos);
  137.                 // personne déjà inscrite (JOIN dans getLinkedObject)
  138.             } else {
  139.                 return parent::getItemsList($idperson$type);
  140.             }
  141.         }
  142.         // les contacts
  143.         if ($type == 'idcontact') {
  144.             return parent::getItemsList($idcontact$type);
  145.         }
  146.         // les rôles
  147.         if ($type == 'roles') {
  148.             return $this->getEntityManager()->getRepository(EcoleRole::class)->getRolesByPerson($idperson, array('idcontact' => $idcontact));
  149.         }
  150.     }
  151.     // }}}
  152.     //  {{{ getRelationsArray()
  153.     /** Décrit les relations de l'objet
  154.      *
  155.      * @access  public
  156.      * @param string|int $type si on souhaite retourner uniquement un type precis
  157.      * @see     Theatre::getLinkedObject()
  158.      */
  159.     public function getRelationsArray($type)
  160.     {
  161.         $array_types = array(
  162.             // jointure avec persons
  163.             'idperson' => array(// contacts associés
  164.                 'type_join' => 'simple',
  165.                 'dest_table' => 'persons',
  166.                 'fields_dest_table' => array('idperson'),
  167.                 'src_table_field' => 'idperson',
  168.                 'fields' => array('idperson''lastname''firstname''url_clean'),
  169.                 'description' => 'Personne associé',
  170.             ),
  171.             // jointure avec contacts
  172.             'idcontact' => array(// contacts associés
  173.                 'type_join' => 'simple',
  174.                 'dest_table' => 'contacts',
  175.                 'fields_dest_table' => array('idcontact'),
  176.                 'src_table_field' => 'idcontact',
  177.                 'fields' => array('idcontact''organisation''firstname''lastname''type'),
  178.                 'description' => 'Contact associé',
  179.             ),
  180.         );
  181.         // retourne uniquement un type précis
  182.         if ($type == 'all') {
  183.             return $array_types;
  184.         }
  185.         if (!empty($array_types[$type])) {
  186.             $array_types[$type]['type'] = $type;
  187.             return $array_types[$type];
  188.         } else {
  189.             return null;
  190.         }
  191.     }
  192.     // }}}
  193.     //  {{{ deleteEleve()
  194.     /** Supprime un élève d'une école
  195.      *
  196.      * @access  public
  197.      * @param int $idcontact le contact
  198.      * @param int $idperson la personne
  199.      * @return bool
  200.      */
  201.     public function deleteEleve($idcontact$idperson)
  202.     {
  203.         try {
  204.             $sql 'DELETE FROM ' $this->__table ' WHERE idcontact=' . (int)$idcontact ' AND idperson=' . (int)$idperson;
  205.             $res $this->exec($sql);
  206.             $sql 'DELETE FROM ecole_role WHERE idcontact=' . (int)$idcontact ' AND idperson=' . (int)$idperson;
  207.             $res $this->exec($sql);
  208.             $this->logDeletion('[Person_ecole::deleteEleve] Supprime un élève d\une école');
  209.             return true;
  210.         } catch (\Throwable $e) {
  211.             return false;
  212.         }
  213.     }
  214.     // }}}
  215.     //  {{{ getEcoles()
  216.     /** Récupèrer les écoles d'une personne
  217.      *
  218.      * @access   public
  219.      * param     int|array $idperson identifiant de la personne
  220.      * @return   array
  221.      */
  222.     public function getEcoles($idperson$exclude 3914)
  223.     {
  224.         $sql 'SELECT ' $this->__table '.*, contacts.`organisation`, contacts.`url_clean` 
  225.                 FROM ' $this->__table 
  226.                     JOIN contacts ON contacts.`idcontact`=' $this->__table '.`idcontact` 
  227.                 WHERE  ' $this->__table '.`idperson`';
  228.         // pour une personne ou plusieurs
  229.         if (is_array($idperson)) {
  230.             $sql .= ' IN (' join(','$idperson) . ') ';
  231.         } else {
  232.             $sql .= '=' . (int)$idperson;
  233.         }
  234.         // exclure le JTN
  235.         if ($exclude) {
  236.             $sql .= ' AND contacts.`idcontact`!=' . (int)$exclude;
  237.         }
  238.         $infos $this->queryAll($sql);
  239.         if (empty($infos)) {
  240.             return array();
  241.         } else {
  242.             // promo ==> date_end
  243.             foreach ($infos as $k => $v) {
  244.                 $infos[$k]['promo'] = substr($v['date_end'], 04);
  245.                 // si date_end > date du jour = promo courante
  246.                 $now = new DateTime(date('Y-m-d'));
  247.                 $expire = new DateTime($v['date_end']);
  248.                 if ($now $expire) {
  249.                     $infos[$k]['current'] = true;
  250.                 }
  251.             }
  252.             // groupement par persons
  253.             if (is_array($idperson)) {
  254.                 foreach ($infos as $k => $v) {
  255.                     $new_infos[$v['idperson']][$v['idcontact']] = $v;
  256.                 }
  257.                 return $new_infos;
  258.             } else { // classement par idcontact
  259.                 return $infos;
  260.             }
  261.         }
  262.     }
  263.     // }}}
  264.     //  {{{ getPersons()
  265.     /** Récupèrer les personnes d'une école
  266.      *
  267.      * @access   public
  268.      * param     int|array $idcontact identifiant de l'école
  269.      * @return   array
  270.      */
  271.     public function getPersons($idcontact null$params = array())
  272.     {
  273.         $sql 'SELECT ' $this->__table '.*, contacts.`organisation`, contacts.`url_clean` 
  274.                 FROM ' $this->__table 
  275.                     JOIN contacts ON contacts.`idcontact`=' $this->__table '.`idcontact` 
  276.                 WHERE  ' $this->__table '.`idcontact`';
  277.         // pour une personne ou plusieurs
  278.         if (!empty($idcontact)) {
  279.             if (is_array($idcontact)) {
  280.                 $sql .= ' IN (' join(','$idcontact) . ') ';
  281.             } else {
  282.                 $sql .= '=' . (int)$idcontact;
  283.             }
  284.         }
  285.         $infos $this->queryAll($sql);
  286.         if (empty($infos)) {
  287.             return array();
  288.         } else {
  289.             // uniquement les ids
  290.             if (array_key_exists('get_ids'$params)) {
  291.                 foreach ($infos as $k => $v) {
  292.                     $ids[$v['idperson']] = $v['idperson'];
  293.                 }
  294.                 return $ids;
  295.             }
  296.             // promo ==> date_end
  297.             foreach ($infos as $k => $v) {
  298.                 // formater les dates
  299.                 foreach (array('start''end') as $f) {
  300.                     $day substr($infos[$k]['date_' $f], 82);
  301.                     $month substr($infos[$k]['date_' $f], 52);
  302.                     $year substr($infos[$k]['date_' $f], 04);
  303.                     $infos[$k]['date_' $f '_day'] = $day;
  304.                     $infos[$k]['date_' $f '_month'] = $month;
  305.                     $infos[$k]['date_' $f '_year'] = $year;
  306.                     $infos[$k]['date_' $f '_str'] = $day '/' $month '/' $year;
  307.                 }
  308.                 $infos[$k]['promo'] = substr($v['date_end'], 04);
  309.             }
  310.             // groupement par persons
  311.             if (!empty($idcontact)) {
  312.                 if (is_array($idcontact)) {
  313.                     foreach ($infos as $k => $v) {
  314.                         $new_infos[$v['idcontact']][$v['idperson']] = $v;
  315.                     }
  316.                     return $new_infos;
  317.                 } else { // classement par idcontact
  318.                     return $infos;
  319.                 }
  320.             } else {
  321.                 return $infos;
  322.             }
  323.         }
  324.     }
  325.     // }}}
  326.     //  {{{ getRoles()
  327.     /** Différent rôle des éléves
  328.      *
  329.      * @access   public
  330.      * param     int|array $idcontact identifiant de l'école
  331.      * @return   array
  332.      */
  333.     public function getRoles($idcontact)
  334.     {
  335.         $sql 'SELECT DISTINCT(`comments`)
  336.                 FROM ' $this->__table 
  337.                 WHERE  ' $this->__table '.`idcontact`';
  338.         // pour une personne ou plusieurs
  339.         if (is_array($idcontact)) {
  340.             $sql .= ' IN (' join(','$idcontact) . ') ';
  341.         } else {
  342.             $sql .= '=' . (int)$idcontact;
  343.         }
  344.         try {
  345.             $infos $this->queryCol($sql);
  346.             if (empty($infos)) {
  347.                 return [];
  348.             }
  349.             foreach ($infos as $i) {
  350.                 $new_infos[$i] = $i;
  351.             }
  352.             return $new_infos;
  353.         } catch (\Throwable $e) {
  354.             return [];
  355.         }
  356.     }
  357.     // }}}
  358.     //  {{{ getPromos()
  359.     /** Différentes promos d'une école
  360.      *
  361.      * @access   public
  362.      * param     int|array $idcontact identifiant de l'école
  363.      * @return   array
  364.      */
  365.     public function getPromos($idcontact null$last_only false)
  366.     {
  367.         $sql 'SELECT YEAR(date_end) as year_end 
  368.                 FROM person_ecole pe 
  369.                 WHERE 1 ';
  370.         if (!empty($idcontact)) {
  371.             $sql .= ' AND pe.`idcontact`=' . (int)$idcontact;
  372.         }
  373.         if ($last_only) {
  374.             $sql .= ' AND YEAR(pe.`date_end`)<=\'' date('Y') . '\' ';
  375.         }
  376.         $sql .= '
  377.                 GROUP BY year_end 
  378.                 ORDER BY year_end DESC';
  379.         $infos $this->queryCol($sql);
  380.         if (empty($infos)) {
  381.             return array();
  382.         } else {
  383.             // si on est avant le 31/07@-, exclure l'année courante
  384.             $now = new DateTime(date('Y-m-d'));
  385.             $expire = new DateTime(date('Y') . '-07-01');
  386.             foreach ($infos as $i) {
  387.                 if ($now $expire && $i == date('Y')) {
  388.                     continue;
  389.                 }
  390.                 $new_infos[$i] = $i;
  391.             }
  392.             return $new_infos;
  393.         }
  394.     }
  395.     // }}}
  396.     //  {{{ getLastPromo()
  397.     /** Dernière promo
  398.      *
  399.      * @access   public
  400.      * param     array $promos liste des promos
  401.      * @return   int
  402.      */
  403.     public function getLastPromo($promos)
  404.     {
  405.         // si on est avant le 31/07@-, exclure l'année courante
  406.         $now = new DateTime(date('Y-m-d'));
  407.         $expire = new DateTime(date('Y') . '-07-01');
  408.         foreach ($promos as $p) {
  409.             if ($now $expire && $p == date('Y')) {
  410.                 continue;
  411.             }
  412.             return $p;
  413.         }
  414.     }
  415.     // }}}
  416.     //  {{{ countSpectacles()
  417.     /** Compter les spectacles d'une école
  418.      *
  419.      * @access   public
  420.      * param     int $idcontact le contact
  421.      * @return   int
  422.      */
  423.     public function countSpectacles($idcontact)
  424.     {
  425.         // nb de spectacle avec la classification JTN
  426.         if ($idcontact == 3914) {
  427.             $idclassification_specifique 103;
  428.             $sql_nb_spectacles '
  429.                 SELECT COUNT(DISTINCT(spectacles.`idspectacle`)) as total
  430.                 FROM spectacles
  431.                     INNER JOIN  spectacle_classification ON spectacle_classification.`idspectacle`=spectacles.`idspectacle`
  432.                 WHERE  spectacle_classification.`idclassification`=' $idclassification_specifique '
  433.                 AND spectacles.`publish`=1';
  434.         } else {
  435.             $sql_nb_spectacles '
  436.                 SELECT COUNT(DISTINCT(spectacles.`idspectacle`)) as total
  437.                 FROM spectacles
  438.                     INNER JOIN  spectacle_person ON spectacle_person.`idspectacle`=spectacles.`idspectacle`
  439.                     INNER JOIN person_ecole ON person_ecole.`idperson`=spectacle_person.`idperson`
  440.                 WHERE  person_ecole.`idcontact`=' $idcontact '
  441.                 AND spectacles.`publish`=1';
  442.         }
  443.         return $this->queryOne($sql_nb_spectacles);
  444.     }
  445.     // }}}
  446.     //  {{{ countDates()
  447.     /** Compter les dates de représentation pour une école
  448.      *
  449.      * @access   public
  450.      * param     int $idcontact le contact
  451.      * @return   int
  452.      */
  453.     public function countDates($idcontact)
  454.     {
  455.         // nombre de représentations avec la classification JTN
  456.         if ($idcontact == 3914) {
  457.             $idclassification_specifique 103;
  458.             $sql_nb_dates 'SELECT COUNT(DISTINCT(schedules.`date`)) as total
  459.                     FROM spectacles
  460.                         INNER JOIN schedules ON schedules.`idspectacle`=spectacles.`idspectacle`
  461.                         INNER JOIN  spectacle_classification ON spectacle_classification.`idspectacle`=spectacles.`idspectacle`
  462.                     WHERE  spectacle_classification.`idclassification`=' $idclassification_specifique ' AND schedules.`date`>\'1999-12-31\'
  463.                     AND spectacles.`publish`=1';
  464.         } else {
  465.             $sql_nb_dates 'SELECT COUNT(DISTINCT(schedules.`date`)) as total
  466.                     FROM spectacles
  467.                         INNER JOIN schedules ON schedules.`idspectacle`=spectacles.`idspectacle`
  468.                         INNER JOIN  spectacle_person ON spectacle_person.`idspectacle`=spectacles.`idspectacle`
  469.                             INNER JOIN person_ecole ON person_ecole.`idperson`=spectacle_person.`idperson`
  470.                     WHERE  person_ecole.`idcontact`=' $idcontact ' AND schedules.`date`>\'1999-12-31\'
  471.                     AND spectacles.`publish`=1';
  472.         }
  473.         return $this->queryOne($sql_nb_dates);
  474.     }
  475.     // }}}
  476.     //  {{{ getIdsSpectacles()
  477.     /** les spectacles pour une saison
  478.      *
  479.      * @access   public
  480.      * param     int $idcontact le contact
  481.      * @return   int
  482.      */
  483.     public function getIdsSpectacles($idcontact$params = array(), $list_idpersons = array())
  484.     {
  485.         if ($idcontact == 3914) {
  486.             $idclassification_specifique 103;
  487.             $sql_spectacles '
  488.                 SELECT DISTINCT(s.`idspectacle`) 
  489.                 FROM  spectacles s 
  490.                     JOIN text_spectacle ts ON ts.idspectacle=s.idspectacle
  491.                     JOIN schedules sc ON sc.idspectacle=s.idspectacle
  492.                     JOIN spectacle_classification scl ON scl.idspectacle=s.idspectacle
  493.                 WHERE 1
  494.                 AND s.`publish`=1 AND scl.`idclassification` = ' $idclassification_specifique ' ';
  495.         } else {
  496.             // nb de spectacle pour la saison en cours pour les élèves
  497.             $sql_spectacles '
  498.                 SELECT DISTINCT(s.`idspectacle`) 
  499.                 FROM  spectacles s
  500.                     JOIN spectacle_person sp ON sp.idspectacle=s.idspectacle
  501.                     JOIN schedules sc ON sc.idspectacle=s.idspectacle
  502.                 WHERE 1
  503.                 AND s.`publish`=1 AND sp.`idperson` IN (' join(','$list_idpersons) . ') ';
  504.         }
  505.         if (!empty($params['saison'])) {
  506.             $s explode('-'$params['saison']);
  507.             $sql_spectacles .= ' AND sc.`date` BETWEEN \'' $s[0] . '-09-01\' AND \'' $s[1] . '-08-31\' ';
  508.         }
  509.         return $this->queryCol($sql_spectacles);
  510.     }
  511.     // }}}
  512. }