function theme_office_hours_formatter_grouped in Office Hours 6
Theme function for 'grouped' text field formatter. johnv 4/4/2012: this function is a partial backport from the D7-formatter options the code is copied, but the options are fixed.
File
- ./
office_hours.theme.inc, line 52
Code
function theme_office_hours_formatter_grouped($element) {
$first = variable_get('date_first_day', 0);
$field = content_fields($element['#field_name'], $element['#type_name']);
// In D7, the following parameters are set in the formatter options.
$settings['grouped'] = TRUE;
$settings['daysformat'] = 'short';
$settings['separator_grouped_days'] = isset($settings['separator_grouped_days']) ? $settings['separator_grouped_days'] : ' - ';
// Finally, generate the formatter output.
$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;
// This is the minimum width for day labels. It is adjusted when adding new labels.
// $show_closed = !empty($settings['showclosed']); // D7-version
$show_closed = $field['showclosed'];
// D6-version
// get ordered weekdays
// @todo should abbr be an option?
$weekdays = date_week_days_abbr(TRUE);
$items = array();
// create hours groups from items
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,
);
// We're aggregating hours for days together.
$items[$day]['label'] = $weekdays[$day];
// aggregate by hours
$group_id = "{$strhrs}::{$endhrs}";
if (!isset($items[$day]['group_id'])) {
$items[$day]['group_id'] = $group_id;
}
else {
// more hours info.
$items[$day]['group_id'] .= '-' . $group_id;
}
if (!isset($items[$day])) {
$items[$day]['hours'] = array(
array(
'strhrs' => $strhrs,
'endhrs' => $endhrs,
),
);
}
}
}
}
// add the closed days again, to 1) sort by first_day_of_week and 2) toggle show on/off
foreach ($weekdays as $day => $label) {
if (!array_key_exists($day, $items)) {
$items[$day][] = array(
'closed' => 'Closed',
);
//silly, but we need this as an array because we can't use a string offset later
$items[$day]['label'] = $weekdays[$day];
$max_label_length = max($max_label_length, strlen($items[$day]['label']));
// Add group_id for closed days
if ($settings['grouped']) {
$group_id = 'closed';
$items[$day]['group_id'] = $group_id;
}
}
}
// Sort the days of the week again, because sorting is lost by adding the closed days.
ksort($items);
// Sort by first_day_of_week, except for certain types: 'number'/'none' are more frequently used by machines/interfaces.
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;
}
// Change label for grouped days.
if ($settings['grouped']) {
$group_id = '';
foreach ($items as $day => $hours) {
if ($group_id != $hours['group_id']) {
// new group, save label of first day.
$group_id = $hours['group_id'];
$group_from_label = $hours['label'];
}
else {
// second, third day of group: adjust the label of this group to e.g. 'mon-wed'
$items[$day]['label'] = $group_from_label . $settings['separator_grouped_days'] . $hours['label'];
$max_label_length = max($max_label_length, strlen($items[$day]['label']));
// delete previous item of this group, so it will not be shown.
unset($items[$day - 1]);
}
}
}
// Finally, generate the formatter output.
$output = '';
$values = array();
foreach ($items as $day => $hours) {
$closed = '';
$regular = '';
$additional = '';
if (isset($hours[0]['closed'])) {
if ($show_closed) {
// Format the empty day with a text like 'Closed' or '00:00-00:00'
// $closed = check_plain( t($settings['closedformat'] ) ); // D7-version
$closed = $hours[0]['closed'];
// D6-version
}
else {
// Don't output unnecessary fields.
}
}
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 {
// Override endhours of morning with endhours of evening.
$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;
}