diff --git a/cronjobs/findblock.php b/cronjobs/findblock.php index 2f978eed..d52c1cc9 100755 --- a/cronjobs/findblock.php +++ b/cronjobs/findblock.php @@ -112,6 +112,16 @@ foreach ($aAllBlocks as $iIndex => $aBlock) { . $strStatus . "\n" ); + + // Notify users + $aAccounts = $notification->getNotificationByType('new_block'); + foreach ($aAccounts as $account_id) { + $aMailData = $aBlock; + $aMailData['subject'] = 'New Block'; + $aMailData['email'] = $user->getUserEmail($user->getUserName($account_id)); + $aMailData['shares'] = $iRoundShares; + $notification->sendNotification($account_id, 'new_block', $aMailData); + } } } ?> diff --git a/cronjobs/notifications.php b/cronjobs/notifications.php index eddef3de..cd8c020a 100755 --- a/cronjobs/notifications.php +++ b/cronjobs/notifications.php @@ -32,15 +32,8 @@ if (empty($aWorkers)) { $aData['username'] = $user->getUserName($aWorker['account_id']); $aData['subject'] = 'IDLE Worker : ' . $aWorker['username']; $aData['email'] = $user->getUserEmail($aData['username']); - if ( $notification->isNotified($aData) ) { - verbose("Worker already notified\n"); - continue; - } - if ($notification->addNotification($aWorker['account_id'], '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"); - } + if (!$notification->sendNotification($aWorker['account_id'], 'idle_worker', $aData)) + verbose($notification->getError() . "\n"); } } diff --git a/public/include/classes/mail.class.php b/public/include/classes/mail.class.php index 2800500c..e34423c5 100644 --- a/public/include/classes/mail.class.php +++ b/public/include/classes/mail.class.php @@ -16,6 +16,9 @@ class Mail { public function setSmarty($smarty) { $this->smarty = $smarty; } + public function setUser($user) { + $this->user = $user; + } public function setConfig($config) { $this->config = $config; } @@ -35,14 +38,14 @@ class Mail { return true; } - public function sendMail($email, $template, $aData) { + public function sendMail($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"; - if (mail($email, + if (mail($aData['email'], $this->smarty->fetch(BASEPATH . 'templates/mail/subject.tpl'), $this->smarty->fetch(BASEPATH . 'templates/mail/' . $template . '.tpl'), $headers)) { @@ -61,5 +64,4 @@ $mail->setDebug($debug); $mail->setMysql($mysqli); $mail->setSmarty($smarty); $mail->setConfig($config); - ?> diff --git a/public/include/classes/notification.class.php b/public/include/classes/notification.class.php index cff05abb..d324d4a7 100644 --- a/public/include/classes/notification.class.php +++ b/public/include/classes/notification.class.php @@ -44,7 +44,6 @@ 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; } @@ -73,7 +72,7 @@ class Notification extends Mail { if ($stmt && $stmt->bind_param('iss', $account_id, $type, $data) && $stmt->execute()) return true; $this->debug->append("Failed to add notification for $type with $data: " . $this->mysqli->error); - $this->setErrorMessage("Unable to add new notification"); + $this->setErrorMessage("Unable to add new notification " . $this->mysqli->error); return false; } @@ -109,6 +108,21 @@ class Notification extends Mail { return false; } + /** + * Get all accounts that wish to receive a specific notification + * @param strType string Notification type + * @return data array User Accounts + **/ + public function getNotificationAccountIdByType($strType) { + $this->debug->append("STA " . __METHOD__, 4); + $stmt = $this->mysqli->prepare("SELECT account_id FROM $this->tableSettings WHERE type = ? AND active = 1"); + if ($stmt && $stmt->bind_param('s', $strType) && $stmt->execute() && $result = $stmt->get_result()) { + return $result->fetch_all(MYSQLI_ASSOC); + } + // Catchall + return false; + } + /** * Update accounts notification settings * @param account_id int Account ID @@ -144,6 +158,28 @@ class Notification extends Mail { } return true; } + + /** + * Send a specific notification setup in notification_settings + * @param type string Notification type + * @return bool + **/ + public function sendNotification($account_id, $strType, $aMailData) { + // Check if we notified for this event already + if ( $this->isNotified($aMailData) ) { + $this->setErrorMessage('A notification for this event has been sent already'); + return false; + } + // Check if this user wants strType notifications + $stmt = $this->mysqli->prepare("SELECT account_id FROM $this->tableSettings WHERE type = ? AND active = 1 AND account_id = ?"); + if ($stmt && $stmt->bind_param('si', $strType, $account_id) && $stmt->execute() && $stmt->bind_result($id) && $stmt->fetch()) { + if ($stmt->close() && $this->sendMail('notifications/' . $strType, $aMailData) && $this->addNotification($account_id, $strType, $aMailData)) + return true; + } else { + $this->setErrorMessage('User disabled ' . $strType . ' notifications'); + } + return false; + } } $notification = new Notification(); @@ -151,3 +187,5 @@ $notification->setDebug($debug); $notification->setMysql($mysqli); $notification->setSmarty($smarty); $notification->setConfig($config); + +?> diff --git a/public/templates/mail/idle_worker.tpl b/public/templates/mail/notifications/idle_worker.tpl similarity index 100% rename from public/templates/mail/idle_worker.tpl rename to public/templates/mail/notifications/idle_worker.tpl diff --git a/public/templates/mail/notifications/new_block.tpl b/public/templates/mail/notifications/new_block.tpl new file mode 100644 index 00000000..9d17d06e --- /dev/null +++ b/public/templates/mail/notifications/new_block.tpl @@ -0,0 +1,7 @@ + + +

A new block has been discovered!

+
+
+ + diff --git a/public/templates/mmcFE/account/notifications/default.tpl b/public/templates/mmcFE/account/notifications/default.tpl index 1b737f79..94c0c2b0 100644 --- a/public/templates/mmcFE/account/notifications/default.tpl +++ b/public/templates/mmcFE/account/notifications/default.tpl @@ -8,6 +8,13 @@ Type Active + + IDLE Worker + + + + + New Blocks