You are here

date.theme in Date 6

Same filename and directory in other branches
  1. 5.2 date/date.theme
  2. 6.2 date/date.theme

Theme functions.

File

date/date.theme
View source
<?php

/**
 * @file
 * Theme functions.
 */

/**
 * @addtogroup themeable
 * @{
 *
 * Formatter themes
 */

/**
 *  Theme from/to date combination in the view.
 *
 *  Useful values:
 *    $dates['format'] - the format string used on these dates
 *    $dates['value']['local']['object'] - the local date object for the From date
 *    $dates['value2']['local']['object'] - the local date object for the To date
 *    $dates['value']['local']['datetime'] - the datetime value of the From date database (GMT) value
 *    $dates['value2']['local']['datetime'] - the datetime value of the To date database (GMT) value
 *    $dates['value']['formatted'] = formatted From date, i.e. 'February 15, 2007 2:00 pm';
 *    $dates['value']['formatted_date'] - only the date part of the formatted From date
 *    $dates['value']['formatted_time'] - only the time part of the formatted From date
 *    $dates['value2']['formatted'] = formatted To date, i.e. 'February 15, 2007 6:00 pm';
 *    $dates['value2']['formatted_date'] - only the date part of the formatted To date
 *    $dates['value2']['formatted_time'] - only the time part of the formatted To date
 */
function theme_date_display_combination($element) {
  $dates = date_formatter_process($element);
  $date1 = $dates['value']['formatted'];
  $date2 = $dates['value2']['formatted'];

  // Pull the timezone out of the formatted result and tack it
  // back on at the end, if it is in the current formatted date.
  $timezone = '';
  if (!empty($element['#item']['timezone']) && strpos($date1, $element['#item']['timezone'])) {
    $timezone = ' ' . $element['#item']['timezone'];
  }
  $date1 = str_replace($element['#item']['timezone'], '', $date1);
  $date2 = str_replace($element['#item']['timezone'], '', $date2);

  // No date values, display nothing.
  $output = '';
  if (empty($date1) && empty($date2)) {
    $output .= '';
  }
  elseif ($date1 == $date2 || empty($date2)) {
    $output .= '<span class="date-display-single">' . $date1 . $timezone . '</span>';
  }
  elseif ($dates['value']['formatted_date'] == $dates['value2']['formatted_date']) {
    $output .= '<span class="date-display-single">' . $dates['value']['formatted_date'] . '</span> ' . '<span class="date-display-start">' . $dates['value']['formatted_time'] . '</span>' . '<span class="date-display-separator"> - </span>' . '<span class="date-display-end">' . $dates['value2']['formatted_time'] . $timezone . '</span>';
  }
  else {
    $output .= '<span class="date-display-start">' . $date1 . '</span>' . '<span class="date-display-separator"> - </span>' . '<span class="date-display-end">' . $date2 . $timezone . '</span>';
  }
  if (module_exists('date_repeat') && !empty($item['rrule'])) {
    include_once drupal_get_path('module', 'date') . '/date_repeat.inc';
    $output .= theme('date_repeat_display', $field, $item, $node, $dates);
  }
  return $output;
}

/**
 * Theme a format interval for a date element
 *
 *  @param $field = the field settings
 *  @param $node = node information, this is not always available and not
 *     always the full node, it depends on what value was provided to the formatter.
 *     Only the nid is always guaranteed to be available.
 *  @param $dates - an array of date information, see explanation for date_field_object for details.
 *  @return a formatted display
 *
 */
function theme_date_format_interval($element) {
  $dates = date_formatter_process($element);

  // Time to compare dates to
  $now = date_format(date_make_date('now', date_default_timezone()), DATE_FORMAT_DATETIME);
  $start = $dates['value']['local']['datetime'];
  $end = $dates['value2']['local']['datetime'];

  // 1) The date is entirely in the future
  if ($now < $start) {
    return t('!time from now', array(
      '!time' => $dates['value']['interval'],
    ));
  }
  elseif ($now > $start && $now <= $end) {

    //return t('Started !time ago', array('!time' => $dates['value']['interval']));
    return t('ongoing');
  }
  else {
    return t('!time ago', array(
      '!time' => $dates['value']['interval'],
    ));
  }
}

/**
 * Theme the human-readable description for a Date Repeat rule.
 *
 * TODO -
 * add in ways to store the description in the date so it isn't regenerated
 * over and over and find a way to allow description to be shown or hidden.
 */
function theme_date_repeat_display($field, $item, $node = NULL, $dates) {

  // If $field['field_name'] isn't set, this isn't a node, and is
  // probably on a calendar view. The description is too expensive an
  // operation to run on every field in a view, so don't do it.
  if (isset($node->{$field}['field_name'])) {
    $output = date_repeat_rrule_description($item['rrule'], $dates['value']['local']['object']);
    $output = '<div>' . $output . '</div>';
  }
  return $output;
}

/**
 *  Theme for the current period label name
 */
function theme_date_views_browser_period_label($period, $date) {
  switch ($period) {
    case 'year':
      $format = 'Y';
      break;
    case 'month':
      $format = 'F Y';
      break;
    case 'day':

      // Use just the year/month/day part of the short date format.
      $format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array(
        'month',
        'day',
        'year',
      ));
      break;
    case 'hour':

      // Use just the year/month/day/hour part of the short date format.
      $format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), array(
        'month',
        'day',
        'year',
        'hour',
      ));
  }
  return t('%period of %date', array(
    '%period' => ucwords($period),
    '%date' => date_format_date($date, 'custom', $format),
  ));
}

/**
 *  Theme for page navigation
 */
function theme_date_views_browser_navigation($label, $period, $prev, $next, $view) {
  drupal_add_css(drupal_get_path('module', 'date_api') . '/date.css');
  $output = '<div class="book-navigation date-browser-navigation">';
  $output .= '<div class="page-links">';
  $output .= l(t('‹ prev !period  ', array(
    '!period' => $period,
  )), $prev, array(
    'class' => 'page-previous',
  ));
  $output .= '<h3 class="date-browser-label"><span class="page-up">' . $label . '</span></h3>';
  $output .= l(t('  next !period  ›', array(
    '!period' => $period,
  )), $next, array(
    'class' => 'page-next',
  ));
  $output .= '</div></div>';
  return $output;
  return $output;
}

/**
 * Display a summary version of a view.
 */
function theme_date_views_browser_summary_view($view, $type, $level, $nodes, $args) {
  return theme('date_views_browser_full_view', $view, $nodes, $type);
}

/**
 * View, themed so it can be overridden
 *
 * Set $display to views_view_list, views_view_table, views_view_teasers,
 * or views_view_nodes
 *
 * As an alternative, override this theme and set the display to
 * date_views_browser_items for a display of only the current item
 * value to keep multiple value dates from repeating the whole
 * teaser each time.
 */
function theme_date_views_browser_full_view($view, $nodes, $type) {
  $teasers = true;
  $links = true;
  $date_views_browser_views = date_views_browser_get_views();
  $period = $date_views_browser_views[$view->name]->options;
  switch ($type) {
    case 'block':
      $arg = date_views_browser_period_arg(NULL, $view->argument[0]['options']);
      if ($view->url) {
        $url = $view->url . '/' . $arg;
      }
      $output .= '<h5 class="date-browser-block-label">' . l(date_views_browser_period_label(NULL, $period), $url) . '</h5>';
      $display = 'views_view_list';
      break;
    default:
      $output .= date_views_browser_navigation($view, $period);
      $display = 'views_view_teasers';
      break;
  }
  $output .= theme($display, $view, $nodes, $type, $teasers, $links);
  return $output;
}

/**
 * Alternative Date Browser Display
 *
 * Display only the selected browser items instead of teasers or nodes,
 * use for multiple date values to keep the entire teaser or node from being
 * repeated for every multiple value of the date.
 */
function theme_date_views_browser_items($view, $nodes, $type, $teasers, $links) {
  $fields = _views_get_fields();
  $items = array();
  foreach ($nodes as $node) {
    $item = '';
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        if ($field['label']) {
          $item .= "<div class='view-label " . views_css_safe('view-label-' . $field['queryname']) . "'>" . $field['label'] . "</div>";
        }

        // If this is the date field, format and display the current value.
        if (strstr($field['handler'], 'content_views_')) {
          $field_name = explode('_value', $field['field']);
          if (!empty($node->date_combo)) {
            $values = explode('|', $node->date_combo);
            $node_item = array(
              'value' => $values[0],
              'value2' => $values[1],
            );
          }
          else {
            $node_item = array(
              'value' => $node->{$field}['field'],
            );
          }
          $item .= "<div class='view-field " . views_css_safe('view-data-' . $field['queryname']) . "'>" . content_format($field_name[0], $node_item, $field['options'], $node) . "</div>";
        }
        else {
          $item .= "<div class='view-field " . views_css_safe('view-data-' . $field['queryname']) . "'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "</div>";
        }
      }
    }
    $items[] = "<div class='view-item " . views_css_safe('view-item-' . $view->name) . "'>{$item}</div>\n";
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

/** @} End of addtogroup themeable */

Functions

Namesort descending Description
theme_date_display_combination Theme from/to date combination in the view.
theme_date_format_interval Theme a format interval for a date element
theme_date_repeat_display Theme the human-readable description for a Date Repeat rule.
theme_date_views_browser_full_view View, themed so it can be overridden
theme_date_views_browser_items Alternative Date Browser Display
theme_date_views_browser_navigation Theme for page navigation
theme_date_views_browser_period_label Theme for the current period label name
theme_date_views_browser_summary_view Display a summary version of a view.