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) { |
206c0c43 |
97 | if ($this->entity_id && $this->entity_table) { |
98 | if ($this->find(TRUE)) { |
99 | $this->mode = $mode; |
100 | } else { |
101 | $this->parent_id = $this->entity_id; |
102 | $this->mode = $mode; |
103 | } |
104 | $this->save(); |
dfbe9b91 |
105 | } |
72e95527 |
106 | } |
107 | |
108 | // generate all new entities based on object vars |
109 | function generate() { |
c09936e1 |
110 | $this->generateRecursiveDates(); |
c09936e1 |
111 | |
112 | return $this->generateEntities(); |
113 | } |
114 | |
dfbe9b91 |
115 | function generateRecursion() { |
c09936e1 |
116 | // return if already generated |
117 | if (is_a($this->recursion, 'When')) { |
118 | return $this->recursion; |
119 | } |
72e95527 |
120 | |
121 | if ($this->scheduleId) { |
122 | // get params by ID |
123 | $this->recursion = $this->getRecursionFromReminder($this->scheduleId); |
124 | } else if (!empty($this->schedule)) { |
125 | $this->scheduleDBParams = $this->mapFormValuesToDB($this->schedule);//call using obj |
126 | } |
c09936e1 |
127 | |
72e95527 |
128 | if (!empty($this->scheduleDBParams)) { |
129 | $this->recursion = $this->getRecursionFromReminderByDBParams($this->scheduleDBParams); |
130 | } |
c09936e1 |
131 | return $this->recursion; |
132 | } |
72e95527 |
133 | |
c09936e1 |
134 | // generate new DAOs and along with entries in recurring_entity table |
135 | function generateEntities() { |
dfbe9b91 |
136 | // fixme: check if entityid & entitytable set |
137 | |
c09936e1 |
138 | $newEntities = array(); |
139 | if (!empty($this->recursionDates)) { |
72e95527 |
140 | // save an entry with initiating entity-id & entity-table |
141 | if (!$this->find(TRUE)) { |
142 | $this->parent_id = $this->entity_id; |
143 | $this->save(); |
144 | } |
72e95527 |
145 | |
c09936e1 |
146 | foreach ($this->recursionDates as $key => $dateCols) { |
147 | $newCriteria = $dateCols; |
148 | foreach ($this->overwriteColumns as $col => $val) { |
149 | $newCriteria[$col] = $val; |
150 | } |
c09936e1 |
151 | $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table, |
152 | array('id' => $this->entity_id), |
153 | $newCriteria, |
154 | $this->isGenRecurringEntity |
155 | ); |
156 | $newEntities[] = $obj->id; |
72e95527 |
157 | } |
72e95527 |
158 | } |
159 | return $newEntities; |
160 | } |
161 | |
c09936e1 |
162 | function generateRecursiveDates() { |
dfbe9b91 |
163 | $this->generateRecursion(); |
c09936e1 |
164 | |
165 | $recursionDates = array(); |
72e95527 |
166 | if (is_a($this->recursion, 'When')) { |
167 | $initialCount = CRM_Utils_Array::value('start_action_offset', $this->scheduleDBParams); |
c09936e1 |
168 | |
169 | $exRangeStart = $exRangeEnd = NULL; |
170 | if (!empty($this->excludeDateRangeColumns)) { |
171 | $exRangeStart = $this->excludeDateRangeColumns[0]; |
172 | $exRangeEnd = $this->excludeDateRangeColumns[1]; |
173 | } |
72e95527 |
174 | |
175 | $count = 1; |
176 | while ($result = $this->recursion->next()) { |
c09936e1 |
177 | $baseDate = CRM_Utils_Date::processDate($result->format('Y-m-d H:i:s')); |
178 | |
179 | foreach ($this->dateColumns as $col) { |
180 | $recursionDates[$count][$col] = $baseDate; |
181 | } |
182 | foreach ($this->intervalDateColumns as $col => $interval) { |
183 | $newDate = new DateTime($baseDate); |
184 | $newDate->add($interval); |
185 | $recursionDates[$count][$col] = CRM_Utils_Date::processDate($newDate->format('Y-m-d H:i:s')); |
186 | } |
187 | if ($exRangeStart) { |
188 | $exRangeStartDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeStart], NULL, FALSE, 'Ymd'); |
189 | $exRangeEndDate = CRM_Utils_Date::processDate($recursionDates[$count][$exRangeEnd], NULL, FALSE, 'Ymd'); |
190 | } |
72e95527 |
191 | |
192 | $skip = FALSE; |
c09936e1 |
193 | foreach ($this->excludeDates as $exDate) { |
194 | $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd'); |
195 | if (!$exRangeStart) { |
196 | if ($exDate == $result->format('Ymd')) { |
197 | $skip = TRUE; |
198 | break; |
199 | } |
200 | } else { |
201 | if (($exDate == $exRangeStartDate) || |
202 | ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate)) |
203 | ) { |
204 | $skip = TRUE; |
205 | break; |
206 | } |
72e95527 |
207 | } |
208 | } |
209 | |
210 | if ($skip) { |
c09936e1 |
211 | unset($recursionDates[$count]); |
72e95527 |
212 | if ($initialCount && ($initialCount > 0)) { |
213 | // lets increase the counter, so we get correct number of occurrences |
214 | $initialCount++; |
215 | $this->recursion->count($initialCount); |
c09936e1 |
216 | $this->recursionCounter = $initialCount; |
72e95527 |
217 | } |
218 | continue; |
219 | } |
220 | $count++; |
221 | } |
222 | } |
c09936e1 |
223 | $this->recursionDates = $recursionDates; |
c09936e1 |
224 | |
225 | return $recursionDates; |
72e95527 |
226 | } |
227 | |
36b8b5f3 |
228 | // MODE = 3 (ALL) |
229 | static public function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) { |
62933949 |
230 | $entities = array(); |
231 | |
36b8b5f3 |
232 | if (!$initiatorId) { |
233 | $initiatorId = $parentId; |
234 | } |
235 | |
236 | $queryParams = array( |
237 | 1 => array($parentId, 'Integer'), |
238 | 2 => array($entityTable, 'String'), |
239 | 3 => array($initiatorId, 'Integer'), |
240 | ); |
241 | |
242 | if (!$mode) { |
60b36f60 |
243 | $mode = CRM_Core_DAO::singleValueQuery("SELECT mode FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
36b8b5f3 |
244 | } |
245 | |
62933949 |
246 | $query = "SELECT * |
247 | FROM civicrm_recurring_entity |
248 | WHERE parent_id = %1 AND entity_table = %2"; |
249 | if (!$includeParent) { |
36b8b5f3 |
250 | $query .= " AND entity_id != " . ($initiatorId ? "%3" : "%1"); |
251 | } |
252 | |
253 | if ($mode == '1') { // MODE = SINGLE |
254 | $query .= " AND entity_id = %3"; |
255 | } else if ($mode == '2') { // MODE = FUTURE |
256 | $recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
257 | if ($recurringEntityID) { |
258 | $query .= $includeParent ? " AND id >= %4" : " AND id > %4"; |
259 | $query .= " ORDER BY id ASC"; // FIXME: change to order by dates |
260 | $queryParams[4] = array($recurringEntityID, 'Integer'); |
261 | } else { |
262 | // something wrong, return empty |
263 | return array(); |
264 | } |
62933949 |
265 | } |
62933949 |
266 | |
36b8b5f3 |
267 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
62933949 |
268 | while ($dao->fetch()) { |
269 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['table'] = $dao->entity_table; |
270 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['id'] = $dao->entity_id; |
271 | } |
272 | return $entities; |
273 | } |
274 | |
36b8b5f3 |
275 | static public function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) { |
62933949 |
276 | $parentId = self::getParentFor($entityId, $entityTable); |
277 | if ($parentId) { |
36b8b5f3 |
278 | return self::getEntitiesForParent($parentId, $entityTable, $includeParent, $mode, $entityId); |
62933949 |
279 | } |
280 | return array(); |
281 | } |
282 | |
283 | static public function getParentFor($entityId, $entityTable, $includeParent = TRUE) { |
284 | $query = " |
285 | SELECT parent_id |
286 | FROM civicrm_recurring_entity |
287 | WHERE entity_id = %1 AND entity_table = %2"; |
288 | if (!$includeParent) { |
289 | $query .= " AND parent_id != %1"; |
290 | } |
291 | $parentId = |
292 | CRM_Core_DAO::singleValueQuery($query, |
293 | array( |
294 | 1 => array($entityId, 'Integer'), |
295 | 2 => array($entityTable, 'String'), |
296 | ) |
297 | ); |
298 | return $parentId; |
299 | } |
300 | |
62933949 |
301 | static public function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) { |
302 | $daoName = self::$_tableDAOMapper[$entityTable]; |
303 | $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams); |
304 | |
305 | if ($newObject->id && $createRecurringEntity) { |
306 | $object = new $daoName( ); |
307 | foreach ($fromCriteria as $key => $value) { |
308 | $object->$key = $value; |
309 | } |
310 | $object->find(TRUE); |
311 | |
312 | CRM_Core_BAO_RecurringEntity::quickAdd($object->id, $newObject->id, $entityTable); |
313 | } |
314 | return $newObject; |
315 | } |
316 | |
317 | static public function triggerUpdate($obj) { |
60b36f60 |
318 | // if DB version is earlier than 4.6 skip any processing |
319 | static $currentVer = NULL; |
320 | if (!$currentVer) { |
321 | $currentVer = CRM_Core_BAO_Domain::version(); |
322 | } |
323 | if (version_compare($currentVer, '4.6.alpha1') < 0) { |
324 | return; |
325 | } |
326 | |
62933949 |
327 | static $processedEntities = array(); |
328 | if (empty($obj->id) || empty($obj->__table)) { |
329 | return FALSE; |
330 | } |
331 | $key = "{$obj->__table}_{$obj->id}"; |
332 | |
333 | if (array_key_exists($key, $processedEntities)) { |
334 | // already processed |
335 | return NULL; |
336 | } |
337 | |
338 | // get related entities |
36b8b5f3 |
339 | $repeatingEntities = self::getEntitiesFor($obj->id, $obj->__table, FALSE, NULL); |
62933949 |
340 | if (empty($repeatingEntities)) { |
341 | // return if its not a recurring entity parent |
342 | return NULL; |
343 | } |
36b8b5f3 |
344 | // mark being processed |
62933949 |
345 | $processedEntities[$key] = 1; |
346 | |
36b8b5f3 |
347 | // to make sure we not copying to source itself |
348 | unset($repeatingEntities[$key]); |
349 | |
350 | foreach($repeatingEntities as $key => $val) { |
62933949 |
351 | $entityID = $val['id']; |
352 | $entityTable = $val['table']; |
353 | |
354 | $processedEntities[$key] = 1; |
355 | |
356 | if (array_key_exists($entityTable, self::$_tableDAOMapper)) { |
357 | $daoName = self::$_tableDAOMapper[$entityTable]; |
358 | |
62933949 |
359 | // FIXME: generalize me |
360 | $skipData = array('start_date' => NULL, |
361 | 'end_date' => NULL, |
362 | ); |
363 | |
36b8b5f3 |
364 | $updateDAO = CRM_Core_DAO::cascadeUpdate($daoName, $obj->id, $entityID, $skipData); |
365 | CRM_Core_DAO::freeResult(); |
50b39b5b |
366 | } else { |
367 | CRM_Core_Error::fatal("DAO Mapper missing for $entityTable."); |
62933949 |
368 | } |
369 | } |
370 | // done with processing. lets unset static var. |
371 | unset($processedEntities); |
372 | } |
373 | |
72e95527 |
374 | function mapFormValuesToDB($formParams = array()){ |
62933949 |
375 | $dbParams = array(); |
376 | if(CRM_Utils_Array::value('used_for', $formParams)){ |
377 | $dbParams['used_for'] = $formParams['used_for']; |
378 | } |
1b1fcd9e |
379 | |
62933949 |
380 | if(CRM_Utils_Array::value('parent_event_id', $formParams)){ |
1b1fcd9e |
381 | $dbParams['entity_value'] = $formParams['parent_event_id']; |
b1d1479a |
382 | } |
1b1fcd9e |
383 | |
c09936e1 |
384 | if(CRM_Utils_Array::value('repetition_start_date', $formParams)) { |
385 | $repetitionStartDate = $formParams['repetition_start_date']; |
386 | if (CRM_Utils_Array::value('repetition_start_date_time', $formParams)){ |
387 | $repetitionStartDate = $repetitionStartDate . " " . $formParams['repetition_start_date_time']; |
1b1fcd9e |
388 | } |
c09936e1 |
389 | $repetition_start_date = new DateTime($repetitionStartDate); |
390 | $repetition_start_date->modify('+1 day'); |
a1a821bc |
391 | $dbParams['start_action_date'] = CRM_Utils_Date::processDate($repetition_start_date->format('Y-m-d H:i:s')); |
c09936e1 |
392 | } |
62933949 |
393 | |
394 | if(CRM_Utils_Array::value('repetition_frequency_unit', $formParams)){ |
b1d1479a |
395 | $dbParams['repetition_frequency_unit'] = $formParams['repetition_frequency_unit']; |
396 | } |
62933949 |
397 | |
398 | if(CRM_Utils_Array::value('repetition_frequency_interval', $formParams)){ |
b1d1479a |
399 | $dbParams['repetition_frequency_interval'] = $formParams['repetition_frequency_interval']; |
400 | } |
62933949 |
401 | |
402 | //For Repeats on:(weekly case) |
b1d1479a |
403 | if($formParams['repetition_frequency_unit'] == 'week'){ |
404 | if(CRM_Utils_Array::value('start_action_condition', $formParams)){ |
405 | $repeats_on = CRM_Utils_Array::value('start_action_condition', $formParams); |
406 | $dbParams['start_action_condition'] = implode(",", array_keys($repeats_on)); |
62933949 |
407 | } |
b1d1479a |
408 | } |
62933949 |
409 | |
410 | //For Repeats By:(monthly case) |
b1d1479a |
411 | if($formParams['repetition_frequency_unit'] == 'month'){ |
412 | if($formParams['repeats_by'] == 1){ |
413 | if(CRM_Utils_Array::value('limit_to', $formParams)){ |
414 | $dbParams['limit_to'] = $formParams['limit_to']; |
62933949 |
415 | } |
b1d1479a |
416 | } |
417 | if($formParams['repeats_by'] == 2){ |
a1a821bc |
418 | if(CRM_Utils_Array::value('entity_status_1', $formParams) && CRM_Utils_Array::value('entity_status_2', $formParams)){ |
419 | $dbParams['entity_status'] = $formParams['entity_status_1']." ".$formParams['entity_status_2']; |
62933949 |
420 | } |
421 | } |
b1d1479a |
422 | } |
62933949 |
423 | |
424 | //For "Ends" - After: |
b1d1479a |
425 | if($formParams['ends'] == 1){ |
426 | if(CRM_Utils_Array::value('start_action_offset', $formParams)){ |
427 | $dbParams['start_action_offset'] = $formParams['start_action_offset']; |
62933949 |
428 | } |
b1d1479a |
429 | } |
62933949 |
430 | |
b1d1479a |
431 | //For "Ends" - On: |
432 | if($formParams['ends'] == 2){ |
433 | if(CRM_Utils_Array::value('repeat_absolute_date', $formParams)){ |
434 | $dbParams['absolute_date'] = CRM_Utils_Date::processDate($formParams['repeat_absolute_date']); |
62933949 |
435 | } |
62933949 |
436 | } |
b1d1479a |
437 | return $dbParams; |
438 | } |
62933949 |
439 | |
b1d1479a |
440 | static public function getScheduleReminderDetailsById($scheduleReminderId){ |
441 | $query = "SELECT * |
1b1fcd9e |
442 | FROM civicrm_action_schedule WHERE 1"; |
b1d1479a |
443 | if($scheduleReminderId){ |
444 | $query .= " |
1b1fcd9e |
445 | AND id = %1"; |
62933949 |
446 | } |
b1d1479a |
447 | $dao = CRM_Core_DAO::executeQuery($query, |
1b1fcd9e |
448 | array( |
449 | 1 => array($scheduleReminderId, 'Integer') |
450 | ) |
451 | ); |
b1d1479a |
452 | $dao->fetch(); |
453 | return $dao; |
454 | } |
1b1fcd9e |
455 | |
72e95527 |
456 | function getRecursionFromReminder($scheduleReminderId){ |
b1d1479a |
457 | if($scheduleReminderId){ |
458 | //Get all the details from schedule reminder table |
459 | $scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId); |
460 | $scheduleReminderDetails = (array) $scheduleReminderDetails; |
461 | $recursionDetails = self::getRecursionFromReminderByDBParams($scheduleReminderDetails); |
62933949 |
462 | } |
b1d1479a |
463 | return $recursionDetails; |
464 | } |
1b1fcd9e |
465 | |
72e95527 |
466 | function getRecursionFromReminderByDBParams($scheduleReminderDetails = array()){ |
b1d1479a |
467 | $r = new When(); |
468 | //If there is some data for this id |
469 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
a1a821bc |
470 | if($scheduleReminderDetails['start_action_date']){ |
471 | $currDate = date('Y-m-d H:i:s', strtotime($scheduleReminderDetails['start_action_date'])); |
b1d1479a |
472 | }else{ |
62933949 |
473 | $currDate = date("Y-m-d H:i:s"); |
b1d1479a |
474 | } |
475 | $start = new DateTime($currDate); |
476 | if($scheduleReminderDetails['repetition_frequency_unit']){ |
477 | $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; |
478 | if($repetition_frequency_unit == "day"){ |
479 | $repetition_frequency_unit = "dai"; |
62933949 |
480 | } |
b1d1479a |
481 | $repetition_frequency_unit = $repetition_frequency_unit.'ly'; |
482 | $r->recur($start, $repetition_frequency_unit); |
483 | } |
62933949 |
484 | |
b1d1479a |
485 | if($scheduleReminderDetails['repetition_frequency_interval']){ |
486 | $r->interval($scheduleReminderDetails['repetition_frequency_interval']); |
487 | }else{ |
488 | $r->errors[] = 'Repeats every: is a required field'; |
489 | } |
62933949 |
490 | |
b1d1479a |
491 | //week |
492 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'week'){ |
493 | if($scheduleReminderDetails['start_action_condition']){ |
494 | $startActionCondition = $scheduleReminderDetails['start_action_condition']; |
495 | $explodeStartActionCondition = explode(',', $startActionCondition); |
496 | $buildRuleArray = array(); |
497 | foreach($explodeStartActionCondition as $key => $val){ |
498 | $buildRuleArray[] = strtoupper(substr($val, 0, 2)); |
62933949 |
499 | } |
b1d1479a |
500 | $r->wkst('MO')->byday($buildRuleArray); |
62933949 |
501 | } |
b1d1479a |
502 | } |
62933949 |
503 | |
b1d1479a |
504 | //month |
505 | if($scheduleReminderDetails['repetition_frequency_unit'] == 'month'){ |
a1a821bc |
506 | if($scheduleReminderDetails['entity_status']){ |
507 | $startActionDate = explode(" ", $scheduleReminderDetails['entity_status']); |
b1d1479a |
508 | switch ($startActionDate[0]) { |
1b1fcd9e |
509 | case 'first': |
510 | $startActionDate1 = 1; |
511 | break; |
512 | case 'second': |
513 | $startActionDate1 = 2; |
514 | break; |
515 | case 'third': |
516 | $startActionDate1 = 3; |
517 | break; |
518 | case 'fourth': |
519 | $startActionDate1 = 4; |
520 | break; |
521 | case 'last': |
522 | $startActionDate1 = -1; |
523 | break; |
c828f12a |
524 | } |
b1d1479a |
525 | $concatStartActionDateBits = $startActionDate1.strtoupper(substr($startActionDate[1], 0, 2)); |
526 | $r->byday(array($concatStartActionDateBits)); |
4cf90acd |
527 | }else if($scheduleReminderDetails['limit_to']){ |
528 | $r->bymonthday(array($scheduleReminderDetails['limit_to'])); |
62933949 |
529 | } |
b1d1479a |
530 | } |
62933949 |
531 | |
b1d1479a |
532 | //Ends |
533 | if($scheduleReminderDetails['start_action_offset']){ |
534 | if($scheduleReminderDetails['start_action_offset'] > 30){ |
535 | $r->errors[] = 'Occurrences should be less than or equal to 30'; |
62933949 |
536 | } |
b1d1479a |
537 | $r->count($scheduleReminderDetails['start_action_offset']); |
538 | } |
62933949 |
539 | |
690bf076 |
540 | if(CRM_Utils_Array::value('absolute_date', $scheduleReminderDetails)) { |
b1d1479a |
541 | $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']); |
542 | $endDate = new DateTime($absoluteDate[0].' '.$absoluteDate[1]); |
543 | $r->until($endDate); |
544 | } |
545 | |
546 | if(!$scheduleReminderDetails['start_action_offset'] && !$scheduleReminderDetails['absolute_date']){ |
547 | $r->errors[] = 'Ends: is a required field'; |
548 | } |
1b1fcd9e |
549 | }else{ |
550 | $r->errors[] = 'Repeats: is a required field'; |
551 | } |
b1d1479a |
552 | return $r; |
553 | } |
1b1fcd9e |
554 | |
555 | /* |
556 | * Get Reminder id based on event id |
557 | */ |
558 | static public function getReminderDetailsByEventId($eventId, $used_for){ |
559 | if($eventId){ |
560 | $query = " |
561 | SELECT * |
562 | FROM civicrm_action_schedule |
563 | WHERE entity_value = %1"; |
564 | if($used_for){ |
565 | $query .= " AND used_for = %2"; |
fd1abec4 |
566 | } |
1b1fcd9e |
567 | $params = array( |
568 | 1 => array($eventId, 'Integer'), |
569 | 2 => array($used_for, 'String') |
570 | ); |
571 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
572 | $dao->fetch(); |
573 | } |
574 | return $dao; |
575 | } |
576 | |
577 | static public function getInterval($startDate, $endDate) { |
578 | if ($startDate && $endDate) { |
579 | $startDate = new DateTime($startDate); |
580 | $endDate = new DateTime($endDate); |
581 | |
582 | return $startDate->diff($endDate); |
fd1abec4 |
583 | } |
1b1fcd9e |
584 | } |
585 | |
8dc6e78a |
586 | static public function delEntityRelations($entityId, $entityTable){ |
587 | if(!$entityId && !$entityTable){ |
588 | return FALSE; |
589 | } |
590 | $parentID = self::getParentFor($entityId, $entityTable); |
591 | if($parentID){ |
592 | $dao = new CRM_Core_DAO_RecurringEntity(); |
593 | $dao->parent_id = $parentID; |
594 | return $dao->delete(); |
595 | } |
596 | } |
1b1fcd9e |
597 | |
f51acb7a |
598 | static public function getParticipantCountforEvent($listOfRelatedEntities = array()){ |
599 | if(!empty($listOfRelatedEntities)){ |
600 | $implodeRelatedEntities = implode(',', array_map(function($entity){ |
1b1fcd9e |
601 | return $entity['id']; |
602 | }, $listOfRelatedEntities)); |
f51acb7a |
603 | if($implodeRelatedEntities){ |
604 | $query = "SELECT p.event_id as event_id, |
1b1fcd9e |
605 | 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, |
606 | count(p.id) as participant_count |
607 | FROM civicrm_participant p, civicrm_event e |
608 | WHERE p.event_id = e.id AND p.event_id IN ({$implodeRelatedEntities}) |
609 | GROUP BY p.event_id"; |
f51acb7a |
610 | $dao = CRM_Core_DAO::executeQuery($query); |
611 | $participantDetails = array(); |
612 | while($dao->fetch()) { |
613 | $participantDetails['countByID'][$dao->event_id] = $dao->participant_count; |
614 | $participantDetails['countByName'][$dao->event_id][$dao->event_data] = $dao->participant_count; |
615 | } |
616 | } |
617 | } |
618 | return $participantDetails; |
619 | } |
fd1abec4 |
620 | } |