Changed logic to send notice email to a message recipient

This commit is contained in:
Paolo
2020-02-14 19:17:09 +01:00
parent a4bfdfb4a0
commit 11f3fad1f7
6 changed files with 261 additions and 156 deletions
+5
View File
@@ -12,7 +12,12 @@ $config['message_html_view_url'] = '/system/messages/ViewMessage/toHTML/';
// Change this to CIS Server (https://cis.example.com/index.ci.php) if you are sending Messages from Vilesci
$config['message_server'] = site_url();
// Organisation unit function that are allowed to read messages for the organisation unit
$config['ou_receivers'] = array('ass');
// Organisation units that will never receive notice emails
$config['ou_receivers_no_notice'] = array('infocenter');
// Organisation units that will not send the notice email to the internal account, but to the private one
$config['ou_receivers_private'] = array('eac', 'ewu', 'scs');
$config['message_redirect_url'] = array();
$config['message_redirect_url']['fallback'] = site_url('system/messages/ViewMessage/writeReply');
+189 -76
View File
@@ -13,6 +13,8 @@ class MessageLib
const CFG_MESSAGE_SERVER = 'message_server';
const CFG_MESSAGE_HTML_VIEW_URL = 'message_html_view_url';
const CFG_OU_RECEIVERS = 'ou_receivers';
const CFG_OU_RECEIVERS_NO_NOTICE = 'ou_receivers_no_notice';
const CFG_OU_RECEIVERS_PRIVATE = 'ou_receivers_private';
const CFG_REDIRECT_VIEW_MESSAGE_URL = 'redirect_view_message_url';
// Templates names
@@ -24,6 +26,8 @@ class MessageLib
const EMAIL_KONTAKT_TYPE = 'email'; // Email kontakt type
const SENT_INFO_NEWLINE = '\n'; // tbl_msg_recipient->sentInfo separator
const ALT_OE = 'infocenter'; // alternative organisation unit when no one is found for a presetudent
private $_ci;
/**
@@ -47,11 +51,6 @@ class MessageLib
$this->_ci->load->model('system/MsgStatus_model', 'MsgStatusModel');
$this->_ci->load->model('system/Recipient_model', 'RecipientModel');
$this->_ci->load->model('system/Attachment_model', 'AttachmentModel');
// Loads extra models
$this->_ci->load->model('person/Person_model', 'PersonModel');
$this->_ci->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel');
$this->_ci->load->model('organisation/Organisationseinheit_model', 'OrganisationseinheitModel');
}
//------------------------------------------------------------------------------------------------------------------
@@ -219,6 +218,8 @@ class MessageLib
*/
public function getOeKurzbz($sender_id)
{
$this->_ci->load->model('person/Benutzerfunktion_model', 'BenutzerfunktionModel');
// Retrieves organisation units for a user from database
$benutzer = $this->_ci->BenutzerfunktionModel->getByPersonId($sender_id);
if (isSuccess($benutzer)) // if everything is ok
@@ -388,6 +389,8 @@ class MessageLib
*/
private function _getReceiversByPersonId($receiver_id)
{
$this->_ci->load->model('person/Person_model', 'PersonModel');
// Reset an eventually already buit query
$this->_ci->PersonModel->resetQuery();
@@ -543,32 +546,137 @@ class MessageLib
*/
private function _sendOneNotice($message_id)
{
// Get the message and related data (sender, recipient, etc...)
$messageResult = $this->_ci->RecipientModel->getMessages(
self::EMAIL_KONTAKT_TYPE,
$message_id
);
if (isError($messageResult)) return $messageResult; // if an error occured then return it
// Retrieves the message information by message_id
$messageResult = $this->_ci->RecipientModel->getMessageById($message_id);
if (isError($messageResult)) return $messageRecipientResult; // if an error occured then return it
if (!hasData($messageResult)) return error('No data found with the given message id'); // if no data found then return an error
// If the message was sent to an organisation unit, then retrives the allowed people to receive the notice email for that organisation unit
$messages = getData($messageResult);
if (count($messages) == 1
&& $messages[0]->receiver_id == $this->_ci->config->item(self::CFG_SYSTEM_PERSON_ID)
&& !isEmptyString($messages[0]->oe_kurzbz))
// Contains the message info
$message = getData($messageResult)[0];
// If the recipient organisation unit is in the list of organisation units that do not receive notice emails
if (array_search($message->receiver_ou, $this->_ci->config->item(self::CFG_OU_RECEIVERS_NO_NOTICE)))
{
$messageResult = $this->_ci->RecipientModel->getMessagesToSentToOE(
$messages[0]->oe_kurzbz,
$this->_ci->config->item(MessageLib::CFG_OU_RECEIVERS),
self::EMAIL_KONTAKT_TYPE
);
return success('There is no need to send a notice email to this organisation unit');
}
if (isError($messageResult)) return $messageResult;
if (!hasData($messageResult)) return error('It is not possible to retrieve the recipient for this message: '.$message_id);
$message->receiverContact = null; // by default set the recipient contact as null
return $this->_sendNotice(getData($messageResult));
// If the message was sent to an organisation unit then retrives degree program email
if ($message->receiver_id == $this->_ci->config->item(self::CFG_SYSTEM_PERSON_ID) && !isEmptyString($message->receiver_ou))
{
$this->_ci->load->model('organisation/Studiengang_model', 'StudiengangModel');
$studiengangResult = $this->_ci->StudiengangModel->loadWhere(array('oe_kurzbz' => $message->receiver_ou));
if (isError($studiengangResult)) return $studiengangResult; // if an error occured then return it
// 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 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)
{
$this->_ci->load->model('person/Benutzer_model', 'BenutzerModel');
// And the receiver has an active account for the given organisation unit
$benutzerResult = $this->_ci->BenutzerModel->getActiveUserByPersonIdAndOrganisationUnit($message->receiver_id, $message->sender_ou);
if (isError($benutzerResult)) return $benutzerResult; // if an error occured then return it
// Use the uid + domain email
if (hasData($benutzerResult)) $message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN;
}
// Otherwise try with the private email
if ($message->receiverContact == null)
{
$this->_ci->load->model('person/Kontakt_model', 'KontaktModel');
$kontaktResult = $this->_ci->KontaktModel->getContactByPersonId($person_id, self::EMAIL_KONTAKT_TYPE);
if (isError($kontaktResult)) return $kontaktResult; // if an error occured then return it
// Use the private email
if (hasData($kontaktResult)) $message->receiverContact = getData($kontaktResult)[0]->kontakt;
}
}
else
{
$this->_ci->load->model('person/Benutzer_model', 'BenutzerModel');
// The receiver has an active account for the given organisation unit
$benutzerResult = $this->_ci->BenutzerModel->getActiveUserByPersonIdAndOrganisationUnit($message->receiver_id, $message->sender_ou);
if (isError($benutzerResult)) return $benutzerResult; // if an error occured then return it
// Use the uid + domain email
if (hasData($benutzerResult))
{
$this->_ci->load->model('ressource/Mitarbeiter_model', 'MitarbeiterModel');
$mitarbeiterResult = $this->_ci->MitarbeiterModel->loadWhere(array('mitarbeiter_uid' => getData($benutzerResult)[0]->uid));
if (isError($mitarbeiterResult)) return $mitarbeiterResult; // if an error occured then return it
// If employee
if (hasData($mitarbeiterResult))
{
$message->receiverContact = getData($benutzerResult)[0]->uid .'@'.DOMAIN;
}
else
{
$this->_ci->load->model('crm/Prestudent_model', 'PrestudentModel');
$prestudentResults = $this->_ci->PrestudentModel->getOrganisationunitsByPersonId($message->receiver_id);
if (isError($prestudentResults)) return $prestudentResults; // if an error occured then return it
$inArray = true;
if (hasData($prestudentResults))
{
$organisationUnits = getData($prestudentResults);
foreach ($organisationUnits as $organisationUnit)
{
// 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)
{
$inArray = false;
break;
}
}
if (!$inArray)
{
$this->_ci->load->model('person/Kontakt_model', 'KontaktModel');
$kontaktResult = $this->_ci->KontaktModel->getContactByPersonId($message->receiver_id, self::EMAIL_KONTAKT_TYPE);
if (isError($kontaktResult)) return $kontaktResult; // if an error occured then return it
// Use the private email
if (hasData($kontaktResult)) $message->receiverContact = getData($kontaktResult)[0]->kontakt;
}
else
{
}
}
}
}
else // use the private email
{
$this->_ci->load->model('person/Kontakt_model', 'KontaktModel');
$kontaktResult = $this->_ci->KontaktModel->getContactByPersonId($message->receiver_id, self::EMAIL_KONTAKT_TYPE);
if (isError($kontaktResult)) return $kontaktResult; // if an error occured then return it
// Use the private email
if (hasData($kontaktResult)) $message->receiverContact = getData($kontaktResult)[0]->kontakt;
}
}
var_dump_to_error_log($message);
return $this->_sendNotice(array($message));
}
/**
@@ -589,71 +697,71 @@ class MessageLib
foreach ($messagesResult as $messageData)
{
// Checks if this person has a valid email address where to send the notice email
if (isEmptyString($messageData->receiver) && isEmptyString($messageData->employeecontact))
// NOTE: this is a NON blocking error!
if (isEmptyString($messageData->receiverContact))
{
// Set in database why this email is NOT going to be send
$sse = $this->_setSentError(
$messageData->message_id,
$messageData->receiver_id,
'This person does not have an email account',
'This person or organization unit does not have an email account',
$messageData->sentinfo
);
// If database error occurred then return it
if (isError($sse)) return $sse;
continue; // Skip the rest, continue with the next one
}
// Create a link to the controller to view the message using a token
$viewMessageLink = $prefixLink.$messageData->token;
// Generates notice email body in HTML and plain text version.
// If an error occured during the generation then the error itself is returned
$noticeHTMLBody = $this->_getNoticeBody(
$dbEmailNoticeTemplateHTML, $fsEmailNoticeTemplateHTML, $viewMessageLink, $messageData->subject, $messageData->body
);
if (isError($noticeHTMLBody)) return $noticeHTMLBody;
$noticeTXTBody = $this->_getNoticeBody(
$dbEmailNoticeTemplateTXT, $fsEmailNoticeTemplateTXT, $viewMessageLink, $messageData->subject, $messageData->body
);
if (isError($noticeTXTBody)) return $noticeTXTBody;
// If an employeecontact contact is present then use it, otherwise use the personal contacts
$receiverContact = $messageData->receiver;
if (!isEmptyString($messageData->employeecontact)) $receiverContact = $messageData->employeecontact.'@'.DOMAIN;
// Sending email
$sent = $this->_ci->maillib->send(
null,
$receiverContact,
$messageData->subject,
getData($noticeHTMLBody),
null,
null,
null,
getData($noticeTXTBody)
);
// If errors occurred while sending the email
if (!$sent)
else
{
// Set in database why this email is NOT going to be send
$sse = $this->_setSentError(
$messageData->message_id,
$messageData->receiver_id,
'An error occurred while sending the email',
$messageData->sentinfo
// Create a link to the controller to view the message using a token
$viewMessageLink = $prefixLink.$messageData->token;
// Generates notice email body in HTML format
$noticeHTMLBody = $this->_getNoticeBody(
$dbEmailNoticeTemplateHTML, $fsEmailNoticeTemplateHTML, $viewMessageLink, $messageData->subject, $messageData->body
);
// If an error occured during the generation then the error itself is returned
if (isError($noticeHTMLBody)) return $noticeHTMLBody;
// Generates notice email body in plain text format
$noticeTXTBody = $this->_getNoticeBody(
$dbEmailNoticeTemplateTXT, $fsEmailNoticeTemplateTXT, $viewMessageLink, $messageData->subject, $messageData->body
);
// If an error occured during the generation then the error itself is returned
if (isError($noticeTXTBody)) return $noticeTXTBody;
// Sending email
$sent = $this->_ci->maillib->send(
null,
$messageData->receiverContact,
$messageData->subject,
getData($noticeHTMLBody),
null,
null,
null,
getData($noticeTXTBody)
);
// If database error occurred then return it, otherwise return a logic error
return isError($sse) ? $sse : error('An error occurred while sending the email');
}
else // success!
{
// Set in database that the notice email was succesfully sent
$sss = $this->_setSentSuccess($messageData->message_id, $messageData->receiver_id);
if (isError($sss)) return $sss; // If database error occurred then return it
// If errors occurred while sending the email
if (!$sent)
{
// Set in database why this email is NOT going to be send
$sse = $this->_setSentError(
$messageData->message_id,
$messageData->receiver_id,
'An error occurred while sending the notice email',
$messageData->sentinfo
);
// If database error occurred then return it, otherwise return a logic error
return isError($sse) ? $sse : error('An error occurred while sending the notice email');
}
else // success!
{
// Set in database that the notice email was succesfully sent
$sss = $this->_setSentSuccess($messageData->message_id, $messageData->receiver_id);
if (isError($sss)) return $sss; // If database error occurred then return it
}
}
}
@@ -710,6 +818,9 @@ class MessageLib
{
// Send message notice via email!
$sendNotice = $this->_sendOneNotice($message_id);
var_dump_to_error_log($sendNotice);
// If an error occurred then return it
if (isError($sendNotice)) return $sendNotice;
}
@@ -723,6 +834,8 @@ class MessageLib
*/
private function _ouExists($ou)
{
$this->_ci->load->model('organisation/Organisationseinheit_model', 'OrganisationseinheitModel');
// Reset an eventually already buit query
$this->_ci->OrganisationseinheitModel->resetQuery();
// Get only this columns
+1 -2
View File
@@ -21,7 +21,6 @@ class Messages_model extends CI_Model
</blockquote>';
const NO_AUTH_UID = 'online'; // hard coded uid if no authentication is performed
const ALT_OE = 'infocenter'; // alternative organisation unit when no one is found for a presetudent
// Recipients types
const TYPE_PERSONS = 'persons';
@@ -91,7 +90,7 @@ class Messages_model extends CI_Model
{
$ouOptions .= sprintf(
"\n".'<option value="%s">%s</option>',
is_numeric($ou->prestudent_id) ? $ou->oe_kurzbz : self::ALT_OE,
is_numeric($ou->prestudent_id) ? $ou->oe_kurzbz : MessageLib::ALT_OE,
$ou->bezeichnung . (is_numeric($ou->prestudent_id) ? '' : ' *')
);
}
@@ -18,4 +18,19 @@ class Benutzer_model extends DB_Model
return $this->loadWhere(array('person_id' => $person_id, 'aktiv' => true));
}
/**
*
*/
public function getActiveUserByPersonIdAndOrganisationUnit($person_id, $oe_kurzbz)
{
$sql = 'SELECT b.uid
FROM public.tbl_benutzer b
JOIN public.tbl_prestudent ps USING (person_id)
JOIN public.tbl_studiengang sg USING (studiengang_kz)
WHERE ps.person_id = ?
AND sg.oe_kurzbz = ?
AND b.aktiv = TRUE';
return $this->execQuery($sql, array($person_id, $oe_kurzbz));
}
}
+28 -13
View File
@@ -8,17 +8,17 @@ class Kontakt_model extends DB_Model
public function __construct()
{
parent::__construct();
$this->dbTable = "public.tbl_kontakt";
$this->pk = "kontakt_id";
$this->dbTable = 'public.tbl_kontakt';
$this->pk = 'kontakt_id';
}
public function getWholeKontakt($kontakt_id, $person_id = null, $kontakttyp = null)
{
$result = null;
$this->addJoin("public.tbl_standort", "standort_id", "LEFT");
$this->addJoin("public.tbl_firma", "firma_id", "LEFT");
$this->addJoin('public.tbl_standort', 'standort_id', 'LEFT');
$this->addJoin('public.tbl_firma', 'firma_id', 'LEFT');
if (isset($kontakt_id))
{
$result = $this->load($kontakt_id);
@@ -26,22 +26,37 @@ class Kontakt_model extends DB_Model
else
{
$parametersArray = array();
if (!is_null($person_id))
{
$parametersArray["person_id"] = $person_id;
$parametersArray['person_id'] = $person_id;
}
if (!is_null($kontakttyp))
{
$parametersArray["kontakttyp"] = $kontakttyp;
$parametersArray['kontakttyp'] = $kontakttyp;
}
if (count($parametersArray) > 0)
{
$result = $this->loadWhere($parametersArray);
}
}
return $result;
}
}
/**
*
*/
public function getContactByPersonId($person_id, $kontakttyp)
{
$sql = 'SELECT kontakt
FROM public.tbl_kontakt
WHERE zustellung = TRUE
AND person_id = ?
AND kontakttyp = ?
ORDER BY updateamum, insertamum';
return $this->execQuery($sql, array($person_id, $kontakttyp));
}
}
+23 -65
View File
@@ -378,71 +378,6 @@ class Recipient_model extends DB_Model
return $this->execQuery($sql, array($person_id, $functions, $person_id));
}
/**
* Get all the messages to sent to an organisation unit
*/
public function getMessagesToSentToOE($oe_kurzbz, $functions, $kontaktType)
{
// Messages sent to a person that belongs to the recipient organisation unit
$sql = 'SELECT mm.message_id,
mm.subject,
mm.body,
mrou.person_id as receiver_id,
mrou.sentinfo,
mrou.token,
mrou.oe_kurzbz,
ks.kontakt as sender,
kr.kontakt as receiver,
mu.mitarbeiter_uid as employeeContact,
mb.mitarbeiter_uid as senderemployeeContact
FROM public.tbl_benutzer b
JOIN (
SELECT uid, oe_kurzbz
FROM public.tbl_benutzerfunktion
WHERE (datum_von IS NULL OR datum_von <= NOW())
AND (datum_bis IS NULL OR datum_bis >= NOW())
AND funktion_kurzbz IN ?
) bf ON (bf.uid = b.uid)
JOIN public.tbl_msg_recipient mrou ON (mrou.oe_kurzbz = bf.oe_kurzbz)
JOIN public.tbl_msg_message mm ON (mm.message_id = mrou.message_id)
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)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE zustellung = true AND kontakttyp = ?
) ks ON (ks.person_id = mm.person_id)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE zustellung = true AND kontakttyp = ?
) kr ON (kr.person_id = mrou.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 = mrou.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
) mb ON (mb.person_id = mm.person_id)
WHERE bf.oe_kurzbz = ?
AND mrou.sent IS NULL
AND mrou.sentinfo IS NULL
GROUP BY mm.message_id,
mm.subject,
mm.body,
mrou.person_id,
mrou.sentinfo,
mrou.token,
mrou.oe_kurzbz,
ks.kontakt,
kr.kontakt,
mu.mitarbeiter_uid,
mb.mitarbeiter_uid';
return $this->execQuery($sql, array($functions, $kontaktType, $kontaktType, $oe_kurzbz));
}
/**
* Gets all the sent message by the given person
*/
@@ -483,4 +418,27 @@ class Recipient_model extends DB_Model
return $this->execQuery($sql, array($person_id));
}
/**
*
*/
public function getMessageById($message_id)
{
$sql = 'SELECT mm.message_id,
mm.person_id AS sender_id,
mm.subject,
mm.body,
mm.relationmessage_id,
mm.oe_kurzbz AS sender_ou,
mr.person_id AS receiver_id,
mr.token,
mr.sent,
mr.sentinfo,
mr.oe_kurzbz AS receiver_ou
FROM public.tbl_msg_message mm
JOIN public.tbl_msg_recipient mr ON (mr.message_id = mm.message_id)
WHERE mm.message_id = ?';
return $this->execQuery($sql, array($message_id));
}
}