PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Calendar/RepeatEvents.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 180 lines | 133 code | 14 blank | 33 comment | 35 complexity | ffa050a7633f53e14c9ac8121e479e80 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*********************************************************************************
  3. ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. *
  10. ********************************************************************************/
  11. /**
  12. * Class to handle repeating events
  13. */
  14. class Calendar_RepeatEvents {
  15. /**
  16. * Get timing using YYYY-MM-DD HH:MM:SS input string.
  17. */
  18. static function mktime($fulldateString) {
  19. $splitpart = self::splittime($fulldateString);
  20. $datepart = split('-', $splitpart[0]);
  21. $timepart = split(':', $splitpart[1]);
  22. return mktime($timepart[0], $timepart[1], 0, $datepart[1], $datepart[2], $datepart[0]);
  23. }
  24. /**
  25. * Increment the time by interval and return value in YYYY-MM-DD HH:MM format.
  26. */
  27. static function nexttime($basetiming, $interval) {
  28. return date('Y-m-d H:i', strtotime($interval, $basetiming));
  29. }
  30. /**
  31. * Based on user time format convert the YYYY-MM-DD HH:MM value.
  32. */
  33. static function formattime($timeInYMDHIS) {
  34. global $current_user;
  35. $format_string = 'Y-m-d H:i';
  36. switch($current_user->date_format) {
  37. case 'dd-mm-yyyy': $format_string = 'd-m-Y H:i'; break;
  38. case 'mm-dd-yyyy': $format_string = 'm-d-Y H:i'; break;
  39. case 'yyyy-mm-dd': $format_string = 'Y-m-d H:i'; break;
  40. }
  41. return date($format_string, self::mktime($timeInYMDHIS));
  42. }
  43. /**
  44. * Split full timing into date and time part.
  45. */
  46. static function splittime($fulltiming) {
  47. return split(' ', $fulltiming);
  48. }
  49. /**
  50. * Calculate the time interval to create repeated event entries.
  51. */
  52. static function getRepeatInterval($type, $frequency, $recurringInfo, $start_date, $limit_date) {
  53. $repeatInterval = Array();
  54. $starting = self::mktime($start_date);
  55. $limiting = self::mktime($limit_date);
  56. if($type == 'Daily') {
  57. $count = 0;
  58. while(true) {
  59. ++$count;
  60. $interval = ($count * $frequency);
  61. if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  62. break;
  63. }
  64. $repeatInterval[] = $interval;
  65. }
  66. } else if($type == 'Weekly') {
  67. if($recurringInfo->dayofweek_to_rpt == null) {
  68. $count = 0;
  69. $weekcount = 7;
  70. while(true) {
  71. ++$count;
  72. $interval = $count * $weekcount;
  73. if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  74. break;
  75. }
  76. $repeatInterval[] = $interval;
  77. }
  78. } else {
  79. $count = 0;
  80. while(true) {
  81. ++$count;
  82. $interval = $count;
  83. $new_timing = self::mktime(self::nexttime($starting, "+$interval days"));
  84. $new_timing_dayofweek = date('N', $new_timing);
  85. if($new_timing > $limiting) {
  86. break;
  87. }
  88. if(in_array($new_timing_dayofweek-1, $recurringInfo->dayofweek_to_rpt)) {
  89. $repeatInterval[] = $interval;
  90. }
  91. }
  92. }
  93. } else if($type == 'Monthly') {
  94. $count = 0;
  95. $avg_monthcount = 30; // TODO: We need to handle month increments precisely!
  96. while(true) {
  97. ++$count;
  98. $interval = $count * $avg_monthcount;
  99. if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  100. break;
  101. }
  102. $repeatInterval[] = $interval;
  103. }
  104. } else if($type == 'Yearly') {
  105. $count = 0;
  106. $avg_monthcount = 30;
  107. while(true) {
  108. ++$count;
  109. $interval = $count * $avg_monthcount;
  110. if(self::mktime(self::nexttime($starting, "+$interval days")) > $limiting) {
  111. break;
  112. }
  113. $repeatInterval[] = $interval;
  114. }
  115. }
  116. return $repeatInterval;
  117. }
  118. /**
  119. * Repeat Activity instance till given limit.
  120. */
  121. static function repeat($focus, $recurObj) {
  122. $frequency = $recurObj->recur_freq;
  123. $repeattype= $recurObj->recur_type;
  124. $base_focus = new Activity();
  125. $base_focus->column_fields = $focus->column_fields;
  126. $base_focus->id = $focus->id;
  127. $skip_focus_fields = Array ('record_id', 'createdtime', 'modifiedtime', 'recurringtype');
  128. /** Create instance before and reuse */
  129. $new_focus = new Activity();
  130. $eventStartDate = $focus->column_fields['date_start'];
  131. $interval = strtotime($focus->column_fields['due_date']) -
  132. strtotime($focus->column_fields['date_start']);
  133. foreach ($recurObj->recurringdates as $index => $startDate) {
  134. if($index == 0 && $eventStartDate == $startDate) {
  135. continue;
  136. }
  137. $startDateTimestamp = strtotime($startDate);
  138. $endDateTime = $startDateTimestamp + $interval;
  139. $endDate = date('Y-m-d', $endDateTime);
  140. // Reset the new_focus and prepare for reuse
  141. if(isset($new_focus->id)) unset($new_focus->id);
  142. $new_focus->column_fields = array();
  143. foreach($base_focus->column_fields as $key=>$value) {
  144. if(in_array($key, $skip_focus_fields)) {
  145. // skip copying few fields
  146. } else if($key == 'date_start') {
  147. $new_focus->column_fields['date_start'] = $startDate;
  148. } else if($key == 'due_date') {
  149. $new_focus->column_fields['due_date'] = $endDate;
  150. } else {
  151. $new_focus->column_fields[$key] = $value;
  152. }
  153. }
  154. if($numberOfRepeats > 10 && $index > 10) {
  155. unset($new_focus->column_fields['sendnotification']);
  156. }
  157. $new_focus->save('Calendar');
  158. }
  159. }
  160. static function repeatFromRequest($focus) {
  161. global $log, $default_charset, $current_user;
  162. $recurObj = getrecurringObjValue();
  163. self::repeat($focus, $recurObj);
  164. }
  165. }
  166. ?>