Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: intégration de l'api adresse #212

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/controllers/EtablissementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function init(): void
$modelCapsuleRubrique = new Model_DbTable_CapsuleRubrique();
$this->view->assign('nomOngletDescriptif', $modelCapsuleRubrique->getCapsuleRubriqueByInternalName('descriptifEtablissement')['NOM']);
$this->view->assign('nomOngletEffectifsDegagements', $modelCapsuleRubrique->getCapsuleRubriqueByInternalName('effectifsDegagementsEtablissement')['NOM']);
$this->view->assign('useApiAdresse', getenv('PREVARISC_API_ADRESSE_MODAL'));

$this->serviceEtablissement = new Service_Etablissement();
$id = $this->getRequest()->getParam('id');
Expand Down Expand Up @@ -88,7 +89,6 @@ public function editAction(): void
$viewHeadLink->headLink()->appendStylesheet('/js/geoportail/sdk-ol/GpSDK2D.css', 'all');

$service_carto = new Service_Carto();

$this->view->assign('key_ign', getenv('PREVARISC_PLUGIN_IGNKEY'));
$this->view->assign('geoconcept_url', getenv('PREVARISC_PLUGIN_GEOCONCEPT_URL'));
$this->view->assign('default_lon', getenv('PREVARISC_CARTO_DEFAULT_LON') ?: '2.71490430425517');
Expand Down
61 changes: 61 additions & 0 deletions application/controllers/MigrationAdressesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

class MigrationAdressesController extends Zend_Controller_Action
{
public function indexAction(): void
{
$this->_helper->layout->setLayout('menu_admin');
}

public function executeMigrationAction(): void
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

$etablissementAdresseModel = new Model_DbTable_EtablissementAdresse();
$etablissementAdresseApiModel = new Model_DbTable_EtablissementAdresseApi();
$adresseApiService = new Service_AdresseApi();

$oldAddresses = $etablissementAdresseModel->getAllAdresses();
$totalAddresses = count($oldAddresses);
$migratedAddresses = 0;

foreach ($oldAddresses as $oldAddress) {
$query = trim(($oldAddress['NUMERO_ADRESSE'] ?? '').' '.
($oldAddress['LIBELLE_RUE'] ?? '').' '.
($oldAddress['COMPLEMENT_ADRESSE'] ?? '').' '.
($oldAddress['CODEPOSTAL_COMMUNE'] ?? '').' '.
($oldAddress['LIBELLE_COMMUNE'] ?? ''));
$apiResult = $adresseApiService->getAdresseApi($query, 'housenumber', 1);

if (null !== $apiResult) {
$apiAddress = $apiResult[0];

$dataToSave = [
'ADRESSE' => $apiAddress['ADRESSE'],
'LON_ETABLISSEMENTADRESSE' => $apiAddress['longitude'],
'LAT_ETABLISSEMENTADRESSE' => $apiAddress['latitude'],
'NUMINSEE_COMMUNE' => $apiAddress['insee_code'],
'CODEPOSTAL_COMMUNE' => $apiAddress['postal_code'],
'LIBELLE_COMMUNE' => $apiAddress['city'],
'LIBELLE_RUE' => $apiAddress['street'],
];

$addressExists = $etablissementAdresseApiModel->exists($dataToSave);
if (!$addressExists) {
$etablissementAdresseApiModel->save($dataToSave, $oldAddress['ID_ETABLISSEMENT']);
++$migratedAddresses;
} else {
error_log("Migration échouée pour l'adresse : ".$query." - L'adresse existe déjà dans la base de données.");
}
} else {
error_log("Aucun résultat de l'API pour la requête: ".$query);
}
}

echo json_encode([
'total' => $totalAddresses,
'migrated' => $migratedAddresses,
]);
}
}
12 changes: 8 additions & 4 deletions application/controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ public function etablissementAction(): void
$commissions = array_key_exists('commissions', $parameters) && '' != $parameters['commissions'] ? $parameters['commissions'] : null;
$groupements_territoriaux = array_key_exists('groupements_territoriaux', $parameters) && '' != $parameters['groupements_territoriaux'] ? $parameters['groupements_territoriaux'] : null;
$preventionniste = array_key_exists('preventionniste', $parameters) && '' != $parameters['preventionniste'] ? $parameters['preventionniste'] : null;
$type = array_key_exists('type', $parameters) && '' != $parameters['type'] ? $parameters['type'] : null;
$adresse = array_key_exists('adresse', $parameters) && '' != $parameters['adresse'] ? $parameters['adresse'] : null;

$search = $service_search->extractionEtablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $local_sommeil, null, null, null, $city, $street, $number, $commissions, $groupements_territoriaux, $preventionniste);
$search = $service_search->extractionEtablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $local_sommeil, null, null, null, $type, $adresse, $city, $street, $number, $commissions, $groupements_territoriaux, $preventionniste);

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
Expand Down Expand Up @@ -251,12 +253,14 @@ public function etablissementAction(): void
$avis_favorable = array_key_exists('avis', $parameters) && 1 == count($parameters['avis']) ? 'true' == $parameters['avis'][0] : null;
$statuts = $parameters['statuts'] ?? null;
$local_sommeil = array_key_exists('presences_local_sommeil', $parameters) && 1 == count($parameters['presences_local_sommeil']) ? 'true' == $parameters['presences_local_sommeil'][0] : null;
$city = array_key_exists('city', $parameters) && '' != $parameters['city'] ? $parameters['city'] : null;
$street = array_key_exists('street', $parameters) && '' != $parameters['street'] ? $parameters['street'] : null;
$number = array_key_exists('number', $parameters) && '' != $parameters['number'] ? $parameters['number'] : null;
$city = array_key_exists('city', $parameters) && '' != $parameters['city'] ? $parameters['city'] : null;

$commissions = array_key_exists('commissions', $parameters) && '' != $parameters['commissions'] ? $parameters['commissions'] : null;
$preventionniste = array_key_exists('preventionniste', $parameters) && '' != $parameters['preventionniste'] ? $parameters['preventionniste'] : null;

$type = array_key_exists('type', $parameters) && '' != $parameters['type'] ? $parameters['type'] : null;
$adresse = array_key_exists('adresse', $parameters) && '' != $parameters['adresse'] ? $parameters['adresse'] : null;
if (array_key_exists('groupements_territoriaux', $parameters) && '' != $parameters['groupements_territoriaux']) {
$groupements_territoriaux = $parameters['groupements_territoriaux'];
} elseif (null != $this->view->user && array_key_exists('groupements', $this->view->user) && count($this->view->user['groupements']) > 0) {
Expand All @@ -270,7 +274,7 @@ public function etablissementAction(): void
$groupements_territoriaux = null;
}

$search = $service_search->etablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $local_sommeil, null, null, null, $city, $street, $number, $commissions, $groupements_territoriaux, $preventionniste, 50, $page);
$search = $service_search->etablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $type, $adresse, $local_sommeil, null, null, null, $city, $street, $number, $commissions, $groupements_territoriaux, $preventionniste, 50, $page);

$paginator = new Zend_Paginator(new SDIS62_Paginator_Adapter_Array($search['results'], $search['search_metadata']['count']));
$paginator->setItemCountPerPage(50)->setCurrentPageNumber($page)->setDefaultScrollingStyle('Elastic');
Expand Down
4 changes: 4 additions & 0 deletions application/layouts/menu_admin.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<a href='/gestion-des-communes' ><i class="icon-globe icon-black"></i> Gestion des communes</a>
</li>

<li <?php if (Zend_Controller_Front::getInstance()->getRequest()->getControllerName() == 'migration-adresses') echo "class='active'" ?>>
<a href='/migration-adresses'><i class="icon-repeat icon-black"></i> Migration des adresses</a>
</li>

<li <?php if(Zend_Controller_Front::getInstance()->getRequest()->getControllerName() === 'fusion-des-communes') echo "class='active'" ?> >
<a href='/fusion-des-communes' ><i class="icon-random icon-black"></i> Fusion des communes</a>
</li>
Expand Down
37 changes: 36 additions & 1 deletion application/models/DbTable/EtablissementAdresse.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class Model_DbTable_EtablissementAdresse extends Zend_Db_Table_Abstract
class Model_DbTable_EtablissementAdresse extends Zend_Db_Table_Abstract implements Service_Interface_EtablissementAdresse
{
// Nom de la base
protected $_name = 'etablissementadresse';
Expand Down Expand Up @@ -74,6 +74,41 @@ public function get($id_etablissement)
}
}

public function save($adresse, $etablissementID)
{
if (
array_key_exists('ID_RUE', $adresse)
&& (int) $adresse['ID_RUE'] > 0
) {
$row = $this->createRow([
'NUMERO_ADRESSE' => $adresse['NUMERO_ADRESSE'],
'COMPLEMENT_ADRESSE' => $adresse['COMPLEMENT_ADRESSE'],
'LON_ETABLISSEMENTADRESSE' => empty($adresse['LON_ETABLISSEMENTADRESSE']) ? null : $adresse['LON_ETABLISSEMENTADRESSE'],
'LAT_ETABLISSEMENTADRESSE' => empty($adresse['LAT_ETABLISSEMENTADRESSE']) ? null : $adresse['LAT_ETABLISSEMENTADRESSE'],
'ID_ETABLISSEMENT' => $etablissementID,
'ID_RUE' => $adresse['ID_RUE'],
'NUMINSEE_COMMUNE' => $adresse['NUMINSEE_COMMUNE'],
]);

return $row->save();
}

return null;
}

public function getAllAdresses()
{
$select = $this->select()
->setIntegrityCheck(false)
->from('etablissementadresse')
->joinLeft('adressecommune', 'etablissementadresse.NUMINSEE_COMMUNE = adressecommune.NUMINSEE_COMMUNE', ['LIBELLE_COMMUNE', 'CODEPOSTAL_COMMUNE'])
->joinLeft('adresserue', 'etablissementadresse.ID_RUE = adresserue.ID_RUE AND etablissementadresse.NUMINSEE_COMMUNE = adresserue.NUMINSEE_COMMUNE', 'LIBELLE_RUE')
->joinLeft('adresseruetype', 'adresseruetype.ID_RUETYPE = adresserue.ID_RUETYPE', ['LIBELLE_RUETYPE', 'ABREVIATION_RUETYPE'])
;

return $this->fetchAll($select)->toArray();
}

public function getAdresse($idAdresse)
{
$select = $this->select()
Expand Down
108 changes: 108 additions & 0 deletions application/models/DbTable/EtablissementAdresseApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

class Model_DbTable_EtablissementAdresseApi extends Zend_Db_Table_Abstract implements Service_Interface_EtablissementAdresse
{
protected $_name = 'etablissementadresseapi';

// Nom de la base
protected $_primary = 'ID_ADRESSE'; // Clé primaire

/**
* @param float|int|string $id_etablissement
*/
public function get($id_etablissement)
{
$model_etablissement = new Model_DbTable_Etablissement();
$informations = $model_etablissement->getInformations($id_etablissement);

switch ($informations->ID_GENRE) {
// Adresse d'un site
case 1:
$search = new Model_DbTable_Search();

$etablissement_enfants = $search->setItem('etablissement')
->setCriteria('etablissementlie.ID_ETABLISSEMENT', $id_etablissement)
->run()
->getAdapter()
->getItems(0, 99999999999)
;

if (!empty($etablissement_enfants)) {
$i = 0;
foreach ($etablissement_enfants as $key => $ets) {
if (
($ets['EFFECTIFPUBLIC_ETABLISSEMENTINFORMATIONS'] + $ets['EFFECTIFPERSONNEL_ETABLISSEMENTINFORMATIONS']) >
($etablissement_enfants[$i]['EFFECTIFPUBLIC_ETABLISSEMENTINFORMATIONS'] + $etablissement_enfants[$i]['EFFECTIFPERSONNEL_ETABLISSEMENTINFORMATIONS'])
) {
$i = $key;
}
}

return $this->get($etablissement_enfants[$i]['ID_ETABLISSEMENT']);
}

return [];

// Adresse d'une cellule
case 3:
// Récupération des parents de l'établissement
$results = [];
$id_enfant = $id_etablissement;
do {
$parent = $model_etablissement->getParent($id_enfant);
if (null != $parent) {
$results[] = $parent;
$id_enfant = $parent['ID_ETABLISSEMENT'];
}
} while (null != $parent);

$etablissement_parents = [] === $results ? [] : array_reverse($results);
$pere = end($etablissement_parents);

if ($pere) {
return $this->get($pere['ID_ETABLISSEMENT']);
}

return [];

// Adresse par défaut
default:
$select = $this->select()
->setIntegrityCheck(false)
->from('etablissementadresseapi')
->where('etablissementadresseapi.ID_ETABLISSEMENT = ?', $id_etablissement)
;

return $this->fetchAll($select)->toArray();
}
}

public function save($adresse, $etablissementID)
{
if ('' !== $adresse['ADRESSE']) {
$row = $this->createRow([
'ADRESSE' => $adresse['ADRESSE'],
'LON_ETABLISSEMENTADRESSE' => empty($adresse['LON_ETABLISSEMENTADRESSE']) ? null : $adresse['LON_ETABLISSEMENTADRESSE'],
'LAT_ETABLISSEMENTADRESSE' => empty($adresse['LAT_ETABLISSEMENTADRESSE']) ? null : $adresse['LAT_ETABLISSEMENTADRESSE'],
'ID_ETABLISSEMENT' => $etablissementID,
'NUMINSEE_COMMUNE' => $adresse['NUMINSEE_COMMUNE'],
'CODEPOSTAL_COMMUNE' => $adresse['CODEPOSTAL_COMMUNE'],
'LIBELLE_COMMUNE' => $adresse['LIBELLE_COMMUNE'],
'LIBELLE_RUE' => $adresse['LIBELLE_RUE'],
]);

return $row->save();
}

return null;
}

public function exists(array $data): bool
{
$select = $this->select()
->where('ADRESSE = ?', $data['ADRESSE'])
;

return null !== $this->fetchRow($select);
}
}
4 changes: 4 additions & 0 deletions application/modules/api/configs/routes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<route>get_adresse</route>
<defaults method="getAdresse" />
</get_adresse>
<get_adresse_api type="Zend_Controller_Router_Route">
<route>get_adresse_api</route>
<defaults method="getAdresseApi" />
</get_adresse_api>
</chains>
</etablissement>

Expand Down
7 changes: 7 additions & 0 deletions application/modules/api/services/Etablissement.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ public function getAdresse(int $id)

return $serviceAdresse->getAdresseById($id);
}

public function getAdresseApi(string $query, string $type, int $limit): ?array
{
$service_etablissement = new Service_AdresseApi();

return $service_etablissement->getAdresseApi($query, $type, $limit);
}
}
6 changes: 4 additions & 2 deletions application/modules/api/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ class Api_Service_Search
* @param null|mixed $preventionniste
* @param int $count Par défaut 10, max 1000
* @param int $page par défaut = 1
* @param null|mixed $type
* @param null|mixed $adresse
*
* @return array
*/
public function etablissements($label = null, $identifiant = null, $genres = null, $categories = null, $classes = null, $familles = null, $types_activites = null, $avis_favorable = null, $statuts = null, $local_sommeil = null, $lon = null, $lat = null, $parent = null, $commissions = null, $groupements_territoriaux = null, $preventionniste = null, $count = 10, $page = 1)
public function etablissements($label = null, $identifiant = null, $genres = null, $categories = null, $classes = null, $familles = null, $types_activites = null, $avis_favorable = null, $statuts = null, $type = null, $adresse = null, $local_sommeil = null, $lon = null, $lat = null, $parent = null, $commissions = null, $groupements_territoriaux = null, $preventionniste = null, $count = 10, $page = 1)
{
$service_search = new Service_Search();

return $service_search->etablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $local_sommeil, $lon, $lat, $parent, null, null, null, $commissions, $groupements_territoriaux, $preventionniste, $count, $page);
return $service_search->etablissements($label, $identifiant, $genres, $categories, $classes, $familles, $types_activites, $avis_favorable, $statuts, $type, $adresse, $local_sommeil, $lon, $lat, $parent, null, null, null, $commissions, $groupements_territoriaux, $preventionniste, $count, $page);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions application/navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,17 @@
</save>
</pages>
</gestion-des-communes>
<migration-adresses>
<label>Migration des adresses</label>
<controller>migration-adresses</controller>
<pages>
<executeMigration>
<label>Migration</label>
<controller>migration-adresses</controller>
<action>executeMigration</action>
</executeMigration>
</pages>
</migration-adresses>
<fusion-des-communes>
<label>Fusion des communes</label>
<controller>fusion-des-communes</controller>
Expand Down
Loading
Loading