View source
<?php
namespace Drupal\rng\Form;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\rng\EventManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\rng\Plugin\Condition\CurrentTime;
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\Entity\RuleInterface;
class MessageListForm extends FormBase {
protected $redirectDestination;
protected $eventManager;
public function __construct(RedirectDestinationInterface $redirect_destination, EventManagerInterface $event_manager) {
$this->redirectDestination = $redirect_destination;
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('redirect.destination'), $container
->get('rng.event_manager'));
}
public function getFormId() {
return 'rng_message_list';
}
protected function getCommunicationRules(EntityInterface $event) {
$communication_actions = [
'rng_courier_message',
];
$rules = [];
$rules_all = $this->eventManager
->getMeta($event)
->getRules(NULL, FALSE, NULL);
foreach ($rules_all as $rid => $rule) {
foreach ($rule
->getActions() as $action) {
$action_id = $action
->getPluginId();
if (in_array($action_id, $communication_actions)) {
$rules[$rid] = $rule;
continue 2;
}
}
}
return $rules;
}
public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $rng_event = NULL) {
$form['#rng_event'] = $rng_event;
$rng_triggers = [
'entity:registration:new' => $this
->t('Registration creation'),
'entity:registration:update' => $this
->t('Registration updated'),
'rng:custom:date' => $this
->t('Send on a date'),
];
$form['actions'] = [
'#type' => 'details',
'#title' => $this
->t('Operations'),
'#attributes' => [
'class' => [
'container-inline',
],
],
'#open' => TRUE,
];
$form['actions']['operation'] = [
'#title' => $this
->t('With selection'),
'#type' => 'select',
'#options' => [
'enable' => $this
->t('Enable messages'),
'disable' => $this
->t('Disable messages'),
'delete' => $this
->t('Delete messages'),
],
'#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',
'#checkboxes' => TRUE,
'#items' => [],
];
foreach ($this
->getCommunicationRules($form['#rng_event']) as $rid => $rule) {
$trigger_id = $rule
->getTriggerID();
if ($template_collection = $this
->getTemplateCollectionForRule($rule)) {
$description = NULL;
if ($component = $this
->getDateCondition($rule)) {
$condition = $component
->createInstance();
$description = $condition
->getDateFormatted();
}
$form['list']['#items'][$rule
->id()] = [
'#title' => $this
->t('@label (@status)', [
'@label' => isset($rng_triggers[$trigger_id]) ? $rng_triggers[$trigger_id] : $trigger_id,
'@status' => $rule
->isActive() ? $this
->t('active') : $this
->t('disabled'),
]),
'#description' => $description,
'#template_collection' => $template_collection,
'#operations' => $this
->getOperations($rule),
];
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$message = NULL;
$operation = $form_state
->getValue([
'operation',
]);
$checkbox = $form_state
->getValue([
'list',
'checkboxes',
]);
$rules = [];
foreach ($this
->getCommunicationRules($form['#rng_event']) as $rule) {
if ($checkbox[$rule
->id()]) {
$rules[] = $rule;
}
}
foreach ($rules as $rule) {
if (in_array($operation, [
'enable',
'disable',
])) {
$operation_active = $operation == 'enable';
if ($rule
->isActive() != $operation_active) {
$rule
->setIsActive($operation_active)
->save();
}
$message = $operation == 'enable' ? $this
->t('Messages enabled.') : $this
->t('Messages disabled.');
}
elseif ($operation == 'delete') {
$rule
->delete();
$message = $this
->t('Messages deleted');
}
}
drupal_set_message($message ? $message : $this
->t('No action performed.'));
}
protected function getTemplateCollectionForRule(RuleInterface $rule) {
foreach ($rule
->getActions() as $action) {
$conf = $action
->getConfiguration();
$id = $conf['template_collection'];
if ($id && ($template_collection = TemplateCollection::load($id))) {
return $template_collection;
}
}
return NULL;
}
protected function getDateCondition(RuleInterface $rule) {
foreach ($rule
->getConditions() as $component) {
$condition = $component
->createInstance();
if ($condition instanceof CurrentTime) {
return $component;
}
}
return NULL;
}
protected function getOperations(RuleInterface $rule) {
$links = [];
$destination = $this->redirectDestination
->getAsArray();
if ($component = $this
->getDateCondition($rule)) {
if ($component
->access('edit')) {
$links['edit-date'] = [
'title' => $this
->t('Edit date'),
'url' => $component
->urlInfo('edit-form'),
'query' => $destination,
];
}
}
if ($rule
->access('delete')) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => $rule
->urlInfo('delete-form'),
'query' => $destination,
];
}
return $links;
}
}