vendor/theatre/core/src/Repository/SpectacleAideRepository.php line 358

Open in your IDE?
  1. <?php
  2. namespace TheatreCore\Repository;
  3. use TheatreCore\Entity\Aides;
  4. use TheatreCore\Entity\SpectacleAide;
  5. use TheatreCore\Entity\Spectacles;
  6. use TheatreCore\Traits\TheatreTrait;
  7. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. /**
  10.  * SpectacleAideRepository
  11.  *
  12.  * This class was generated by the Doctrine ORM. Add your own custom
  13.  * repository methods below.
  14.  */
  15. class SpectacleAideRepository 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($registrySpectacleAide::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.     // get list of nb of existing recommandations for all spectacle
  32.     public function getIndiceNombresRecommandations(): array
  33.     {
  34.         $query "SELECT totalCount.totalRecommandation
  35.             FROM
  36.                 (SELECT 
  37.                     COUNT(DISTINCT spectacle_aide.idspectacle) AS totalSpectacle,
  38.                     COUNT(DISTINCT spectacle_aide.idaide) AS totalRecommandation
  39.                     FROM 
  40.                         `aides`,
  41.                         `spectacle_aide`,
  42.                         `aidecontacts`
  43.                     WHERE
  44.                         aides.`publish` = 1
  45.                         AND aides.`idaide` = spectacle_aide.`idaide`
  46.                         AND aidecontacts.`title` LIKE 'Spectacle recommandé'
  47.                     GROUP BY spectacle_aide.idspectacle
  48.                     ORDER BY totalRecommandation DESC
  49.                 ) AS totalCount
  50.             GROUP BY totalCount.totalRecommandation
  51.             ORDER BY totalCount.totalRecommandation DESC";
  52.         $data $this->queryAll($query);
  53.         if (!empty($data)) {
  54.             $return $data;
  55.         } else {
  56.             $return = [];
  57.         }
  58.         return $return;
  59.     }
  60.     //  {{{ countAides()
  61.     /** Compter le nombre d'aide
  62.      *
  63.      * @access   public
  64.      * param     int idaide identifiant de l'aide
  65.      * @return   int
  66.      */
  67.     public function countAides($idaide null$idspectacle null$params = array())
  68.     {
  69.         if (!empty($params['count_spectacles'])) {
  70.             $sql 'SELECT COUNT(DISTINCT(idspectacle))
  71.                     FROM ' $this->__table '
  72.                     WHERE ';
  73.         } else {
  74.             $sql 'SELECT COUNT(*)
  75.                     FROM ' $this->__table '
  76.                     WHERE ';
  77.         }
  78.         if (!empty($idaide)) {
  79.             $sql_where[] = 'idaide IN (' $this->arrayToIn($idaide) . ') ';
  80.         }
  81.         if (!empty($idspectacle)) {
  82.             $sql_where[] = 'idspectacle IN (' $this->arrayToIn($idspectacle) . ') ';
  83.         }
  84.         $sql $sql join(' AND '$sql_where);
  85.         $nb_aides $this->queryOne($sql);
  86.         return $nb_aides;
  87.     }
  88.     // }}}
  89.     //  {{{ getInfosSpectacle_aide()
  90.     /** R&écupèrer les infos
  91.      *
  92.      * @access   public
  93.      * param     int idaide identifiant de l'aide
  94.      * param     int idspectacle identifiant du spectaclee
  95.      * @return   array
  96.      */
  97.     public function getInfosSpectacle_aide($idspectacleaide)
  98.     {
  99.         try {
  100.             $sql 'SELECT * FROM ' $this->__table '
  101.                 WHERE idspectacleaide=' . (int)$idspectacleaide;
  102.             $infos $this->queryRow($sql);
  103.             if (empty($infos)) {
  104.                 return [];
  105.             }
  106.             $infos['id'] = $this->idspectacleaide;
  107.             if ($infos['date_start'] == '0000-00-00 00:00:00') {
  108.                 $infos['date_start'] = NULL;
  109.             }
  110.             if ($infos['date_end'] == '0000-00-00 00:00:00') {
  111.                 $infos['date_end'] = NULL;
  112.             }
  113.             foreach (array('start''end') as $i) {
  114.                 if (!empty($infos['date_' $i])) {
  115.                     $infos['date_' $i '_year'] = substr($infos['date_' $i], 04);
  116.                     $infos['date_' $i '_month'] = substr($infos['date_' $i], 52);
  117.                     $infos['date_' $i '_day'] = substr($infos['date_' $i], 82);
  118.                 }
  119.             }
  120.             return $infos;
  121.         } catch (\Throwable $e) {
  122.             return [];
  123.         }
  124.     }
  125.     // }}}
  126.     //  {{{ getRelationsArray()
  127.     /** Décrit les relations de l'objet
  128.      *
  129.      * @access  public
  130.      * @param string|int $type si on souhaite retourner uniquement un type precis
  131.      * @see     Theatre::getLinkedObject()
  132.      */
  133.     public function getRelationsArray($type)
  134.     {
  135.         $array_types = array(
  136.             // jointure avec spectacles
  137.             'idspectacle' => array(// spectacles associés
  138.                 'type_join' => 'simple',
  139.                 'dest_table' => 'spectacles',
  140.                 'join_field_src' => 'idspectacle',
  141.                 'fields' => array('idspectacle''title''valid''url_clean'),
  142.                 'silent_delete_relation' => true,
  143.                 'description' => 'Texte associé',
  144.             ),
  145.             // jointure avec aides
  146.             'idaide' => array(// aides associés
  147.                 'type_join' => 'simple',
  148.                 'dest_table' => 'aides',
  149.                 'join_field_src' => 'idaide',
  150.                 'fields' => array('idaide''aide''idcontact'),
  151.                 'silent_delete_relation' => true,
  152.                 'description' => 'Aide associé',
  153.             ),
  154.         );
  155.         // retourne uniquement un type précis
  156.         if ($type == 'all') {
  157.             return $array_types;
  158.         }
  159.         if (!empty($array_types[$type])) {
  160.             $array_types[$type]['type'] = $type;
  161.             return $array_types[$type];
  162.         } else {
  163.             return null;
  164.         }
  165.     }
  166.     // }}}
  167.     //  {{{ deleteAide()
  168.     /** Supprime une aide d'un spectacle
  169.      *
  170.      * @access  public
  171.      * @param int $idaide l'aide
  172.      * @param int $idspectacle le spectacle
  173.      * @return bool
  174.      */
  175.     public function deleteAide($idaide$idspectacle)
  176.     {
  177.         try {
  178.             $sql 'DELETE FROM ' $this->__table '
  179.                 WHERE idaide=' . (int)$idaide '
  180.                 AND idspectacle=' . (int)$idspectacle;
  181.             // excute
  182.             $res $this->exec($sql);
  183.             return true;
  184.         } catch (\Throwable $e) {
  185.             return false;
  186.         }
  187.     }
  188.     // }}}
  189.     //  {{{ getAides()
  190.     /** Récupèrer les aides d'un spectacle
  191.      *
  192.      * @access   public
  193.      * @param array $aueru Les paramètres pour la requete
  194.      * @param bool $debug afficher la requête
  195.      * @return   string
  196.      */
  197.     public function getAides($idspectacle)
  198.     {
  199.         return $this->getEntityManager()->getRepository(Aides::class)->getListAides(array(
  200.             'special' => array(
  201.                 'idspectacles' => array($idspectacle)
  202.             ),
  203.             'dbg' => array('les_aides''Liste des aides du spectacle ' $idspectacle)
  204.         ));
  205.     }
  206.     // }}}
  207.     //  {{{ getListSpectacleAide()
  208.     /** Spectacles avec une aide
  209.      *
  210.      * @access   public
  211.      * @param array $params_sql paramètre pour la requete SQL
  212.      * @return   array
  213.      */
  214.     public function getListSpectacleAide($params_sql null$params_display = array())
  215.     {
  216.         // si pas de tableau
  217.         if (!isset($params_sql['special'])) {
  218.             $params_sql['special'] = array();
  219.         }
  220.         if (!isset($params_sql['params'])) {
  221.             $params_sql['params'] = array();
  222.         }
  223.         // construction de la requête avec le passage de paramètres
  224.         $sql $this->getSQLSpectacleAide($params_sql);
  225.         // paramètre de debug
  226.         $dbg = (empty($params_sql['dbg'])) ? array() : $dbg $params_sql['dbg'];
  227.         // executer la requete
  228.         $this->query($sql$dbg);
  229.         // debug
  230.         $start_boucle microtime(true);
  231.         $spectacle_aide = array();
  232.         $conn $this->getEntityManager();
  233.         // on boucle
  234.         while ($this->fetch()) {
  235.             if (!empty($params_sql['idspectacle'])) {
  236.                 $id $this->idaide;
  237.             } else {
  238.                 $id $this->idspectacle;
  239.             }
  240.             if (!empty($params_sql['get_spectacles_with_aides'])) {
  241.                 $id $this->idspectacleaide;
  242.             }
  243.             $spectacle_aide[$id] = array(
  244.                 'idspectacleaide' => $this->idspectacleaide,
  245.                 'id' => $this->idspectacleaide,
  246.                 'idspectacle' => $this->idspectacle,
  247.                 'object' => 'spectacle_aide',
  248.                 'publish' => $this->spectacle_aide_publish,
  249.                 'valid' => $this->valid,
  250.                 'idaide' => $this->idaide,
  251.                 'date_start' => $this->date_start,
  252.                 'date_end' => $this->date_end,
  253.                 'comments' => $this->comments,
  254.                 'url' => $this->url,
  255.                 'year_start' => substr($this->date_start04),
  256.             );
  257.             if (!empty($params_sql['recommandations_only'])) {
  258.                 $spectacle_aide[$id]["aide"] = [];
  259.             } else {
  260.                 $spectacle_aide[$id]["aide"] = $conn->getRepository(Aides::class)->getInfosAide($this->idaide, array(
  261.                     'get_aidecontact' => true,
  262.                     'get_contact' => true,
  263.                 ));
  264.             }
  265.             if ($this->date_end != '' && $this->date_end != '0000-00-00 00:00:00') {
  266.                 $spectacle_aide[$id]['year_end'] = substr($this->date_end04);
  267.             } else {
  268.                 $spectacle_aide[$id]['year_end'] = null;
  269.             }
  270.             $idspectacles[$this->idspectacle] = $this->idspectacle;
  271.         }
  272.         // debug de la boucle
  273.         if ($this->isTraceMode('list_object')) {
  274.             $this->setTraceInfos(array(
  275.                 'execution_time_boucle' => microtime(true) - $start_boucle,
  276.                 'nb_occurence' => count($spectacle_aide),
  277.                 'dbg' => $dbg,
  278.             ));
  279.         }
  280.         $this->free();
  281.         $spectacle_aide_group = array();
  282.         // récupèrer également les spectacles
  283.         if (!empty($idspectacles) && count($idspectacles) > 0) {
  284.             $params_sql_spectacles = array(
  285.                 'special' => array(
  286.                     'idspectacle' => $idspectacles,
  287.                 ),
  288.                 'params' => array(
  289.                     'limit' => 'all'
  290.                 ),
  291.                 'dbg' => array('list_spectacles_spectacle_aide''Récupération des spectacles d\'une aide'),
  292.             );
  293.             $spectacles $conn->getRepository(Spectacles::class)->getListSpectacles($params_sql_spectaclesnullfalse, ['affiche_date_creation' => true'carrer_couleurs' => true'allauthors' => true]);
  294.             foreach ($spectacle_aide as $k => $v) {
  295.                 if (!empty($spectacles[$v['idspectacle']])) {
  296.                     $spectacles[$v['idspectacle']]['display']['affiche_date_creation'] = true;
  297.                     $spectacles[$v['idspectacle']]['annee_creation'] = $conn->getRepository(Spectacles::class)->getFormeDate($spectacles[$v['idspectacle']]['creation_date'], '%Y'true); //%a %d/%m/%Y
  298.                     if (!empty($params_sql['group_by_aide_type_aide_spectacle'])) {
  299.                         $key $v['idaide'] . '_' $v['aide']['type_aide'] . '_' $v['idspectacle'];
  300.                         if (empty($spectacle_aide_group[$key])) {
  301.                             $spectacle_aide_group[$key] = $v;
  302.                         }
  303.                         $spectacle_aide_group[$key]['spectacles'][$v['idspectacle']] = $spectacles[$v['idspectacle']];
  304.                     } else {
  305.                         $spectacle_aide[$k]['spectacles'][$v['idspectacle']] = $spectacles[$v['idspectacle']];
  306.                     }
  307.                 }
  308.             }
  309.         }
  310.         if (!empty($params_sql['group_by_aide_type_aide_spectacle'])) {
  311.             return $spectacle_aide_group;
  312.         } else {
  313.             return $spectacle_aide;
  314.         }
  315.     }
  316.     // }}}
  317.     //  {{{ countListSpectacleAide()
  318.     /** Compter la liste des aides
  319.      *
  320.      * @access   public
  321.      * @param array $params_sql paramètre pour la requete SQL
  322.      * @param bool $debug mode débugage
  323.      * @return   int
  324.      */
  325.     public function countListSpectacleAide($params_sql null$debug false)
  326.     {
  327.         // si pas de limite => tout
  328.         if (empty($params_sql['limit'])) {
  329.             $params_sql['limit'] = array('all');
  330.         }
  331.         $params_sql['count_only'] = true;
  332.         // construction de la requête avec le passage de paramètres
  333.         return $this->queryOne($this->getSQLSpectacleAide($params_sql$debug));
  334.     }
  335.     // }}}
  336.     //  {{{ getSQLSpectacleAide()
  337.     /** Générer la requete standard pour des aides
  338.      *
  339.      * @access   public
  340.      * @param array $aueru Les paramètres pour la requete
  341.      * @param bool $debug afficher la requête
  342.      * @return   string
  343.      */
  344.     public function getSQLSpectacleAide($query = array(), $debug false)
  345.     {
  346.         // on prepare les tableaux
  347.         foreach (array('select''from''where''order''group''limit') as $t) {
  348.             if (!empty($query[$t])) {
  349.                 if (!is_array($query[$t])) {
  350.                     $query[$t][] = $query[$t];
  351.                 } else {
  352.                     $query[$t] = $query[$t];
  353.                 }
  354.             } else {
  355.                 $query[$t] = array();
  356.             }
  357.         }
  358.         $this->addSQLParams('distinct'$query'no_primary');
  359.         $this->addSQLParams('group'$query'no');
  360.         // champs par défaut
  361.         $this->addSQLParams('select'$query, array(
  362.             array('spectacle_aide' => '*'),
  363.             array('aides' => '*')
  364.         ));
  365.         $this->addSQLParams('select_more'$query',`spectacle_aide`.`publish` as spectacle_aide_publish');
  366.         $this->addSQLParams('from'$query, array(
  367.             'aides',
  368.         ));
  369.         $this->addSQLParams('join'$query, array(
  370.             'aides.`idaide`' => 'spectacle_aide.`idaide`',
  371.         ));
  372.         // publié ou pas
  373.         if (empty($query['publish']) && $_ENV['CHECK_IF_ONLINE']) {
  374.             $this->addSQLParams('where'$query, array(
  375.                 array('aides''publish'1)
  376.             ));
  377.         } elseif (!empty($query['publish'])) {
  378.             $this->addSQLParams('where'$query, array(
  379.                 array('aides''publish'$query['publish'])
  380.             ));
  381.         }
  382.         // recherche par contact
  383.         if (!empty($query['special']['aides'])) {
  384.             if (!empty($query['special']['aides']['idcontacts'])) {
  385.                 $this->addSQLParams('where'$query, array(
  386.                     array('aides''idcontact'$query['special']['aides']['idcontacts']),
  387.                 ));
  388.             }
  389.             if (!empty($query['special']['aides']['type_aides'])) {
  390.                 $this->addSQLParams('where'$query, array(
  391.                     array('aides''type_aide'$query['special']['aides']['type_aides']),
  392.                 ));
  393.             }
  394.             if (!empty($query['special']['aides']['idaidecontact'])) {
  395.                 $this->addSQLParams('where'$query, array(
  396.                     array('aides''idaidecontact'$query['special']['aides']['idaidecontact']),
  397.                 ));
  398.             }
  399.             if (!empty($query['special']['aides']['idaide'])) {
  400.                 $this->addSQLParams('where'$query, array(
  401.                     array('aides''idaide'$query['special']['aides']['idaide']),
  402.                 ));
  403.             }
  404.         }
  405.         // pour un spectacle
  406.         if (!empty($query['idspectacle'])) {
  407.             $this->addSQLParams('where'$query, array(
  408.                 array('spectacle_aide''idspectacle'$query['idspectacle'])
  409.             ));
  410.         }
  411.         // recherche par année
  412.         if (!empty($query['special']['period'])) {
  413.             if (!empty($query['special']['period']['year'])) {
  414.                 $this->addSQLParams('where_more'$query' AND YEAR(spectacle_aide.`date_start`)=' $query['special']['period']['year']);
  415.             }
  416.         }
  417.         $this->addSQLParams('order'$query, array(
  418.             array('spectacle_aide' => 'date_start'),
  419.         ));
  420.         return $this->getSQLObject($query);
  421.     }
  422.     // }}}
  423.     //  {{{ getYears()
  424.     /** trouver les années où il y a eu des prix
  425.      *
  426.      * @access   public
  427.      * @param array $auery Les paramètres pour la requete
  428.      * @param bool $debug afficher la requête
  429.      * @return   array
  430.      */
  431.     public function getYears($params = array(), $debug false)
  432.     {
  433.         $query 'SELECT DISTINCT YEAR(spectacle_aide.`date_start`), YEAR(spectacle_aide.`date_start`) as annee
  434.                   FROM spectacle_aide
  435.                   JOIN aides ON spectacle_aide.`idaide`=aides.`idaide`
  436.                   WHERE 1';
  437.         // type prix ou nominations
  438.         if (!empty($params['special']['aides']['type_aides'])) {
  439.             $query .= ' AND aides.`type_aide` IN (' $this->arrayToIn($params['special']['aides']['type_aides']) . ') ';
  440.         }
  441.         // contact spécifique
  442.         if (!empty($params['special']['aides']['idcontacts'])) {
  443.             $query .= ' AND aides.`idcontact` IN (' $this->arrayToIn($params['special']['aides']['idcontacts']) . ') ';
  444.         }
  445.         $query .= ' HAVING annee <> 0 
  446.                     ORDER BY annee DESC';
  447.         $years $this->getEntityManager()->getConnection()->fetchAllKeyValue($query);
  448.         return !empty($years) ? $years : [];
  449.     }
  450.     // }}}
  451. }