Beispiel #1
0
/**
 * Overrides theme_date_display_combination().
 */
function favrskovtheme_date_display_combination($variables)
{
    static $repeating_ids = array();
    $entity_type = $variables['entity_type'];
    $entity = $variables['entity'];
    $field = $variables['field'];
    $instance = $variables['instance'];
    $langcode = $variables['langcode'];
    $item = $variables['item'];
    $delta = $variables['delta'];
    $display = $variables['display'];
    $field_name = $field['field_name'];
    $formatter = $display['type'];
    $options = $display['settings'];
    $dates = $variables['dates'];
    $attributes = $variables['attributes'];
    $rdf_mapping = $variables['rdf_mapping'];
    $add_rdf = $variables['add_rdf'];
    $precision = date_granularity_precision($field['settings']['granularity']);
    $output = '';
    // If date_id is set for this field and delta doesn't match, don't display it.
    if (!empty($entity->date_id)) {
        foreach ((array) $entity->date_id as $key => $id) {
            list($module, $nid, $field_name, $item_delta, $other) = explode('.', $id . '.');
            if ($field_name == $field['field_name'] && isset($delta) && $item_delta != $delta) {
                return $output;
            }
        }
    }
    // Check the formatter settings to see if the repeat rule should be displayed.
    // Show it only with the first multiple value date.
    list($id) = entity_extract_ids($entity_type, $entity);
    if (!in_array($id, $repeating_ids) && module_exists('date_repeat_field') && !empty($item['rrule']) && $options['show_repeat_rule'] == 'show') {
        $repeat_vars = array('field' => $field, 'item' => $item, 'entity_type' => $entity_type, 'entity' => $entity);
        $output .= theme('date_repeat_display', $repeat_vars);
        $repeating_ids[] = $id;
    }
    // If this is a full node or a pseudo node created by grouping multiple
    // values, see exactly which values are supposed to be visible.
    if (isset($entity->{$field_name})) {
        $entity = date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
        // Did the current value get removed by formatter settings?
        if (empty($entity->{$field_name}[$langcode][$delta])) {
            return $output;
        }
        // Adjust the $element values to match the changes.
        $element['#entity'] = $entity;
    }
    switch ($options['fromto']) {
        case 'value':
            $date1 = $dates['value']['formatted'];
            $date2 = $date1;
            break;
        case 'value2':
            $date2 = $dates['value2']['formatted'];
            $date1 = $date2;
            break;
        default:
            $date1 = $dates['value']['formatted'];
            $date2 = $dates['value2']['formatted'];
            break;
    }
    // Pull the timezone, if any, out of the formatted result and tack it back on
    // at the end, if it is in the current formatted date.
    $timezone = $dates['value']['formatted_timezone'];
    if ($timezone) {
        $timezone = ' ' . $timezone;
    }
    $date1 = str_replace($timezone, '', $date1);
    $date2 = str_replace($timezone, '', $date2);
    $time1 = preg_replace('`^([\\(\\[])`', '', $dates['value']['formatted_time']);
    $time1 = preg_replace('([\\)\\]]$)', '', $time1);
    $time2 = preg_replace('`^([\\(\\[])`', '', $dates['value2']['formatted_time']);
    $time2 = preg_replace('([\\)\\]]$)', '', $time2);
    // A date with a granularity of 'hour' has a time string that is an integer
    // value. We can't use that to replace time strings in formatted dates.
    $has_time_string = date_has_time($field['settings']['granularity']);
    if ($precision == 'hour') {
        $has_time_string = FALSE;
    }
    // No date values, display nothing.
    if (empty($date1) && empty($date2)) {
        $output .= '';
    } elseif ($date1 == $date2 || empty($date2)) {
        $output .= theme('date_display_single', array('date' => $date1, 'timezone' => $timezone, 'attributes' => $attributes, 'rdf_mapping' => $rdf_mapping, 'add_rdf' => $add_rdf, 'dates' => $dates));
    } else {
        $output .= theme('date_display_range', array('date1' => $date1, 'date2' => $date2, 'timezone' => $timezone, 'attributes' => $attributes, 'rdf_mapping' => $rdf_mapping, 'add_rdf' => $add_rdf, 'dates' => $dates));
    }
    return $output;
}
/**
 * Rewrite a format string so it only includes elements from a
 * specified granularity array.
 *
 * Example:
 *   date_limit_format('F j, Y - H:i', array('year', 'month', 'day'));
 *   returns 'F j, Y'
 *
 * @param $format
 *   a format string
 * @param $granularity
 *   an array of allowed date parts, all others will be removed
 *   array('year', 'month', 'day', 'hour', 'minute', 'second');
 * @return
 *   a format string with all other elements removed
 */
function date_limit_format($format, $granularity)
{
    // If punctuation has been escaped, remove the escaping.
    // Done using strtr because it is easier than getting the
    // escape character extracted using preg_replace.
    $replace = array('\\-' => '-', '\\:' => ':', "\\'" => "'", '\\. ' => ' . ', '\\,' => ',');
    $format = strtr($format, $replace);
    // Get the 'T' out of ISO date formats that don't have
    // both date and time.
    if (!date_has_time($granularity) || !date_has_date($granularity)) {
        $format = str_replace('\\T', ' ', $format);
        $format = str_replace('T', ' ', $format);
    }
    $regex = array();
    if (!date_has_time($granularity)) {
        $regex[] = '((?<!\\\\)[a|A])';
    }
    // Create regular expressions to remove selected values from string.
    // Use (?<!\\\\) to keep escaped letters from being removed.
    foreach (date_nongranularity($granularity) as $element) {
        switch ($element) {
            case 'year':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[Yy])';
                break;
            case 'day':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[l|D|d|dS|j|jS]{1,2})';
                break;
            case 'month':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[FMmn])';
                break;
            case 'hour':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[HhGg])';
                break;
            case 'minute':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[i])';
                break;
            case 'second':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[s])';
                break;
            case 'timezone':
                $regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[TOZPe])';
                break;
        }
    }
    // Remove empty parentheses, brackets, pipes.
    $regex[] = '(\\(\\))';
    $regex[] = '(\\[\\])';
    $regex[] = '(\\|\\|)';
    // Remove selected values from string.
    $format = trim(preg_replace($regex, array(), $format));
    // Remove orphaned punctuation at the beginning of the string.
    $format = preg_replace('`^([\\-/\\.,:\'])`', '', $format);
    // Remove orphaned punctuation at the end of the string.
    $format = preg_replace('([\\-/\\.,:\']$)', '', $format);
    $format = preg_replace('(\\$)', '', $format);
    // Trim any whitespace from the result.
    $format = trim($format);
    // After removing the non-desired parts of the format, test if the only
    // things left are escaped, non-date, characters. If so, return nothing.
    if (!($test = trim(preg_replace('(\\\\\\w{1})', '', $format)))) {
        return '';
    }
    return $format;
}