You are here

class InvoiceController in Drupal PM (Project Management) 4.x

Class InvoiceController.

Returns responses for Invoice routes.

Hierarchy

Expanded class hierarchy of InvoiceController

File

modules/pm_invoice/src/Controller/InvoiceController.php, line 18

Namespace

Drupal\pm_invoice\Controller
View source
class InvoiceController extends ControllerBase implements ContainerInjectionInterface {

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

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

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

  /**
   * Displays a Invoice revision.
   *
   * @param int $pm_invoice_revision
   *   The Invoice revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   */
  public function revisionShow($pm_invoice_revision) {
    $pm_invoice = $this
      ->entityTypeManager()
      ->getStorage('pm_invoice')
      ->loadRevision($pm_invoice_revision);
    $view_builder = $this
      ->entityTypeManager()
      ->getViewBuilder('pm_invoice');
    return $view_builder
      ->view($pm_invoice);
  }

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

  /**
   * Generates an overview table of older revisions of a Invoice.
   *
   * @param \Drupal\pm_invoice\Entity\InvoiceInterface $pm_invoice
   *   A Invoice object.
   *
   * @return array
   *   An array as expected by drupal_render().
   */
  public function revisionOverview(InvoiceInterface $pm_invoice) {
    $account = $this
      ->currentUser();
    $pm_invoice_storage = $this
      ->entityTypeManager()
      ->getStorage('pm_invoice');
    $build['#title'] = $this
      ->t('Revisions for %title', [
      '%title' => $pm_invoice
        ->label(),
    ]);
    $header = [
      $this
        ->t('Revision'),
      $this
        ->t('Operations'),
    ];
    $revert_permission = $account
      ->hasPermission("revert all invoice revisions") || $account
      ->hasPermission('administer invoice entities');
    $delete_permission = $account
      ->hasPermission("delete all invoice revisions") || $account
      ->hasPermission('administer invoice entities');
    $rows = [];
    $vids = $pm_invoice_storage
      ->revisionIds($pm_invoice);
    $latest_revision = TRUE;
    foreach (array_reverse($vids) as $vid) {

      /** @var \Drupal\pm_invoice\InvoiceInterface $revision */
      $revision = $pm_invoice_storage
        ->loadRevision($vid);
      $username = [
        '#theme' => 'username',
        '#account' => $revision
          ->getRevisionUser(),
      ];

      // Use revision link to link to revisions that are not active.
      $date = $this->dateFormatter
        ->format($revision
        ->getRevisionCreationTime(), 'short');
      if ($vid != $pm_invoice
        ->getRevisionId()) {
        $link = Link::fromTextAndUrl($date, new Url('entity.pm_invoice.revision', [
          'pm_invoice' => $pm_invoice
            ->id(),
          'pm_invoice_revision' => $vid,
        ]));
      }
      else {
        $link = $pm_invoice
          ->toLink()
          ->toString($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
                ->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.pm_invoice.revision_revert', [
              'pm_invoice' => $pm_invoice
                ->id(),
              'pm_invoice_revision' => $vid,
            ]),
          ];
        }
        if ($delete_permission) {
          $links['delete'] = [
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('entity.pm_invoice.revision_delete', [
              'pm_invoice' => $pm_invoice
                ->id(),
              'pm_invoice_revision' => $vid,
            ]),
          ];
        }
        $row[] = [
          'data' => [
            '#type' => 'operations',
            '#links' => $links,
          ],
        ];
      }
      $rows[] = $row;
    }
    $build['pm_invoice_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::$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::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.
ControllerBase::state protected function Returns the state storage service.
InvoiceController::$dateFormatter protected property The date formatter.
InvoiceController::$renderer protected property The renderer.
InvoiceController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
InvoiceController::revisionOverview public function Generates an overview table of older revisions of a Invoice.
InvoiceController::revisionPageTitle public function Page title callback for a Invoice revision.
InvoiceController::revisionShow public function Displays a Invoice revision.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.