diff --git a/application/controllers/system/FASMessages.php b/application/controllers/system/FASMessages.php
index 6928a919d..55d1da25f 100644
--- a/application/controllers/system/FASMessages.php
+++ b/application/controllers/system/FASMessages.php
@@ -11,17 +11,15 @@ class FASMessages extends Auth_Controller
{
parent::__construct(
array(
- 'write' => 'basis/message:rw',
- 'writeReply' => 'basis/message:rw'
+ 'writeTemplate' => 'basis/message:rw',
+ 'writeReplyTemplate' => 'basis/message:rw'
)
);
- // Loads the message library
- $this->load->library('MessageLib');
-
- // Loads the widget library
- $this->load->library('WidgetLib');
+ // Loads model CLMessagesModel which contains the GUI logic
+ $this->load->model('CL/Messages_model', 'CLMessagesModel');
+ // Phrases used in loaded views
$this->loadPhrases(
array(
'global',
@@ -30,162 +28,33 @@ class FASMessages extends Auth_Controller
);
}
- // -----------------------------------------------------------------------------------------------------------------
- // Public methods
-
/**
- * Write a new message
+ * Writes a new message to a prestudent using templates
*/
- public function write($sender_id)
+ public function writeTemplate()
{
- $prestudent_id = $this->input->post('prestudent_id'); // recipients prestudend_id(s)
+ $prestudents = $this->input->post('prestudent_id'); // recipients prestudend_id(s)
- if (!is_numeric($sender_id))
- {
- show_error('The current logged user person_id is not defined');
- }
-
- $msgVarsData = $this->_getMsgVarsData($prestudent_id);
-
- // Retrieves message vars for a person from view view vw_msg_vars_person
- $variablesArray = $this->_getMessageVarsPerson();
-
- // Organisation units used to get the templates
- $oe_kurzbz = $this->_getOeKurzbz($sender_id);
-
- // Admin or commoner?
- $isAdmin = $this->_getIsAdmin($sender_id);
-
- $data = array(
- 'recipients' => getData($msgVarsData),
- 'variables' => $variablesArray,
- 'oe_kurzbz' => $oe_kurzbz, // used to get the templates
- 'isAdmin' => $isAdmin
- );
-
- $this->load->view('system/messages/messageWrite', $data);
+ // Loads the view to write a new message with a template
+ $this->load->view(
+ 'system/messages/htmlWriteTemplate',
+ $this->CLMessagesModel->prepareHtmlWriteTemplatePrestudents($prestudents)
+ );
}
/**
- * Write a reply
+ * Writes a reply to a message identified by parameters $message_id and $recipient_id
+ * The recipient is a prestudent
+ * Uses templates
*/
- public function writeReply($sender_id, $msg_id, $receiver_id)
+ public function writeReplyTemplate($message_id, $recipient_id)
{
- $prestudent_id = $this->input->post('prestudent_id'); // recipients prestudend_id(s)
+ $prestudents = $this->input->post('prestudent_id'); // recipients prestudend_id(s)
- if (!is_numeric($sender_id))
- {
- show_error('The current logged user person_id is not defined');
- }
-
- if (!is_numeric($msg_id))
- {
- show_error('The msg_id must be a number');
- }
-
- if (!is_numeric($receiver_id))
- {
- show_error('The receiver_id must be a number');
- }
-
- $msg = $this->_getMessage($msg_id, $receiver_id);
-
- $msgVarsData = $this->_getMsgVarsData($prestudent_id);
-
- // Retrieves message vars for a person from view view vw_msg_vars_person
- $variablesArray = $this->_getMessageVarsPerson();
-
- // Organisation units used to get the templates
- $oe_kurzbz = $this->_getOeKurzbz($sender_id);
-
- // Admin or commoner?
- $isAdmin = $this->_getIsAdmin($sender_id);
-
- $data = array(
- 'recipients' => getData($msgVarsData),
- 'message' => $msg,
- 'variables' => $variablesArray,
- 'oe_kurzbz' => $oe_kurzbz, // used to get the templates
- 'isAdmin' => $isAdmin
- );
-
- $this->load->view('system/messages/messageWrite', $data);
+ // Loads the view to write a new message with a template
+ $this->load->view(
+ 'system/messages/htmlWriteTemplate',
+ $this->CLMessagesModel->prepareHtmlWriteTemplatePrestudents($prestudents, $message_id, $recipient_id)
+ );
}
-
- // -----------------------------------------------------------------------------------------------------------------
- // Private methods
-
- /**
- *
- */
- private function _getMessage($msg_id, $receiver_id)
- {
- $msg = $this->messagelib->getMessage($msg_id, $receiver_id);
- if (isError($msg))
- {
- show_error(getData($msg));
- }
- elseif (!hasData($msg))
- {
- show_error('The selected message does not exist');
- }
- else
- {
- $msg = getData($msg)[0];
- }
-
- return $msg;
- }
-
- /**
- * Retrieves message vars from view vw_msg_vars
- */
- private function _getMsgVarsData($prestudent_id)
- {
- $this->load->model('system/Message_model', 'MessageModel');
- $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudent_id);
-
- if (isError($msgVarsData))
- {
- show_error(getData($msgVarsData));
- }
-
- return $msgVarsData;
- }
-
- /**
- * Wrapper method to call messagelib->getMessageVarsPerson
- */
- private function _getMessageVarsPerson()
- {
- $variables = $this->messagelib->getMessageVarsPerson();
-
- if (isError($variables)) show_error(getData($variables));
-
- return getData($variables);
- }
-
- /**
- * Wrapper method to call messagelib->getOeKurzbz
- */
- private function _getOeKurzbz($sender_id)
- {
- $oe_kurzbz = $this->messagelib->getOeKurzbz($sender_id);
-
- if (isError($oe_kurzbz)) show_error(getData($oe_kurzbz));
-
- return getData($oe_kurzbz);
- }
-
- /**
- * Wrapper method to call messagelib->getIsAdmin
- */
- private function _getIsAdmin($sender_id)
- {
- $isAdmin = $this->messagelib->getIsAdmin($sender_id);
-
- if (isError($isAdmin)) show_error(getData($isAdmin));
-
- return getData($isAdmin);
- }
}
diff --git a/application/controllers/system/Messages.php b/application/controllers/system/Messages.php
index a7709a702..20fed38ad 100644
--- a/application/controllers/system/Messages.php
+++ b/application/controllers/system/Messages.php
@@ -4,28 +4,23 @@ if (! defined('BASEPATH')) exit('No direct script access allowed');
class Messages extends Auth_Controller
{
- /**
- * MessageLib is loaded by CLMessagesModel
- */
public function __construct()
{
parent::__construct(
array(
- 'write' => array('basis/message:rw', 'infocenter:rw'),
- 'send' => array('basis/message:rw', 'infocenter:rw'),
- 'sendJson' => array('basis/message:rw', 'infocenter:rw'),
+ 'writeTemplate' => array('basis/message:rw', 'infocenter:rw'),
+ 'sendImplicitTemplate' => array('basis/message:rw', 'infocenter:rw'),
+ 'sendExplicitTemplateJson' => array('basis/message:rw', 'infocenter:rw'),
'getVorlage' => array('basis/message:r', 'infocenter:r'),
'parseMessageText' => array('basis/message:r', 'infocenter:r'),
'getMessageFromIds' => array('basis/message:r', 'infocenter:r')
)
);
- // Loads the widget library
- $this->load->library('WidgetLib');
-
- $this->load->model('system/Message_model', 'MessageModel');
+ // Loads model CLMessagesModel which contains the GUI logic
$this->load->model('CL/Messages_model', 'CLMessagesModel');
+ // Phrases used in loaded views
$this->loadPhrases(
array(
'global',
@@ -38,65 +33,45 @@ class Messages extends Auth_Controller
// Public methods - HTML output
/**
- * Initialize all the parameters used by view system/messages/messageWrite
- * to build a GUI used to write a messate to user/s
+ * Initialize all the parameters used by view system/messages/htmlWriteTemplate
+ * to build a GUI used to write a messate to user/s using a template
*/
- public function write()
+ public function writeTemplate()
{
$persons = $this->input->post('person_id');
- $authUser = $this->CLMessagesModel->getAuthUser();
- if (isError($authUser)) show_error(getData($authUser));
-
- $sender_id = getData($authUser)[0]->person_id;
-
- // Retrieves person information
- $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($persons);
- if (isError($msgVarsData)) show_error(getData($msgVarsData));
-
- // Retrieves message vars from view vw_msg_vars_person
- $variables = $this->messagelib->getMessageVarsPerson();
- if (isError($variables)) show_error(getData($variables));
-
- // Organisation units used to get the templates
- $oe_kurzbz = $this->messagelib->getOeKurzbz($sender_id);
- if (isError($oe_kurzbz)) show_error(getData($oe_kurzbz));
-
- // Admin or commoner?
- $isAdmin = $this->messagelib->getIsAdmin($sender_id);
- if (isError($isAdmin)) show_error(getData($isAdmin));
-
+ // Loads the view to write a new message with a template
$this->load->view(
- 'system/messages/messageWrite',
- array (
- 'recipients' => getData($msgVarsData), // recipients data
- 'variables' => getData($variables), // message vars
- 'oe_kurzbz' => getData($oe_kurzbz), // used to get the templates
- 'isAdmin' => getData($isAdmin) // is admin?
- )
+ 'system/messages/htmlWriteTemplate',
+ $this->CLMessagesModel->prepareHtmlWriteTemplatePersons($persons)
);
}
/**
* Send a new message or reply to user/s
* If a relationmessage_id this message is a reply to another one
+ * Body is a template and will be parsed using information present in persons parameter
*/
- public function send()
+ public function sendImplicitTemplate()
{
+ $subject = $this->input->post('subject');
+ $body = $this->input->post('body');
$persons = $this->input->post('persons');
$relationmessage_id = $this->input->post('relationmessage_id');
- // Retrieves message vars data for the fiven user/s
- $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($persons);
-
- // Send the message
- $send = $this->CLMessagesModel->send($msgVarsData, $relationmessage_id);
-
- $this->load->view('system/messages/messageSent', array('success' => isSuccess($send)));
+ $sendImplicitTemplate = $this->CLMessagesModel->sendImplicitTemplate($persons, $subject, $body, $relationmessage_id);
+ if (isSuccess($sendImplicitTemplate))
+ {
+ $this->load->view('system/messages/htmlSuccess');
+ }
+ else
+ {
+ $this->load->view('system/messages/htmlError');
+ }
}
// -----------------------------------------------------------------------------------------------------------------
- // Public methods - JSON output - called by this controller and FASMessages (view system/messages/messageWrite)
+ // Public methods - JSON output - called by this controller and FASMessages (view system/messages/htmlWriteTemplate)
/**
* Returns an object that represent a template store in database
@@ -106,24 +81,8 @@ class Messages extends Auth_Controller
public function getVorlage()
{
$vorlage_kurzbz = $this->input->get('vorlage_kurzbz');
- $result = error('The given vorlage_kurzbz is not valid');
- if (!isEmptyString($vorlage_kurzbz))
- {
- $this->load->model('system/Vorlagestudiengang_model', 'VorlagestudiengangModel');
- $this->VorlagestudiengangModel->addOrder('version','DESC');
-
- $result = $this->VorlagestudiengangModel->loadWhere(array('vorlage_kurzbz' => $vorlage_kurzbz));
- }
-
- if (isError($result) || !hasData($result))
- {
- $this->outputJsonError(getData($result));
- }
- else
- {
- $this->outputJsonSuccess(getData($result));
- }
+ $this->outputJson($this->CLMessagesModel->getVorlage($vorlage_kurzbz));
}
/**
@@ -134,26 +93,8 @@ class Messages extends Auth_Controller
{
$person_id = $this->input->get('person_id');
$text = $this->input->get('text');
- $msgVarsData = error('The given person_id is not a valid number');
- if (is_numeric($person_id))
- {
- $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($person_id);
- }
-
- if (isError($msgVarsData) || !hasData($msgVarsData))
- {
- $this->outputJsonError(getData($msgVarsData));
- }
- else
- {
- $this->outputJsonSuccess(
- parseText(
- $text,
- $this->CLMessagesModel->replaceKeys((array)getData($msgVarsData)[0])
- )
- );
- }
+ $this->outputJson($this->CLMessagesModel->parseMessageText($person_id, $text));
}
// -----------------------------------------------------------------------------------------------------------------
@@ -164,19 +105,10 @@ class Messages extends Auth_Controller
*/
public function getMessageFromIds()
{
- $msg_id = $this->input->get('msg_id');
+ $message_id = $this->input->get('msg_id');
$receiver_id = $this->input->get('receiver_id');
- $msg = $this->messagelib->getMessage($msg_id, $receiver_id);
-
- if (isError($msg) || !hasData($msg))
- {
- $this->outputJson(array());
- }
- else
- {
- $this->outputJson(array(getData($msg)[0]));
- }
+ $this->outputJson($this->CLMessagesModel->getMessageFromIds($message_id, $receiver_id));
}
/**
@@ -184,27 +116,16 @@ class Messages extends Auth_Controller
* - The recipients are prestudents
* - An email template with message var may be provided
* - A global organisation unit may be provided, otherwise is used the prestudent one
+ * - A template is explicitly specified
*/
- public function sendJson()
+ public function sendExplicitTemplateJson()
{
$prestudents = $this->input->post('prestudents');
- $vorlage_kurzbz = $this->input->post('vorlage_kurzbz');
$oe_kurzbz = $this->input->post('oe_kurzbz');
+ $vorlage_kurzbz = $this->input->post('vorlage_kurzbz');
$msgVars = $this->input->post('msgvars');
- $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudents);
-
- $this->load->model('crm/Prestudent_model', 'PrestudentModel');
- $prestudentsData = $this->PrestudentModel->getOrganisationunits($prestudents);
-
- // Adds the organisation unit to each prestudent
- if (isEmptyString($oe_kurzbz) && hasData($msgVarsData) && hasData($prestudentsData))
- {
- $this->CLMessagesModel->addOeToPrestudents($msgVarsData, $prestudentsData);
- }
-
- $send = $this->CLMessagesModel->send($msgVarsData, null, $oe_kurzbz, $vorlage_kurzbz, $msgVars);
-
- $this->outputJson(getData($send));
+ $sendExplicitTemplate = $this->CLMessagesModel->sendExplicitTemplate($prestudents, $oe_kurzbz, $vorlage_kurzbz, $msgVars);
+ $this->outputJson(getData($sendExplicitTemplate));
}
}
diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php
index c3c1169d4..7bf6b7518 100644
--- a/application/libraries/MessageLib.php
+++ b/application/libraries/MessageLib.php
@@ -49,7 +49,6 @@ class MessageLib
// Loads extra models
$this->_ci->load->model('person/Person_model', 'PersonModel');
- $this->_ci->load->model('system/Benutzerrolle_model', 'BenutzerrolleModel');
$this->_ci->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel');
$this->_ci->load->model('organisation/Organisationseinheit_model', 'OrganisationseinheitModel');
}
@@ -208,14 +207,6 @@ class MessageLib
return $benutzer; // otherwise returns the error
}
- /**
- * Admin or commoner?
- */
- public function getIsAdmin($sender_id)
- {
- return $this->_ci->BenutzerrolleModel->isAdminByPersonId($sender_id);
- }
-
//------------------------------------------------------------------------------------------------------------------
// Public methods used by REST API
@@ -629,6 +620,7 @@ class MessageLib
/**
* Sends new message core method, may be wrapped by other methods.
+ * If success then returns an array of successfully saved message ids
*/
private function _sendMessage(
$receivers, $receiversOU, $subject, $body, $sender_id, $senderOU, $relationmessage_id, $priority, $multiPartMime
@@ -658,11 +650,7 @@ class MessageLib
);
if (isSuccess($saveMessageResult)) // If successfully saved
{
- // If the system is configured to send messages immediately
- if ($this->_ci->config->item(self::CFG_SEND_IMMEDIATELY) === true)
- {
- $savedMessages[] = getData($saveMessageResult); // store the message id of the saved message
- }
+ $savedMessages[] = getData($saveMessageResult); // store the message id of the saved message
}
else
{
@@ -683,7 +671,7 @@ class MessageLib
}
}
- return success('Message sent successfully');
+ return success($savedMessages);
}
/**
diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php
index c17d608c7..05eaa8b16 100644
--- a/application/models/CL/Messages_model.php
+++ b/application/models/CL/Messages_model.php
@@ -1,11 +1,15 @@
-------------------------------------------------------------------------------';
+
/**
* Constructor
*/
@@ -14,102 +18,143 @@ class Messages_model extends CI_Model
parent::__construct();
// Loads the message library
- $this->load->library('MessageLib');
-
+ $this->load->library('MessageLib'); // MessageModel loaded here!
// Loads the person log library
$this->load->library('PersonLogLib');
+ // Loads the widget library
+ $this->load->library('WidgetLib');
+ // Loads model MessageToken_model
$this->load->model('system/MessageToken_model', 'MessageTokenModel');
+
+ // Loads model Benutzerrolle_model
+ $this->load->model('system/Benutzerrolle_model', 'BenutzerrolleModel');
+ }
+
+ //------------------------------------------------------------------------------------------------------------------
+ // Public methods
+
+ /**
+ * Prepares data for the view system/messages/htmlWriteTemplate using person ids as main parameter
+ * Wrap method to _prepareHtmlWriteTemplate
+ */
+ public function prepareHtmlWriteTemplatePersons($persons, $message_id = null, $recipient_id = null)
+ {
+ // Retrieves persons information
+ $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($persons);
+
+ return $this->_prepareHtmlWriteTemplate($msgVarsData, $message_id, $recipient_id);
}
/**
- * Executes message sending
- * @param $sender_id
- * @return array wether execution was successfull - error or success
+ * Prepares data for the view system/messages/htmlWriteTemplate using prestudent ids as main parameter
+ * Wrap method to _prepareHtmlWriteTemplate
*/
- public function send($msgVarsData, $relationmessage_id = null, $oe_kurzbz = null, $vorlage_kurzbz = null, $msgVars = null)
+ public function prepareHtmlWriteTemplatePrestudents($prestudents, $message_id = null, $recipient_id = null)
{
- $subject = $this->input->post('subject');
- $body = $this->input->post('body');
+ // Retrieves prestudents information
+ $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudents);
- $authUser = $this->getAuthUser();
+ return $this->_prepareHtmlWriteTemplate($msgVarsData, $message_id, $recipient_id);
+ }
- if (isError($authUser)) return $authUser;
+ /**
+ * Sends a new message or a reply to a message (if $relationmessage_id is given)
+ * using the template stored in the subject and body
+ */
+ 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;
+ // Retrieves message vars data for the given user/s
+ $msgVarsData = $this->MessageModel->getMsgVarsDataByPersonId($persons);
+ if (isError($msgVarsData)) show_error(getData($msgVarsData));
+ if (!hasData($msgVarsData)) show_error('No recipients were given');
- // Send message(s)
- if (hasData($msgVarsData))
+ foreach (getData($msgVarsData) as $receiver)
{
- for ($i = 0; $i < count(getData($msgVarsData)); $i++)
- {
- $parsedText = '';
- $msgVarsDataArray = $this->replaceKeys((array)getData($msgVarsData)[$i]); // replaces array keys
+ $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)$receiver); // replaces array keys
- // Send without vorlage
- if (isEmptyString($vorlage_kurzbz))
- {
- $parsedText = parseText($body, $msgVarsDataArray);
+ $parsedSubject = parseText($subject, $msgVarsDataArray);
+ $parsedBody = parseText($body, $msgVarsDataArray);
- $msg = $this->messagelib->sendMessageUser(
- $msgVarsDataArray['person_id'], // receiverPersonId
- $subject, // subject
- $parsedText, // body
- $sender_id, // sender_id
- $oe_kurzbz, // senderOU
- $relationmessage_id, // relationmessage_id
- MSG_PRIORITY_NORMAL // priority
- );
- }
- // Send with vorlage
- else
- {
- if (is_array($msgVars))
- {
- // Additional message variables
- foreach ($msgVars as $key => $msgvar)
- {
- $msgVarsDataArray[$key] = $msgvar;
- }
- }
+ $message = $this->messagelib->sendMessageUser(
+ $msgVarsDataArray['person_id'], // receiverPersonId
+ $parsedSubject, // subject
+ $parsedBody, // body
+ $sender_id, // sender_id
+ null, // senderOU
+ $relationmessage_id, // relationmessage_id
+ MSG_PRIORITY_NORMAL // priority
+ );
- $msg = $this->messagelib->sendMessageUserTemplate(
- $msgVarsDataArray['person_id'], // receiversPersonId
- $vorlage_kurzbz, // vorlage
- $msgVarsDataArray, // parseData
- null, // orgform
- $sender_id, // sender_id
- $oe_kurzbz // senderOU
- );
- }
+ 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->personloglib->log(
- $msgVarsDataArray['person_id'],
- 'Action',
- array(
- 'name' => 'Message sent',
- 'message' => 'Message sent from person '.$sender_id.' to '.$msgVarsDataArray['person_id'].', messageid '.getData($msg),
- 'success' => 'true'
- ),
- 'kommunikation',
- 'core',
- null,
- getAuthUID()
- );
-
- if (isError($personLog)) return $personLog;
- }
-
- return success('Messages sent successfully');
+ // Write log entry
+ $personLog = $this->_personLog($sender_id, $msgVarsDataArray['person_id'], getData($message)[0]);
+ if (isError($personLog)) return $personLog;
}
- else
+
+ return success('Messages sent successfully');
+ }
+
+ /**
+ * Sends a new message using the given template and information present in parameter prestudents
+ * Extra variables can be added using parameter $msgVars
+ */
+ 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;
+
+ // Retrieves message vars data for the given user/s
+ $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($prestudents);
+ 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
+ if (isEmptyString($oe_kurzbz) && hasData($msgVarsData) && hasData($prestudentsData))
{
- return $msgVarsData;
+ $this->CLMessagesModel->_addOeToPrestudents($msgVarsData, $prestudentsData);
}
+
+ foreach (getData($msgVarsData) as $receiver)
+ {
+ $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)$receiver); // replaces array keys
+
+ // Additional message variables
+ if (is_array($msgVars)) $msgVarsDataArray = array_merge($msgVarsDataArray, $msgVars);
+
+ $message = $this->messagelib->sendMessageUserTemplate(
+ $msgVarsDataArray['person_id'], // receiversPersonId
+ $vorlage_kurzbz, // vorlage
+ $msgVarsDataArray, // parseData
+ null, // orgform
+ $sender_id, // sender_id
+ $oe_kurzbz // senderOU
+ );
+
+ if (isError($message)) return $message;
+
+ // Write log entry
+ $personLog = $this->_personLog($sender_id, $msgVarsDataArray['person_id'], getData($message)[0]);
+ if (isError($personLog)) return $personLog;
+ }
+
+ return success('Messages sent successfully');
}
/**
@@ -171,29 +216,98 @@ class Messages_model extends CI_Model
return success('Reply sent');
}
- /**
- *
- */
- public function getAuthUser()
- {
- $sender_id = null;
+ //------------------------------------------------------------------------------------------------------------------
+ // Public methods called by controller system/Messages
+ /**
+ * Returns an object that represent a template store in database
+ * If no templates are found with the given parameter or the given parameter is an empty string,
+ * then an error is returned
+ */
+ public function getVorlage($vorlage_kurzbz)
+ {
+ $getVorlage = error('The given vorlage_kurzbz is not valid');
+
+ if (!isEmptyString($vorlage_kurzbz))
+ {
+ $this->load->model('system/Vorlagestudiengang_model', 'VorlagestudiengangModel');
+ $this->VorlagestudiengangModel->addOrder('version','DESC');
+
+ $getVorlage = $this->VorlagestudiengangModel->loadWhere(array('vorlage_kurzbz' => $vorlage_kurzbz));
+ }
+
+ return $getVorlage;
+ }
+
+ /**
+ * Parse the given given text using data from the given user
+ * Use the CI parser which performs simple text substitution for pseudo-variable
+ */
+ public function parseMessageText($person_id, $text)
+ {
+ $parseMessageText = error('The given person_id is not a valid number');
+
+ if (is_numeric($person_id))
+ {
+ $parseMessageText = $this->MessageModel->getMsgVarsDataByPersonId($person_id);
+ }
+
+ if (hasData($parseMessageText))
+ {
+ $parseMessageText = success(
+ parseText(
+ $text,
+ $this->_lowerReplaceSpaceArrayKeys((array)getData($parseMessageText)[0])
+ )
+ );
+ }
+
+ return $parseMessageText;
+ }
+
+ /**
+ * Outputs message data for a message (identified my msg id and receiver id) in JSON format
+ */
+ public function getMessageFromIds($message_id, $receiver_id)
+ {
+ $getMessageFromIds = error('The given message id or receiver id are not valid');
+
+ if (is_numeric($message_id) && is_numeric($receiver_id))
+ {
+ $getMessageFromIds = $this->messagelib->getMessage($message_id, $receiver_id);
+ }
+
+ if (isError($getMessageFromIds) || !hasData($getMessageFromIds))
+ {
+ return array();
+ }
+ else
+ {
+ return array(getData($getMessageFromIds)[0]);
+ }
+ }
+
+ //------------------------------------------------------------------------------------------------------------------
+ // Private methods
+
+ /**
+ * Returns the current authenticated person object
+ */
+ private function _getAuthUser()
+ {
$this->load->model('person/Person_model', 'PersonModel');
$authUser = $this->PersonModel->getByUid(getAuthUID());
- if (!hasData($authUser)) $authUser = error('The current logged user person_id is not defined');
-
return $authUser;
}
/**
- *
+ * Replaces data array keys to a lowercase string with underscores instead of spaces
*/
- public function replaceKeys($data)
+ private function _lowerReplaceSpaceArrayKeys($data)
{
$tmpData = array();
- // Replaces data array keys to a lowercase without spaces string
foreach ($data as $key => $val)
{
$tmpData[str_replace(' ', '_', strtolower($key))] = $val;
@@ -203,9 +317,9 @@ class Messages_model extends CI_Model
}
/**
- *
+ * Add organisation unit to an array of prestudents (objects)
*/
- public function addOeToPrestudents(&$msgVarsData, $prestudentsData)
+ private function _addOeToPrestudents(&$msgVarsData, $prestudentsData)
{
for ($i = 0; $i < count(getData($msgVarsData)); $i++)
{
@@ -219,4 +333,127 @@ class Messages_model extends CI_Model
}
}
}
+
+ /**
+ * Perform a person log after a message is sent
+ */
+ private function _personLog($sender_id, $receiver_id, $message_id)
+ {
+ $_personLog = $this->personloglib->log(
+ $receiver_id,
+ 'Action',
+ array(
+ 'name' => 'Message sent',
+ 'message' => 'Message sent from person '.$sender_id.' to '.$receiver_id.', message id: '.$message_id,
+ 'success' => 'true'
+ ),
+ 'kommunikation',
+ 'core',
+ null,
+ getAuthUID()
+ );
+
+ return $_personLog;
+ }
+
+ /**
+ * Prepares data for the view system/messages/htmlWriteTemplate using the given parameters
+ */
+ private function _prepareHtmlWriteTemplate($info, $message_id, $recipient_id)
+ {
+ // Checks that info parameter is valid
+ if (isError($info)) show_error(getData($info));
+ if (!hasData($info)) show_error('No recipients were given');
+
+ // If the message id and recipient id are given, then both they must be valid numbers
+ if ((is_numeric($message_id) && !is_numeric($recipient_id))
+ || (!is_numeric($message_id) && is_numeric($recipient_id)))
+ {
+ show_error('If given, message id and recipient id both must be valid numbers');
+ }
+
+ // ---------------------------------------------------------------------------------------
+ // Retrieves the recipients information and builds:
+ // - recipientsArray: an array that contains objects with id (person_id) and description (Vorname + Nachname) of recipient
+ // - recipientsList: a string that contains all the recipients descriptions (Vorname + Nachname) separated by ;
+ // - persons: a string that contains HTML input hidden with alla the receivers id (person_id)
+ $recipientsArray = array();
+ $recipientsList = '';
+ $persons = '';
+ foreach (getData($info) as $receiver)
+ {
+ $recipient = new stdClass();
+ $recipient->id = $receiver->person_id;
+ $recipient->description = $receiver->Vorname.' '.$receiver->Nachname;
+
+ $recipientsArray[] = $recipient;
+ $recipientsList .= $receiver->Vorname.' '.$receiver->Nachname.'; ';
+ $persons .= ''."\n";
+ }
+
+ // ---------------------------------------------------------------------------------------
+ // Retrieves the message to reply to, if it is specified by parameters $message_id and $recipient_id
+ $replySubject = ''; // message reply subject
+ $replyBody = ''; // message reply body
+ $relationmessage = ''; // input hidden that contains the message id to be replied to
+ // If both are given and they are valid
+ if (is_numeric($message_id) && is_numeric($recipient_id))
+ {
+ // Retrieves a received message from tbl_msg_recipient
+ $messageResult = $this->messagelib->getMessage($message_id, $recipient_id);
+ if (isError($messageResult)) show_error(getData($messageResult));
+ if (!hasData($messageResult)) show_error('The selected message does not exist');
+
+ $message = getData($messageResult)[0];
+
+ $replySubject = self::REPLY_SUBJECT_PREFIX.$message->subject;
+ $replyBody = self::REPLY_BODY_PREFIX.$message->body;
+ $relationmessage = '';
+ }
+
+ // ---------------------------------------------------------------------------------------
+ // Retrieves message vars from database view vw_msg_vars_person
+ $variablesResult = $this->messagelib->getMessageVarsPerson();
+ if (isError($variablesResult)) show_error(getData($variablesResult));
+
+ // Then builds an array that contains objects with id (person_id) and description (Vorname + Nachname) of recipient
+ $variables = array();
+ foreach (getData($variablesResult) as $id => $description)
+ {
+ $tmpVar = new stdClass();
+ $tmpVar->id = $id;
+ $tmpVar->description = $description;
+
+ $variables[] = $tmpVar;
+ }
+
+ // ---------------------------------------------------------------------------------------
+ // 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;
+
+ // ---------------------------------------------------------------------------------------
+ // Organisation units and a boolean (true if the sender is administrator) are used to get the templates
+ $organisationUnits = $this->messagelib->getOeKurzbz($sender_id);
+ if (isError($organisationUnits)) show_error(getData($organisationUnits));
+ $senderIsAdmin = $this->BenutzerrolleModel->isAdminByPersonId($sender_id);
+ if (isError($senderIsAdmin)) show_error(getData($senderIsAdmin));
+
+ // ---------------------------------------------------------------------------------------
+ // Returns data as an array
+ return array (
+ 'recipientsList' => $recipientsList,
+ 'subject' => $replySubject,
+ 'body' => $replyBody,
+ 'variables' => $variables,
+ 'organisationUnits' => getData($organisationUnits),
+ 'senderIsAdmin' => getData($senderIsAdmin),
+ 'recipientsArray' => $recipientsArray,
+ 'persons' => $persons,
+ 'relationmessage_id' => $relationmessage
+ );
+ }
}
diff --git a/application/views/system/infocenter/stammdaten.php b/application/views/system/infocenter/stammdaten.php
index 2b481692e..ebd812c6a 100644
--- a/application/views/system/infocenter/stammdaten.php
+++ b/application/views/system/infocenter/stammdaten.php
@@ -113,7 +113,7 @@