You are here

public function IcalFieldsWizard::addSmartDateEvent in Views iCal 8

Create an event based on a smartdate field.

Parameters

array $events: The array of events that will be output.

\Drupal\Core\Entity\ContentEntityInterface $row: The views result being processed.

\DateTimeZone $timezone: A timezone object to use for output.

array $fieldMapping: An array of mappings to specify which entity fields to use for output.

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

File

src/Plugin/views/row/IcalFieldsWizard.php, line 458
Contains \Drupal\views_ical\Plugin\views\row\Fields.

Class

IcalFieldsWizard
The 'Ical Fields' row plugin

Namespace

Drupal\views_ical\Plugin\views\row

Code

public function addSmartDateEvent(array &$events, \Drupal\views\ResultRow $row, \DateTimeZone $timezone, array $fieldMapping) : void {
  $entity = $this
    ->getEntity($row);
  $datefieldValues = $entity
    ->get($fieldMapping['date_field'])
    ->getValue();
  $processed_rules = [];
  foreach ($datefieldValues as $delta => $datefieldValue) {
    $dateValue = $datefieldValues[$delta]['value'];
    $dateEndValue = $datefieldValues[$delta]['end_value'];
    $dateRrule = $datefieldValues[$delta]['rrule'];
    $dateTZ = $datefieldValues[$delta]['timezone'] ?: $timezone;
    if (in_array($dateRrule, $processed_rules)) {
      continue;
    }

    // Generate the event.
    $event = $this
      ->createDefaultEvent($entity, $fieldMapping, $row);

    // Set the start time.
    $startDatetime = new \DateTime();
    $startDatetime
      ->setTimestamp(trim($dateValue));
    $startDatetime
      ->setTimezone($dateTZ);
    $event
      ->setDtStart($startDatetime);

    // Set the end time.
    $endDatetime = new \DateTime();
    $endDatetime
      ->setTimestamp(trim($dateEndValue));
    $endDatetime
      ->setTimezone($dateTZ);
    $event
      ->setDtEnd($endDatetime);

    // Can the date be considered all-day?
    if (SmartDateTrait::isAllDay($startDatetime
      ->getTimestamp(), $endDatetime
      ->getTimestamp(), $dateTZ)) {
      $event
        ->setNoTime(TRUE);
    }

    // Determine recurring rules.
    if ($dateRrule) {
      $smartDateRrule = SmartDateRule::load($dateRrule);

      // Gets the rule text.
      $recurRuleObject = $smartDateRrule
        ->getAssembledRule();

      // eluceo/ical has functionality to support recurring rules, however,
      // that appears to require that we parse our already ical formatted rule
      // into components, and feed them into methods to set each one.
      // So for us it's more convenient to set it as a custom property.
      $icalRrule = new RecurrenceRule();
      if ($recurRuleObject
        ->getFreq()) {
        $freqId = $recurRuleObject
          ->getFreq();

        // Recurr/Rule stores freqs as numbers 0 - 6. We need to convert it
        // back to a string. There has to be something that does this for us...
        $freqsArray = [
          0 => 'YEARLY',
          1 => 'MONTHLY',
          2 => 'WEEKLY',
          3 => 'DAILY',
          4 => 'HOURLY',
          5 => 'MINUTELY',
          6 => 'SECONDLY',
        ];
        $freq = $freqsArray[$freqId];
        $icalRrule
          ->setFreq($freq);
      }
      if ($recurRuleObject
        ->getInterval()) {
        $icalRrule
          ->setInterval($recurRuleObject
          ->getInterval());
      }
      if ($recurRuleObject
        ->getCount()) {
        $icalRrule
          ->setCount($recurRuleObject
          ->getCount());
      }
      if ($recurRuleObject
        ->getByDay()) {
        $icalRrule
          ->setByDay(implode(',', $recurRuleObject
          ->getByDay()));
      }
      if ($recurRuleObject
        ->getUntil()) {
        $icalRrule
          ->setUntil($recurRuleObject
          ->getUntil());
      }
      $event
        ->setRecurrenceRule($icalRrule);
    }
    $events[] = $event;
  }
}