You are here

office_hours.theme.inc in Office Hours 6

Same filename and directory in other branches
  1. 6.2 office_hours.theme.inc

File

office_hours.theme.inc
View source
<?php

/**
 * Theme function for 'default' text field formatter.
 */
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,
      );

      //we're aggregating hours for days together.
    }
  }

  // add the closed days again, to 1) sort by first_day_of_week and 2) toggle show on/off
  foreach ($weekdays as $key => $day) {
    if (!array_key_exists($key, $items)) {
      $items[$key][] = array(
        'closed' => 'closed',
      );

      //silly, but we need this as an array because we can't use a string offset later
    }
  }
  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']))) {

      // don't output unnecessary fields
      $output .= theme('office_hours_display', $dayname, $hours);
    }
  }
  return $output;
}

/**
 * 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.
 */
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;
}
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'];
}

/**
 * Theme function for the office hours selector element.
 */
function theme_office_hours_select($element) {
  return theme('form_element', $element, $element['#children']);
}

/**
 * copied from content module's theme_content_multiple_values- we're taking out the draggable feature.
 *
 * Theme the hour table form
 *
 */
function theme_office_hours_multiple_values($element) {
  $field_name = $element['#field_name'];
  $field = content_fields($field_name);
  $colspan = $field['addhrs'] + 1;

  // Extra column when 'Display the "Add more hours" link'
  $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();

    // Sort items according to '_weight' (needed when the form comes back after
    // preview or failed validation)
    $items = array();
    foreach (element_children($element) as $key) {
      if ($key !== $element['#field_name'] . '_add_more') {
        $items[] =& $element[$key];
      }
    }
    $items = _office_hours_arrange_day($items);

    //this calls the function that arranges the first day of the week.

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      $delta_element = drupal_render($item['_weight']);
      if (!($key % 2)) {

        //this is an even row, start a new row array_keys
        $cells = array(
          drupal_render($item),
        );
      }
      else {

        //this is an odd row
        $cells[] = drupal_render($item);

        //we've add the second cell;
        $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>' : '';

    //  $output .= drupal_render($element[$element['#field_name'] .'_add_more']);

    //drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
  }
  else {
    foreach (element_children($element) as $key) {
      $output .= drupal_render($element[$key]);
    }
  }
  return $output;
}

/**
 * Helper function to set combined days label for hour group
 */
function _office_hours_set_group_label(&$group) {

  // reference days
  $group_days =& $group['days'];
  asort($group_days);

  // initialize label
  list($day_name, $prev_day_num) = each($group_days);
  $label_start = $day_name;
  $label_end = '';
  $label = $label_start;

  // build label
  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;
  }

  // update label if all were consecutive
  if ($label_end) {
    $label .= ' - ' . $label_end;
  }

  // update group
  $group['label'] = $label;

  // clean up and return
  unset($group_days);
  return $label;
}

Functions

Namesort descending Description
template_preprocess_office_hours_display
theme_office_hours
theme_office_hours_formatter_default Theme function for 'default' text field formatter.
theme_office_hours_formatter_grouped 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.
theme_office_hours_multiple_values copied from content module's theme_content_multiple_values- we're taking out the draggable feature.
theme_office_hours_select Theme function for the office hours selector element.
_office_hours_set_group_label Helper function to set combined days label for hour group