62933949 |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | CiviCRM version 4.4 | |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | |
9 | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
11 | | under the terms of the GNU Affero General Public License | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
13 | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
17 | | See the GNU Affero General Public License for more details. | |
18 | | | |
19 | | You should have received a copy of the GNU Affero General Public | |
20 | | License and the CiviCRM Licensing Exception along | |
21 | | with this program; if not, contact CiviCRM LLC | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
25 | +--------------------------------------------------------------------+ |
26 | */ |
27 | |
28 | /** |
29 | * |
30 | * @package CRM |
31 | * @copyright CiviCRM LLC (c) 2004-2013 |
32 | * $Id$ |
33 | * |
34 | */ |
35 | |
36 | require_once 'packages/When/When.php'; |
37 | |
38 | class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity { |
39 | |
05121fdd |
40 | public $schedule = array(); |
41 | public $scheduleId = NULL; |
42 | public $scheduleDBParams = array(); |
72e95527 |
43 | |
05121fdd |
44 | public $dateColumns = array(); |
45 | public $overwriteColumns = array(); |
46 | public $intervalDateColumns = array(); |
72e95527 |
47 | |
05121fdd |
48 | public $excludeDates = array(); |
72e95527 |
49 | |
05121fdd |
50 | public $recursion = NULL; |
c09936e1 |
51 | public $recursionCounter = NULL; |
72e95527 |
52 | |
05121fdd |
53 | public $isGenRecurringEntity = TRUE; |
72e95527 |
54 | |
62933949 |
55 | static $_tableDAOMapper = |
56 | array( |
50b39b5b |
57 | 'civicrm_event' => 'CRM_Event_DAO_Event', |
62933949 |
58 | 'civicrm_price_set_entity' => 'CRM_Price_DAO_PriceSetEntity', |
59 | 'civicrm_uf_join' => 'CRM_Core_DAO_UFJoin', |
60 | 'civicrm_tell_friend' => 'CRM_Friend_DAO_Friend', |
61 | 'civicrm_pcp_block' => 'CRM_PCP_DAO_PCPBlock', |
50b39b5b |
62 | 'civicrm_activity' => 'CRM_Activity_DAO_Activity', |
62933949 |
63 | ); |
64 | |
65 | static function add(&$params) { |
66 | if (CRM_Utils_Array::value('id', $params)) { |
67 | CRM_Utils_Hook::pre('edit', 'RecurringEntity', $params['id'], $params); |
68 | } |
69 | else { |
70 | CRM_Utils_Hook::pre('create', 'RecurringEntity', NULL, $params); |
71 | } |
72 | |
73 | $daoRecurringEntity = new CRM_Core_DAO_RecurringEntity(); |
74 | $daoRecurringEntity->copyValues($params); |
75 | $result = $daoRecurringEntity->save(); |
76 | |
77 | if (CRM_Utils_Array::value('id', $params)) { |
78 | CRM_Utils_Hook::post('edit', 'RecurringEntity', $daoRecurringEntity->id, $daoRecurringEntity); |
79 | } |
80 | else { |
81 | CRM_Utils_Hook::post('create', 'RecurringEntity', $daoRecurringEntity->id, $daoRecurringEntity); |
82 | } |
83 | return $result; |
84 | } |
85 | |
86 | static function quickAdd($parentId, $entityId, $entityTable) { |
87 | $params = |
88 | array( |
89 | 'parent_id' => $parentId, |
90 | 'entity_id' => $entityId, |
91 | 'entity_table' => $entityTable |
92 | ); |
93 | return self::add($params); |
94 | } |
95 | |
05121fdd |
96 | function mode($mode) { |
72e95527 |
97 | $this->mode = $mode; |
98 | $this->parent_id = $this->entity_id; |
99 | $this->save(); |
100 | } |
101 | |
102 | // generate all new entities based on object vars |
103 | function generate() { |
104 | // fixme: check if entityid & entitytable set |
c09936e1 |
105 | $this->generateRecursiveDates(); |
106 | CRM_Core_Error::debug_var('$this->recursionDates1', $this->recursionDates); |
107 | |
108 | return $this->generateEntities(); |
109 | } |
110 | |
111 | function generateRecursion2() { |
112 | // return if already generated |
113 | if (is_a($this->recursion, 'When')) { |
114 | return $this->recursion; |
115 | } |
72e95527 |
116 | |
117 | if ($this->scheduleId) { |
118 | // get params by ID |
119 | $this->recursion = $this->getRecursionFromReminder($this->scheduleId); |
120 | } else if (!empty($this->schedule)) { |
121 | $this->scheduleDBParams = $this->mapFormValuesToDB($this->schedule);//call using obj |
122 | } |
c09936e1 |
123 | |
72e95527 |
124 | CRM_Core_Error::debug_var('$this->scheduleDBParams', $this->scheduleDBParams); |
125 | if (!empty($this->scheduleDBParams)) { |
126 | $this->recursion = $this->getRecursionFromReminderByDBParams($this->scheduleDBParams); |
127 | } |
c09936e1 |
128 | return $this->recursion; |
129 | } |
72e95527 |
130 | |
c09936e1 |
131 | // generate new DAOs and along with entries in recurring_entity table |
132 | function generateEntities() { |
133 | $newEntities = array(); |
134 | if (!empty($this->recursionDates)) { |
72e95527 |
135 | // save an entry with initiating entity-id & entity-table |
136 | if (!$this->find(TRUE)) { |
137 | $this->parent_id = $this->entity_id; |
138 | $this->save(); |
139 | } |
72e95527 |
140 | |
c09936e1 |
141 | foreach ($this->recursionDates as $key => $dateCols) { |
142 | $newCriteria = $dateCols; |
143 | foreach ($this->overwriteColumns as $col => $val) { |
144 | $newCriteria[$col] = $val; |
145 | } |
146 | CRM_Core_Error::debug_var('$newCriteria', $newCriteria); |
147 | $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table, |
148 | array('id' => $this->entity_id), |
149 | $newCriteria, |
150 | $this->isGenRecurringEntity |
151 | ); |
152 | $newEntities[] = $obj->id; |
72e95527 |
153 | } |
72e95527 |
154 | } |
155 | return $newEntities; |
156 | } |
157 | |
c09936e1 |
158 | function generateRecursiveDates() { |
159 | $this->generateRecursion2(); |
160 | |
161 | $recursionDates = array(); |
72e95527 |
162 | if (is_a($this->recursion, 'When')) { |
163 | $initialCount = CRM_Utils_Array::value('start_action_offset', $this->scheduleDBParams); |
c09936e1 |
164 | |
165 | $exRangeStart = $exRangeEnd = NULL; |
166 | if (!empty($this->excludeDateRangeColumns)) { |
167 | $exRangeStart = $this->excludeDateRangeColumns[0]; |
168 | $exRangeEnd = $this->excludeDateRangeColumns[1]; |
169 | } |
72e95527 |
170 | |
171 | $count = 1; |
c09936e1 |
172 | CRM_Core_Error::debug_var('$this->intervalDateColumns', $this->intervalDateColumns); |
72e95527 |
173 | while ($result = $this->recursion->next()) { |
c09936e1 |
174 | $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); |
175 | |
176 | foreach ($this->dateColumns as $col) { |
177 | $recursionDates[$count][$col] = $baseDate; |
178 | } |
179 | foreach ($this->intervalDateColumns as $col => $interval) { |
180 | $newDate = new DateTime($baseDate); |
181 | $newDate->add($interval); |
182 | $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s')); |
183 | } |
184 | if ($exRangeStart) { |
185 | $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd'); |
186 | $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd'); |
187 | } |
72e95527 |
188 | |
189 | $skip = FALSE; |
c09936e1 |
190 | foreach ($this->excludeDates as $exDate) { |
191 | $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd'); |
192 | if (!$exRangeStart) { |
193 | if ($exDate == $result->format('Ymd')) { |
194 | $skip = TRUE; |
195 | break; |
196 | } |
197 | } else { |
198 | if (($exDate == $exRangeStartDate) || |
199 | ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate)) |
200 | ) { |
201 | $skip = TRUE; |
202 | break; |
203 | } |
72e95527 |
204 | } |
205 | } |
206 | |
207 | if ($skip) { |
c09936e1 |
208 | unset($recursionDates[$count]); |
72e95527 |
209 | if ($initialCount && ($initialCount > 0)) { |
210 | // lets increase the counter, so we get correct number of occurrences |
211 | $initialCount++; |
212 | $this->recursion->count($initialCount); |
c09936e1 |
213 | $this->recursionCounter = $initialCount; |
72e95527 |
214 | } |
215 | continue; |
216 | } |
217 | $count++; |
218 | } |
219 | } |
c09936e1 |
220 | $this->recursionDates = $recursionDates; |
221 | CRM_Core_Error::debug_var('$recursionDates', $recursionDates); |
222 | |
223 | return $recursionDates; |
72e95527 |
224 | } |
225 | |
36b8b5f3 |
226 | // MODE = 3 (ALL) |
227 | static public function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) { |
62933949 |
228 | $entities = array(); |
229 | |
36b8b5f3 |
230 | if (!$initiatorId) { |
231 | $initiatorId = $parentId; |
232 | } |
233 | |
234 | $queryParams = array( |
235 | 1 => array($parentId, 'Integer'), |
236 | 2 => array($entityTable, 'String'), |
237 | 3 => array($initiatorId, 'Integer'), |
238 | ); |
239 | |
240 | if (!$mode) { |
60b36f60 |
241 | $mode = CRM_Core_DAO::singleValueQuery("SELECT mode FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
36b8b5f3 |
242 | } |
243 | |
62933949 |
244 | $query = "SELECT * |
245 | FROM civicrm_recurring_entity |
246 | WHERE parent_id = %1 AND entity_table = %2"; |
247 | if (!$includeParent) { |
36b8b5f3 |
248 | $query .= " AND entity_id != " . ($initiatorId ? "%3" : "%1"); |
249 | } |
250 | |
251 | if ($mode == '1') { // MODE = SINGLE |
252 | $query .= " AND entity_id = %3"; |
253 | } else if ($mode == '2') { // MODE = FUTURE |
254 | $recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
255 | if ($recurringEntityID) { |
256 | $query .= $includeParent ? " AND id >= %4" : " AND id > %4"; |
257 | $query .= " ORDER BY id ASC"; // FIXME: change to order by dates |
258 | $queryParams[4] = array($recurringEntityID, 'Integer'); |
259 | } else { |
260 | // something wrong, return empty |
261 | return array(); |
262 | } |
62933949 |
263 | } |
62933949 |
264 | |
36b8b5f3 |
265 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
62933949 |
266 | while ($dao->fetch()) { |
267 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['table'] = $dao->entity_table; |
268 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['id'] = $dao->entity_id; |
269 | } |
270 | return $entities; |
271 | } |
272 | |
36b8b5f3 |
273 | static public function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) { |
62933949 |
274 | $parentId = self::getParentFor($entityId, $entityTable); |
275 | if ($parentId) { |
36b8b5f3 |
276 | return self::getEntitiesForParent($parentId, $entityTable, $includeParent, $mode, $entityId); |
62933949 |
277 | } |
278 | return array(); |
279 | } |
280 | |
281 | static public function getParentFor($entityId, $entityTable, $includeParent = TRUE) { |
282 | $query = " |
283 | SELECT parent_id |
284 | FROM civicrm_recurring_entity |
285 | WHERE entity_id = %1 AND entity_table = %2"; |
286 | if (!$includeParent) { |
287 | $query .= " AND parent_id != %1"; |
288 | } |
289 | $parentId = |
290 | CRM_Core_DAO::singleValueQuery($query, |
291 | array( |
292 | 1 => array($entityId, 'Integer'), |
293 | 2 => array($entityTable, 'String'), |
294 | ) |
295 | ); |
296 | return $parentId; |
297 | } |
298 | |
62933949 |
299 | static public function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) { |
300 | $daoName = self::$_tableDAOMapper[$entityTable]; |
301 | $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams); |
302 | |
303 | if ($newObject->id && $createRecurringEntity) { |
304 | $object = new $daoName( ); |
305 | foreach ($fromCriteria as $key => $value) { |
306 | $object->$key = $value; |
307 | } |
308 | $object->find(TRUE); |
309 | |
310 | CRM_Core_BAO_RecurringEntity::quickAdd($object->id, $newObject->id, $entityTable); |
311 | } |
312 | return $newObject; |
313 | } |
314 | |
315 | static public function triggerUpdate($obj) { |
60b36f60 |
316 | // if DB version is earlier than 4.6 skip any processing |
317 | static $currentVer = NULL; |
318 | if (!$currentVer) { |
319 | $currentVer = CRM_Core_BAO_Domain::version(); |
320 | } |
321 | if (version_compare($currentVer, '4.6.alpha1') < 0) { |
322 | return; |
323 | } |
324 | |
62933949 |
325 | static $processedEntities = array(); |
326 | if (empty($obj->id) || empty($obj->__table)) { |
327 | return FALSE; |
328 | } |
329 | $key = "{$obj->__table}_{$obj->id}"; |
330 | |
331 | if (array_key_exists($key, $processedEntities)) { |
332 | // already processed |
333 | return NULL; |
334 | } |
335 | |
336 | // get related entities |
36b8b5f3 |
337 | $repeatingEntities = self::getEntitiesFor($obj->id, $obj->__table, FALSE, NULL); |
62933949 |
338 | if (empty($repeatingEntities)) { |
339 | // return if its not a recurring entity parent |
340 | return NULL; |
341 | } |
36b8b5f3 |
342 | // mark being processed |
62933949 |
343 | $processedEntities[$key] = 1; |
344 | |
36b8b5f3 |
345 | // to make sure we not copying to source itself |
346 | unset($repeatingEntities[$key]); |
347 | |
348 | foreach($repeatingEntities as $key => $val) { |
62933949 |
349 | $entityID = $val['id']; |
350 | $entityTable = $val['table']; |
351 | |
352 | $processedEntities[$key] = 1; |
353 | |
354 | if (array_key_exists($entityTable, self::$_tableDAOMapper)) { |
355 | $daoName = self::$_tableDAOMapper[$entityTable]; |
356 | |
62933949 |
357 | // FIXME: generalize me |
358 | $skipData = array('start_date' => NULL, |
359 | 'end_date' => NULL, |
360 | ); |
361 | |
36b8b5f3 |
362 | $updateDAO = CRM_Core_DAO::cascadeUpdate($daoName, $obj->id, $entityID, $skipData); |
363 | CRM_Core_DAO::freeResult(); |
50b39b5b |
364 | } else { |
365 | CRM_Core_Error::fatal("DAO Mapper missing for $entityTable."); |
62933949 |
366 | } |
367 | } |
368 | // done with processing. lets unset static var. |
369 | unset($processedEntities); |
370 | } |
371 | |
72e95527 |
372 | function mapFormValuesToDB($formParams = array()){ |
62933949 |
373 | $dbParams = array(); |
374 | if(CRM_Utils_Array::value('used_for', $formParams)){ |
375 | $dbParams['used_for'] = $formParams['used_for']; |
376 | } |
1b1fcd9e |
377 | |
62933949 |
378 | if(CRM_Utils_Array::value('parent_event_id', $formParams)){ |
1b1fcd9e |
379 | $dbParams['entity_value'] = $formParams['parent_event_id']; |
b1d1479a |
380 | } |
1b1fcd9e |
381 | |
c09936e1 |
382 | if(CRM_Utils_Array::value('repetition_start_date', $formParams)) { |
383 | $repetitionStartDate = $formParams['repetition_start_date']; |
384 | if (CRM_Utils_Array::value('repetition_start_date_time', $formParams)){ |
385 | $repetitionStartDate = $repetitionStartDate . " " . $formParams['repetition_start_date_time']; |
1b1fcd9e |
386 | } |
c09936e1 |
387 | $repetition_start_date = new DateTime($repetitionStartDate); |
388 | $repetition_start_date->modify('+1 day'); |
389 | $dbParams['entity_status'] = CRM_Utils_Date::processDate($repetition_start_date->format('Y-m-d H:i:s')); |
390 | } |
62933949 |
391 | |
392 | if(CRM_Utils_Array::value('repetition_frequency_unit', $formParams)){ |
b1d1479a |
393 | $dbParams['repetition_frequency_unit'] = $formParams['repetition_frequency_unit']; |
394 | } |
62933949 |
395 | |
396 | if(CRM_Utils_Array::value('repetition_frequency_interval', $formParams)){ |
b1d1479a |
397 | $dbParams['repetition_frequency_interval'] = $formParams['repetition_frequency_interval']; |
398 | } |
62933949 |
399 | |
400 | //For Repeats on:(weekly case) |
b1d1479a |
401 | if($formParams['repetition_frequency_unit'] == 'week'){ |
402 | if(CRM_Utils_Array::value('start_action_condition', $formParams)){ |
403 | $repeats_on = CRM_Utils_Array::value('start_action_condition', $formParams); |
404 | $dbParams['start_action_condition'] = implode(",", array_keys($repeats_on)); |
62933949 |
405 | } |
b1d1479a |
406 | } |
62933949 |
407 | |
408 | //For Repeats By:(monthly case) |
b1d1479a |
409 | if($formParams['repetition_frequency_unit'] == 'month'){ |
410 | if($formParams['repeats_by'] == 1){ |
411 | if(CRM_Utils_Array::value('limit_to', $formParams)){ |
412 | $dbParams['limit_to'] = $formParams['limit_to']; |
62933949 |
413 | } |
b1d1479a |
414 | } |
415 | if($formParams['repeats_by'] == 2){ |
416 | if(CRM_Utils_Array::value('start_action_date_1', $formParams) && CRM_Utils_Array::value('start_action_date_2', $formParams)){ |
417 | $dbParams['start_action_date'] = $formParams['start_action_date_1']." ".$formParams['start_action_date_2']; |
62933949 |
418 | } |
419 | } |
b1d1479a |
420 | } |
62933949 |
421 | |
422 | //For "Ends" - After: |
b1d1479a |
423 | if($formParams['ends'] == 1){ |
424 | if(CRM_Utils_Array::value('start_action_offset', $formParams)){ |
425 | $dbParams['start_action_offset'] = $formParams['start_action_offset']; |
62933949 |
426 | } |
b1d1479a |
427 | } |
62933949 |
428 | |
b1d1479a |
429 | //For "Ends" - On: |
430 | if($formParams['ends'] == 2){ |
431 | if(CRM_Utils_Array::value('repeat_absolute_date', $formParams)){ |
432 | $dbParams['absolute_date'] = CRM_Utils_Date::processDate($formParams['repeat_absolute_date']); |
62933949 |
433 | } |
62933949 |
434 | } |
b1d1479a |
435 | return $dbParams; |
436 | } |
62933949 |
437 | |
b1d1479a |
438 | static public function getScheduleReminderDetailsById($scheduleReminderId){ |
439 | $query = "SELECT * |
1b1fcd9e |
440 | FROM civicrm_action_schedule WHERE 1"; |
b1d1479a |
441 | if($scheduleReminderId){ |
442 | $query .= " |
1b1fcd9e |
443 | AND id = %1"; |
62933949 |
444 | } |
b1d1479a |
445 | $dao = CRM_Core_DAO::executeQuery($query, |
1b1fcd9e |
446 | array( |
447 | 1 => array($scheduleReminderId, 'Integer') |
448 | ) |
449 | ); |
b1d1479a |
450 | $dao->fetch(); |
451 | return $dao; |
452 | } |
1b1fcd9e |
453 | |
72e95527 |
454 | function getRecursionFromReminder($scheduleReminderId){ |
b1d1479a |
455 | if($scheduleReminderId){ |
456 | //Get all the details from schedule reminder table |
457 | $scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId); |
458 | $scheduleReminderDetails = (array) $scheduleReminderDetails; |
459 | $recursionDetails = self::getRecursionFromReminderByDBParams($scheduleReminderDetails); |
62933949 |
460 | } |
b1d1479a |
461 | return $recursionDetails; |
462 | } |
1b1fcd9e |
463 | |
72e95527 |
464 | function getRecursionFromReminderByDBParams($scheduleReminderDetails = array()){ |
b1d1479a |
465 | $r = new When(); |
466 | //If there is some data for this id |
467 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
468 | if($scheduleReminderDetails['entity_status']){ |
469 | $currDate = date('Y-m-d H:i:s', strtotime($scheduleReminderDetails['entity_status'])); |
470 | }else{ |
62933949 |
471 | $currDate = date("Y-m-d H:i:s"); |
b1d1479a |
472 | } |
473 | $start = new DateTime($currDate); |
474 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
475 | $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; |
476 | if($repetition_frequency_unit == "day"){ |
477 | $repetition_frequency_unit = "dai"; |
62933949 |
478 | } |
b1d1479a |
479 | $repetition_frequency_unit = $repetition_frequency_unit.'ly'; |
480 | $r->recur($start, $repetition_frequency_unit); |
481 | } |
62933949 |
482 | |
b1d1479a |
483 | if($scheduleReminderDetails['repetition_frequency_interval']){ |
484 | $r->interval($scheduleReminderDetails['repetition_frequency_interval']); |
485 | }else{ |
486 | $r->errors[] = 'Repeats every: is a required field'; |
487 | } |
62933949 |
488 | |
b1d1479a |
489 | //week |
490 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'week'){ |
491 | if($scheduleReminderDetails['start_action_condition']){ |
492 | $startActionCondition = $scheduleReminderDetails['start_action_condition']; |
493 | $explodeStartActionCondition = explode(',', $startActionCondition); |
494 | $buildRuleArray = array(); |
495 | foreach($explodeStartActionCondition as $key => $val){ |
496 | $buildRuleArray[] = strtoupper(substr($val, 0, 2)); |
62933949 |
497 | } |
b1d1479a |
498 | $r->wkst('MO')->byday($buildRuleArray); |
62933949 |
499 | } |
b1d1479a |
500 | } |
62933949 |
501 | |
b1d1479a |
502 | //month |
503 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'month'){ |
b1d1479a |
504 | if($scheduleReminderDetails['start_action_date']){ |
505 | $startActionDate = explode(" ", $scheduleReminderDetails['start_action_date']); |
506 | switch ($startActionDate[0]) { |
1b1fcd9e |
507 | case 'first': |
508 | $startActionDate1 = 1; |
509 | break; |
510 | case 'second': |
511 | $startActionDate1 = 2; |
512 | break; |
513 | case 'third': |
514 | $startActionDate1 = 3; |
515 | break; |
516 | case 'fourth': |
517 | $startActionDate1 = 4; |
518 | break; |
519 | case 'last': |
520 | $startActionDate1 = -1; |
521 | break; |
c828f12a |
522 | } |
b1d1479a |
523 | $concatStartActionDateBits = $startActionDate1.strtoupper(substr($startActionDate[1], 0, 2)); |
524 | $r->byday(array($concatStartActionDateBits)); |
4cf90acd |
525 | }else if($scheduleReminderDetails['limit_to']){ |
526 | $r->bymonthday(array($scheduleReminderDetails['limit_to'])); |
62933949 |
527 | } |
b1d1479a |
528 | } |
62933949 |
529 | |
b1d1479a |
530 | //Ends |
531 | if($scheduleReminderDetails['start_action_offset']){ |
532 | if($scheduleReminderDetails['start_action_offset'] > 30){ |
533 | $r->errors[] = 'Occurrences should be less than or equal to 30'; |
62933949 |
534 | } |
b1d1479a |
535 | $r->count($scheduleReminderDetails['start_action_offset']); |
536 | } |
62933949 |
537 | |
690bf076 |
538 | if(CRM_Utils_Array::value('absolute_date', $scheduleReminderDetails)) { |
b1d1479a |
539 | $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']); |
540 | $endDate = new DateTime($absoluteDate[0].' '.$absoluteDate[1]); |
541 | $r->until($endDate); |
542 | } |
543 | |
544 | if(!$scheduleReminderDetails['start_action_offset'] && !$scheduleReminderDetails['absolute_date']){ |
545 | $r->errors[] = 'Ends: is a required field'; |
546 | } |
1b1fcd9e |
547 | }else{ |
548 | $r->errors[] = 'Repeats: is a required field'; |
549 | } |
b1d1479a |
550 | return $r; |
551 | } |
1b1fcd9e |
552 | |
553 | /* |
554 | * Get Reminder id based on event id |
555 | */ |
556 | static public function getReminderDetailsByEventId($eventId, $used_for){ |
557 | if($eventId){ |
558 | $query = " |
559 | SELECT * |
560 | FROM civicrm_action_schedule |
561 | WHERE entity_value = %1"; |
562 | if($used_for){ |
563 | $query .= " AND used_for = %2"; |
fd1abec4 |
564 | } |
1b1fcd9e |
565 | $params = array( |
566 | 1 => array($eventId, 'Integer'), |
567 | 2 => array($used_for, 'String') |
568 | ); |
569 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
570 | $dao->fetch(); |
571 | } |
572 | return $dao; |
573 | } |
574 | |
575 | static public function getInterval($startDate, $endDate) { |
576 | if ($startDate && $endDate) { |
577 | $startDate = new DateTime($startDate); |
578 | $endDate = new DateTime($endDate); |
579 | |
580 | return $startDate->diff($endDate); |
fd1abec4 |
581 | } |
1b1fcd9e |
582 | } |
583 | |
ad82cae1 |
584 | static public function generateRecursions($recursionObj, $params = array(), $excludeDates = array()) { |
2eeee284 |
585 | $newParams = $recursionResult = array(); |
9d2af827 |
586 | if (is_a($recursionObj, 'When')) { |
ad82cae1 |
587 | $initialCount = CRM_Utils_Array::value('start_action_offset', $params); |
d10273dc |
588 | $interval = CRM_Utils_Array::value('interval', $params); |
589 | |
590 | $count = 1; |
591 | while ($result = $recursionObj->next()) { |
592 | $recursionResult[$count]['start_date'] = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); |
593 | |
594 | if($interval){ |
595 | $endDate = new DateTime($recursionResult[$count]['start_date']); |
596 | $endDate->add($interval); |
597 | $recursionResult[$count]['end_date'] = CRM_Utils_Date::processDate($endDate->format('Y-m-d H:i:s')); |
598 | } |
599 | |
600 | $skip = FALSE; |
601 | foreach ($excludeDates as $date) { |
602 | $date = CRM_Utils_Date::processDate($date, NULL, FALSE, 'Ymd'); |
603 | if (($date == $result->format('Ymd')) || |
604 | ($endDate && ($date > $result->format('Ymd')) && ($date <= $endDate->format('Ymd'))) |
605 | ) { |
606 | $skip = TRUE; |
607 | break; |
ad82cae1 |
608 | } |
d10273dc |
609 | } |
ad82cae1 |
610 | |
d10273dc |
611 | if ($skip) { |
612 | unset($recursionResult[$count]); |
613 | if ($initialCount && ($initialCount > 0)) { |
614 | // lets increase the counter, so we get correct number of occurrences |
615 | $initialCount++; |
616 | $recursionObj->count($initialCount); |
ad82cae1 |
617 | } |
d10273dc |
618 | continue; |
2eeee284 |
619 | } |
d10273dc |
620 | $count++; |
2eeee284 |
621 | } |
622 | } |
623 | return $recursionResult; |
624 | } |
1b1fcd9e |
625 | |
8dc6e78a |
626 | static public function delEntityRelations($entityId, $entityTable){ |
627 | if(!$entityId && !$entityTable){ |
628 | return FALSE; |
629 | } |
630 | $parentID = self::getParentFor($entityId, $entityTable); |
631 | if($parentID){ |
632 | $dao = new CRM_Core_DAO_RecurringEntity(); |
633 | $dao->parent_id = $parentID; |
634 | return $dao->delete(); |
635 | } |
636 | } |
1b1fcd9e |
637 | |
f51acb7a |
638 | static public function getParticipantCountforEvent($listOfRelatedEntities = array()){ |
639 | if(!empty($listOfRelatedEntities)){ |
640 | $implodeRelatedEntities = implode(',', array_map(function($entity){ |
1b1fcd9e |
641 | return $entity['id']; |
642 | }, $listOfRelatedEntities)); |
f51acb7a |
643 | if($implodeRelatedEntities){ |
644 | $query = "SELECT p.event_id as event_id, |
1b1fcd9e |
645 | concat_ws(' ', e.title, concat_ws(' - ', DATE_FORMAT(e.start_date, '%b %d %Y %h:%i %p'), DATE_FORMAT(e.end_date, '%b %d %Y %h:%i %p'))) as event_data, |
646 | count(p.id) as participant_count |
647 | FROM civicrm_participant p, civicrm_event e |
648 | WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities}) |
649 | GROUP BY p.event_id"; |
f51acb7a |
650 | $dao = CRM_Core_DAO::executeQuery($query); |
651 | $participantDetails = array(); |
652 | while($dao->fetch()) { |
653 | $participantDetails['countByID'][$dao->event_id] = $dao->participant_count; |
654 | $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count; |
655 | } |
656 | } |
657 | } |
658 | return $participantDetails; |
659 | } |
72e95527 |
660 | |
661 | static function testActivityGeneration() { |
662 | //Activity set initial params |
663 | $daoActivity = new CRM_Activity_DAO_Activity(); |
664 | $daoActivity->activity_type_id = 1; |
665 | $daoActivity->subject = "Initial Activity"; |
666 | $daoActivity->activity_date_time = date('YmdHis'); |
667 | $daoActivity->save(); |
668 | |
669 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
05121fdd |
670 | $recursion->entity_id = $daoActivity->id; |
671 | $recursion->entity_table = 'civicrm_activity'; |
672 | $recursion->dateColumns = array('activity_date_time'); |
673 | $recursion->scheduleDBParams = array( |
72e95527 |
674 | 'entity_value' => $daoActivity->id, |
675 | 'entity_status' => $daoActivity->activity_date_time, |
676 | 'start_action_date' => 'fourth saturday', |
677 | 'repetition_frequency_unit' => 'month', |
678 | 'repetition_frequency_interval' => 3, |
679 | 'start_action_offset' => 5, |
690bf076 |
680 | 'used_for' => 'activity' |
05121fdd |
681 | ); |
72e95527 |
682 | |
72e95527 |
683 | $generatedEntities = $recursion->generate(); |
684 | |
685 | // try changing something |
05121fdd |
686 | $recursion->mode(3); // sets ->mode var & saves in DB |
72e95527 |
687 | |
688 | // lets change subject of initial activity that we created in begining |
689 | $daoActivity->find(TRUE); |
690 | $daoActivity->subject = 'I changed it'; |
691 | $daoActivity->save(); |
692 | } |
626c9bcb |
693 | |
694 | static function testEventGeneration(){ |
695 | //Event set initial params |
696 | $daoEvent = new CRM_Event_DAO_Event(); |
697 | $daoEvent->title = 'Test event for Recurring Entity'; |
698 | $daoEvent->event_type_id = 3; |
699 | $daoEvent->is_public = 1; |
700 | $daoEvent->start_date = date('YmdHis', strtotime('2014-09-24 10:30:00')); |
701 | $daoEvent->end_date = date('YmdHis', strtotime('2014-09-26 10:30:00')); |
702 | $daoEvent->created_date = date('YmdHis'); |
c09936e1 |
703 | $daoEvent->is_active = 1; |
626c9bcb |
704 | $daoEvent->save(); |
705 | |
626c9bcb |
706 | $recursion = new CRM_Core_BAO_RecurringEntity(); |
707 | $recursion->entity_id = $daoEvent->id; |
708 | $recursion->entity_table = 'civicrm_event'; |
709 | $recursion->dateColumns = array('start_date'); |
710 | $recursion->scheduleDBParams = array ( |
711 | 'entity_value' => $daoEvent->id, |
712 | 'entity_status' => $daoEvent->start_date, |
c09936e1 |
713 | 'start_action_condition' => 'wednesday', |
626c9bcb |
714 | 'repetition_frequency_unit' => 'week', |
c09936e1 |
715 | 'repetition_frequency_interval' => 1, |
626c9bcb |
716 | 'start_action_offset' => 4, |
717 | 'used_for' => 'event' |
718 | ); |
719 | |
c09936e1 |
720 | $interval = $recursion->getInterval($daoEvent->start_date, $daoEvent->end_date); |
721 | $recursion->intervalDateColumns = array('end_date' => $interval); |
722 | |
723 | $recursion->excludeDates = array(date('Ymd', strtotime('2014-10-02')), '20141008');// = array('date1', date2, date2) |
724 | $recursion->excludeDateRangeColumns = array('start_date', 'end_date'); |
725 | |
626c9bcb |
726 | $generatedEntities = $recursion->generate(); |
c09936e1 |
727 | CRM_Core_Error::debug_var('$generatedEntities', $generatedEntities); |
626c9bcb |
728 | |
729 | // try changing something |
730 | $recursion->mode(3); // sets ->mode var & saves in DB |
731 | |
732 | $daoEvent->find(TRUE); |
733 | $daoEvent->title = 'I changed event'; |
734 | $daoEvent->save(); |
735 | } |
fd1abec4 |
736 | } |