Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Fix mailer digest computes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdujeu committed Mar 10, 2016
1 parent b898844 commit 7f33c2b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
99 changes: 58 additions & 41 deletions core/src/plugins/core.mailer/class.AjxpMailer.php
Expand Up @@ -122,6 +122,10 @@ public function mailConsumeQueue ($action, $httpVars, $fileVars) {
}
}

protected function stringify($int){
return ($int < 10 ? "0".$int : "".$int);
}

public function processNotification(AJXP_Notification &$notification)
{
$userExist = AuthService::userExists($notification->getTarget());
Expand All @@ -131,55 +135,62 @@ public function processNotification(AJXP_Notification &$notification)
$messages = ConfService::getMessages();
throw new AJXP_Exception($messages['core.mailer.2']);
}
$notification_email_get = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_GET", AJXP_REPO_SCOPE_ALL,"true");
$notification_email_frequency = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_FREQUENCY", AJXP_REPO_SCOPE_ALL,"M");
$notification_email_frequency_user = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_FREQUENCY_USER", AJXP_REPO_SCOPE_ALL,"5");
$notification_email = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL", AJXP_REPO_SCOPE_ALL,"");
$notification_email_get = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_GET", AJXP_REPO_SCOPE_ALL,"true");
if($notification_email_get !== "true"){
// Do nothing!
return;
}
$notification_email_frequency = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_FREQUENCY", AJXP_REPO_SCOPE_ALL,"M");
$notification_email_frequency_user = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_FREQUENCY_USER", AJXP_REPO_SCOPE_ALL,"5");
$notification_email = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL", AJXP_REPO_SCOPE_ALL,"");
$notification_email_send_html = $userObject->mergedRole->filterParameterValue("core.mailer","NOTIFICATIONS_EMAIL_SEND_HTML", AJXP_REPO_SCOPE_ALL,"true");
if ($notification_email_send_html === "true") {
$html = 1;
} else {
$html = 0;
}
$arrayRecipent = explode(',', $notification_email);
if ($notification_email_get === "true" && count($arrayRecipent) > 0 && !empty($notification_email_frequency_user)) {
$arrayRecipients = array();
$mainRecipient = $userObject->mergedRole->filterParameterValue("core.conf", "email", AJXP_REPO_SCOPE_ALL, "");
if(!empty($mainRecipient)) $arrayRecipients[] = $mainRecipient;
$additionalRecipients = array_map("trim", explode(',', $notification_email));
foreach($additionalRecipients as $addR){
if(!empty($addR)) $arrayRecipients[] = $addR;
}
if (count($arrayRecipients) && !empty($notification_email_frequency_user)) {
$date = new DateTime("now");
$hour = $date->format('H');
$minute = $date->format('i');
$year = $date->format("Y");
$day = $date->format("d");
$month = $date->format("m");
$hour = intval($date->format('H'));
$minute = intval($date->format('i'));
$frequency = $notification_email_frequency_user;
$allMinute = ($hour * 60) + $minute;
$nextFrequency = null;
switch ($notification_email_frequency) {
case "M":
//FOR EVERY X MIN
$allMinute = ($hour * 60) + $minute;
$nextInterval = $allMinute - ($allMinute % $frequency) + $frequency;
$nextInterval = $nextInterval - $frequency;
$nextInterval = $nextInterval / 60;
$nextIntervalDecimal = $nextInterval - (int)$nextInterval;
$nextIntervalMinute = $nextIntervalDecimal * 60;
$nextIntervalHour = (int)$nextInterval;
$nextIntervalDate = new DateTime($nextIntervalHour . ':' . $nextIntervalMinute);
$nextFrequency = $nextIntervalDate->modify('+' . $frequency . ' minutes')->format('Y-m-d H:i:s');
$lastInterval = $allMinute - ($allMinute % $frequency);
$newMinutes = $this->stringify($lastInterval % 60);
$newHour = $this->stringify(($lastInterval - $newMinutes) / 60);
$lastFrequency = DateTime::createFromFormat("Y-m-d H:i", "$year-$month-$day $newHour:$newMinutes");
$nextFrequency = $lastFrequency->modify("+ $frequency minutes")->getTimestamp();
break;
case "H":
//FOR EVERY X HOUR
$frequency = $frequency * 60;
$allMinute = ($hour * 60) + $minute;
$nextInterval = $allMinute - ($allMinute % $frequency) + $frequency;
$nextInterval = $nextInterval - $frequency;
$nextInterval = $nextInterval / 60;
$nextIntervalDecimal = $nextInterval - (int)$nextInterval;
$nextIntervalMinute = $nextIntervalDecimal * 60;
$nextIntervalHour = (int)$nextInterval;
$nextIntervalDate = new DateTime($nextIntervalHour . ':' . $nextIntervalMinute);
$nextFrequency = $nextIntervalDate->modify('+' . $frequency . ' minutes')->format('Y-m-d H:i:s');
$lastInterval = $allMinute - ($allMinute % $frequency);
$newHour = $this->stringify($lastInterval / 60);
$lastFrequency = DateTime::createFromFormat("Y-m-d H:i", "$year-$month-$day $newHour:00");
$nextFrequency = $lastFrequency->modify("+ $frequency minutes")->getTimestamp();
break;
case "D1":
$compareDate = new DateTime($date->format('d-m-Y') . ' ' . $frequency . ':00');
if ($date > $compareDate) {
//FREQUENCY ALREADY GONE, NEXT INTERVAL => NEXT DAY
$compareDate = $compareDate->modify('+1 day');
}
$nextFrequency = $compareDate->format('Y-m-d ' . $frequency . ':00');
$nextFrequency = new DateTime($compareDate->format('Y-m-d ' . $frequency . ':00'));
$nextFrequency = $nextFrequency->getTimestamp();
break;
case "D2":
//FOR EVERY DAY AT X and Y
Expand All @@ -194,27 +205,33 @@ public function processNotification(AJXP_Notification &$notification)
} else {
$nextFrequency = $date->modify('+1 day')->format('Y-m-d ' . $arrayFrequency[0] . ':00');
}
$nextFrequency = new DateTime($nextFrequency);
$nextFrequency = $nextFrequency->getTimestamp();
}
break;
case "W1":
//FOR EVERY WEEK AT THE DAY
$nextFrequency = $date->modify('next ' . $frequency)->format('Y-m-d 00:00:00');
$nextFrequency = $date->modify('next ' . $frequency)->getTimestamp();
break;
}
if (!dibi::isConnected()) {
dibi::connect($this->getDibiDriver());
}
foreach ($arrayRecipent as $recipent) {
try {
dibi::query("INSERT INTO [ajxp_mail_queue] ([recipent],[url],[date_event],[notification_object],[html]) VALUES (%s,%s,%s,%bin,%b) ",
trim($recipent),
$notification->getNode()->getUrl(),
$nextFrequency,
serialize($notification),
$html);
} catch (Exception $e) {
new AJXP_Exception($e->getMessage());
if(!empty($nextFrequency)){
if (!dibi::isConnected()) {
dibi::connect($this->getDibiDriver());
}
foreach ($arrayRecipients as $recipient) {
try {
dibi::query("INSERT INTO [ajxp_mail_queue] ([recipent],[url],[date_event],[notification_object],[html]) VALUES (%s,%s,%s,%bin,%b) ",
$recipient,
$notification->getNode()->getUrl(),
$nextFrequency,
serialize($notification),
$html);
} catch (Exception $e) {
$this->logError("[mailer]", $e->getMessage());
}
}
}else{
$this->logError("[mailer]", "Could not determine email frequency from $notification_email_frequency / $notification_email_frequency_user for send email to user ".$userObject->getId());
}
} else {
$mailer = AJXP_PluginsService::getInstance()->getActivePluginsForType("mailer", true);
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/core.mailer/manifest.xml
Expand Up @@ -11,7 +11,7 @@
<global_param name="BODY_LAYOUT" label="CONF_MESSAGE[Body Layout]" description="CONF_MESSAGE[Main Layout for the email body, use HTML for send nice emails, and use the AJXP_MAIL_BODY keyword for the real content.]" type="textarea" mandatory="false" default="AJXP_MAIL_BODY"/>
<global_param name="LAYOUT_FOLDER" label="CONF_MESSAGE[Layout Folder]" description="CONF_MESSAGE[Extract the main layout from a localized file. Will prevail on the previous parameter, make sure to have an AJXP_MAIL_BODY in the layout file.]" type="string" mandatory="false" default="plugins/core.mailer/layout"/>
<param name="NOTIFICATIONS_EMAIL_GET" type="select" choices="true|Yes,false|No" scope="user" description="Receive Notifcations by email" label="Active notifications by email" expose="true" mandatory="true" default="true" />
<param name="NOTIFICATIONS_EMAIL_FREQUENCY" scope="user" type="select" choices="M|Every X min,H|Every X hour,D1|Every day at ,D2|Twice a day (every day at and),W1|One a Week" label="Emails frequency" description="Emails frenquency" expose="true" mandatory="true" default="M" />
<param name="NOTIFICATIONS_EMAIL_FREQUENCY" scope="user" type="select" choices="M|Every X min,H|Every X hour,D1|Every day at ,D2|Twice a day (every day at and),W1|Once a Week" label="Emails frequency" description="Emails frenquency" expose="true" mandatory="true" default="M" />
<param name="NOTIFICATIONS_EMAIL_FREQUENCY_USER" scope="user" type="string" description="Choose your mail frequency (like 9:00 or 9:00,14:00 or Wednesday)" label="Detail your frequency here" expose="true" mandatory="true" default="5" />
<param name="NOTIFICATIONS_EMAIL" scope="user" description="You can add many email separated by commas" label="Send email to..." type="string" expose="true" editable="true"/>
<param name="NOTIFICATIONS_EMAIL_SEND_HTML" scope="user" type="select" choices="true|Yes,false|No" description="Send as HTML" label="Get the email in HTML" expose="true" mandatory="true" default="true" />
Expand Down

0 comments on commit 7f33c2b

Please sign in to comment.