From fd42653db2eb8b64955315e093b5ff3978558dda Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 4 Jul 2018 12:01:03 +0200
Subject: [PATCH] FEAT #7994 status order

---
 apps/maarch_entreprise/actions/index_mlb.php     | 10 +++-------
 apps/maarch_entreprise/actions/validate_mail.php | 10 +++-------
 sql/develop.sql                                  | 13 +++++++++----
 sql/structure.sql                                |  1 +
 src/app/basket/controllers/BasketController.php  |  5 +++--
 src/app/basket/models/BasketModelAbstract.php    |  4 +++-
 src/core/models/ValidatorModel.php               | 10 +++++-----
 7 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index d94be722dd7..3be44782bc7 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -150,12 +150,11 @@ function get_form_txt($values, $pathManageAction, $actionId, $table, $module, $c
         $owner_usr_grp = $grp_status->group_id;
         $owner_basket_id = str_replace('_'.$_SESSION['current_basket']['basket_owner'], '', $_SESSION['current_basket']['id']);
     } else {
-        $owner_usr_grp = $_SESSION['user']['primarygroup'];
+        $owner_usr_grp = $_SESSION['current_basket']['group_id'];
         $owner_basket_id = $_SESSION['current_basket']['id'];
     }
-    $query = 'SELECT status_id, label_status FROM '.GROUPBASKET_STATUS.' left join '.$_SESSION['tablename']['status']
-        .' on status_id = id '
-        .' WHERE basket_id= ? and (group_id = ?) and action_id = ?';
+    $query = 'SELECT status_id, label_status FROM groupbasket_status left join status on status_id = id '
+        .' WHERE basket_id= ? and (group_id = ?) and action_id = ? ORDER BY groupbasket_status.order';
     $stmt = $db->query($query, array($owner_basket_id, $owner_usr_grp, $actionId));
 
     if ($stmt->rowCount() > 0) {
@@ -820,9 +819,6 @@ function get_form_txt($values, $pathManageAction, $actionId, $table, $module, $c
         //$frmStr .= '<option value="">' . _CHOOSE_STATUS . '</option>';
         for ($i = 0; $i < count($statuses); ++$i) {
             $frmStr .= '<option value="'.functions::xssafe($statuses[$i]['ID']).'" ';
-            if ($statuses[$i]['ID'] == 'NEW') {
-                $frmStr .= 'selected="selected"';
-            }
             $frmStr .= '>'.functions::xssafe($statuses[$i]['LABEL']).'</option>';
         }
         $frmStr .= '</select></td><td><span class="red_asterisk" id="market_mandatory" '
diff --git a/apps/maarch_entreprise/actions/validate_mail.php b/apps/maarch_entreprise/actions/validate_mail.php
index 0023190b2e0..5da490bcac9 100755
--- a/apps/maarch_entreprise/actions/validate_mail.php
+++ b/apps/maarch_entreprise/actions/validate_mail.php
@@ -914,12 +914,11 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         $owner_usr_grp = $grp_status->group_id;
         $owner_basket_id = str_replace('_'.$_SESSION['current_basket']['basket_owner'], '', $_SESSION['current_basket']['id']);
     } else {
-        $owner_usr_grp = $_SESSION['user']['primarygroup'];
+        $owner_usr_grp = $_SESSION['current_basket']['group_id'];
         $owner_basket_id = $_SESSION['current_basket']['id'];
     }
-    $query = 'SELECT status_id, label_status FROM groupbasket_status left join '.$_SESSION['tablename']['status']
-        .' on status_id = id '
-        .' where basket_id= ? and group_id = ? and action_id = ?';
+    $query = 'SELECT status_id, label_status FROM groupbasket_status left join status on status_id = id '
+        .' WHERE basket_id= ? and (group_id = ?) and action_id = ? ORDER BY groupbasket_status.order';
     $stmt = $db->query($query, array($owner_basket_id, $owner_usr_grp, $id_action));
 
     if ($stmt->rowCount() > 0) {
@@ -959,9 +958,6 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         }
         for ($i = 0; $i < count($statuses); ++$i) {
             $frm_str .= '<option value="'.functions::xssafe($statuses[$i]['ID']).'" ';
-            if ($statuses[$i]['ID'] == 'NEW') {
-                $frm_str .= 'selected="selected"';
-            }
             $frm_str .= '>'.functions::xssafe($statuses[$i]['LABEL']).'</option>';
         }
         $frm_str .= '</select></td><td><span class="red_asterisk" id="market_mandatory" '
diff --git a/sql/develop.sql b/sql/develop.sql
index 08aa0c46c36..40e545a91b3 100644
--- a/sql/develop.sql
+++ b/sql/develop.sql
@@ -2,10 +2,6 @@
 DROP VIEW IF EXISTS res_view_letterbox;
 DROP VIEW IF EXISTS res_view_attachments;
 
-UPDATE actions_groupbaskets set used_in_basketlist = 'Y', used_in_action_page = 'Y' WHERE default_action_list = 'Y';
-UPDATE actions_groupbaskets set used_in_action_page = 'Y' WHERE used_in_basketlist = 'N' AND used_in_action_page = 'N';
-DELETE FROM usergroups_services WHERE service_id = 'view_baskets';
-
 DROP TABLE IF EXISTS contacts_groups;
 CREATE TABLE contacts_groups
 (
@@ -31,6 +27,15 @@ CREATE TABLE contacts_groups_lists
 )
 WITH (OIDS=FALSE);
 
+UPDATE actions_groupbaskets SET used_in_basketlist = 'Y', used_in_action_page = 'Y' WHERE default_action_list = 'Y';
+UPDATE actions_groupbaskets SET used_in_action_page = 'Y' WHERE used_in_basketlist = 'N' AND used_in_action_page = 'N';
+DELETE FROM usergroups_services WHERE service_id = 'view_baskets';
+ALTER TABLE groupbasket_status DROP COLUMN IF EXISTS "order";
+ALTER TABLE groupbasket_status ADD COLUMN "order" integer;
+UPDATE groupbasket_status SET "order" = 1;
+ALTER TABLE groupbasket_status ALTER COLUMN "order" SET NOT NULL;
+
+
 /* Docservers */
 ALTER TABLE docservers DROP COLUMN IF EXISTS docserver_location_id;
 ALTER TABLE docservers DROP COLUMN IF EXISTS ext_docserver_info;
diff --git a/sql/structure.sql b/sql/structure.sql
index 94a7d1dda26..d65cdc3c9b8 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -1491,6 +1491,7 @@ CREATE TABLE groupbasket_status
   basket_id character varying(32) NOT NULL,
   action_id integer NOT NULL,
   status_id character varying(32),
+  "order" integer NOT NULL,
   CONSTRAINT groupbasket_status_pkey PRIMARY KEY (system_id)
 )
 WITH (
diff --git a/src/app/basket/controllers/BasketController.php b/src/app/basket/controllers/BasketController.php
index 9ba9d229ab0..7c8240cdf0d 100644
--- a/src/app/basket/controllers/BasketController.php
+++ b/src/app/basket/controllers/BasketController.php
@@ -429,12 +429,13 @@ class BasketController
                 ]);
 
                 if (!empty($groupAction['statuses'])) {
-                    foreach ($groupAction['statuses'] as $status) {
+                    foreach ($groupAction['statuses'] as $key => $status) {
                         BasketModel::createGroupActionStatus([
                             'id'        => $aArgs['id'],
                             'groupId'   => $aArgs['groupId'],
                             'actionId'  => $groupAction['id'],
-                            'statusId'  => $status
+                            'statusId'  => $status,
+                            'order'     => $key
                         ]);
                     }
                 }
diff --git a/src/app/basket/models/BasketModelAbstract.php b/src/app/basket/models/BasketModelAbstract.php
index 733e8de57c5..a8c57a9a599 100644
--- a/src/app/basket/models/BasketModelAbstract.php
+++ b/src/app/basket/models/BasketModelAbstract.php
@@ -254,6 +254,7 @@ abstract class BasketModelAbstract
         ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionId', 'statusId']);
         ValidatorModel::stringType($aArgs, ['id', 'groupId', 'statusId']);
         ValidatorModel::intVal($aArgs, ['actionId']);
+        ValidatorModel::intType($aArgs, ['order']);
 
         DatabaseModel::insert([
             'table'         => 'groupbasket_status',
@@ -261,7 +262,8 @@ abstract class BasketModelAbstract
                 'action_id'     => $aArgs['actionId'],
                 'group_id'      => $aArgs['groupId'],
                 'basket_id'     => $aArgs['id'],
-                'status_id'     => $aArgs['statusId']
+                'status_id'     => $aArgs['statusId'],
+                '"order"'       => $aArgs['order']
             ]
         ]);
 
diff --git a/src/core/models/ValidatorModel.php b/src/core/models/ValidatorModel.php
index 03b28cc8129..c090005b0c4 100755
--- a/src/core/models/ValidatorModel.php
+++ b/src/core/models/ValidatorModel.php
@@ -33,7 +33,7 @@ class ValidatorModel
     public static function intVal(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-            if (empty($aArgs[$key])) {
+            if (!isset($aArgs[$key])) {
                 continue;
             }
             if (!Validator::intVal()->validate($aArgs[$key])) {
@@ -45,7 +45,7 @@ class ValidatorModel
     public static function intType(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-            if (empty($aArgs[$key])) {
+            if (!isset($aArgs[$key])) {
                 continue;
             }
             if (!Validator::intType()->validate($aArgs[$key])) {
@@ -57,7 +57,7 @@ class ValidatorModel
     public static function stringType(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-            if (empty($aArgs[$key])) {
+            if (!isset($aArgs[$key])) {
                 continue;
             }
             if (!Validator::stringType()->validate($aArgs[$key])) {
@@ -69,7 +69,7 @@ class ValidatorModel
     public static function arrayType(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-            if (empty($aArgs[$key])) {
+            if (!isset($aArgs[$key])) {
                 continue;
             }
             if (!Validator::arrayType()->validate($aArgs[$key])) {
@@ -81,7 +81,7 @@ class ValidatorModel
     public static function boolType(array $aArgs, $aKeys)
     {
         foreach ($aKeys as $key) {
-            if (empty($aArgs[$key])) {
+            if (!isset($aArgs[$key])) {
                 continue;
             }
             if (!Validator::boolType()->validate($aArgs[$key])) {
-- 
GitLab