date_repeat_field.devel_generate.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @file
  4. * Handling of devel generate functionality for repeating dates.
  5. */
  6. /**
  7. * Implements hook_date_field_insert().
  8. *
  9. * A substitute for hook_devel_generate to handle repeating dates.
  10. */
  11. function date_repeat_field_date_field_insert(&$items, $context) {
  12. $entity_type = $context['entity_type'];
  13. $entity = $context['entity'];
  14. $field = $context['field'];
  15. $instance = $context['instance'];
  16. $langcode = $context['langcode'];
  17. // The first value was already created by the regular Devel Generate code.
  18. // Skipping doing anything if there is no value for this field.
  19. if (empty($items)) {
  20. return;
  21. }
  22. $item = $items[0];
  23. // Unset any previous dates past the first one.
  24. $count = count($items);
  25. for ($i = 1; $i < $count; $i++) {
  26. unset($items[$i]);
  27. }
  28. // Compute repeating date values.
  29. module_load_include('inc', 'date_repeat', 'date_repeat_calc');
  30. module_load_include('inc', 'date_api', 'date_api_ical');
  31. $increment = $instance['widget']['settings']['increment'];
  32. $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
  33. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  34. switch ($field['type']) {
  35. case 'date':
  36. $format = DATE_FORMAT_ISO;
  37. break;
  38. case 'datestamp':
  39. $format = DATE_FORMAT_UNIX;
  40. break;
  41. case 'datetime':
  42. $format = DATE_FORMAT_DATETIME;
  43. break;
  44. }
  45. $start = new dateObject($item['value'], $timezone_db, $format);
  46. $start2 = new dateObject($item['value2'], $timezone_db, $format);
  47. // Create a repeating date rule.
  48. $duration = $start->difference($start2);
  49. $form_values = array();
  50. // Create the default case more frequently than case 1 or 2.
  51. $which = mt_rand(0, 10);
  52. $max_items = mt_rand(3, 10);
  53. $intervals = array_keys(date_repeat_interval_options());
  54. unset($intervals[0]);
  55. $interval = $intervals[mt_rand(1, 3)];
  56. switch ($which) {
  57. case 1:
  58. $mo = mt_rand(1, 28);
  59. $options = array('YEARLY', 'MONTHLY');
  60. $freq = date_content_generate_key($options);
  61. $freq = $options[$freq];
  62. $form_values['FREQ'] = $freq;
  63. // Make sure we'll find a match in our range.
  64. if ($freq == 'YEARLY') {
  65. $interval = 1;
  66. }
  67. $form_values['BYMONTHDAY'] = array($mo);
  68. break;
  69. case 2:
  70. $mo = mt_rand(1, 12);
  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['BYMONTH'] = array($mo);
  80. break;
  81. default:
  82. $dows = array_keys(date_content_repeat_dow_options());
  83. $day = date_content_generate_key($dows);
  84. $dow = $dows[$day];
  85. $options = array('MONTHLY', 'DAILY', 'WEEKLY');
  86. $freq = date_content_generate_key($options);
  87. $freq = $options[$freq];
  88. $form_values['FREQ'] = $freq;
  89. $form_values['BYDAY'] = array($dow);
  90. break;
  91. }
  92. $form_values['INTERVAL'] = $interval;
  93. switch ($freq) {
  94. case 'YEARLY':
  95. $period = 'year';
  96. break;
  97. case 'MONTHLY':
  98. $period = 'month';
  99. break;
  100. case 'WEEKLY':
  101. $period = 'week';
  102. break;
  103. default:
  104. $period = 'day';
  105. break;
  106. }
  107. $form_values['UNTIL'] = array();
  108. $form_values['COUNT'] = $max_items;
  109. $rrule = date_api_ical_build_rrule($form_values);
  110. $items[0]['rrule'] = $rrule;
  111. $values = date_repeat_build_dates($field, $item, $rrule, $form_values);
  112. $items += $values;
  113. }
  114. /**
  115. * Generate a random content keys.
  116. */
  117. function date_content_generate_key($array) {
  118. $keys = array_keys($array);
  119. $min = array_shift($keys);
  120. $max = array_pop($keys);
  121. return mt_rand($min, $max);
  122. }
  123. /**
  124. * Helper function for BYDAY options.
  125. *
  126. * Creates options like -1SU and 2TU
  127. * Omit options that won't find many matches, like 5th Sunday.
  128. */
  129. function date_content_repeat_dow_options() {
  130. $options = array();
  131. foreach (date_repeat_dow_count_options() as $count_key => $count_value) {
  132. foreach (date_repeat_dow_day_options() as $dow_key => $dow_value) {
  133. if ($count_key != 5 && $count_key != -5) {
  134. $options[$count_key . $dow_key] = $count_value . ' ' . $dow_value;
  135. }
  136. }
  137. }
  138. return $options;
  139. }