From a96c0607597d9c41d574976a7eee657d1ed9173d Mon Sep 17 00:00:00 2001
From: Armand Bahi <armand.bahi@veremes.com>
Date: Wed, 23 Jan 2019 10:31:51 +0100
Subject: [PATCH] =?UTF-8?q?Resolve=20"[ANC]=20Modification=20des=20encarts?=
 =?UTF-8?q?=20cartographiques=20sur=20installation=20et=20contr=C3=B4le"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ...c_saisie_anc_controle_controle_schema.json |   5 +-
 ...e_anc_installation_installation_suivi.json |   4 +-
 module/javascript/anc_saisie_map.js           | 396 ++++++++++++++++++
 module/javascript/script_module.js            |  10 +-
 web_service/conf/properties.inc               |   4 +
 5 files changed, 413 insertions(+), 6 deletions(-)
 create mode 100644 module/javascript/anc_saisie_map.js

diff --git a/module/forms/anc_saisie/anc_saisie_anc_controle_controle_schema.json b/module/forms/anc_saisie/anc_saisie_anc_controle_controle_schema.json
index 3c173b8f..45642a39 100755
--- a/module/forms/anc_saisie/anc_saisie_anc_controle_controle_schema.json
+++ b/module/forms/anc_saisie/anc_saisie_anc_controle_controle_schema.json
@@ -64,13 +64,13 @@
                         "label": "",
                         "required": false,
                         "nb_cols": 12,
-                        "id": "Element_0_1_1",
+                        "id":"anc_saisie_anc_controle_schema_map",
                         "style": {
                             "height": "350px"
                         },
                         "grid_height": "150px",
                         "map_options": {
-                            "proj": "EPSG:3857",
+                            "proj": "EPSG:2154",
                             "base_proj": "EPSG:4326",
                             "output_format": "geojson",
                             "type": "OSM",
@@ -307,6 +307,7 @@
                 ]
             }
         ],
+        "initEvent": "initAncControleSchemaFormMap()",
         "event": "sendSimpleForm()"
     }
 }
diff --git a/module/forms/anc_saisie/anc_saisie_anc_installation_installation_suivi.json b/module/forms/anc_saisie/anc_saisie_anc_installation_installation_suivi.json
index 3fa7403c..76476b27 100755
--- a/module/forms/anc_saisie/anc_saisie_anc_installation_installation_suivi.json
+++ b/module/forms/anc_saisie/anc_saisie_anc_installation_installation_suivi.json
@@ -307,7 +307,7 @@
                         "label":"Emplacement",
                         "required":false,
                         "nb_cols":12,
-                        "id":"geom_7_1",
+                        "id":"anc_saisie_anc_installation_suivi_map",
                         "style":{
                             "height":"350px"
                         },
@@ -411,4 +411,4 @@
     "datasources":{
 
     }
-}
\ No newline at end of file
+}
diff --git a/module/javascript/anc_saisie_map.js b/module/javascript/anc_saisie_map.js
new file mode 100644
index 00000000..0ecd9857
--- /dev/null
+++ b/module/javascript/anc_saisie_map.js
@@ -0,0 +1,396 @@
+/* global vitisApp, goog, angular, bootbox, oVFB */
+
+'use strict';
+goog.provide('vmap.anc.anc_saisie_map');
+vitisApp.on('appMainDrtvLoaded', function () {
+
+    var $q = angular.element(vitisApp.appMainDrtv).injector().get(['$q']);
+    var $log = angular.element(vitisApp.appMainDrtv).injector().get(['$log']);
+    var envSrvc = angular.element(vitisApp.appMainDrtv).injector().get(['envSrvc']);
+    var $rootScope = angular.element(vitisApp.appMainDrtv).injector().get(["$rootScope"]);
+    var propertiesSrvc = angular.element(vitisApp.appMainDrtv).injector().get(['propertiesSrvc']);
+
+    /**
+     * Initialise la carte du menu installation > suivi
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['initAncInstallationSuiviFormMap'] = function () {
+        $log.info("initAncInstallationSuiviFormMap");
+
+        var this_ = this;
+        var scope = this;
+
+        // Remplace si besoin l'identifiant et le type de carte à utiliser
+        var sMapId = this['getAncFormMapId']('installation');
+        this['setFormMapTreeByMapId'](sMapId);
+
+        // Résultat de la propertie anc.installation.zoom_on_parcelle
+        var bZoomOnParcelle = this['getAncZoomOnParcellePropertie'](propertiesSrvc, 'installation');
+
+        // Récupère l'identifiant de la parcelle correspondante
+        var sIdPar = this['getAncInstallSuiviIdPar'](scope);
+
+        // Récupère la géométrie de l'installation
+        var sGeom = this['ancMapGetFormGeom'](scope, 'installation');
+
+        if (!goog.isDefAndNotNull(sGeom)) {
+            // Zoom la carte sur la parcelle
+            if (goog.isDefAndNotNull(sIdPar) && bZoomOnParcelle === true) {
+                this['ancMapZoomOnParcelle'](sIdPar, 'installation');
+            }
+        }
+    }
+
+    /**
+     * Initialise la carte du menu controle > schema
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['initAncControleSchemaFormMap'] = function () {
+        $log.info("initAncInstallationSuiviFormMap");
+
+        var this_ = this;
+        var scope = angular.element("form[name='" + envSrvc["oFormDefinition"][envSrvc["sFormDefinitionName"]]["name"]).scope();
+
+        // Remplace si besoin l'identifiant et le type de carte à utiliser
+        var sMapId = this['getAncFormMapId']('controle');
+        this['setFormMapTreeByMapId'](sMapId);
+
+        // Résultat de la propertie anc.installation.zoom_on_parcelle
+        var bZoomOnParcelle = this['getAncZoomOnParcellePropertie'](propertiesSrvc, 'controle');
+
+        // Récupère la géométrie de l'installation
+        var sGeom = this['ancMapGetFormGeom'](scope, 'controle');
+
+        // Récupère l'identifiant de la parcelle correspondante
+        var sIdPar = this['getAncControleSchemaIdPar'](scope).then(function(sIdPar){
+
+            // Zoom la carte sur la parcelle
+            if (!goog.isDefAndNotNull(sGeom)) {
+                if (goog.isDefAndNotNull(sIdPar) && bZoomOnParcelle === true) {
+                    this_['ancMapZoomOnParcelle'](sIdPar, 'controle');
+                }
+            }
+        });
+    }
+
+    /**
+     * Remplace la carte OSM si une carte est fournie sur properties.anc.installation..map_id
+     * @param  {string} sMapId map id
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['setFormMapTreeByMapId'] = function (sMapId) {
+        $log.info("initAncInstallationSuiviFormMapTree");
+
+        var formScope = angular.element("form[name='" + envSrvc["oFormDefinition"][envSrvc["sFormDefinitionName"]]["name"]).scope();
+
+        var oForm;
+        if (goog.isDefAndNotNull(formScope)) {
+            if (goog.isDefAndNotNull(formScope['oFormDefinition'])) {
+                if (goog.isDefAndNotNull(formScope['oFormDefinition'][envSrvc["sFormDefinitionName"]])) {
+                    oForm = formScope['oFormDefinition'][envSrvc["sFormDefinitionName"]];
+                }
+            }
+        }
+        if (goog.isDefAndNotNull(oForm) && goog.isDefAndNotNull(sMapId)) {
+            if (goog.isArray(oForm['rows'])) {
+                for (var i = 0; i < oForm['rows'].length; i++) {
+                    if (goog.isArray(oForm['rows'][i]['fields'])) {
+                        for (var ii = 0; ii < oForm['rows'][i]['fields'].length; ii++) {
+                            if (goog.isObject(oForm['rows'][i]['fields'][ii]['map_options'])) {
+                                if(oForm['rows'][i]['fields'][ii]['type'] === 'map_osm'){
+                                    oForm['rows'][i]['fields'][ii]['type'] = 'map_vmap';
+                                }
+                                oForm['rows'][i]['fields'][ii]['map_options']['type'] = 'vmap';
+                                oForm['rows'][i]['fields'][ii]['map_options']['map_id'] = sMapId;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Récupère l'identifiant de la cate fourni sur properties.anc.installation.map_id
+     *
+     * @return {string|undefined}  map_id
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['getAncFormMapId'] = function (sObject) {
+
+        var sMapId;
+        if (goog.isDefAndNotNull(propertiesSrvc)) {
+            if (goog.isDefAndNotNull(propertiesSrvc["anc"])) {
+                if (goog.isDefAndNotNull(propertiesSrvc["anc"][sObject])) {
+                    if (goog.isDefAndNotNull(propertiesSrvc["anc"][sObject]["map_id"])) {
+                        if (propertiesSrvc["anc"][sObject]["map_id"] !== false) {
+                            sMapId = propertiesSrvc["anc"][sObject]["map_id"];
+                        }
+                    }
+                }
+            }
+        }
+        return sMapId;
+    }
+
+    /**
+     * Get propertie anc.installation.zoom_on_parcelle
+     *
+     * @param  {object} oProperties Properties
+     * @return {boolean} anc.installation.zoom_on_parcelle value
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['getAncZoomOnParcellePropertie'] = function(oProperties, sObject){
+        $log.info('getAncZoomOnParcellePropertie');
+
+        var bZoomOnParcelle = false;
+        if (goog.isDefAndNotNull(oProperties)) {
+            if (goog.isDefAndNotNull(oProperties["anc"])) {
+                if (goog.isDefAndNotNull(oProperties["anc"][sObject])) {
+                    if (goog.isDefAndNotNull(oProperties["anc"][sObject]["zoom_on_parcelle"])) {
+                        if (oProperties["anc"][sObject]["zoom_on_parcelle"] === true) {
+                            bZoomOnParcelle = true;
+                        }
+                    }
+                }
+            }
+        }
+        return bZoomOnParcelle;
+    }
+
+    /**
+     * Récupère l'id_par correspondant à l'installation
+     *
+     * @param  {object} scope description
+     * @return {string|undefined} id_par de la parcelle correspondante
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['getAncInstallSuiviIdPar'] = function(scope){
+        $log.info('getAncInstallSuiviIdPar');
+
+        var sParcelle;
+        if (goog.isDefAndNotNull(scope['oFormValues'])) {
+            if (goog.isDefAndNotNull(scope['sFormDefinitionName'])) {
+                if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']])) {
+                    if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']]['id_parc'])) {
+                        sParcelle = scope['oFormValues'][scope['sFormDefinitionName']]['id_parc'];
+                    }
+                }
+            }
+        }
+
+        return sParcelle;
+    }
+
+    /**
+     * Récupère l'id_par correspondant à l'installation dans une promesse
+     *
+     * @param  {object} scope description
+     * @return {promise}
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['getAncControleSchemaIdPar'] = function(scope){
+        $log.info('getAncControleSchemaIdPar');
+
+        var deferred = $q.defer();
+
+        var sInstall = this['getAncControleSchemaIdInstall'](scope);
+
+        // Requête Ajax pour récupérer la définition complète de l'install
+        if (!goog.isDefAndNotNull(sInstall) || sInstall == '') {
+            console.error('id_installation non valide');
+            deferred.reject();
+        } else {
+            // Récupère la liste des rapports disponibles
+            ajaxRequest({
+                'method': 'GET',
+                'url': propertiesSrvc["web_server_name"] + "/" + propertiesSrvc["services_alias"] + '/anc/installations/' + sInstall,
+                'headers': {
+                    'Accept': 'application/x-vm-json'
+                },
+                'success': function (response) {
+                    if (!goog.isDefAndNotNull(response['data'])) {
+                        console.error('response.data undefined: ', response);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'])) {
+                        console.error('Aucune installation disponible pour ' + sInstall);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'][0])) {
+                        console.error('Aucune installation disponible pour ' + sInstall);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'][0]['id_parc'])) {
+                        console.error('Aucune parcelle disponible pour ' + sInstall);
+                        deferred.reject();
+                        return 0;
+                    }
+                    var sParcelle = response['data']['data'][0]['id_parc'];
+                    deferred.resolve(sParcelle);
+                }
+            });
+        }
+
+        return deferred.promise;
+    }
+
+    /**
+     * Récupère l'id_install correspondant au contrôle
+     *
+     * @param  {object} scope description
+     * @return {string|undefined} id_install de la parcelle correspondante
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['getAncControleSchemaIdInstall'] = function(scope){
+        $log.info('getAncControleSchemaIdInstall');
+
+        var sInstall;
+        if (goog.isDefAndNotNull(scope['oFormValues'])) {
+            if (goog.isDefAndNotNull(scope['sFormDefinitionName'])) {
+                if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']])) {
+                    if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']]['id_installation'])) {
+                        sInstall = scope['oFormValues'][scope['sFormDefinitionName']]['id_installation'];
+                    }
+                }
+            }
+        }
+
+        return sInstall;
+    }
+
+    /**
+     * Récupère la geom correspondant à l'installation
+     *
+     * @param  {object} scope description
+     * @return {string|undefined}
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['ancMapGetFormGeom'] = function(scope, sObject){
+        $log.info('ancMapGetFormGeom');
+
+        var sGeom, sField;
+
+        if (sObject === 'installation') {
+            sField = 'geom';
+        }
+        if (sObject === 'controle') {
+            sField = 'composants';
+        }
+
+        if (goog.isDefAndNotNull(sField)) {
+            if (goog.isDefAndNotNull(scope['oFormValues'])) {
+                if (goog.isDefAndNotNull(scope['sFormDefinitionName'])) {
+                    if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']])) {
+                        if (goog.isDefAndNotNull(scope['oFormValues'][scope['sFormDefinitionName']][sField])) {
+                            sGeom = scope['oFormValues'][scope['sFormDefinitionName']][sField];
+                        }
+                    }
+                }
+            }
+        }
+
+        return sGeom;
+    }
+
+    /**
+     * Requeête Ajax pour récupérer la définition de la parcelle
+     *
+     * @param  {string} sIdPar id_par de la parcelle
+     * @return {promise}
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['ancMapGetParcelleDef'] = function(sIdPar){
+        $log.info('ancMapGetParcelleDef');
+
+        var deferred = $q.defer();
+
+        if (!goog.isDefAndNotNull(sIdPar) || sIdPar == '') {
+            console.error('id_par non valide');
+            deferred.reject();
+        } else {
+
+            // Récupère la liste des rapports disponibles
+            ajaxRequest({
+                'method': 'GET',
+                'url': propertiesSrvc["web_server_name"] + "/" + propertiesSrvc["services_alias"] + '/cadastreV2/parcelles/' + sIdPar,
+                'headers': {
+                    'Accept': 'application/x-vm-json'
+                },
+                'success': function (response) {
+                    if (!goog.isDefAndNotNull(response['data'])) {
+                        console.error('response.data undefined: ', response);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'])) {
+                        console.error('Aucune parcelle disponible pour ' + sIdPar);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'][0])) {
+                        console.error('Aucune parcelle disponible pour ' + sIdPar);
+                        deferred.reject();
+                        return 0;
+                    }
+                    if (!goog.isDefAndNotNull(response['data']['data'][0]['id_par'])) {
+                        console.error('Aucune parcelle disponible pour ' + sIdPar);
+                        deferred.reject();
+                        return 0;
+                    }
+                    var oParcelle = response['data']['data'][0];
+                    deferred.resolve(oParcelle);
+                }
+            });
+        }
+
+        return deferred.promise;
+    }
+
+    /**
+     * Met à jour l'étendue de la carte sur des features
+     *
+     * @param  {object} oMap carte
+     * @param  {array} aFeatures features
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['ancMapUpdateOlMapExtent'] = function(oMap, aFeatures){
+        $log.info('ancMapUpdateOlMapExtent');
+
+        if (goog.isDefAndNotNull(aFeatures[0])) {
+            var featureExtent = aFeatures[0].getGeometry().getExtent();
+            oMap.MapObject.getView().fit(featureExtent);
+            if (aFeatures[0].getGeometry().getType() === 'Point') {
+                oMap.MapObject.getView().setZoom(15);
+            }
+        }
+    }
+
+    /**
+     * Zoom la carte sur la parcelle correspondante
+     *
+     * @param  {string} sIdPar id_par de la parcelle
+     * @return {promise}
+     */
+    angular.element(vitisApp.appMainDrtv).scope()['ancMapZoomOnParcelle'] = function(sIdPar, sObject){
+        $log.info('ancMapZoomOnParcelle');
+
+        var this_ = this;
+        var sMapElemId;
+
+        if (sObject === 'installation') {
+            sMapElemId = 'anc_saisie_anc_installation_suivi_map';
+        } else if (sObject === 'controle') {
+            sMapElemId = 'anc_saisie_anc_controle_schema_map';
+        }
+
+        if (goog.isDefAndNotNull(sIdPar) || sIdPar == '') {
+            this['ancMapGetParcelleDef'](sIdPar).then(function(oParcelle){
+                setTimeout(function () {
+                    var oMap = angular.element($('#' + sMapElemId)).scope()['oMap'];
+                    var sGeom = oParcelle['geom'];
+                    if (!goog.isDefAndNotNull(sGeom) || sGeom == '') {
+                        console.error('parcelle ' + sIdPar + ' non valide');
+                        return null;
+                    }
+                    var aFeatures = oMap.getFeaturesByString(oParcelle['geom'], 'ewkt');
+                    this_['ancMapUpdateOlMapExtent'](oMap, aFeatures);
+                }, 1000);
+            });
+        } else {
+            console.error('parcelle ' + sIdPar + ' non valide');
+        }
+    }
+});
diff --git a/module/javascript/script_module.js b/module/javascript/script_module.js
index c1c63cfd..81e39224 100755
--- a/module/javascript/script_module.js
+++ b/module/javascript/script_module.js
@@ -2,7 +2,9 @@
 
 'use strict';
 goog.provide('vmap.anc.script_module');
+goog.require('vmap.anc.anc_saisie_map');
 vitisApp.on('appMainDrtvLoaded', function () {
+
     /**
      * initAncControlForm function.
      * Traitements avant l'affichage du formulaire de la section "Dossier" de l'onglet "Contrôle".
@@ -337,9 +339,13 @@ vitisApp.on('appMainDrtvLoaded', function () {
         var $log = angular.element(vitisApp.appMainDrtv).injector().get(["$log"]);
         var $rootScope = angular.element(vitisApp.appMainDrtv).injector().get(["$rootScope"]);
         var envSrvc = angular.element(vitisApp.appMainDrtv).injector().get(["envSrvc"]);
-        //
+
         $log.info("initAncInstallationSuiviForm");
-        // Attends la fin du chargement de tous les champs du formulaire.
+
+        // Initialise la carte
+        this['initAncInstallationSuiviFormMap']();
+
+        // Attend la fin du chargement de tous les champs du formulaire.
         var clearListener = $rootScope.$on('formExtracted', function (event, sFormDefinitionName) {
             // Supprime le "listener".
             clearListener();
diff --git a/web_service/conf/properties.inc b/web_service/conf/properties.inc
index a8477bb7..49ff6cc5 100755
--- a/web_service/conf/properties.inc
+++ b/web_service/conf/properties.inc
@@ -8,4 +8,8 @@
     $properties["anc"]["code_postal"]["table"] = '';
     $properties["anc"]["code_postal"]["column"] = '';
     $properties["anc"]["controle"]["business_object_id"] = '';
+    $properties["anc"]["controle"]["map_id"] = false;
+    $properties["anc"]["controle"]["zoom_on_parcelle"] = true;
+    $properties["anc"]["installation"]["map_id"] = false;
+    $properties["anc"]["installation"]["zoom_on_parcelle"] = true;
 ?>
-- 
GitLab