EditionsController.php in Simplenews Scheduler 8
File
src/Controller/EditionsController.php
View source
<?php
namespace Drupal\simplenews_scheduler\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditionsController extends ControllerBase {
protected $entityManager;
protected $currentUser;
protected $database;
public function __construct(EntityManagerInterface $entity_manager, AccountProxyInterface $current_user, Connection $database) {
$this->entityManager = $entity_manager;
$this->currentUser = $current_user;
$this->database = $database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('current_user'), $container
->get('database'));
}
public function checkAccess(NodeInterface $node) {
if ($node
->hasField('simplenews_issue') && $node->simplenews_issue->target_id != NULL && $this->currentUser
->hasPermission('overview scheduled newsletters')) {
return AccessResult::allowedIf(!empty($node->simplenews_scheduler) || !empty($node->is_edition));
}
return AccessResult::forbidden();
}
function getPid(NodeInterface $node) {
if (isset($node->simplenews_scheduler_edition)) {
return $node->simplenews_scheduler_edition->pid;
}
elseif (isset($node->simplenews_scheduler)) {
return $node
->id();
}
return FALSE;
}
public function nodeEditionsPage(NodeInterface $node) {
$nid = $this
->getPid($node);
$output = array();
$rows = array();
if ($nid == $node
->id()) {
$output['prefix']['#markup'] = '<p>' . t('This is a newsletter template node of which all corresponding editions nodes are based on.') . '</p>';
$result = $this->database
->select('simplenews_scheduler_editions', 's')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->limit(20)
->fields('s')
->condition('s.pid', $nid)
->execute()
->fetchAll();
foreach ($result as $row) {
$node = $this->entityManager
->getStorage('node')
->load($row->eid);
$rows[] = array(
$node
->link(),
format_date($row->date_issued, 'custom', 'Y-m-d H:i'),
);
}
$output['table'] = array(
'#type' => 'table',
'#header' => array(
t('Edition Node'),
t('Date sent'),
),
'#rows' => $rows,
'#attributes' => array(
'class' => array(
'schedule-history',
),
),
'#empty' => t('No scheduled newsletter editions have been sent.'),
);
$output['pager'] = array(
'#type' => 'pager',
);
}
else {
$master_node = $this->entityManager
->getStorage('node')
->load($nid);
$output['prefix']['#markup'] = '<p>' . t('This node is part of a scheduled newsletter configuration. View the original newsletter <a href="@parent">here</a>.', array(
'@parent' => $master_node
->url(),
)) . '</p>';
}
return $output;
}
}