View source
<?php
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\date_api\DateiCalParse;
use Drupal\date_repeat\DateRRuleCalc;
function date_repeat_field_date_field_insert(&$items, $context) {
$entity_type = $context['entity_type'];
$entity = $context['entity'];
$field = $context['field'];
$instance = $context['instance'];
$langcode = $context['langcode'];
if (empty($items)) {
return;
}
$item = $items[0];
$count = count($items);
for ($i = 1; $i < $count; $i++) {
unset($items[$i]);
}
$increment = $instance['widget']['settings']['increment'];
$timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
$format = DATE_FORMAT_ISO;
$start = new DrupalDateTime($item['value'], $timezone_db, $format);
$start2 = new DrupalDateTime($item['value2'], $timezone_db, $format);
$form_values = array();
$which = mt_rand(0, 10);
$max_items = mt_rand(3, 10);
$intervals = array_keys(date_repeat_interval_options());
unset($intervals[0]);
$interval = $intervals[mt_rand(1, 3)];
switch ($which) {
case 1:
$mo = mt_rand(1, 28);
$options = array(
'YEARLY',
'MONTHLY',
);
$freq = date_content_generate_key($options);
$freq = $options[$freq];
$form_values['FREQ'] = $freq;
if ($freq == 'YEARLY') {
$interval = 1;
}
$form_values['BYMONTHDAY'] = array(
$mo,
);
break;
case 2:
$mo = mt_rand(1, 12);
$options = array(
'YEARLY',
'MONTHLY',
);
$freq = date_content_generate_key($options);
$freq = $options[$freq];
$form_values['FREQ'] = $freq;
if ($freq == 'YEARLY') {
$interval = 1;
}
$form_values['BYMONTH'] = array(
$mo,
);
break;
default:
$dows = array_keys(date_content_repeat_dow_options());
$day = date_content_generate_key($dows);
$dow = $dows[$day];
$options = array(
'MONTHLY',
'DAILY',
'WEEKLY',
);
$freq = date_content_generate_key($options);
$freq = $options[$freq];
$form_values['FREQ'] = $freq;
$form_values['BYDAY'] = array(
$dow,
);
break;
}
$form_values['INTERVAL'] = $interval;
switch ($freq) {
case 'YEARLY':
$period = 'year';
break;
case 'MONTHLY':
$period = 'month';
break;
case 'WEEKLY':
$period = 'week';
break;
default:
$period = 'day';
break;
}
$form_values['UNTIL'] = array();
$form_values['COUNT'] = $max_items;
$rrule = date_api_ical_build_rrule($form_values);
$items[0]['rrule'] = $rrule;
$values = date_repeat_build_dates($rrule, $form_values, $field, $item);
$items += $values;
}
function date_content_generate_key($array) {
$keys = array_keys($array);
$min = array_shift($keys);
$max = array_pop($keys);
return mt_rand($min, $max);
}
function date_content_repeat_dow_options() {
$options = array();
foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
if ($count_key != 5 && $count_key != -5) {
$options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
}
}
}
return $options;
}