diff --git a/application/controllers/system/messages/MessageClient.php b/application/controllers/system/messages/MessageClient.php index 3000e6b91..1062202c1 100644 --- a/application/controllers/system/messages/MessageClient.php +++ b/application/controllers/system/messages/MessageClient.php @@ -8,8 +8,11 @@ class MessageClient extends Auth_Controller { parent::__construct( array( - 'read' => array('basis/message:r'), - 'listMessages' => array('basis/message:r') + 'read' => array('basis/person:r'), + 'write' => array('basis/person:r'), + 'listReceivedMessages' => array('basis/person:r'), + 'listSentMessages' => array('basis/person:r'), + 'sendMessageToOU' => array('basis/person:r') ) ); @@ -27,13 +30,39 @@ class MessageClient extends Auth_Controller } /** - * Returns JSON that that contains all the received messages by the currently logged user - * This JSON structure is nested data used by tabulator + * Starts the GUI used to write a personal message to an organisation unit */ - public function listMessages() + public function write() { - $jsonNestedData = $this->CLMessagesModel->prepareAjaxRead(); + // Loads the view to write a message + $this->load->view('system/messages/ajaxWrite', $this->CLMessagesModel->prepareAjaxWrite()); + } - $this->outputJson($jsonNestedData); + /** + * Returns JSON that that contains all the received messages by the currently logged user + */ + public function listReceivedMessages() + { + $this->outputJson($this->CLMessagesModel->prepareAjaxReadReceived()); + } + + /** + * Returns JSON that that contains all the sent messages by the currently logged user + */ + public function listSentMessages() + { + $this->outputJson($this->CLMessagesModel->prepareAjaxReadSent()); + } + + /** + * + */ + public function sendMessageToOU() + { + $receiverOU = $this->input->post('receiverOU'); + $subject = $this->input->post('subject'); + $body = $this->input->post('body'); + + $this->outputJson($this->CLMessagesModel->sendToOrganisationUnit($receiverOU, $subject, $body)); } } diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 206aeed87..18b958e14 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -128,7 +128,9 @@ class MessageLib // If the recipient is an organisation unit that would be possible to send the same message (same message id) // to the entire organisation unit (one to many functionality) // In this case the receiver id is a the one present in message configuration - $receivers = success(array($this->_ci->config->item(self::CFG_SYSTEM_PERSON_ID))); + $receiver = new stdClass(); + $receiver->person_id = $this->_ci->config->item(self::CFG_SYSTEM_PERSON_ID); + $receivers = success(array($receiver)); // Send the message and return the result return $this->_sendMessage($receivers, $receiversOU, $subject, $body, $sender_id, $senderOU, $relationmessage_id, $priority, $multiPartMime); @@ -373,24 +375,6 @@ class MessageLib } } - /** - * Gets the receivers id that are enabled to read messages for that oe_kurzbz - */ - private function _getReceiversByOekurzbz($oe_kurzbz) - { - // Load Benutzerfunktion_model - $this->_ci->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel'); - // Join with table public.tbl_benutzer on field uid - $this->_ci->BenutzerfunktionModel->addJoin('public.tbl_benutzer', 'uid'); - // Get all the valid receivers id using the oe_kurzbz - $receivers = $this->_ci->BenutzerfunktionModel->loadWhere( - 'oe_kurzbz = '.$this->_ci->db->escape($oe_kurzbz). - ' AND funktion_kurzbz = '.$this->_ci->db->escape($this->_ci->config->item(self::CFG_OU_RECEIVERS)). - ' AND (NOW() BETWEEN COALESCE(datum_von, NOW()) AND COALESCE(datum_bis, NOW()))' - ); - return $receivers; - } - /** * Save a new message in DB */ diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index 21cba1d68..5810487a9 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -28,9 +28,10 @@ class Messages_model extends CI_Model // Loads model MessageToken_model $this->load->model('system/MessageToken_model', 'MessageTokenModel'); - // Loads model Benutzerrolle_model $this->load->model('system/Benutzerrolle_model', 'BenutzerrolleModel'); + // Loads model Prestudent_model + $this->load->model('crm/Prestudent_model', 'PrestudentModel'); } //------------------------------------------------------------------------------------------------------------------ @@ -39,20 +40,42 @@ class Messages_model extends CI_Model /** * */ - public function prepareAjaxRead() + public function prepareAjaxWrite() { - $jsonNestedData = error('Something did not go as it should'); + $ouResult = $this->PrestudentModel->getOrganisationunitsByPersonId(getAuthPersonId()); + + if (isError($ouResult)) show_error('An error occurred while loading this page, please contact the site administrator'); + + $ouOptions = ''; + + if (hasData($ouResult)) + { + foreach (getData($ouResult) as $ou) + { + $ouOptions .= sprintf("\n".'', $ou->oe_kurzbz, $ou->bezeichnung); + } + } + + return array('organisationUnitOptions' => $ouOptions); + } + + /** + * + */ + public function prepareAjaxReadReceived() + { + $jsonResult = error('Something did not go as it should'); $loggedUserName = getAuthFirstname().' '.getAuthSurname(); if (isEmptyString($loggedUserName)) $loggedUserName = 'Me'; - $receivedMessagesResult = $this->RecipientModel->getReceivedMessages(getAuthPersonId()); + $receivedMessagesResult = $this->RecipientModel->getReceivedMessages( + getAuthPersonId(), + $this->config->item(MessageLib::CFG_OU_RECEIVERS) + ); if (isError($receivedMessagesResult)) return $receivedMessagesResult; - $sentMessagesResult = $this->RecipientModel->getSentMessages(getAuthPersonId()); - if (isError($sentMessagesResult)) return $sentMessagesResult; - if (hasData($receivedMessagesResult)) { $jsonArray = array(); @@ -60,41 +83,59 @@ class Messages_model extends CI_Model foreach (getData($receivedMessagesResult) as $receivedMessage) { $jsonRecord = new stdClass(); + $jsonRecord->message_id = $receivedMessage->message_id; $jsonRecord->subject = $receivedMessage->subject; + $jsonRecord->body = $receivedMessage->body; $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(); + $jsonArray[] = $jsonRecord; + } - 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; + $jsonResult = success(json_encode($jsonArray)); + } - $jsonChildrenArray[] = $jsonChildrenRecord; - } - } + return $jsonResult; + } - if (!isEmptyArray($jsonChildrenArray)) $jsonRecord->_children = $jsonChildrenArray; - } + /** + * + */ + public function prepareAjaxReadSent() + { + $jsonResult = error('Something did not go as it should'); + + $loggedUserName = getAuthFirstname().' '.getAuthSurname(); + + if (isEmptyString($loggedUserName)) $loggedUserName = 'Me'; + + $sentMessagesResult = $this->RecipientModel->getSentMessages(getAuthPersonId()); + if (isError($sentMessagesResult)) return $sentMessagesResult; + + if (hasData($sentMessagesResult)) + { + $jsonArray = array(); + + foreach (getData($sentMessagesResult) as $sentMessage) + { + $jsonRecord = new stdClass(); + $jsonRecord->message_id = $sentMessage->message_id; + $jsonRecord->subject = $sentMessage->subject; + $jsonRecord->body = $sentMessage->body; + $jsonRecord->from = $sentMessage->vorname.' '.$sentMessage->nachname; + $sentDate = new DateTime($sentMessage->sent); + $jsonRecord->sent = $sentDate->format('d/m/Y H:i:s'); + $jsonRecord->status = $sentMessage->status; $jsonArray[] = $jsonRecord; } - $jsonNestedData = success(json_encode($jsonArray)); + $jsonResult = success(json_encode($jsonArray)); } - return $jsonNestedData; + return $jsonResult; } /** @@ -260,7 +301,6 @@ class Messages_model extends CI_Model if (isError($msgVarsData)) show_error(getData($msgVarsData)); if (!hasData($msgVarsData)) show_error('No recipients were given'); - $this->load->model('crm/Prestudent_model', 'PrestudentModel'); $prestudentsData = $this->PrestudentModel->getOrganisationunits($prestudents); // Adds the organisation unit to each prestudent @@ -337,6 +377,34 @@ class Messages_model extends CI_Model return success('Messages sent successfully'); } + /** + * Send a message to an organisation unit + */ + public function sendToOrganisationUnit($receiverOU, $subject, $body) + { + if (isEmptyString($receiverOU)) return error('Not a valid organisation unit'); + if (isEmptyString($subject)) return error('Subject is an empty string'); + if (isEmptyString($body)) return error('Body is an empty string'); + + $sender_id = getAuthPersonId(); + + $message = $this->messagelib->sendMessageOU( + $receiverOU, // receiverPersonId + $subject, // subject + $body, // body + $sender_id // sender_id + ); + + if (isError($message)) return $message; + if (!hasData($message)) return error('No messages were saved in database'); + + // Write log entry + $personLog = $this->_personLog($sender_id, $this->config->item(MessageLib::CFG_SYSTEM_PERSON_ID), getData($message)[0], $receiverOU); + if (isError($personLog)) return $personLog; + + return success('Messages sent successfully'); + } + //------------------------------------------------------------------------------------------------------------------ // Public methods called by controller system/messages/Messages @@ -447,18 +515,21 @@ class Messages_model extends CI_Model /** * Perform a person log after a message is sent */ - private function _personLog($sender_id, $receiver_id, $message_id) + private function _personLog($sender_id, $receiver_id, $message_id, $receiverOU = null) { // In case the message is accessed via ViewMessage controller -> no authentication // If no authentication is performed then use a hard coded uid $loggedUserUID = isLogged() ? getAuthUID() : self::NO_AUTH_UID; + $message = 'Message sent from person '.$sender_id.' to '.$receiver_id.', message id: '.$message_id; + if (!isEmptyString($receiverOU)) $message .= ', receiverOU: '.$receiverOU; + return $this->personloglib->log( $receiver_id, 'Action', array( 'name' => 'Message sent', - 'message' => 'Message sent from person '.$sender_id.' to '.$receiver_id.', message id: '.$message_id, + 'message' => $message, 'success' => 'true' ), 'kommunikation', diff --git a/application/models/crm/Prestudent_model.php b/application/models/crm/Prestudent_model.php index ffc6afb0e..b4c756e52 100644 --- a/application/models/crm/Prestudent_model.php +++ b/application/models/crm/Prestudent_model.php @@ -523,4 +523,20 @@ class Prestudent_model extends DB_Model } } } + + /** + * Get organisation units for all the prestudents of a person + */ + public function getOrganisationunitsByPersonId($person_id) + { + $query = 'SELECT o.oe_kurzbz, o.bezeichnung + FROM public.tbl_prestudent p + JOIN public.tbl_studiengang s USING(studiengang_kz) + JOIN public.tbl_organisationseinheit o USING(oe_kurzbz) + WHERE p.person_id = ? + GROUP BY o.oe_kurzbz, o.bezeichnung + ORDER BY o.bezeichnung'; + + return $this->execQuery($query, array($person_id)); + } } diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 8d1162de5..39961cdbf 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -303,13 +303,13 @@ class Recipient_model extends DB_Model /** * */ - public function getReceivedMessages($person_id) + public function getReceivedMessages($person_id, $functions) { $sql = 'SELECT mr.message_id, mm.relationmessage_id, mm.subject, mm.body, - mr.sent, + mr.sent AS sent, p.vorname, p.nachname, MAX(ms.status) AS status @@ -327,9 +327,34 @@ class Recipient_model extends DB_Model mr.sent, p.vorname, p.nachname - ORDER BY mr.sent DESC'; + UNION + SELECT mrou.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mrou.sent AS sent, + p.vorname, + p.nachname, + MAX(ms.status) AS status + 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) + JOIN public.tbl_msg_recipient mrou ON (mrou.oe_kurzbz = bf.oe_kurzbz) + JOIN public.tbl_msg_message mm ON (mm.message_id = mrou.message_id) + JOIN public.tbl_msg_status ms ON (ms.message_id = mrou.message_id AND ms.person_id = mrou.person_id) + WHERE p.person_id = ? + AND mrou.sent IS NOT NULL + AND mrou.sentinfo IS NULL + GROUP BY mrou.message_id, + mm.relationmessage_id, + mm.subject, + mm.body, + mrou.sent, + p.vorname, + p.nachname + ORDER BY sent DESC'; - return $this->execQuery($sql, array($person_id)); + return $this->execQuery($sql, array($person_id, $functions, $person_id)); } /** @@ -342,10 +367,13 @@ class Recipient_model extends DB_Model mm.subject, mm.body, mr.sent, + p.vorname, + p.nachname, 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) + JOIN public.tbl_person p ON (p.person_id = mr.person_id) WHERE mm.person_id = ? AND mr.sent IS NOT NULL AND mr.sentinfo IS NULL @@ -353,7 +381,9 @@ class Recipient_model extends DB_Model mm.relationmessage_id, mm.subject, mm.body, - mr.sent + mr.sent, + p.vorname, + p.nachname 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 e3ff133f0..b8d2ea4a6 100644 --- a/application/views/system/messages/ajaxRead.php +++ b/application/views/system/messages/ajaxRead.php @@ -6,15 +6,28 @@ 'jquery' => true, 'jqueryui' => true, 'bootstrap' => true, + 'fontawesome' => true, + 'sbadmintemplate' => true, 'momentjs' => true, 'tabulator' => true, 'ajaxlib' => true, 'dialoglib' => true, - 'customJSs' => array('public/js/messaging/messageClient.js') + 'tinymce' => true, + 'customJSs' => array('public/js/messaging/read.js') ) ); ?>
+