You are here

public function SupportTicketRevisionController::compareSupportTicketRevisions in Support Ticketing System 8

Returns a table which shows the differences between two support ticket revisions.

Parameters

SupportTicketInterface $support_ticket: The support ticket whose revisions are compared.

$left_vid: Vid of the support ticket revision from the left.

$right_vid: Vid of the support ticket revision from the right.

$filter: If $filter == 'raw' raw text is compared (including html tags) If filter == 'raw-plain' markdown function is applied to the text before comparison.

Return value

array Table showing the diff between the two support ticket revisions.

1 string reference to 'SupportTicketRevisionController::compareSupportTicketRevisions'
support_ticket.routing.yml in modules/support_ticket/support_ticket.routing.yml
modules/support_ticket/support_ticket.routing.yml

File

modules/support_ticket/src/Controller/SupportTicketRevisionController.php, line 49
Contains \Drupal\support_ticket\Controller\SupportTicketRevisionController.

Class

SupportTicketRevisionController
Returns responses for Support Ticket Revision routes.

Namespace

Drupal\support_ticket\Controller

Code

public function compareSupportTicketRevisions(SupportTicketInterface $support_ticket, $left_vid, $right_vid, $filter) {
  $diff_rows = array();
  $build = array(
    '#title' => $this
      ->t('Revisions for %title', array(
      '%title' => $support_ticket
        ->label(),
    )),
  );
  if (!in_array($filter, array(
    'raw',
    'raw-plain',
  ))) {
    $filter = 'raw';
  }
  elseif ($filter == 'raw-plain') {
    $filter = 'raw_plain';
  }

  // Support Ticket storage service.
  $storage = $this
    ->entityManager()
    ->getStorage('support_ticket');
  $left_revision = $storage
    ->loadRevision($left_vid);
  $right_revision = $storage
    ->loadRevision($right_vid);
  $vids = $storage
    ->revisionIds($support_ticket);
  $diff_rows[] = $this
    ->buildRevisionsNavigation($support_ticket
    ->id(), $vids, $left_vid, $right_vid);
  $diff_rows[] = $this
    ->buildMarkdownNavigation($support_ticket
    ->id(), $left_vid, $right_vid, $filter);
  $diff_header = $this
    ->buildTableHeader($left_revision, $right_revision);

  // Perform comparison only if both support ticket revisions loaded successfully.
  if ($left_revision != FALSE && $right_revision != FALSE) {
    $fields = $this
      ->compareRevisions($left_revision, $right_revision);
    $support_ticket_base_fields = $this
      ->entityManager()
      ->getBaseFieldDefinitions('support_ticket');

    // Check to see if we need to display certain fields or not based on
    // selected view mode display settings.
    foreach ($fields as $field_name => $field) {

      // If we are dealing with support tickets only compare those fields
      // set as visible from the selected view mode.
      $view_mode = $this->config
        ->get('support_ticket_type_settings.' . $support_ticket
        ->getType() . '.view_mode');

      // If no view mode is selected use the default view mode.
      if ($view_mode == NULL) {
        $view_mode = 'default';
      }
      $visible = entity_get_display('support_ticket', $support_ticket
        ->getType(), $view_mode)
        ->getComponent($field_name);
      if ($visible == NULL && !array_key_exists($field_name, $support_ticket_base_fields)) {
        unset($fields[$field_name]);
      }
    }

    // Build the diff rows for each field and append the field rows
    // to the table rows.
    foreach ($fields as $field) {
      $field_label_row = '';
      if (!empty($field['#name'])) {
        $field_label_row = array(
          'data' => $this
            ->t('Changes to %name', array(
            '%name' => $field['#name'],
          )),
          'colspan' => 4,
          'class' => array(
            'field-name',
          ),
        );
      }
      $field_diff_rows = $this
        ->getRows($field['#states'][$filter]['#left'], $field['#states'][$filter]['#right']);

      // Add the field label to the table only if there are changes to that field.
      if (!empty($field_diff_rows) && !empty($field_label_row)) {
        $diff_rows[] = array(
          $field_label_row,
        );
      }

      // Add field diff rows to the table rows.
      $diff_rows = array_merge($diff_rows, $field_diff_rows);
    }

    // Add the CSS for the diff.
    $build['#attached']['library'][] = 'diff/diff.general';
    $theme = $this->config
      ->get('general_settings.theme');
    if ($theme) {
      if ($theme == 'default') {
        $build['#attached']['library'][] = 'diff/diff.default';
      }
      elseif ($theme == 'github') {
        $build['#attached']['library'][] = 'diff/diff.github';
      }
    }
    elseif ($theme == NULL) {
      $build['#attached']['library'][] = 'diff/diff.github';
    }
    $build['diff'] = array(
      '#type' => 'table',
      '#header' => $diff_header,
      '#rows' => $diff_rows,
      '#empty' => $this
        ->t('No visible changes'),
      '#attributes' => array(
        'class' => array(
          'diff',
        ),
      ),
    );
    $build['back'] = array(
      '#type' => 'link',
      '#attributes' => array(
        'class' => array(
          'button',
          'diff-button',
        ),
      ),
      '#title' => $this
        ->t('Back to Revision Overview'),
      '#url' => Url::fromRoute('entity.support_ticket.version_history', [
        'support_ticket' => $support_ticket
          ->id(),
      ]),
    );
    return $build;
  }
  else {

    // @todo When task 'Convert drupal_set_message() to a service' (2278383)
    //   will be merged use the corresponding service instead.
    drupal_set_message($this
      ->t('Selected support ticket revisions could not be loaded.'), 'error');
  }
}