- 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
This commit is contained in:
Paolo
2019-07-04 11:15:07 +02:00
parent 99e541d506
commit c62b4dc018
4 changed files with 183 additions and 67 deletions
+60 -19
View File
@@ -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 = '<br><br><blockquote><i>On %s %s %s wrote:</i></blockquote><blockquote style="border-left:2px solid; padding-left: 8px">%s</blockquote>';
const REPLY_SUBJECT_PREFIX = 'Re: '; // reply subject prefix
// To quote a reply body message
const REPLY_BODY_FORMAT = '<br>
<br>
<blockquote>
<i>
On %s %s %s wrote:
</i>
</blockquote>
<blockquote style="border-left:2px solid; padding-left: 8px">
%s
</blockquote>';
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)
{
+18 -9
View File
@@ -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));