View source
<?php
use Drupal\activity_creator\ActivityInterface;
use Drupal\activity_send_email\Plugin\ActivityDestination\EmailActivityDestination;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
const FREQUENCY_DAILY = 'daily';
const FREQUENCY_IMMEDIATELY = 'immediately';
const FREQUENCY_NONE = 'none';
const FREQUENCY_WEEKLY = 'weekly';
function activity_send_email_cron() {
$emailfrequencymanager = \Drupal::service('plugin.manager.emailfrequency');
$plugins = $emailfrequencymanager
->getDefinitions();
foreach ($plugins as $frequency) {
$instance = $emailfrequencymanager
->createInstance($frequency['id']);
$interval = $instance
->getInterval();
if ($interval > 0) {
$last_run = \Drupal::state()
->get('digest.' . $frequency['id'] . '.last_run', 0);
if (time() - $last_run > $interval) {
$db = Database::getConnection();
$query = $db
->select('user_activity_digest', 'uad')
->fields('uad', [
'uid',
'activity',
])
->condition('uad.frequency', $frequency['id'])
->orderBy('uad.timestamp', 'DESC');
$activitities = $query
->execute()
->fetchAll();
if (!empty($activitities)) {
$queue_items = [];
foreach ($activitities as $activitity) {
$queue_items[$activitity->uid]['uid'] = $activitity->uid;
$queue_items[$activitity->uid]['frequency'] = $frequency['id'];
$queue_items[$activitity->uid]['activities'][] = $activitity->activity;
}
foreach ($queue_items as $queue_item) {
$queue = \Drupal::queue('activity_digest_worker');
$queue
->createItem($queue_item);
$db
->delete('user_activity_digest')
->condition('uid', $queue_item['uid'])
->condition('activity', $queue_item['activities'], 'IN')
->execute();
}
}
\Drupal::state()
->set('digest.' . $frequency['id'] . '.last_run', time());
}
}
}
}
function activity_send_email_theme() {
$items = [
'digestmail' => [
'template' => 'digestmail',
'variables' => [
'notification_count' => NULL,
'notifications' => NULL,
'notification_settings' => NULL,
],
],
'directmail' => [
'template' => 'directmail',
'variables' => [
'notification' => NULL,
'notification_settings' => NULL,
],
],
];
return $items;
}
function activity_send_email_form_user_form_alter(&$form, FormStateInterface $form_state) {
$account = $form_state
->getFormObject()
->getEntity();
if ($account
->isNew()) {
return;
}
$form['email_notifications'] = [
'#type' => 'fieldset',
'#title' => [
'text' => [
'#markup' => t('Email notifications'),
],
'icon' => [
'#markup' => '<svg class="icon icon-expand_more"><use xlink:href="#icon-expand_more" /></svg>',
'#allowed_tags' => [
'svg',
'use',
],
],
],
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => [
'class' => [
'form-horizontal',
'form-email-notification',
],
],
];
$form['email_notifications']['description'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('For each email notification below, you can choose to turn it off, receive it immediately or in a daily or weekly digest. Email notifications will only be sent when you are not active in the platform.'),
];
$items = _activity_send_email_default_template_items();
$email_message_templates = EmailActivityDestination::getSendEmailMessageTemplates();
$context = $email_message_templates;
\Drupal::moduleHandler()
->alter('activity_send_email_notifications', $items, $context);
$email_frequencies = sort_email_frequency_options();
$notification_options = [];
foreach ($email_frequencies as $option) {
$notification_options[$option['id']] = $option['name'];
}
$user_email_settings = EmailActivityDestination::getSendEmailUserSettings($account);
foreach ($items as $item_id => $item) {
if (empty($item['templates'])) {
continue;
}
$form['email_notifications'][$item_id] = [
'#type' => 'fieldset',
'#title' => [
'text' => [
'#markup' => $item['title'],
],
'icon' => [
'#markup' => '<svg class="icon icon-expand_more"><use xlink:href="#icon-expand_more" /></svg>',
'#allowed_tags' => [
'svg',
'use',
],
],
],
'#attributes' => [
'class' => [
'form-fieldset',
],
],
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#open' => TRUE,
];
$mail_configs = Drupal::config('social_swiftmail.settings');
$template_frequencies = $mail_configs
->get('template_frequencies') ?: [];
foreach ($item['templates'] as $template) {
$default_frequency = isset($template_frequencies[$template]) ? $template_frequencies[$template] : FREQUENCY_IMMEDIATELY;
$form['email_notifications'][$item_id][$template] = [
'#type' => 'select',
'#title' => $email_message_templates[$template],
'#options' => $notification_options,
'#default_value' => isset($user_email_settings[$template]) ? $user_email_settings[$template] : $default_frequency,
];
}
}
$form['actions']['submit']['#submit'][] = '_activity_send_email_form_user_form_submit';
$form['#attached']['library'][] = 'activity_send_email/admin';
}
function _activity_send_email_form_user_form_submit(array $form, FormStateInterface $form_state) {
$account = \Drupal::routeMatch()
->getParameter('user');
$values = $form_state
->getValue('email_notifications');
$values = array_merge($values['message_to_me'], $values['what_manage'], $values['what_follow'], $values['system_notifications']);
if (is_object($account) && !empty($values)) {
EmailActivityDestination::setSendEmailUserSettings($account, $values);
}
}
function sort_email_frequency_options() {
$emailfrequencymanager = \Drupal::service('plugin.manager.emailfrequency');
$plugins = $emailfrequencymanager
->getDefinitions();
$emailfrequencies = [];
foreach ($plugins as $frequency) {
$instance = $emailfrequencymanager
->createInstance($frequency['id']);
$emailfrequencies[] = [
'id' => $frequency['id'],
'name' => $instance
->getName(),
'weight' => $instance
->getWeight(),
];
}
uasort($emailfrequencies, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
return $emailfrequencies;
}
function activity_send_email_mail($key, &$message, $params) {
$options = [
'langcode' => $message['langcode'],
];
$variables = [
'%site_name' => \Drupal::config('system.site')
->get('name'),
];
switch ($key) {
case 'activity_send_email':
$message['subject'] = t('Notification from %site_name', $variables, $options);
$message['body'][] = $params['body'];
break;
}
}
function activity_send_email_activity_insert(ActivityInterface $activity) {
if (!in_array('email', $activity
->getDestinations())) {
return;
}
$activity_send_factory = \Drupal::service('plugin.manager.activity_send.processor');
$create_action = $activity_send_factory
->createInstance('email_activity_send');
$create_action
->create($activity);
}
function _activity_send_email_default_template_items() {
return [
'message_to_me' => [
'title' => t('Message to me'),
'templates' => [
'create_post_profile',
'create_mention_post',
'create_mention_comment',
'create_comment_reply_mention',
'create_comment_reply',
'create_comment_post_profile',
'create_like_node_or_post',
],
],
'what_manage' => [
'title' => t('What I manage'),
'templates' => [
'create_comment_author_node_post',
'join_to_group',
'request_event_enrollment',
],
],
'what_follow' => [
'title' => t('What I follow'),
'templates' => [
'create_comment_following_node',
'create_content_in_joined_group',
],
],
'system_notifications' => [
'title' => t('System notifications'),
'templates' => [],
],
];
}