You are here

function availability_calendar_booking_formlet_extract_date in Availability Calendars 7.5

Same name and namespace in other branches
  1. 7.3 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_extract_date()
  2. 7.4 booking_formlet/availability_calendar_booking_formlet.module \availability_calendar_booking_formlet_extract_date()

Returns a date based on the settings and global context.

Parameters

array $settings:

string $date: Either 'preset_begin_date' or 'preset_end_date'

null|DateTime $offset: null or a date to use as offset if the source indicates a duration rather than a date on its own.

Return value

null|DateTime

1 call to availability_calendar_booking_formlet_extract_date()
availability_calendar_booking_formlet_field_formatter_view_inc in booking_formlet/availability_calendar_booking_formlet.inc
Implements hook_field_formatter_view(). @link http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...

File

booking_formlet/availability_calendar_booking_formlet.inc, line 659
General helper methods for Availability Calendar Booking formlet to make the .module file smaller:

Code

function availability_calendar_booking_formlet_extract_date($settings, $date, $offset = NULL) {
  $source = $settings["{$date}_source"];
  $global = substr($source, 0, strlen('get')) === 'get' ? $_GET : $_POST;
  $key = $settings["{$date}_key"];
  $result = NULL;
  switch ($source) {
    case 'today':
      $result = new DateTime();
      break;
    case 'fixed_duration':
      if ($offset instanceof DateTime) {
        $result = clone $offset;
        $duration = (int) $key;

        // Adjust duration as to date is inclusive.
        $duration--;
        $result
          ->modify("+{$duration} days");
      }
      break;
    case 'get':
    case 'post':
    case 'get1':
    case 'post1':
      $date = availability_calendar_booking_formlet_get_nested_array_value($global, $key);
      if (is_string($date)) {
        $date = availability_calendar_parse_entry_date($date);
        if ($date instanceof DateTime) {
          $result = $date;

          // get1 and post1 are not inclusive: modify by 1 day.
          if (substr($source, -strlen('1')) === '1') {
            $result
              ->modify('-1 day');
          }
        }
      }
      break;
    case 'get_duration':
    case 'post_duration':
      if ($offset instanceof DateTime) {
        $duration = availability_calendar_booking_formlet_get_nested_array_value($global, $key);
        if (is_int($duration) || ctype_digit($duration)) {
          $result = clone $offset;
          $duration = (int) $duration;

          // Adjust duration as to date is inclusive.
          $duration--;
          $result
            ->modify("+{$duration} days");
        }
      }
      break;
    case 'none':
    default:
      break;
  }

  // Set time to noon to avoid timezone conversion rounding errors later.
  if ($result instanceof DateTime) {
    $result
      ->setTime(12, 0, 0);
  }
  return $result;
}