You are here

public function CalendarEventController::updateEvent in Fullcalendar View 8.3

Same name and namespace in other branches
  1. 8 src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::updateEvent()
  2. 8.2 src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::updateEvent()
  3. 6.x src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::updateEvent()
  4. 5.x src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::updateEvent()

Event Update Handler.

1 string reference to 'CalendarEventController::updateEvent'
fullcalendar_view.routing.yml in ./fullcalendar_view.routing.yml
fullcalendar_view.routing.yml

File

src/Controller/CalendarEventController.php, line 56

Class

CalendarEventController
Calendar Event Controller.

Namespace

Drupal\fullcalendar_view\Controller

Code

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)) {

          // Field definitions.
          $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();
          }

          // Multiple value of start field.
          if (is_array($entity->{$start_field})) {
            if ($start_type === 'datetime' || $start_type === 'daterange') {
              $length = strlen($entity->{$start_field}[0]);
              if ($length > 10) {

                // Only update the first value.
                $entity->{$start_field}[0] = [
                  'value' => gmdate("Y-m-d\\TH:i:s", strtotime($start_date)),
                ];
              }
              else {
                $entity->{$start_field}[0] = [
                  'value' => $start_date,
                ];
              }
            }
          }
          else {

            // Datetime field.
            if (is_numeric($entity->{$start_field}->value)) {
              $entity->{$start_field}->value = strtotime($start_date);
            }
            else {
              $length = strlen($entity->{$start_field}->value);
              if ($length > 10) {

                // UTC Date with time.
                $entity->{$start_field}->value = gmdate("Y-m-d\\TH:i:s", strtotime($start_date));
              }
              else {
                $entity->{$start_field}->value = $start_date;
              }
            }
          }

          // End date.
          if (isset($end_type)) {

            // Multiple value of end field.
            if (is_array($entity->{$end_field})) {
              if ($end_type === 'datetime') {
                $length = strlen($entity->{$end_field}[0]);
                if ($length > 10) {

                  // Only update the first value.
                  $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) {

                  // UTC Date with time.
                  $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 {

              // Datetime field.
              if ($end_type === 'datetime') {
                $length = strlen($entity->{$end_field}->value);
                if ($length > 10) {

                  // UTC Date with time.
                  $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) {

                  // UTC Date with time.
                  $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();

          // Log the content changed.
          $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!'));
  }
}