diff --git a/application/config/message.php b/application/config/message.php index af5861c55..29b354f7d 100644 --- a/application/config/message.php +++ b/application/config/message.php @@ -5,6 +5,8 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); $config['msg_delivery'] = true; $config['send_immediately'] = false; // If the message should be sent immediately $config['system_person_id'] = 1; // Dummy sender, used for sending messages from the system +$config['cis_message_view_url'] = 'readMessageByToken.php'; // Dummy sender, used for sending messages from the system +$config['message_html_view_url'] = 'index.ci.php/system/Messages/toHTML/'; define('EMAIL_KONTAKT_TYPE', 'email'); // Email kontakt type define('SENT_INFO_NEWLINE', '\n'); // tbl_msg_recipient->sentInfo separator @@ -57,4 +59,5 @@ define('MSG_ERR_INVALID_SENDER_ID', 104); define('MSG_ERR_INVALID_RECIPIENTS', 105); define('MSG_ERR_INVALID_RECEIVER_ID', 106); define('MSG_ERR_INVALID_OU', 107); -define('MSG_ERR_INVALID_TEMPLATE', 108); \ No newline at end of file +define('MSG_ERR_INVALID_TEMPLATE', 108); +define('MSG_ERR_INVALID_TOKEN', 109); \ No newline at end of file diff --git a/application/controllers/system/Messages.php b/application/controllers/system/Messages.php index 80f846786..6b7939e80 100755 --- a/application/controllers/system/Messages.php +++ b/application/controllers/system/Messages.php @@ -1,19 +1,19 @@ load->library("MessageLib"); - $this->load->model("person/Person_model", "PersonModel"); + $this->load->library('MessageLib'); + $this->load->model('person/Person_model', 'PersonModel'); } public function index() { - $this->load->view("system/messages.php", array("person_id" => $this->getPersonId())); + $this->load->view('system/messages.php', array('person_id' => $this->getPersonId())); } public function inbox($person_id) @@ -31,11 +31,11 @@ class Messages extends VileSci_Controller } $data = array ( - "messages" => $msg->retval, - "person" => $person->retval[0] + 'messages' => $msg->retval, + 'person' => $person->retval[0] ); - $this->load->view("system/messagesInbox.php", $data); + $this->load->view('system/messagesInbox.php', $data); } public function outbox($person_id) @@ -53,11 +53,11 @@ class Messages extends VileSci_Controller } $data = array ( - "messages" => $msg->retval, - "person" => $person->retval[0] + 'messages' => $msg->retval, + 'person' => $person->retval[0] ); - $this->load->view("system/messagesOutbox.php", $data); + $this->load->view('system/messagesOutbox.php', $data); } public function view($msg_id, $person_id) @@ -68,7 +68,7 @@ class Messages extends VileSci_Controller show_error($msg->retval); } - $v = $this->load->view("system/messageView", array("message" => $msg->retval[0])); + $v = $this->load->view('system/messageView', array('message' => $msg->retval[0])); } public function reply($msg_id, $person_id) @@ -79,15 +79,15 @@ class Messages extends VileSci_Controller show_error($msg->retval); } - $v = $this->load->view("system/messageReply", array("message" => $msg->retval[0])); + $v = $this->load->view('system/messageReply', array('message' => $msg->retval[0])); } public function sendReply($msg_id, $person_id) { - $subject = $this->input->post("subject"); - $body = $this->input->post("body"); + $subject = $this->input->post('subject'); + $body = $this->input->post('body'); - $this->load->model("system/Message_model", "MessageModel"); + $this->load->model('system/Message_model', 'MessageModel'); $originMsg = $this->MessageModel->load($msg_id); if ($originMsg->error) { @@ -100,7 +100,7 @@ class Messages extends VileSci_Controller show_error($msg->retval); } - redirect("/system/Messages/view/" . $msg->retval . "/" . $originMsg->retval[0]->person_id); + redirect('/system/Messages/view/' . $msg->retval . '/' . $originMsg->retval[0]->person_id); } public function write($sender_id, $receiver_id) @@ -112,20 +112,20 @@ class Messages extends VileSci_Controller } $data = array ( - "sender_id" => $sender_id, - "receiver_id" => $receiver_id, - "receiver" => $person->retval[0] + 'sender_id' => $sender_id, + 'receiver_id' => $receiver_id, + 'receiver' => $person->retval[0] ); - $v = $this->load->view("system/messageWrite", $data); + $v = $this->load->view('system/messageWrite', $data); } public function send($sender_id, $receiver_id) { - $subject = $this->input->post("subject"); - $body = $this->input->post("body"); + $subject = $this->input->post('subject'); + $body = $this->input->post('body'); - $this->load->model("system/Message_model", "MessageModel"); + $this->load->model('system/Message_model', 'MessageModel'); $originMsg = $this->MessageModel->load($msg_id); if ($originMsg->error) { @@ -138,25 +138,43 @@ class Messages extends VileSci_Controller show_error($msg->retval); } - redirect("/system/Messages/view/" . $msg->retval . "/" . $receiver_id); + redirect('/system/Messages/view/' . $msg->retval . '/' . $receiver_id); + } + + public function toHTML($token) + { + $msg = $this->messagelib->getMessageByToken($token); + if ($msg->error) + { + show_error($msg->retval); + } + + if (is_array($msg->retval) && count($msg->retval) > 0) + { + $data = array ( + 'message' => $msg->retval[0] + ); + + $this->load->view('system/messageHTML.php', $data); + } } private function getPersonId() { $person_id = null; - if ($this->input->get("person_id") !== null) + if ($this->input->get('person_id') !== null) { - $person_id = $this->input->get("person_id"); + $person_id = $this->input->get('person_id'); } - else if ($this->input->post("person_id") !== null) + else if ($this->input->post('person_id') !== null) { - $person_id = $this->input->get("person_id"); + $person_id = $this->input->get('person_id'); } if (!is_numeric($person_id)) { - show_error("Person_id is not numeric"); + show_error('Person_id is not numeric'); } return $person_id; @@ -164,12 +182,12 @@ class Messages extends VileSci_Controller public function getVorlage() { - $vorlage_kurzbz = $this->input->get("vorlage_kurzbz"); + $vorlage_kurzbz = $this->input->get('vorlage_kurzbz'); if (isset($vorlage_kurzbz)) { - $this->load->model("system/Vorlagestudiengang_model", "VorlagestudiengangModel"); - $result = $this->VorlagestudiengangModel->loadWhere(array("vorlage_kurzbz" => $vorlage_kurzbz)); + $this->load->model('system/Vorlagestudiengang_model', 'VorlagestudiengangModel'); + $result = $this->VorlagestudiengangModel->loadWhere(array('vorlage_kurzbz' => $vorlage_kurzbz)); $this->output ->set_content_type('application/json') diff --git a/application/libraries/MailLib.php b/application/libraries/MailLib.php index 4272e6620..fcb450848 100644 --- a/application/libraries/MailLib.php +++ b/application/libraries/MailLib.php @@ -45,7 +45,7 @@ class MailLib /** * Sends a single email */ - public function send($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null) + public function send($from, $to, $subject, $message, $alias = "", $cc = null, $bcc = null, $altMessage = '') { // If from is not specified then use the standard one if (is_null($from) || $from == "") @@ -59,6 +59,7 @@ class MailLib if (!is_null($bcc)) $this->ci->email->bcc($bcc); $this->ci->email->subject($subject); $this->ci->email->message($message); + if (!empty($altMessage)) $this->ci->email->set_alt_message($altMessage); // Avoid printing on standard output ugly error messages $result = @$this->ci->email->send(); diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 080797bdd..aafaf5042 100644 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -113,7 +113,7 @@ class MessageLib public function getMessageByToken($token) { if (empty($token)) - return $this->_error('', MSG_ERR_INVALID_MSG_ID); + return $this->_error('', MSG_ERR_INVALID_TOKEN); $result = $this->ci->RecipientModel->getMessageByToken($token); if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) @@ -138,7 +138,12 @@ class MessageLib 'status' => MSG_STATUS_READ ); - $result = $this->ci->MsgStatusModel->insert($statusKey); + $resultIns = $this->ci->MsgStatusModel->insert($statusKey); + // If an error occured while writing on data base, then return it + if ($resultIns->error == EXIT_ERROR) + { + $result = $resultIns; + } } } @@ -236,7 +241,8 @@ class MessageLib $msg_id = $result->retval; $recipientData = array( 'person_id' => $receiver_id, - 'message_id' => $msg_id + 'message_id' => $msg_id, + 'token' => generateToken() ); $result = $this->ci->RecipientModel->insert($recipientData); if (is_object($result) && $result->error == EXIT_SUCCESS) @@ -259,7 +265,7 @@ class MessageLib if ($this->ci->config->item('send_immediately') === true) { // Send message by email! - $resultSendEmail = $this->sendOne($msg_id, $subject, $parsedText); + $resultSendEmail = $this->sendOne($msg_id, $subject, $body); } }*/ } @@ -506,11 +512,31 @@ class MessageLib // If the person has an email account if (!is_null($result->retval[$i]->receiver) && $result->retval[$i]->receiver != '') { - // Using a template as email body - $body = $this->ci->parser->parse('templates/mail', array('body' => $result->retval[$i]->body), true); + $href = CIS_ROOT . $this->ci->config->item('cis_message_view_url') . '?token=' . $result->retval[$i]->token; + // Using a template for the html email body + $body = $this->ci->parser->parse( + 'templates/mailHTML', + array( + 'src' => APP_ROOT . $this->ci->config->item('message_html_view_url') . $result->retval[$i]->token, + 'href' => $href + ), + true + ); if (is_null($body) || $body == '') { - // $body = $result->retval[$i]->body; + $this->ci->loglib->logError('Error while parsing the mail template'); + } + + // Using a template for the plain text email body + $altBody = $this->ci->parser->parse( + 'templates/mailTXT', + array( + 'href' => $href + ), + true + ); + if (is_null($altBody) || $altBody == '') + { $this->ci->loglib->logError('Error while parsing the mail template'); } @@ -525,7 +551,11 @@ class MessageLib $sender, $result->retval[$i]->receiver, $result->retval[$i]->subject, - $body + $body, + null, + null, + null, + $altBody ); // If errors were occurred while sending the email if (!$sent) @@ -533,10 +563,10 @@ class MessageLib $this->ci->loglib->logError('Error while sending an email'); // Writing errors in tbl_message_status $sme = $this->setMessageError( - $result->retval[$i]->message_id, - $result->retval[$i]->receiver_id, - 'Error while sending an email', - $result->retval[$i]->sentinfo + $result->retval[$i]->message_id, + $result->retval[$i]->receiver_id, + 'Error while sending an email', + $result->retval[$i]->sentinfo ); if (!$sme) { @@ -613,16 +643,38 @@ class MessageLib // Using a template as email body if it is not given as method parameter if (is_null($body)) { - $bodyMsg = $this->ci->parser->parse('templates/mail', array('body' => $result->retval[0]->body), true); + // Using a template for the html email body + $href = CIS_ROOT . $this->ci->config->item('cis_message_view_url') . '?token=' . $result->retval[$i]->token; + $bodyMsg = $this->ci->parser->parse( + 'templates/mailHTML', + array( + 'src' => APP_ROOT . $this->ci->config->item('message_html_view_url') . $result->retval[$i]->token, + 'href' => $href + ), + true + ); if (is_null($bodyMsg) || $bodyMsg == '') { // $body = $result->retval[0]->body; - $this->ci->loglib->logError('Error while parsing the mail template'); + $this->ci->loglib->logError('Error while parsing the html mail template'); + } + + // Using a template for the plain text email body + $altBody = $this->ci->parser->parse( + 'templates/mailTXT', + array( + 'href' => $href + ), + true + ); + if (is_null($altBody) || $altBody == '') + { + $this->ci->loglib->logError('Error while parsing the plain text mail template'); } } else { - $bodyMsg = $body; + $bodyMsg = $altBody = $body; } // If the sender kontakt does not exist, then use system @@ -637,7 +689,11 @@ class MessageLib $sender, $result->retval[0]->receiver, is_null($subject) ? $result->retval[0]->subject : $subject, // if parameter subject is not null, use it! - $bodyMsg + $bodyMsg, + null, + null, + null, + $altBody ); // If errors were occurred while sending the email if (!$sent) diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index d39595275..27d38377a 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -68,8 +68,8 @@ class Recipient_model extends DB_Model return $this->_error(lang("fhc_".FHC_NORIGHT)." -> ".$this->getBerechtigungKurzbz("public.tbl_msg_status"), FHC_MODEL_ERROR); $sql = "SELECT r.message_id, - r.person_id as receiver_id, m.person_id as sender_id, + r.person_id as receiver_id, m.subject, m.body, m.insertamum, @@ -80,13 +80,12 @@ class Recipient_model extends DB_Model s.insertamum as statusamum FROM public.tbl_msg_recipient r JOIN public.tbl_msg_message m USING (message_id) JOIN ( - SELECT * FROM public.tbl_msg_status ORDER BY insertamum DESC LIMIT 1 + SELECT * FROM public.tbl_msg_status WHERE status < ? ORDER BY insertamum DESC, status DESC ) s ON (r.message_id = s.message_id AND r.person_id = s.person_id) WHERE r.token = ? - AND status < ? - ORDER BY s.insertamum DESC"; + LIMIT 1"; - $result = $this->db->query($sql, array($token, MSG_STATUS_DELETED)); + $result = $this->db->query($sql, array(MSG_STATUS_DELETED, $token)); if (is_object($result)) return $this->_success($result->result()); else @@ -236,6 +235,7 @@ class Recipient_model extends DB_Model ks.kontakt as sender, kr.kontakt as receiver, mr.person_id as receiver_id, + mr.token, mm.subject, mm.body, mr.sentinfo diff --git a/application/views/system/messageHTML.php b/application/views/system/messageHTML.php new file mode 100644 index 000000000..7a005a228 --- /dev/null +++ b/application/views/system/messageHTML.php @@ -0,0 +1,6 @@ +