Projet

Général

Profil

Paste
Télécharger (17,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / date.theme @ 1f683914

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
  $show_remaining_days = $variables['show_remaining_days'];
81

    
82
  $output = '';
83

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

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

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

    
120
  switch ($options['fromto']) {
121
    case 'value':
122
      $date1 = $dates['value']['formatted'];
123
      $date2 = $date1;
124
      break;
125

    
126
    case 'value2':
127
      $date2 = $dates['value2']['formatted'];
128
      $date1 = $date2;
129
      break;
130

    
131
    default:
132
      $date1 = $dates['value']['formatted'];
133
      $date2 = $dates['value2']['formatted'];
134
      break;
135
  }
136

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

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

    
157
  // Check remaining days.
158
  $show_remaining_days = '';
159
  if (!empty($variables['show_remaining_days'])) {
160
    $remaining_days = floor((strtotime($variables['dates']['value']['formatted_iso'])
161
    - strtotime('now')) / (24 * 3600));
162

    
163
    // Show remaining days only for future events.
164
    if ($remaining_days >= 0) {
165
      $show_remaining_days = theme('date_display_remaining', array(
166
        'remaining_days' => $remaining_days,
167
      ));
168
    }
169
  }
170

    
171
  // No date values, display nothing.
172
  if (empty($date1) && empty($date2)) {
173
    $output .= '';
174
  }
175
  // Start and End dates match or there is no End date, display a complete
176
  // single date.
177
  elseif ($date1 == $date2 || empty($date2)) {
178
    $output .= theme('date_display_single', array(
179
      'date' => $date1,
180
      'timezone' => $timezone,
181
      'attributes' => $attributes,
182
      'rdf_mapping' => $rdf_mapping,
183
      'add_rdf' => $add_rdf,
184
      'microdata' => $microdata,
185
      'add_microdata' => $add_microdata,
186
      'dates' => $dates,
187
      'show_remaining_days' => $show_remaining_days,
188
    ));
189
  }
190
  // Same day, different times, don't repeat the date but show both Start and
191
  // End times. We can NOT do this if the replacement value is an integer
192
  // instead of a time string.
193
  elseif ($has_time_string && $dates['value']['formatted_date'] == $dates['value2']['formatted_date']) {
194
    // Replace the original time with the start/end time in the formatted start
195
    // date. Make sure that parentheses or brackets wrapping the time will be
196
    // retained in the final result.
197
    $time = theme('date_display_range', array(
198
      'date1' => $time1,
199
      'date2' => $time2,
200
      'timezone' => $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
    $replaced = str_replace($time1, $time, $date1);
210
    $output .= theme('date_display_single', array(
211
      'date' => $replaced,
212
      'timezone' => $timezone,
213
      'attributes' => array(),
214
      'rdf_mapping' => array(),
215
      'add_rdf' => FALSE,
216
      'dates' => $dates,
217
    ));
218
  }
219
  // Different days, display both in their entirety.
220
  else {
221
    $output .= theme('date_display_range', array(
222
      'date1' => $date1,
223
      'date2' => $date2,
224
      'timezone' => $timezone,
225
      'attributes' => $attributes,
226
      'rdf_mapping' => $rdf_mapping,
227
      'add_rdf' => $add_rdf,
228
      'microdata' => $microdata,
229
      'add_microdata' => $add_microdata,
230
      'dates' => $dates,
231
      'show_remaining_days' => $show_remaining_days,
232
    ));
233
  }
234

    
235
  return $output;
236
}
237

    
238
/**
239
 * Template preprocess function for displaying a single date.
240
 */
241
function template_preprocess_date_display_single(&$variables) {
242
  if ($variables['add_rdf'] || !empty($variables['add_microdata'])) {
243
    // Pass along the rdf mapping for this field, if any. Add some default rdf
244
    // attributes that will be used if not overridden by attributes passed in.
245
    $rdf_mapping = $variables['rdf_mapping'];
246
    $base_attributes = array(
247
      'property' => array('dc:date'),
248
      'datatype' => 'xsd:dateTime',
249
      'content' => $variables['dates']['value']['formatted_iso'],
250
    );
251
    $variables['attributes'] = $variables['attributes'] + $base_attributes;
252
  }
253

    
254
  // Pass along microdata attributes, or set display to false if none are set.
255
  if (!empty($variables['add_microdata'])) {
256
    // Because the Entity API integration for Date has a variable data
257
    // structure depending on whether there is an end value, the attributes
258
    // could be attached to the field or to the value property.
259
    if (!empty($variables['microdata']['#attributes']['itemprop'])) {
260
      $variables['microdata']['value']['#attributes'] = $variables['microdata']['#attributes'];
261
    }
262

    
263
    // Add the machine readable time using the content attribute.
264
    if (!empty($variables['microdata']['value']['#attributes'])) {
265
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
266
    }
267
    else {
268
      $variables['add_microdata'] = FALSE;
269
    }
270
  }
271
}
272

    
273
/**
274
 * Returns HTML for a date element formatted as a single date.
275
 */
276
function theme_date_display_single($variables) {
277
  $date = $variables['date'];
278
  $timezone = $variables['timezone'];
279
  $attributes = $variables['attributes'];
280
  $show_remaining_days = isset($variables['show_remaining_days']) ? $variables['show_remaining_days'] : '';
281

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

    
285
  if (!empty($variables['add_microdata'])) {
286
    $output .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
287
  }
288

    
289
  // Add remaining message and return.
290
  return $output . $show_remaining_days;
291
}
292

    
293
/**
294
 * Template preprocess function for displaying a range of dates.
295
 */
296
function template_preprocess_date_display_range(&$variables) {
297
  // Merge in the shared attributes for themes to use.
298
  $variables['attributes_start'] += $variables['attributes'];
299
  $variables['attributes_end'] += $variables['attributes'];
300

    
301
  if ($variables['add_rdf']) {
302
    // Pass along the rdf mapping for this field, if any. Add some default rdf
303
    // attributes that will be used if not overridden by attributes passed in.
304
    $dates = $variables['dates'];
305
    $base_attributes = array(
306
      'property' => array('dc:date'),
307
      'datatype' => 'xsd:dateTime',
308
      'content' => $dates['value']['formatted_iso'],
309
    );
310
    $variables['attributes_start'] += $base_attributes;
311
    $variables['attributes_end'] += $base_attributes;
312
    $variables['attributes_end']['content'] = $dates['value2']['formatted_iso'];
313
    foreach ($variables['attributes_end']['property'] as $delta => $property) {
314
      $variables['attributes_end']['property'][$delta] = str_replace('start', 'end', $property);
315
    }
316
  }
317

    
318
  // Pass along microdata attributes, or set display to false if none are set.
319
  if ($variables['add_microdata']) {
320
    if (!empty($variables['microdata']['value']['#attributes'])) {
321
      $variables['microdata']['value']['#attributes']['content'] = $variables['dates']['value']['formatted_iso'];
322
      $variables['microdata']['value2']['#attributes']['content'] = $variables['dates']['value2']['formatted_iso'];
323
    }
324
    else {
325
      $variables['add_microdata'] = FALSE;
326
    }
327
  }
328
}
329

    
330
/**
331
 * Returns HTML for a date element formatted as a range.
332
 */
333
function theme_date_display_range($variables) {
334
  $date1 = $variables['date1'];
335
  $date2 = $variables['date2'];
336
  $timezone = $variables['timezone'];
337
  $attributes_start = $variables['attributes_start'];
338
  $attributes_end = $variables['attributes_end'];
339
  $show_remaining_days = $variables['show_remaining_days'];
340

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

    
344
  // If microdata attributes for the start date property have been passed in,
345
  // add the microdata in meta tags.
346
  if (!empty($variables['add_microdata'])) {
347
    $start_date .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
348
    $end_date .= '<meta' . drupal_attributes($variables['microdata']['value2']['#attributes']) . '/>';
349
  }
350

    
351
  // Wrap the result with the attributes.
352
  $output = '<span class="date-display-range">' . t('!start-date to !end-date', array(
353
    '!start-date' => $start_date,
354
    '!end-date' => $end_date,
355
  )) . '</span>';
356

    
357
  // Add remaining message and return.
358
  return $output . $show_remaining_days;
359
}
360

    
361
/**
362
 * Returns HTML for a date element formatted as an interval.
363
 */
364
function theme_date_display_interval($variables) {
365
  $entity = $variables['entity'];
366
  $options = $variables['display']['settings'];
367
  $dates = $variables['dates'];
368
  $attributes = $variables['attributes'];
369

    
370
  // Get the formatter settings, either the default settings for this node type
371
  // or the View settings stored in $entity->date_info.
372
  if (!empty($entity->date_info) && !empty($entity->date_info->formatter_settings)) {
373
    $options = $entity->date_info->formatter_settings;
374
  }
375

    
376
  $time_ago_vars = array(
377
    'start_date' => $dates['value']['local']['object'],
378
    'end_date' => $dates['value2']['local']['object'],
379
    'interval' => $options['interval'],
380
    'interval_display' => $options['interval_display'],
381
    'use_end_date' => !empty($options['use_end_date']) ?
382
      $options['use_end_date'] : FALSE,
383
  );
384

    
385
  if ($return = theme('date_time_ago', $time_ago_vars)) {
386
    return '<span class="date-display-interval"' . drupal_attributes($attributes) . ">$return</span>";
387
  }
388
  else {
389
    return '';
390
  }
391
}
392

    
393
/**
394
 * Returns HTML for a start/end date combination on form.
395
 */
396
function theme_date_combo($variables) {
397
  $element = $variables['element'];
398
  $field = field_info_field($element['#field_name']);
399
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
400

    
401
  // Group start/end items together in fieldset.
402
  $fieldset = array(
403
    '#title' => field_filter_xss(t($element['#title'])) . ($element['#delta'] > 0 ? ' ' . intval($element['#delta'] + 1) : ''),
404
    '#value' => '',
405
    '#description' => !empty($element['#description']) ? $element['#description'] : '',
406
    '#attributes' => array('class' => array('date-combo')),
407
    '#children' => $element['#children'],
408
  );
409
  // Add marker to required date fields.
410
  if ($element['#required']) {
411
    $fieldset['#title'] .= " " . theme('form_required_marker');
412
  }
413
  return theme('fieldset', array('element' => $fieldset));
414
}
415

    
416
/**
417
 * Returns HTML for the text/select options for date parts in a table.
418
 */
419
function theme_date_text_parts($variables) {
420
  $element = $variables['element'];
421
  $rows = array();
422
  foreach (date_granularity_names() as $key => $part) {
423
    if ($element[$key]['#type'] == 'hidden') {
424
      $rows[] = drupal_render($element[$key]);
425
    }
426
    else {
427
      $rows[] = array(
428
        $part,
429
        drupal_render($element[$key][0]),
430
        drupal_render($element[$key][1]),
431
      );
432
    }
433
  }
434
  if ($element['year']['#type'] == 'hidden') {
435
    return implode($rows) . drupal_render_children($element);
436
  }
437
  else {
438
    $header = array(t('Date part'), t('Select list'), t('Text field'));
439
    return theme('table', array('header' => $header, 'rows' => $rows)) . drupal_render_children($element);
440
  }
441
}
442

    
443
/**
444
 * Render a date combo as a form element.
445
 */
446
function theme_date_form_element($variables) {
447
  $element = &$variables['element'];
448

    
449
  // Detect whether element is multiline.
450
  $count = preg_match_all('`<(?:div|span)\b[^>]* class="[^"]*\b(?:date-no-float|date-clear)\b`', $element['#children'], $matches, PREG_OFFSET_CAPTURE);
451
  $multiline = FALSE;
452
  if ($count > 1) {
453
    $multiline = TRUE;
454
  }
455
  elseif ($count) {
456
    $before = substr($element['#children'], 0, $matches[0][0][1]);
457
    if (preg_match('`<(?:div|span)\b[^>]* class="[^"]*\bdate-float\b`', $before)) {
458
      $multiline = TRUE;
459
    }
460
  }
461

    
462
  // Detect if there is more than one subfield.
463
  $count = count(explode('<label', $element['#children'])) - 1;
464
  if ($count == 1) {
465
    $element['#title_display'] = 'none';
466
  }
467

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

    
471
  return theme('form_element', $variables);
472
}
473

    
474
/**
475
 * Returns HTML for remaining message.
476
 */
477
function theme_date_display_remaining($variables) {
478
  $remaining_days = $variables['remaining_days'];
479
  $output = '';
480
  $show_remaining_text = t('The upcoming date less then 1 day.');
481
  if ($remaining_days) {
482
    $show_remaining_text = format_plural($remaining_days, 'To event remaining 1 day', 'To event remaining @count days');
483
  }
484

    
485
  return '<div class="date-display-remaining"><span class="date-display-remaining">' . $show_remaining_text . '</span></div>';
486
}
487

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