View source
<?php
namespace Drupal\courier_system\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Render\Element;
use Drupal\courier\Entity\CourierContext;
use Drupal\courier\Entity\GlobalTemplateCollection;
use Drupal\courier\Service\CourierManagerInterface;
use Drupal\courier\TemplateCollectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Settings extends ConfigFormBase {
protected $courierManager;
public function __construct(ConfigFactoryInterface $config_factory, CourierManagerInterface $courier_manager) {
parent::__construct($config_factory);
$this->courierManager = $courier_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('courier.manager'));
}
public function getFormId() {
return 'courier_system_settings';
}
protected function getEditableConfigNames() {
return [
'courier_system.settings',
];
}
protected function getSystemMails() {
$options['user'] = [
'user_cancel_confirm' => [
'title' => $this
->t('Account cancellation confirmation'),
'description' => $this
->t('Sent to users when they attempt to cancel their accounts.'),
],
'user_password_reset' => [
'title' => $this
->t('Notify user when password reset'),
'description' => $this
->t('Sent to users who request a new password.'),
],
'user_status_activated' => [
'title' => $this
->t('Notify user when account is activated'),
'description' => $this
->t('Sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required)'),
],
'user_status_blocked' => [
'title' => $this
->t('Account blocked'),
'description' => $this
->t('Sent to users when their accounts are blocked.'),
],
'user_status_canceled' => [
'title' => $this
->t('Account canceled'),
'description' => $this
->t('Sent to users when they attempt to cancel their accounts.'),
],
'user_register_admin_created' => [
'title' => $this
->t('Welcome (new user created by administrator)'),
'description' => $this
->t('Sent to new member accounts created by an administrator.'),
],
'user_register_no_approval_required' => [
'title' => $this
->t('Welcome (no approval required)'),
'description' => $this
->t('Sent to new members upon registering, when no administrator approval is required.'),
],
'user_register_pending_approval' => [
'title' => $this
->t('Welcome (awaiting approval)'),
'description' => $this
->t('Sent to new members upon registering, when administrative approval is required.'),
],
];
return $options;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('courier_system.settings');
$override = $config
->get('override');
$form['actions'] = [
'#type' => 'details',
'#attributes' => [
'class' => [
'container-inline',
],
],
'#open' => TRUE,
];
$form['actions']['operation'] = [
'#title' => $this
->t('With selection'),
'#type' => 'select',
'#options' => [
'copy_email' => $this
->t('Copy Drupal email to Courier'),
'enable' => $this
->t('Override Drupal email (enable selected)'),
'disable' => $this
->t('Restore Drupal email (disable selected)'),
'delete' => $this
->t('Delete'),
],
'#empty_option' => $this
->t('- Select -'),
'#button_type' => 'primary',
];
$form['actions']['apply'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply'),
'#button_type' => 'primary',
];
$form['list'] = [
'#type' => 'courier_template_collection_list',
'#title' => $this
->t('Replace Drupal mails'),
'#checkboxes' => TRUE,
'#items' => [],
];
$header = [
$this
->t('Module'),
$this
->t('Description'),
];
$form['add_missing'] = [
'#type' => 'details',
'#title' => $this
->t('Add missing messages'),
];
$form['add_missing']['table'] = [
'#type' => 'table',
'#header' => $header,
'#tree' => TRUE,
'#tableselect' => TRUE,
'#multiple' => TRUE,
'#empty' => $this
->t('No messages are missing.'),
];
$form['add_missing']['submit'] = [
'#type' => 'submit',
'#value' => t('Create messages'),
'#submit' => [
[
$this,
'submitCreateMessages',
],
],
];
foreach ($this
->getSystemMails() as $module => $mails) {
foreach ($mails as $mail_id => $definition) {
$gtc_id = 'courier_system.' . $mail_id;
if ($gtc = GlobalTemplateCollection::load($gtc_id)) {
$template_collection = $gtc
->getTemplateCollection();
$form['list']['#items'][$mail_id] = [
'#title' => $this
->t('@module: @title (@status)', [
'@title' => $definition['title'],
'@module' => \Drupal::moduleHandler()
->getName($module),
'@status' => !empty($override[$mail_id]) ? $this
->t('enabled - using Courier') : $this
->t('disabled - using Drupal'),
]),
'#description' => $definition['description'],
'#template_collection' => $template_collection,
];
}
else {
$row = [];
$row['module']['#markup'] = \Drupal::moduleHandler()
->getName($module);
$row['title']['#markup'] = $definition['title'];
$form['add_missing']['table'][$mail_id] = $row;
}
}
}
$form['add_missing']['#open'] = !count($form['list']['#items']);
if ($count = count(Element::children($form['add_missing']['table']))) {
$form['add_missing']['#title'] = $this
->t('Add missing messages (@count)', [
'@count' => $count,
]);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$message = $this
->t('No operations were executed.');
$config = $this
->config('courier_system.settings');
$global_template_collections = [];
foreach ($this
->getSystemMails() as $module => $mails) {
foreach ($mails as $mail_id => $definition) {
$gtc = GlobalTemplateCollection::load('courier_system.' . $mail_id);
if ($gtc) {
$global_template_collections[$mail_id] = $gtc;
}
}
}
$checkboxes = [];
foreach ($form_state
->getValue([
'list',
'checkboxes',
]) as $id => $checked) {
if ($checked) {
$checkboxes[] = $id;
}
}
$operation = $form_state
->getValue('operation');
$override = $config
->get('override');
foreach ($checkboxes as $mail_id) {
if (isset($global_template_collections[$mail_id])) {
$gtc = $global_template_collections[$mail_id];
$template_collection = $gtc
->getTemplateCollection();
if (in_array($operation, [
'enable',
'disable',
])) {
$enable = $operation == 'enable';
$override[$mail_id] = $enable;
$message = $enable ? $this
->t('Messages enabled.') : $this
->t('Messages disabled.');
}
elseif ($operation == 'delete') {
$message = $this
->t('Messages deleted');
$gtc
->delete();
$template_collection
->delete();
unset($override[$mail_id]);
}
elseif ($operation == 'copy_email') {
$this
->copyCoreToCourierEmail($template_collection, $mail_id);
$template_collection
->save();
$message = $this
->t('Messages copied from Drupal to Courier.');
}
}
}
$config
->set('override', $override)
->save();
$this
->messenger()
->addMessage($message);
}
public function submitCreateMessages(array &$form, FormStateInterface $form_state) {
$config = $this
->config('courier_system.settings');
$override = $config
->get('override');
foreach ($form_state
->getValue('table') as $mail_id => $create) {
if ($create) {
$values = [
'id' => 'courier_system.' . $mail_id,
];
$gtc = GlobalTemplateCollection::create($values);
$gtc
->save();
$template_collection = $gtc
->getTemplateCollection();
if (!($courier_context = CourierContext::load('courier_system_user'))) {
$courier_context = CourierContext::create([
'label' => t('Courier System: Account'),
'id' => 'courier_system_user',
'tokens' => [
'user',
],
]);
$courier_context
->save();
}
$template_collection
->setContext($courier_context);
$this
->copyCoreToCourierEmail($template_collection, $mail_id);
$template_collection
->save();
$override[$mail_id] = TRUE;
}
}
$config
->set('override', $override)
->save();
}
protected function copyCoreToCourierEmail(TemplateCollectionInterface &$template_collection, $mail_id) {
$key = substr($mail_id, strlen('user_'));
$user_mails = $this
->config('user.mail');
$mail = $user_mails
->get($key);
if ($courier_email = $template_collection
->getTemplate('courier_email')) {
foreach ($mail as &$value) {
$value = nl2br($value);
$value = str_replace('[user:name]', '[identity:label]', $value);
}
$courier_email
->setSubject($mail['subject'])
->setBody($mail['body'])
->save();
$template_collection
->setTemplate($courier_email);
}
}
}