Source of file ImportManager.php

Size: 11,116 Bytes - Last Modified: 2023-11-16T22:56:03+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/town-bundle/src/Service/ImportManager.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
<?php
namespace TIC\TownBundle\Service;

use Doctrine\ORM\EntityManagerInterface;

use TIC\TownBundle\Entity\Region;
use TIC\TownBundle\Entity\Departement;
use TIC\TownBundle\Entity\Commune;

class ImportManager {

	protected $em;          /** @var EntityManager  Gestionnaire Doctrine des entités en base de données. */
	protected $repoR;
	protected $repoD;
	protected $repoC;


	public function __construct(EntityManagerInterface $em)
	{
		$this->em = $em;
		$this->repoR = $this->em->getRepository(Region::class);
		$this->repoD = $this->em->getRepository(Departement::class);
		$this->repoC = $this->em->getRepository(Commune::class);
	}


	public function loadPosteC(string $source, array $departements = array(), bool $test = false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ';');
		if (count($data) != 6) {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		$imported = [];

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ';');
			list($insee, $nom, $postal, $ligne5, $libelle, $coordonnees) = $data;
			if ($postal == "") continue;
			if ($insee  == "") continue;
			if ($nom    == "") continue;

			// Si la ville ne fait pas partie du filtre département, on ne la traite pas
			$dep = substr($postal, 0, 2);
			if (! empty($departements) && ! in_array($dep, $departements)) continue;

			$codes = $insee . ":" . $postal;
			if (isset($imported[$codes])) continue;
			$imported[$codes] = true;

			$commune = new Commune();
			$commune->setInsee($insee);
			$commune->setPostal($postal);
			$commune->setNomMaj($nom);
			$commune->setNom(ucwords(strtolower($nom)));
			$commune->setGeomap($coordonnees);
			if (! $test) $this->em->persist($commune);
			printf("Ville : [%s] %s\n", $insee, $commune);

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}


	public function loadInseeC(string $source, array $departements=array(), bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($typecom,$com,$reg,$dep,,$arr,$tncc,$ncc,$nccenr,$libelle,, ) = $data;
		if ($typecom!="TYPECOM" || $com!="COM" || $reg!="REG" || $dep!="DEP" || $arr!="ARR" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
			list($typecom,$com,$reg,$dep,,$arr,$tncc,$ncc,$nccenr,$libelle,, ) = $data;
			if ($com     === ""   ) continue;
			if ($typecom !== "COM" && $typecom !== "ARM") continue;

			// Si la ville ne fait pas partie du filtre département, on ne la traite pas
			if (! empty($departements) && ! in_array($dep, $departements)) continue;
			if ($test) printf("Ville : [%s] %s\n", $com, $libelle);

			$communes = $this->repoC->findByInsee($com);
			foreach ($communes as $commune) {
				$commune->setType($typecom);
				$commune->setTncc($tncc);
				$commune->setNomMaj($ncc);
				$commune->setNom($libelle);
				if ($departement = $this->repoD->find($dep)) $commune->setDepartement($departement);
				printf("Ville : [%s] %s\n", $com, $commune);
			}

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}
	
    public function loadInseeComerC(string $source, array $departements=array(), bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($com,$tncc,$ncc,$nccenr,$libelle,$nature,$comer,$lcomer) = $data;
		if ($com!="COM_COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE" || $nature!="NATURE_ZONAGE" || $comer!="COMER" || $lcomer!="LIBELLE_COMER") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
		    list($com,$tncc,$ncc,$nccenr,$libelle,$nature,$comer,$lcomer) = $data;
			if ($com     === ""   ) continue;

			// Si la ville ne fait pas partie du filtre département, on ne la traite pas
			if (! empty($departements) && ! in_array($comer, $departements)) continue;
			if ($test) printf("Ville : [%s] %s\n", $com, $libelle);

			$communes = $this->repoC->findByInsee($com);
			foreach ($communes as $commune) {
				//$commune->setType($typecom);
				$commune->setTncc($tncc);
				$commune->setNomMaj($ncc);
				$commune->setNom($libelle);
				$commune->setOutreMer(true);
				if ($departement = $this->repoD->find($comer)) $commune->setDepartement($departement);
				printf("Ville : [%s] %s\n", $com, $commune);
			}

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}


	public function loadInseeD(string $source, bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($dep,$reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data;
		if ($dep!="DEP" || $reg!="REG" || $cheflieu!="CHEFLIEU" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
			list($dep, $reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data;
			if ($dep == "") continue;
			if ($reg == "") continue;

			$departement = new Departement();
			$departement->setInsee($dep);
			$departement->setCheflieu($cheflieu);
			$departement->setNomMaj($ncc);
			$departement->setNom($libelle);
			$departement->setTncc($tncc);
			if ($region = $this->repoR->find($reg)) $departement->setRegion($region);
			if (! $test) $this->em->persist($departement);
			printf("Département : [%s] %s\n", $dep, $departement);

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}
	
    public function loadInseeComerD(string $source, bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($comer,$tncc,$ncc,$nccenr,$libelle) = $data;
		if ($comer!="COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
			list($comer,$tncc,$ncc,$nccenr,$libelle) = $data;
			if ($comer == "") continue;

			$departement = new Departement();
			$departement->setInsee($comer);
			$departement->setNomMaj($ncc);
			$departement->setNom($libelle);
			$departement->setTncc($tncc);
			$departement->setOutreMer(true);
			if ($region = $this->repoR->find($comer)) $departement->setRegion($region);
			if (! $test) $this->em->persist($departement);
			printf("Département : [%s] %s\n", $comer, $departement);

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}


	public function loadInseeR(string $source, bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data;
		if ($reg!="REG" || $cheflieu!="CHEFLIEU" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
			list($reg,$cheflieu,$tncc,$ncc,$nccenr,$libelle) = $data;
			if ($reg == "") continue;

			$region = new Region();
			$region->setInsee($reg);
			$region->setCheflieu($cheflieu);
			$region->setNomMaj($ncc);
			$region->setNom($libelle);
			$region->setTncc($tncc);
			if (! $test) $this->em->persist($region);
			printf("Région : [%s] %s\n", $reg, $region);

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}
	
    public function loadInseeComerR(string $source, bool $test=false)
	{
		if (! file_exists($source)) return;

		$zp = gzopen($source, 'r');
		$line = gzgets($zp, 4096);
		$count = 0;
		$this->em->getConnection()->getConfiguration()->setSQLLogger(null);

		// test la première ligne (qui doit contenir les entêtes)
		$data = str_getcsv($line, ",");
		list($comer,$tncc,$ncc,$nccenr,$libelle) = $data;
		if ($comer!="COMER" || $tncc!="TNCC" || $ncc!="NCC" || $nccenr!="NCCENR" || $libelle!="LIBELLE") {
			echo "Les données du fichier CSV ne sont pas conformes.\n";
			gzclose($zp);
			return false;
		}

		while (! gzeof($zp)) {
			$line = gzgets($zp, 4096);
			$data = str_getcsv($line, ",");
			list($comer,$tncc,$ncc,$nccenr,$libelle) = $data;
			if ($comer == "") continue;

			$region = new Region();
			$region->setInsee($comer);
			//$region->setCheflieu($cheflieu);
			$region->setNomMaj($ncc);
			$region->setNom($libelle);
			$region->setTncc($tncc);
			$region->setOutreMer(true);
			if (! $test) $this->em->persist($region);
			printf("Région : [%s] %s\n", $comer, $region);

			if ($count++ < 100) continue;
			if (! $test) { $this->em->flush(); $this->em->clear(); }
			$count = 0;
		}
		gzclose($zp);
		if (! $test) { $this->em->flush(); $this->em->clear(); }
	}

}