diff --git a/application/helpers/hlp_authentication_helper.php b/application/helpers/hlp_authentication_helper.php index 740823ff9..459c7fb80 100644 --- a/application/helpers/hlp_authentication_helper.php +++ b/application/helpers/hlp_authentication_helper.php @@ -20,7 +20,6 @@ function getAuthPersonId() return isLogged() ? ($ci->authlib->getAuthObj())->{AuthLib::AO_PERSON_ID} : null; } - /** * If the user is NOT logged then a null value is returned. * If the user is alredy logged, then it is possible to access to the authentication object @@ -34,3 +33,31 @@ function getAuthUID() return isLogged() ? ($ci->authlib->getAuthObj())->{AuthLib::AO_USERNAME} : null; } + +/** + * If the user is NOT logged then a null value is returned. + * If the user is alredy logged, then it is possible to access to the authentication object + * that contains the firstname of the logged user + * NOTE: if the user is logged with a "foreign" method (ex. Bewerbungstool), + * then it is possible that the firstname is null! + */ +function getAuthFirstname() +{ + $ci =& get_instance(); // get CI instance + + return isLogged() ? ($ci->authlib->getAuthObj())->{AuthLib::AO_NAME} : null; +} + +/** + * If the user is NOT logged then a null value is returned. + * If the user is alredy logged, then it is possible to access to the authentication object + * that contains the surname of the logged user + * NOTE: if the user is logged with a "foreign" method (ex. Bewerbungstool), + * then it is possible that the surname is null! + */ +function getAuthSurname() +{ + $ci =& get_instance(); // get CI instance + + return isLogged() ? ($ci->authlib->getAuthObj())->{AuthLib::AO_SURNAME} : null; +} diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index 302e51340..21cba1d68 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -36,6 +36,67 @@ class Messages_model extends CI_Model //------------------------------------------------------------------------------------------------------------------ // Public methods + /** + * + */ + public function prepareAjaxRead() + { + $jsonNestedData = error('Something did not go as it should'); + + $loggedUserName = getAuthFirstname().' '.getAuthSurname(); + + if (isEmptyString($loggedUserName)) $loggedUserName = 'Me'; + + $receivedMessagesResult = $this->RecipientModel->getReceivedMessages(getAuthPersonId()); + if (isError($receivedMessagesResult)) return $receivedMessagesResult; + + $sentMessagesResult = $this->RecipientModel->getSentMessages(getAuthPersonId()); + if (isError($sentMessagesResult)) return $sentMessagesResult; + + if (hasData($receivedMessagesResult)) + { + $jsonArray = array(); + + foreach (getData($receivedMessagesResult) as $receivedMessage) + { + $jsonRecord = new stdClass(); + $jsonRecord->subject = $receivedMessage->subject; + $jsonRecord->from = $receivedMessage->vorname.' '.$receivedMessage->nachname; + $sentDate = new DateTime($receivedMessage->sent); + $jsonRecord->sent = $sentDate->format('d/m/Y H:i:s'); + $jsonRecord->status = $receivedMessage->status; + + if (hasData($sentMessagesResult)) + { + $jsonChildrenArray = array(); + + foreach (getData($sentMessagesResult) as $sentMessage) + { + if ($receivedMessage->relationmessage_id == $sentMessage->message_id) + { + $jsonChildrenRecord = new stdClass(); + $jsonChildrenRecord->subject = $sentMessage->subject; + $jsonChildrenRecord->from = $loggedUserName; + $sentDate = new DateTime($sentMessage->sent); + $jsonChildrenRecord->sent = $sentDate->format('d/m/Y H:i:s'); + $jsonChildrenRecord->status = $sentMessage->status; + + $jsonChildrenArray[] = $jsonChildrenRecord; + } + } + + if (!isEmptyArray($jsonChildrenArray)) $jsonRecord->_children = $jsonChildrenArray; + } + + $jsonArray[] = $jsonRecord; + } + + $jsonNestedData = success(json_encode($jsonArray)); + } + + return $jsonNestedData; + } + /** * Prepares data for the view system/messages/htmlRead using a token that identifies a single message */ @@ -148,11 +209,8 @@ class Messages_model extends CI_Model public function sendImplicitTemplate($persons, $subject, $body, $relationmessage_id = null) { // Retrieves the sender id - $authUser = $this->_getAuthUser(); - if (isError($authUser)) show_error(getData($authUser)); - if (!hasData($authUser)) show_error('The current logged user person_id is not defined'); - - $sender_id = getData($authUser)[0]->person_id; + $sender_id = getAuthPersonId(); + if (!is_numeric($sender_id)) show_error('The current logged user person_id is not defined'); // Retrieves message vars data for the given user/s $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($persons); @@ -194,11 +252,8 @@ class Messages_model extends CI_Model public function sendExplicitTemplate($prestudents, $oe_kurzbz, $vorlage_kurzbz, $msgVars) { // Retrieves the sender id - $authUser = $this->_getAuthUser(); - if (isError($authUser)) show_error(getData($authUser)); - if (!hasData($authUser)) show_error('The current logged user person_id is not defined'); - - $sender_id = getData($authUser)[0]->person_id; + $sender_id = getAuthPersonId(); + if (!is_numeric($sender_id)) show_error('The current logged user person_id is not defined'); // Retrieves message vars data for the given user/s $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudents); @@ -356,17 +411,6 @@ class Messages_model extends CI_Model //------------------------------------------------------------------------------------------------------------------ // Private methods - /** - * Returns the current authenticated person object - */ - private function _getAuthUser() - { - $this->load->model('person/Person_model', 'PersonModel'); - $authUser = $this->PersonModel->getByUid(getAuthUID()); - - return $authUser; - } - /** * Replaces data array keys to a lowercase string with underscores instead of spaces */ @@ -407,7 +451,7 @@ class Messages_model extends CI_Model { // In case the message is accessed via ViewMessage controller -> no authentication // If no authentication is performed then use a hard coded uid - $loggedUserUID = function_exists('getAuthUID') ? getAuthUID() : self::NO_AUTH_UID; + $loggedUserUID = isLogged() ? getAuthUID() : self::NO_AUTH_UID; return $this->personloglib->log( $receiver_id, @@ -508,11 +552,8 @@ class Messages_model extends CI_Model // --------------------------------------------------------------------------------------- // Retrieves the sender id - $authUser = $this->_getAuthUser(); - if (isError($authUser)) show_error(getData($authUser)); - if (!hasData($authUser)) show_error('The current logged user person_id is not defined'); - - $sender_id = getData($authUser)[0]->person_id; + $sender_id = getAuthPersonId(); + if (!is_numeric($sender_id)) show_error('The current logged user person_id is not defined'); // --------------------------------------------------------------------------------------- // Organisation units and a boolean (true if the sender is administrator) are used to get the templates diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 65812580e..8d1162de5 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -299,4 +299,63 @@ class Recipient_model extends DB_Model return $this->execQuery($sql, $parametersArray); } + + /** + * + */ + public function getReceivedMessages($person_id) + { + $sql = 'SELECT mr.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mr.sent, + p.vorname, + p.nachname, + MAX(ms.status) AS status + 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) + JOIN public.tbl_person p ON (p.person_id = mm.person_id) + WHERE mr.person_id = ? + AND mr.sent IS NOT NULL + AND mr.sentinfo IS NULL + GROUP BY mr.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mr.sent, + p.vorname, + p.nachname + ORDER BY mr.sent DESC'; + + return $this->execQuery($sql, array($person_id)); + } + + /** + * + */ + public function getSentMessages($person_id) + { + $sql = 'SELECT mm.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mr.sent, + MAX(ms.status) AS status + 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) + WHERE mm.person_id = ? + AND mr.sent IS NOT NULL + AND mr.sentinfo IS NULL + GROUP BY mm.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mr.sent + ORDER BY mr.sent DESC'; + + return $this->execQuery($sql, array($person_id)); + } } diff --git a/application/views/system/messages/ajaxRead.php b/application/views/system/messages/ajaxRead.php index 97f16ce3b..e3ff133f0 100644 --- a/application/views/system/messages/ajaxRead.php +++ b/application/views/system/messages/ajaxRead.php @@ -4,10 +4,12 @@ array( 'title' => 'Read personal messages', 'jquery' => true, + 'jqueryui' => true, 'bootstrap' => true, 'momentjs' => true, 'tabulator' => true, 'ajaxlib' => true, + 'dialoglib' => true, 'customJSs' => array('public/js/messaging/messageClient.js') ) ); diff --git a/public/js/messaging/messageClient.js b/public/js/messaging/messageClient.js new file mode 100644 index 000000000..850777ea9 --- /dev/null +++ b/public/js/messaging/messageClient.js @@ -0,0 +1,53 @@ +/** + * + */ +$(document).ready(function () { + + FHC_AjaxClient.ajaxCallGet( + FHC_JS_DATA_STORAGE_OBJECT.called_path + '/listMessages', + null, + { + successCallback: function(data, textStatus, jqXHR) { + + if (FHC_AjaxClient.hasData(data)) + { + try + { + var jsonMessageLst = JSON.parse(FHC_AjaxClient.getData(data)); + + console.log(jsonMessageLst); + + var tableMessageLst = new Tabulator("#lstMessagesPanel", { + height: "400px", + data: jsonMessageLst, + dataTree: true, + dataTreeStartExpanded: true, + dataTreeElementColumn: "subject", + columns: [ + {title: "Subject", field: "subject", width: 700, responsive: 0}, + {title: "From", field: "from", width: 400}, + {title: "Date", field: "sent", sorter: "datetime", width: 150} + ], + rowClick: function(e, row) { + // TODO + } + }); + } + catch (syntaxError) + { + FHC_DialogLib.alertError("An error occurred while retrieving message, contact the website administrator"); + } + } + else + { + FHC_DialogLib.alertWarning("No message currently available"); + } + }, + errorCallback: function() { + + }, + veilTimeout: 300 + } + ); + +});