vendor/theatre/core/src/Repository/BigregionsRepository.php line 216

Open in your IDE?
  1. <?php
  2. namespace TheatreCore\Repository;
  3. use TheatreCore\Entity\Bigregions;
  4. use TheatreCore\Traits\TheatreTrait;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8.  * BigregionsRepository
  9.  *
  10.  * This class was generated by the Doctrine ORM. Add your own custom
  11.  * repository methods below.
  12.  */
  13. class BigregionsRepository extends ServiceEntityRepository
  14. {
  15.     use TheatreTrait;
  16.     private $em;
  17.     public $__table;
  18.     public $__table_object;
  19.     public $__idtable;
  20.     private $returnAnd false;
  21.     public function __construct(ManagerRegistry $registry)
  22.     {
  23.         parent::__construct($registryBigregions::class);
  24.         $this->__table $this->getClassMetadata()->getTableName();
  25.         $this->__table_object $this->getClassMetadata()->name;
  26.         $this->__idtable $this->getClassMetadata()->getSingleIdentifierFieldName();
  27. //        $this->__table = 'persons';
  28. //        $this->__table_object = '\\App\\Entity\\Persons';
  29.     }
  30.     // cache pour la liste obtenue dans getBigregionsList()
  31.     static protected $bigregionslist null;
  32.     // {{{ getBigregionsList()
  33.     /** Retourne une liste des grandes régions et de leurs départements
  34.      *
  35.      *
  36.      * @param mixed $select_def intitulé optionnel pour la tête de liste (select boxes)
  37.      * @access protected
  38.      * @return array  tableau au format array(array('id'=>idbigregioninsee, 'nom'=>bigregion, 'codes'=>'67,68', 'codes_array'=>array(67,68)),...)
  39.      */
  40.     protected function getBigregionsList($select_def null)
  41.     {
  42.         if (is_null(self::$bigregionslist)) { // en cache ?
  43.             // on fait la requête
  44.             $sql 'SELECT *,`bigregions`.idbigregion as idbigregion
  45.                     FROM `bigregions`
  46.                         JOIN `regions` ON `regions`.`idbigregion`=`bigregions`.`idbigregion`
  47.                         JOIN `departements` ON `regions`.`idregion`=`departements`.`idregion`
  48.                     ORDER BY `bigregions`.`bigregion` ASC, `departements`.`departement` ASC';
  49.             $res $this->queryAll($sql);
  50.             // réorganisation des données
  51.             $bigregions = array();
  52.             foreach ($res as $val) {
  53.                 if (!isset($bigregions[$val['idbigregion']])) {
  54.                     $bigregions[$val['idbigregion']] = array(
  55.                         'id' => $val['idbigregion'],
  56.                         'nom' => $val['bigregion'],
  57.                         'codes_array' => array(
  58.                             $val['departement']
  59.                         )
  60.                     );
  61.                 } else {
  62.                     $bigregions[$val['idbigregion']]['codes_array'][] = $val['departement'];
  63.                 }
  64.             }
  65.             // liste des départements pour chaque région (séparés par des virgules)
  66.             foreach ($bigregions as $key => $val) {
  67.                 $bigregions[$key]['codes'] = implode(','$bigregions[$key]['codes_array']);
  68.             }
  69.             self::$bigregionslist $bigregions;
  70.         }
  71.         // on ajoute la mention pour la boite de sélection (si demandé)
  72.         if (!empty($select_def)) {
  73. //            $list[0] = array(
  74. //                'id' => 0,
  75. //                'nom' => $select_def,
  76. //                'codes' => '',
  77. //                'codes_array' => array()
  78. //            );
  79.             $list self::$bigregionslist;
  80.             return $list;
  81.         }
  82.         // on retourne la liste
  83.         return self::$bigregionslist;
  84.     }
  85.     // }}}
  86.     // {{{ getZipCode()
  87.     /** contruit une partie de requête pour la recherche de codes postaux associés à une région
  88.      *
  89.      *
  90.      * @param int $area code insee région
  91.      * @param string $table table (defaut: c)
  92.      * @param string $field champ (defaut: zip)
  93.      * @param mixed $inverse si non vide, inverse la requête: code postal n'étant pas dans la région
  94.      * @param string $countryfield champ pays (defaut: country)
  95.      * @param string $country code pays (defaut: FR)
  96.      * @access public
  97.      * @return string           la partie de requête
  98.      */
  99.     public function getZipCode($area 0$table 'contacts'$field 'zip'$inverse null$countryfield 'country'$country 'FR')
  100.     {
  101.         if (!$this->my_is_int($area) && strlen($area) == 2) {
  102.             return $this->getCountryCode($area$table$countryfield);
  103.         }
  104.         //tableau des bigregions
  105.         $bigregions $this->getBigregionsList();
  106.         if (!isset($bigregions[$area])) return false;
  107.         // trouver en fonction de l'id
  108.         $listezip $bigregions[$area]['codes'];
  109.         $nom $bigregions[$area]['nom'];
  110.         // listes des départements, par longueur de préfixe (2 en général, 3 pour les dom)
  111.         $listes = array();
  112.         foreach ($bigregions[$area]['codes_array'] as $dept) {
  113.             $len strlen($dept);
  114.             if (!isset($listes[$len])) {
  115.                 $listes[$len] = '';
  116.             } else {
  117.                 $listes[$len] .= ',';
  118.             }
  119.             $listes[$len] .= '\'' $dept '\'';
  120.         }
  121.         // construction clause pour chaque longueur de préfixe
  122.         $sql '';
  123.         foreach ($listes as $len => $val) {
  124.             if ($sql) {
  125.                 if (empty($inverse)) {
  126.                     $sql .= ' OR';
  127.                     $inverse '';
  128.                 } else {
  129.                     $sql .= ' AND'// requête inverse, donc ET logique
  130.                     $inverse 'NOT';
  131.                 }
  132.             }
  133.             $sql .= ' SUBSTRING(' $table '.' $field ',1,' $len ') ' $inverse ' IN (' $val ')';
  134.         }
  135.         $sql ' ' $table '.' $countryfield '=\'' $country '\' AND (' $sql ')';
  136.         //retourner la requete
  137.         return $sql;
  138.     }
  139.     // }}}
  140.     // {{{ getBigbigregionName()
  141.     /** Retourne le nom de la région ayant pour code insee $area
  142.      *
  143.      *
  144.      * @param int $area code région insee
  145.      * @access public
  146.      * @return string|boolean  le nom de la région, ou false si non trouvé
  147.      */
  148.     public function getBigbigregionName($area)
  149.     {
  150.         if (!$this->my_is_int($area)) {
  151.             return $this->getCountries($area);
  152.         }
  153.         //tableau des bigregions
  154.         $bigregions $this->getBigregionsList();
  155.         // trouver en fonction de l'id
  156.         if (isset($bigregions[$area])) {
  157.             // retourner le nom
  158.             return $bigregions[$area]['nom'];
  159.         }
  160.         // pas trouvé
  161.         return false;
  162.     }
  163.     // }}}
  164.     // {{{ getRegionName()
  165.     /** Retourne le nom de la région ayant pour code insee $area
  166.      *
  167.      *
  168.      * @param int $area code région insee
  169.      * @access public
  170.      * @return string|boolean  le nom de la région, ou false si non trouvé
  171.      */
  172.     public function getRegionName($area)
  173.     {
  174.         return $this->getBigbigregionName($area);
  175.     }
  176.     // }}}
  177.     // {{{ getArea()
  178.     /** tableau des régions pour l'affichage (boîtes de sélection)
  179.      *
  180.      *
  181.      * @param mixed $select_def intitulé de la tête de liste
  182.      * @access public
  183.      * @return array tableau (format array(idbigregioninsee=>nom))
  184.      */
  185.     public function getArea($select_def true$rand false$exclude_dom true$add_country false)
  186.     {
  187.         //tableau des bigregions
  188.         $bigregions $this->getBigregionsList($select_def);
  189.         //tableau de sortie
  190.         $area = array();
  191.         foreach ($bigregions as $key => $value) {
  192.             if ($exclude_dom) {
  193.                 if (in_array($value['id'], array(1415161718 /* code région insee */))) continue; // on exclut les dom-tom
  194.             }
  195.             $area[$value['id']] = $value['nom'];
  196.         }
  197.         // ajouter suisse et belgique
  198.         if ($add_country) {
  199.             $area['BE'] = 'Belgique';
  200.             $area['CH'] = 'Suisse';
  201.             $area['FR'] = 'Luxembourg';
  202.         }
  203.         if (!empty($rand)) {
  204.             return array_rand($area);
  205.         }
  206.         //retourner le tableau
  207.         return $area;
  208.     }
  209.     // }}}
  210.     // {{{ getIdbigregionByZip()
  211.     /** Trouver l'idbigregion à partir du code postal
  212.      *
  213.      *
  214.      * @param int $zip le code postal
  215.      * @access public
  216.      * @return int Identifiant de la région
  217.      */
  218.     public function getIdbigregionByZip($zip): ?int
  219.     {
  220.         $doms = [
  221.             974 => 18,
  222.             973 => 15,
  223.             972 => 16,
  224.             971 => 14
  225.         ];
  226.         // si le code postal n'est pas null
  227.         if ($zip !== null) {
  228.             //on vérifie si Dom-Tom
  229.             $zip_dom substr($zip03);
  230.             // si code Dom-Tom, on renvoie l'id big region directement
  231.             if (!empty($doms[$zip_dom])) {
  232.                 $idBigRegion $doms[$zip_dom];
  233.             } else {
  234.                 // sinon on récupère le département pour chercher l'id big region en base
  235.                 $department substr($zip02);
  236.                 $tableId '`bigregions`.`idbigregion`';
  237.                 $sql 'SELECT ' $tableId '
  238.                 FROM `departements`
  239.                     JOIN `regions` ON `regions`.`idregion`=`departements`.`idregion`
  240.                     JOIN `bigregions` ON `bigregions`.`idbigregion`=`regions`.`idbigregion`
  241.                 WHERE `departements`.`departement`=' . (int)$department ' ';
  242.                 $idBigRegion $this->queryOne($sql);
  243.             }
  244.         } else {
  245.             // si le code postal est null, l'id big region est null
  246.             $idBigRegion null;
  247.         }
  248.         // on retourne l'id de la big region
  249.         return $idBigRegion;
  250.     }
  251.     // }}}
  252.     // {{{ getIdDepartementByZip()
  253.     /** Trouver l'id du departement à partir du code postal
  254.      *
  255.      *
  256.      * @param int $zip le code postal
  257.      * @access public
  258.      * @return int Identifiant de la région
  259.      */
  260.     public function getIdDepartementByZip($zip)
  261.     {
  262.         return $this->getIdbigregionByZip($ziptrue);
  263.     }
  264.     // }}}
  265.     // {{{ getCountryCode()
  266.     /** contruit une partie de requête pour la recherche de pays
  267.      *
  268.      *
  269.      * @param int $area code insee région
  270.      * @param string $table table (defaut: c)
  271.      * @param string $field champ (defaut: zip)
  272.      * @param mixed $inverse si non vide, inverse la requête: code postal n'étant pas dans la région
  273.      * @param string $countryfield champ pays (defaut: country)
  274.      * @param string $country code pays (defaut: FR)
  275.      * @access public
  276.      * @return string           la partie de requête
  277.      */
  278.     public function getCountryCode($country 'FR'$table 'contacts'$countryfield 'country')
  279.     {
  280.         $sql ' ' $table '.' $countryfield '=\'' $this->escape($country) . '\' ';
  281.         return $sql;
  282.     }
  283.     // }}}
  284.     // {{{ getXmlClassement()
  285.     /** Comment classer dans le XML du SWF
  286.      *
  287.      * @access public
  288.      * @return array
  289.      */
  290.     public function getXMLClassement()
  291.     {
  292.         foreach (self::$bigregionslist as $k => $v) {
  293.             $bigregions[$k] = $k;
  294.         }
  295.         return $bigregions;
  296.     }
  297.     // }}}
  298.     // {{{ getCountries()
  299.     /** Les pays
  300.      *
  301.      * @access public
  302.      * @return array
  303.      */
  304.     public function getCountries($area null)
  305.     {
  306.         return array(
  307.             'FR' => array(
  308.                 'pays' => 'France',
  309.                 'prefixe' => 'fr'
  310.             ),
  311.             'BE' => array(
  312.                 'pays' => 'Belgique',
  313.                 'prefixe' => 'be'
  314.             ),
  315.             'LU' => array(
  316.                 'pays' => 'Luxembourg',
  317.                 'prefixe' => 'lu'
  318.             ),
  319.             'CH' => array(
  320.                 'pays' => 'Suisse',
  321.                 'prefixe' => 'ch'
  322.             ),
  323.         );
  324.     }
  325.     // }}}
  326.     // {{{ getRegionsCountries()
  327.     /** Retourne une liste des grandes régions et de leurs départements
  328.      *
  329.      *
  330.      * @access public
  331.      * @return array  tableau
  332.      */
  333.     public function getRegionsCountries()
  334.     {
  335.         // on fait la requête
  336.         $sql 'SELECT *,`bigregions`.idbigregion as idbigregion
  337.                 FROM `bigregions`
  338.                     JOIN `regions` ON `regions`.`idbigregion`=`bigregions`.`idbigregion`
  339.                     JOIN `departements` ON `regions`.`idregion`=`departements`.`idregion`
  340.                 ORDER BY `bigregions`.`bigregion` ASC, `departements`.`departement` ASC';
  341.         $res $this->queryAll($sql);
  342.         foreach ($res as $k => $v) {
  343.             $results['@' $v['idbigregion']] = array(
  344.                 'name' => $v['bigregion'],
  345.                 'code' => $v['idbigregion'],
  346.                 'type' => 'region',
  347.             );
  348.         }
  349.         foreach ($this->getCountries() as $k => $v) {
  350.             $results['@' $k] = array(
  351.                 'name' => $v['pays'],
  352.                 'code' => $k,
  353.                 'type' => 'country',
  354.             );
  355.         }
  356.         // les pays
  357.         return $results;
  358.     }
  359.     // }}}
  360. }