You are here

function availability_calendar_is_available in Availability Calendars 7.3

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

Checks whether a calendar is available for the given period.

Parameters

int $cid:

DateTime $from:

int|DateTime $to_or_duration:

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

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).

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 625
General helper methods for Availability Calendar

Code

function availability_calendar_is_available($cid, $from, $to_or_duration, $default_state) {
  if ($to_or_duration instanceof DateTime) {
    $to = $to_or_duration;

    //PHP5.3: $duration = $arrival->diff($to)->days + 1;
    $timestamp_from = (int) $from
      ->format('U');
    $timestamp_to = (int) $to
      ->format('U');
    $diff = (int) round(($timestamp_to - $timestamp_from) / (60 * 60 * 24));
    $duration = $diff + 1;
  }
  else {
    $duration = (int) $to_or_duration;
    $diff = $duration - 1;
    $to = clone $from;
    $to
      ->modify("+{$diff} days");
  }

  // Check parameters.
  if ($duration <= 0) {
    return null;
  }

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

    // I have to make a choice :( (I could try to find out if there is only 1
    // calendar field or if the default is always the same or if the defaults
    // are always to be treated as either available or not available.)
    $default_is_available = FALSE;
  }

  // If the default status = available then no single day may be marked as non
  // available. Check by counting the non available days in the given period.
  // If the default status = non available then all days must be marked as
  // available. Check by counting the available days in the given period.
  //
  // Get the sids with opposite "treat as available".
  $sids = array_keys(availability_calendar_get_states(!$default_is_available));

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

  // Check the count (as explained above).
  return $default_is_available ? $count == 0 : $count == $duration;
}