View source
<?php
function theme_office_hours_formatter_default($element) {
$output = '';
$items = array();
$first = variable_get('date_first_day', 0);
$field = content_fields($element['#field_name'], $element['#type_name']);
$weekdays = date_week_days(TRUE);
foreach (element_children($element) as $key => $arraykey) {
$item = $element[$arraykey]['#item'];
$day = $item['day'];
if (isset($day)) {
$strhrs = _office_hours_mil_to_tf(check_plain($item['starthours']));
$endhrs = _office_hours_mil_to_tf(check_plain($item['endhours']));
if ($field['hoursformat']) {
$strhrs = _office_hours_convert_to_ampm($strhrs);
$endhrs = _office_hours_convert_to_ampm($endhrs);
}
$items[$day][] = array(
'strhrs' => $strhrs,
'endhrs' => $endhrs,
);
}
}
foreach ($weekdays as $key => $day) {
if (!array_key_exists($key, $items)) {
$items[$key][] = array(
'closed' => 'closed',
);
}
}
ksort($items);
$items = date_week_days_ordered($items);
$weekdays = date_week_days_ordered($weekdays);
foreach ($items as $day => $hours) {
$dayname = $weekdays[$day];
if (!(!empty($hours[0]['closed']) && empty($field['showclosed']))) {
$output .= theme('office_hours_display', $dayname, $hours);
}
}
return $output;
}
function theme_office_hours_formatter_grouped($element) {
$first = variable_get('date_first_day', 0);
$field = content_fields($element['#field_name'], $element['#type_name']);
$settings['grouped'] = TRUE;
$settings['daysformat'] = 'short';
$settings['separator_grouped_days'] = isset($settings['separator_grouped_days']) ? $settings['separator_grouped_days'] : ' - ';
$settings['separator_more_hours'] = isset($settings['separator_more_hours']) ? $settings['separator_more_hours'] : ', ';
$settings['separator_hours_hours'] = isset($settings['separator_hours_hours']) ? $settings['separator_hours_hours'] : ' - ';
$settings['separator_day_hours'] = isset($settings['separator_day_hours']) ? $settings['separator_day_hours'] : ': ';
$settings['separator_days'] = isset($settings['separator_days']) ? $settings['separator_days'] : '<br>';
$max_label_length = 3;
$show_closed = $field['showclosed'];
$weekdays = date_week_days_abbr(TRUE);
$items = array();
foreach (element_children($element) as $key => $arraykey) {
$item = $element[$arraykey]['#item'];
$day = $element[$arraykey]['#item']['day'];
if (isset($day)) {
$day_num = check_plain($day);
if (is_numeric($day_num)) {
$day_num = (int) $day_num;
$day_name = $weekdays[$day_num];
$endhrs = _office_hours_mil_to_tf(check_plain($item['endhours']));
$strhrs = _office_hours_mil_to_tf(check_plain($item['starthours']));
if ($field['hoursformat']) {
$endhrs = _office_hours_convert_to_ampm($endhrs);
$strhrs = _office_hours_convert_to_ampm($strhrs);
}
$items[$day][] = array(
'strhrs' => $strhrs,
'endhrs' => $endhrs,
);
$items[$day]['label'] = $weekdays[$day];
$group_id = "{$strhrs}::{$endhrs}";
if (!isset($items[$day]['group_id'])) {
$items[$day]['group_id'] = $group_id;
}
else {
$items[$day]['group_id'] .= '-' . $group_id;
}
if (!isset($items[$day])) {
$items[$day]['hours'] = array(
array(
'strhrs' => $strhrs,
'endhrs' => $endhrs,
),
);
}
}
}
}
foreach ($weekdays as $day => $label) {
if (!array_key_exists($day, $items)) {
$items[$day][] = array(
'closed' => 'Closed',
);
$items[$day]['label'] = $weekdays[$day];
$max_label_length = max($max_label_length, strlen($items[$day]['label']));
if ($settings['grouped']) {
$group_id = 'closed';
$items[$day]['group_id'] = $group_id;
}
}
}
ksort($items);
switch ($settings['daysformat']) {
case 'long':
case 'short':
$items = date_week_days_ordered($items);
$weekdays = date_week_days_ordered($weekdays);
break;
case 'number':
case 'none':
break;
}
if ($settings['grouped']) {
$group_id = '';
foreach ($items as $day => $hours) {
if ($group_id != $hours['group_id']) {
$group_id = $hours['group_id'];
$group_from_label = $hours['label'];
}
else {
$items[$day]['label'] = $group_from_label . $settings['separator_grouped_days'] . $hours['label'];
$max_label_length = max($max_label_length, strlen($items[$day]['label']));
unset($items[$day - 1]);
}
}
}
$output = '';
$values = array();
foreach ($items as $day => $hours) {
$closed = '';
$regular = '';
$additional = '';
if (isset($hours[0]['closed'])) {
if ($show_closed) {
$closed = $hours[0]['closed'];
}
else {
}
}
else {
$strhrs1 = $hours[0]['strhrs'];
$endhrs1 = $hours[0]['endhrs'];
if (isset($hours[1])) {
if (!$settings['compress']) {
$strhrs2 = $hours[1]['strhrs'];
$endhrs2 = $hours[1]['endhrs'];
$additional = $settings['separator_more_hours'] . $strhrs2 . $settings['separator_hours_hours'] . $endhrs2;
}
else {
$endhrs1 = $hours[1]['endhrs'];
}
}
$regular = $strhrs1 . $settings['separator_hours_hours'] . $endhrs1;
}
$output_hours = $closed . $regular . $additional;
if (!empty($output_hours)) {
$output .= '<span class="oh-display">' . '<span class="oh-display-label">' . $hours['label'] . $settings['separator_day_hours'] . '</span>';
if ($closed) {
$output .= '<span class="oh-display-closed">' . $closed . $regular . $additional . $settings['separator_days'] . '</span>';
}
else {
$output .= '<span class="oh-display-hours">' . $closed . $regular . $additional . $settings['separator_days'] . '</span>';
}
$output .= '</span>';
}
}
return $output;
}
function template_preprocess_office_hours_display(&$vars) {
$vars['starthours1'] = isset($vars['hours'][0]['strhrs']) ? $vars['hours'][0]['strhrs'] : NULL;
$vars['endhours1'] = isset($vars['hours'][0]['endhrs']) ? $vars['hours'][0]['endhrs'] : NULL;
$vars['starthours2'] = isset($vars['hours'][1]['strhrs']) ? $vars['hours'][1]['strhrs'] : NULL;
$vars['endhours2'] = isset($vars['hours'][1]['endhrs']) ? $vars['hours'][1]['endhrs'] : NULL;
$vars['additional'] = isset($vars['hours'][1]) ? TRUE : FALSE;
$vars['closed'] = isset($vars['hours'][0]['closed']) ? $vars['hours'][0]['closed'] : NULL;
}
function theme_office_hours($element) {
return $element['#children'];
}
function theme_office_hours_select($element) {
return theme('form_element', $element, $element['#children']);
}
function theme_office_hours_multiple_values($element) {
$field_name = $element['#field_name'];
$field = content_fields($field_name);
$colspan = $field['addhrs'] + 1;
$output = '';
if ($field['multiple'] >= 1) {
$table_id = $element['#field_name'] . '_values';
$order_class = $element['#field_name'] . '-delta-order';
$required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : '';
$header = array(
array(
'data' => t('!title: !required', array(
'!title' => $element['#title'],
'!required' => $required,
)),
'colspan' => $colspan,
),
);
$rows = array();
$items = array();
foreach (element_children($element) as $key) {
if ($key !== $element['#field_name'] . '_add_more') {
$items[] =& $element[$key];
}
}
$items = _office_hours_arrange_day($items);
foreach ($items as $key => $item) {
$delta_element = drupal_render($item['_weight']);
if (!($key % 2)) {
$cells = array(
drupal_render($item),
);
}
else {
$cells[] = drupal_render($item);
$rows[] = array(
'data' => $cells,
);
}
}
$output .= theme('table', $header, $rows, array(
'id' => $table_id,
'class' => 'office_hours_table content-multiple-table',
));
$output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
}
else {
foreach (element_children($element) as $key) {
$output .= drupal_render($element[$key]);
}
}
return $output;
}
function _office_hours_set_group_label(&$group) {
$group_days =& $group['days'];
asort($group_days);
list($day_name, $prev_day_num) = each($group_days);
$label_start = $day_name;
$label_end = '';
$label = $label_start;
while (list($day_name, $day_num) = each($group_days)) {
$diff = $day_num - $prev_day_num;
if ($diff == 1) {
$label_end = $day_name;
}
else {
if ($label_end) {
$label .= ' - ' . $label_end;
}
$label_start = $day_name;
$label_end = '';
$label .= ', ' . $label_start;
}
$prev_day_num = $day_num;
}
if ($label_end) {
$label .= ' - ' . $label_end;
}
$group['label'] = $label;
unset($group_days);
return $label;
}