You are here

function availability_calendar_get_availability in Availability Calendars 7.5

Same name and namespace in other branches
  1. 7.3 availability_calendar.inc \availability_calendar_get_availability()
  2. 7.4 availability_calendar.inc \availability_calendar_get_availability()

Returns the availability for the given calendar and date range.

The from and to dates are inclusive.

Parameters

int $cid: cid may be 0 for not yet existing calendars.

DateTime $from:

DateTime $to:

int|null $default_state: If $default_state is null only the stored availability is returned, otherwise the returned array is completed with this default state for missing dates.

Return value

array A list of dates (yyyymmdd, within the given date range) mapped to their availability state.

5 calls to availability_calendar_get_availability()
AvailabilityCalendarICalFeedsProcessor::update1Period in ./AvailabilityCalendarICalFeedsProcessor.inc
Updates 1 period on the availability calendar.
availability_calendar_calendar_entity_property_filtered_availability_get in ./availability_calendar.entity.inc
Entity property getter callback for the 'filtered availability' property.
availability_calendar_get_unavailable_periods in ./availability_calendar.inc
Returns the blocked periods for the given calendar and date range.
availability_calendar_process_availability_calendar_months in ./availability_calendar.theme.inc
Implements hook_process_HOOK for theme availability_calendar_months @link http://api.drupal.org/api/drupal/modules--system--theme.api.php/function...
availability_calendar_update_availability in ./availability_calendar.inc
Sets the given date range to the given state for the given calendar.

File

./availability_calendar.inc, line 573

Code

function availability_calendar_get_availability($cid, DateTime $from, DateTime $to, $default_state = NULL) {

  // Get the states from the database.
  $availability = array();
  if (!empty($cid)) {
    $availability = db_select('availability_calendar_availability')
      ->fields('availability_calendar_availability', array(
      'date',
      'sid',
    ))
      ->condition('cid', $cid)
      ->condition('date', array(
      $from
        ->format(AC_ISODATE),
      $to
        ->format(AC_ISODATE),
    ), 'BETWEEN')
      ->execute()
      ->fetchAllKeyed();
  }
  if (!empty($default_state)) {
    for ($date = clone $from; $date <= $to; $date
      ->add(new DateInterval('P1D'))) {
      $day = $date
        ->format(AC_ISODATE);
      if (!array_key_exists($day, $availability)) {
        $availability[$day] = $default_state;
      }
    }
  }
  return $availability;
}