3
* Implementation of Devel module's hook_content_generate().
5
* Included only when needed.
7
function _date_content_generate($node, $field) {
8
include_once('./'. drupal_get_path('module', 'date_api') .'/date_api_elements.inc');
10
$node_field = array();
11
if (isset($field['widget']['year_range'])) {
12
$split = explode(':', $field['widget']['year_range']);
13
$back = str_replace('-', '', $split[0]);
14
$forward = str_replace('+', '', $split[1]);
20
// Pick a random year within the time range,
21
// and a random second within that year.
22
$year = date_format(date_now(), 'Y') - $back + mt_rand(0, ($forward + $back));
23
$start = date_make_date($year .'-01-01 00:00:00', date_get_timezone_db($field['tz_handling']));
24
$leap = date_format($start, 'L');
25
$max_days = $leap ? 366 : 365;
26
$seconds = mt_rand(0, ($max_days * 86400));
27
date_modify($start, "+$seconds seconds");
28
$increment = $field['widget']['increment'];
29
date_increment_round($start, $increment);
31
// Modify To date by 1 hour to 3 days, shorter for repeating dates
33
$start2 = drupal_clone($start);
34
$max = !empty($field['repeat']) ? 720 : 4320;
36
date_modify($start2, '+'. mt_rand(60, $max) .' minutes');
37
date_increment_round($start2, $increment);
39
if ($field['tz_handling'] == 'date') {
40
// Choose a random timezone.
41
// Not all keys exist, so we have to check.
42
$timezones = array_keys(date_timezone_names(TRUE));
43
$key = mt_rand(0, count($timezones) - 1);
44
if (!array_key_exists($key, $timezones)) {
45
$timezone = date_default_timezone_name();
48
$timezone = $timezones[$key];
52
$timezone = date_get_timezone($field['tz_handling']);
55
switch ($field['type']) {
57
$format = DATE_FORMAT_ISO;
60
$format = DATE_FORMAT_UNIX;
63
$format = DATE_FORMAT_DATETIME;
66
$node_field['value'] = date_format($start, $format);
67
if ($field['todate']) {
68
$node_field['value2'] = date_format($start2, $format);
70
date_timezone_set($start, timezone_open($timezone));
71
$node_field['timezone'] = $timezone;
72
$node_field['offset'] = date_offset_get($start);
73
date_timezone_set($start2, timezone_open($timezone));
74
$node_field['offset2'] = date_offset_get($start2);
76
if (!empty($field['repeat'])) {
77
include_once('./'. drupal_get_path('module', 'date_repeat') .'/date_repeat_calc.inc');
78
include_once('./'. drupal_get_path('module', 'date') .'/date_repeat.inc');
79
include_once('./'. drupal_get_path('module', 'date_api') .'/date_api_ical.inc');
81
// Create a repeating date.
82
$duration = date_difference($start, $start2);
83
$form_values = array();
85
// Create the default case more frequently than case 1 or 2.
86
$which = mt_rand(0, 10);
88
$intervals = array_keys(INTERVAL_options());
90
$interval = $intervals[mt_rand(1, 3)];
94
$options = array('YEARLY', 'MONTHLY');
95
$freq = date_content_generate_key($options);
96
$freq = $options[$freq];
97
$form_values['FREQ'] = $freq;
98
// Make sure we'll find a match in our range.
99
if ($freq == 'YEARLY') {
102
$form_values['BYMONTHDAY'] = array($mo);
105
$mo = mt_rand(1, 12);
106
$options = array('YEARLY', 'MONTHLY');
107
$freq = date_content_generate_key($options);
108
$freq = $options[$freq];
109
$form_values['FREQ'] = $freq;
110
// Make sure we'll find a match in our range.
111
if ($freq == 'YEARLY') {
114
$form_values['BYMONTH'] = array($mo);
117
$dows = array_keys(date_content_repeat_dow_options());
118
$day = date_content_generate_key($dows);
120
$options = array('MONTHLY', 'DAILY', 'WEEKLY');
121
$freq = date_content_generate_key($options);
122
$freq = $options[$freq];
123
$form_values['FREQ'] = $freq;
124
$form_values['BYDAY'] = array($dow);
128
$form_values['INTERVAL'] = $interval;
145
date_modify($start2, '+'. max(1, $forward) .' years');
146
date_increment_round($start2, $increment);
147
$until = date_format($start2, 'Y-m-d H:i:s');
148
$form_values['UNTIL'] = array('datetime' => $until, 'tz' => 'UTC');
149
$form_values['COUNT'] = $max_items;
151
$rrule = date_api_ical_build_rrule($form_values);
152
$values = date_repeat_build_dates($rrule, $form_values, $field, $node_field);
154
$start = $node_field;
155
$node_field = array(0 => $start);
156
$node_field[0]['rrule'] = $rrule;
157
$node_field += $values;
162
function date_content_generate_key($array) {
163
$keys = array_keys($array);
164
$min = array_shift($keys);
165
$max = array_pop($keys);
166
return mt_rand($min, $max);
170
* Helper function for BYDAY options.
172
* Creates options like -1SU and 2TU
173
* Omit options that won't find many matches, like 5th Sunday.
175
function date_content_repeat_dow_options() {
177
foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
178
foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
179
if ($count_key != 5 && $count_key != -5) {
180
$options[$count_key . $dow_key] = $count_value .' '. $dow_value;
b'\\ No newline at end of file'