diff --git a/application/controllers/ViewMessage.php b/application/controllers/ViewMessage.php
index 3c22c8977..9c6fc4c4e 100644
--- a/application/controllers/ViewMessage.php
+++ b/application/controllers/ViewMessage.php
@@ -16,14 +16,10 @@ if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Handles sending messages with token
- * NOTE: in this controller is not possible to include/call everything
- * that automatically call the authentication system, like the most of models or libraries
+ * NOTE: it extends FHC_Controller instead of Auth_Controller because authentication is not needed
*/
class ViewMessage extends FHC_Controller
{
- /**
- * API constructor
- */
public function __construct()
{
parent::__construct();
@@ -32,121 +28,48 @@ class ViewMessage extends FHC_Controller
$this->config->load('message');
// Load model MessageToken_model, not calling the authentication system
- $this->load->model('system/MessageToken_model', 'MessageTokenModel');
$this->load->model('CL/Messages_model', 'CLMessagesModel');
}
/**
- * Using the MessageTokenModel instead of MessageLib to allow
- * viewing the message without prompting the login
+ * Display a message in read mode only using the specified token
*/
public function toHTML($token)
{
- $msg = $this->MessageTokenModel->getMessageByToken($token);
-
- if ($msg->error)
- {
- show_error(getData($msg));
- }
-
- if (is_array(getData($msg)) && count(getData($msg)) > 0)
- {
- $setReadMessageStatusByToken = $this->MessageTokenModel->setReadMessageStatusByToken($token);
-
- if (isError($setReadMessageStatusByToken))
- {
- show_error($msg->$setReadMessageStatusByToken);
- }
-
- $sender_id = getData($msg)[0]->sender_id;
- $receiver_id = getData($msg)[0]->receiver_id;
- $sender = $this->MessageTokenModel->getSenderData($sender_id);
-
- // To decide how to change the redirection
- $isEmployee = $this->MessageTokenModel->isEmployee($receiver_id);
- if (!is_bool($isEmployee) && isError($isEmployee))
- {
- show_error($isEmployee);
- }
-
- if($this->config->item('redirect_view_message_url') != '')
- $href = $this->config->item('message_server').$this->config->item('redirect_view_message_url').$token;
- else
- $href = '';
-
- $data = array (
- 'sender_id' => $sender_id,
- 'sender' => getData($sender)[0],
- 'message' => getData($msg)[0],
- 'isEmployee' => $isEmployee,
- 'href' => $href
- );
-
- $this->load->view('system/messages/messageHTML.php', $data);
- }
+ // Loads the view to read a received message using its token as identifier
+ $this->load->view('system/messages/htmlRead', $this->CLMessagesModel->prepareHtmlRead($token));
}
/**
- * write the reply
+ * Write a reply message to a received one using its token as identifier
*/
public function writeReply()
{
- $token = $this->input->get('token');
+ $token = $this->input->get('token'); // gets received message token
- if (isEmptyString($token))
- {
- show_error('No token supplied');
- }
-
- $msg = null;
-
- // Get message data if possible
- $msg = $this->MessageTokenModel->getMessageByToken($token);
- if (!hasData($msg))
- {
- show_error('No message found');
- }
-
- $msg = getData($msg)[0];
-
- // Get variables
- $receiverData = $this->MessageTokenModel->getPersonData($msg->sender_id);
- if (!hasData($receiverData))
- {
- show_error('No sender found');
- }
-
- $data = array (
- 'receivers' => getData($receiverData),
- 'message' => $msg,
- 'token' => $token
- );
-
- $this->load->view('system/messages/messageWriteReply', $data);
+ // Loads the view to write a reply message
+ $this->load->view('system/messages/htmlWriteReply', $this->CLMessagesModel->prepareHtmlWriteReply($token));
}
/**
- * Send a reply
+ * Send a reply message (no templates are used)
*/
public function sendReply()
{
$subject = $this->input->post('subject');
$body = $this->input->post('body');
- $persons = $this->input->post('persons');
+ $receiver_id = $this->input->post('receiver_id');
$relationmessage_id = $this->input->post('relationmessage_id');
$token = $this->input->post('token');
- if (!isset($relationmessage_id) || $relationmessage_id == '' || !isset($token) || $token == '')
+ $sendReply = $this->CLMessagesModel->sendReply($receiver_id, $subject, $body, $relationmessage_id, $token);
+ if (isSuccess($sendReply))
{
- show_error('Error while sending reply');
+ $this->load->view('system/messages/htmlSuccess');
}
-
- $sendReply = $this->CLMessagesModel->sendReply($subject, $body, $persons, $relationmessage_id, $token);
- if (isError($sendReply))
+ else
{
- show_error(getData($sendReply));
+ $this->load->view('system/messages/htmlError');
}
-
- $this->load->view('system/messages/messageReplySent');
}
}
diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php
index 7bf6b7518..206aeed87 100644
--- a/application/libraries/MessageLib.php
+++ b/application/libraries/MessageLib.php
@@ -13,6 +13,7 @@ class MessageLib
const CFG_MESSAGE_SERVER = 'message_server';
const CFG_MESSAGE_HTML_VIEW_URL = 'message_html_view_url';
const CFG_OU_RECEIVERS = 'ou_receivers';
+ const CFG_REDIRECT_VIEW_MESSAGE_URL = 'redirect_view_message_url';
// Templates names
const NOTICE_TEMPLATE_HTML = 'MessageMailHTML';
diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php
index 05eaa8b16..33bc90e80 100644
--- a/application/models/CL/Messages_model.php
+++ b/application/models/CL/Messages_model.php
@@ -8,7 +8,9 @@
class Messages_model extends CI_Model
{
const REPLY_SUBJECT_PREFIX = 'Re: ';
- const REPLY_BODY_PREFIX = '
-------------------------------------------------------------------------------';
+ const REPLY_BODY_FORMAT = '
On %s %s %s wrote:
%s'; + + const NO_AUTH_UID = 'online'; // hard coded uid if no authentication is performed /** * Constructor @@ -34,6 +36,87 @@ class Messages_model extends CI_Model //------------------------------------------------------------------------------------------------------------------ // Public methods + /** + * Prepares data for the view system/messages/htmlRead using a token that identifies a single message + */ + public function prepareHtmlRead($token) + { + if (isEmptyString($token)) show_error('The given token is not valid'); + + // Retrieves message using the given token + $messageResult = $this->MessageTokenModel->getMessageByToken($token); + if (isError($messageResult)) show_error(getData($messageResult)); + if (!hasData($messageResult)) show_error('No message found with the given token'); + + $message = getData($messageResult)[0]; // Found message data + + // Set message as read + $srmsbtResult = $this->MessageTokenModel->setReadMessageStatusByToken($token); + if (isError($srmsbtResult)) show_error(getData($srmsbtResult)); + + // Retrieves message sender information + $senderResult = $this->MessageTokenModel->getSenderData($message->sender_id); + if (isError($senderResult)) show_error(getData($senderResult)); + if (!hasData($senderResult)) show_error('No sender information found'); + + $sender = getData($senderResult)[0]; // Found sender data + + // Check if the receiver is an employee + $isEmployee = false; // not by default + $isEmployeeResult = $this->MessageTokenModel->isEmployee($message->receiver_id); + if (isError($isEmployeeResult)) show_error(getData($isEmployeeResult)); + if (hasData($isEmployeeResult)) $isEmployee = true; + + // If the sender is an employee and are present configurations to reply + $hrefReply = ''; + if ($isEmployee && !isEmptyString($this->config->item(MessageLib::CFG_REDIRECT_VIEW_MESSAGE_URL))) + { + $hrefReply = $this->config->item(MessageLib::CFG_MESSAGE_SERVER). + $this->config->item(MessageLib::CFG_REDIRECT_VIEW_MESSAGE_URL). + $token; + } + + return array ( + 'sender' => $sender, + 'message' => $message, + 'hrefReply' => $hrefReply + ); + } + + /** + * Prepares data for the view system/messages/htmlWriteReply using a token that identifies a single message + */ + public function prepareHtmlWriteReply($token) + { + if (isEmptyString($token)) show_error('The given token is not valid'); + + // Retrieves message using the given token + $messageResult = $this->MessageTokenModel->getMessageByToken($token); + if (isError($messageResult)) show_error(getData($messageResult)); + if (!hasData($messageResult)) show_error('No message found with the given token'); + + $message = getData($messageResult)[0]; // Found message data + + // Retrieves message sender information + $senderResult = $this->MessageTokenModel->getSenderData($message->sender_id); + if (isError($senderResult)) show_error(getData($senderResult)); + if (!hasData($senderResult)) show_error('No sender information found'); + + $sender = getData($senderResult)[0]; // Found sender data + + $replySubject = self::REPLY_SUBJECT_PREFIX.$message->subject; + $replyBody = $this->_getReplyBody($message->body, $sender->vorname, $sender->nachname, $message->sent); + + return array ( + 'receiver' => $sender->vorname.' '.$sender->nachname, // yep! the sender of the sent message is the receiver of the reply message + 'subject' => $replySubject, + 'body' => $replyBody, + 'receiver_id' => $message->sender_id, + 'relationmessage_id' => $message->message_id, + 'token' => $token + ); + } + /** * Prepares data for the view system/messages/htmlWriteTemplate using person ids as main parameter * Wrap method to _prepareHtmlWriteTemplate @@ -158,62 +241,45 @@ class Messages_model extends CI_Model } /** - * Send a reply to a message accessed using a token + * Send a reply to a single recipient for a message identified by a token (no templates are used) */ - public function sendReply($subject, $body, $persons, $relationmessage_id, $token) + public function sendReply($receiver_id, $subject, $body, $relationmessage_id, $token) { - $relationmsg = $this->MessageTokenModel->getMessageByToken($token); - if (!hasData($relationmsg) || $relationmessage_id !== getData($relationmsg)[0]->message_id) + // Retrieves message sender information + $senderResult = $this->MessageTokenModel->getSenderData($receiver_id); + if (isError($senderResult)) show_error(getData($senderResult)); + if (!hasData($senderResult)) show_error('No sender information found'); + + $sender = getData($senderResult)[0]; // Found sender data + + $messageResult = $this->MessageTokenModel->getMessageByToken($token); + if (isError($messageResult)) show_error(getData($messageResult)); + // Security check! It is possible to reply only to a received message!! + if (!hasData($messageResult) || $relationmessage_id != getData($messageResult)[0]->message_id) { - show_error('Error while sending reply'); + show_error('An error occurred while sending your message, please contact the site administrator'); } - // Get sender (receiver of previous msg) - $sender_id = getData($relationmsg)[0]->receiver_id; + $sender_id = getData($messageResult)[0]->receiver_id; - // Get message data of persons - $data = $this->MessageTokenModel->getPersonData($persons); - if (hasData($data)) - { - for ($i = 0; $i < count(getData($data)); $i++) - { - $dataArray = (array)getData($data)[$i]; + $message = $this->messagelib->sendMessageUser( + $receiver_id, // receiverPersonId + $subject, // subject + $body, // body + $sender_id, // sender_id, the receiver of the previous message is the sender of the current one + null, // senderOU + $relationmessage_id, // relationmessage_id + MSG_PRIORITY_NORMAL // priority + ); - $msg = $this->messagelib->sendMessageUser( - $dataArray['person_id'], // receiverPersonId - $subject, // subject - $body, // body - $sender_id, // sender_id - null, // senderOU - $relationmessage_id, // relationmessage_id - MSG_PRIORITY_NORMAL // priority - ); + if (isError($message)) return $message; + if (!hasData($message)) return error('No messages were saved in database'); - if (isError($msg)) return $msg; + // Write log entry + $personLog = $this->_personLog($sender_id, $receiver_id, getData($message)[0]); + if (isError($personLog)) return $personLog; - // Logs person data - $personLog = $this->personloglib->log( - $sender_id, - 'Action', - array( - 'name' => 'Message sent', - 'message' => 'Message sent from person '.$sender_id.' to '.$dataArray['person_id'].', messageid '.getData($msg), - 'success' => 'true' - ), - 'kommunikation', - 'core', - null, - 'online' - ); - - // Unpark bewerber after he sends message - $personLog = $this->personloglib->unPark($sender_id); - - if (isError($personLog)) return $personLog; - } - } - - return success('Reply sent'); + return success('Messages sent successfully'); } //------------------------------------------------------------------------------------------------------------------ @@ -339,7 +405,11 @@ class Messages_model extends CI_Model */ private function _personLog($sender_id, $receiver_id, $message_id) { - $_personLog = $this->personloglib->log( + // 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; + + return $this->personloglib->log( $receiver_id, 'Action', array( @@ -350,10 +420,19 @@ class Messages_model extends CI_Model 'kommunikation', 'core', null, - getAuthUID() + $loggedUserUID ); + } - return $_personLog; + /** + * + */ + private function _getReplyBody($body, $receiverName, $receiverSurname, $sentDate) + { + return sprintf( + self::REPLY_BODY_FORMAT, + date_format(date_create($sentDate), 'd.m.Y H:i'), $receiverName, $receiverSurname, $body + ); } /** @@ -407,7 +486,7 @@ class Messages_model extends CI_Model $message = getData($messageResult)[0]; $replySubject = self::REPLY_SUBJECT_PREFIX.$message->subject; - $replyBody = self::REPLY_BODY_PREFIX.$message->body; + $replyBody = $this->_getReplyBody($message->body, $receiver->Vorname, $receiver->Nachname, $message->sent); $relationmessage = ''; } diff --git a/application/models/system/MessageToken_model.php b/application/models/system/MessageToken_model.php index 2398bf57a..bdb6136ac 100644 --- a/application/models/system/MessageToken_model.php +++ b/application/models/system/MessageToken_model.php @@ -152,45 +152,7 @@ class MessageToken_model extends DB_Model LEFT JOIN public.tbl_mitarbeiter m ON(b.uid = m.mitarbeiter_uid) WHERE p.person_id = ?'; - $result = $this->db->query($sql, array($person_id)); - - // If no errors occurred - if ($result) - { - return success($result->result()); - } - else - { - return error($this->db->error()); - } - } - - /** - * Get data of a person - */ - public function getPersonData($person_id) - { - $sql = 'SELECT person_id, - vorname as "Vorname", - nachname as "Nachname", - anrede as "Anrede", - titelpost as "TitelPost", - titelpre as "TitelPre", - vornamen as "Vornamen" - FROM public.tbl_person - WHERE person_id %s ?'; - - $result = $this->db->query(sprintf($sql, is_array($person_id) ? 'IN' : '='), array($person_id)); - - // If no errors occurred - if ($result) - { - return success($result->result()); - } - else - { - return error($this->db->error()); - } + return $this->execQuery($sql, array($person_id)); } /** @@ -205,30 +167,7 @@ class MessageToken_model extends DB_Model WHERE p.person_id = ? AND b.aktiv = TRUE'; - $result = $this->db->query($sql, array($person_id)); - - // If no errors occurred - if ($result) - { - // If data are present - if (is_array($result->result()) && count($result->result()) > 0) - { - $personresults = $result->result(); - $person = $personresults[0]; - - // If it is an employee - if ($person->mitarbeiter_uid != null) - { - return true; - } - } - - return false; - } - else - { - return error($this->db->error()); - } + return $this->execQuery($sql, array($person_id)); } /** diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 3a30bba1f..65812580e 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -25,7 +25,8 @@ class Recipient_model extends DB_Model ks.kontakt, p.nachname, p.vorname, - b.uid + b.uid, + mr.sent FROM public.tbl_msg_recipient mr INNER JOIN public.tbl_msg_message mm USING (message_id) INNER JOIN public.tbl_person p ON (mm.person_id = p.person_id) LEFT JOIN public.tbl_benutzer b ON (mr.person_id = b.person_id) diff --git a/application/views/system/messages/messageHTML.php b/application/views/system/messages/htmlRead.php similarity index 87% rename from application/views/system/messages/messageHTML.php rename to application/views/system/messages/htmlRead.php index 0ae6c3866..97751f96f 100644 --- a/application/views/system/messages/messageHTML.php +++ b/application/views/system/messages/htmlRead.php @@ -1,4 +1,4 @@ - load->view( 'templates/FHC-Header', array( @@ -10,7 +10,7 @@ 'customCSSs' => array('public/css/sbadmin2/admintemplate_contentonly.css', 'public/css/messaging/messageReply.css') ) ); - ?> +?>
| + vorname.' '.$sender->nachname; ?> + |
| + subject; ?> + |
| + body; ?> + |
On '.date_format(date_create($message->sent), 'd.m.Y H:i').' '.$receivers[0]->Vorname.' '.$receivers[0]->Nachname.' wrote:'.''; - $body .= '
'; - $body .= $message->body.''; - } - ?> - -
- Fachhochschule Technikum Wien | University of Applied Sciences Technikum Wien
-
Hoechstaedtplatz 6, 1200 Wien, AUSTRIA
-
www.technikum-wien.at
-