You are here

class CalendarEventController in Fullcalendar View 8

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

Calendar Event Controller.

Hierarchy

Expanded class hierarchy of CalendarEventController

File

src/Controller/CalendarEventController.php, line 15

Namespace

Drupal\fullcalendar_view\Controller
View source
class CalendarEventController extends ControllerBase {

  /**
   * Construct the Controller.
   *
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
   *   Logger factory object.
   */
  public function __construct(LoggerChannelFactoryInterface $loggerFactory) {
    $this->loggerFactory = $loggerFactory;
  }

  /**
   * Create a CalendarEventController instance.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Container object.
   *
   * @return \Drupal\fullcalendar_view\Controller\CalendarEventController
   *   The instance of CalendarEventController.
   */
  public static function create(ContainerInterface $container) {
    $loggerFactory = $container
      ->get('logger.factory');
    return new static($loggerFactory);
  }

  /**
   * Event Update Handler.
   */
  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!');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CalendarEventController::create public static function Create a CalendarEventController instance. Overrides ControllerBase::create
CalendarEventController::updateEvent public function Event Update Handler.
CalendarEventController::__construct public function Construct the Controller.
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.