You are here

class SupportTicketController in Support Ticketing System 8

Returns responses for Support Ticket routes.

Hierarchy

Expanded class hierarchy of SupportTicketController

File

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

Namespace

Drupal\support_ticket\Controller
View source
class SupportTicketController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a SupportTicketController object.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
  }

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

  /**
   * Displays add content links for available support ticket types.
   *
   * Redirects to support_ticket/add/[type] if only one support ticket type is available.
   *
   * @return array
   *   A render array for a list of the support ticket types that can be added; however,
   *   if there is only one support ticket type defined for the site, the function
   *   redirects to the support ticket add page for that one support ticket type and does
   *   not return at all.
   *
   * @see support_ticket_menu()
   */
  public function addPage() {
    $build = [
      '#theme' => 'support_ticket_add_list',
      '#cache' => [
        'tags' => $this
          ->entityManager()
          ->getDefinition('support_ticket_type')
          ->getListCacheTags(),
      ],
    ];
    $types = array();

    // Only use support ticket types the user has access to.
    foreach ($this
      ->entityManager()
      ->getStorage('support_ticket_type')
      ->loadMultiple() as $type) {
      $access = $this
        ->entityManager()
        ->getAccessControlHandler('support_ticket')
        ->createAccess($type
        ->id(), NULL, [], TRUE);
      if ($access
        ->isAllowed()) {
        $types[$type
          ->id()] = $type;
      }
      $this->renderer
        ->addCacheableDependency($build, $access);
    }

    // Bypass the support_ticket/add listing if only one support ticket type is available.
    if (count($types) == 1) {
      $type = array_shift($types);
      return $this
        ->redirect('support_ticket.add', array(
        'support_ticket_type' => $type
          ->id(),
      ));
    }
    $build['#content'] = $types;
    return $build;
  }

  /**
   * Provides the support ticket submission form.
   *
   * @param \Drupal\support_ticket\SupportTicketTypeInterface $support_ticket_type
   *   The support ticket type entity for the support ticket.
   *
   * @return array
   *   A support ticket submission form.
   */
  public function add(SupportTicketTypeInterface $support_ticket_type) {
    $support_ticket = $this
      ->entityManager()
      ->getStorage('support_ticket')
      ->create(array(
      'support_ticket_type' => $support_ticket_type
        ->id(),
    ));
    $form = $this
      ->entityFormBuilder()
      ->getForm($support_ticket);
    return $form;
  }

  /**
   * Displays a support_ticket revision.
   *
   * @param int $supprt_ticket_revision
   *   The support ticket revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function revisionShow($support_ticket_revision) {
    $support_ticket = $this
      ->entityManager()
      ->getStorage('support_ticket')
      ->loadRevision($support_ticket_revision);
    $support_ticket_view_controller = new SupportTicketViewController($this->entityManager, $this->renderer);
    $page = $support_ticket_view_controller
      ->view($support_ticket);
    unset($page['support_tickets'][$support_ticket
      ->id()]['#cache']);
    return $page;
  }

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

  /**
   * Generates an overview table of older revisions of a support ticket.
   *
   * @param \Drupal\support_ticket\SupportTicketInterface $support_ticket
   *   A support_ticket object.
   *
   * @return array
   *   An array as expected by drupal_render().
   */
  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;
  }

  /**
   * The _title_callback for the support_ticket.add route.
   *
   * @param \Drupal\support_ticket\SupportTicketTypeInterface $support_ticket_type
   *   The current support ticket.
   *
   * @return string
   *   The page title.
   */
  public function addPageTitle(SupportTicketTypeInterface $support_ticket_type) {
    return $this
      ->t('Create @name', array(
      '@name' => $support_ticket_type
        ->label(),
    ));
  }

}

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.
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.
SupportTicketController::$dateFormatter protected property The date formatter service.
SupportTicketController::$renderer protected property The renderer service.
SupportTicketController::add public function Provides the support ticket submission form.
SupportTicketController::addPage public function Displays add content links for available support ticket types.
SupportTicketController::addPageTitle public function The _title_callback for the support_ticket.add route.
SupportTicketController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SupportTicketController::revisionOverview public function Generates an overview table of older revisions of a support ticket.
SupportTicketController::revisionPageTitle public function Page title callback for a support ticket revision.
SupportTicketController::revisionShow public function Displays a support_ticket revision.
SupportTicketController::__construct public function Constructs a SupportTicketController object.
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.