You are here

public function ScheduleController::revisionOverview in Business Rules 8

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

Generates an overview table of older revisions of a Schedule .

Parameters

string $business_rules_schedule: A Schedule object id.

Return value

array An array as expected by drupal_render().

File

src/Controller/ScheduleController.php, line 233

Class

ScheduleController
Class ScheduleController.

Namespace

Drupal\business_rules\Controller

Code

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;
}