Skip to content
Snippets Groups Projects
Commit 43e9ae58 authored by dpi's avatar dpi
Browse files

Implement rule status (NO UI)

Messages must enable status via code (UI coming...)
Add $is_active flag to EventMeta::getRules()
#40
parent 0c71b15b
No related branches found
No related tags found
No related merge requests found
......@@ -249,7 +249,9 @@ class EventController extends ControllerBase implements ContainerInjectionInterf
// list of communication related action plugin ids.
$communication_actions = array('rng_courier_message');
$rules = $this->eventManager->getMeta($event)->getRules();
$rules = $this->eventManager
->getMeta($event)
->getRules(NULL, FALSE, NULL);
foreach ($rules as $rule) {
/* @var \Drupal\rng\RuleInterface $rule */
foreach ($rule->getActions() as $action) {
......@@ -276,8 +278,7 @@ class EventController extends ControllerBase implements ContainerInjectionInterf
}
}
$configuration = $action->getConfiguration();
$row['status'] = !empty($configuration['active']) ? $this->t('Active') : $this->t('Draft');
$row['status'] = $rule->isActive() ? $this->t('Active') : $this->t('Inactive');
if ($action->access('edit')) {
$links['edit-templates'] = [
......
......@@ -65,6 +65,21 @@ class Rule extends ContentEntityBase implements RuleInterface {
return $this->get('trigger_id')->value;
}
/**
* {@inheritdoc}
*/
public function isActive() {
return $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setIsActive($is_active) {
$this->set('status', $is_active);
return $this;
}
/**
* {@inheritdoc}
*/
......@@ -199,7 +214,9 @@ class Rule extends ContentEntityBase implements RuleInterface {
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDescription(t('Whether this rule should run if the trigger is used. 0=disabled, 1=active.'));
->setDescription(t('Whether this rule should run if the trigger is used. 0=disabled, 1=active.'))
->setDefaultValue(FALSE)
->setRequired(TRUE);
return $fields;
}
......
......@@ -229,14 +229,20 @@ class EventMeta implements EventMetaInterface {
/**
* {@inheritdoc}
*/
function getRules($trigger = NULL, $defaults = FALSE) {
function getRules($trigger = NULL, $defaults = FALSE, $is_active = TRUE) {
$query = $this->buildRuleQuery();
if ($trigger) {
$query->condition('trigger_id', $trigger, '=');
}
$rules = $this->entityManager->getStorage('rng_rule')->loadMultiple($query->execute());
if (isset($is_active)) {
$query->condition('status', $is_active, '=');
}
$rules = $this->entityManager
->getStorage('rng_rule')
->loadMultiple($query->execute());
if ($defaults && !$rules) {
return $this->getDefaultRules($trigger);
}
......@@ -280,6 +286,7 @@ class EventMeta implements EventMetaInterface {
$rule = $this->entityManager->getStorage('rng_rule')->create(array(
'event' => array('entity' => $this->getEvent()),
'trigger_id' => 'rng_event.register',
'status' => TRUE,
));
foreach (['condition', 'action'] as $component_type) {
if (isset($definition[$component_type])) {
......
......@@ -185,11 +185,13 @@ interface EventMetaInterface {
* The trigger ID for the rule.
* @param boolean $defaults
* If there are no rules in the database, generate some unsaved rules.
* @param boolean|NULL $is_active
* The status of the rules, or set to NULL for any status.
*
* @return \Drupal\rng\RuleInterface[]
* An array of rng_rule entities.
*/
function getRules($trigger = NULL, $defaults = FALSE);
function getRules($trigger = NULL, $defaults = FALSE, $is_active = TRUE);
/**
* Gets site default access rules and associated conditions and actions.
......
......@@ -90,7 +90,7 @@ class RuleListBuilder extends EntityListBuilder {
*/
public function load() {
if (isset($this->event)) {
return $this->eventManager->getMeta($this->event)->getRules();
return $this->eventManager->getMeta($this->event)->getRules(NULL, FALSE, NULL);
}
return parent::load();
}
......@@ -114,6 +114,7 @@ class RuleListBuilder extends EntityListBuilder {
$header['trigger'] = t('Trigger ID');
$header['conditions'] = t('Conditions');
$header['actions'] = t('Actions');
$header['status'] = t('Status');
return $header + parent::buildHeader();
}
......@@ -153,6 +154,8 @@ class RuleListBuilder extends EntityListBuilder {
);
}
$row['status'] = $entity->isActive() ? $this->t('Active') : $this->t('Inactive');
return $row + parent::buildRow($entity);
}
......
......@@ -177,7 +177,7 @@ Each template requires content suitable to the channel.');
* registrations to send the message.
*/
public function execute($context = NULL) {
if ($this->isActive() && ($collection_original = $this->getTemplateCollection())) {
if ($collection_original = $this->getTemplateCollection()) {
foreach ($context['registrations'] as $registration) {
$options = [];
/** @var \Drupal\rng\RegistrationInterface $registration */
......
......@@ -30,6 +30,25 @@ interface RuleInterface extends ContentEntityInterface {
*/
public function getTriggerID();
/**
* Determine if the can be executed.
*
* @return bool
* Whether the rule can be executed.
*/
public function isActive();
/**
* Set if the rule can be executed.
*
* @param bool $is_active
* Whether the rule can be executed.
* @return \Drupal\rng\RuleInterface
* Return this object for chaining.
*/
public function setIsActive($is_active);
/**
* Get actions for the rule.
*
......
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