From ea920d11d406d30d60940bc5230c4b339b678ef5 Mon Sep 17 00:00:00 2001 From: bison-paolo Date: Fri, 14 Oct 2016 11:32:17 +0200 Subject: [PATCH] - Added private method _saveMessage in library MessageLib - Bug fixed in model Studiensemester_model - Config property send_immediately set to false, default value --- application/config/message.php | 4 +- application/libraries/MessageLib.php | 180 +++++++----------- .../organisation/Studiensemester_model.php | 2 +- 3 files changed, 75 insertions(+), 111 deletions(-) diff --git a/application/config/message.php b/application/config/message.php index 73d81a8f8..18e4e997e 100644 --- a/application/config/message.php +++ b/application/config/message.php @@ -2,8 +2,8 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); -$config['msg_delivery'] = true; -$config['send_immediately'] = true; // If the message should be sent immediately +$config['msg_delivery'] = true; // Default true +$config['send_immediately'] = false; // If the message should be sent immediately. Default false $config['system_person_id'] = 1; // Dummy sender, used for sending messages from the system $config['redirect_view_message_url'] = 'index.ci.php/Redirect/redirectByToken/'; // $config['message_html_view_url'] = 'index.ci.php/ViewMessage/toHTML/'; diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 09891583c..26d80e95d 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -234,66 +234,18 @@ class MessageLib // If the text and the subject of the template are not empty if (!empty($subject) && !empty($body)) { - $this->ci->db->trans_start(false); - // Save Message - $msgData = array( - 'person_id' => $sender_id, - 'subject' => $subject, - 'body' => $body, - 'priority' => PRIORITY_NORMAL, - 'relationmessage_id' => $relationmessage_id, - 'oe_kurzbz' => $oe_kurzbz - ); - $result = $this->ci->MessageModel->insert($msgData); + $result = $this->_saveMessage($sender_id, $receiver_id, $subject, $body, $relationmessage_id, $oe_kurzbz); + // If no errors were occurred + // Leave the code commented if (is_object($result) && $result->error == EXIT_SUCCESS) { - // Link the message with the receiver - $msg_id = $result->retval; - $recipientData = array( - 'person_id' => $receiver_id, - 'message_id' => $msg_id, - 'token' => generateToken() - ); - $result = $this->ci->RecipientModel->insert($recipientData); - if (is_object($result) && $result->error == EXIT_SUCCESS) + // If the system is configured to send messages immediately + if ($this->ci->config->item('send_immediately') === true) { - // Save message status - $statusData = array( - 'message_id' => $msg_id, - 'person_id' => $receiver_id, - 'status' => MSG_STATUS_UNREAD - ); - $result = $this->ci->MsgStatusModel->insert($statusData); - - // If no errors were occurred - /** - * TODO: different config item??? - */ - /*if (is_object($result) && $result->error == EXIT_SUCCESS) - { - // If the system is configured to send messages immediately - if ($this->ci->config->item('send_immediately') === true) - { - // Send message by email! - $resultSendEmail = $this->sendOne($msg_id, $subject, $body); - } - }*/ + // Send message by email! + $resultSendEmail = $this->sendOne($result->retval, $subject, $body); } } - - $this->ci->db->trans_complete(); - - if ($this->ci->db->trans_status() === false || (is_object($result) && $result->error != EXIT_SUCCESS)) - { - $this->ci->db->trans_rollback(); - $result = $this->_error($result->msg, EXIT_ERROR); - break; - } - else - { - $this->ci->db->trans_commit(); - $result = $this->_success($msg_id); - } } else { @@ -385,64 +337,19 @@ class MessageLib // Parses template text $parsedText = $this->ci->vorlagelib->parseVorlagetext($result->retval[0]->text, $data); $subject = $result->retval[0]->subject; - - $this->ci->db->trans_start(false); - // Save Message - $msgData = array( - 'person_id' => $sender_id, - 'subject' => $subject, - 'body' => $parsedText, - 'priority' => PRIORITY_NORMAL, - 'relationmessage_id' => $relationmessage_id, - 'oe_kurzbz' => $oe_kurzbz - ); - $result = $this->ci->MessageModel->insert($msgData); + + // Save message + $result = $this->_saveMessage($sender_id, $receiver_id, $subject, $parsedText, $relationmessage_id, $oe_kurzbz); + // If no errors were occurred if (is_object($result) && $result->error == EXIT_SUCCESS) { - // Link the message with the receiver - $msg_id = $result->retval; - $recipientData = array( - 'person_id' => $receiver_id, - 'message_id' => $msg_id, - 'token' => generateToken() - ); - $result = $this->ci->RecipientModel->insert($recipientData); - if (is_object($result) && $result->error == EXIT_SUCCESS) + // If the system is configured to send messages immediately + if ($this->ci->config->item('send_immediately') === true) { - // Save message status - $statusData = array( - 'message_id' => $msg_id, - 'person_id' => $receiver_id, - 'status' => MSG_STATUS_UNREAD - ); - $result = $this->ci->MsgStatusModel->insert($statusData); - - // If no errors were occurred - if (is_object($result) && $result->error == EXIT_SUCCESS) - { - // If the system is configured to send messages immediately - if ($this->ci->config->item('send_immediately') === true) - { - // Send message by email! - $resultSendEmail = $this->sendOne($msg_id, $subject, $parsedText); - } - } + // Send message by email! + $resultSendEmail = $this->sendOne($result->retval, $subject, $parsedText); } } - - $this->ci->db->trans_complete(); - - if ($this->ci->db->trans_status() === false || (is_object($result) && $result->error != EXIT_SUCCESS)) - { - $this->ci->db->trans_rollback(); - $result = $this->_error($result->msg, EXIT_ERROR); - break; - } - else - { - $this->ci->db->trans_commit(); - $result = $this->_success($msg_id); - } } else { @@ -884,6 +791,63 @@ class MessageLib return false; } + /** + * Save a message in DB + **/ + private function _saveMessage($sender_id, $receiver_id, $subject, $body, $relationmessage_id, $oe_kurzbz) + { + // Starts db transaction + $this->ci->db->trans_start(false); + + // Save Message + $msgData = array( + 'person_id' => $sender_id, + 'subject' => $subject, + 'body' => $body, + 'priority' => PRIORITY_NORMAL, + 'relationmessage_id' => $relationmessage_id, + 'oe_kurzbz' => $oe_kurzbz + ); + $result = $this->ci->MessageModel->insert($msgData); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + // Link the message with the receiver + $msg_id = $result->retval; + $recipientData = array( + 'person_id' => $receiver_id, + 'message_id' => $msg_id, + 'token' => generateToken() + ); + $result = $this->ci->RecipientModel->insert($recipientData); + if (is_object($result) && $result->error == EXIT_SUCCESS) + { + // Save message status + $statusData = array( + 'message_id' => $msg_id, + 'person_id' => $receiver_id, + 'status' => MSG_STATUS_UNREAD + ); + $result = $this->ci->MsgStatusModel->insert($statusData); + } + } + + $this->ci->db->trans_complete(); + + if ($this->ci->db->trans_status() === false || (is_object($result) && $result->error != EXIT_SUCCESS)) + { + $this->ci->db->trans_rollback(); + $result = $this->_error($result->msg, EXIT_ERROR); + break; + } + else + { + $this->ci->db->trans_commit(); + $result = $this->_success($msg_id); + } + + return $result; + } + /** * Wrapper for function error */ diff --git a/application/models/organisation/Studiensemester_model.php b/application/models/organisation/Studiensemester_model.php index bc1c0436b..17fa706f9 100644 --- a/application/models/organisation/Studiensemester_model.php +++ b/application/models/organisation/Studiensemester_model.php @@ -88,7 +88,7 @@ class Studiensemester_model extends DB_Model $ss = 'WS'; } - $query .= ' WHERE SUBSTRING(studiensemester_kurzbz FROM 1 FOR 2) = '' . $ss . '''; + $query .= ' WHERE SUBSTRING(studiensemester_kurzbz FROM 1 FOR 2) = \'' . $ss . '\''; } $query .= ' ORDER BY delta LIMIT 1';