You are here

function rooms_pricing_json in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Creates the necessary json for the date range provided.

Needs at least start year and month at which point it will return the entire month.

1 string reference to 'rooms_pricing_json'
rooms_pricing_menu in modules/rooms_pricing/rooms_pricing.module
Implements hook_menu().

File

modules/rooms_pricing/rooms_pricing.module, line 278
Manages pricing for Bookable Units and displaying dates on the jquery FullCalendar plugin.

Code

function rooms_pricing_json($unit, $start_year = '', $start_month = '', $start_day = '', $end_year = '', $end_month = '', $end_day = '') {
  $start_year = (int) $start_year;
  $start_month = (int) $start_month;
  $start_day = (int) $start_day;
  $end_year = (int) $end_year;
  $end_month = (int) $end_month;
  $end_day = (int) $end_day;
  $eom = rooms_end_of_month_dates($start_year);
  if ($start_year == 0 || $start_month == 0) {
    echo drupal_json_encode('missing basic info');
    return;
  }
  elseif ($start_day == 0) {
    $start_date = new DateTime("{$start_year}-{$start_month}-1");
    $end_day = $eom[$start_month];
    $end_date = new DateTime("{$start_year}-{$start_month}-{$end_day}");
  }
  elseif ($start_day != 0 && $end_year == 0) {
    $start_date = new DateTime("{$start_year}-{$start_month}-{$start_day}");
    $end_date = new DateTime("{$start_year}-{$start_month}-15");
    $end_date
      ->add(new DateInterval('P1M'));
    $end_year = $end_date
      ->format('Y');
    $end_month = $end_date
      ->format('n');
    $end_day = $eom[$end_date
      ->format('n')];
    $end_date = new DateTime("{$end_year}-{$end_month}-{$end_day}");
  }
  else {
    $start_date = new DateTime("{$start_year}-{$start_month}-{$start_day}");
    $end_date = new DateTime("{$end_year}-{$end_month}-{$end_day}");
  }
  $rc = new UnitPricingCalendar($unit->unit_id);
  $events = $rc
    ->getEvents($start_date, $end_date);
  $json_events = array();
  foreach ($events as $event) {
    $json_events[] = $event
      ->formatJson();
  }
  echo drupal_json_encode($json_events);
}