You are here

public function CalendarEventController::updateEvent in Fullcalendar View 8

Same name and namespace in other branches
  1. 8.3 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 44

Class

CalendarEventController
Calendar Event Controller.

Namespace

Drupal\fullcalendar_view\Controller

Code

public function updateEvent(Request $request) {
  $user = $this
    ->currentUser();
  if (!empty($user)) {
    $nid = $request->request
      ->get('nid', '');
    $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($nid) && !empty($start_date) && !empty($start_field)) {
      $node = $this
        ->entityTypeManager()
        ->getStorage('node')
        ->load($nid);
      if (!empty($node) && $node
        ->access('update')) {
        if (isset($node->{$start_field})) {

          // Field definitions.
          $fields_def = $node
            ->getFieldDefinition($start_field);
          $start_type = $fields_def
            ->getType();
          if (isset($node->{$end_field}) && !empty($end_date)) {
            $fields_def = $node
              ->getFieldDefinition($end_field);
            $end_type = $fields_def
              ->getType();
          }

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

                // Only update the first value.
                $node->{$start_field}[0] = [
                  'value' => substr(gmdate(DATE_ATOM, strtotime($start_date)), 0, $length),
                ];
              }
              else {
                $node->{$start_field}[0] = [
                  'value' => $start_date,
                ];
              }
            }
          }
          else {

            // Dateime field.
            if ($start_type === 'datetime') {
              $length = strlen($node->{$start_field}->value);
              if ($length > 10) {

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

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

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

                  // Only update the first value.
                  $node->{$end_field}[0] = [
                    'value' => substr(gmdate(DATE_ATOM, strtotime($end_date)), 0, $length),
                  ];
                }
                else {
                  $node->{$end_field}[0] = [
                    'value' => $end_date,
                  ];
                }
              }
            }
            else {

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

                  // UTC Date with time.
                  $node->{$end_field}->value = gmdate("Y-m-d\\TH:i:s", strtotime($end_date));
                }
                else {
                  $node->{$end_field}->value = $end_date;
                }
              }
              elseif ($end_type === 'timestamp') {
                $node->{$end_field}->value = strtotime($end_date);
              }
            }
          }
          $node
            ->save();

          // Log the content changed.
          $this->loggerFactory
            ->get('content')
            ->notice($node
            ->getType() . ': updated ' . $node
            ->getTitle());
          return new Response($node
            ->getTitle() . ' is updated to from ' . $start_date . ' to ' . $end_date);
        }
      }
      else {
        return new Response('Access denied!');
      }
    }
    else {
      return new Response('Parameter Missing.');
    }
  }
  else {
    return new Response('Invalid User!');
  }
}