| 1: | <?php |
| 2: | namespace TIC\TownBundle\Service; |
| 3: | |
| 4: | use Doctrine\ORM\EntityManagerInterface; |
| 5: | |
| 6: | use TIC\TownBundle\Entity\Region; |
| 7: | use TIC\TownBundle\Entity\Departement; |
| 8: | use TIC\TownBundle\Entity\Commune; |
| 9: | |
| 10: | class ImportManager { |
| 11: | |
| 12: | protected $em; |
| 13: | protected $repoR; |
| 14: | protected $repoD; |
| 15: | protected $repoC; |
| 16: | |
| 17: | |
| 18: | public function __construct(EntityManagerInterface $em) |
| 19: | { |
| 20: | $this->em = $em; |
| 21: | $this->repoR = $this->em->getRepository(Region::class); |
| 22: | $this->repoD = $this->em->getRepository(Departement::class); |
| 23: | $this->repoC = $this->em->getRepository(Commune::class); |
| 24: | } |
| 25: | |
| 26: | |
| 27: | public function loadPosteC(string $source, array $departements = array(), bool $test = false) |
| 28: | { |
| 29: | if (! file_exists($source)) return; |
| 30: | |
| 31: | $zp = gzopen($source, 'r'); |
| 32: | $line = gzgets($zp, 4096); |
| 33: | $count = 0; |
| 34: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 35: | |
| 36: | |
| 37: | $data = str_getcsv($line, ';'); |
| 38: | if (count($data) != 6) { |
| 39: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 40: | gzclose($zp); |
| 41: | return false; |
| 42: | } |
| 43: | |
| 44: | $imported = []; |
| 45: | |
| 46: | while (! gzeof($zp)) { |
| 47: | $line = gzgets($zp, 4096); |
| 48: | $data = str_getcsv($line, ';'); |
| 49: | list($insee, $nom, $postal, $ligne5, $libelle, $coordonnees) = $data; |
| 50: | if ($postal == "") continue; |
| 51: | if ($insee == "") continue; |
| 52: | if ($nom == "") continue; |
| 53: | |
| 54: | |
| 55: | $dep = substr($postal, 0, 2); |
| 56: | if (! empty($departements) && ! in_array($dep, $departements)) continue; |
| 57: | |
| 58: | $codes = $insee . ":" . $postal; |
| 59: | if (isset($imported[$codes])) continue; |
| 60: | $imported[$codes] = true; |
| 61: | |
| 62: | $commune = new Commune(); |
| 63: | $commune->setInsee($insee); |
| 64: | $commune->setPostal($postal); |
| 65: | $commune->setNomMaj($nom); |
| 66: | $commune->setNom(ucwords(strtolower($nom))); |
| 67: | $commune->setGeomap($coordonnees); |
| 68: | if (! $test) $this->em->persist($commune); |
| 69: | printf("Ville : [%s] %s\n", $insee, $commune); |
| 70: | |
| 71: | if ($count++ < 100) continue; |
| 72: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 73: | $count = 0; |
| 74: | } |
| 75: | gzclose($zp); |
| 76: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 77: | } |
| 78: | |
| 79: | |
| 80: | public function loadInseeC(string $source, array $departements=array(), bool $test=false) |
| 81: | { |
| 82: | if (! file_exists($source)) return; |
| 83: | |
| 84: | $zp = gzopen($source, 'r'); |
| 85: | $line = gzgets($zp, 4096); |
| 86: | $count = 0; |
| 87: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 88: | |
| 89: | |
| 90: | $data = str_getcsv($line, ","); |
| 91: | list($typecom,$com,$reg,$dep,,$arr,$tncc,$ncc,$nccenr,$libelle,, ) = $data; |
| 92: | if ($typecom!="TYPECOM" || $com!="COM" || $reg!="REG" || $dep!="DEP" || $arr!="ARR" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") { |
| 93: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 94: | gzclose($zp); |
| 95: | return false; |
| 96: | } |
| 97: | |
| 98: | while (! gzeof($zp)) { |
| 99: | $line = gzgets($zp, 4096); |
| 100: | $data = str_getcsv($line, ","); |
| 101: | list($typecom,$com,$reg,$dep,,$arr,$tncc,$ncc,$nccenr,$libelle,, ) = $data; |
| 102: | if ($com === "" ) continue; |
| 103: | if ($typecom !== "COM" && $typecom !== "ARM") continue; |
| 104: | |
| 105: | |
| 106: | if (! empty($departements) && ! in_array($dep, $departements)) continue; |
| 107: | if ($test) printf("Ville : [%s] %s\n", $com, $libelle); |
| 108: | |
| 109: | $communes = $this->repoC->findByInsee($com); |
| 110: | foreach ($communes as $commune) { |
| 111: | $commune->setType($typecom); |
| 112: | $commune->setTncc($tncc); |
| 113: | $commune->setNomMaj($ncc); |
| 114: | $commune->setNom($libelle); |
| 115: | if ($departement = $this->repoD->find($dep)) $commune->setDepartement($departement); |
| 116: | printf("Ville : [%s] %s\n", $com, $commune); |
| 117: | } |
| 118: | |
| 119: | if ($count++ < 100) continue; |
| 120: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 121: | $count = 0; |
| 122: | } |
| 123: | gzclose($zp); |
| 124: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 125: | } |
| 126: | |
| 127: | public function loadInseeComerC(string $source, array $departements=array(), bool $test=false) |
| 128: | { |
| 129: | if (! file_exists($source)) return; |
| 130: | |
| 131: | $zp = gzopen($source, 'r'); |
| 132: | $line = gzgets($zp, 4096); |
| 133: | $count = 0; |
| 134: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 135: | |
| 136: | |
| 137: | $data = str_getcsv($line, ","); |
| 138: | list($com,$tncc,$ncc,$nccenr,$libelle,$nature,$comer,$lcomer) = $data; |
| 139: | if ($com!="COM_COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE" || $nature!="NATURE_ZONAGE" || $comer!="COMER" || $lcomer!="LIBELLE_COMER") { |
| 140: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 141: | gzclose($zp); |
| 142: | return false; |
| 143: | } |
| 144: | |
| 145: | while (! gzeof($zp)) { |
| 146: | $line = gzgets($zp, 4096); |
| 147: | $data = str_getcsv($line, ","); |
| 148: | list($com,$tncc,$ncc,$nccenr,$libelle,$nature,$comer,$lcomer) = $data; |
| 149: | if ($com === "" ) continue; |
| 150: | |
| 151: | |
| 152: | if (! empty($departements) && ! in_array($comer, $departements)) continue; |
| 153: | if ($test) printf("Ville : [%s] %s\n", $com, $libelle); |
| 154: | |
| 155: | $communes = $this->repoC->findByInsee($com); |
| 156: | foreach ($communes as $commune) { |
| 157: | |
| 158: | $commune->setTncc($tncc); |
| 159: | $commune->setNomMaj($ncc); |
| 160: | $commune->setNom($libelle); |
| 161: | $commune->setOutreMer(true); |
| 162: | if ($departement = $this->repoD->find($comer)) $commune->setDepartement($departement); |
| 163: | printf("Ville : [%s] %s\n", $com, $commune); |
| 164: | } |
| 165: | |
| 166: | if ($count++ < 100) continue; |
| 167: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 168: | $count = 0; |
| 169: | } |
| 170: | gzclose($zp); |
| 171: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 172: | } |
| 173: | |
| 174: | |
| 175: | public function loadInseeD(string $source, bool $test=false) |
| 176: | { |
| 177: | if (! file_exists($source)) return; |
| 178: | |
| 179: | $zp = gzopen($source, 'r'); |
| 180: | $line = gzgets($zp, 4096); |
| 181: | $count = 0; |
| 182: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 183: | |
| 184: | |
| 185: | $data = str_getcsv($line, ","); |
| 186: | list($dep,$reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 187: | if ($dep!="DEP" || $reg!="REG" || $cheflieu!="CHEFLIEU" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") { |
| 188: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 189: | gzclose($zp); |
| 190: | return false; |
| 191: | } |
| 192: | |
| 193: | while (! gzeof($zp)) { |
| 194: | $line = gzgets($zp, 4096); |
| 195: | $data = str_getcsv($line, ","); |
| 196: | list($dep, $reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 197: | if ($dep == "") continue; |
| 198: | if ($reg == "") continue; |
| 199: | |
| 200: | $departement = new Departement(); |
| 201: | $departement->setInsee($dep); |
| 202: | $departement->setCheflieu($cheflieu); |
| 203: | $departement->setNomMaj($ncc); |
| 204: | $departement->setNom($libelle); |
| 205: | $departement->setTncc($tncc); |
| 206: | if ($region = $this->repoR->find($reg)) $departement->setRegion($region); |
| 207: | if (! $test) $this->em->persist($departement); |
| 208: | printf("Département : [%s] %s\n", $dep, $departement); |
| 209: | |
| 210: | if ($count++ < 100) continue; |
| 211: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 212: | $count = 0; |
| 213: | } |
| 214: | gzclose($zp); |
| 215: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 216: | } |
| 217: | |
| 218: | public function loadInseeComerD(string $source, bool $test=false) |
| 219: | { |
| 220: | if (! file_exists($source)) return; |
| 221: | |
| 222: | $zp = gzopen($source, 'r'); |
| 223: | $line = gzgets($zp, 4096); |
| 224: | $count = 0; |
| 225: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 226: | |
| 227: | |
| 228: | $data = str_getcsv($line, ","); |
| 229: | list($comer,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 230: | if ($comer!="COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") { |
| 231: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 232: | gzclose($zp); |
| 233: | return false; |
| 234: | } |
| 235: | |
| 236: | while (! gzeof($zp)) { |
| 237: | $line = gzgets($zp, 4096); |
| 238: | $data = str_getcsv($line, ","); |
| 239: | list($comer,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 240: | if ($comer == "") continue; |
| 241: | |
| 242: | $departement = new Departement(); |
| 243: | $departement->setInsee($comer); |
| 244: | $departement->setNomMaj($ncc); |
| 245: | $departement->setNom($libelle); |
| 246: | $departement->setTncc($tncc); |
| 247: | $departement->setOutreMer(true); |
| 248: | if ($region = $this->repoR->find($comer)) $departement->setRegion($region); |
| 249: | if (! $test) $this->em->persist($departement); |
| 250: | printf("Département : [%s] %s\n", $comer, $departement); |
| 251: | |
| 252: | if ($count++ < 100) continue; |
| 253: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 254: | $count = 0; |
| 255: | } |
| 256: | gzclose($zp); |
| 257: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 258: | } |
| 259: | |
| 260: | |
| 261: | public function loadInseeR(string $source, bool $test=false) |
| 262: | { |
| 263: | if (! file_exists($source)) return; |
| 264: | |
| 265: | $zp = gzopen($source, 'r'); |
| 266: | $line = gzgets($zp, 4096); |
| 267: | $count = 0; |
| 268: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 269: | |
| 270: | |
| 271: | $data = str_getcsv($line, ","); |
| 272: | list($reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 273: | if ($reg!="REG" || $cheflieu!="CHEFLIEU" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") { |
| 274: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 275: | gzclose($zp); |
| 276: | return false; |
| 277: | } |
| 278: | |
| 279: | while (! gzeof($zp)) { |
| 280: | $line = gzgets($zp, 4096); |
| 281: | $data = str_getcsv($line, ","); |
| 282: | list($reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 283: | if ($reg == "") continue; |
| 284: | |
| 285: | $region = new Region(); |
| 286: | $region->setInsee($reg); |
| 287: | $region->setCheflieu($cheflieu); |
| 288: | $region->setNomMaj($ncc); |
| 289: | $region->setNom($libelle); |
| 290: | $region->setTncc($tncc); |
| 291: | if (! $test) $this->em->persist($region); |
| 292: | printf("Région : [%s] %s\n", $reg, $region); |
| 293: | |
| 294: | if ($count++ < 100) continue; |
| 295: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 296: | $count = 0; |
| 297: | } |
| 298: | gzclose($zp); |
| 299: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 300: | } |
| 301: | |
| 302: | public function loadInseeComerR(string $source, bool $test=false) |
| 303: | { |
| 304: | if (! file_exists($source)) return; |
| 305: | |
| 306: | $zp = gzopen($source, 'r'); |
| 307: | $line = gzgets($zp, 4096); |
| 308: | $count = 0; |
| 309: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 310: | |
| 311: | |
| 312: | $data = str_getcsv($line, ","); |
| 313: | list($comer,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 314: | if ($comer!="COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") { |
| 315: | echo "Les données du fichier CSV ne sont pas conformes.\n"; |
| 316: | gzclose($zp); |
| 317: | return false; |
| 318: | } |
| 319: | |
| 320: | while (! gzeof($zp)) { |
| 321: | $line = gzgets($zp, 4096); |
| 322: | $data = str_getcsv($line, ","); |
| 323: | list($comer,$tncc,$ncc,$nccenr,$libelle) = $data; |
| 324: | if ($comer == "") continue; |
| 325: | |
| 326: | $region = new Region(); |
| 327: | $region->setInsee($comer); |
| 328: | |
| 329: | $region->setNomMaj($ncc); |
| 330: | $region->setNom($libelle); |
| 331: | $region->setTncc($tncc); |
| 332: | $region->setOutreMer(true); |
| 333: | if (! $test) $this->em->persist($region); |
| 334: | printf("Région : [%s] %s\n", $comer, $region); |
| 335: | |
| 336: | if ($count++ < 100) continue; |
| 337: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 338: | $count = 0; |
| 339: | } |
| 340: | gzclose($zp); |
| 341: | if (! $test) { $this->em->flush(); $this->em->clear(); } |
| 342: | } |
| 343: | |
| 344: | } |
| 345: | |