Projet

Général

Profil

Paste
Télécharger (18,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.theme @ 599a39cd

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
  // Verify these variables exist.
120
  if (!isset($dates['value']['formatted'])) {
121
    $dates['value']['formatted'] = '';
122
  }
123
  if (!isset($dates['value2']['formatted'])) {
124
    $dates['value2']['formatted'] = '';
125
  }
126
  if (!isset($dates['value']['formatted_time'])) {
127
    $dates['value']['formatted_time'] = '';
128
  }
129
  if (!isset($dates['value2']['formatted_time'])) {
130
    $dates['value2']['formatted_time'] = '';
131
  }
132
  if (!isset($dates['value1']['formatted_timezone'])) {
133
    $dates['value1']['formatted_timezone'] = '';
134
  }
135
  if (!isset($dates['value2']['formatted_timezone'])) {
136
    $dates['value2']['formatted_timezone'] = '';
137
  }
138

    
139
  switch ($options['fromto']) {
140
    case 'value':
141
      $date1 = $dates['value']['formatted'];
142
      $date2 = $date1;
143
      break;
144

    
145
    case 'value2':
146
      $date2 = $dates['value2']['formatted'];
147
      $date1 = $date2;
148
      break;
149

    
150
    default:
151
      $date1 = $dates['value']['formatted'];
152
      $date2 = $dates['value2']['formatted'];
153
      break;
154
  }
155

    
156
  // Pull the timezone, if any, out of the formatted result and tack it back on
157
  // at the end, if it is in the current formatted date.
158
  $timezone = $dates['value']['formatted_timezone'];
159
  if ($timezone) {
160
    $timezone = ' ' . $timezone;
161
  }
162
  $date1 = str_replace($timezone, '', $date1);
163
  $date2 = str_replace($timezone, '', $date2);
164
  $time1 = preg_replace('`^([\(\[])`', '', $dates['value']['formatted_time']);
165
  $time1 = preg_replace('([\)\]]$)', '', $time1);
166
  $time2 = preg_replace('`^([\(\[])`', '', $dates['value2']['formatted_time']);
167
  $time2 = preg_replace('([\)\]]$)', '', $time2);
168

    
169
  // A date with a granularity of 'hour' has a time string that is an integer
170
  // value. We can't use that to replace time strings in formatted dates.
171
  $has_time_string = date_has_time($field['settings']['granularity']);
172
  if ($precision == 'hour') {
173
    $has_time_string = FALSE;
174
  }
175

    
176
  // Check remaining days.
177
  $show_remaining_days = '';
178
  if (!empty($variables['show_remaining_days']) && isset($variables['dates']['value']['formatted_iso'])) {
179
    $remaining_days = $variables['dates']['value']['formatted_iso'];
180
    $remaining_days = floor((strtotime($remaining_days) - strtotime('now')) / (24 * 3600));
181

    
182
    // Show remaining days only for future events.
183
    if ($remaining_days >= 0) {
184
      $show_remaining_days = theme('date_display_remaining', array(
185
        'remaining_days' => $remaining_days,
186
      ));
187
    }
188
  }
189

    
190
  // No date values, display nothing.
191
  if (empty($date1) && empty($date2)) {
192
    $output .= '';
193
  }
194

    
195
  // Start and End dates match or there is no End date, display a complete
196
  // single date.
197
  elseif ($date1 == $date2 || empty($date2)) {
198
    $output .= theme('date_display_single', array(
199
      'date' => $date1,
200
      'timezone' => $has_time_string ? $timezone : '',
201
      'attributes' => $attributes,
202
      'rdf_mapping' => $rdf_mapping,
203
      'add_rdf' => $add_rdf,
204
      'microdata' => $microdata,
205
      'add_microdata' => $add_microdata,
206
      'dates' => $dates,
207
      'show_remaining_days' => $show_remaining_days,
208
    ));
209
  }
210

    
211
  // Same day, different times, don't repeat the date but show both Start and
212
  // End times. We can NOT do this if the replacement value is an integer
213
  // instead of a time string.
214
  elseif ($has_time_string && $dates['value']['formatted_date'] == $dates['value2']['formatted_date']) {
215
    // Replace the original time with the start/end time in the formatted start
216
    // date. Make sure that parentheses or brackets wrapping the time will be
217
    // retained in the final result.
218
    $time = theme('date_display_range', array(
219
      'date1' => $time1,
220
      'date2' => $time2,
221
      'timezone' => $timezone,
222
      'attributes' => $attributes,
223
      'rdf_mapping' => $rdf_mapping,
224
      'add_rdf' => $add_rdf,
225
      'microdata' => $microdata,
226
      'add_microdata' => $add_microdata,
227
      'dates' => $dates,
228
      'show_remaining_days' => $show_remaining_days,
229
    ));
230
    $replaced = str_replace($time1, $time, $date1);
231
    $output .= theme('date_display_single', array(
232
      'date' => $replaced,
233
      'timezone' => $timezone,
234
      'attributes' => array(),
235
      'rdf_mapping' => array(),
236
      'add_rdf' => FALSE,
237
      'dates' => $dates,
238
    ));
239
  }
240

    
241
  // Different days, display both in their entirety.
242
  else {
243
    $output .= theme('date_display_range', array(
244
      'date1' => $date1,
245
      'date2' => $date2,
246
      'timezone' => $has_time_string ? $timezone : '',
247
      'attributes' => $attributes,
248
      'rdf_mapping' => $rdf_mapping,
249
      'add_rdf' => $add_rdf,
250
      'microdata' => $microdata,
251
      'add_microdata' => $add_microdata,
252
      'dates' => $dates,
253
      'show_remaining_days' => $show_remaining_days,
254
    ));
255
  }
256

    
257
  return $output;
258
}
259

    
260
/**
261
 * Template preprocess function for displaying a single date.
262
 */
263
function template_preprocess_date_display_single(&$variables) {
264
  if ($variables['add_rdf'] || !empty($variables['add_microdata'])) {
265
    // Pass along the rdf mapping for this field, if any. Add some default rdf
266
    // attributes that will be used if not overridden by attributes passed in.
267
    $rdf_mapping = $variables['rdf_mapping'];
268
    $base_attributes = array(
269
      'property' => array('dc:date'),
270
      'datatype' => 'xsd:dateTime',
271
      'content' => $variables['dates']['value']['formatted_iso'],
272
    );
273
    $variables['attributes'] = $variables['attributes'] + $base_attributes;
274
  }
275

    
276
  // Pass along microdata attributes, or set display to FALSE if none are set.
277
  if (!empty($variables['add_microdata'])) {
278
    // Because the Entity API integration for Date has a variable data
279
    // structure depending on whether there is an end value, the attributes
280
    // could be attached to the field or to the value property.
281
    if (!empty($variables['microdata']['#attributes']['itemprop'])) {
282
      $variables['microdata']['value']['#attributes'] = $variables['microdata']['#attributes'];
283
    }
284

    
285
    // Add the machine readable time using the content attribute.
286
    if (!empty($variables['microdata']['value']['#attributes'])) {
287
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
288
    }
289
    else {
290
      $variables['add_microdata'] = FALSE;
291
    }
292
  }
293
}
294

    
295
/**
296
 * Returns HTML for a date element formatted as a single date.
297
 */
298
function theme_date_display_single($variables) {
299
  $date = $variables['date'];
300
  $timezone = $variables['timezone'];
301
  $attributes = $variables['attributes'];
302
  $show_remaining_days = isset($variables['show_remaining_days']) ? $variables['show_remaining_days'] : '';
303

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

    
307
  if (!empty($variables['add_microdata'])) {
308
    $output .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
309
  }
310

    
311
  // Add remaining message and return.
312
  return $output . $show_remaining_days;
313
}
314

    
315
/**
316
 * Template preprocess function for displaying a range of dates.
317
 */
318
function template_preprocess_date_display_range(&$variables) {
319
  // Merge in the shared attributes for themes to use.
320
  $variables['attributes_start'] += $variables['attributes'];
321
  $variables['attributes_end'] += $variables['attributes'];
322

    
323
  if ($variables['add_rdf']) {
324
    // Pass along the rdf mapping for this field, if any. Add some default RDF
325
    // attributes that will be used if not overridden by attributes passed in.
326
    $dates = $variables['dates'];
327
    $base_attributes = array(
328
      'property' => array('dc:date'),
329
      'datatype' => 'xsd:dateTime',
330
      'content' => $dates['value']['formatted_iso'],
331
    );
332
    $variables['attributes_start'] += $base_attributes;
333
    $variables['attributes_end'] += $base_attributes;
334
    $variables['attributes_end']['content'] = $dates['value2']['formatted_iso'];
335
    foreach ($variables['attributes_end']['property'] as $delta => $property) {
336
      $variables['attributes_end']['property'][$delta] = str_replace('start', 'end', $property);
337
    }
338
  }
339

    
340
  // Pass along microdata attributes, or set display to FALSE if none are set.
341
  if ($variables['add_microdata']) {
342
    if (!empty($variables['microdata']['value']['#attributes'])) {
343
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
344
      $variables['microdata']['value2']['#attributes']['content'] = $variables['dates']['value2']['formatted_iso'];
345
    }
346
    else {
347
      $variables['add_microdata'] = FALSE;
348
    }
349
  }
350
}
351

    
352
/**
353
 * Returns HTML for a date element formatted as a range.
354
 */
355
function theme_date_display_range($variables) {
356
  $date1 = $variables['date1'];
357
  $date2 = $variables['date2'];
358
  $timezone = $variables['timezone'];
359
  $attributes_start = $variables['attributes_start'];
360
  $attributes_end = $variables['attributes_end'];
361
  $show_remaining_days = isset($variables['show_remaining_days']) ? $variables['show_remaining_days'] : '';
362

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

    
366
  // If microdata attributes for the start date property have been passed in,
367
  // add the microdata in meta tags.
368
  if (!empty($variables['add_microdata'])) {
369
    $start_date .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
370
    $end_date .= '<meta' . drupal_attributes($variables['microdata']['value2']['#attributes']) . '/>';
371
  }
372

    
373
  // Wrap the result with the attributes.
374
  $output = '<span class="date-display-range">' . t('!start-date to !end-date', array(
375
    '!start-date' => $start_date,
376
    '!end-date' => $end_date,
377
  )) . '</span>';
378

    
379
  // Add remaining message and return.
380
  return $output . $show_remaining_days;
381
}
382

    
383
/**
384
 * Returns HTML for a date element formatted as an interval.
385
 */
386
function theme_date_display_interval($variables) {
387
  $entity = $variables['entity'];
388
  $options = $variables['display']['settings'];
389
  $dates = $variables['dates'];
390
  $attributes = $variables['attributes'];
391

    
392
  // Get the formatter settings, either the default settings for this node type
393
  // or the View settings stored in $entity->date_info.
394
  if (!empty($entity->date_info) && !empty($entity->date_info->formatter_settings)) {
395
    $options = $entity->date_info->formatter_settings;
396
  }
397

    
398
  $time_ago_vars = array(
399
    'start_date' => $dates['value']['local']['object'],
400
    'end_date' => $dates['value2']['local']['object'],
401
    'interval' => $options['interval'],
402
    'interval_display' => $options['interval_display'],
403
    'use_end_date' => !empty($options['use_end_date']) ?
404
    $options['use_end_date'] : FALSE,
405
  );
406

    
407
  if ($return = theme('date_time_ago', $time_ago_vars)) {
408
    return '<span class="date-display-interval"' . drupal_attributes($attributes) . ">$return</span>";
409
  }
410
  else {
411
    return '';
412
  }
413
}
414

    
415
/**
416
 * Returns HTML for a start/end date combination on form.
417
 */
418
function theme_date_combo($variables) {
419
  $element = $variables['element'];
420
  $field = field_info_field($element['#field_name']);
421
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
422

    
423
  // Group start/end items together in fieldset.
424
  $fieldset = array(
425
    '#title' => field_filter_xss(t($element['#title'])) . ($element['#delta'] > 0 ? ' ' . intval($element['#delta'] + 1) : ''),
426
    '#value' => '',
427
    '#description' => !empty($element['#description']) ? $element['#description'] : '',
428
    '#attributes' => array('class' => array('date-combo')),
429
    '#children' => $element['#children'],
430
  );
431
  // Add marker to required date fields.
432
  if ($element['#required']) {
433
    $fieldset['#title'] .= " " . theme('form_required_marker');
434
  }
435
  return theme('fieldset', array('element' => $fieldset));
436
}
437

    
438
/**
439
 * Returns HTML for the text/select options for date parts in a table.
440
 */
441
function theme_date_text_parts($variables) {
442
  $element = $variables['element'];
443
  $rows = array();
444
  foreach (date_granularity_names() as $key => $part) {
445
    if ($element[$key]['#type'] == 'hidden') {
446
      $rows[] = drupal_render($element[$key]);
447
    }
448
    else {
449
      $rows[] = array(
450
        $part,
451
        drupal_render($element[$key][0]),
452
        drupal_render($element[$key][1]),
453
      );
454
    }
455
  }
456
  if ($element['year']['#type'] == 'hidden') {
457
    return implode($rows) . drupal_render_children($element);
458
  }
459
  else {
460
    $header = array(t('Date part'), t('Select list'), t('Text field'));
461
    return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
462
  }
463
}
464

    
465
/**
466
 * Render a date combo as a form element.
467
 */
468
function theme_date_form_element($variables) {
469
  $element = &$variables['element'];
470

    
471
  // Detect whether element is multiline.
472
  $count = preg_match_all('`<(?:div|span)\b[^>]* class="[^"]*\b(?:date-no-float|date-clear)\b`', $element['#children'], $matches, PREG_OFFSET_CAPTURE);
473
  $multiline = FALSE;
474
  if ($count > 1) {
475
    $multiline = TRUE;
476
  }
477
  elseif ($count) {
478
    $before = substr($element['#children'], 0, $matches[0][0][1]);
479
    if (preg_match('`<(?:div|span)\b[^>]* class="[^"]*\bdate-float\b`', $before)) {
480
      $multiline = TRUE;
481
    }
482
  }
483

    
484
  // Detect if there is more than one subfield.
485
  $count = count(explode('<label', $element['#children'])) - 1;
486
  if ($count == 1) {
487
    // If there is only one subfield, hide the label of the main field only if
488
    // the label of the subfield is visible.
489
    if (empty($variables['element']['value']['#instance'])
490
      || empty($variables['element']['value']['#instance']['widget'])
491
      || empty($variables['element']['value']['#instance']['widget']['settings'])
492
      || empty($variables['element']['value']['#instance']['widget']['settings']['label_position'])
493
      || $variables['element']['value']['#instance']['widget']['settings']['label_position'] != 'none') {
494
      $element['#title_display'] = 'none';
495
    }
496
  }
497

    
498
  // Wrap children with a div and add an extra class if element is multiline.
499
  $element['#children'] = '<div class="date-form-element-content' . ($multiline ? ' date-form-element-content-multiline' : '') . '">' . $element['#children'] . '</div>';
500

    
501
  return theme('form_element', $variables);
502
}
503

    
504
/**
505
 * Returns HTML for remaining message.
506
 */
507
function theme_date_display_remaining($variables) {
508
  $remaining_days = $variables['remaining_days'];
509
  $output = '';
510
  $show_remaining_text = t('Less than 1 day remaining');
511
  if ($remaining_days) {
512
    $show_remaining_text = format_plural($remaining_days, '1 day remaining', '@count days remaining');
513
  }
514

    
515
  return '<div class="date-display-remaining"><span class="date-display-remaining">' . $show_remaining_text . '</span></div>';
516
}
517

    
518
/**
519
 * @} End of addtogroup themeable */