From 432540335fd966ee000b385b5082fa3e71a59be1 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 7 Jun 2013 15:35:58 +0200 Subject: [PATCH] Working notification system * Added things to mail templates * Modified user password reset call for new mail template * Added BASEPATH to smarty code to ensure templates are compiled in the proper directory * Updated mail and notification class * Updated notification cron * Added notification cron to run-cron list --- cronjobs/notifications.php | 23 +++++++++++++------ cronjobs/run-crons.sh | 2 +- public/include/classes/mail.class.php | 11 +++++++-- public/include/classes/notification.class.php | 1 + public/include/classes/user.class.php | 1 + public/include/smarty.inc.php | 6 ++--- public/templates/mail/idle_worker.tpl | 2 +- public/templates/mail/subject.tpl | 2 +- 8 files changed, 33 insertions(+), 15 deletions(-) diff --git a/cronjobs/notifications.php b/cronjobs/notifications.php index e70d2b07..45bda82a 100755 --- a/cronjobs/notifications.php +++ b/cronjobs/notifications.php @@ -30,23 +30,32 @@ if (empty($aWorkers)) { foreach ($aWorkers as $aWorker) { $aData = $aWorker; $aData['username'] = $user->getUserName($aWorker['account_id']); + $aData['subject'] = 'IDLE Worker : ' . $aWorker['username']; $aData['email'] = $user->getUserEmail($aData['username']); - if (!$notification->isNotified($aData)) { - if (!$notification->addNotification('idle_worker', $aData) && $notification->sendMail('sebastian@grewe.ca', 'idle_worker', $aData)) - verbose("Unable to send notification: " . $notification->getError() . "\n"); - } else { - verbose("Already notified for this worker\n"); + if ( $notification->isNotified($aData) ) { + verbose("Worker already notified\n"); + continue; } + if ($notification->addNotification('idle_worker', $aData) && $notification->sendMail($aData['email'], 'idle_worker', $aData)) { + verbose ("Notified " . $aData['email'] . " for IDLE worker " . $aWorker['username'] . "\n"); + } else { + verbose("Unable to send notification: " . $notification->getError() . "\n"); + } } } + // We notified, lets check which recovered $aNotifications = $notification->getAllActive(); foreach ($aNotifications as $aNotification) { $aData = json_decode($aNotification['data'], true); $aWorker = $worker->getWorker($aData['id']); - if ($aWorker['active'] == 1) - if (!$notification->setInactive($aNotification['id'])) + if ($aWorker['active'] == 1) { + if ($notification->setInactive($aNotification['id'])) { + verbose("Marked notification " . $aNotification['id'] . " as inactive\n"); + } else { verbose("Failed to set notification inactive for " . $aWorker['username'] . "\n"); + } + } } ?> diff --git a/cronjobs/run-crons.sh b/cronjobs/run-crons.sh index 730a9063..5f9179dc 100755 --- a/cronjobs/run-crons.sh +++ b/cronjobs/run-crons.sh @@ -16,7 +16,7 @@ PIDFILE='/tmp/mmcfe-ng-cron.pid' CRONHOME='.' # List of cruns to execute -CRONS="findblock.php proportional_payout.php blockupdate.php auto_payout.php tickerupdate.php" +CRONS="findblock.php proportional_payout.php blockupdate.php auto_payout.php tickerupdate.php notifications.php" # Additional arguments to pass to cronjobs CRONARGS="-v" diff --git a/public/include/classes/mail.class.php b/public/include/classes/mail.class.php index a1cf2137..2800500c 100644 --- a/public/include/classes/mail.class.php +++ b/public/include/classes/mail.class.php @@ -19,7 +19,12 @@ class Mail { public function setConfig($config) { $this->config = $config; } - + public function setErrorMessage($msg) { + $this->sError = $msg; + } + public function getError() { + return $this->sError; + } function checkStmt($bState) { $this->debug->append("STA " . __METHOD__, 4); if ($bState ===! true) { @@ -30,8 +35,10 @@ class Mail { return true; } - public function sendMail($email, $template, $vars) { + public function sendMail($email, $template, $aData) { $this->smarty->assign('WEBSITENAME', $this->config['website']['name']); + $this->smarty->assign('SUBJECT', $aData['subject']); + $this->smarty->assign('DATA', $aData); $headers = 'From: Website Administration <' . $this->config['website']['email'] . ">\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; diff --git a/public/include/classes/notification.class.php b/public/include/classes/notification.class.php index 9a6dcf3b..983db437 100644 --- a/public/include/classes/notification.class.php +++ b/public/include/classes/notification.class.php @@ -41,6 +41,7 @@ class Notification extends Mail { return true; // Catchall // Does not seem to have a notification set + $this->setErrorMessage("Unable to run query: " . $this->mysqli->error); return false; } diff --git a/public/include/classes/user.class.php b/public/include/classes/user.class.php index 8db17a1e..b4627729 100644 --- a/public/include/classes/user.class.php +++ b/public/include/classes/user.class.php @@ -486,6 +486,7 @@ class User { } $smarty->assign('TOKEN', $token); $smarty->assign('USERNAME', $username); + $smarty->assign('SUBJECT', 'Password Reset Request'); $smarty->assign('WEBSITENAME', $this->config['website']['name']); $headers = 'From: Website Administration <' . $this->config['website']['email'] . ">\n"; $headers .= "MIME-Version: 1.0\n"; diff --git a/public/include/smarty.inc.php b/public/include/smarty.inc.php index b2e67f37..5668e1d3 100644 --- a/public/include/smarty.inc.php +++ b/public/include/smarty.inc.php @@ -16,10 +16,10 @@ $smarty = new Smarty; // Assign our local paths $debug->append('Define Smarty Paths', 3); -$smarty->template_dir = 'templates/' . THEME . '/'; -$smarty->compile_dir = 'templates/compile/'; +$smarty->template_dir = BASEPATH . 'templates/' . THEME . '/'; +$smarty->compile_dir = BASEPATH . 'templates/compile/'; // Optional smarty caching, check Smarty documentation for details $smarty->caching = $config['cache']; -$smarty->cache_dir = "templates/cache"; +$smarty->cache_dir = BASEPATH . "templates/cache"; ?> diff --git a/public/templates/mail/idle_worker.tpl b/public/templates/mail/idle_worker.tpl index a21aaac3..6d1c282c 100644 --- a/public/templates/mail/idle_worker.tpl +++ b/public/templates/mail/idle_worker.tpl @@ -1,6 +1,6 @@ -

One of your workers is currently IDLE.

+

One of your workers is currently IDLE: {$DATA.username}

Since monitoring is enabled for this worker, this notification was sent.

Please check your workers!


diff --git a/public/templates/mail/subject.tpl b/public/templates/mail/subject.tpl index 665e26f5..94fd6a28 100644 --- a/public/templates/mail/subject.tpl +++ b/public/templates/mail/subject.tpl @@ -1 +1 @@ -[ {$WEBSITENAME} ] Password Reset Request +[ {$WEBSITENAME} ] {$SUBJECT}