From fb44c781536b5076304b3950fa93a42366300f5e Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Mon, 24 Sep 2018 16:24:30 +0200
Subject: [PATCH] FEAT #8338 add warn msg after MAILING action

---
 core/manage_action.php                        |   6 +-
 modules/visa/lang/en.php                      |   4 +
 modules/visa/lang/fr.php                      |   4 +
 modules/visa/send_signed_docs.php             | 115 +++++++------
 modules/visa/visa_workflow.php                | 151 +++++++++---------
 .../controllers/AttachmentController.php      |  26 ++-
 src/app/resource/models/ResModelAbstract.php  |  14 ++
 7 files changed, 187 insertions(+), 133 deletions(-)

diff --git a/core/manage_action.php b/core/manage_action.php
index f89b95ead6c..2ad84448f41 100755
--- a/core/manage_action.php
+++ b/core/manage_action.php
@@ -315,7 +315,11 @@ else if(empty($_POST['values']) || !isset($_POST['action_id']) || empty($_POST['
         }
         elseif( $_POST['req'] == 'first_request' && $confirm == true)
         {
-            echo "{status : 2, confirm_content : '".addslashes(_ACTION_CONFIRM." ".functions::xssafe($label_action))."', validate : '"._VALIDATE."', cancel : '"._CANCEL."', label_action : '".addslashes(functions::xssafe($label_action))."', 'action_status' : '".functions::xssafe($status)."'}";
+            if (!empty($warnMsg)) {
+                echo "{status : 2, confirm_content : '".addslashes("<p style='font-size: 12px;text-align: center;color: white;background: #F99830;padding: 5px;margin: -10px -10px 20px -10px;'><i class='fa fa-exclamation-triangle'></i> ".$warnMsg."<p>"._ACTION_CONFIRM." ".functions::xssafe($label_action))."', validate : '"._VALIDATE."', cancel : '"._CANCEL."', label_action : '".addslashes(functions::xssafe($label_action))."', 'action_status' : '".functions::xssafe($status)."'}";
+            } else {
+                echo "{status : 2, confirm_content : '".addslashes(_ACTION_CONFIRM." ".functions::xssafe($label_action))."', validate : '"._VALIDATE."', cancel : '"._CANCEL."', label_action : '".addslashes(functions::xssafe($label_action))."', 'action_status' : '".functions::xssafe($status)."'}";
+            }
             exit();
         }
         elseif( $_POST['req'] == 'first_request' && $confirm == false && $action_page == 'close_mail_with_attachment')
diff --git a/modules/visa/lang/en.php b/modules/visa/lang/en.php
index 8c14fd15e09..8d33473c19f 100755
--- a/modules/visa/lang/en.php
+++ b/modules/visa/lang/en.php
@@ -263,3 +263,7 @@ if (!defined('_USE_DATE_IN_SIGNBLOCK_DESC')) {
 if (!defined('_SELECT_MESSAGE_MODEL_IXBUS')) {
     define('_SELECT_MESSAGE_MODEL_IXBUS', "Choose a circuit");
 }
+
+if (!defined('_RESPONSES_WILL_BE_GENERATED')) {
+    define('_RESPONSES_WILL_BE_GENERATED', "answer(s) will be generated.");
+}
diff --git a/modules/visa/lang/fr.php b/modules/visa/lang/fr.php
index 78d18cc1a2c..de7a7ef4deb 100755
--- a/modules/visa/lang/fr.php
+++ b/modules/visa/lang/fr.php
@@ -413,3 +413,7 @@ if (!defined('_USE_DATE_IN_SIGNBLOCK_DESC')) {
 if (!defined('_SELECT_MESSAGE_MODEL_IXBUS')) {
     define('_SELECT_MESSAGE_MODEL_IXBUS', "Choisissez un circuit");
 }
+
+if (!defined('_RESPONSES_WILL_BE_GENERATED')) {
+    define('_RESPONSES_WILL_BE_GENERATED', "réponse(s) seront générée(s).");
+}
diff --git a/modules/visa/send_signed_docs.php b/modules/visa/send_signed_docs.php
index 3e9f56bb127..5322c5cf6a6 100755
--- a/modules/visa/send_signed_docs.php
+++ b/modules/visa/send_signed_docs.php
@@ -1,58 +1,57 @@
-<?php
-/**
-* Copyright Maarch since 2008 under licence GPLv3.
-* See LICENCE.txt file at the root folder for more details.
-* This file is part of Maarch software.
-
-* @brief   send_signed_docs
-* @author  dev <dev@maarch.org>
-* @ingroup visa
-*/
-
-require_once 'core' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'class_db.php';
-
-function checkAllSigned($id)
-{
-    $db = new Database();
-    $stmt = $db->query("SELECT status from res_view_attachments where attachment_type= ? and res_id_master = ?", array('response_project', $id));
-    while ($line = $stmt->fetchObject()) {
-        if ($line->status == 'TRA' || $line->status == 'A_TRA' ) {
-            return false;
-        }
-    }
-    return true;
-}
-
-require_once 'modules/visa/class/class_modules_tools.php';
-$visa = new visa();
-
-if ($visa->currentUserSignRequired($_SESSION['doc_id']) == 'true') {
-    $confirm = true;
-    $label_action .=" ("._NO_USER_SIGNED_DOC.")";
-} else {
-    $confirm = false;
-}
-$etapes = ['empty_error'];
- 
-/**
-* $etapes  array Contains only one etap, the status modification
-*/
- $etapes = array('empty_error');
- 
-function manage_empty_error($arr_id, $history, $id_action, $label_action, $status)
-{
-    $_SESSION['action_error'] = '';
-    $result = '';
-    $new_result = '';
-    for ($i=0; $i<count($arr_id);$i++) {
-
-        if (checkAllSigned($arr_id[$i]))
-            $new_result .= $arr_id[$i].',';
-        $result .= $arr_id[$i].'#';
-    }
-    $new_result = substr($new_result, 0, -1);
-
-    return array('result' => $result, 'history_msg' => '', 'newResultId' => $new_result, 'action_status' => $status);
-}
-
-?>
+<?php
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+
+* @brief   send_signed_docs
+* @author  dev <dev@maarch.org>
+* @ingroup visa
+*/
+
+require_once 'core' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'class_db.php';
+
+function checkAllSigned($id)
+{
+    $db = new Database();
+    $stmt = $db->query("SELECT status from res_view_attachments where attachment_type= ? and res_id_master = ?", array('response_project', $id));
+    while ($line = $stmt->fetchObject()) {
+        if ($line->status == 'TRA' || $line->status == 'A_TRA' ) {
+            return false;
+        }
+    }
+    return true;
+}
+
+require_once 'modules/visa/class/class_modules_tools.php';
+$visa = new visa();
+$warnMsg = '';
+
+if ($visa->currentUserSignRequired($_SESSION['doc_id']) == 'true') {
+    $confirm = true;
+    $warnMsg = _NO_USER_SIGNED_DOC;
+} else {
+    $confirm = false;
+}
+$etapes = ['empty_error'];
+ 
+/**
+* $etapes  array Contains only one etap, the status modification
+*/
+ $etapes = array('empty_error');
+ 
+function manage_empty_error($arr_id, $history, $id_action, $label_action, $status)
+{
+    $_SESSION['action_error'] = '';
+    $result = '';
+    $new_result = '';
+    for ($i=0; $i<count($arr_id); $i++) {
+        if (checkAllSigned($arr_id[$i])) {
+            $new_result .= $arr_id[$i].',';
+        }
+        $result .= $arr_id[$i].'#';
+    }
+    $new_result = substr($new_result, 0, -1);
+
+    return array('result' => $result, 'history_msg' => '', 'newResultId' => $new_result, 'action_status' => $status);
+}
diff --git a/modules/visa/visa_workflow.php b/modules/visa/visa_workflow.php
index 03b78f5f3b6..f687032dd76 100755
--- a/modules/visa/visa_workflow.php
+++ b/modules/visa/visa_workflow.php
@@ -1,73 +1,78 @@
-<?php
-/**
-* Copyright Maarch since 2008 under licence GPLv3.
-* See LICENCE.txt file at the root folder for more details.
-* This file is part of Maarch software.
-
-*
-* @brief   visa_workflow
-*
-* @author  dev <dev@maarch.org>
-* @ingroup visa
-*/
-require_once 'modules/visa/class/class_modules_tools.php';
-$visa = new visa();
-$confirm = true;
-
-$error_visa_workflow_signature_book = false;
-if ($visa->isAllAttachementSigned($_SESSION['doc_id']) == 'noAttachment') {
-    $error_visa_workflow_signature_book = true;
-} elseif ($visa->currentUserSignRequired($_SESSION['doc_id']) == 'true') {
-    $label_action .= ' ('._NO_USER_SIGNED_DOC.')';
-}
-
-$etapes = ['empty_error'];
-
-function manage_empty_error($arr_id, $history, $id_action, $label_action, $status)
-{
-    $db = new Database();
-    $result = '';
-
-    if (!empty($_SESSION['stockCheckbox'])) {
-        $arr_id = $_SESSION['stockCheckbox'];
-    }
-
-    for ($i = 0; $i < count($arr_id); ++$i) {
-        $_SESSION['action_error'] = '';
-        $coll_id = $_SESSION['current_basket']['coll_id'];
-        $res_id = $arr_id[$i];
-        include_once 'core/class/class_security.php';
-        $sec = new security();
-        include_once 'core/class/class_history.php';
-        $history = new history();
-        $table = $sec->retrieve_table_from_coll($coll_id);
-        $circuit_visa = new visa();
-        $sequence = $circuit_visa->getCurrentStep($res_id, $coll_id, 'VISA_CIRCUIT');
-        $stepDetails = array();
-        $stepDetails = $circuit_visa->getStepDetails($res_id, $coll_id, 'VISA_CIRCUIT', $sequence);
-
-        $message = $circuit_visa->processVisaWorkflow(['stepDetails' => $stepDetails, 'res_id' => $res_id]);
-
-        $stmt = $db->query('SELECT status FROM res_letterbox WHERE res_id = ?', array($res_id));
-        $resource = $stmt->fetchObject();
-        if ($resource->status == 'EVIS' || $resource->status == 'ESIG') {
-            $circuit_visa->setStatusVisa($res_id, 'letterbox_coll');
-        }
-
-        //USEFULL FOR SPM PARAM (can set SIGN WITH OTHER STATUS)
-        $stmt = $db->query(
-            'select count(1) as total FROM listinstance '
-            .' WHERE item_mode = ? AND res_id = ? AND difflist_type = ? AND requested_signature = ?',
-            array('sign', $res_id, 'VISA_CIRCUIT', true)
-        );
-        $res = $stmt->fetchObject();
-        if ($res->total > 0 && $circuit_visa->getCurrentStep($res_id, $coll_id, 'VISA_CIRCUIT') == $circuit_visa->nbVisa($res_id, $coll_id)) {
-            $mailStatus = 'ESIG';
-            $db->query('UPDATE res_letterbox SET status = ? WHERE res_id = ? ', [$mailStatus, $res_id]);
-        }
-
-        $result .= $arr_id[$i].'#';
-    }
-
-    return array('result' => $result, 'history_msg' => $message);
-}
+<?php
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+
+*
+* @brief   visa_workflow
+*
+* @author  dev <dev@maarch.org>
+* @ingroup visa
+*/
+require_once 'modules/visa/class/class_modules_tools.php';
+$visa = new visa();
+$confirm = true;
+$warnMsg = '';
+
+$error_visa_workflow_signature_book = false;
+$isMailingAttach = \Attachment\controllers\AttachmentController::isMailingAttach(["resIdMaster" => $_SESSION['doc_id']]);
+
+if ($visa->isAllAttachementSigned($_SESSION['doc_id']) == 'noAttachment') {
+    $error_visa_workflow_signature_book = true;
+} elseif ($visa->currentUserSignRequired($_SESSION['doc_id']) == 'true') {
+    $warnMsg = _NO_USER_SIGNED_DOC;
+} else if ($isMailingAttach != false) {
+    $warnMsg = $isMailingAttach['nbContacts'] . " " . _RESPONSES_WILL_BE_GENERATED;
+}
+
+$etapes = ['empty_error'];
+
+function manage_empty_error($arr_id, $history, $id_action, $label_action, $status)
+{
+    $db = new Database();
+    $result = '';
+
+    if (!empty($_SESSION['stockCheckbox'])) {
+        $arr_id = $_SESSION['stockCheckbox'];
+    }
+
+    for ($i = 0; $i < count($arr_id); ++$i) {
+        $_SESSION['action_error'] = '';
+        $coll_id = $_SESSION['current_basket']['coll_id'];
+        $res_id = $arr_id[$i];
+        include_once 'core/class/class_security.php';
+        $sec = new security();
+        include_once 'core/class/class_history.php';
+        $history = new history();
+        $table = $sec->retrieve_table_from_coll($coll_id);
+        $circuit_visa = new visa();
+        $sequence = $circuit_visa->getCurrentStep($res_id, $coll_id, 'VISA_CIRCUIT');
+        $stepDetails = array();
+        $stepDetails = $circuit_visa->getStepDetails($res_id, $coll_id, 'VISA_CIRCUIT', $sequence);
+
+        $message = $circuit_visa->processVisaWorkflow(['stepDetails' => $stepDetails, 'res_id' => $res_id]);
+
+        $stmt = $db->query('SELECT status FROM res_letterbox WHERE res_id = ?', array($res_id));
+        $resource = $stmt->fetchObject();
+        if ($resource->status == 'EVIS' || $resource->status == 'ESIG') {
+            $circuit_visa->setStatusVisa($res_id, 'letterbox_coll');
+        }
+
+        //USEFULL FOR SPM PARAM (can set SIGN WITH OTHER STATUS)
+        $stmt = $db->query(
+            'select count(1) as total FROM listinstance '
+            .' WHERE item_mode = ? AND res_id = ? AND difflist_type = ? AND requested_signature = ?',
+            array('sign', $res_id, 'VISA_CIRCUIT', true)
+        );
+        $res = $stmt->fetchObject();
+        if ($res->total > 0 && $circuit_visa->getCurrentStep($res_id, $coll_id, 'VISA_CIRCUIT') == $circuit_visa->nbVisa($res_id, $coll_id)) {
+            $mailStatus = 'ESIG';
+            $db->query('UPDATE res_letterbox SET status = ? WHERE res_id = ? ', [$mailStatus, $res_id]);
+        }
+
+        $result .= $arr_id[$i].'#';
+    }
+
+    return array('result' => $result, 'history_msg' => $message);
+}
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index bf92cb4c155..250ce2185be 100644
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -29,6 +29,7 @@ use SrcCore\models\CoreConfigModel;
 use Resource\controllers\StoreController;
 use Template\controllers\TemplateController;
 use SrcCore\models\DatabaseModel;
+use Resource\models\ResModel;
 
 class AttachmentController
 {
@@ -395,7 +396,30 @@ class AttachmentController
                 }
             }
         }
-
         return ['success' => 'success'];
     }
+
+    public function isMailingAttach(array $aArgs)
+    {
+        //TODO REMOVE SESSION AFTER V2
+        if (!Validator::intVal()->validate($aArgs['resIdMaster']) || !ResController::hasRightByResId(['resId' => $aArgs['resIdMaster'], 'userId' => $_SESSION['user']['UserId']])) {
+            return ['errors' => 'Document out of perimeter'];
+        }
+
+        $attachments = AttachmentModel::getOnView([
+            'select' => ['res_id'],
+            'where' => ['res_id_master = ?', 'status = ?'],
+            'data' => [$aArgs['resIdMaster'],'SEND_MASS']
+        ]);
+
+        $return['nbAttach'] = count($attachments);
+
+        if ($return['nbAttach'] == 0) {
+            return false;
+        }
+
+        $return['nbContacts'] = ResModel::getNbContactsByResId(["resId" => $aArgs['resIdMaster']]);
+        
+        return $return;
+    }
 }
diff --git a/src/app/resource/models/ResModelAbstract.php b/src/app/resource/models/ResModelAbstract.php
index 61529c26970..e43b661ef89 100644
--- a/src/app/resource/models/ResModelAbstract.php
+++ b/src/app/resource/models/ResModelAbstract.php
@@ -433,4 +433,18 @@ abstract class ResModelAbstract
 
         return $natures;
     }
+
+    public static function getNbContactsByResId(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['resId']);
+        ValidatorModel::intVal($aArgs, ['resId']);
+
+        $aResources = DatabaseModel::select([
+            'select'    => ['count(1) as nb_contacts'],
+            'table'     => ['contacts_res'],
+            'where'     => ['res_id = ?', 'mode = ?'],
+            'data'      => [$aArgs['resId'], 'multi']
+        ]);
+        return $aResources[0]['nb_contacts'];
+    }
 }
-- 
GitLab