View source
<?php
namespace Drupal\content_calendar\Controller;
use Drupal\content_calendar\Component\Calendar;
use Drupal\content_calendar\ContentCalendarService;
use Drupal\content_calendar\DateTimeHelper;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\content_calendar\ContentTypeConfigService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Zend\Diactoros\Response\JsonResponse;
class CalendarController extends ControllerBase {
protected $request;
protected $contentTypeConfigService;
protected $contentCalendarService;
public function __construct(RequestStack $request_stack, ContentTypeConfigService $content_type_config_service, ContentCalendarService $content_calendar_service, AccountProxyInterface $current_user) {
$this->request = $request_stack
->getCurrentRequest();
$this->contentTypeConfigService = $content_type_config_service;
$this->contentCalendarService = $content_calendar_service;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('content_calendar.content_type_config_service'), $container
->get('content_calendar.content_calendar_service'), $container
->get('current_user'));
}
public function showCurrentCalendarYear() {
$year = date('Y');
return $this
->showCalendarYear($year);
}
public function showCalendarYear($year) {
$calendars = [];
$content_type_config_entities = $this->contentTypeConfigService
->loadAllEntities();
if (!$content_type_config_entities) {
$this
->messenger()
->addMessage($this
->t('Content Calendar is not configured yet. Please do this in the settings tab.'), 'error');
return [];
}
foreach (range(1, 12) as $month) {
$calender = new Calendar($this->contentTypeConfigService, $this->contentCalendarService, $month, $year, $this->currentUser);
$calendars[] = $calender
->build();
}
$form_params = [
'current_year' => date('Y'),
'selected_year' => $year,
];
$filters_form = \Drupal::formBuilder()
->getForm('Drupal\\content_calendar\\Form\\CalenderOverviewFilterForm', $form_params);
if (\Drupal::currentUser()
->hasPermission('administer content calendar settings')) {
$has_permission = TRUE;
}
else {
$has_permission = FALSE;
}
$build = [
'#theme' => 'content_calendar_overview',
'#calendars' => $calendars,
'#filters_form' => $filters_form,
'#has_permission' => $has_permission,
];
return $build;
}
public function updateNodePublishDate(NodeInterface $node, $date) {
$data = [
'success' => FALSE,
'message' => NULL,
];
$content_type_config_entities = $this->contentTypeConfigService
->loadAllEntities();
if (!array_key_exists($node
->getType(), $content_type_config_entities)) {
$data['message'] = $this
->t('Action is not allowed for Nodes of type @type', [
'@type' => $node
->getType(),
]);
return new JsonResponse($data);
}
$created_on_timestamp = $node
->get('created')
->getValue();
$created_on_timestamp_value = $created_on_timestamp[0]['value'];
$original_created_on_datetime = DateTimeHelper::convertUnixTimestampToDatetime($created_on_timestamp_value);
$hour = $original_created_on_datetime
->format('H');
$minutes = $original_created_on_datetime
->format('i');
$seconds = $original_created_on_datetime
->format('s');
$new_created_on_datetime = \DateTime::createFromFormat('Y-m-d', $date);
$new_created_on_datetime
->setTime($hour, $minutes, $seconds);
$node
->set('created', $new_created_on_datetime
->getTimestamp());
$publish_on_timestamp = $node
->get('publish_on')
->getValue();
$publish_on_timestamp_value = $publish_on_timestamp[0]['value'];
if ($publish_on_timestamp_value) {
$original_publish_datetime = DateTimeHelper::convertUnixTimestampToDatetime($publish_on_timestamp_value);
$hour = $original_publish_datetime
->format('H');
$minutes = $original_publish_datetime
->format('i');
$seconds = $original_publish_datetime
->format('s');
$new_publish_datetime = \DateTime::createFromFormat('Y-m-d', $date);
$new_publish_datetime
->setTime($hour, $minutes, $seconds);
$node
->set('publish_on', $new_publish_datetime
->getTimestamp());
$node
->set('created', $new_publish_datetime
->getTimestamp());
}
if ($node
->save() == SAVED_UPDATED) {
$data['success'] = TRUE;
$data['message'] = $this
->t('The creation date for Node @id has been updated', [
'@id' => $node
->id(),
]);
}
return new JsonResponse($data);
}
public function redirectToCurrentCalendar() {
$calendar_id = date('Y-n');
return $this
->redirect('content_calendar.calendar', [], [
'fragment' => $calendar_id,
]);
}
public function redirectToCalendar($year, $month) {
$fragment = $year . '-' . $month;
return $this
->redirect('content_calendar.calendar', [
'year' => $year,
], [
'fragment' => $fragment,
]);
}
public function duplicateNode(NodeInterface $node) {
$duplicate = $node
->createDuplicate();
$duplicate
->setTitle($duplicate
->getTitle() . ' clone');
$duplicate
->save();
$destination = \Drupal::destination()
->get();
return new RedirectResponse($destination);
}
}