- Added new functions getAuthFirstname and getAuthSurname to helper hlp_authentication_helper

- Added methods getReceivedMessages and getSentMessages to model system/Recipient_model.php
- Added jqueryui and dialoglib to view system/messages/ajaxRead
- Added method prepareAjaxRead to model CL/Messages_model
- Removed private method _getAuthUser from model CL/Messages_model
- Adapted code in model CL/Messages_model to use functions isLogged and getAuthPersonId
This commit is contained in:
Paolo
2019-06-24 17:32:31 +02:00
parent 5072088e0d
commit 27ccd086ac
5 changed files with 210 additions and 28 deletions
+68 -27
View File
@@ -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
@@ -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));
}
}