From 178ce6afb63cc8794cd27c05b39a25e8c1941442 Mon Sep 17 00:00:00 2001
From: Alex Orluc <alex.orluc@maarch.org>
Date: Wed, 21 Mar 2018 15:07:17 +0100
Subject: [PATCH] FEAT #5966 merge from MCC_dev_initiator

---
 apps/maarch_entreprise/actions/index_mlb.php  | 44 +++++++++++++++--
 .../actions/validate_mail.php                 | 48 +++++++++++++++++--
 apps/maarch_entreprise/xml/export.xml         | 16 +++++--
 core/class/ExportControler.php                | 35 ++++++++++++++
 4 files changed, 132 insertions(+), 11 deletions(-)

diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index 59d7f7411f5..2ffd816f150 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -681,13 +681,37 @@ function get_form_txt($values, $pathManageAction, $actionId, $table, $module, $c
             .'style="display:inline;"><i class="fa fa-star"></i></span>&nbsp;</td>';
     $frmStr .= '</tr>';
     $frmStr .= '<script>$j("#nature_id").chosen({width: "226px", disable_search_threshold: 10, search_contains: true});</script>';
+
     /****** RECOMMANDE ******/
     $frmStr .= '<tr id="reference_number_tr" style="display:none;">';
-    $frmStr .= '<td ><label for="reference_number" class="form_title" >'._MONITORING_NUMBER.'</label></td>';
+    $frmStr .= '<td><label for="reference_number" class="form_title" >'._MONITORING_NUMBER.'</label></td>';
     $frmStr .= '<td>&nbsp;</td>';
-    $frmStr .= '<td><input type="text" name="reference_number" id="reference_number"/></td>';
+    $frmStr .= '<td class="indexing_field"><input type="text" name="reference_number" id="reference_number"/></td>';
     $frmStr .= '</tr>';
 
+    /*** Initiator ***/
+    $frmStr .= '<tr id="initiator_tr" style="display:'
+        .$displayValue.';">';
+    $frmStr .= '<td><label for="intitiator" class="form_title" >'
+            ._INITIATOR.'</label></td>';
+    $frmStr .= '<td>&nbsp;</td>';
+    $frmStr .= '<td class="indexing_field">'
+            .'<select name="initiator" id="initiator">';
+    foreach ($_SESSION['user']['entities'] as $entity) {
+        $frmStr .= '<option value="'.$entity['ENTITY_ID'].'"';
+        if ($_SESSION['user']['primaryentity']['id'] == $entity['ENTITY_ID']) {
+            $frmStr .= ' selected="selected" ';
+        }
+        $frmStr .= '>'.$entity['ENTITY_LABEL'].'</option>';
+    }
+    $frmStr .= '</select>'
+            .'</td>';
+    $frmStr .= '<td><span class="red_asterisk" '
+            .'id="process_limit_date_use_mandatory" style="display:inline;"><i class="fa fa-star"></i>'
+            .'</span>&nbsp;</td>';
+    $frmStr .= '</tr>';
+    $frmStr .= '<script>$j("#initiator").chosen({width: "226px", disable_search_threshold: 10, search_contains: true});</script>';
+
     /*** Subject ***/
     $frmStr .= '<tr id="subject_tr" style="display:'.$displayValue.';">';
     $frmStr .= '<td><label for="subject" class="form_title" >'._SUBJECT
@@ -1492,15 +1516,27 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
     }
 
     //store the initiator entity
-    if (isset($_SESSION['user']['primaryentity']['id'])) {
+    $initiator = get_value_fields($formValues, 'initiator');
+    if (!empty($initiator)) {
         array_push(
             $_SESSION['data'],
             array(
                 'column' => 'initiator',
-                'value' => $_SESSION['user']['primaryentity']['id'],
+                'value' => $initiator,
                 'type' => 'string',
             )
         );
+    } else {
+        if (isset($_SESSION['user']['primaryentity']['id'])) {
+            array_push(
+                $_SESSION['data'],
+                array(
+                    'column' => 'initiator',
+                    'value' => $_SESSION['user']['primaryentity']['id'],
+                    'type' => 'string',
+                )
+            );
+        }
     }
     $status_id = get_value_fields($formValues, 'status');
     if (empty($status_id) || $status_id === '') {
diff --git a/apps/maarch_entreprise/actions/validate_mail.php b/apps/maarch_entreprise/actions/validate_mail.php
index d1b01bb66b0..0ac46c49908 100755
--- a/apps/maarch_entreprise/actions/validate_mail.php
+++ b/apps/maarch_entreprise/actions/validate_mail.php
@@ -101,11 +101,12 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
     $data = get_general_data($coll_id, $res_id, 'minimal');
     $_SESSION['category_id'] = $data['category_id']['value'];
     $view = $sec->retrieve_view_from_coll_id($coll_id);
-    $stmt = $db->query('SELECT alt_identifier, creation_date FROM '.$view.' WHERE res_id = ?', array($res_id));
+    $stmt = $db->query('SELECT initiator, alt_identifier, creation_date FROM '.$view.' WHERE res_id = ?', array($res_id));
     $resChrono = $stmt->fetchObject();
     $chrono_number = explode('/', $resChrono->alt_identifier);
     $chrono_number = $chrono_number[1];
     $creation_date = functions::format_date_db($resChrono->creation_date, false);
+    $initiator = $resChrono->initiator;
 
     //LAUNCH DOCLOCKER
     $docLockerCustomPath = 'apps/maarch_entreprise/actions/docLocker.php';
@@ -796,6 +797,35 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
     $frm_str .= '/></td>';
     $frm_str .= '</tr>';
 
+    /*** Initiator ***/
+    $frm_str .= '<tr id="initiator_tr" style="display:'.$displayValue.';">';
+    $frm_str .= '<td><label for="intitiator" class="form_title" >'
+            ._INITIATOR.'</label></td>';
+    $frm_str .= '<td>&nbsp;</td>';
+    $frm_str .= '<td class="indexing_field">'
+            .'<select name="initiator" id="initiator">';
+    if ($initiator) {
+        $frm_str .= '<optgroup label="Service initiateur actuel">';
+        $frm_str .= '<option value="'.$initiator.'">'.$ent->getentitylabel($initiator).'</option>';
+        $frm_str .= '</optgroup>';
+    }
+    $frm_str .= '<optgroup label="Autre(s) service(s) disponible">';
+    foreach ($_SESSION['user']['entities'] as $entity) {
+        $frm_str .= '<option value="'.$entity['ENTITY_ID'].'"';
+        if ($_SESSION['user']['primaryentity']['id'] == $entity['ENTITY_ID'] && (empty($initiator) || $initiator == null)) {
+            $frm_str .= ' selected="selected" ';
+        }
+        $frm_str .= '>'.$entity['ENTITY_LABEL'].'</option>';
+    }
+    $frm_str .= '</optgroup>';
+    $frm_str .= '</select>'
+            .'</td>';
+    $frm_str .= '<td><span class="red_asterisk" '
+            .'id="initiator_mandatory" style="display:inline;"><i class="fa fa-star"></i>'
+            .'</span>&nbsp;</td>';
+    $frm_str .= '</tr>';
+    $frm_str .= '<script>$j("#initiator").chosen({width: "226px", disable_search_threshold: 10, search_contains: true});</script>';
+
     /*** Subject ***/
     $frm_str .= '<tr id="subject_tr" style="display:'.$display_value.';">';
     $frm_str .= '<td class="indexing_label"><label for="subject" class="form_title" >'._SUBJECT.'</label></td>';
@@ -1770,6 +1800,18 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
         }
     }
 
+    //store the initiator entity
+    $initiator = get_value_fields($values, 'initiator');
+    if (!empty($initiator)) {
+        $query_res .= ', initiator = ?';
+        $arrayPDOres = array_merge($arrayPDOres, array($initiator));
+    } else {
+        if (isset($_SESSION['user']['primaryentity']['id'])) {
+            $query_res .= ', initiator = ?';
+            $arrayPDOres = array_merge($arrayPDOres, array($_SESSION['user']['primaryentity']['id']));
+        }
+    }
+
     $query_res = preg_replace('/set ,/', 'set ', $query_res);
     //$query_res = substr($query_res, strpos($query_string, ','));
 
@@ -1856,8 +1898,8 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
             }
         }
     } elseif ($cat_id == 'attachment') {
-        require'modules/attachments/add_attachments.php';                     //      NCH01
-        require'modules/attachments/remove_letterbox.php';
+        require 'modules/attachments/add_attachments.php';                     //      NCH01
+        require 'modules/attachments/remove_letterbox.php';
     }
 
     //$_SESSION['indexing'] = array();
diff --git a/apps/maarch_entreprise/xml/export.xml b/apps/maarch_entreprise/xml/export.xml
index d790116c406..26968e68970 100755
--- a/apps/maarch_entreprise/xml/export.xml
+++ b/apps/maarch_entreprise/xml/export.xml
@@ -70,6 +70,14 @@
                 <LIBELLE>Thème</LIBELLE>
                 <CALL>get_parent_folder</CALL>
             </FUNCTION>
+            <FUNCTION>
+                <LIBELLE>Catégorie</LIBELLE>
+                <CALL>get_category_label</CALL>
+            </FUNCTION>
+            <FUNCTION>
+                <LIBELLE>Service versant</LIBELLE>
+                <CALL>get_entity_initiator_short_label</CALL>
+            </FUNCTION>
             <FUNCTION>
                 <LIBELLE>Service destinataire</LIBELLE>
                 <CALL>get_entity_dest_short_label</CALL>
@@ -83,12 +91,12 @@
                 <CALL>get_contact_civility</CALL>
             </FUNCTION>
             <FUNCTION>
-                <LIBELLE>Mots clés</LIBELLE>
-                <CALL>get_tags</CALL>
+                <LIBELLE>Fonction du contact</LIBELLE>
+                <CALL>get_contact_function</CALL>
             </FUNCTION>
             <FUNCTION>
-                <LIBELLE>Nature de la réponse</LIBELLE>
-                <CALL>get_answer_nature</CALL>
+                <LIBELLE>Mots clés</LIBELLE>
+                <CALL>get_tags</CALL>
             </FUNCTION>
             <FUNCTION>
                 <LIBELLE>Signataire</LIBELLE>
diff --git a/core/class/ExportControler.php b/core/class/ExportControler.php
index b63ebb9c8ca..6974f85c2c3 100755
--- a/core/class/ExportControler.php
+++ b/core/class/ExportControler.php
@@ -358,6 +358,30 @@ class ExportFunctions
         $this->object_export->{$this->pos}->get_contact_civility = $_SESSION['mail_titles'][$result->title];
     }
 
+    public function get_contact_function($libelle, $res_id)
+    {
+        $db = new Database();
+
+        $query = 'SELECT c.function FROM mlb_coll_ext r LEFT JOIN contacts_v2 c ON c.contact_id = r.dest_contact_id WHERE r.res_id = ?';
+        $stmt = $db->query($query, array($res_id));
+        $result = $stmt->fetchObject();
+
+        $this->object_export->{$this->pos}->get_contact_function = $result->function;
+    }
+
+    public function get_entity_initiator_short_label($libelle)
+    {
+        require_once 'modules/entities/class/class_manage_entities.php';
+        $db = new Database();
+        $entities = new entity();
+
+        $query = 'SELECT initiator FROM res_letterbox r WHERE r.res_id = ?';
+        $stmt = $db->query($query, array($res_id));
+        $result = $stmt->fetchObject();
+
+        $this->object_export->{$this->pos}->get_entity_initiator_short_label = $entities->getentityshortlabel($result->initiator);
+    }
+
     public function get_entity_dest_short_label($libelle, $res_id)
     {
         require_once 'modules/entities/class/class_manage_entities.php';
@@ -411,4 +435,15 @@ class ExportFunctions
 
         $this->object_export->{$this->pos}->get_parent_folder = $result->folder_name;
     }
+
+    public function get_category_label($libelle, $res_id)
+    {
+        $db = new Database();
+
+        $query = 'SELECT category_id FROM mlb_coll_ext WHERE res_id = ?';
+        $stmt = $db->query($query, array($res_id));
+        $result = $stmt->fetchObject();
+
+        $this->object_export->{$this->pos}->get_category_label = $_SESSION['coll_categories']['letterbox_coll'][$result->category_id];
+    }
 }
-- 
GitLab