From 0db611ed270d2128cc80b37f0707a8cdf8dc2dd5 Mon Sep 17 00:00:00 2001 From: Paolo Date: Tue, 18 Feb 2020 15:19:58 +0100 Subject: [PATCH] - Added new config entry in mail config file called enable_debug - If enable_debug is true then info about the mail are logged into the CI error logs - Fixed queries to get sent and received messages with Recipient_model, now messages for which a notice email was not sent are retrieved and messages are sorted by insertamum - CL/Messages_model: - Before setting a message as read check if a record is already present - If the recipient is a prestudent then in table tbl_msg_message oe_kurzbz is set with the ou of the prestudent - The sender id (person_id) now is get from the currently logged user also when replying - MessageLib->_sendNoticeEmails checks that the message was sent only from FAS (NOT from infocenter) --- application/config/mail.php | 3 + application/libraries/MailLib.php | 50 ++++++++++----- application/libraries/MessageLib.php | 7 ++- application/models/CL/Messages_model.php | 61 +++++++++++++++---- application/models/system/Recipient_model.php | 22 +++---- 5 files changed, 100 insertions(+), 43 deletions(-) diff --git a/application/config/mail.php b/application/config/mail.php index 2163bc122..9d577720d 100644 --- a/application/config/mail.php +++ b/application/config/mail.php @@ -29,3 +29,6 @@ $config['wrapchars'] = 76; // Character count to wrap at. $config['mailtype'] = 'html'; // html or text $config['priority'] = 3; // Email Priority. 1 = highest. 5 = lowest. 3 = normal $config['validate'] = false; // If true then the email address will be validated + +// If enabled will be logged info about emails in Codeigniter error logs +$config['enable_debug'] = false; diff --git a/application/libraries/MailLib.php b/application/libraries/MailLib.php index e9e571f6c..dbbc22f08 100644 --- a/application/libraries/MailLib.php +++ b/application/libraries/MailLib.php @@ -7,6 +7,8 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); */ class MailLib { + const ENABLE_DEBUG = 'enable_debug'; + private $sended; // Sended email counter // Properties for storing the configuration @@ -15,6 +17,8 @@ class MailLib private $email_time_range; private $email_from_system; + private $_ci; // Codeigniter instance + /** * Class constructor */ @@ -24,16 +28,16 @@ class MailLib $this->sended = 0; // Get CI instance - $this->ci =& get_instance(); + $this->_ci =& get_instance(); // The second parameter is used to avoiding name collisions in the config array - $this->ci->config->load('mail', true); + $this->_ci->config->load('mail', true); // CI Email library - $this->ci->load->library('email'); + $this->_ci->load->library('email'); // Initializing email library with the loaded configurations - $this->ci->email->initialize($this->ci->config->config['mail']); + $this->_ci->email->initialize($this->_ci->config->config['mail']); // Set the configuration properties with the standard configuration values $this->email_number_to_sent = $this->getEmailCfgItem('email_number_to_sent'); @@ -48,6 +52,22 @@ class MailLib */ public function send($from, $to, $subject, $message, $alias = '', $cc = null, $bcc = null, $altMessage = '', $bulk = false, $autogenerated = false) { + // If it is configured then log mail info into the CI error logs + if ($this->getEmailCfgItem(self::ENABLE_DEBUG) === true) + { + $this->_ci->load->library('LogLib'); // Loads logging library + + // Log them all! + $this->_ci->loglib->logError('From: '.$from); + $this->_ci->loglib->logError('To: '.$to); + $this->_ci->loglib->logError('Subject: '.$subject); + $this->_ci->loglib->logError('Message: '.$message); + $this->_ci->loglib->logError('Alias: '.$alias); + $this->_ci->loglib->logError('CC: '.$cc); + $this->_ci->loglib->logError('BCC: '.$bcc); + $this->_ci->loglib->logError('Alternative message: '.$altMessage); + } + // If from is not specified then use the standard one if (is_null($from) || $from == '') { @@ -67,7 +87,7 @@ class MailLib } } - $this->ci->email->from($from, $alias); + $this->_ci->email->from($from, $alias); // Check if the email address of the debug recipient is a valid one $recipient = $to; @@ -83,20 +103,20 @@ class MailLib $recipientBCC = MAIL_DEBUG; } - $this->ci->email->to($recipient); - if (!is_null($recipientCC)) $this->ci->email->cc($recipientCC); - if (!is_null($recipientBCC)) $this->ci->email->bcc($recipientBCC); - $this->ci->email->subject($subject); - $this->ci->email->message($message); - if (!isEmptyString($altMessage)) $this->ci->email->set_alt_message($altMessage); + $this->_ci->email->to($recipient); + if (!is_null($recipientCC)) $this->_ci->email->cc($recipientCC); + if (!is_null($recipientBCC)) $this->_ci->email->bcc($recipientBCC); + $this->_ci->email->subject($subject); + $this->_ci->email->message($message); + if (!isEmptyString($altMessage)) $this->_ci->email->set_alt_message($altMessage); if($bulk) - $this->ci->email->set_header('Precedence', 'bulk'); + $this->_ci->email->set_header('Precedence', 'bulk'); if($autogenerated) - $this->ci->email->set_header('Auto-Submitted', 'auto-generated'); + $this->_ci->email->set_header('Auto-Submitted', 'auto-generated'); // Avoid printing on standard output ugly error messages - $result = @$this->ci->email->send(); + $result = @$this->_ci->email->send(); // If the email was succesfully sended then increment the counter // and checks if it has to wait until the sending of the next @@ -171,6 +191,6 @@ class MailLib */ private function getEmailCfgItem($itemName) { - return $this->ci->config->item($itemName, EMAIL_CONFIG_INDEX); + return $this->_ci->config->item($itemName, EMAIL_CONFIG_INDEX); } } diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 240055978..3a8f133cf 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -575,8 +575,9 @@ class MessageLib // Use the degree program email if (hasData($studiengangResult)) $message->receiverContact = getData($studiengangResult)[0]->email; } - // If message was sent from FAS - elseif (!isEmptyString($message->sender_ou)) + // If message was sent from FAS and NOT from infocenter + elseif (!isEmptyString($message->sender_ou) + && !array_search($message->sender_ou, $this->_ci->config->item(self::CFG_OU_RECEIVERS_NO_NOTICE))) { // If the recipient organisation unit is NOT in the list of organisation units that sent only to private emails if (array_search($message->receiver_ou, $this->_ci->config->item(self::CFG_OU_RECEIVERS_PRIVATE)) === false) @@ -592,7 +593,7 @@ class MessageLib } // Otherwise try with the private email - if ($message->receiverContact == null) + if (isEmptyString($message->receiverContact)) { $privateEmailResult = $this->_getPrivateEmail($message->receiver_id); if (isError($privateEmailResult)) return $privateEmailResult; // if an error occured then return it diff --git a/application/models/CL/Messages_model.php b/application/models/CL/Messages_model.php index e2c5c34c2..a841e266a 100644 --- a/application/models/CL/Messages_model.php +++ b/application/models/CL/Messages_model.php @@ -62,15 +62,31 @@ class Messages_model extends CI_Model // 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, - 'insertvon' => getAuthUID() + $statuResult = $this->MsgStatusModel->loadWhere( + array( + 'message_id' => $message_id, + 'person_id' => $person_id, + 'status' => MSG_STATUS_READ + ) ); - return $this->MsgStatusModel->insert($statusData); // insert and return result + if (isError($statuResult)) return $statuResult; + if (!hasData($statuResult)) + { + // Set date used to insert + return $this->MsgStatusModel->insert( + array( + 'message_id' => $message_id, + 'person_id' => $person_id, + 'status' => MSG_STATUS_READ, + 'insertvon' => getAuthUID() + ) + ); // insert and return result + } + else + { + return success('Already set as read'); + } } /** @@ -348,23 +364,43 @@ class Messages_model extends CI_Model elseif ($type == self::TYPE_PRESTUDENTS) // otherwise prestudents were given { $msgVarsData = $this->MessageModel->getMsgVarsDataByPrestudentId($recipients_ids); + + // Retrieve organisation unit for the recipients + $organisationUnitsResult = $this->PrestudentModel->getOrganisationunits($recipients_ids); + if (isError($organisationUnitsResult)) return $organisationUnitsResult; + if (hasData($organisationUnitsResult)) $senderOUArray = getData($organisationUnitsResult); } if (isError($msgVarsData)) show_error(getError($msgVarsData)); if (!hasData($msgVarsData)) show_error('No recipients were given'); + $senderOU = null; // sender organisation unit only for presetudents + $receiversCounter = 0; // a counter + + // Looping on receivers data foreach (getData($msgVarsData) as $receiver) { $msgVarsDataArray = $this->_lowerReplaceSpaceArrayKeys((array)$receiver); // replaces array keys - $parsedSubject = parseText($subject, $msgVarsDataArray); $parsedBody = parseText($body, $msgVarsDataArray); + // If exist an organisation unit for this prestudent and it is valid + if (isset($senderOUArray[$receiversCounter]) + && isset($senderOUArray[$receiversCounter]->oe_kurzbz) + && !isEmptyString($senderOUArray[$receiversCounter]->oe_kurzbz)) + { + $senderOU = $senderOUArray[$receiversCounter]->oe_kurzbz; + } + else + { + $senderOU = null; + } + $message = $this->messagelib->sendMessageUser( $msgVarsDataArray['person_id'], // receiverPersonId $parsedSubject, // subject $parsedBody, // body $sender_id, // sender_id - null, // senderOU + $senderOU, // senderOU $relationmessage_id, // relationmessage_id MSG_PRIORITY_NORMAL // priority ); @@ -378,6 +414,8 @@ class Messages_model extends CI_Model $personLog = $this->_personLog($sender_id, $msgVarsDataArray['person_id'], getData($message)[0]); if (isError($personLog)) return $personLog; } + + $receiversCounter++; // increment the counter } return success('Messages sent successfully'); @@ -452,7 +490,8 @@ class Messages_model extends CI_Model show_error('An error occurred while sending your message, please contact the site administrator'); } - $sender_id = getData($messageResult)[0]->receiver_id; + $sender_id = getAuthPersonId(); + if (!is_numeric($sender_id)) return error('The current logged user person_id is not defined'); $message = $this->messagelib->sendMessageUser( $receiver_id, // receiverPersonId @@ -484,6 +523,7 @@ class Messages_model extends CI_Model if (isEmptyString($body)) return error('Body is an empty string'); $sender_id = getAuthPersonId(); + if (!is_numeric($sender_id)) return error('The current logged user person_id is not defined'); $message = $this->messagelib->sendMessageOU( $receiverOU, // receiverPersonId @@ -672,7 +712,6 @@ class Messages_model extends CI_Model */ private function _prepareHtmlWriteTemplate($info, $message_id, $recipient_id) { - // Checks that info parameter is valid if (isError($info)) show_error(getError($info)); if (!hasData($info)) show_error('No recipients were given'); diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index bc7ab246f..cea11a7a7 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -314,7 +314,7 @@ class Recipient_model extends DB_Model mm.relationmessage_id, mm.subject, mm.body, - mr.sent AS sent, + mm.insertamum AS sent, p.vorname, p.nachname, MAX(ms.status) AS status, @@ -325,13 +325,11 @@ class Recipient_model extends DB_Model 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, + mm.insertamum, p.vorname, p.nachname, ms.person_id, @@ -342,7 +340,7 @@ class Recipient_model extends DB_Model mm.relationmessage_id, mm.subject, mm.body, - mrou.sent AS sent, + mm.insertamum AS sent, pr.vorname, pr.nachname, MAX(ms.status) AS status, @@ -362,13 +360,11 @@ class Recipient_model extends DB_Model JOIN public.tbl_msg_status ms ON (ms.message_id = mrou.message_id AND ms.person_id = mrou.person_id) JOIN public.tbl_person pr ON (pr.person_id = mm.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, + mm.insertamum, pr.vorname, pr.nachname, ms.person_id, @@ -387,7 +383,7 @@ class Recipient_model extends DB_Model mm.relationmessage_id, mm.subject, mm.body, - mr.sent, + mm.insertamum AS sent, p.person_id, p.vorname, p.nachname, @@ -397,24 +393,22 @@ class Recipient_model extends DB_Model mr.token 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_msg_status ms ON (ms.message_id = mm.message_id AND ms.person_id = mr.person_id) JOIN public.tbl_person p ON (p.person_id = mr.person_id) LEFT JOIN public.tbl_organisationseinheit oe ON (oe.oe_kurzbz = mr.oe_kurzbz) 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, + mm.insertamum, p.person_id, p.vorname, p.nachname, ms.person_id, oe.bezeichnung, mr.token - ORDER BY mr.sent DESC'; + ORDER BY sent DESC'; return $this->execQuery($sql, array($person_id)); }