View source
<?php
namespace Drupal\content_calendar\Component;
use Drupal\content_calendar\ContentTypeConfigService;
use Drupal\content_calendar\ContentCalendarService;
use Drupal\content_calendar\DateTimeHelper;
use Drupal\Core\Session\AccountProxyInterface;
class Calendar {
protected $contentTypeConfigService;
protected $contentCalendarService;
protected $contentTypeConfigEntities;
protected $month;
protected $year;
protected $user;
public function __construct(ContentTypeConfigService $content_type_config_service, ContentCalendarService $content_calendar_service, $month, $year, AccountProxyInterface $user) {
$this->contentTypeConfigService = $content_type_config_service;
$this->contentCalendarService = $content_calendar_service;
$this->contentTypeConfigEntities = $this->contentTypeConfigService
->loadAllEntities();
$this->month = $month;
$this->year = $year;
$this->user = $user;
}
protected function generateCalendarId() {
return $this->year . '-' . $this->month;
}
public function build() {
$calendar = $this
->buildCalendarDataStructure();
$node_basic_data = [];
foreach ($this->contentTypeConfigEntities as $node_type => $config_entity) {
$node_basic_data[$node_type] = $this->contentCalendarService
->getNodesByType($node_type, [
'month' => $this->month,
'year' => $this->year,
]);
}
$this
->placeNodesInCalendars($calendar, $node_basic_data);
$build = [
'#theme' => 'content_calendar',
'#calendar' => $calendar,
'#node_type_creation_permissions' => $this
->getPermittedNodeTypeCreationActions(),
'#attached' => [
'library' => [
'content_calendar/calendar',
],
],
];
return $build;
}
protected function getPermittedNodeTypeCreationActions() {
$permitted_node_types = [];
foreach ($this->contentTypeConfigEntities as $node_type => $config_entity) {
if ($this->user
->hasPermission("create {$node_type} content")) {
$permitted_node_types[$node_type] = $config_entity;
}
}
return $permitted_node_types;
}
protected function buildCalendarDataStructure() {
$today_datetime = new \DateTime();
$today_datetime
->setTime(0, 0, 0);
$one_day_interval = new \DateInterval('P1D');
$datetime = DateTimeHelper::getFirstDayOfMonth($this->month, $this->year);
$scaffold_data = [
'calendar_id' => $this
->generateCalendarId(),
'month' => $this->month,
'year' => $this->year,
'label' => DateTimeHelper::getMonthLabelByNumber($this->month) . ' ' . $this->year,
'first_date_weekday' => $datetime
->format('N'),
'days' => [],
];
$days_in_month = DateTimeHelper::getDayCountInMonth($this->month, $this->year);
$i = 1;
while ($i <= $days_in_month) {
$scaffold_data['days'][] = [
'date' => $datetime
->format('Y-m-d'),
'day' => $datetime
->format('j'),
'weekday' => $datetime
->format('N'),
'nodes' => [],
'is_today' => $today_datetime == $datetime ? TRUE : FALSE,
];
$i++;
$datetime
->add($one_day_interval);
}
return $scaffold_data;
}
protected function placeNodesInCalendars(array &$calendar, array $node_basic_data) {
foreach ($node_basic_data as $node_type => $node_rows) {
foreach ($node_rows as $node_row) {
$calendar_entry = new CalendarEntry($this->month, $this->year, $this
->getNodeTypeConfig($node_type), $node_row);
foreach ($calendar['days'] as &$day) {
if ($day['date'] == $calendar_entry
->formatSchedulingDateAsMySQLDateOnly()) {
$key = $calendar_entry
->getRelevantDate() . '_' . $calendar_entry
->getNodeID();
$day['nodes'][$key] = $calendar_entry
->build();
ksort($day['nodes']);
}
}
}
}
}
protected function getNodeTypeConfig($node_type) {
if (array_key_exists($node_type, $this->contentTypeConfigEntities)) {
return $this->contentTypeConfigEntities[$node_type];
}
return FALSE;
}
}