View source
<?php
module_load_include('inc', 'availability_calendar');
function theme_availability_calendar($variables) {
availability_calendar_add_full_calendar_js($variables);
$output = '';
$output .= "<div id=\"cal-view-{$variables['cvid']}\" class=\"cal-{$variables['cid']} cal clearfix\">\n";
if ($variables['settings']['add_name'] == '1' && !empty($variables['name'])) {
$output .= '<div class="field-label">' . $variables['name'] . ': </div>' . "\n";
}
$output .= theme('availability_calendar_months', $variables);
$output .= "</div>\n";
return $output;
}
function theme_availability_calendar_colorbox($variables) {
availability_calendar_add_full_calendar_js($variables);
$colorbox_settings = $variables['settings']['colorbox'];
$urlParams = '?inline=true';
if (!empty($colorbox_settings['width'])) {
$urlParams .= '&width=' . check_plain($colorbox_settings['width']);
}
if (!empty($colorbox_settings['height'])) {
$urlParams .= '&height=' . check_plain($colorbox_settings['height']);
}
$link_text = availability_calendar_get_customizable_text('availability_calendar_colorbox_link_text');
$link_title = availability_calendar_get_customizable_text('availability_calendar_colorbox_link_title');
if ($link_title != '') {
$link_title = " title='{$link_title}'";
}
$output = '';
$output .= "<div><a class='colorbox-inline' href='{$urlParams}#cal-view-{$variables['cvid']}'{$link_title}>{$link_text}</a></div>\n";
$output .= "<div style='display: none;'>";
$output .= theme('availability_calendar', $variables);
$output .= "</div>\n";
return $output;
}
function theme_availability_calendar_viewport($variables) {
availability_calendar_add_viewport_js($variables);
$output = '';
$viewport_settings = $variables['settings']['viewport'];
$placement = $viewport_settings['button_placement'];
if ($placement !== 'not') {
$responsive = $viewport_settings['dimensions_calculation'] === 'responsive_block' || $viewport_settings['dimensions_calculation'] === 'responsive_inline';
$scroll = (int) ($viewport_settings['rows'] > 1 ? $viewport_settings['scroll'] * $viewport_settings['cols'] : $viewport_settings['scroll']);
$button_back = theme('availability_calendar_viewport_button', array(
'direction' => 'back',
'scroll' => $scroll,
'responsive' => $responsive,
));
$button_forward = theme('availability_calendar_viewport_button', array(
'direction' => 'forward',
'scroll' => $scroll,
'responsive' => $responsive,
));
}
else {
$button_back = '';
$button_forward = '';
}
$output .= "<div id=\"cal-view-{$variables['cvid']}\" class=\"cal-{$variables['cid']} cal clearfix\">\n";
if ($variables['settings']['add_name'] == '1' && !empty($variables['name'])) {
$output .= '<div class="field-label">' . $variables['name'] . ': </div>' . "\n";
}
if ($placement === 'before') {
$output .= "<div class=\"cal-buttons\">{$button_back} {$button_forward}</div>";
}
else {
if ($placement === 'before_after') {
$output .= "<div class=\"cal-buttons cal-buttons-before\">{$button_back}</div>";
}
}
$output .= '<div class="cal-viewport"><div class="cal-viewport-inner">' . "\n";
$output .= theme('availability_calendar_months', $variables);
$output .= "</div></div>\n";
if ($placement === 'before_after') {
$output .= "<div class=\"cal-buttons cal-buttons-after\">{$button_forward}</div>";
}
else {
if ($placement === 'after') {
$output .= "<div class=\"cal-buttons\">{$button_back}{$button_forward}</div>";
}
}
$output .= "</div>\n";
return $output;
}
function theme_availability_calendar_viewport_button($variables) {
$direction = $variables['direction'] === 'forward' ? 'forward' : 'backward';
$months = (int) $variables['scroll'];
$responsive = $variables['responsive'];
$key_part1 = $direction === 'backward' ? 'prev' : 'next';
$key_part2 = $months === 1 ? '1' : ($responsive ? 'unknown' : 'n');
$key_start = "availability_calendar_viewport_button_{$key_part1}_{$key_part2}_";
$button_text = availability_calendar_get_customizable_formatted_text($key_start . 'text', array(
'@count' => $months,
));
$button_title = availability_calendar_get_customizable_title($key_start . 'title');
$output = "<button type=\"button\" class=\"cal-{$direction}\"{$button_title}>{$button_text}</button>";
return $output;
}
function availability_calendar_process_availability_calendar_months(&$variables) {
if (empty($variables['year'])) {
$variables['year'] = (int) date('Y');
}
if (empty($variables['month'])) {
$variables['month'] = (int) date('n');
}
if (empty($variables['availability'])) {
$months_to_render = $variables['settings']['show_number_of_months'];
$from = new DateTime();
$from
->setDate($variables['year'], $variables['month'], 1);
$to = clone $from;
$to
->add(new DateInterval("P{$months_to_render}M"));
$to
->sub(new DateInterval('P1D'));
if ($variables['settings']['allocation_type'] === AC_ALLOCATION_TYPE_OVERNIGHT && $variables['settings']['show_split_day']) {
$from
->sub(new DateInterval('P1D'));
}
$variables['availability'] = availability_calendar_get_availability((int) $variables['cid'], $from, $to, $variables['settings']['default_state']);
}
}
function theme_availability_calendar_months($variables) {
$year =& $variables['year'];
$month =& $variables['month'];
$months_to_render = $variables['settings']['show_number_of_months'];
$output = '';
for ($i = 0; $i < $months_to_render; $i++) {
$output .= theme('availability_calendar_month', $variables);
$month++;
if ($month > 12) {
$month = 1;
$year++;
}
}
return $output;
}
function theme_availability_calendar_month($variables) {
$year = $variables['year'];
$month = $variables['month'];
$settings = $variables['settings'];
$availability = $variables['availability'];
$caption = format_date(mktime(12, 0, 0, $month, 1, $year), 'availability_calendar_month_caption');
$header = availability_calendar_month_header($settings);
$cells = availability_calendar_month_cells($year, $month, $availability, $settings);
$rows = array_chunk($cells, 7, TRUE);
while (count($rows) < 6) {
$rows[] = array(
array(
'data' => '<div></div>',
'class' => array(
'cal-empty',
),
'colspan' => 7,
),
);
}
if ($settings['show_week_number']) {
array_unshift($header, array(
'data' => t('Nr.'),
'class' => array(
'cal-weekno-header',
),
));
foreach ($rows as &$row) {
if (count($row) > 1) {
if ($settings['first_day_of_week'] < 5) {
reset($row);
}
else {
end($row);
}
$day_to_use = new DateTime(key($row));
array_unshift($row, array(
'data' => $day_to_use
->format('W'),
'header' => TRUE,
));
}
else {
array_unshift($row, array(
'data' => ' ',
'header' => TRUE,
'class' => array(
'cal-empty',
),
));
}
}
}
foreach ($rows as &$row) {
$row = array(
'no_striping' => TRUE,
'data' => $row,
);
}
$month2 = sprintf('%02d', $month);
$output = "<div class=\"cal-month\" data-cal-id=\"{$variables['cid']}\" data-cal-year=\"{$year}\" data-cal-month=\"{$month2}\">\n";
$output .= theme('table', array(
'caption' => $caption,
'header' => $header,
'rows' => $rows,
'sticky' => FALSE,
));
$output .= "\n</div>\n";
return $output;
}
function availability_calendar_month_header($settings) {
$day_names_abbr = array(
t('Sun'),
t('Mon'),
t('Tue'),
t('Wed'),
t('Thu'),
t('Fri'),
t('Sat'),
);
$headers = array();
for ($i = 0; $i < 7; $i++) {
$header = $day_names_abbr[($settings['first_day_of_week'] + $i) % 7];
if ($settings['show_only_first_letter']) {
$header = drupal_substr($header, 0, 1);
}
$headers[] = $header;
}
return $headers;
}
function availability_calendar_month_cells($year, $month, $availability, $settings) {
$day = new DateTime();
$day
->setDate($year, $month, 1);
$days_in_month = (int) $day
->format('t');
$days_to_prepend = (int) $day
->format('N') - $settings['first_day_of_week'];
if ($days_to_prepend < 0) {
$days_to_prepend += 7;
}
$days_to_append = ($days_to_prepend + $days_in_month) % 7;
if ($days_to_append > 0) {
$days_to_append = 7 - $days_to_append;
}
$showSplitDays = $settings['allocation_type'] === AC_ALLOCATION_TYPE_OVERNIGHT && $settings['show_split_day'];
$states = availability_calendar_get_states();
$total_days = $days_to_prepend + $days_in_month + $days_to_append;
$today_iso = date(AC_ISODATE);
$day
->modify('-' . ($days_to_prepend + 1) . ' days');
$previous_day_iso = $day
->format(AC_ISODATE);
$day
->modify('+1 day');
$cells = array();
for ($i = 0; $i < $total_days; $i++) {
$day_iso = $day
->format(AC_ISODATE);
$classes = array();
if (empty($availability[$day_iso]) || (int) $day
->format('n') != $month) {
$classes[] = 'cal-other';
}
else {
if ($day_iso < $today_iso) {
$classes[] = 'cal-pastdate';
}
else {
if ($day_iso == $today_iso) {
$classes[] = 'cal-today';
if ($showSplitDays) {
$classes[] = 'cal-pastdate-am';
}
}
else {
if ($showSplitDays && isset($availability[$previous_day_iso])) {
$classes[] = $states[$availability[$previous_day_iso]]['css_class'] . '-am';
}
}
$state = $states[$availability[$day_iso]]['css_class'];
if ($showSplitDays) {
$state .= '-pm';
}
$classes[] = $state;
}
}
$cell_contents = availability_calendar_theme_day((int) $day
->format('j'), $settings);
$cells[$day_iso] = array(
'data' => $cell_contents,
'class' => $classes,
);
$day
->modify('+1 day');
$previous_day_iso = $day_iso;
}
return $cells;
}
function availability_calendar_theme_day($day, $settings) {
if ($settings['allocation_type'] === AC_ALLOCATION_TYPE_OVERNIGHT && $settings['show_split_day']) {
$result = "<span><span>{$day}</span></span>";
}
else {
$result = "<div>{$day}</div>";
}
return $result;
}
function theme_availability_calendar_key($variables) {
$caption = '';
$rows = array();
$states_to_show = isset($variables['states_to_show']) ? $variables['states_to_show'] : array();
$states = availability_calendar_get_states(array_filter($states_to_show));
foreach ($states as $state) {
$rows[] = array(
array(
'data' => '<div>' . check_plain(t($state['label'])) . '</div>',
'class' => $state['css_class'],
'no_striping' => TRUE,
),
);
}
$key = theme('table', array(
'caption' => $caption,
'rows' => $rows,
'sticky' => FALSE,
));
return '<div class="cal-key">' . $key . '</div>';
}
function availability_calendar_add_full_calendar_js($variables) {
if (!isset($variables['settings']['selectable']) || $variables['settings']['selectable']) {
availability_calendar_add_calendar_view_js($variables['cvid'], $variables['cid'], $variables['name'], $variables['settings']);
}
}
function availability_calendar_add_viewport_js($variables) {
$cvid = $variables['cvid'];
availability_calendar_add_calendar_view_js($cvid, $variables['cid'], $variables['name'], $variables['settings']);
drupal_add_js(drupal_get_path('module', 'availability_calendar') . '/availability_calendar.viewport.js');
drupal_add_js(array(
'availabilityCalendar' => array(
'viewports' => array(
$cvid => array(
'cvid' => $cvid,
'dimensionsCalculation' => $variables['settings']['viewport']['dimensions_calculation'],
'cols' => (int) $variables['settings']['viewport']['cols'],
'rows' => (int) $variables['settings']['viewport']['rows'],
'scroll' => (int) $variables['settings']['viewport']['scroll'],
),
),
),
), array(
'type' => 'setting',
));
}