From 8813c424340a5284672edabb7db07dc7f5a69e5f Mon Sep 17 00:00:00 2001
From: Armand Bahi <armand.bahi@veremes.com>
Date: Thu, 3 Jan 2019 11:59:24 +0100
Subject: [PATCH] =?UTF-8?q?Resolve=20"Cartes=20par=20d=C3=A9faut=20aux=20u?=
 =?UTF-8?q?tilisateurs"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../users/users_vitis_users_vmap_user.json    | 60 ++++++++++++++++-
 src/module_vmap/module/lang/lang-en.json      |  1 +
 src/module_vmap/module/lang/lang-fr.json      |  1 +
 .../web_service/sql/sqlQueries.xml            | 18 +++++
 .../web_service/ws/MapCatalog.class.inc       | 44 ++++++++++--
 .../web_service/ws/Vmap.class.sql.inc         |  6 +-
 .../web_service/ws/VmapUser.class.inc         | 67 ++++++++++++-------
 .../web_service/ws/VmapUsers.class.inc        | 38 +++++++++--
 8 files changed, 197 insertions(+), 38 deletions(-)

diff --git a/src/module_vmap/module/forms/users/users_vitis_users_vmap_user.json b/src/module_vmap/module/forms/users/users_vitis_users_vmap_user.json
index d922e45e..1136d285 100755
--- a/src/module_vmap/module/forms/users/users_vitis_users_vmap_user.json
+++ b/src/module_vmap/module/forms/users/users_vitis_users_vmap_user.json
@@ -1,4 +1,19 @@
 {
+    "datasources": {
+        "datasource_1": {
+            "type": "web_service",
+            "dataType": "webService",
+            "name": "datasource_maps",
+            "description": "",
+            "ressource_id": "vmap/Maps",
+            "webservice": {
+                "name": "vmap"
+            },
+            "ressource": {
+                "name": "Maps"
+            }
+        }
+    },
     "search":{
 
     },
@@ -19,6 +34,27 @@
                     }
                 ]
             },
+            {
+                "fields": [
+                    {
+                        "type": "select",
+                        "name": "default_map",
+                        "label": "FORM_DEFAULT_MAP_USERS_USER_VMAP_USER",
+                        "required": false,
+                        "nb_cols": 12,
+                        "datasource": {
+                            "datasource_id": "datasource_1",
+                            "sort_order": "ASC",
+                            "distinct": "true",
+                            "label_key": "name",
+                            "order_by": "name",
+                            "id_key": "map_id",
+                            "attributs": "map_id|name"
+                        },
+                        "id_from": "Element_0_6_1_from"
+                    }
+                ]
+            },
             {
                 "fields":[
                     {
@@ -87,6 +123,28 @@
                     }
                 ]
             },
+            {
+                "fields": [
+                    {
+                        "type": "select",
+                        "name": "default_map",
+                        "label": "FORM_DEFAULT_MAP_USERS_USER_VMAP_USER",
+                        "required": false,
+                        "disabled": true,
+                        "nb_cols": 12,
+                        "datasource": {
+                            "datasource_id": "datasource_1",
+                            "sort_order": "ASC",
+                            "distinct": "true",
+                            "label_key": "name",
+                            "order_by": "name",
+                            "id_key": "map_id",
+                            "attributs": "map_id|name"
+                        },
+                        "id_from": "Element_0_6_1_from"
+                    }
+                ]
+            },
             {
                 "fields":[
                     {
@@ -117,4 +175,4 @@
             }
         ]
     }
-}
\ No newline at end of file
+}
diff --git a/src/module_vmap/module/lang/lang-en.json b/src/module_vmap/module/lang/lang-en.json
index dfca19f9..eab78cc7 100644
--- a/src/module_vmap/module/lang/lang-en.json
+++ b/src/module_vmap/module/lang/lang-en.json
@@ -217,6 +217,7 @@
     "FORM_MAP_USERS_VMAP_PRINT_STYLE": "Users linked to style",
     "": "",
     "FORM_ID_USERS_USER_VMAP_USER": "ID",
+    "FORM_DEFAULT_MAP_USERS_USER_VMAP_USER": "Default map",
     "FORM_PRINT_STYLES_USERS_USER_VMAP_USER": "Print styles",
     "FORM_PRINT_STYLES_AVAILABLE_USERS_USER_VMAP_USER": "Available print styles",
     "FORM_GROUP_PRINT_STYLES_USERS_USER_VMAP_USER": "Print styles linked to user",
diff --git a/src/module_vmap/module/lang/lang-fr.json b/src/module_vmap/module/lang/lang-fr.json
index a1b1a7bb..0a145e87 100644
--- a/src/module_vmap/module/lang/lang-fr.json
+++ b/src/module_vmap/module/lang/lang-fr.json
@@ -217,6 +217,7 @@
     "FORM_MAP_USERS_VMAP_PRINT_STYLE": "Utilisateurs liés au style",
     "": "",
     "FORM_ID_USERS_USER_VMAP_USER": "ID",
+    "FORM_DEFAULT_MAP_USERS_USER_VMAP_USER": "Carte par défaut",
     "FORM_PRINT_STYLES_USERS_USER_VMAP_USER": "Styles d'impression",
     "FORM_PRINT_STYLES_AVAILABLE_USERS_USER_VMAP_USER": "Styles d'impression disponibles",
     "FORM_GROUP_PRINT_STYLES_USERS_USER_VMAP_USER": "Styles d'impression liés à l'utilisateur",
diff --git a/src/module_vmap/web_service/sql/sqlQueries.xml b/src/module_vmap/web_service/sql/sqlQueries.xml
index a7a4ff66..154432ce 100644
--- a/src/module_vmap/web_service/sql/sqlQueries.xml
+++ b/src/module_vmap/web_service/sql/sqlQueries.xml
@@ -1145,5 +1145,23 @@
 				]]>
 			</code>
 		</query>
+		<query>
+			<type>update</type>
+			<version>2019.01.00</version>
+			<code>
+				<![CDATA[
+					-- Armand 02/01/2019 Ajout carte par défaut par utilisateur
+					CREATE TABLE s_vmap.user_defaultmap ( user_id integer NOT NULL, map_id integer NOT NULL, CONSTRAINT user_defaultmap_pkey PRIMARY KEY (map_id, user_id), CONSTRAINT fk_user_defaultmap_user FOREIGN KEY (user_id) REFERENCES s_vitis."user" (user_id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT fk_user_defaultmap_map FOREIGN KEY (map_id) REFERENCES s_vmap.map (map_id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION) WITH ( OIDS=FALSE);
+					ALTER TABLE s_vmap.user_defaultmap OWNER TO u_vitis;
+					GRANT ALL ON TABLE s_vmap.user_defaultmap TO u_vitis;
+					GRANT ALL ON TABLE s_vmap.user_defaultmap TO vmap_admin;
+					GRANT SELECT ON TABLE s_vmap.user_defaultmap TO vmap_user;
+					CREATE OR REPLACE VIEW s_vmap.v_user_defaultmap AS SELECT user_defaultmap.map_id, user_defaultmap.user_id FROM s_vmap.user_defaultmap WHERE user_defaultmap.user_id = ( SELECT "user".user_id FROM s_vitis."user" WHERE "user".login::name = CURRENT_USER );
+					ALTER TABLE s_vmap.v_user_defaultmap  OWNER TO u_vitis;
+					GRANT ALL ON TABLE s_vmap.v_user_defaultmap TO vmap_admin;
+					GRANT SELECT ON TABLE s_vmap.v_user_defaultmap TO vmap_user;
+				]]>
+			</code>
+		</query>
 	</queriesCollection>
 </sqlQueries>
diff --git a/src/module_vmap/web_service/ws/MapCatalog.class.inc b/src/module_vmap/web_service/ws/MapCatalog.class.inc
index 34e8a343..01c90bad 100755
--- a/src/module_vmap/web_service/ws/MapCatalog.class.inc
+++ b/src/module_vmap/web_service/ws/MapCatalog.class.inc
@@ -16,7 +16,7 @@ require_once dirname($_SERVER['SCRIPT_FILENAME']) . '/class/vitis_lib/Connection
  * \brief This file contains the Map php class
  *
  * This class defines operation for one Map
- * 
+ *
  */
 class MapCatalog extends Vmap {
 
@@ -39,7 +39,7 @@ class MapCatalog extends Vmap {
     }
 
     /**
-     * @SWG\Get(path="/mapcatalog", 
+     * @SWG\Get(path="/mapcatalog",
      *   tags={"Catalog"},
      *   summary="Get Map",
      *   description="Request to get Map by id",
@@ -74,13 +74,13 @@ class MapCatalog extends Vmap {
         // Récupère les groupes correspondants
         $sGroups = $this->oConnection->sesGroup;
 
-        // Récupère les cartes correspondante      
+        // Récupère les cartes correspondante
         $aMaps = $this->getMaps($sGroups);
 
         // Récupère les services correspondants
         $aServices = $this->getServices();
 
-        $MapCatalog['usedMap'] = 0;
+        $MapCatalog['usedMap'] = $this->getDefaultMap($aMaps);
         $MapCatalog['maps'] = $aMaps;
         $MapCatalog['services'] = $aServices;
 
@@ -238,6 +238,40 @@ class MapCatalog extends Vmap {
         return $aServices;
     }
 
+    /**
+     * getDefaultMap - get the default map index
+     *
+     * @param  {array} $aMaps
+     * @return {number} default map index
+     */
+    function getDefaultMap($aMaps) {
+        require $this->sRessourcesFile;
+
+        $iDefaultMapId = false;
+        $iDefaultMapIndex = 0;
+
+        // Récupère la carte par défaut de l'utilisateur en cours
+        $sSql = $aSql['getCurrentUserDefaultMap'];
+        $aSQLParams = array(
+            'sSchemaFramework' => array('value' => $this->aProperties['schema_framework'], 'type' => 'schema_name'),
+            'sSchemaVmap' => array('value' => $this->aProperties['schema_vmap'], 'type' => 'column_name')
+        );
+        $oPDOresult = $this->oConnection->oBd->executeWithParams($sSql, $aSQLParams);
+        while($aLigne=$this->oConnection->oBd->ligneSuivante ($oPDOresult)) {
+            $iDefaultMapId = $aLigne['map_id'];
+        }
+
+        if ($iDefaultMapId !== false) {
+            for ($i=0; $i < count($aMaps); $i++) {
+                if ($aMaps[$i]['map_id'] == $iDefaultMapId) {
+                    $iDefaultMapIndex = $i;
+                }
+            }
+        }
+
+        return $iDefaultMapIndex;
+    }
+
     function httpPost($url, $params) {
         $postData = '';
         //create name value pairs seperated by &
@@ -262,4 +296,4 @@ class MapCatalog extends Vmap {
 
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/src/module_vmap/web_service/ws/Vmap.class.sql.inc b/src/module_vmap/web_service/ws/Vmap.class.sql.inc
index cf54181a..3b44532a 100755
--- a/src/module_vmap/web_service/ws/Vmap.class.sql.inc
+++ b/src/module_vmap/web_service/ws/Vmap.class.sql.inc
@@ -27,6 +27,10 @@ $aSql['updateMapLayerOpacity'] = "UPDATE [sSchemaVmap].map_layer SET layer_opaci
 // VmapUsers
 $aSql['getUserPrintStyles'] = "SELECT user_printstyle.printstyle_id,printstyle.name FROM [sSchemaVmap].user_printstyle LEFT JOIN [sSchemaVmap].printstyle ON user_printstyle.printstyle_id = printstyle.printstyle_id WHERE user_printstyle.user_id = [user_id]";
 $aSql['insertUserPrintStyles'] = "INSERT INTO [sSchemaVmap].user_printstyle(user_id, printstyle_id) VALUES([user_id], [printstyle_id])";
+$aSql['getUserDefaultMap'] = "SELECT user_defaultmap.map_id FROM [sSchemaVmap].user_defaultmap WHERE user_defaultmap.user_id = [user_id]";
+$aSql['getCurrentUserDefaultMap'] = "SELECT map_id FROM [sSchemaVmap].v_user_defaultmap";
+$aSql['removeUserDefaultMap'] = "DELETE FROM [sSchemaVmap].user_defaultmap WHERE user_defaultmap.user_id = [user_id]";
+$aSql['insertUserDefaultMap'] = "INSERT INTO [sSchemaVmap].user_defaultmap(user_id, map_id) VALUES([user_id], [map_id])";
 // PrintTemplates
 $aSql['getPrintTemplateParameters'] = "SELECT * FROM [sSchemaVmap].v_print_parameter WHERE printtemplate_id=[printtemplate_id]";
 $aSql['insertPrintTemplateGroups'] = "INSERT INTO [sSchemaVmap].printtemplate_group(group_id, printtemplate_id) VALUES([group_id], [printtemplate_id])";
@@ -42,4 +46,4 @@ $aSql['setLayerBoId'] = "UPDATE [sSchemaVmap].layer SET bo_id = NULL WHERE layer
 $aSql['deleteAssociatedBos'] = "DELETE FROM [sSchemaVmap].layer_businessobject WHERE layer_id = [layer_id]";
 $aSql['deleteAssociatedLayers'] = "DELETE FROM [sSchemaVmap].layer_businessobject WHERE business_object_id = [business_object_id]";
 $aSql['addAssociatedBos'] = "INSERT INTO [sSchemaVmap].layer_businessobject(layer_id, business_object_id) VALUES ([layer_id], [business_object_id]);";
-?>
\ No newline at end of file
+?>
diff --git a/src/module_vmap/web_service/ws/VmapUser.class.inc b/src/module_vmap/web_service/ws/VmapUser.class.inc
index d2225a63..41a75bba 100755
--- a/src/module_vmap/web_service/ws/VmapUser.class.inc
+++ b/src/module_vmap/web_service/ws/VmapUser.class.inc
@@ -10,10 +10,10 @@ require_once dirname($_SERVER['SCRIPT_FILENAME']) . '/class/vitis_lib/Connection
 *	\brief This file contains the VmapUser php class
 *
 * This class defines operation for one User
-* 
+*
 */
 class VmapUser  extends Vmap {
-    
+
     public $oError;
     /**
      * construct
@@ -25,10 +25,10 @@ class VmapUser  extends Vmap {
      */
     function __construct($aPath, $aValues, $properties, $bShortcut = false, $oConnection = false) {
         parent::__construct($aPath, $aValues, $properties, $bShortcut, $oConnection);
-        $this->aSelectedFields = Array("user_id", "login", "print_styles");
+        $this->aSelectedFields = Array("user_id", "login", "print_styles", "default_map");
     }
       /**
-     * @SWG\Get(path="/vmapusers/{user_id}", 
+     * @SWG\Get(path="/vmapusers/{user_id}",
      *   tags={"Users"},
      *   summary="Get User",
      *   description="Request to get user by id",
@@ -69,31 +69,50 @@ class VmapUser  extends Vmap {
         require $this->sRessourcesFile;
         $this->aFields = $this->getFields($this->aProperties['schema_framework'], "v_user", "user_id");
         $this->getPrintStyles();
+        $this->getDefaultMap();
     }
-    
+
     /**
-     *  get print styles of user
-     */
+    *  get print styles of user
+    */
     function getPrintStyles(){
         require $this->sRessourcesFile;
         if (in_array("print_styles", $this->aSelectedFields)){
-                $aParams['sSchemaVmap'] = array('value' => $this->aProperties['schema_vmap'], 'type' => 'schema_name');
-                $aParams['user_id'] = array('value' => $this->aValues['my_vitis_id'], 'type' => 'number');
-                $oPDOresult = $this->oConnection->oBd->executeWithParams($aSql['getUserPrintStyles'], $aParams);
-		$sListPrintStyleId = "";
-                $aListPrintStyleId = array();
-		while($aLigne=$this->oConnection->oBd->ligneSuivante ($oPDOresult)) {
-			if ($sListPrintStyleId == ""){
-				$sListPrintStyleId = $aLigne["printstyle_id"];
-			}else{
-				$sListPrintStyleId .= "|".$aLigne["printstyle_id"];
-			}
-                        $aListPrintStyleId[] = $aLigne["name"];
-		}
-		$oPDOresult=$this->oConnection->oBd->fermeResultat();
-                $this->aFields['print_styles'] = $sListPrintStyleId;
-                $this->aFields['print_styles_label'] = implode(',', $aListPrintStyleId);
+            $aParams['sSchemaVmap'] = array('value' => $this->aProperties['schema_vmap'], 'type' => 'schema_name');
+            $aParams['user_id'] = array('value' => $this->aValues['my_vitis_id'], 'type' => 'number');
+            $oPDOresult = $this->oConnection->oBd->executeWithParams($aSql['getUserPrintStyles'], $aParams);
+            $sListPrintStyleId = "";
+            $aListPrintStyleId = array();
+            while($aLigne=$this->oConnection->oBd->ligneSuivante ($oPDOresult)) {
+                if ($sListPrintStyleId == ""){
+                    $sListPrintStyleId = $aLigne["printstyle_id"];
+                }else{
+                    $sListPrintStyleId .= "|".$aLigne["printstyle_id"];
+                }
+                $aListPrintStyleId[] = $aLigne["name"];
+            }
+            $oPDOresult=$this->oConnection->oBd->fermeResultat();
+            $this->aFields['print_styles'] = $sListPrintStyleId;
+            $this->aFields['print_styles_label'] = implode(',', $aListPrintStyleId);
+        }
+    }
+
+    /**
+     *  get the user default map
+     */
+    function getDefaultMap(){
+        require $this->sRessourcesFile;
+        if (in_array("default_map", $this->aSelectedFields)){
+            $aParams['sSchemaVmap'] = array('value' => $this->aProperties['schema_vmap'], 'type' => 'schema_name');
+            $aParams['user_id'] = array('value' => $this->aValues['my_vitis_id'], 'type' => 'number');
+            $oPDOresult = $this->oConnection->oBd->executeWithParams($aSql['getUserDefaultMap'], $aParams);
+            $sDefaultMapId = "";
+            while($aLigne=$this->oConnection->oBd->ligneSuivante ($oPDOresult)) {
+                $sDefaultMapId = $aLigne['map_id'];
+            }
+		    $oPDOresult=$this->oConnection->oBd->fermeResultat();
+            $this->aFields['default_map'] = $sDefaultMapId;
         }
     }
 }
-?>
\ No newline at end of file
+?>
diff --git a/src/module_vmap/web_service/ws/VmapUsers.class.inc b/src/module_vmap/web_service/ws/VmapUsers.class.inc
index ed8262b4..84088f8b 100755
--- a/src/module_vmap/web_service/ws/VmapUsers.class.inc
+++ b/src/module_vmap/web_service/ws/VmapUsers.class.inc
@@ -9,7 +9,7 @@
 *	\brief This file contains the Modes php class
 *
 * This class defines Rest Api to Vmap Users
-* 
+*
 */
 require_once 'Vmap.class.inc';
 require_once dirname($_SERVER['SCRIPT_FILENAME']) . '/class/vitis_lib/Connection.class.inc';
@@ -17,7 +17,7 @@ require_once 'VmapUser.class.inc';
 require_once(dirname($_SERVER['SCRIPT_FILENAME']) . '/class/vmlib/BdDataAccess.inc');
 
 class VmapUsers extends Vmap {
-    
+
      /**
      * @SWG\Definition(
      *   definition="/vmapusers",
@@ -42,7 +42,7 @@ class VmapUsers extends Vmap {
         parent::__construct($aPath, $aValues, $properties, $bShortcut, $oConnection);
         $this->aSelectedFields = Array("user_id", "login", "print_styles");
     }
-    
+
     /**
      * get Users
      * @return  Users
@@ -51,7 +51,7 @@ class VmapUsers extends Vmap {
         $aReturn = $this->genericGet( $this->aProperties['schema_framework'], "v_user", "user_id");
         return $aReturn['sMessage'];
     }
-    
+
     /**
      * @SWG\Put(path="/vmapusers/{user_id}",
      *   tags={"Users"},
@@ -106,10 +106,10 @@ class VmapUsers extends Vmap {
         $sMessage = $this->asDocument('', 'vitis', $this->aValues['sEncoding'], True, $aXmlRacineAttribute, $this->aValues['sSourceEncoding'], $this->aValues['output']);
         // Supprime les styles d'impression rattachés à l'utilisateur.
         $this->oConnection->oBd->delete($this->aProperties['schema_vmap'], 'user_printstyle', 'user_id', $this->aValues["my_vitis_id"]);
-        // Styles d'impression à rattacher au usere ?
+        // Styles d'impression à rattacher au user ?
         if (!empty($this->aValues['print_styles'])) {
             $aPrintStyles = explode('|', $this->aValues['print_styles']);
-            foreach ($aPrintStyles as $iPrintStyleId) {                
+            foreach ($aPrintStyles as $iPrintStyleId) {
                 $sSql = $aSql['insertUserPrintStyles'];
                 $aSQLParams = array(
                     'sSchemaVmap' => array('value' => $this->aProperties['schema_vmap'], 'type' => 'column_name'),
@@ -125,7 +125,31 @@ class VmapUsers extends Vmap {
                 }
             }
         }
+        // Carte par défaut à rattacher au user ?
+        // Supprime la carte par défaut
+        $sSql = $aSql['removeUserDefaultMap'];
+        $aSQLParams = array(
+            'sSchemaVmap' => array('value' => $this->aProperties['schema_vmap'], 'type' => 'column_name'),
+            'user_id' => array('value' => $this->aValues["my_vitis_id"], 'type' => 'number')
+        );
+        $resultat = $this->oConnection->oBd->executeWithParams($sSql, $aSQLParams);
+        if (!empty($this->aValues['default_map'])) {
+            // Ajoute la carte par défaut
+            $sSql = $aSql['insertUserDefaultMap'];
+            $aSQLParams = array(
+                'sSchemaVmap' => array('value' => $this->aProperties['schema_vmap'], 'type' => 'column_name'),
+                'user_id' => array('value' => $this->aValues["my_vitis_id"], 'type' => 'number'),
+                'map_id' => array('value' => $this->aValues['default_map'], 'type' => 'number')
+            );
+            $resultat = $this->oConnection->oBd->executeWithParams($sSql, $aSQLParams);
+            if ($this->oConnection->oBd->enErreur()) {
+                $this->oError = new VitisError(1, $this->oConnection->oBd->getBDMessage());
+                $oError = new VitisError(1, $this->oConnection->oBd->getBDMessage());
+                $aXmlRacineAttribute['status'] = 0;
+                $sMessage = $oError->asDocument('', 'vitis', $this->aValues['sEncoding'], True, $aXmlRacineAttribute, $this->aValues['sSourceEncoding'], $this->aValues['output']);
+            }
+        }
         return $sMessage;
     }
 }
-?>
\ No newline at end of file
+?>
-- 
GitLab