Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date_repeat / date_repeat.module @ db9ffd17

1
<?php
2
/**
3
 * @file
4
 *
5
 * This module creates a form element that allows users to select
6
 * repeat rules for a date, and reworks the result into an iCal
7
 * RRULE string that can be stored in the database.
8
 *
9
 * The module also parses iCal RRULEs to create an array of dates
10
 * that meet their criteria.
11
 *
12
 * Other modules can use this API to add self-validating form elements
13
 * to their dates, and identify dates that meet the RRULE criteria.
14
 *
15
 */
16
/**
17
 * Implements hook_element_info().
18
 */
19
function date_repeat_element_info() {
20
  $type['date_repeat_rrule'] = array(
21
    '#input' => TRUE,
22
    '#process' => array('date_repeat_rrule_process'),
23
    '#element_validate' => array('date_repeat_rrule_validate'),
24
    '#theme_wrappers' => array('date_repeat_rrule'),
25
  );
26
  $type['date_repeat_form_element_radios'] = array(
27
    '#input' => TRUE,
28
    '#process' => array('date_repeat_form_element_radios_process'),
29
    '#theme_wrappers' => array('radios'),
30
    '#pre_render' => array('form_pre_render_conditional_form_element'),
31
  );
32
  if (module_exists('ctools')) {
33
    $type['date_repeat_rrule']['#pre_render'] = array('ctools_dependent_pre_render');
34
  }
35
  return $type;
36
}
37

    
38
function date_repeat_theme() {
39
  return array(
40
    'date_repeat_current_exceptions' => array('render element' => 'element'),
41
    'date_repeat_current_additions' => array('render element' => 'element'),
42
    'date_repeat_rrule' => array('render element' => 'element'),
43
  );
44
}
45

    
46
/**
47
 * Helper function for FREQ options.
48
 */
49
function date_repeat_freq_options() {
50
  return array(
51
    'DAILY' => t('Daily', array(), array('context' => 'datetime_singular')),
52
    'WEEKLY' => t('Weekly', array(), array('context' => 'datetime_singular')),
53
    'MONTHLY' => t('Monthly', array(), array('context' => 'datetime_singular')),
54
    'YEARLY' => t('Yearly', array(), array('context' => 'datetime_singular')),
55
  );
56
}
57

    
58
function date_repeat_interval_options() {
59
  $options = range(0, 366);
60
  unset($options[0]);
61

    
62
  return $options;
63
}
64

    
65
/**
66
 * Helper function for FREQ options.
67
 *
68
 * Translated and untranslated arrays of the iCal day of week names.
69
 * We need the untranslated values for date_modify(), translated
70
 * values when displayed to user.
71
 */
72
function date_repeat_dow_day_options($translated = TRUE) {
73
  return array(
74
    'SU' => $translated ? t('Sunday', array(), array('context' => 'day_name')) : 'Sunday',
75
    'MO' => $translated ? t('Monday', array(), array('context' => 'day_name')) : 'Monday',
76
    'TU' => $translated ? t('Tuesday', array(), array('context' => 'day_name')) : 'Tuesday',
77
    'WE' => $translated ? t('Wednesday', array(), array('context' => 'day_name')) : 'Wednesday',
78
    'TH' => $translated ? t('Thursday', array(), array('context' => 'day_name')) : 'Thursday',
79
    'FR' => $translated ? t('Friday', array(), array('context' => 'day_name')) : 'Friday',
80
    'SA' => $translated ? t('Saturday', array(), array('context' => 'day_name')) : 'Saturday',
81
  );
82
}
83

    
84
/**
85
 * Helper function for FREQ options.
86
 *
87
 * Translated and untranslated arrays of the iCal abbreviated day of week names.
88
 */
89
function date_repeat_dow_day_options_abbr($translated = TRUE, $length = 3) {
90
  $return = array();
91
  switch ($length) {
92
    case 1:
93
      $context = 'day_abbr1';
94
      break;
95
    case 2:
96
      $context = 'day_abbr2';
97
      break;
98
    default:
99
      $context = '';
100
      break;
101
  }
102
  foreach (date_repeat_dow_day_untranslated() as $key => $day) {
103
    $return[$key] = $translated ? t(substr($day, 0, $length), array(), array('context' => $context)) : substr($day, 0, $length);
104
  }
105
  return $return;
106
}
107

    
108
function date_repeat_dow_day_untranslated() {
109
  static $date_repeat_weekdays;
110
  if (empty($date_repeat_weekdays)) {
111
    $date_repeat_weekdays = array('SU' => 'Sunday', 'MO' => 'Monday', 'TU' => 'Tuesday',
112
      'WE' => 'Wednesday', 'TH' => 'Thursday', 'FR' => 'Friday',
113
      'SA' => 'Saturday');
114
  }
115
  return $date_repeat_weekdays;
116
}
117

    
118
function date_repeat_dow_day_options_ordered($weekdays) {
119
  $day_keys = array_keys($weekdays);
120
  $day_values = array_values($weekdays);
121
  for ($i = 1; $i <= variable_get('date_first_day', 0); $i++) {
122
    $last_key = array_shift($day_keys);
123
    array_push($day_keys, $last_key);
124
    $last_value = array_shift($day_values);
125
    array_push($day_values, $last_value);
126
  }
127
  $weekdays = array_combine($day_keys, $day_values);
128
  return $weekdays;
129
}
130

    
131
/**
132
 * Helper function for BYDAY options.
133
 */
134
function date_repeat_dow_count_options() {
135
  return array('' => t('Every', array(), array('context' => 'date_order'))) + date_order_translated();
136
}
137

    
138
/**
139
 * Helper function for BYDAY options.
140
 *
141
 * Creates options like -1SU and 2TU
142
 */
143
function date_repeat_dow_options() {
144
  $options = array();
145
  foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
146
    foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
147
      $options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
148
    }
149
  }
150
  return $options;
151
}
152

    
153
/**
154
 * Translate a day of week position to the iCal day name.
155
 *
156
 * Used with date_format($date, 'w') or get_variable('date_first_day'),
157
 * which return 0 for Sunday, 1 for Monday, etc.
158
 *
159
 * dow 2 becomes 'TU', dow 3 becomes 'WE', and so on.
160
 */
161
function date_repeat_dow2day($dow) {
162
  $days_of_week = array_keys(date_repeat_dow_day_options(FALSE));
163
  return $days_of_week[$dow];
164
}
165

    
166
/**
167
 * Shift the array of iCal day names into the right order
168
 * for a specific week start day.
169
 */
170
function date_repeat_days_ordered($week_start_day) {
171
  $days = array_flip(array_keys(date_repeat_dow_day_options(FALSE)));
172
  $start_position = $days[$week_start_day];
173
  $keys = array_flip($days);
174
  if ($start_position > 0) {
175
    for ($i = 1; $i <= $start_position; $i++) {
176
      $last = array_shift($keys);
177
      array_push($keys, $last);
178
    }
179
  }
180
  return $keys;
181
}
182

    
183
/**
184
 * Build a description of an iCal rule.
185
 *
186
 * Constructs a human-readable description of the rule.
187
 */
188
function date_repeat_rrule_description($rrule, $format = 'D M d Y') {
189
  // Empty or invalid value.
190
  if (empty($rrule) || !strstr($rrule, 'RRULE')) {
191
    return;
192
  }
193

    
194
  module_load_include('inc', 'date_api', 'date_api_ical');
195
  module_load_include('inc', 'date_repeat', 'date_repeat_calc');
196

    
197
  $parts = date_repeat_split_rrule($rrule);
198
  $additions = $parts[2];
199
  $exceptions = $parts[1];
200
  $rrule = $parts[0];
201
  if ($rrule['FREQ'] == 'NONE') {
202
    return;
203
  }
204

    
205
  // Make sure there will be an empty description for any unused parts.
206
  $description = array(
207
    '!interval' => '',
208
    '!byday' => '',
209
    '!bymonth' => '',
210
    '!count' => '',
211
    '!until' => '',
212
    '!except' => '',
213
    '!additional' => '',
214
    '!week_starts_on' => '',
215
    );
216
  $interval = date_repeat_interval_options();
217
  switch ($rrule['FREQ']) {
218
    case 'WEEKLY':
219
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every week', 'every @count weeks') . ' ';
220
      break;
221
    case 'MONTHLY':
222
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every month', 'every @count months') . ' ';
223
      break;
224
    case 'YEARLY':
225
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every year', 'every @count years') . ' ';
226
      break;
227
    default:
228
      $description['!interval'] = format_plural($rrule['INTERVAL'], 'every day', 'every @count days') . ' ';
229
      break;
230
  }
231

    
232
  if (!empty($rrule['BYDAY'])) {
233
    $days = date_repeat_dow_day_options();
234
    $counts = date_repeat_dow_count_options();
235
    $results = array();
236
    foreach ($rrule['BYDAY'] as $byday) {
237
      // Get the numeric part of the BYDAY option, i.e. +3 from +3MO.
238
      $day = substr($byday, -2);
239
      $count = str_replace($day, '', $byday);
240
      if (!empty($count)) {
241
        // See if there is a 'pretty' option for this count, i.e. +1 => First.
242
        $order = array_key_exists($count, $counts) ? strtolower($counts[$count]) : $count;
243
        $results[] = trim(t('!repeats_every_interval on the !date_order !day_of_week', array('!repeats_every_interval ' => '', '!date_order' => $order, '!day_of_week' => $days[$day])));
244
      }
245
      else {
246
        $results[] = trim(t('!repeats_every_interval every !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => $days[$day])));
247
      }
248
    }
249
    $description['!byday'] = implode(' ' . t('and') . ' ', $results);
250
  }
251
  if (!empty($rrule['BYMONTH'])) {
252
    if (sizeof($rrule['BYMONTH']) < 12) {
253
      $results = array();
254
      $months = date_month_names();
255
      foreach ($rrule['BYMONTH'] as $month) {
256
        $results[] = $months[$month];
257
      }
258
      if (!empty($rrule['BYMONTHDAY'])) {
259
        $description['!bymonth'] = trim(t('!repeats_every_interval on the !month_days of !month_names', array('!repeats_every_interval ' => '', '!month_days' => implode(', ', $rrule['BYMONTHDAY']), '!month_names' => implode(', ', $results))));
260
      }
261
      else {
262
        $description['!bymonth'] = trim(t('!repeats_every_interval on !month_names', array('!repeats_every_interval ' => '', '!month_names' => implode(', ', $results))));
263
      }
264
    }
265
  }
266
  if ($rrule['INTERVAL'] < 1) {
267
    $rrule['INTERVAL'] = 1;
268
  }
269
  if (!empty($rrule['COUNT'])) {
270
    $description['!count'] = trim(t('!repeats_every_interval !count times', array('!repeats_every_interval ' => '', '!count' => $rrule['COUNT'])));
271
  }
272
  if (!empty($rrule['UNTIL'])) {
273
    $until = date_ical_date($rrule['UNTIL'], 'UTC');
274
    date_timezone_set($until, date_default_timezone_object());
275
    $description['!until'] = trim(t('!repeats_every_interval until !until_date', array('!repeats_every_interval ' => '', '!until_date' => date_format_date($until, 'custom', $format))));
276
  }
277
  if ($exceptions) {
278
    $values = array();
279
    foreach ($exceptions as $exception) {
280
      $except = date_ical_date($exception, 'UTC');
281
      date_timezone_set($except, date_default_timezone_object());
282
      $values[] = date_format_date($except, 'custom', $format);
283
    }
284
    $description['!except'] = trim(t('!repeats_every_interval except !except_dates', array('!repeats_every_interval ' => '', '!except_dates' => implode(', ', $values))));
285
  }
286
  if (!empty($rrule['WKST'])) {
287
    $day_names = date_repeat_dow_day_options();
288
    $description['!week_starts_on'] = trim(t('!repeats_every_interval where the week start on !day_of_week', array('!repeats_every_interval ' => '', '!day_of_week' => $day_names[trim($rrule['WKST'])])));
289
  }
290
  if ($additions) {
291
    $values = array();
292
    foreach ($additions as $addition) {
293
      $add = date_ical_date($addition, 'UTC');
294
      date_timezone_set($add, date_default_timezone_object());
295
      $values[] = date_format_date($add, 'custom', $format);
296
    }
297
    $description['!additional'] = trim(t('Also includes !additional_dates.', array('!additional_dates' => implode(', ', $values))));
298
  }
299
  return t('Repeats !interval !bymonth !byday !count !until !except. !additional', $description);
300
}
301

    
302
/**
303
 * Parse an iCal rule into a parsed RRULE array and an EXDATE array.
304
 */
305
function date_repeat_split_rrule($rrule) {
306
  $parts = explode("\n", str_replace("\r\n", "\n", $rrule));
307
  $rrule = array();
308
  $exceptions = array();
309
  $additions = array();
310
  $additions = array();
311
  foreach ($parts as $part) {
312
    if (strstr($part, 'RRULE')) {
313
      $RRULE = str_replace('RRULE:', '', $part);
314
      $rrule = (array) date_ical_parse_rrule('RRULE:', $RRULE);
315
    }
316
    elseif (strstr($part, 'EXDATE')) {
317
      $EXDATE = str_replace('EXDATE:', '', $part);
318
      $exceptions = (array) date_ical_parse_exceptions('EXDATE:', $EXDATE);
319
      unset($exceptions['DATA']);
320
    }
321
    elseif (strstr($part, 'RDATE')) {
322
      $RDATE = str_replace('RDATE:', '', $part);
323
      $additions = (array) date_ical_parse_exceptions('RDATE:', $RDATE);
324
      unset($additions['DATA']);
325
    }
326
  }
327
  return array($rrule, $exceptions, $additions);
328
}
329

    
330
/**
331
 * Analyze a RRULE and return dates that match it.
332
 */
333
function date_repeat_calc($rrule, $start, $end, $exceptions = array(), $timezone = NULL, $additions = array()) {
334
  module_load_include('inc', 'date_repeat', 'date_repeat_calc');
335
  return _date_repeat_calc($rrule, $start, $end, $exceptions, $timezone, $additions);
336
}
337

    
338
/**
339
 * Generate the repeat rule setting form.
340
 */
341
function date_repeat_rrule_process($element, &$form_state, $form) {
342
  module_load_include('inc', 'date_repeat', 'date_repeat_form');
343
  return _date_repeat_rrule_process($element, $form_state, $form);
344
}
345

    
346
/**
347
 * Process function for 'date_repeat_form_element_radios'.
348
 */
349
function date_repeat_form_element_radios_process($element) {
350
  $childrenkeys = element_children($element);
351

    
352
  if (count($element['#options']) &&
353
      count($element['#options']) == count($childrenkeys)) {
354
    $weight = 0;
355
    $children = array();
356
    $classes = isset($element['#div_classes']) ?
357
      $element['#div_classes'] : array();
358
    foreach ($childrenkeys as $childkey) {
359
      $children[$childkey] = $element[$childkey];
360
      unset($element[$childkey]);
361
    }
362
    foreach ($element['#options'] as $key => $choice) {
363
      $currentchildkey = array_shift($childrenkeys);
364
      $weight += 0.001;
365
      $class = array_shift($classes);
366
      $element += array($key => array());
367
      $parents_for_id = array_merge($element['#parents'], array($key));
368
      $element[$key] += array(
369
        '#prefix' => '<div' . ($class ? " class=\"{$class}\"" : '') . '>',
370
        '#type' => 'radio',
371
        '#title' => $choice,
372
        '#title_display' => 'invisible',
373
        '#return_value' => $key,
374
        '#default_value' => isset($element['#default_value']) ?
375
          $element['#default_value'] : NULL,
376
        '#attributes' => $element['#attributes'],
377
        '#parents' => $element['#parents'],
378
        '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)),
379
        '#ajax' => isset($element['#ajax']) ? $element['ajax'] : NULL,
380
        '#weight' => $weight,
381
        '#theme_wrappers' => array(),
382
        '#suffix' => ' ',
383
      );
384

    
385
      $child = $children[$currentchildkey];
386

    
387
      $weight += 0.001;
388

    
389
      $child['#weight'] = $weight;
390
      $child['#title_display'] = 'invisible';
391
      $child['#suffix'] = (!empty($child['#suffix']) ? $child['#suffix'] : '') .
392
        '</div>';
393
      $child['#parents'] = $element['#parents'];
394
      array_pop($child['#parents']);
395
      array_push($child['#parents'], $currentchildkey);
396

    
397
      $element_prototype = element_info($child['#type']);
398
      $old_wrappers = array();
399
      if (isset($child['#theme_wrappers'])) {
400
        $old_wrappers += $child['#theme_wrappers'];
401
      }
402
      if (isset($element_prototype['#theme_wrappers'])) {
403
        $old_wrappers += $element_prototype['#theme_wrappers'];
404
      }
405

    
406
      $child['#theme_wrappers'] = array();
407

    
408
      foreach ($old_wrappers as $wrapper) {
409
        if ($wrapper != 'form_element') {
410
          $child['#theme_wrappers'][] = $wrapper;
411
        }
412
      }
413

    
414
      $element[$currentchildkey] = $child;
415
    }
416
  }
417

    
418
  return $element;
419
}