You are here

function availability_calendar_get_unavailable_periods in Availability Calendars 7.5

Returns the blocked periods for the given calendar and date range.

The from and to dates are inclusive.

Parameters

int $cid:

DateTime $from:

DateTime $to:

int $default_state:

Return value

string[][] A list of periods defined by their begin and end dates (yyyymmdd), within the given date range that define the non-availability of the calendar.

Throws

\Exception but not really: remove if phpstorm does no longer warn.

1 call to availability_calendar_get_unavailable_periods()
availability_calendar_process_availability_calendar_ical in ./availability_calendar.ical.inc
Implements hook_process_HOOK for theme availability_calendar_ical.

File

./availability_calendar.inc, line 733

Code

function availability_calendar_get_unavailable_periods($cid, DateTime $from, DateTime $to, $default_state) {
  $availability = availability_calendar_get_availability($cid, $from, $to, $default_state);
  $non_available_states = availability_calendar_get_states(FALSE);
  $result = array();
  $start = NULL;
  for ($date = clone $from; $date <= $to; $date
    ->add(new DateInterval('P1D'))) {
    $day = $date
      ->format(AC_ISODATE);
    $sid = $availability[$day];
    if (array_key_exists($sid, $non_available_states)) {

      // This date is not available, start a new period of not available if not
      // already started.
      if ($start === NULL) {
        $start = clone $date;
      }
    }
    else {

      // This date is available, end period of not available if one was active.
      if ($start !== NULL) {
        $notAvailablePeriod = new stdClass();
        $notAvailablePeriod->start = $start;
        $notAvailablePeriod->end = clone $date;
        $result[] = $notAvailablePeriod;
        $start = NULL;
      }
    }
  }

  // End the last not available period, if we were in one.
  if ($start !== NULL) {
    $notAvailablePeriod = new stdClass();
    $notAvailablePeriod->start = $start;

    /** @noinspection PhpUndefinedVariableInspection $date will be set if $start is set. */
    $notAvailablePeriod->end = clone $date;
    $result[] = $notAvailablePeriod;
  }
  return $result;
}