Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / date_repeat_field / date_repeat_field.devel_generate.inc @ b720ea3e

1
<?php
2
/**
3
 * @file
4
 * Handling of devel generate functionality for repeating dates.
5
 */
6

    
7
/**
8
 * Implements hook_date_field_insert().
9
 *
10
 * A substitute for hook_devel_generate to handle repeating dates.
11
 */
12
function date_repeat_field_date_field_insert(&$items, $context) {
13

    
14
  $entity_type = $context['entity_type'];
15
  $entity = $context['entity'];
16
  $field = $context['field'];
17
  $instance = $context['instance'];
18
  $langcode = $context['langcode'];
19

    
20
  // The first value was already created by the regular Devel Generate code.
21
  // Skipping doing anything if there is no value for this field.
22
  if (empty($items)) {
23
    return;
24
  }
25
  $item = $items[0];
26

    
27
  // Unset any previous dates past the first one.
28
  $count = count($items);
29
  for ($i = 1; $i < $count; $i++) {
30
    unset($items[$i]);
31
  }
32

    
33
  // Compute repeating date values.
34
  module_load_include('inc', 'date_repeat', 'date_repeat_calc');
35
  module_load_include('inc', 'date_api', 'date_api_ical');
36

    
37
  $increment = $instance['widget']['settings']['increment'];
38
  $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
39
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
40

    
41
  switch ($field['type']) {
42
    case 'date':
43
      $format = DATE_FORMAT_ISO;
44
      break;
45

    
46
    case 'datestamp':
47
      $format = DATE_FORMAT_UNIX;
48
      break;
49

    
50
    case 'datetime':
51
      $format = DATE_FORMAT_DATETIME;
52
      break;
53
  }
54

    
55
  $start = new dateObject($item['value'], $timezone_db, $format);
56
  $start2 = new dateObject($item['value2'], $timezone_db, $format);
57

    
58
  // Create a repeating date rule.
59
  $duration = $start->difference($start2);
60
  $form_values = array();
61

    
62
  // Create the default case more frequently than case 1 or 2.
63
  $which = mt_rand(0, 10);
64

    
65
  $max_items = mt_rand(3, 10);
66

    
67
  $intervals = array_keys(date_repeat_interval_options());
68
  unset($intervals[0]);
69
  $interval = $intervals[mt_rand(1, 3)];
70
  switch ($which) {
71
    case 1:
72
      $mo = mt_rand(1, 28);
73
      $options = array('YEARLY', 'MONTHLY');
74
      $freq = date_content_generate_key($options);
75
      $freq = $options[$freq];
76
      $form_values['FREQ'] = $freq;
77
      // Make sure we'll find a match in our range.
78
      if ($freq == 'YEARLY') {
79
        $interval = 1;
80
      }
81
      $form_values['BYMONTHDAY'] = array($mo);
82
      break;
83

    
84
    case 2:
85
      $mo = mt_rand(1, 12);
86
      $options = array('YEARLY', 'MONTHLY');
87
      $freq = date_content_generate_key($options);
88
      $freq = $options[$freq];
89
      $form_values['FREQ'] = $freq;
90
      // Make sure we'll find a match in our range.
91
      if ($freq == 'YEARLY') {
92
        $interval = 1;
93
      }
94
      $form_values['BYMONTH'] = array($mo);
95
      break;
96

    
97
    default:
98
      $dows = array_keys(date_content_repeat_dow_options());
99
      $day = date_content_generate_key($dows);
100
      $dow = $dows[$day];
101
      $options = array('MONTHLY', 'DAILY', 'WEEKLY');
102
      $freq = date_content_generate_key($options);
103
      $freq = $options[$freq];
104
      $form_values['FREQ'] = $freq;
105
      $form_values['BYDAY'] = array($dow);
106
      break;
107
  }
108

    
109
  $form_values['INTERVAL'] = $interval;
110

    
111
  switch ($freq) {
112
    case 'YEARLY':
113
      $period = 'year';
114
      break;
115

    
116
    case 'MONTHLY':
117
      $period = 'month';
118
      break;
119

    
120
    case 'WEEKLY':
121
      $period = 'week';
122
      break;
123

    
124
    default:
125
      $period = 'day';
126
      break;
127
  }
128

    
129
  $form_values['UNTIL'] = array();
130
  $form_values['COUNT'] = $max_items;
131

    
132
  $rrule = date_api_ical_build_rrule($form_values);
133
  $items[0]['rrule'] = $rrule;
134

    
135
  $values = date_repeat_build_dates($field, $item, $rrule, $form_values);
136

    
137
  $items += $values;
138

    
139
}
140

    
141
/**
142
 * Generate a random content keys.
143
 */
144
function date_content_generate_key($array) {
145
  $keys = array_keys($array);
146
  $min = array_shift($keys);
147
  $max = array_pop($keys);
148
  return mt_rand($min, $max);
149
}
150

    
151
/**
152
 * Helper function for BYDAY options.
153
 *
154
 * Creates options like -1SU and 2TU
155
 * Omit options that won't find many matches, like 5th Sunday.
156
 */
157
function date_content_repeat_dow_options() {
158
  $options = array();
159
  foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
160
    foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
161
      if ($count_key != 5 && $count_key != -5) {
162
        $options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
163
      }
164
    }
165
  }
166
  return $options;
167
}