From 739f035087cd49be56657064581915bd609108e2 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 4 Apr 2014 10:05:12 +0200 Subject: [PATCH] [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. --- public/include/classes/mail.class.php | 10 ++++- public/include/classes/notification.class.php | 10 +++++ public/include/config/admin_settings.inc.php | 7 ++++ public/include/config/global.inc.dist.php | 1 + .../pages/account/notifications.inc.php | 1 + public/include/pages/admin/newsletter.inc.php | 41 +++++++++++++++++++ .../account/notifications/default.tpl | 13 +++++- .../bootstrap/admin/newsletter/default.tpl | 37 +++++++++++++++++ .../templates/bootstrap/global/navigation.tpl | 1 + public/templates/mail/newsletter/body.tpl | 9 ++++ 10 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 public/include/pages/admin/newsletter.inc.php create mode 100644 public/templates/bootstrap/admin/newsletter/default.tpl create mode 100644 public/templates/mail/newsletter/body.tpl diff --git a/public/include/classes/mail.class.php b/public/include/classes/mail.class.php index 51a228ba..a5c4fc6b 100644 --- a/public/include/classes/mail.class.php +++ b/public/include/classes/mail.class.php @@ -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'); diff --git a/public/include/classes/notification.class.php b/public/include/classes/notification.class.php index cc586ce1..a393a99e 100644 --- a/public/include/classes/notification.class.php +++ b/public/include/classes/notification.class.php @@ -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; } } diff --git a/public/include/config/admin_settings.inc.php b/public/include/config/admin_settings.inc.php index 48a5a0d6..e0adf7cb 100644 --- a/public/include/config/admin_settings.inc.php +++ b/public/include/config/admin_settings.inc.php @@ -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' ), diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 548db7b6..c5fd9d74 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -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 diff --git a/public/include/pages/account/notifications.inc.php b/public/include/pages/account/notifications.inc.php index ef26761c..f06c796a 100644 --- a/public/include/pages/account/notifications.inc.php +++ b/public/include/pages/account/notifications.inc.php @@ -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']); diff --git a/public/include/pages/admin/newsletter.inc.php b/public/include/pages/admin/newsletter.inc.php new file mode 100644 index 00000000..10a69c09 --- /dev/null +++ b/public/include/pages/admin/newsletter.inc.php @@ -0,0 +1,41 @@ +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"); +} +?> diff --git a/public/templates/bootstrap/account/notifications/default.tpl b/public/templates/bootstrap/account/notifications/default.tpl index ab2fedea..f1fa1a0d 100644 --- a/public/templates/bootstrap/account/notifications/default.tpl +++ b/public/templates/bootstrap/account/notifications/default.tpl @@ -50,6 +50,17 @@ + {if $DISABLE_POOLNEWSLETTER|default:"" != 1} + + + + + + + + + + {/if} - \ No newline at end of file + diff --git a/public/templates/bootstrap/admin/newsletter/default.tpl b/public/templates/bootstrap/admin/newsletter/default.tpl new file mode 100644 index 00000000..11721854 --- /dev/null +++ b/public/templates/bootstrap/admin/newsletter/default.tpl @@ -0,0 +1,37 @@ + + + +
+
+
+
+ Write Newsletter +
+ Newsletters support the Markdown syntax +
+
+
+
+ + + + +
+ + +
+
+ + +
+
+
+
+ +
+
+
diff --git a/public/templates/bootstrap/global/navigation.tpl b/public/templates/bootstrap/global/navigation.tpl index e360a61c..5a09ab9d 100644 --- a/public/templates/bootstrap/global/navigation.tpl +++ b/public/templates/bootstrap/global/navigation.tpl @@ -34,6 +34,7 @@
  • Transactions
  • Settings
  • News
  • +
  • Newsletter
  • Reports
  • Registrations
  • Invitations
  • diff --git a/public/templates/mail/newsletter/body.tpl b/public/templates/mail/newsletter/body.tpl new file mode 100644 index 00000000..f7860adc --- /dev/null +++ b/public/templates/mail/newsletter/body.tpl @@ -0,0 +1,9 @@ + + +

    Dear {nocache}{$DATA.username}{/nocache},

    +
    +{$DATA.CONTENT} +
    +

    + +