Adding new notification system for new blocks

* Modified findblocks cron
* Modified notifications cron for new structure
* Improved notification class
* Added new template for new_block type
* Moved idle_worker type template
* Added new_block type to notification settings
This commit is contained in:
Sebastian Grewe 2013-06-09 14:26:18 +02:00
parent 88ade9cfa3
commit 4ea8b6c695
7 changed files with 71 additions and 14 deletions

View File

@ -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);
}
}
}
?>

View File

@ -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");
}
}

View File

@ -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);
?>

View File

@ -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);
?>

View File

@ -0,0 +1,7 @@
<html>
<body>
<p>A new block has been discovered!</p>
<br/>
<br/>
</body>
</html>

View File

@ -8,6 +8,13 @@
<th class="left">Type</th>
<th class="center">Active</th>
</tr>
<tr>
<td class="left">IDLE Worker</td>
<td class="center">
<input type="hidden" name="data[idle_worker]" value="0" />
<input type="checkbox" name="data[idle_worker]" value="1"{if $SETTINGS['idle_worker']}checked{/if} />
</td>
</tr>
<tr>
<td class="left">New Blocks</td>
<td class="center">