From 3e7402955268dba501b2f5216d9d066db36c0d5f Mon Sep 17 00:00:00 2001 From: Paolo Date: Thu, 7 Sep 2017 13:43:56 +0200 Subject: [PATCH] If the recipient is an employee and has a UID then send the email to UID@Domain --- application/libraries/MessageLib.php | 24 +++++-- application/models/system/Recipient_model.php | 67 ++++++++++--------- 2 files changed, 57 insertions(+), 34 deletions(-) diff --git a/application/libraries/MessageLib.php b/application/libraries/MessageLib.php index 7a1d8df8f..3b18874eb 100755 --- a/application/libraries/MessageLib.php +++ b/application/libraries/MessageLib.php @@ -422,7 +422,8 @@ class MessageLib for ($i = 0; $i < count($result->retval) && $sent; $i++) { // If the person has an email account - if (!is_null($result->retval[$i]->receiver) && $result->retval[$i]->receiver != '') + if ((!is_null($result->retval[$i]->receiver) && $result->retval[$i]->receiver != '') + || (!is_null($result->retval[$i]->employeecontact) && $result->retval[$i]->employeecontact != '')) { $href = $this->ci->config->item('message_server').$this->ci->config->item('message_html_view_url').$result->retval[0]->token; // Using a template for the html email body @@ -461,10 +462,17 @@ class MessageLib { $sender = $result->retval[0]->sender; } + + $receiverContanct = $result->retval[$i]->receiver; + if (!is_null($result->retval[$i]->employeecontact) && $result->retval[$i]->employeecontact != '') + { + $receiverContanct = $result->retval[$i]->employeecontact.'@'.DOMAIN; + } + // Sending email $sent = $this->ci->maillib->send( $sender, - $result->retval[$i]->receiver, + $receiverContanct, $result->retval[$i]->subject, $body, null, @@ -546,6 +554,7 @@ class MessageLib null, $message_id ); + // Checks if errors were occurred if (isSuccess($result)) { @@ -553,7 +562,8 @@ class MessageLib if (is_array($result->retval) && count($result->retval) > 0) { // If the person has an email account - if (!is_null($result->retval[0]->receiver) && $result->retval[0]->receiver != '') + if ((!is_null($result->retval[0]->receiver) && $result->retval[0]->receiver != '') + || (!is_null($result->retval[0]->employeecontact) && $result->retval[0]->employeecontact != '')) { // If it is required use a multi-part message in MIME format if ($multiPartMime === true) @@ -602,10 +612,16 @@ class MessageLib $sender = $result->retval[0]->sender; } + $receiverContanct = $result->retval[0]->receiver; + if (!is_null($result->retval[0]->employeecontact) && $result->retval[0]->employeecontact != '') + { + $receiverContanct = $result->retval[0]->employeecontact.'@'.DOMAIN; + } + // Sending email $sent = $this->ci->maillib->send( $sender, - $result->retval[0]->receiver, + $receiverContanct, is_null($subject) ? $result->retval[0]->subject : $subject, // if parameter subject is not null, use it! $bodyMsg, null, diff --git a/application/models/system/Recipient_model.php b/application/models/system/Recipient_model.php index 790f7487e..b8ffecab3 100644 --- a/application/models/system/Recipient_model.php +++ b/application/models/system/Recipient_model.php @@ -12,7 +12,7 @@ class Recipient_model extends DB_Model $this->pk = array('person_id', 'message_id'); $this->hasSequence = false; } - + /** * Get data for a received message */ @@ -27,7 +27,7 @@ class Recipient_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_msg_recipient', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $query = 'SELECT mr.message_id, mr.person_id, mm.subject, @@ -43,13 +43,13 @@ class Recipient_model extends DB_Model SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = \'email\' ) ks ON (ks.person_id = mr.person_id) WHERE mr.message_id = ? AND mr.person_id = ?'; - + $parametersArray = array($message_id, $person_id); - + // Get data of the messages to sent return $this->execQuery($query, $parametersArray); } - + /** * Get a received message identified by token */ @@ -62,7 +62,7 @@ class Recipient_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_msg_status', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $sql = 'SELECT r.message_id, m.person_id as sender_id, r.person_id as receiver_id, @@ -80,10 +80,10 @@ class Recipient_model extends DB_Model ) s ON (r.message_id = s.message_id AND r.person_id = s.person_id) WHERE r.token = ? LIMIT 1'; - + return $this->execQuery($sql, array(MSG_STATUS_DELETED, $token)); } - + /** * Get all received messages for a person identified by person_id */ @@ -98,7 +98,7 @@ class Recipient_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_msg_status', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $sql = 'SELECT DISTINCT ON (r.message_id) r.message_id, m.person_id, m.subject, @@ -126,9 +126,9 @@ class Recipient_model extends DB_Model ) s ON (m.message_id = s.message_id AND r.person_id = s.person_id) WHERE r.person_id = ? ORDER BY r.message_id DESC, s.status DESC'; - + $parametersArray = array($person_id); - + if ($all == 'true') { $sql = sprintf($sql, ''); @@ -138,10 +138,10 @@ class Recipient_model extends DB_Model array_push($parametersArray, $person_id, $person_id); $sql = sprintf($sql, 'WHERE person_id = ? AND message_id NOT IN (SELECT message_id FROM public.tbl_msg_status WHERE status >= 3 AND person_id = ?)'); } - + return $this->execQuery($sql, $parametersArray); } - + /** * Get all received messages for a person identified by uid */ @@ -188,18 +188,18 @@ class Recipient_model extends DB_Model SELECT * FROM public.tbl_msg_status ORDER BY insertamum DESC LIMIT 1 ) s ON (r.message_id = s.message_id AND r.person_id = s.person_id) WHERE b.uid = ?'; - + if (! $all) $sql .= ' AND (status < 3 OR status IS NULL)'; - + return $this->execQuery($sql, array($uid)); } - + /** * getMessages - * + * * Gets all the messages to be sent - * + * * @param kontaktType specifies the type of the kontakt to get * @param sent specifies the status of the messages to get (NULL never sent, otherwise the shipping date) * @param limit specifies the number of messages to get @@ -214,10 +214,11 @@ class Recipient_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_kontakt', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $query = 'SELECT mm.message_id, ks.kontakt as sender, kr.kontakt as receiver, + mu.mitarbeiter_uid as employeeContact, mr.person_id as receiver_id, mr.token, mm.subject, @@ -229,10 +230,16 @@ class Recipient_model extends DB_Model ) ks ON (ks.person_id = mm.person_id) LEFT JOIN ( SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = ? - ) kr ON (kr.person_id = mr.person_id)'; - + ) kr ON (kr.person_id = mr.person_id) + LEFT JOIN ( + SELECT b.person_id, + m.mitarbeiter_uid + FROM public.tbl_benutzer b INNER JOIN public.tbl_mitarbeiter m ON(b.uid = m.mitarbeiter_uid) + WHERE b.aktiv = TRUE + ) mu ON (mu.person_id = mr.person_id)'; + $parametersArray = array($kontaktType, $kontaktType); - + if (is_null($sent) || $sent == '') { $query .= ' WHERE mr.sent IS NULL'; @@ -242,24 +249,24 @@ class Recipient_model extends DB_Model array_push($parametersArray, $sent); $query .= ' WHERE mr.sent = ?'; } - + if (!is_null($message_id)) { array_push($parametersArray, $message_id); $query .= ' AND mm.message_id = ?'; } - + $query .= ' ORDER BY mr.insertamum ASC'; - + if (!is_null($limit)) { $query .= ' LIMIT ?'; array_push($parametersArray, $limit); } - + return $this->execQuery($query, $parametersArray); } - + /** * Get all unread messages for a person identified by person_id */ @@ -270,7 +277,7 @@ class Recipient_model extends DB_Model return $ent; if (isError($ent = $this->isEntitled('public.tbl_msg_status', PermissionLib::SELECT_RIGHT, FHC_NORIGHT, FHC_MODEL_ERROR))) return $ent; - + $sql = 'SELECT COUNT(r.message_id) AS unreadMessages FROM public.tbl_msg_recipient r JOIN public.tbl_msg_status s ON (r.message_id = s.message_id AND r.person_id = s.person_id) @@ -283,9 +290,9 @@ class Recipient_model extends DB_Model WHERE r.person_id = ? AND s.status > ? )'; - + $parametersArray = array($person_id, MSG_STATUS_UNREAD, $person_id, MSG_STATUS_UNREAD); - + return $this->execQuery($sql, $parametersArray); } }