View source
<?php
namespace Drupal\fullcalendar_view\Controller;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class CalendarEventController extends ControllerBase {
private $csrfToken;
public function __construct(LoggerChannelFactoryInterface $loggerFactory, CsrfTokenGenerator $csrfToken) {
$this->loggerFactory = $loggerFactory;
$this->csrfToken = $csrfToken;
}
public static function create(ContainerInterface $container) {
$loggerFactory = $container
->get('logger.factory');
$csrfToken = $container
->get('csrf_token');
return new static($loggerFactory, $csrfToken);
}
public function updateEvent(Request $request) {
$user = $this
->currentUser();
if (!empty($user)) {
$csrf_token = $request->request
->get('token');
if (!$this->csrfToken
->validate($csrf_token, $user
->id())) {
return new Response($this
->t('Access denied!'));
}
$eid = $request->request
->get('eid', '');
$entity_type = $request->request
->get('entity_type', '');
$start_date = $request->request
->get('start', '');
$end_date = $request->request
->get('end', '');
$start_field = $request->request
->get('start_field', '');
$end_field = $request->request
->get('end_field', '');
if (!empty($eid) && !empty($start_date) && !empty($start_field) && !empty($entity_type)) {
$entity = $this
->entityTypeManager()
->getStorage($entity_type)
->load($eid);
if (!empty($entity) && $entity
->access('update')) {
if ($entity
->hasField($start_field)) {
$fields_def = $entity
->getFieldDefinition($start_field);
$start_type = $fields_def
->getType();
if (isset($entity->{$end_field}) && !empty($end_date)) {
$fields_def = $entity
->getFieldDefinition($end_field);
$end_type = $fields_def
->getType();
}
if (is_array($entity->{$start_field})) {
if ($start_type === 'datetime' || $start_type === 'daterange') {
$length = strlen($entity->{$start_field}[0]);
if ($length > 10) {
$entity->{$start_field}[0] = [
'value' => gmdate("Y-m-d\\TH:i:s", strtotime($start_date)),
];
}
else {
$entity->{$start_field}[0] = [
'value' => $start_date,
];
}
}
}
else {
if (is_numeric($entity->{$start_field}->value)) {
$entity->{$start_field}->value = strtotime($start_date);
}
else {
$length = strlen($entity->{$start_field}->value);
if ($length > 10) {
$entity->{$start_field}->value = gmdate("Y-m-d\\TH:i:s", strtotime($start_date));
}
else {
$entity->{$start_field}->value = $start_date;
}
}
}
if (isset($end_type)) {
if (is_array($entity->{$end_field})) {
if ($end_type === 'datetime') {
$length = strlen($entity->{$end_field}[0]);
if ($length > 10) {
$entity->{$end_field}[0] = [
'value' => gmdate("Y-m-d\\TH:i:s", strtotime($end_date)),
];
}
else {
$entity->{$end_field}[0] = [
'value' => $end_date,
];
}
}
elseif ($end_type === 'daterange') {
$length = strlen($entity->{$end_field}[0]->end_value);
if ($length > 10) {
$entity->{$end_field}[0]->end_value = gmdate("Y-m-d\\TH:i:s", strtotime($end_date));
}
else {
$entity->{$end_field}[0]->end_value = $end_date;
}
}
elseif (is_numeric($entity->{$end_field}[0]->value)) {
$entity->{$end_field}[0]->value = strtotime($end_date);
}
}
else {
if ($end_type === 'datetime') {
$length = strlen($entity->{$end_field}->value);
if ($length > 10) {
$entity->{$end_field}->value = gmdate("Y-m-d\\TH:i:s", strtotime($end_date));
}
else {
$entity->{$end_field}->value = $end_date;
}
}
elseif ($end_type === 'daterange') {
$length = strlen($entity->{$end_field}->end_value);
if ($length > 10) {
$entity->{$end_field}->end_value = gmdate("Y-m-d\\TH:i:s", strtotime($end_date));
}
else {
$entity->{$end_field}->end_value = $end_date;
}
}
elseif ($end_type === 'timestamp') {
$entity->{$end_field}->value = strtotime($end_date);
}
}
}
$entity
->save();
$this->loggerFactory
->get($entity_type)
->notice('%entity_type: updated %title', [
'%entity_type' => $entity
->getType(),
'%title' => $entity
->getTitle(),
]);
return new Response($this
->t('%title is updated to from %start to %end', [
'%title' => $entity
->getTitle(),
'%start' => $start_date,
'%end' => $end_date,
]));
}
}
else {
return new Response($this
->t('Access denied!'));
}
}
else {
return new Response($this
->t('Parameter Missing.'));
}
}
else {
return new Response($this
->t('Invalid User!'));
}
}
public function addEvent(Request $request) {
$entity_type_id = $request
->get('entity', '');
$bundle = $request
->get('bundle', '');
$start_field = $request
->get('start_field', '');
$end_field = $request
->get('end_field', '');
$form = [];
if (!empty($bundle) && !empty($entity_type_id)) {
$access_control_handler = $this
->entityTypeManager()
->getAccessControlHandler($entity_type_id);
if ($access_control_handler
->createAccess($bundle)) {
$data = [
'type' => $bundle,
];
$entity = $this
->entityTypeManager()
->getStorage($entity_type_id)
->create($data);
if (!empty($entity)) {
$form = $this
->entityFormBuilder()
->getForm($entity);
$field_def = $entity
->getFieldDefinitions();
foreach ($form as $name => &$element) {
switch ($name) {
case 'advanced':
case 'body':
$element['#access'] = FALSE;
}
if (substr($name, 0, 6) === 'field_' && $name !== $start_field && $name !== $end_field && $name !== 'field_monthly_event' && $name !== 'field_weekly_event' && !$field_def[$name]
->isRequired()) {
$element['#access'] = FALSE;
}
}
if (isset($form['actions']['preview'])) {
$form['actions']['preview']['#access'] = FALSE;
}
$form['actions']['#weight'] = 10000;
return $form;
}
}
}
throw new AccessDeniedHttpException();
}
}