1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
b720ea3e
|
Assos Assos
|
/**
|
4 |
85ad3d82
|
Assos Assos
|
* @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 |
599a39cd
|
Assos Assos
|
// A substitute for hook_devel_generate to handle repeating dates.
|
13 |
85ad3d82
|
Assos Assos
|
$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 |
b720ea3e
|
Assos Assos
|
|
45 |
85ad3d82
|
Assos Assos
|
case 'datestamp':
|
46 |
|
|
$format = DATE_FORMAT_UNIX;
|
47 |
|
|
break;
|
48 |
b720ea3e
|
Assos Assos
|
|
49 |
85ad3d82
|
Assos Assos
|
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 |
b720ea3e
|
Assos Assos
|
|
83 |
85ad3d82
|
Assos Assos
|
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 |
b720ea3e
|
Assos Assos
|
|
96 |
85ad3d82
|
Assos Assos
|
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 |
b720ea3e
|
Assos Assos
|
|
114 |
85ad3d82
|
Assos Assos
|
case 'MONTHLY':
|
115 |
|
|
$period = 'month';
|
116 |
|
|
break;
|
117 |
b720ea3e
|
Assos Assos
|
|
118 |
85ad3d82
|
Assos Assos
|
case 'WEEKLY':
|
119 |
|
|
$period = 'week';
|
120 |
|
|
break;
|
121 |
b720ea3e
|
Assos Assos
|
|
122 |
85ad3d82
|
Assos Assos
|
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 |
b720ea3e
|
Assos Assos
|
$values = date_repeat_build_dates($field, $item, $rrule, $form_values);
|
133 |
85ad3d82
|
Assos Assos
|
|
134 |
|
|
$items += $values;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
b720ea3e
|
Assos Assos
|
/**
|
138 |
|
|
* Generate a random content keys.
|
139 |
599a39cd
|
Assos Assos
|
*
|
140 |
|
|
* @return string
|
141 |
|
|
* A random string generated by mt_rand().
|
142 |
b720ea3e
|
Assos Assos
|
*/
|
143 |
85ad3d82
|
Assos Assos
|
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 |
599a39cd
|
Assos Assos
|
* @return array
|
154 |
|
|
* Creates options like -1SU and 2TU. Omits options that won't find many
|
155 |
|
|
* matches, like 5th Sunday.
|
156 |
85ad3d82
|
Assos Assos
|
*/
|
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 |
b720ea3e
|
Assos Assos
|
} |