function rooms_availability_generate_json in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7
Generates json based on date range provided.
1 call to rooms_availability_generate_json()
- rooms_availability_event in modules/
rooms_availability/ rooms_availability.module - Displays the necessary json for the date range provided - needs at least start year and month at which point it will return the entire month.
File
- modules/
rooms_availability/ rooms_availability.module, line 525 - Manages availability for Bookable Units and displaying dates on the jquery FullCalendar plugin.
Code
function rooms_availability_generate_json($unit, $start_year = '', $start_month = '', $start_day = '', $end_year = '', $end_month = '', $end_day = '', $event_style = ROOMS_AVAILABILITY_ADMIN_STYLE) {
$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;
$today = new DateTime();
$month_start = new DateTime("{$start_year}-{$start_month}-1");
$previous_month = $today >= $month_start;
if (!user_access('view past availability information') && $previous_month) {
$start_day = date('j');
$start_month = date('n');
$start_year = date('Y');
}
$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_day = $eom[$start_month];
$end_date = new DateTime("{$start_year}-{$start_month}-{$end_day}");
}
else {
$start_date = new DateTime("{$start_year}-{$start_month}-{$start_day}");
$end_date = new DateTime("{$end_year}-{$end_month}-{$end_day}");
}
$json_events = array();
$rc = new UnitCalendar($unit->unit_id, $unit->default_state);
$events = $rc
->getEvents($start_date, $end_date);
$event_style = rooms_availability_get_style($event_style, $unit);
foreach ($events as $event) {
if (variable_get('rooms_calendar_events_view', '0') == '0') {
$event->end_date
->add(new DateInterval('P1D'));
}
$json_events[] = $event
->formatJson($event_style, $unit->name);
}
return $json_events;
}