Skip to content
Snippets Groups Projects
Commit 8b892668 authored by dpi's avatar dpi
Browse files

Added support for new Courier template collection list element.

Creating a message will now redirect back to message list.
parent 0d38643b
No related branches found
No related tags found
No related merge requests found
...@@ -103,7 +103,7 @@ class MessageActionForm extends FormBase { ...@@ -103,7 +103,7 @@ class MessageActionForm extends FormBase {
$form['actions'] = array('#type' => 'actions'); $form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array( $form['actions']['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Add message and edit templates'), '#value' => t('Create message'),
); );
$form['actions']['cancel'] = array( $form['actions']['cancel'] = array(
'#type' => 'link', '#type' => 'link',
...@@ -167,7 +167,10 @@ class MessageActionForm extends FormBase { ...@@ -167,7 +167,10 @@ class MessageActionForm extends FormBase {
$rule_component->save(); $rule_component->save();
} }
$form_state->setRedirectUrl($action->urlInfo('edit-form')); $entity_type = $event->getEntityTypeId();
$form_state->setRedirectUrl(Url::fromRoute('rng.event.' . $entity_type . '.messages', [
$entity_type => $event->id(),
]));
} }
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\rng\Form; namespace Drupal\rng\Form;
use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\rng\EventManagerInterface; use Drupal\rng\EventManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -15,6 +16,7 @@ use Drupal\Core\Form\FormBase; ...@@ -15,6 +16,7 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\rng\Plugin\Condition\CurrentTime; use Drupal\rng\Plugin\Condition\CurrentTime;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\RuleInterface;
/** /**
* Creates message list form. * Creates message list form.
...@@ -99,113 +101,183 @@ class MessageListForm extends FormBase { ...@@ -99,113 +101,183 @@ class MessageListForm extends FormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $event = NULL) { public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $event = NULL) {
$header = [ $form['#rng_event'] = $route_match->getParameter($event);
$this->t('Trigger'),
$this->t('Date'), // @todo: move trigger definitions to a discovery service.
[ $rng_triggers = [
'data' => $this->t('Enabled'), 'entity:registration:new' => $this->t('Registration creation'),
'class' => ['checkbox'], 'entity:registration:update' => $this->t('Registration updated'),
'rng:custom:date' => $this->t('Send on a date'),
];
// Actions.
$form['actions'] = [
'#type' => 'details',
'#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'),
], ],
$this->t('Operations'), '#empty_option' => $this->t(' - Select - '),
'#button_type' => 'primary',
]; ];
$form['action_list'] = [ $form['actions']['enable'] = [
'#type' => 'table', '#type' => 'submit',
'#header' => $header, '#value' => $this->t('Save'),
'#title' => $this->t('Messages'), '#button_type' => 'primary',
'#empty' => $this->t('No messages found for this event.'),
]; ];
$rng_triggers = [ // List items.
'entity:registration:new' => $this->t('When registrations are created.'), $form['list'] = [
'entity:registration:update' => $this->t('When registrations are updated.'), '#type' => 'courier_template_collection_list',
'rng:custom:date' => $this->t('Current date is after a date.'), '#checkboxes' => TRUE,
'#items' => [],
]; ];
$destination = $this->redirectDestination->getAsArray();
$form['#rng_event'] = $route_match->getParameter($event);
foreach ($this->getCommunicationRules($form['#rng_event']) as $rid => $rule) { foreach ($this->getCommunicationRules($form['#rng_event']) as $rid => $rule) {
foreach ($rule->getActions() as $action) { $trigger_id = $rule->getTriggerID();
$row = []; if ($template_collection = $this->getTemplateCollectionForRule($rule)) {
$links = []; // Add description for date conditions.
// @todo: move trigger definitions to a discovery service. $description = NULL;
$trigger_id = $rule->getTriggerID(); if ($component = $this->getDateCondition($rule)) {
$row['trigger']['#markup'] = isset($rng_triggers[$trigger_id]) ? $rng_triggers[$trigger_id] : $trigger_id;
$row['date']['#markup'] = $this->t('N/A');
foreach ($rule->getConditions() as $component) {
$condition = $component->createInstance(); $condition = $component->createInstance();
if ($condition instanceof CurrentTime) { $description = $condition->getDateFormatted();
$row['date']['#markup'] = $condition->getDateFormatted();
if ($component->access('edit')) {
$links['edit-date'] = [
'title' => $this->t('Edit date'),
'url' => $component->urlInfo('edit-form'),
'query' => $destination,
];
}
}
} }
$row['status'] = [ $form['list']['#items'][$rule->id()] = [
'#type' => 'checkbox', '#title' => $this->t('@label (@status)', [
'#title' => $this->t('Active'), '@label' => isset($rng_triggers[$trigger_id]) ? $rng_triggers[$trigger_id] : $trigger_id,
'#title_display' => 'invisible', '@status' => $rule->isActive() ? $this->t('active') : $this->t('disabled'),
'#default_value' => (bool) $rule->isActive(), ]),
'#wrapper_attributes' => [ '#description' => $description,
'class' => [ '#template_collection' => $template_collection,
'checkbox', '#operations' => $this->getOperations($rule),
],
],
]; ];
}
}
if ($action->access('edit')) { return $form;
$links['edit-templates'] = [ }
'title' => $this->t('Edit templates'),
'url' => $action->urlInfo('edit-form'),
'query' => $destination,
];
}
if ($rule->access('delete')) {
$links['delete'] = [
'title' => $this->t('Delete'),
'url' => $rule->urlInfo('delete-form'),
'query' => $destination,
];
}
$row['operations']['data'] = [ /**
'#type' => 'operations', * {@inheritdoc}
'#links' => $links, */
]; public function submitForm(array &$form, FormStateInterface $form_state) {
$message = NULL;
$operation = $form_state->getValue(['operation']);
// Checkbox is checked, keyed by rule ID.
$checkbox = $form_state->getValue(['list', 'checkboxes']);
$form['action_list'][$rid] = $row; // A list of checked rules.
$rules = [];
foreach ($this->getCommunicationRules($form['#rng_event']) as $rule) {
// Checkbox is checked.
if ($checkbox[$rule->id()]) {
$rules[] = $rule;
} }
} }
$form['actions'] = ['#type' => 'actions']; /** @var RuleInterface $rule */
$form['actions']['submit'] = [ foreach ($rules as $rule) {
'#type' => 'submit', if (in_array($operation, ['enable', 'disable'])) {
'#value' => t('Save'), $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');
}
}
return $form; drupal_set_message($message ? $message : $this->t('No action performed.'));
} }
/** /**
* {@inheritdoc} * Gets the template collection from an action on the rule.
*
* @param \Drupal\rng\RuleInterface $rule
* The rule.
*
* @return \Drupal\courier\TemplateCollectionInterface|NULL
* A template collection entity, or NULL if no template collection is
* associated.
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { protected function getTemplateCollectionForRule(RuleInterface $rule) {
$form_rules = $form_state->getValue('action_list'); foreach ($rule->getActions() as $action) {
foreach ($this->getCommunicationRules($form['#rng_event']) as $rid => $rule) { $conf = $action->getConfiguration();
$enabled = !empty($form_rules[$rid]['status']); $id = $conf['template_collection'];
if ($rule->isActive() != $enabled) { if ($id && $template_collection = TemplateCollection::load($id)) {
$rule return $template_collection;
->setIsActive($enabled)
->save();
} }
} }
return NULL;
}
/**
* Gets the condition containing a date instance.
*
* @param \Drupal\rng\RuleInterface $rule
* The rule.
*
* @return \Drupal\rng\RuleComponentInterface|NULL
* A rule component entity, or NULL if no date condition is associated.
*/
protected function getDateCondition(RuleInterface $rule) {
foreach ($rule->getConditions() as $component) {
$condition = $component->createInstance();
if ($condition instanceof CurrentTime) {
return $component;
}
}
return NULL;
}
/**
* Gets operations for a rule.
*
* @param \Drupal\rng\RuleInterface $rule
* The rule.
*
* @return array
* An array of links suitable for an 'operations' element.
*/
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,
];
}
drupal_set_message($this->t('Messages saved.')); return $links;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment