Projet

Général

Profil

Paste
Télécharger (14,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.theme @ db9ffd17

1
<?php
2

    
3
/**
4
 * @file
5
 * Theme functions.
6
 */
7

    
8
/**
9
 * @addtogroup themeable
10
 * @{
11
 *
12
 * Formatter themes
13
 */
14

    
15
/**
16
 * Returns HTML for a date element formatted as a Start/End combination.
17
 *
18
 *  $entity->date_id
19
 *    If set, this will show only an individual date on a field with
20
 *    multiple dates. The value should be a string that contains
21
 *    the following values, separated with periods:
22
 *    - module name of the module adding the item
23
 *    - node nid
24
 *    - field name
25
 *    - delta value of the field to be displayed
26
 *    - other information the module's custom theme might need
27
 *
28
 *    Used by the calendar module and available for other uses.
29
 *    example: 'date.217.field_date.3.test'
30
 *
31
 *  $entity->date_repeat_show
32
 *    If true, tells the theme to show all the computed values of a repeating
33
 *    date. If not true or not set, only the start date and the repeat rule
34
 *    will be displayed.
35
 *
36
 *  $dates['format']
37
 *    The format string used on these dates
38
 *  $dates['value']['local']['object']
39
 *    The local date object for the Start date
40
 *  $dates['value2']['local']['object']
41
 *    The local date object for the End date
42
 *  $dates['value']['local']['datetime']
43
 *    The datetime value of the Start date database (GMT) value
44
 *  $dates['value2']['local']['datetime']
45
 *    The datetime value of the End date database (GMT) value
46
 *  $dates['value']['formatted']
47
 *    Formatted Start date, i.e. 'February 15, 2007 2:00 pm';
48
 *  $dates['value']['formatted_date']
49
 *    Only the date part of the formatted Start date
50
 *  $dates['value']['formatted_time']
51
 *    Only the time part of the formatted Start date
52
 *  $dates['value2']['formatted']
53
 *    Formatted End date, i.e. 'February 15, 2007 6:00 pm';
54
 *  $dates['value2']['formatted_date']
55
 *    Only the date part of the formatted End date
56
 *  $dates['value2']['formatted_time']
57
 *    Only the time part of the formatted End date
58
 */
59
function theme_date_display_combination($variables) {
60
  static $repeating_ids = array();
61

    
62
  $entity_type = $variables['entity_type'];
63
  $entity      = $variables['entity'];
64
  $field       = $variables['field'];
65
  $instance    = $variables['instance'];
66
  $langcode    = $variables['langcode'];
67
  $item        = $variables['item'];
68
  $delta       = $variables['delta'];
69
  $display     = $variables['display'];
70
  $field_name  = $field['field_name'];
71
  $formatter   = $display['type'];
72
  $options     = $display['settings'];
73
  $dates       = $variables['dates'];
74
  $attributes  = $variables['attributes'];
75
  $rdf_mapping = $variables['rdf_mapping'];
76
  $add_rdf     = $variables['add_rdf'];
77
  $microdata   = $variables['microdata'];
78
  $add_microdata = $variables['add_microdata'];
79
  $precision   = date_granularity_precision($field['settings']['granularity']);
80

    
81
  $output = '';
82

    
83
  // If date_id is set for this field and delta doesn't match, don't display it.
84
  if (!empty($entity->date_id)) {
85
    foreach ((array) $entity->date_id as $key => $id) {
86
      list($module, $nid, $field_name, $item_delta, $other) = explode('.', $id . '.');
87
      if ($field_name == $field['field_name'] && isset($delta) && $item_delta != $delta) {
88
        return $output;
89
      }
90
    }
91
  }
92

    
93
  // Check the formatter settings to see if the repeat rule should be displayed.
94
  // Show it only with the first multiple value date.
95
  list($id) = entity_extract_ids($entity_type, $entity);
96
  if (!in_array($id, $repeating_ids) && module_exists('date_repeat_field') && !empty($item['rrule']) && $options['show_repeat_rule'] == 'show') {
97
    $repeat_vars = array(
98
      'field' => $field,
99
      'item' => $item,
100
      'entity_type' => $entity_type,
101
      'entity' => $entity,
102
    );
103
    $output .= theme('date_repeat_display', $repeat_vars);
104
    $repeating_ids[] = $id;
105
  }
106

    
107
  // If this is a full node or a pseudo node created by grouping multiple
108
  // values, see exactly which values are supposed to be visible.
109
  if (isset($entity->$field_name)) {
110
    $entity = date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
111
    // Did the current value get removed by formatter settings?
112
    if (empty($entity->{$field_name}[$langcode][$delta])) {
113
      return $output;
114
    }
115
    // Adjust the $element values to match the changes.
116
    $element['#entity'] = $entity;
117
  }
118

    
119
  switch ($options['fromto']) {
120
    case 'value':
121
      $date1 = $dates['value']['formatted'];
122
      $date2 = $date1;
123
      break;
124
    case 'value2':
125
      $date2 = $dates['value2']['formatted'];
126
      $date1 = $date2;
127
      break;
128
    default:
129
      $date1 = $dates['value']['formatted'];
130
      $date2 = $dates['value2']['formatted'];
131
      break;
132
  }
133

    
134
  // Pull the timezone, if any, out of the formatted result and tack it back on
135
  // at the end, if it is in the current formatted date.
136
  $timezone = $dates['value']['formatted_timezone'];
137
  if ($timezone) {
138
    $timezone = ' ' . $timezone;
139
  }
140
  $date1 = str_replace($timezone, '', $date1);
141
  $date2 = str_replace($timezone, '', $date2);
142
  $time1 = preg_replace('`^([\(\[])`', '', $dates['value']['formatted_time']);
143
  $time1 = preg_replace('([\)\]]$)', '', $time1);
144
  $time2 = preg_replace('`^([\(\[])`', '', $dates['value2']['formatted_time']);
145
  $time2 = preg_replace('([\)\]]$)', '', $time2);
146

    
147
  // A date with a granularity of 'hour' has a time string that is an integer
148
  // value. We can't use that to replace time strings in formatted dates.
149
  $has_time_string = date_has_time($field['settings']['granularity']);
150
  if ($precision == 'hour') {
151
    $has_time_string = FALSE;
152
  }
153

    
154
  // No date values, display nothing.
155
  if (empty($date1) && empty($date2)) {
156
    $output .= '';
157
  }
158
  // Start and End dates match or there is no End date, display a complete
159
  // single date.
160
  elseif ($date1 == $date2 || empty($date2)) {
161
    $output .= theme('date_display_single', array(
162
      'date' => $date1,
163
      'timezone' => $timezone,
164
      'attributes' => $attributes,
165
      'rdf_mapping' => $rdf_mapping,
166
      'add_rdf' => $add_rdf,
167
      'microdata' => $microdata,
168
      'add_microdata' => $add_microdata,
169
      'dates' => $dates,
170
    ));
171
  }
172
  // Same day, different times, don't repeat the date but show both Start and
173
  // End times. We can NOT do this if the replacement value is an integer
174
  // instead of a time string.
175
  elseif ($has_time_string && $dates['value']['formatted_date'] == $dates['value2']['formatted_date']) {
176
    // Replace the original time with the start/end time in the formatted start
177
    // date. Make sure that parentheses or brackets wrapping the time will be
178
    // retained in the final result.
179
    $time = theme('date_display_range', array(
180
      'date1' => $time1,
181
      'date2' => $time2,
182
      'timezone' => $timezone,
183
      'attributes' => $attributes,
184
      'rdf_mapping' => $rdf_mapping,
185
      'add_rdf' => $add_rdf,
186
      'microdata' => $microdata,
187
      'add_microdata' => $add_microdata,
188
      'dates' => $dates,
189
    ));
190
    $replaced = str_replace($time1, $time, $date1);
191
    $output .= theme('date_display_single', array(
192
      'date' => $replaced,
193
      'timezone' => $timezone,
194
      'attributes' => array(),
195
      'rdf_mapping' => array(),
196
      'add_rdf' => FALSE,
197
      'dates' => $dates,
198
    ));
199
  }
200
  // Different days, display both in their entirety.
201
  else {
202
    $output .= theme('date_display_range', array(
203
      'date1' => $date1,
204
      'date2' => $date2,
205
      'timezone' => $timezone,
206
      'attributes' => $attributes,
207
      'rdf_mapping' => $rdf_mapping,
208
      'add_rdf' => $add_rdf,
209
      'microdata' => $microdata,
210
      'add_microdata' => $add_microdata,
211
      'dates' => $dates,
212
    ));
213
  }
214

    
215
  return $output;
216
}
217

    
218
/**
219
 * Template preprocess function for displaying a single date.
220
 */
221
function template_preprocess_date_display_single(&$variables) {
222
  if ($variables['add_rdf'] || !empty($variables['add_microdata'])) {
223
    // Pass along the rdf mapping for this field, if any. Add some default rdf
224
    // attributes that will be used if not overridden by attributes passed in.
225
    $rdf_mapping = $variables['rdf_mapping'];
226
    $base_attributes = array(
227
      'property' => array('dc:date'),
228
      'datatype' => 'xsd:dateTime',
229
      'content' => $variables['dates']['value']['formatted_iso'],
230
    );
231
    $variables['attributes'] = $variables['attributes'] + $base_attributes;
232
  }
233

    
234
  // Pass along microdata attributes, or set display to false if none are set.
235
  if (!empty($variables['add_microdata'])) {
236
    // Because the Entity API integration for Date has a variable data
237
    // structure depending on whether there is an end value, the attributes
238
    // could be attached to the field or to the value property.
239
    if(!empty($variables['microdata']['#attributes']['itemprop'])) {
240
      $variables['microdata']['value']['#attributes'] = $variables['microdata']['#attributes'];
241
    }
242

    
243
    // Add the machine readable time using the content attribute.
244
    if(!empty($variables['microdata']['value']['#attributes'])) {
245
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
246
    }
247
    else {
248
      $variables['add_microdata'] = FALSE;
249
    }
250
  }
251
}
252

    
253
/**
254
 * Returns HTML for a date element formatted as a single date.
255
 */
256
function theme_date_display_single($variables) {
257
  $date = $variables['date'];
258
  $timezone = $variables['timezone'];
259
  $attributes = $variables['attributes'];
260

    
261
  // Wrap the result with the attributes.
262
  $output = '<span class="date-display-single"' . drupal_attributes($attributes) . '>' . $date . $timezone . '</span>';
263

    
264
  if (!empty($variables['add_microdata'])) {
265
    $output .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
266
  }
267

    
268
  return $output;
269
}
270

    
271
/**
272
 * Template preprocess function for displaying a range of dates.
273
 */
274
function template_preprocess_date_display_range(&$variables) {
275
  // Merge in the shared attributes for themes to use.
276
  $variables['attributes_start'] += $variables['attributes'];
277
  $variables['attributes_end'] += $variables['attributes'];
278

    
279
  if ($variables['add_rdf']) {
280
    // Pass along the rdf mapping for this field, if any. Add some default rdf
281
    // attributes that will be used if not overridden by attributes passed in.
282
    $dates = $variables['dates'];
283
    $base_attributes = array(
284
      'property' => array('dc:date'),
285
      'datatype' => 'xsd:dateTime',
286
      'content' => $dates['value']['formatted_iso'],
287
    );
288
    $variables['attributes_start'] += $base_attributes;
289
    $variables['attributes_end'] += $base_attributes;
290
    $variables['attributes_end']['content'] = $dates['value2']['formatted_iso'];
291
    foreach ($variables['attributes_end']['property'] as $delta => $property) {
292
      $variables['attributes_end']['property'][$delta] = str_replace('start', 'end', $property);
293
    }
294
  }
295

    
296
  // Pass along microdata attributes, or set display to false if none are set.
297
  if ($variables['add_microdata']) {
298
    if (!empty($variables['microdata']['value']['#attributes'])) {
299
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
300
      $variables['microdata']['value2']['#attributes']['content'] = $variables['dates']['value2']['formatted_iso'];
301
    }
302
    else {
303
      $variables['add_microdata'] = FALSE;
304
    }
305
  }
306
}
307

    
308
/**
309
 * Returns HTML for a date element formatted as a range.
310
 */
311
function theme_date_display_range($variables) {
312
  $date1 = $variables['date1'];
313
  $date2 = $variables['date2'];
314
  $timezone = $variables['timezone'];
315
  $attributes_start = $variables['attributes_start'];
316
  $attributes_end = $variables['attributes_end'];
317

    
318
  $start_date = '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>';
319
  $end_date = '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . $timezone . '</span>';
320

    
321
  // If microdata attributes for the start date property have been passed in,
322
  // add the microdata in meta tags.
323
  if (!empty($variables['add_microdata'])) {
324
    $start_date .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
325
    $end_date .= '<meta' . drupal_attributes($variables['microdata']['value2']['#attributes']) . '/>';
326
  }
327

    
328
  // Wrap the result with the attributes.
329
  return t('!start-date to !end-date', array(
330
    '!start-date' => $start_date,
331
    '!end-date' => $end_date,
332
  ));
333
}
334

    
335
/**
336
 * Returns HTML for a date element formatted as an interval.
337
 */
338
function theme_date_display_interval($variables) {
339
  $entity = $variables['entity'];
340
  $options = $variables['display']['settings'];
341
  $dates = $variables['dates'];
342
  $attributes = $variables['attributes'];
343

    
344
  // Get the formatter settings, either the default settings for this node type
345
  // or the View settings stored in $entity->date_info.
346
  if (!empty($entity->date_info) && !empty($entity->date_info->formatter_settings)) {
347
    $options = $entity->date_info->formatter_settings;
348
  }
349

    
350
  $time_ago_vars = array(
351
    'start_date' => $dates['value']['local']['object'],
352
    'end_date' => $dates['value2']['local']['object'],
353
    'interval' => $options['interval'],
354
    'interval_display' => $options['interval_display'],
355
  );
356

    
357
  if ($return = theme('date_time_ago', $time_ago_vars)) {
358
    return '<span class="date-display-interval"' . drupal_attributes($attributes) . ">$return</span>";
359
  }
360
  else {
361
    return '';
362
  }
363
}
364

    
365
/**
366
 * Returns HTML for a start/end date combination on form.
367
 */
368
function theme_date_combo($variables) {
369
  $element = $variables['element'];
370
  $field = field_info_field($element['#field_name']);
371
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
372

    
373
  // Group start/end items together in fieldset.
374
  $fieldset = array(
375
    '#title' => field_filter_xss(t($element['#title'])) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
376
    '#value' => '',
377
    '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
378
    '#attributes' => array(),
379
    '#children' => $element['#children'],
380
  );
381
  // Add marker to required date fields.
382
  if ($element['#required']) {
383
    $fieldset['#title'] .= " " . theme('form_required_marker');
384
  }
385
  return theme('fieldset', array('element' => $fieldset));
386
}
387

    
388
/**
389
 * Returns HTML for the text/select options for date parts in a table.
390
 */
391
function theme_date_text_parts($variables) {
392
  $element = $variables['element'];
393
  $rows = array();
394
  foreach (date_granularity_names() as $key => $part) {
395
    if ($element[$key]['#type'] == 'hidden') {
396
      $rows[] = drupal_render($element[$key]);
397
    }
398
    else {
399
      $rows[] = array($part, drupal_render($element[$key][0]), drupal_render($element[$key][1]));
400
    }
401
  }
402
  if ($element['year']['#type'] == 'hidden') {
403
    return implode($rows) . drupal_render_children($element);
404
  }
405
  else {
406
    $header = array(t('Date part'), t('Select list'), t('Text field'));
407
    return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
408
  }
409
}
410

    
411
/** @} End of addtogroup themeable */