[ADDED] Admin Pool Newsletter

* Added throttle option to swiftmailer for SMTP limiting
* Added newsletter page for admin panel
* Added newsletter mail template (simple one for now)
* Added new global configuration option
* Added default notification setting system in notification class
* Added global pool newsletter enable/disable switch in admin settings

Fixes #2087 once merged.
This commit is contained in:
Sebastian Grewe 2014-04-04 10:05:12 +02:00
parent 51e97eceb7
commit 739f035087
10 changed files with 128 additions and 2 deletions

View File

@ -52,7 +52,7 @@ class Mail extends Base {
* subject : Mail Subject
* email : Destination address
**/
public function sendMail($template, $aData) {
public function sendMail($template, $aData, $throttle=false) {
// Prepare SMTP transport and mailer
$transport_type = $this->config['swiftmailer']['type'];
if ($transport_type == 'sendmail') {
@ -65,6 +65,14 @@ class Mail extends Base {
}
}
$mailer = Swift_Mailer::newInstance($transport);
// Throttle mails to x per minute, used for newsletter for example
if ($this->config['swiftmailer']['type'] == 'smtp' && $throttle) {
$mailer->registerPlugin(new Swift_Plugins_ThrottlerPlugin(
$this->config['switfmailer']['smtp']['throttle'], Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE
));
}
// Prepare the smarty templates used
$this->smarty->clearCache(BASEPATH . 'templates/mail/' . $template . '.tpl');
$this->smarty->clearCache(BASEPATH . 'templates/mail/subject.tpl');

View File

@ -72,13 +72,23 @@ class Notification extends Mail {
* @return array Notification settings
**/
public function getNotificationSettings($account_id) {
// Some defaults, we cover them here so we can avoid adding default settings on user creation
$aDefaults = array( 'newsletter' => 1 );
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("SELECT * FROM $this->tableSettings WHERE account_id = ?");
if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result()) {
if ($result->num_rows > 0) {
$aFound = array();
while ($row = $result->fetch_assoc()) {
if (array_key_exists($row['type'], $aDefaults)) $aFound[] = $row['type'];
$aData[$row['type']] = $row['active'];
}
// Check found types against our defaults, set if required
foreach ($aDefaults as $type => $value) {
if (!in_array($type, $aFound)) {
$aData[$type] = $value;
}
}
return $aData;
}
}

View File

@ -468,6 +468,13 @@ $aSettings['notifications'][] = array(
'name' => 'notifications_disable_idle_worker', 'value' => $setting->getValue('notifications_disable_idle_worker'),
'tooltip' => 'Enable/Disable IDLE worker notifications globally. Will remove the user option too.'
);
$aSettings['notifications'][] = array(
'display' => 'Disable Pool Newsletter', 'type' => 'select',
'options' => array( 0 => 'No', 1 => 'Yes'),
'default' => 0,
'name' => 'notifications_disable_pool_newsletter', 'value' => $setting->getValue('notifications_disable_pool_newsletter'),
'tooltip' => 'Enable/Disable pool newsletter globally. Will remove the user option too.'
);
$aSettings['pools'][] = array(
'display' => 'Enable Pool Navigation', 'type' => 'select',
'options' => array( 0 => 'No', 1 => 'Yes' ),

View File

@ -70,6 +70,7 @@ $config['switfmailer']['smtp']['port'] = '587';
$config['switfmailer']['smtp']['encryption'] = 'tls';
$config['switfmailer']['smtp']['username'] = '';
$config['switfmailer']['smtp']['password'] = '';
$config['switfmailer']['smtp']['throttle'] = 100;
/**
* Getting Started Config

View File

@ -25,6 +25,7 @@ if ($user->isAuthenticated()) {
// Fetch global settings
$smarty->assign('DISABLE_BLOCKNOTIFICATIONS', $setting->getValue('notifications_disable_block'));
$smarty->assign('DISABLE_IDLEWORKERNOTIFICATIONS', $setting->getValue('notifications_disable_idle_worker'));
$smarty->assign('DISABLE_POOLNEWSLETTER', $setting->getValue('notifications_disable_pool_newsletter'));
// Fetch user notification settings
$aSettings = $notification->getNotificationSettings($_SESSION['USERDATA']['id']);

View File

@ -0,0 +1,41 @@
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
// Check user to ensure they are admin
if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) {
header("HTTP/1.1 404 Page not found");
die("404 Page not found");
}
// Include markdown library
use \Michelf\Markdown;
if ($setting->getValue('notifications_disable_pool_newsletter', 0) == 1) {
$_SESSION['POPUP'][] = array('CONTENT' => 'Pool newsletters are disabled.', 'TYPE' => 'alert alert-info');
$smarty->assign("CONTENT", "");
} else {
if (@$_REQUEST['do'] == 'send') {
if (!$config['csrf']['enabled'] || $config['csrf']['enabled'] && $csrftoken->valid) {
$iFailed = 0;
$iSuccess = 0;
foreach ($user->getAllAssoc() as $aData) {
$aUserNotificationSettings = $notification->getNotificationSettings($aData['id']);
if (!$aUserNotificationSettings['newsletter'] == 1) continue;
$aData['subject'] = $_REQUEST['data']['subject'];
$aData['CONTENT'] = $_REQUEST['data']['content'];
if (!$mail->sendMail('newsletter/body', $aData, true)) {
$iFailed++;
} else {
$iSuccess++;
}
}
$_SESSION['POPUP'][] = array('CONTENT' => 'Newsletter sent to ' . $iSuccess . ' users.', 'TYPE' => 'alert alert-success');
if ($iFailed > 0)
$_SESSION['POPUP'][] = array('CONTENT' => 'Failed to send e-mail to ' . $iFailed . ' users. ', 'TYPE' => 'alert alert-info');
} else {
$_SESSION['POPUP'][] = array('CONTENT' => $csrftoken->getErrorWithDescriptionHTML(), 'TYPE' => 'alert alert-warning');
}
}
$smarty->assign("CONTENT", "default.tpl");
}
?>

View File

@ -50,6 +50,17 @@
<input type="checkbox" class="switch" data-size="mini" name="data[success_login]" id="success_login" value="1"{nocache}{if $SETTINGS['success_login']|default:"0" == 1}checked{/if}{/nocache} />
</td>
</tr>
{if $DISABLE_POOLNEWSLETTER|default:"" != 1}
<tr>
<td>
<label>Pool Newsletter</label>
</td>
<td>
<input type="hidden" name="data[newsletter]" value="0" />
<input type="checkbox"class="switch" data-size="mini" name="data[newsletter]" id="new_block" value="1"{nocache}{if $SETTINGS['newsletter']|default:"1" == 1}checked{/if}{/nocache} />
</td>
</tr>
{/if}
</table>
</div>
<div class="panel-footer">
@ -97,4 +108,4 @@
</div>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,37 @@
<script src="{$PATH}/js/cleditor/jquery.cleditor.min.js"></script>
<link rel="stylesheet" href="{$PATH}/js/cleditor/jquery.cleditor.css">
<script type="text/javascript">
$(document).ready(function () { $(".cleditor").cleditor(); });
</script>
<div class="row">
<form class="col-lg-12" method="POST" action="{$smarty.server.SCRIPT_NAME}" role="form">
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-edit fa-fw"></i> Write Newsletter
<br>
<font size="1px">Newsletters support the Markdown syntax</font>
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<input type="hidden" name="page" value="{$smarty.request.page|escape}">
<input type="hidden" name="action" value="{$smarty.request.action|escape}">
<input type="hidden" name="do" value="send">
<input type="hidden" name="ctoken" value="{$CTOKEN|escape|default:""}" />
<div class="form-group">
<label>Subject</label>
<input class="form-control" size="30" type="text" name="data[subject]" value="{$smarty.request.data.subject|default:""}" required />
</div>
<div class="form-group">
<label>Content</label>
<textarea class="form-control cleditor" name="data[content]" rows="5" required>{$smarty.request.data.content|default:""}</textarea>
</div>
</div>
</div>
</div>
<div class="panel-footer">
<input type="submit" value="Send" class="btn btn-success btn-sm">
</div>
</div>
</form>
</div>

View File

@ -34,6 +34,7 @@
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=transactions"><i class="fa fa-tasks fa-fw"></i> Transactions</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=settings"><i class="fa fa-gears fa-fw"></i> Settings</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=news"><i class="fa fa-list-alt fa-fw"></i> News</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=newsletter"><i class="fa fa-list-alt fa-fw"></i> Newsletter</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=reports"><i class="fa fa-list-ol fa-fw"></i> Reports</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=registrations"><i class="fa fa-pencil-square-o fa-fw"></i> Registrations</a></li>
<li><a href="{$smarty.server.SCRIPT_NAME}?page=admin&action=invitations"><i class="fa fa-users fa-fw"></i> Invitations</a></li>

View File

@ -0,0 +1,9 @@
<html>
<body>
<p>Dear {nocache}{$DATA.username}{/nocache},</p>
<div>
{$DATA.CONTENT}
</div>
<p></p>
</body>
</html>