RuleScheduler.php in RNG - Events and Registrations 8.2
File
src/Plugin/Condition/RuleScheduler.php
View source
<?php
namespace Drupal\rng\Plugin\Condition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rng\Entity\RuleInterface;
use Drupal\rng\Entity\RuleSchedule;
use Drupal\rng\Entity\RuleComponent;
use Drupal\Core\Datetime\DrupalDateTime;
class RuleScheduler extends CurrentTime implements ContainerFactoryPluginInterface {
protected $schedulerStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->schedulerStorage = $entity_manager
->getStorage('rng_rule_scheduler');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.manager'));
}
public function defaultConfiguration() {
return [
'rng_rule_component' => NULL,
'rng_rule_scheduler' => NULL,
] + parent::defaultConfiguration();
}
public function getRuleComponentId() {
if (isset($this->configuration['rng_rule_component'])) {
return $this->configuration['rng_rule_component'];
}
return NULL;
}
public function getRuleScheduler() {
if (isset($this->configuration['rng_rule_scheduler'])) {
return RuleSchedule::load($this->configuration['rng_rule_scheduler']);
}
return NULL;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['date']['#description'] = t('Rule will trigger once on this date.');
$rule_scheduler = $this
->getRuleScheduler();
if ($rule_scheduler) {
if ($rule_scheduler
->getInQueue()) {
drupal_set_message($this
->t('This message is queued for execution.'));
$form['date']['#disabled'] = TRUE;
}
}
unset($form['negate']);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['negate'] = FALSE;
$this
->updateRuleSchedulerEntity();
}
public function updateRuleSchedulerEntity() {
$rule_scheduler = $this
->getRuleScheduler();
$rule_component_id = $this
->getRuleComponentId();
$rule_component = $rule_component_id ? RuleComponent::load($rule_component_id) : NULL;
$rule = $rule_component ? $rule_component
->getRule() : NULL;
$rule_active = $rule instanceof RuleInterface ? $rule
->isActive() : FALSE;
if ($rule_active) {
if (!$rule_scheduler) {
$rule_scheduler = RuleSchedule::create([
'component' => $this
->getRuleComponentId(),
]);
$rule_scheduler
->save();
$this->configuration['rng_rule_scheduler'] = $rule_scheduler
->id();
}
if ($rule_scheduler) {
$rule_scheduler
->setDate($this->configuration['date']);
$rule_scheduler
->save();
}
}
else {
if ($rule_scheduler && !$rule_scheduler
->getInQueue()) {
$rule_scheduler
->delete();
}
}
}
public function summary() {
$rule_scheduler = $this
->getRuleScheduler();
if ($rule_scheduler) {
return $this
->t('Current date is after @date', [
'@date' => DrupalDateTime::createFromTimestamp($rule_scheduler
->getDate()),
]);
}
else {
return $this
->t('Current date is after a date');
}
}
}
Classes
Name |
Description |
RuleScheduler |
Schedules rule execution based on the configured date. |