Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Handling of devel generate functionality for repeating dates.
6
 */
7

    
8
/**
9
 * Implements hook_date_field_insert().
10
 */
11
function date_repeat_field_date_field_insert(&$items, $context) {
12
  // A substitute for hook_devel_generate to handle repeating dates.
13
  $entity_type = $context['entity_type'];
14
  $entity = $context['entity'];
15
  $field = $context['field'];
16
  $instance = $context['instance'];
17
  $langcode = $context['langcode'];
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
107
  $form_values['INTERVAL'] = $interval;
108

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

    
114
    case 'MONTHLY':
115
      $period = 'month';
116
      break;
117

    
118
    case 'WEEKLY':
119
      $period = 'week';
120
      break;
121

    
122
    default:
123
      $period = 'day';
124
  }
125

    
126
  $form_values['UNTIL'] = array();
127
  $form_values['COUNT'] = $max_items;
128

    
129
  $rrule = date_api_ical_build_rrule($form_values);
130
  $items[0]['rrule'] = $rrule;
131

    
132
  $values = date_repeat_build_dates($field, $item, $rrule, $form_values);
133

    
134
  $items += $values;
135
}
136

    
137
/**
138
 * Generate a random content keys.
139
 *
140
 * @return string
141
 *   A random string generated by mt_rand().
142
 */
143
function date_content_generate_key($array) {
144
  $keys = array_keys($array);
145
  $min = array_shift($keys);
146
  $max = array_pop($keys);
147
  return mt_rand($min, $max);
148
}
149

    
150
/**
151
 * Helper function for BYDAY options.
152
 *
153
 * @return array
154
 *   Creates options like -1SU and 2TU. Omits options that won't find many
155
 *   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
}