From c62b4dc018fb62ced6370187d7d7ce28eae71ceb Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 4 Jul 2019 11:15:07 +0200 Subject: [PATCH] - Added new public method setMessageRead to system/messages/MessageClient - system/Recipient_model methods getReceivedMessages and getSentMessages now retrieve person_id from tbl_msg_status - Added new public method setMessageRead to CL/Messages_model - Improved code in public/js/messaging/read.js --- .../system/messages/MessageClient.php | 16 ++- application/models/CL/Messages_model.php | 79 ++++++++--- application/models/system/Recipient_model.php | 27 ++-- public/js/messaging/read.js | 128 +++++++++++++----- 4 files changed, 183 insertions(+), 67 deletions(-) diff --git a/application/controllers/system/messages/MessageClient.php b/application/controllers/system/messages/MessageClient.php index 1062202c1..4cc4c9f2a 100644 --- a/application/controllers/system/messages/MessageClient.php +++ b/application/controllers/system/messages/MessageClient.php @@ -12,7 +12,8 @@ class MessageClient extends Auth_Controller 'write' => array('basis/person:r'), 'listReceivedMessages' => array('basis/person:r'), 'listSentMessages' => array('basis/person:r'), - 'sendMessageToOU' => array('basis/person:r') + 'sendMessageToOU' => array('basis/person:r'), + 'setMessageRead' => array('basis/person:r') ) ); @@ -55,7 +56,7 @@ class MessageClient extends Auth_Controller } /** - * + * Sends a message to an organisation unit */ public function sendMessageToOU() { @@ -65,4 +66,15 @@ class MessageClient extends Auth_Controller $this->outputJson($this->CLMessagesModel->sendToOrganisationUnit($receiverOU, $subject, $body)); } + + /** + * Set a message as read + */ + public function setMessageRead() + { + $message_id = $this->input->post('message_id'); + $statusPersonId = $this->input->post('statusPersonId'); + + $this->outputJson($this->CLMessagesModel->setMessageRead($message_id, $statusPersonId)); + } } diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index 37b244038..e907022aa 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -2,13 +2,23 @@ /** * Messages GUI logic - * This model extends CI_Model because here is just implemented logic - * It does not represent a resource (ex. like models that extend DB_Model) + * - This model extends CI_Model because here is just implemented logic + * - It does not represent a resource (ex. like models that extend DB_Model) */ class Messages_model extends CI_Model { - const REPLY_SUBJECT_PREFIX = 'Re: '; - const REPLY_BODY_FORMAT = '

On %s %s %s wrote:
%s
'; + const REPLY_SUBJECT_PREFIX = 'Re: '; // reply subject prefix + // To quote a reply body message + const REPLY_BODY_FORMAT = '
+
+
+ + On %s %s %s wrote: + +
+
+ %s +
'; const NO_AUTH_UID = 'online'; // hard coded uid if no authentication is performed @@ -38,7 +48,28 @@ class Messages_model extends CI_Model // Public methods /** - * + * Set a message as read by its id ($message_id + $person_id) + */ + public function setMessageRead($message_id, $person_id) + { + // Checks parameters + if (!is_numeric($message_id) || !is_numeric($person_id)) return error('Invalid setMessageRead parameters'); + + // Loads needed models + $this->load->model('system/MsgStatus_model', 'MsgStatusModel'); + + // Set date used to insert + $statusData = array( + 'message_id' => $message_id, + 'person_id' => $person_id, + 'status' => MSG_STATUS_READ + ); + + return $this->MsgStatusModel->insert($statusData); // insert and return result + } + + /** + * Prepares data for the view system/messages/ajaxWrite */ public function prepareAjaxWrite() { @@ -60,26 +91,31 @@ class Messages_model extends CI_Model } /** - * + * Prepares data for the view system/messages/ajaxRead + * If everything is fine returns a list of received messages (objects) */ public function prepareAjaxReadReceived() { - $jsonResult = error('Something did not go as it should'); - + // Name and surname of the logged user $loggedUserName = getAuthFirstname().' '.getAuthSurname(); + // If empty then use a hard coded one if (isEmptyString($loggedUserName)) $loggedUserName = 'Me'; + // Retrieves received messages for the logged user and its organisation units $receivedMessagesResult = $this->RecipientModel->getReceivedMessages( getAuthPersonId(), $this->config->item(MessageLib::CFG_OU_RECEIVERS) ); + // If an error occurred return it if (isError($receivedMessagesResult)) return $receivedMessagesResult; + // If data were found if (hasData($receivedMessagesResult)) { - $jsonArray = array(); + $jsonArray = array(); // array that contains all the received messages + // Collect'em all in the array $jsonArray foreach (getData($receivedMessagesResult) as $receivedMessage) { $jsonRecord = new stdClass(); @@ -90,34 +126,38 @@ class Messages_model extends CI_Model $sentDate = new DateTime($receivedMessage->sent); $jsonRecord->sent = $sentDate->format('d/m/Y H:i:s'); $jsonRecord->status = $receivedMessage->status; + $jsonRecord->statusPersonId = $receivedMessage->statuspersonid; $jsonArray[] = $jsonRecord; } - $jsonResult = success(json_encode($jsonArray)); + return success(json_encode($jsonArray)); // return as an json encoded string } - return $jsonResult; + return success('No messages were found'); // NOT a blocking error } /** - * + * Prepares data for the view system/messages/ajaxRead + * If everything is fine returns a list of sent messages (objects) */ public function prepareAjaxReadSent() { - $jsonResult = error('Something did not go as it should'); - + // Name and surname of the logged user $loggedUserName = getAuthFirstname().' '.getAuthSurname(); + // If empty then use a hard coded one if (isEmptyString($loggedUserName)) $loggedUserName = 'Me'; + // Retrieves sent messages from the logged user $sentMessagesResult = $this->RecipientModel->getSentMessages(getAuthPersonId()); - if (isError($sentMessagesResult)) return $sentMessagesResult; + if (isError($sentMessagesResult)) return $sentMessagesResult; // If an error occurred return it if (hasData($sentMessagesResult)) { - $jsonArray = array(); + $jsonArray = array();// array that contains all the sent messages + // Collect'em all in the array $jsonArray foreach (getData($sentMessagesResult) as $sentMessage) { $jsonRecord = new stdClass(); @@ -128,14 +168,15 @@ class Messages_model extends CI_Model $sentDate = new DateTime($sentMessage->sent); $jsonRecord->sent = $sentDate->format('d/m/Y H:i:s'); $jsonRecord->status = $sentMessage->status; + $jsonRecord->statusPersonId = $sentMessage->statuspersonid; $jsonArray[] = $jsonRecord; } - $jsonResult = success(json_encode($jsonArray)); + return success(json_encode($jsonArray)); // return as an json encoded string } - return $jsonResult; + return success('No messages were found'); // NOT a blocking error } /** @@ -540,7 +581,7 @@ class Messages_model extends CI_Model } /** - * + * Quotes the previous message body */ private function _getReplyBody($body, $receiverName, $receiverSurname, $sentDate) { diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 8a3ee1aa6..46cf0e36f 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -301,18 +301,21 @@ class Recipient_model extends DB_Model } /** - * + * - Gets the directly recieved messages using the given person id + * - Gets the recieved messages from an organisation unit where this person plays a role given by the parameter functions */ public function getReceivedMessages($person_id, $functions) { - $sql = 'SELECT mr.message_id, + $sql = '-- Messages sent directly to the person + SELECT mr.message_id, mm.relationmessage_id, mm.subject, mm.body, mr.sent AS sent, p.vorname, p.nachname, - MAX(ms.status) AS status + MAX(ms.status) AS status, + ms.person_id AS statusPersonId FROM public.tbl_msg_recipient mr JOIN public.tbl_msg_message mm ON (mm.message_id = mr.message_id) JOIN public.tbl_msg_status ms ON (ms.message_id = mr.message_id AND ms.person_id = mr.person_id) @@ -326,8 +329,10 @@ class Recipient_model extends DB_Model mm.body, mr.sent, p.vorname, - p.nachname + p.nachname, + ms.person_id UNION + -- Messages sent to a person that belongs to the recipient organisation unit SELECT mrou.message_id, mm.relationmessage_id, mm.subject, @@ -335,7 +340,8 @@ class Recipient_model extends DB_Model mrou.sent AS sent, pr.vorname, pr.nachname, - MAX(ms.status) AS status + MAX(ms.status) AS status, + ms.person_id AS statusPersonId FROM public.tbl_person p JOIN public.tbl_benutzer b ON (b.person_id = p.person_id) JOIN (SELECT uid, oe_kurzbz FROM public.tbl_benutzerfunktion WHERE funktion_kurzbz IN ?) bf ON (bf.uid = b.uid) @@ -352,14 +358,15 @@ class Recipient_model extends DB_Model mm.body, mrou.sent, pr.vorname, - pr.nachname + pr.nachname, + ms.person_id ORDER BY sent DESC'; return $this->execQuery($sql, array($person_id, $functions, $person_id)); } /** - * + * Gets all the sent message by the given person */ public function getSentMessages($person_id) { @@ -370,7 +377,8 @@ class Recipient_model extends DB_Model mr.sent, p.vorname, p.nachname, - MAX(ms.status) AS status + MAX(ms.status) AS status, + ms.person_id AS statusPersonId FROM public.tbl_msg_message mm JOIN public.tbl_msg_recipient mr ON (mr.message_id = mm.message_id) JOIN public.tbl_msg_status ms ON (ms.message_id = mm.message_id AND mr.person_id = mr.person_id) @@ -384,7 +392,8 @@ class Recipient_model extends DB_Model mm.body, mr.sent, p.vorname, - p.nachname + p.nachname, + ms.person_id ORDER BY mr.sent DESC'; return $this->execQuery($sql, array($person_id)); diff --git a/public/js/messaging/read.js b/public/js/messaging/read.js index 348e6b3ed..99426bffa 100644 --- a/public/js/messaging/read.js +++ b/public/js/messaging/read.js @@ -2,35 +2,108 @@ // List all personal messages, used by view system/messages/ajaxRead // *********************************************************************** -// +// Global variable that contains tha tabulator instance var tableMessageLst; /** - * + * Use DialogLib to display a Generic error */ -function genericError() +function readMessagesGenericError() { FHC_DialogLib.alertError("An error occurred while retrieving message, contact the website administrator"); } /** - * + * Gets all the received messages */ function getReceivedMessages() { - _getMessages(FHC_JS_DATA_STORAGE_OBJECT.called_path + '/listReceivedMessages'); + _getMessages(FHC_JS_DATA_STORAGE_OBJECT.called_path + "/listReceivedMessages"); } /** - * + * Gets all the sent messages */ function getSentMessages() { - _getMessages(FHC_JS_DATA_STORAGE_OBJECT.called_path + '/listSentMessages'); + _getMessages(FHC_JS_DATA_STORAGE_OBJECT.called_path + "/listSentMessages"); } /** - * + * Change the TinyMCE content + */ +function changeTinyMCE(row) +{ + tinyMCE.get("readMessagePanel").setContent(row.getData().body); +} + +/** + * Called on Tabulator row click event + * - If a clicked message is unread thes is set as read + * - Change the TinyMCE content with the clicked message body + */ +function rowClick(e, row) +{ + // If the message is unread + if (row.getData().status == "0") + { + FHC_AjaxClient.ajaxCallPost( + FHC_JS_DATA_STORAGE_OBJECT.called_path + "/setMessageRead", + { + message_id: row.getData().message_id, + statusPersonId: row.getData().statusPersonId + }, + { + successCallback: function(data, textStatus, jqXHR) { + + if (FHC_AjaxClient.isSuccess(data)) + { + rowFormatter(row, "normal"); + } + else + { + readMessagesGenericError(); + } + }, + errorCallback: readMessagesGenericError, + veilTimeout: 300 + } + ); + } + + changeTinyMCE(row); // Change TinyMCE content +} + +/** + * Radio button click event to switch between received and sent messages + */ +function toggleMessages() +{ + // + if ($(this)[0].className.search("active") == -1) + { + $(this)[0].id == "r" ? getReceivedMessages() : getSentMessages(); + } +} + +/** + * Formats tabulator rows + */ +function rowFormatter(row, fontWeight = 700) +{ + if (row.getData().status == "0") + { + var cells = row.getElement().childNodes; + + for (var i = 0; i < cells.length; i++) + { + cells[i].style.fontWeight = fontWeight; + } + } +} + +/** + * Get received/sent messages and change tabulator content */ function _getMessages(getMessagesURL) { @@ -48,42 +121,22 @@ function _getMessages(getMessagesURL) } catch (syntaxError) { - genericError(); + readMessagesGenericError(); } } }, - errorCallback: genericError, + errorCallback: readMessagesGenericError, veilTimeout: 300 } ); } /** - * - */ -function changeTinyMCE(e, row) -{ - tinyMCE.get("readMessagePanel").setContent(row._row.data.body); -} - -/** - * - */ -function toggleMessages() -{ - // - if ($(this)[0].className.search('active') == -1) - { - $(this)[0].id == 'r' ? getReceivedMessages() : getSentMessages(); - } -} - -/** - * + * Start! */ $(document).ready(function () { - // + // TinyMCE initialization tinymce.init({ selector: "#readMessagePanel", plugins: "autoresize", @@ -95,7 +148,7 @@ $(document).ready(function () { autoresize_bottom_margin: 0 }); - // + // Tabulator initialization tableMessageLst = new Tabulator("#lstMessagesPanel", { height: "400px", pagination: "local", @@ -105,13 +158,14 @@ $(document).ready(function () { {title: "To", field: "to", width: 400}, {title: "Date", field: "sent", sorter: "datetime", width: 150} ], - rowClick: changeTinyMCE + rowClick: rowClick, + rowFormatter: rowFormatter }); - // - $('.toggleMessages .btn').click(toggleMessages); + // Bind radio buttons click event with toggleMessages function + $(".toggleMessages .btn").click(toggleMessages); - // + // First retrieve the received message and populate the tabulator getReceivedMessages(); });