You are here

public function Calendar::explodeValues in Calendar 8

@todo rename and document

Parameters

\Drupal\calendar\CalendarEvent $event: A calendar event to explode date values.

Return value

array Return an array of calendar rows.

Throws

\Exception

1 call to Calendar::explodeValues()
Calendar::render in src/Plugin/views/row/Calendar.php
Render a row object. This usually passes through to a theme template of some form, but not always.

File

src/Plugin/views/row/Calendar.php, line 570

Class

Calendar
Plugin which creates a view on the resulting object and formats it as a Calendar entity.

Namespace

Drupal\calendar\Plugin\views\row

Code

public function explodeValues(CalendarEvent $event) {
  $rows = [];
  $dateInfo = $this->dateArgument->view->dateInfo;
  $item_start_date = $event
    ->getStartDate()
    ->getTimestamp();
  $item_end_date = $event
    ->getEndDate()
    ->getTimestamp();

  // Now that we have an 'entity' for each view result, we need to remove
  // anything outside the view date range, and possibly create additional
  // nodes so that we have a 'node' for each day that this item occupies in
  // this view.
  // @todo make this work with the CalendarDateInfo object
  $now = $event
    ->getStartDate()
    ->format('Y-m-d');
  $to = $event
    ->getEndDate()
    ->format('Y-m-d');
  $next = new \DateTime();
  $next
    ->setTimestamp($event
    ->getStartDate()
    ->getTimestamp());
  if (timezone_name_get($this->dateArgument->view->dateInfo
    ->getTimezone()) != $event
    ->getTimezone()
    ->getName()) {

    // Make $start and $end (derived from $node) use the timezone $to_zone,
    // just as the original dates do.
    $next
      ->setTimezone($event
      ->getTimezone());
  }
  if (empty($to) || $now > $to) {
    $to = $now;
  }

  // $now and $next are midnight (in display timezone) on
  // the first day where node will occur.
  // $to is midnight on the last day where node will occur.
  // All three were limited by the min-max date range of the view.
  $position = 0;
  while (!empty($now) && $now <= $to) {

    /** @var \Drupal\calendar\CalendarEvent $entity */
    $entity = clone $event;

    // Get start and end of current day.
    $start = $this->dateFormatter
      ->format($next
      ->getTimestamp(), 'custom', 'Y-m-d H:i:s');
    $next
      ->setTimestamp(strtotime(' +1 day -1 second', $next
      ->getTimestamp()));
    $end = $this->dateFormatter
      ->format($next
      ->getTimestamp(), 'custom', 'Y-m-d H:i:s');

    // Get start and end of item, formatted the same way.
    $item_start = $this->dateFormatter
      ->format($item_start_date, 'custom', 'Y-m-d H:i:s');
    $item_end = $this->dateFormatter
      ->format($item_end_date, 'custom', 'Y-m-d H:i:s');

    // Get intersection of current day and the node value's duration (as
    // strings in $to_zone timezone).
    $start_string = $item_start < $start ? $start : $item_start;
    $end_string = !empty($item_end) ? $item_end > $end ? $end : $item_end : NULL;
    $entity->calendar_start_date = new \DateTime($start_string);
    $entity->calendar_end_date = new \DateTime($end_string);

    // @todo don't hardcode granularity and increment
    $granularity = 'day';
    $increment = 1;
    $entity
      ->setAllDay(CalendarHelper::dateIsAllDay($entity
      ->getStartDate()
      ->format('Y-m-d H:i:s'), $entity
      ->getEndDate()
      ->format('Y-m-d H:i:s'), $granularity, $increment));
    $calendar_start = $this->dateFormatter
      ->format($entity->calendar_start_date
      ->getTimestamp(), 'custom', 'Y-m-d H:i:s');
    $calendar_end = $this->dateFormatter
      ->format($entity->calendar_end_date
      ->getTimestamp(), 'custom', 'Y-m-d H:i:s');

    // unset($entity->calendar_fields);.
    if (isset($entity) && empty($calendar_start)) {

      // If no date for the node and no date in the item
      // there is no way to display it on the calendar.
      unset($entity);
    }
    else {
      $entity->date_id .= '.' . $position;
      $rows[] = $entity;
      unset($entity);
    }
    $next
      ->setTimestamp(strtotime('+1 second', $next
      ->getTimestamp()));
    $now = $this->dateFormatter
      ->format($next
      ->getTimestamp(), 'custom', 'Y-m-d');
    $position++;
  }
  return $rows;
}