You are here

class ScheduleController in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Controller/ScheduleController.php \Drupal\business_rules\Controller\ScheduleController

Class ScheduleController.

Returns responses for Schedule routes.

Hierarchy

Expanded class hierarchy of ScheduleController

File

src/Controller/ScheduleController.php, line 25

Namespace

Drupal\business_rules\Controller
View source
class ScheduleController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * A dateFormatter object.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  private $dateFormatter;

  /**
   * {@inheritdoc}
   */
  public function __construct(DateFormatterInterface $dateFormatter) {
    $this->dateFormatter = $dateFormatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $dateFormatter = $container
      ->get('date.formatter');
    return new static($dateFormatter);
  }

  /**
   * View schedule canonical.
   *
   * @return array
   *   The render array.
   */
  public function view($business_rules_schedule) {
    $schedule = Schedule::load($business_rules_schedule);
    $output['#title'] = $schedule
      ->label();
    $output['name'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Name'),
      '#markup' => $schedule
        ->getName(),
    ];
    $output['status'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Executed'),
      '#markup' => $schedule
        ->isExecuted() ? $this
        ->t('Yes') : $this
        ->t('No'),
    ];
    $output['created'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Created'),
      '#markup' => $this->dateFormatter
        ->format($schedule
        ->getCreatedTime(), 'medium'),
    ];
    $output['changed'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Changed'),
      '#markup' => $this->dateFormatter
        ->format($schedule
        ->getChangedTime(), 'medium'),
    ];
    $output['scheduled'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Scheduled'),
      '#markup' => $this->dateFormatter
        ->format($schedule
        ->getScheduled(), 'medium'),
    ];
    $output['triggered_by'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Triggered by'),
      '#markup' => $schedule
        ->getTriggeredBy()
        ->toLink(NULL, 'edit-form')
        ->toString(),
    ];
    return $output;
  }

  /**
   * Manual execution of a scheduled item.
   *
   * @param string $business_rules_schedule
   *   The business_rules_schedule id.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   The redirect response.
   */
  public function execute($business_rules_schedule) {
    $task = Schedule::load($business_rules_schedule);

    /** @var \Drupal\business_rules\Entity\Action $action */
    $action = $task
      ->getTriggeredBy();
    $items = $action
      ->getSettings('items');
    $container = \Drupal::getContainer();
    $util = new BusinessRulesUtil($container);
    $reacts_on_definition = \Drupal::getContainer()
      ->get('plugin.manager.business_rules.reacts_on')
      ->getDefinition('cron_runs');
    $task_event = $task
      ->getEvent();
    $loop_control = time();
    $dummy = new \stdClass();
    $variables = new VariablesSet();
    $dummy_event = new BusinessRulesEvent($dummy, [
      'entity_type_id' => '',
      'bundle' => NULL,
      'entity' => NULL,
      'entity_unchanged' => NULL,
      'variables' => $variables,
      'reacts_on' => $reacts_on_definition,
      'loop_control' => $loop_control,
    ]);
    $event = $task_event instanceof BusinessRulesEvent ? $task_event : $dummy_event;

    /** @var \Drupal\Core\Entity\Entity $entity */
    $entity = $task_event ? $task_event
      ->getSubject() instanceof Entity ? $task_event
      ->getSubject() : FALSE : FALSE;
    if ($entity) {
      $entity = \Drupal::entityTypeManager()
        ->getStorage($entity
        ->getEntityTypeId())
        ->load($entity
        ->id());
      $task_event
        ->setArgument('entity', $entity);
      $event = new BusinessRulesEvent($entity, $task_event
        ->getArguments());
    }
    try {
      foreach ($items as $item) {
        $action_item = Action::load($item['id']);
        $action_item
          ->execute($event);
      }
      $entity = $event
        ->getSubject() instanceof Entity ? $event
        ->getSubject() : FALSE;
      if ($entity && $task
        ->getUpdateEntity()) {
        $entity_exists = \Drupal::entityTypeManager()
          ->getStorage($entity
          ->getEntityTypeId())
          ->load($entity
          ->id());
        if ($entity_exists instanceof EntityInterface) {
          $entity
            ->save();
        }
      }
      $task
        ->setExecuted(1);
      $task
        ->save();
      $util->logger
        ->notice(t('Scheduled task id: @id, name: "@name", triggered by: "@by" has been executed at: @time', [
        '@id' => $task
          ->id(),
        '@name' => $task
          ->getName(),
        '@by' => $task
          ->getTriggeredBy()
          ->id(),
        '@time' => $container
          ->get('date.formatter')
          ->format(time(), 'medium'),
      ]));
    } catch (\Exception $e) {
      $util->logger
        ->error($e
        ->getMessage());
    }
    return new RedirectResponse('/admin/config/workflow/business_rules/schedule/collection');
  }

  /**
   * Displays a Schedule  revision.
   *
   * @param int $schedule_revision
   *   The Schedule  revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function revisionShow($schedule_revision) {
    $schedule = $this
      ->entityManager()
      ->getStorage('business_rules_schedule')
      ->loadRevision($schedule_revision);
    $view_builder = $this
      ->entityManager()
      ->getViewBuilder('business_rules_schedule');

    // Return $view_builder->view($schedule);
    return $this
      ->view($schedule_revision);
  }

  /**
   * Page title callback for a Schedule  revision.
   *
   * @param int $schedule_revision
   *   The Schedule  revision ID.
   *
   * @return string
   *   The page title.
   */
  public function revisionPageTitle($schedule_revision) {
    $schedule = $this
      ->entityManager()
      ->getStorage('schedule')
      ->loadRevision($schedule_revision);
    return $this
      ->t('Revision of %title from %date', [
      '%title' => $schedule
        ->label(),
      '%date' => format_date($schedule
        ->getRevisionCreationTime()),
    ]);
  }

  /**
   * Generates an overview table of older revisions of a Schedule .
   *
   * @param string $business_rules_schedule
   *   A Schedule object id.
   *
   * @return array
   *   An array as expected by drupal_render().
   */
  public function revisionOverview($business_rules_schedule) {
    $business_rules_schedule = Schedule::load($business_rules_schedule);
    $account = $this
      ->currentUser();
    $langcode = $business_rules_schedule
      ->language()
      ->getId();
    $langname = $business_rules_schedule
      ->language()
      ->getName();
    $languages = $business_rules_schedule
      ->getTranslationLanguages();
    $has_translations = count($languages) > 1;
    $schedule_storage = $this
      ->entityManager()
      ->getStorage('business_rules_schedule');
    $build['#title'] = $has_translations ? $this
      ->t('@langname revisions for %title', [
      '@langname' => $langname,
      '%title' => $business_rules_schedule
        ->label(),
    ]) : $this
      ->t('Revisions for %title', [
      '%title' => $business_rules_schedule
        ->label(),
    ]);
    $header = [
      $this
        ->t('Revision'),
      $this
        ->t('Operations'),
    ];
    $revert_permission = $account
      ->hasPermission("revert all schedule revisions") || $account
      ->hasPermission('administer schedule entities');
    $delete_permission = $account
      ->hasPermission("delete all schedule revisions") || $account
      ->hasPermission('administer schedule entities');
    $rows = [];
    $vids = $schedule_storage
      ->revisionIds($business_rules_schedule);
    $latest_revision = TRUE;
    foreach (array_reverse($vids) as $vid) {

      /** @var \Drupal\business_rules\ScheduleInterface $revision */
      $revision = $schedule_storage
        ->loadRevision($vid);

      // Only show revisions that are affected by the language that is being
      // displayed.
      if ($revision
        ->hasTranslation($langcode) && $revision
        ->getTranslation($langcode)
        ->isRevisionTranslationAffected()) {
        $username = [
          '#theme' => 'username',
          '#account' => $revision
            ->getRevisionUser(),
        ];

        // Use revision link to link to revisions that are not active.
        $time = $revision
          ->getRevisionCreationTime();
        if (is_numeric($time)) {
          $date = \Drupal::service('date.formatter')
            ->format($revision
            ->getRevisionCreationTime(), 'short');
          if ($vid != $business_rules_schedule
            ->getRevisionId()) {
            $link = $this
              ->l($date, new Url('entity.business_rules_schedule.revision', [
              'schedule' => $business_rules_schedule
                ->id(),
              'schedule_revision' => $vid,
            ]));
          }
          else {
            $link = $business_rules_schedule
              ->link($date);
          }
        }
        else {
          $link = '';
        }
        $row = [];
        $column = [
          'data' => [
            '#type' => 'inline_template',
            '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
            '#context' => [
              'date' => $link,
              'username' => \Drupal::service('renderer')
                ->renderPlain($username),
              'message' => [
                '#markup' => $revision
                  ->getRevisionLogMessage(),
                '#allowed_tags' => Xss::getHtmlTagList(),
              ],
            ],
          ],
        ];
        $row[] = $column;
        if ($latest_revision) {
          $row[] = [
            'data' => [
              '#prefix' => '<em>',
              '#markup' => $this
                ->t('Current revision'),
              '#suffix' => '</em>',
            ],
          ];
          foreach ($row as &$current) {
            $current['class'] = [
              'revision-current',
            ];
          }
          $latest_revision = FALSE;
        }
        else {
          $links = [];
          if ($revert_permission) {
            $links['revert'] = [
              'title' => $this
                ->t('Revert'),
              'url' => Url::fromRoute('entity.business_rules_schedule.revision_revert', [
                'schedule' => $business_rules_schedule
                  ->id(),
                'schedule_revision' => $vid,
              ]),
            ];
          }
          if ($delete_permission) {
            $links['delete'] = [
              'title' => $this
                ->t('Delete'),
              'url' => Url::fromRoute('entity.business_rules_schedule.revision_delete', [
                'schedule' => $business_rules_schedule
                  ->id(),
                'schedule_revision' => $vid,
              ]),
            ];
          }
          $row[] = [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ];
        }
        $rows[] = $row;
      }
    }
    $build['schedule_revisions_table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
ScheduleController::$dateFormatter private property A dateFormatter object.
ScheduleController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ScheduleController::execute public function Manual execution of a scheduled item.
ScheduleController::revisionOverview public function Generates an overview table of older revisions of a Schedule .
ScheduleController::revisionPageTitle public function Page title callback for a Schedule revision.
ScheduleController::revisionShow public function Displays a Schedule revision.
ScheduleController::view public function View schedule canonical.
ScheduleController::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.