- message_helper.php fixed

- Changes in the logic of sending email
This commit is contained in:
paolo
2016-07-26 18:19:13 +02:00
parent 6eec1ba0b7
commit d3bc72539a
6 changed files with 153 additions and 80 deletions
+1
View File
@@ -3,6 +3,7 @@
// Define constants
define("EMAIL_CONFIG_INDEX", "mail");
define("EMAIL_KONTAKT_TYPE", "email");
define("SENT_INFO_NEWLINE", "\n");
// Define configuration parameters
$config["email_number_to_sent"] = 1000; // Number of emails to sent each time sendAll is called
+1 -1
View File
@@ -40,4 +40,4 @@ define('MSG_STATUS_UPDATE', 9);
define('MSG_PARTICIPANT_ADDED', 10);
define('MSG_ERR_PARTICIPANT_EXISTS', 11);
define('MSG_ERR_PARTICIPANT_NONSYSTEM', 12);
define('MSG_PARTICIPANT_REMOVED', 13);
define('MSG_PARTICIPANT_REMOVED', 13);
+4 -4
View File
@@ -5,12 +5,12 @@
*
* @return array
*/
function success($retval, $message = EXIT_SUCCESS)
function success($retval, $message = null)
{
$return = new stdClass();
$return->error = EXIT_SUCCESS;
$return->fhcCode = $message;
$return->msg = lang('fhc_' . $message);
if (!is_null($message)) $return->msg = lang('fhc_' . $message);
$return->retval = $retval;
return $return;
}
@@ -20,12 +20,12 @@ function success($retval, $message = EXIT_SUCCESS)
*
* @return array
*/
function error($retval = '', $message = EXIT_ERROR)
function error($retval = '', $message = null)
{
$return = new stdClass();
$return->error = EXIT_ERROR;
$return->fhcCode = $message;
$return->msg = lang('fhc_' . $message);
if (!is_null($message)) $return->msg = lang('fhc_' . $message);
$return->retval = $retval;
return $return;
}
+81 -20
View File
@@ -246,6 +246,9 @@ class MessageLib
$result = $this->ci->MessageModel->insert($data);
if (is_object($result) && $result->error == EXIT_SUCCESS)
{
/**
* @TODO: sender_id must be a receiver_id
*/
$msg_id = $result->retval;
$statusData = array(
'message_id' => $msg_id,
@@ -457,10 +460,53 @@ class MessageLib
$this->ci->email->subject($subject);
$this->ci->email->message($message);
// Avoid printing ugly error messages
// Avoid printing on standard output ugly error messages
return @$this->ci->email->send();
}
/**
* Update the table tbl_message_recipient
*/
private function _updateMessageRecipient($message_id, $receiver_id, $parameters)
{
$updated = false;
// Changes the status of the message from unread to read
$resultUpdate = $this->ci->RecipientModel->update(array($receiver_id, $message_id), $parameters);
// Checks if errors were occurred
if (is_object($resultUpdate) && $resultUpdate->error == EXIT_SUCCESS && is_array($resultUpdate->retval))
{
$updated = true;
}
return $updated;
}
/**
* Changes the status of the message from unsent to sent
*/
private function setMessageSent($message_id, $receiver_id)
{
$parameters = array("sent" => "NOW()", "sentinfo" => null);
return $this->_updateMessageRecipient($message_id, $receiver_id, $parameters);
}
/**
* Sets the sentInfo with the error
*/
private function setMessageError($message_id, $receiver_id, $sentInfo, $prevSentInfo = null)
{
if (!is_null($prevSentInfo) && $prevSentInfo != "")
{
$sentInfo = $prevSentInfo . SENT_INFO_NEWLINE . $sentInfo;
}
$parameters = array("sent" => null, "sentinfo" => $sentInfo);
return $this->_updateMessageRecipient($message_id, $receiver_id, $parameters);
}
/**
* Gets all the messages from DB and sends them via email
*/
@@ -468,11 +514,11 @@ class MessageLib
{
$sent = true; // optimistic expectation
// Gets a number (email_number_to_sent) of unread messages from DB
// Gets a number (email_number_to_sent) of unsent messages from DB
// having EMAIL_KONTAKT_TYPE as relative contact type
$result = $this->ci->MsgStatusModel->getMessages(
$result = $this->ci->RecipientModel->getMessages(
EMAIL_KONTAKT_TYPE,
MSG_STATUS_UNREAD,
null,
$this->getEmailCfgItem("email_number_to_sent")
);
// Checks if errors were occurred
@@ -509,27 +555,27 @@ class MessageLib
$result->retval[$i]->subject,
$body
);
// If errors were occurred while sending the email
if (!$sent)
{
$this->ci->loglib->logError("Error while sending emails");
$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
);
if (!$sme)
{
$this->ci->loglib->logError("Error while updating DB");
}
}
else
{
// Changes the status of the message from unread to read
$resultUpdate = $this->ci->MsgStatusModel->update(
array($result->retval[$i]->message_id, $result->retval[$i]->person_id),
array("status" => MSG_STATUS_READ)
);
// Checks if errors were occurred
if (is_object($resultUpdate) && $resultUpdate->error == EXIT_SUCCESS && is_array($resultUpdate->retval))
{
$this->ci->loglib->logError("Error while updating DB");
$sent = false;
}
// If the email has been sent
// Setting the message as sent in DB
$sent = $this->setMessageSent($result->retval[$i]->message_id, $result->retval[$i]->receiver_id);
// If the email has been sent and the DB updated
if ($sent)
{
// If it has been sent a specified number of emails, then it has to wait
@@ -547,12 +593,27 @@ class MessageLib
sleep($seconds); // Wait!!!
}
}
else
{
$this->ci->loglib->logError("Error while updating DB");
}
}
}
else
{
$this->ci->loglib->logError("This person does not have an email account");
$sent = false;
// Writing errors in tbl_message_status
$sme = $this->setMessageError(
$result->retval[$i]->message_id,
$result->retval[$i]->receiver_id,
"This person does not have an email account",
$result->retval[$i]->sentinfo
);
if (!$sme)
{
$this->ci->loglib->logError("Error while updating DB");
}
$sent = true; // Non blocking error
}
}
}
+2 -55
View File
@@ -10,61 +10,8 @@ class MsgStatus_model extends DB_Model
public function __construct()
{
parent::__construct();
$this->dbTable = 'public.tbl_msg_status';
$this->pk = array('message_id', 'person_id');
$this->dbTable = "public.tbl_msg_status";
$this->pk = array("message_id", "person_id");
$this->hasSequence = false;
}
/**
* getMessages
*
* Gets all the messages to be sent
*
* @param kontaktType specifies the type of the kontakt to get
* @param messageStatus specifies the status of the messages to get
* @param limit specifies the number of messages to get
*/
public function getMessages($kontaktType, $messageStatus, $limit = null)
{
// Check wrights
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_recipient'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_recipient'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_message'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_message'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_msg_status'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_msg_status'), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz('public.tbl_kontakt'), 's'))
return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->getBerechtigungKurzbz('public.tbl_kontakt'), FHC_MODEL_ERROR);
$query = "SELECT ms.message_id,
ks.kontakt as sender,
kr.kontakt as receiver,
ms.person_id,
mm.subject,
mm.body
FROM public.tbl_msg_status ms INNER JOIN public.tbl_msg_recipient mr USING (message_id)
INNER JOIN public.tbl_msg_message mm USING (message_id)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = ?
) 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)
WHERE ms.status = ?";
$parametersArray = array($kontaktType, $kontaktType, $messageStatus);
if (!is_null($limit))
{
$query .= " LIMIT ?";
array_push($parametersArray, $limit);
}
// Get data of the messages to sent
$result = $this->db->query($query, $parametersArray);
if (is_object($result))
return $this->_success($result->result());
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
}
@@ -12,4 +12,68 @@ class Recipient_model extends DB_Model
$this->pk = array('person_id', 'message_id');
$this->hasSequence = false;
}
/**
* 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
*/
public function getMessages($kontaktType, $sent, $limit = null)
{
// Check wrights
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz("public.tbl_msg_recipient"), "s"))
return $this->_error(lang("fhc_".FHC_NORIGHT)." -> ".$this->getBerechtigungKurzbz("public.tbl_msg_recipient"), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz("public.tbl_msg_message"), "s"))
return $this->_error(lang("fhc_".FHC_NORIGHT)." -> ".$this->getBerechtigungKurzbz("public.tbl_msg_message"), FHC_MODEL_ERROR);
if (! $this->fhc_db_acl->isBerechtigt($this->getBerechtigungKurzbz("public.tbl_kontakt"), "s"))
return $this->_error(lang("fhc_".FHC_NORIGHT)." -> ".$this->getBerechtigungKurzbz("public.tbl_kontakt"), FHC_MODEL_ERROR);
$query = "SELECT mm.message_id,
ks.kontakt as sender,
kr.kontakt as receiver,
mr.person_id as receiver_id,
mm.subject,
mm.body,
mr.sentinfo
FROM public.tbl_msg_recipient mr INNER JOIN public.tbl_msg_message mm USING (message_id)
LEFT JOIN (
SELECT person_id, kontakt FROM public.tbl_kontakt WHERE kontakttyp = ?
) 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)";
$parametersArray = array($kontaktType, $kontaktType);
if (is_null($sent) || $sent == "")
{
$query .= " WHERE mr.sent IS NULL";
}
else
{
array_push($parametersArray, $sent);
$query .= " WHERE mr.sent = ?";
}
$query .= " ORDER BY mr.insertamum ASC";
if (!is_null($limit))
{
$query .= " LIMIT ?";
array_push($parametersArray, $limit);
}
// Get data of the messages to sent
$result = $this->db->query($query, $parametersArray);
if (is_object($result))
return $this->_success($result->result());
else
return $this->_error($this->db->error(), FHC_DB_ERROR);
}
}