This repository has been archived on 2022-07-12. You can view files and clone it, but cannot push or open issues or pull requests.
opencase/modules/opencase_cases/src/Controller/OCActorCaseRelationControll...

210 lines
7.3 KiB
PHP

<?php
namespace Drupal\opencase_cases\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url;
use Drupal\opencase_cases\Entity\OCActorCaseRelationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class OCActorCaseRelationController.
*
* Returns responses for Actor-Case Relation routes.
*/
class OCActorCaseRelationController 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 Actor-Case Relation revision.
*
* @param int $oc_actor_case_relation_revision
* The Actor-Case Relation revision ID.
*
* @return array
* An array suitable for drupal_render().
*/
public function revisionShow($oc_actor_case_relation_revision) {
$oc_actor_case_relation = $this->entityTypeManager()->getStorage('oc_actor_case_relation')
->loadRevision($oc_actor_case_relation_revision);
$view_builder = $this->entityTypeManager()->getViewBuilder('oc_actor_case_relation');
return $view_builder->view($oc_actor_case_relation);
}
/**
* Page title callback for a Actor-Case Relation revision.
*
* @param int $oc_actor_case_relation_revision
* The Actor-Case Relation revision ID.
*
* @return string
* The page title.
*/
public function revisionPageTitle($oc_actor_case_relation_revision) {
$oc_actor_case_relation = $this->entityTypeManager()->getStorage('oc_actor_case_relation')
->loadRevision($oc_actor_case_relation_revision);
return $this->t('Revision of %title from %date', [
'%title' => $oc_actor_case_relation->label(),
'%date' => $this->dateFormatter->format($oc_actor_case_relation->getRevisionCreationTime()),
]);
}
/**
* Generates an overview table of older revisions of a Actor-Case Relation.
*
* @param \Drupal\opencase_cases\Entity\OCActorCaseRelationInterface $oc_actor_case_relation
* A Actor-Case Relation object.
*
* @return array
* An array as expected by drupal_render().
*/
public function revisionOverview(OCActorCaseRelationInterface $oc_actor_case_relation) {
$account = $this->currentUser();
$oc_actor_case_relation_storage = $this->entityTypeManager()->getStorage('oc_actor_case_relation');
$langcode = $oc_actor_case_relation->language()->getId();
$langname = $oc_actor_case_relation->language()->getName();
$languages = $oc_actor_case_relation->getTranslationLanguages();
$has_translations = (count($languages) > 1);
$build['#title'] = $has_translations ? $this->t('@langname revisions for %title', ['@langname' => $langname, '%title' => $oc_actor_case_relation->label()]) : $this->t('Revisions for %title', ['%title' => $oc_actor_case_relation->label()]);
$header = [$this->t('Revision'), $this->t('Operations')];
$revert_permission = (($account->hasPermission("revert all actor-case relation revisions") || $account->hasPermission('administer actor-case relation entities')));
$delete_permission = (($account->hasPermission("delete all actor-case relation revisions") || $account->hasPermission('administer actor-case relation entities')));
$rows = [];
$vids = $oc_actor_case_relation_storage->revisionIds($oc_actor_case_relation);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
/** @var \Drupal\opencase_cases\OCActorCaseRelationInterface $revision */
$revision = $oc_actor_case_relation_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.
$date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
if ($vid != $oc_actor_case_relation->getRevisionId()) {
$link = $this->l($date, new Url('entity.oc_actor_case_relation.revision', [
'oc_actor_case_relation' => $oc_actor_case_relation->id(),
'oc_actor_case_relation_revision' => $vid,
]));
}
else {
$link = $oc_actor_case_relation->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->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' => $has_translations ?
Url::fromRoute('entity.oc_actor_case_relation.translation_revert', [
'oc_actor_case_relation' => $oc_actor_case_relation->id(),
'oc_actor_case_relation_revision' => $vid,
'langcode' => $langcode,
]) :
Url::fromRoute('entity.oc_actor_case_relation.revision_revert', [
'oc_actor_case_relation' => $oc_actor_case_relation->id(),
'oc_actor_case_relation_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this->t('Delete'),
'url' => Url::fromRoute('entity.oc_actor_case_relation.revision_delete', [
'oc_actor_case_relation' => $oc_actor_case_relation->id(),
'oc_actor_case_relation_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
}
$build['oc_actor_case_relation_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
}