View source
<?php
module_load_include('inc', 'availability_calendar', 'availability_calendar');
function theme_availability_calendar($variables) {
availability_calendar_add_full_calendar_js($variables['cid'], $variables['name'], $variables['settings'], $variables['mode']);
$output = '<div id="cal-' . $variables['cid'] . '" class="cal clearfix">' . "\n";
if (!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_viewport($variables) {
availability_calendar_add_viewport_js($variables['cid'], $variables['name'], $variables['settings'], $variables['mode']);
$viewport_settings = $variables['settings']['viewport'];
$placement = $viewport_settings['button_placement'];
if ($placement !== 'not') {
$scroll = $viewport_settings['scroll'] * ($viewport_settings['rows'] > 1 ? $viewport_settings['cols'] : 1);
$button_back = theme('availability_calendar_viewport_button', array(
'direction' => 'back',
'scroll' => $scroll,
));
$button_forward = theme('availability_calendar_viewport_button', array(
'direction' => 'forward',
'scroll' => $scroll,
));
}
$output = '<div id="cal-' . $variables['cid'] . '" class="cal clearfix">' . "\n";
if (!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'];
$button_text = $direction === 'forward' ? format_plural($months, 'Next month', 'Next @count months') : format_plural($months, 'Previous month', 'Previous @count months');
$output = "<a class=\"cal-{$direction}\">{$button_text}</a>";
return $output;
}
function availability_calendar_preprocess_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
->modify("+{$months_to_render} months");
$to
->modify('-1 day');
if ($variables['settings']['allocation_type'] === ALLOCATION_TYPE_OVERNIGHT && $variables['settings']['show_split_day']) {
$from
->modify('-1 day');
}
$cid = (int) $variables['cid'];
$variables['availability'] = availability_calendar_get_availability($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,
));
}
}
}
foreach ($rows as &$row) {
$row = array(
'no_striping' => TRUE,
'data' => $row,
);
}
$output = "<div id=\"cal-{$variables['cid']}-{$year}-{$month}\" class=\"cal-month\">\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 = 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 = $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'] === 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'] === 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($cid, $name, $settings, $mode) {
if ($mode !== 'none') {
availability_calendar_add_calendar_js($cid, $name, $settings['allocation_type'], $settings['show_split_day'], $mode);
}
}
function availability_calendar_add_viewport_js($cid, $name, $settings, $mode) {
static $viewport_count = 0;
$viewport_count++;
availability_calendar_add_calendar_js($cid, $name, $settings['allocation_type'], $settings['show_split_day'], $mode);
drupal_add_js(drupal_get_path('module', 'availability_calendar') . '/availability_calendar.viewport.js');
drupal_add_js(array(
'availabilityCalendar' => array(
"viewport{$viewport_count}" => array(
'cols' => (int) $settings['viewport']['cols'],
'rows' => (int) $settings['viewport']['rows'],
'scroll' => (int) $settings['viewport']['scroll'],
),
),
), array(
'type' => 'setting',
));
$cid_quoted = $cid == (string) (int) $cid ? $cid : "'{$cid}'";
drupal_add_js("Drupal.behaviors.availabilityCalendarViewport{$viewport_count} = {\n attach: function(context, settings) {\n Drupal.availabilityCalendar.getViewport({$cid_quoted}, 'viewport{$viewport_count}');\n }\n};", array(
'type' => 'inline',
'scope' => 'footer',
));
}