View source
<?php
namespace Drupal\gdpr_consent\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\gdpr_consent\Entity\ConsentAgreement;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConsentAgreementController extends ControllerBase {
private $entityFieldManager;
private $dateFormatter;
private $renderer;
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, DateFormatterInterface $date_formatter, Renderer $renderer) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_field.manager'), $container
->get('entity_type.manager'), $container
->get('date.formatter'), $container
->get('renderer'));
}
public function revisionShow($gdpr_consent_agreement_revision) {
$gdprConsentAgreement = $this->entityTypeManager
->getStorage('gdpr_consent_agreement')
->loadRevision($gdpr_consent_agreement_revision);
$viewBuilder = $this->entityTypeManager
->getViewBuilder('gdpr_consent_agreement');
return $viewBuilder
->view($gdprConsentAgreement);
}
public function revisionPageTitle($gdpr_consent_agreement_revision) {
$gdprConsentAgreement = $this->entityTypeManager
->getStorage('gdpr_consent_agreement')
->loadRevision($gdpr_consent_agreement_revision);
return $this
->t('Revision of %title from %date', [
'%title' => $gdprConsentAgreement
->label(),
'%date' => $this->dateFormatter
->format($gdprConsentAgreement
->getRevisionCreationTime()),
]);
}
public function revisionOverview(ConsentAgreement $gdpr_consent_agreement) {
$account = $this
->currentUser();
$storage = $this->entityTypeManager
->getStorage('gdpr_consent_agreement');
$build['#title'] = $this
->t('Revisions for %title', [
'%title' => $gdpr_consent_agreement->title->value,
]);
$header = [
$this
->t('Revision'),
$this
->t('Operations'),
];
$revert_permission = $account
->hasPermission('manage gdpr agreements');
$delete_permission = $account
->hasPermission('manage gdpr agreements');
$rows = [];
$vids = $storage
->revisionIds($gdpr_consent_agreement);
$latest_revision = TRUE;
foreach (\array_reverse($vids) as $vid) {
$revision = $storage
->loadRevision($vid);
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
$date = $this->dateFormatter
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid !== $gdpr_consent_agreement
->getRevisionId()) {
$link = Link::fromTextAndUrl($date, new Url('entity.gdpr_consent_agreement.revision', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]))
->toRenderable();
$link = $this->renderer
->renderPlain($link);
}
else {
$link = $gdpr_consent_agreement
->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',
];
}
unset($current);
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this
->t('Revert'),
'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_revert', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.gdpr_consent_agreement.revision_delete', [
'gdpr_consent_agreement' => $gdpr_consent_agreement
->id(),
'gdpr_consent_agreement_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
$build['gdpr_consent_agreement_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}
public function myAgreements(AccountInterface $user) {
$map = $this->entityFieldManager
->getFieldMapByFieldType('gdpr_user_consent');
$agreement_storage = $this->entityTypeManager
->getStorage('gdpr_consent_agreement');
$rows = [];
foreach ($map as $entity_type => $fields) {
$field_names = \array_keys($fields);
foreach ($field_names as $field_name) {
$ids = $this->entityTypeManager
->getStorage($entity_type)
->getQuery()
->condition($field_name . '.user_id', $user
->id())
->execute();
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadMultiple($ids);
foreach ($entities as $entity) {
$agreement = $agreement_storage
->loadRevision($entity->{$field_name}->target_revision_id);
$link = $agreement->title->value;
if ($agreement
->access('view', $this->currentUser)) {
$link = $agreement
->toLink($agreement->title->value, 'revision')
->toString();
}
$row = [];
$row[] = [
'data' => [
'#markup' => $link,
],
];
$row[] = [
'data' => [
'#markup' => $entity->{$field_name}->date,
],
];
$rows[] = $row;
}
}
}
$header = [
'Agreement',
'Date Agreed',
];
$build = [
'#title' => 'Consent Agreements',
'table' => [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
'#empty' => $this
->t('You have not yet given any consent.'),
],
];
return $build;
}
}