1: <?php
2: namespace TIC\TownBundle\Repository;
3:
4: use TIC\TownBundle\Entity\Commune as RepoEntity;
5: use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
6: use Doctrine\Persistence\ManagerRegistry;
7:
8: /**
9: * CommuneRepository.
10: *
11: * @method Commune|null find($id, $lockMode = null, $lockVersion = null)
12: * @method Commune|null findOneBy(array $criteria, array $orderBy = null)
13: * @method Commune[] findAll()
14: * @method Commune[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15: */
16: class CommuneRepository extends ServiceEntityRepository
17: {
18:
19: public function __construct(ManagerRegistry $registry)
20: {
21: parent::__construct($registry, RepoEntity::class);
22: }
23:
24:
25: public $sortBy = array('nom_maj' => 'ASC');
26:
27:
28: /**
29: * Recherche basique.
30: */
31: public function searchBasic(string $term, ?int $limit=20)
32: {
33: return $this->createQueryBuilder('c')
34: ->select('c.codes AS id, c.nom AS text, c.postal AS cp, c.art')
35: ->where('c.nom LIKE :term')->setParameter('term', '%'.$term.'%')
36: ->setMaxResults($limit)
37: ->getQuery()->getArrayResult()
38: ;
39: }
40:
41:
42: /**
43: * Recherche du motif en préfixe, complétée si besoin en submatch.
44: */
45: public function search(string $term, ?int $limit=20) {
46: $results = array();
47: foreach (array('%s%%', '%%%s%%') as $motif) {
48: $nbmax = (($limit > 0) ? $limit : 100) - count($results);
49: if ($nbmax < 1) break;
50: $results+= $this->_em->createQueryBuilder()
51: ->select('c.codes AS id, c.nom AS text, c.postal AS cp, c.art')
52: ->from('TICTownBundle:Commune', 'c')
53: ->where((is_numeric($term)?'c.postal':'c.nom') . ' LIKE :term')
54: ->setParameter('term', sprintf($motif, $term))
55: ->orderBy('c.nom', 'ASC')->setMaxResults($nbmax)
56: ->getQuery()->getArrayResult()
57: ;
58: }
59: return array_values($results);
60: }
61:
62:
63: /**
64: * Recherche le code INSEE d'une commune à partir de son nom et/ou code postal.
65: */
66: public function findCodeInsee($nom=null, $cp=null)
67: {
68: if ($nom !== null) {
69: $nom = preg_replace("/^(L'|L |LA |LE |LES )/i", '', $nom);
70: $nom = preg_replace('/^(.* )?ST(E?) /i', '${1}SAINT${2} ', $nom);
71: $nom = trim($nom);
72: $nom = strtr($nom, ' ', '_');
73: }
74: if (($nom === "") && empty($cp)) return null;
75:
76: $qb = $this->createQueryBuilder('c');
77: if (! empty($nom)) $qb->andWhere('c.nom_maj LIKE :nom')->setParameter('nom', $nom);
78: if (! empty($cp)) $qb->andWhere('c.postal = :cp' )->setParameter('cp', $cp);
79: if ($results = $qb->getQuery()->getResult()) {
80: if (count($results) > 0) return $results[0];
81: }
82:
83: if (! empty($nom)) {
84: $qb = $this->createQueryBuilder('c');
85: if (! empty($nom)) $qb->andWhere('c.nom_maj LIKE :nom')->setParameter('nom', $nom.'%');
86: if (! empty($cp)) $qb->andWhere('c.postal = :cp' )->setParameter('cp', $cp);
87: if ($results = $qb->getQuery()->getResult()) {
88: if (count($results) > 0) return $results[0];
89: }
90: }
91:
92: if (! empty($nom)) {
93: $qb = $this->createQueryBuilder('c');
94: if (! empty($nom)) $qb->andWhere('c.nom_maj LIKE :nom')->setParameter('nom', '%'.substr($nom,0,20).'%');
95: if (! empty($cp)) $qb->andWhere('c.postal = :cp' )->setParameter('cp', $cp);
96: if ($results = $qb->getQuery()->getResult()) {
97: if (count($results) > 0) return $results[0];
98: }
99: }
100:
101: if (! empty($nom)) {
102: $qb = $this->createQueryBuilder('c');
103: if (! empty($nom)) $qb->andWhere('c.nom_maj LIKE :nom')->setParameter('nom', '%'.substr($nom,0,20).'%');
104: if (! empty($cp)) $qb->andWhere('c.postal LIKE :cp' )->setParameter('cp', substr($cp,0,2).'%');
105: if ($results = $qb->getQuery()->getResult()) {
106: if (count($results) > 0) return $results[0];
107: }
108: }
109:
110: if (! empty($nom) && ! empty($cp)) {
111: $qb = $this->createQueryBuilder('c');
112: if (! empty($cp)) $qb->andWhere('c.postal = :cp')->setParameter('cp', $cp);
113: if ($results = $qb->getQuery()->getResult()) {
114: if (count($results) > 0) return $results[0];
115: }
116: }
117:
118: return null;
119: }
120:
121:
122: public function disableByInsee($insee)
123: {
124: return $this->createQueryBuilder('c')->update()
125: ->where('c.insee = :insee')->setParameter('insee', $insee)
126: ->set('c.enabled', false)
127: ->getQuery()->execute()
128: ;
129: }
130:
131: public function updateStatusByCodes($codes, bool $enabled)
132: {
133: return $this->createQueryBuilder('c')->update()
134: ->where('c.codes = :codes')->setParameter('codes', $codes)
135: ->set('c.enabled', ':enabled')->setParameter('enabled', $enabled)
136: ->getQuery()->execute()
137: ;
138: }
139:
140:
141: }
142: