Projet

Général

Profil

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

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

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
    case 'datestamp':
46
      $format = DATE_FORMAT_UNIX;
47
      break;
48
    case 'datetime':
49
      $format = DATE_FORMAT_DATETIME;
50
      break;
51
  }
52

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

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

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

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

    
65
  $intervals = array_keys(date_repeat_interval_options());
66
  unset($intervals[0]);
67
  $interval = $intervals[mt_rand(1, 3)];
68
  switch ($which) {
69
    case 1:
70
      $mo = mt_rand(1, 28);
71
      $options = array('YEARLY', 'MONTHLY');
72
      $freq = date_content_generate_key($options);
73
      $freq = $options[$freq];
74
      $form_values['FREQ'] = $freq;
75
      // Make sure we'll find a match in our range.
76
      if ($freq == 'YEARLY') {
77
        $interval = 1;
78
      }
79
      $form_values['BYMONTHDAY'] = array($mo);
80
      break;
81
    case 2:
82
      $mo = mt_rand(1, 12);
83
      $options = array('YEARLY', 'MONTHLY');
84
      $freq = date_content_generate_key($options);
85
      $freq = $options[$freq];
86
      $form_values['FREQ'] = $freq;
87
      // Make sure we'll find a match in our range.
88
      if ($freq == 'YEARLY') {
89
        $interval = 1;
90
      }
91
      $form_values['BYMONTH'] = array($mo);
92
      break;
93
    default:
94
      $dows = array_keys(date_content_repeat_dow_options());
95
      $day = date_content_generate_key($dows);
96
      $dow = $dows[$day];
97
      $options = array('MONTHLY', 'DAILY', 'WEEKLY');
98
      $freq = date_content_generate_key($options);
99
      $freq = $options[$freq];
100
      $form_values['FREQ'] = $freq;
101
      $form_values['BYDAY'] = array($dow);
102
      break;
103
  }
104

    
105
  $form_values['INTERVAL'] = $interval;
106

    
107
  switch ($freq) {
108
    case 'YEARLY':
109
      $period = 'year';
110
      break;
111
    case 'MONTHLY':
112
      $period = 'month';
113
      break;
114
    case 'WEEKLY':
115
      $period = 'week';
116
      break;
117
    default:
118
      $period = 'day';
119
      break;
120

    
121
  }
122

    
123
  $form_values['UNTIL'] = array();
124
  $form_values['COUNT'] = $max_items;
125

    
126
  $rrule = date_api_ical_build_rrule($form_values);
127
  $items[0]['rrule'] = $rrule;
128

    
129
  $values = date_repeat_build_dates($rrule, $form_values, $field, $item);
130

    
131
  $items += $values;
132

    
133
}
134

    
135
function date_content_generate_key($array) {
136
  $keys = array_keys($array);
137
  $min = array_shift($keys);
138
  $max = array_pop($keys);
139
  return mt_rand($min, $max);
140
}
141

    
142
/**
143
 * Helper function for BYDAY options.
144
 *
145
 * Creates options like -1SU and 2TU
146
 * Omit options that won't find many matches, like 5th Sunday.
147
 */
148
function date_content_repeat_dow_options() {
149
  $options = array();
150
  foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
151
    foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
152
      if ($count_key != 5 && $count_key != -5) {
153
        $options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
154
      }
155
    }
156
  }
157
  return $options;
158
}