You are here

function theme_office_hours_field_formatter_default in Office Hours 7

Theme function for field formatter.

1 theme call to theme_office_hours_field_formatter_default()
office_hours_field_formatter_view in includes/office_hours.formatter.inc
Implements hook_field_formatter_view().

File

includes/office_hours.theme.inc, line 10
Themes the Office hours formatter and widget.

Code

function theme_office_hours_field_formatter_default($vars) {
  $days2show = $vars['days'];
  $days2calc = $vars['days2calc'];
  $daynames = $vars['daynames'];
  $settings = $vars['settings'];
  switch ($settings['hoursformat']) {
    case 2:

      // 24hr with leading zero.
      $timeformat = 'H:i';
      break;
    case 0:

      // 24hr without leading zero.
      $timeformat = 'G:i';
      break;
    case 1:

      // 12hr ampm without leading zero.
      $timeformat = 'g:i a';
      break;
    case 3:

      // 12hr format as a.m. - p.m. without leading zero.
      $timeformat = 'g:i a';
      break;
  }

  // Minimum width for day labels. Adjusted when adding new labels.
  $max_label_length = 3;
  $html_hours = '';
  $html_current_status = '';
  foreach ($days2show as $day => &$info) {

    // Format the label.
    $label = $daynames[$info['startday']];
    $label .= !isset($info['endday']) ? '' : $settings['separator_grouped_days'] . $daynames[$info['endday']];
    $label .= $settings['separator_day_hours'];
    $max_label_length = max($max_label_length, drupal_strlen($label));

    // Format the time.
    if (!$info['times']) {

      // Closed day.
      $times = t($settings['closedformat']);
      $comment = NULL;
    }
    else {
      $times = array();
      foreach ($info['times'] as $block_times) {
        $times[] = theme('office_hours_time_range', array(
          'times' => $block_times,
          'format' => $timeformat,
          'separator' => $settings['separator_hours_hours'],
          'comment' => $block_times['comment'],
        ));
      }
      $times = implode($settings['separator_more_hours'], $times);
      if ($settings['hoursformat'] == 3) {
        $times = str_replace(array(
          'am',
          'pm',
        ), array(
          'a.m.',
          'p.m.',
        ), $times);
      }
    }
    $info['output_label'] = $label;
    $info['output_times'] = $times;
  }

  // Start the loop again - only now we have the correct $max_label_length.
  foreach ($days2show as $day => &$info) {

    // Remove unwanted lines.
    switch ($settings['showclosed']) {
      case 'all':
        break;
      case 'open':
        if (!isset($info['times'])) {
          continue 2;
        }
        break;
      case 'next':
        if (!$info['current'] && !$info['next']) {
          continue 2;
        }
        break;
      case 'none':
        continue 2;
        break;
    }

    // Generate HTML for Hours.
    $html_hours .= '<span class="oh-display">' . '<span class="oh-display-label" style="width: ' . $max_label_length * 0.6 . 'em;">' . filter_xss_admin($info['output_label']) . '</span>' . '<div span class="oh-display-times oh-display-' . (!$info['times'] ? 'closed' : 'hours') . ($info['current'] ? ' oh-display-current' : '') . '">' . filter_xss_admin($info['output_times'] . $settings['separator_days']) . '</div>' . '</span>';
  }
  $html = '<span class="oh-wrapper' . ($settings['grouped'] ? ' oh-display-grouped' : '') . '">' . $html_hours . '</span>';
  if ($settings['current_status']['position'] !== 'hide') {

    // Generate HTML for CurrentStatus. Visibility is switched in js, otherwise the output might be wrong with static caching.
    $context_id_array = entity_extract_ids($vars['context']['entity_type'], $vars['context']['entity']);
    $context_id = implode('-', $context_id_array);
    $html_current_status = '<div class="oh-current-wrapper" data-oh-current-context_id="' . $context_id . '">';
    $html_current_status .= '<div class="oh-current-open element-hidden">' . $settings['current_status']['open_text'] . '</div>';
    $html_current_status .= '<div class="oh-current-closed element-hidden">' . $settings['current_status']['closed_text'] . '</div>';
    $html_current_status .= '</div>';
    drupal_add_js(array(
      'office_hours' => array(
        'instances' => array(
          $context_id => array(
            'days2calc' => $days2calc,
          ),
        ),
      ),
    ), 'setting');
    switch ($settings['current_status']['position']) {
      case 'before':
        $html = $html_current_status . $html_hours;
        break;
      case 'after':
        $html = $html_hours . $html_current_status;
        break;
    }
  }
  return $html;
}