You are here

function availability_calendar_is_available in Availability Calendars 7.4

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

Checks whether a calendar is available for the given period.

Parameters

int $cid:

DateTime $from:

DateTime|int $to_or_duration:

int $default_state: The sid of the state to use for dates without availability assigned.

$minimum_days int|bool: The minimum number of days the calendar should be available in the given period to be considered available. False or absent means the whole period, True means 1 day.

Return value

bool|null true or false to indicate whether the calendar is available in the given period, null if the period is not valid (negative duration) or if $minimum_days can never be satisfied (period to short).

1 call to availability_calendar_is_available()
availability_calendar_booking_formlet_field_formatter_view in booking_formlet/availability_calendar_booking_formlet.module
Implements hook_field_formatter_view(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
1 string reference to 'availability_calendar_is_available'
availability_calendar_booking_formlet_field_formatter_view in booking_formlet/availability_calendar_booking_formlet.module
Implements hook_field_formatter_view(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

File

./availability_calendar.inc, line 746

Code

function availability_calendar_is_available($cid, $from, $to_or_duration, $default_state, $minimum_days = FALSE) {

  // Process parameters.
  $info = availability_calendar_get_period_information($from, $to_or_duration);
  if (!$info) {
    return NULL;
  }

  // Process $minimum_days.
  if ($minimum_days === FALSE) {
    $minimum_days = $info['duration'];
  }
  else {
    $minimum_days = (int) $minimum_days;
  }
  if ($minimum_days <= 0 || $minimum_days > $info['duration']) {
    return NULL;
  }

  // Determine whether default availability state is to be treated as available.
  $states = availability_calendar_get_states();
  $default_is_available = $states[$default_state]['is_available'] == 1;

  // If the default status = available then at most duration - minimum_days may
  // be marked as non available. Check by counting the non available days in the
  // given period.
  // If the default status = non available then at least minimun_days must be
  // marked as available. Check by counting the available days.
  //
  // Get the sids with "treat as available" opposite to that of the default.
  $sids = array_keys(availability_calendar_get_states(!$default_is_available));

  // Create and execute the count query and fetch the (scalar) result.
  $count = (int) db_select('availability_calendar_availability')
    ->condition('cid', $cid)
    ->condition('date', array(
    $info['from']
      ->format(AC_ISODATE),
    $info['to']
      ->format(AC_ISODATE),
  ), 'BETWEEN')
    ->condition('sid', $sids, 'IN')
    ->countQuery()
    ->execute()
    ->fetchField();

  // Check the count (as explained above).
  return $default_is_available ? $count <= $info['duration'] - $minimum_days : $count >= $minimum_days;
}