You are here

public function SupportTicketController::revisionOverview in Support Ticketing System 8

Generates an overview table of older revisions of a support ticket.

Parameters

\Drupal\support_ticket\SupportTicketInterface $support_ticket: A support_ticket object.

Return value

array An array as expected by drupal_render().

File

modules/support_ticket/src/Controller/SupportTicketController.php, line 163
Contains \Drupal\support_ticket\Controller\SupportTicketController.

Class

SupportTicketController
Returns responses for Support Ticket routes.

Namespace

Drupal\support_ticket\Controller

Code

public function revisionOverview(SupportTicketInterface $support_ticket) {
  $account = $this
    ->currentUser();
  $support_ticket_storage = $this
    ->entityManager()
    ->getStorage('support_ticket');
  $type = $support_ticket
    ->getType();
  $build = array();
  $build['#title'] = $this
    ->t('Revisions for %title', array(
    '%title' => $support_ticket
      ->label(),
  ));
  $header = array(
    $this
      ->t('Revision'),
    $this
      ->t('Operations'),
  );
  $revert_permission = ($account
    ->hasPermission("revert {$type} revisions") || $account
    ->hasPermission('revert all revisions') || $account
    ->hasPermission('administer support tickets')) && $support_ticket
    ->access('update');
  $delete_permission = ($account
    ->hasPermission("delete {$type} revisions") || $account
    ->hasPermission('delete all revisions') || $account
    ->hasPermission('administer support tickets')) && $support_ticket
    ->access('delete');
  $rows = array();
  $vids = $support_ticket_storage
    ->revisionIds($support_ticket);
  foreach (array_reverse($vids) as $vid) {
    $revision = $support_ticket_storage
      ->loadRevision($vid);
    $username = [
      '#theme' => 'username',
      '#account' => $revision->uid->entity,
    ];

    // Use revision link to link to revisions that are not active.
    $date = $this->dateFormatter
      ->format($revision->revision_timestamp->value, 'short');
    if ($vid != $support_ticket
      ->getRevisionId()) {
      $link = $this
        ->l($date, new Url('entity.support_ticket.revision', [
        'support_ticket' => $support_ticket
          ->id(),
        'support_ticket_revision' => $vid,
      ]));
    }
    else {
      $link = $support_ticket
        ->link($date);
    }
    $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' => $this->renderer
            ->renderPlain($username),
          'message' => [
            '#markup' => $revision->revision_log->value,
          ],
        ],
      ],
    ];

    // @todo Simplify once https://www.drupal.org/node/2334319 lands.
    $this->renderer
      ->addCacheableDependency($column['data'], $username);
    $row[] = $column;
    if ($vid == $support_ticket
      ->getRevisionId()) {
      $row[0]['class'] = [
        'revision-current',
      ];
      $row[] = [
        'data' => [
          '#prefix' => '<em>',
          '#markup' => $this
            ->t('current revision'),
          '#suffix' => '</em>',
        ],
        'class' => [
          'revision-current',
        ],
      ];
    }
    else {
      $links = [];
      if ($revert_permission) {
        $links['revert'] = [
          'title' => $this
            ->t('Revert'),
          'url' => Url::fromRoute('support_ticket.revision_revert_confirm', [
            'support_ticket' => $support_ticket
              ->id(),
            'support_ticket_revision' => $vid,
          ]),
        ];
      }
      if ($delete_permission) {
        $links['delete'] = [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('support_ticket.revision_delete_confirm', [
            'support_ticket' => $support_ticket
              ->id(),
            'support_ticket_revision' => $vid,
          ]),
        ];
      }
      $row[] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $links,
        ],
      ];
    }
    $rows[] = $row;
  }
  $build['support_ticket_revisions_table'] = array(
    '#theme' => 'table',
    '#rows' => $rows,
    '#header' => $header,
    '#attached' => array(
      'library' => array(
        'support_ticket/drupal.support_ticket.admin',
      ),
    ),
  );
  return $build;
}