You are here

public function ScheduleTask::execute in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/BusinessRulesAction/ScheduleTask.php \Drupal\business_rules\Plugin\BusinessRulesAction\ScheduleTask::execute()

Execute the action.

Parameters

\Drupal\business_rules\ActionInterface $action: The configured action.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.

Return value

array The render array to be showed on debug block.

Overrides BusinessRulesActionPlugin::execute

File

src/Plugin/BusinessRulesAction/ScheduleTask.php, line 396

Class

ScheduleTask
Class ScheduleTask.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public function execute(ActionInterface $action, BusinessRulesEvent $event) {
  $time_offset = $action
    ->getSettings('time_offset');
  $time_unit = $action
    ->getSettings('time_unit');
  $update_entity = $action
    ->getSettings('update_entity');

  /** @var \Drupal\Core\Entity\Entity $entity */
  $entity = $event
    ->getSubject();
  $field = $entity
    ->get($action
    ->getSettings('field'))->value;
  $scheduled_date = FALSE;
  if (is_int($field)) {

    // Add number of seconds to timestamp data type.
    switch ($time_unit) {
      case 'seconds':
        $scheduled_date = $field + $time_offset;
        break;
      case 'minutes':
        $scheduled_date = $field + $time_offset * 60;
        break;
      case 'hours':
        $scheduled_date = $field + $time_offset * 60 * 60;
        break;
      case 'days':
        $scheduled_date = $field + $time_offset * 60 * 60 * 24;
        break;
      case 'weeks':
        $scheduled_date = $field + $time_offset * 60 * 60 * 24 * 7;
        break;
      case 'months':
        $date = new \DateTime($field);
        $interval = new \DateInterval('');
        $interval->m = $time_offset;
        $scheduled_date = $date
          ->add($interval)
          ->getTimestamp();
        break;
    }
  }
  $schedule = Schedule::loadByNameAndTriggeredBy($action
    ->getSettings('identifier'), $action
    ->id());
  $schedule
    ->setName($action
    ->getSettings('identifier'));
  $schedule
    ->setDescription($action
    ->getSettings('description'));
  if ($scheduled_date) {
    $schedule
      ->setScheduled($scheduled_date);
  }
  $schedule
    ->setExecuted(0);
  $schedule
    ->setTriggeredBy($action);
  $schedule
    ->setUpdateEntity($update_entity);
  $schedule
    ->setEvent($event);
  $schedule
    ->save();
}