Commit | Line | Data |
---|---|---|
0775436c TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
0775436c | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
0775436c | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
0775436c TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
0775436c TO |
16 | */ |
17 | ||
2ba9aad6 | 18 | use When\When; |
0775436c | 19 | |
1cd3ffa9 | 20 | /** |
8eedd10a | 21 | * Class CRM_Core_BAO_RecurringEntity. |
1cd3ffa9 | 22 | */ |
1191f87e | 23 | class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity implements \Symfony\Component\EventDispatcher\EventSubscriberInterface { |
0775436c TO |
24 | |
25 | const RUNNING = 1; | |
be2fb01f | 26 | public $schedule = []; |
0775436c | 27 | public $scheduleId = NULL; |
be2fb01f | 28 | public $scheduleFormValues = []; |
0775436c | 29 | |
be2fb01f CW |
30 | public $dateColumns = []; |
31 | public $overwriteColumns = []; | |
32 | public $intervalDateColumns = []; | |
33 | public $excludeDates = []; | |
0775436c | 34 | |
be2fb01f | 35 | public $linkedEntities = []; |
0775436c TO |
36 | |
37 | public $isRecurringEntityRecord = TRUE; | |
38 | ||
39 | protected $recursion = NULL; | |
40 | protected $recursion_start_date = NULL; | |
41 | ||
be2fb01f | 42 | public static $_entitiesToBeDeleted = []; |
0775436c TO |
43 | |
44 | public static $status = NULL; | |
45 | ||
518fa0ee | 46 | public static $_recurringEntityHelper |
be2fb01f CW |
47 | = [ |
48 | 'civicrm_event' => [ | |
0775436c TO |
49 | 'helper_class' => 'CRM_Event_DAO_Event', |
50 | 'delete_func' => 'delete', | |
e7483cbe | 51 | 'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents', |
be2fb01f CW |
52 | ], |
53 | 'civicrm_activity' => [ | |
0775436c TO |
54 | 'helper_class' => 'CRM_Activity_DAO_Activity', |
55 | 'delete_func' => 'delete', | |
e7483cbe | 56 | 'pre_delete_func' => '', |
be2fb01f CW |
57 | ], |
58 | ]; | |
0775436c | 59 | |
518fa0ee | 60 | public static $_dateColumns |
be2fb01f CW |
61 | = [ |
62 | 'civicrm_event' => [ | |
63 | 'dateColumns' => ['start_date'], | |
64 | 'excludeDateRangeColumns' => ['start_date', 'end_date'], | |
65 | 'intervalDateColumns' => ['end_date'], | |
66 | ], | |
67 | 'civicrm_activity' => [ | |
68 | 'dateColumns' => ['activity_date_time'], | |
69 | ], | |
70 | ]; | |
0775436c | 71 | |
518fa0ee | 72 | public static $_tableDAOMapper |
be2fb01f | 73 | = [ |
353ffa53 | 74 | 'civicrm_event' => 'CRM_Event_DAO_Event', |
0775436c | 75 | 'civicrm_price_set_entity' => 'CRM_Price_DAO_PriceSetEntity', |
353ffa53 | 76 | 'civicrm_uf_join' => 'CRM_Core_DAO_UFJoin', |
0775436c | 77 | 'civicrm_tell_friend' => 'CRM_Friend_DAO_Friend', |
353ffa53 TO |
78 | 'civicrm_pcp_block' => 'CRM_PCP_DAO_PCPBlock', |
79 | 'civicrm_activity' => 'CRM_Activity_DAO_Activity', | |
80 | 'civicrm_activity_contact' => 'CRM_Activity_DAO_ActivityContact', | |
be2fb01f | 81 | ]; |
0775436c | 82 | |
518fa0ee | 83 | public static $_updateSkipFields |
be2fb01f CW |
84 | = [ |
85 | 'civicrm_event' => ['start_date', 'end_date'], | |
86 | 'civicrm_tell_friend' => ['entity_id'], | |
87 | 'civicrm_pcp_block' => ['entity_id'], | |
88 | 'civicrm_activity' => ['activity_date_time'], | |
89 | ]; | |
0775436c | 90 | |
518fa0ee | 91 | public static $_linkedEntitiesInfo |
be2fb01f CW |
92 | = [ |
93 | 'civicrm_tell_friend' => [ | |
353ffa53 | 94 | 'entity_id_col' => 'entity_id', |
e7483cbe | 95 | 'entity_table_col' => 'entity_table', |
be2fb01f CW |
96 | ], |
97 | 'civicrm_price_set_entity' => [ | |
353ffa53 | 98 | 'entity_id_col' => 'entity_id', |
0775436c | 99 | 'entity_table_col' => 'entity_table', |
353ffa53 | 100 | 'is_multirecord' => TRUE, |
be2fb01f CW |
101 | ], |
102 | 'civicrm_uf_join' => [ | |
353ffa53 | 103 | 'entity_id_col' => 'entity_id', |
0775436c | 104 | 'entity_table_col' => 'entity_table', |
353ffa53 | 105 | 'is_multirecord' => TRUE, |
be2fb01f CW |
106 | ], |
107 | 'civicrm_pcp_block' => [ | |
353ffa53 | 108 | 'entity_id_col' => 'entity_id', |
e7483cbe | 109 | 'entity_table_col' => 'entity_table', |
be2fb01f CW |
110 | ], |
111 | ]; | |
0775436c | 112 | |
b0f2e06f RJ |
113 | //Define global CLASS CONSTANTS for recurring entity mode types |
114 | const MODE_THIS_ENTITY_ONLY = 1; | |
115 | const MODE_NEXT_ALL_ENTITY = 2; | |
116 | const MODE_ALL_ENTITY_IN_SERIES = 3; | |
117 | ||
1191f87e TO |
118 | public static function getSubscribedEvents() { |
119 | return [ | |
120 | 'civi.dao.postInsert' => 'triggerInsert', | |
121 | 'civi.dao.postUpdate' => 'triggerUpdate', | |
122 | 'civi.dao.postDelete' => 'triggerDelete', | |
123 | ]; | |
124 | } | |
125 | ||
2e2605fe EM |
126 | /** |
127 | * Getter for status. | |
128 | * | |
129 | * @return string | |
130 | */ | |
0775436c TO |
131 | public static function getStatus() { |
132 | return self::$status; | |
133 | } | |
134 | ||
2e2605fe EM |
135 | /** |
136 | * Setter for status. | |
137 | * | |
138 | * @param string $status | |
139 | */ | |
0775436c TO |
140 | public static function setStatus($status) { |
141 | self::$status = $status; | |
142 | } | |
353ffa53 | 143 | |
0775436c | 144 | /** |
6a5fec96 | 145 | * Create or update a RecurringEntity. |
0775436c | 146 | * |
6a0b768e | 147 | * @param array $params |
6a5fec96 | 148 | * @return CRM_Core_DAO_RecurringEntity |
0775436c | 149 | */ |
6a5fec96 CW |
150 | public static function add($params) { |
151 | return self::writeRecord($params); | |
0775436c TO |
152 | } |
153 | ||
154 | /** | |
6a5fec96 | 155 | * Convenience wrapper for self::writeRecord |
0775436c | 156 | * |
6a0b768e TO |
157 | * @param int $parentId |
158 | * Parent entity id . | |
159 | * @param int $entityId | |
160 | * Child entity id . | |
161 | * @param string $entityTable | |
162 | * Name of the entity table . | |
0775436c | 163 | * |
6a5fec96 | 164 | * @return CRM_Core_DAO_RecurringEntity |
0775436c TO |
165 | */ |
166 | public static function quickAdd($parentId, $entityId, $entityTable) { | |
6a5fec96 CW |
167 | $params = [ |
168 | 'parent_id' => $parentId, | |
169 | 'entity_id' => $entityId, | |
170 | 'entity_table' => $entityTable, | |
171 | ]; | |
172 | return self::writeRecord($params); | |
0775436c TO |
173 | } |
174 | ||
175 | /** | |
fe482240 | 176 | * This function updates the mode column in the civicrm_recurring_entity table. |
0775436c | 177 | * |
6a0b768e | 178 | * @param int $mode |
8eedd10a | 179 | * Mode of the entity to cascade changes across parent/child relations eg 1 - only this entity, 2 - this and the following entities, 3 - All the entity. |
0775436c TO |
180 | */ |
181 | public function mode($mode) { | |
182 | if ($this->entity_id && $this->entity_table) { | |
183 | if ($this->find(TRUE)) { | |
184 | $this->mode = $mode; | |
185 | } | |
186 | else { | |
187 | $this->parent_id = $this->entity_id; | |
188 | $this->mode = $mode; | |
189 | } | |
190 | $this->save(); | |
191 | } | |
192 | } | |
193 | ||
194 | /** | |
fe482240 | 195 | * This function generates all new entities based on object vars. |
0775436c TO |
196 | * |
197 | * @return array | |
198 | */ | |
199 | public function generate() { | |
200 | $this->generateRecursiveDates(); | |
201 | ||
202 | return $this->generateEntities(); | |
203 | } | |
204 | ||
205 | /** | |
206 | * This function builds a "When" object based on schedule/reminder params | |
207 | * | |
a6c01b45 CW |
208 | * @return object |
209 | * When object | |
0775436c TO |
210 | */ |
211 | public function generateRecursion() { | |
212 | // return if already generated | |
30aa3e54 | 213 | if (is_a($this->recursion, 'When\When')) { |
0775436c TO |
214 | return $this->recursion; |
215 | } | |
216 | ||
217 | if ($this->scheduleId) { | |
218 | // get params by ID | |
219 | $this->schedule = $this->getScheduleParams($this->scheduleId); | |
220 | } | |
221 | elseif (!empty($this->scheduleFormValues)) { | |
222 | $this->schedule = $this->mapFormValuesToDB($this->scheduleFormValues); | |
223 | } | |
224 | ||
225 | if (!empty($this->schedule)) { | |
226 | $this->recursion = $this->getRecursionFromSchedule($this->schedule); | |
227 | } | |
228 | return $this->recursion; | |
229 | } | |
230 | ||
231 | /** | |
fe482240 | 232 | * Generate new DAOs and along with entries in civicrm_recurring_entity table. |
0775436c TO |
233 | * |
234 | * @return array | |
ac15829d | 235 | * @throws CRM_Core_Exception |
0775436c TO |
236 | */ |
237 | public function generateEntities() { | |
238 | self::setStatus(self::RUNNING); | |
239 | ||
be2fb01f CW |
240 | $newEntities = []; |
241 | $findCriteria = []; | |
0775436c TO |
242 | if (!empty($this->recursionDates)) { |
243 | if ($this->entity_id) { | |
be2fb01f | 244 | $findCriteria = ['id' => $this->entity_id]; |
0775436c TO |
245 | |
246 | // save an entry with initiating entity-id & entity-table | |
247 | if ($this->entity_table && !$this->find(TRUE)) { | |
248 | $this->parent_id = $this->entity_id; | |
249 | $this->save(); | |
250 | } | |
251 | } | |
252 | if (empty($findCriteria)) { | |
ac15829d | 253 | throw new CRM_Core_Exception("Find criteria missing to generate form. Make sure entity_id and table is set."); |
0775436c TO |
254 | } |
255 | ||
256 | $count = 0; | |
257 | foreach ($this->recursionDates as $key => $dateCols) { | |
258 | $newCriteria = $dateCols; | |
259 | foreach ($this->overwriteColumns as $col => $val) { | |
260 | $newCriteria[$col] = $val; | |
261 | } | |
262 | // create main entities | |
263 | $obj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($this->entity_table, | |
264 | $findCriteria, | |
265 | $newCriteria, | |
266 | $this->isRecurringEntityRecord | |
267 | ); | |
268 | ||
269 | if (is_a($obj, 'CRM_Core_DAO') && $obj->id) { | |
be2fb01f | 270 | $newCriteria = []; |
0775436c TO |
271 | $newEntities[$this->entity_table][$count] = $obj->id; |
272 | ||
273 | foreach ($this->linkedEntities as $linkedInfo) { | |
274 | foreach ($linkedInfo['linkedColumns'] as $col) { | |
275 | $newCriteria[$col] = $obj->id; | |
276 | } | |
277 | // create linked entities | |
278 | $linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($linkedInfo['table'], | |
279 | $linkedInfo['findCriteria'], | |
280 | $newCriteria, | |
281 | CRM_Utils_Array::value('isRecurringEntityRecord', $linkedInfo, TRUE) | |
282 | ); | |
283 | ||
284 | if (is_a($linkedObj, 'CRM_Core_DAO') && $linkedObj->id) { | |
285 | $newEntities[$linkedInfo['table']][$count] = $linkedObj->id; | |
286 | } | |
287 | } | |
288 | } | |
289 | $count++; | |
290 | } | |
291 | } | |
292 | ||
293 | self::$status = NULL; | |
294 | return $newEntities; | |
295 | } | |
296 | ||
297 | /** | |
b44e3f84 | 298 | * This function iterates through when object criteria and |
0775436c TO |
299 | * generates recursive dates based on that |
300 | * | |
a6c01b45 CW |
301 | * @return array |
302 | * array of dates | |
0775436c TO |
303 | */ |
304 | public function generateRecursiveDates() { | |
305 | $this->generateRecursion(); | |
306 | ||
be2fb01f | 307 | $recursionDates = []; |
2ba9aad6 | 308 | if (is_a($this->recursion, 'When\When')) { |
9c1bc317 | 309 | $initialCount = $this->schedule['start_action_offset'] ?? NULL; |
0775436c TO |
310 | |
311 | $exRangeStart = $exRangeEnd = NULL; | |
312 | if (!empty($this->excludeDateRangeColumns)) { | |
313 | $exRangeStart = $this->excludeDateRangeColumns[0]; | |
353ffa53 | 314 | $exRangeEnd = $this->excludeDateRangeColumns[1]; |
0775436c TO |
315 | } |
316 | ||
30aa3e54 PZ |
317 | if (CRM_Core_Config::singleton()->userFramework == 'UnitTests') { |
318 | $this->recursion->RFC5545_COMPLIANT = When::IGNORE; | |
319 | } | |
0775436c | 320 | $count = 1; |
30aa3e54 PZ |
321 | $result = $this->recursion_start_date; |
322 | while ($result = $this->getNextOccurrence($result)) { | |
323 | $skip = FALSE; | |
324 | if ($result == $this->recursion_start_date) { | |
325 | // skip the recursion-start-date from the list we going to generate | |
326 | $skip = TRUE; | |
5e73744d | 327 | } |
1043bac2 | 328 | $baseDate = $result->format('YmdHis'); |
0775436c TO |
329 | |
330 | foreach ($this->dateColumns as $col) { | |
331 | $recursionDates[$count][$col] = $baseDate; | |
332 | } | |
333 | foreach ($this->intervalDateColumns as $col => $interval) { | |
334 | $newDate = new DateTime($baseDate); | |
335 | $newDate->add($interval); | |
1043bac2 | 336 | $recursionDates[$count][$col] = $newDate->format('YmdHis'); |
0775436c TO |
337 | } |
338 | if ($exRangeStart) { | |
3b39fda2 CW |
339 | $exRangeStartDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeStart, $recursionDates[$count]), NULL, FALSE, 'Ymd'); |
340 | $exRangeEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value($exRangeEnd, $recursionDates[$count]), NULL, FALSE, 'Ymd'); | |
0775436c TO |
341 | } |
342 | ||
343 | foreach ($this->excludeDates as $exDate) { | |
344 | $exDate = CRM_Utils_Date::processDate($exDate, NULL, FALSE, 'Ymd'); | |
345 | if (!$exRangeStart) { | |
346 | if ($exDate == $result->format('Ymd')) { | |
347 | $skip = TRUE; | |
348 | break; | |
349 | } | |
350 | } | |
351 | else { | |
352 | if (($exDate == $exRangeStartDate) || | |
353 | ($exRangeEndDate && ($exDate > $exRangeStartDate) && ($exDate <= $exRangeEndDate)) | |
354 | ) { | |
355 | $skip = TRUE; | |
356 | break; | |
357 | } | |
358 | } | |
359 | } | |
360 | ||
30aa3e54 PZ |
361 | if ($skip) { |
362 | unset($recursionDates[$count]); | |
363 | if ($initialCount && ($initialCount > 0)) { | |
364 | // lets increase the counter, so we get correct number of occurrences | |
365 | $initialCount++; | |
366 | $this->recursion->count($initialCount); | |
367 | } | |
368 | continue; | |
369 | } | |
0775436c TO |
370 | $count++; |
371 | } | |
372 | } | |
373 | $this->recursionDates = $recursionDates; | |
374 | ||
375 | return $recursionDates; | |
376 | } | |
377 | ||
378 | /** | |
fe482240 | 379 | * This function gets all the children for a particular parent entity. |
0775436c | 380 | * |
6a0b768e TO |
381 | * @param int $parentId |
382 | * Parent entity id . | |
383 | * @param string $entityTable | |
384 | * Name of the entity table . | |
385 | * @param bool $includeParent | |
386 | * If true parent id is included in result set and vice versa . | |
387 | * @param int $mode | |
388 | * 1. retrieve only one entity. 2. retrieve all future entities in the repeating set. 3. all entities in the repeating set. . | |
389 | * @param int $initiatorId | |
390 | * The instance where this function is invoked from . | |
0775436c | 391 | * |
0775436c | 392 | * |
a6c01b45 CW |
393 | * @return array |
394 | * an array of child ids | |
0775436c | 395 | */ |
518fa0ee | 396 | public static function getEntitiesForParent($parentId, $entityTable, $includeParent = TRUE, $mode = 3, $initiatorId = NULL) { |
be2fb01f | 397 | $entities = []; |
0775436c TO |
398 | if (empty($parentId) || empty($entityTable)) { |
399 | return $entities; | |
400 | } | |
401 | ||
402 | if (!$initiatorId) { | |
403 | $initiatorId = $parentId; | |
404 | } | |
405 | ||
be2fb01f CW |
406 | $queryParams = [ |
407 | 1 => [$parentId, 'Integer'], | |
408 | 2 => [$entityTable, 'String'], | |
409 | 3 => [$initiatorId, 'Integer'], | |
410 | ]; | |
0775436c TO |
411 | |
412 | if (!$mode) { | |
413 | $mode = CRM_Core_DAO::singleValueQuery("SELECT mode FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); | |
414 | } | |
415 | ||
416 | $query = "SELECT * | |
417 | FROM civicrm_recurring_entity | |
418 | WHERE parent_id = %1 AND entity_table = %2"; | |
419 | if (!$includeParent) { | |
420 | $query .= " AND entity_id != " . ($initiatorId ? "%3" : "%1"); | |
421 | } | |
422 | ||
e7483cbe J |
423 | // MODE = SINGLE |
424 | if ($mode == '1') { | |
0775436c TO |
425 | $query .= " AND entity_id = %3"; |
426 | } | |
e7483cbe J |
427 | // MODE = FUTURE |
428 | elseif ($mode == '2') { | |
0775436c TO |
429 | $recurringEntityID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_recurring_entity WHERE entity_id = %3 AND entity_table = %2", $queryParams); |
430 | if ($recurringEntityID) { | |
431 | $query .= $includeParent ? " AND id >= %4" : " AND id > %4"; | |
518fa0ee SL |
432 | // FIXME: change to order by dates |
433 | $query .= " ORDER BY id ASC"; | |
be2fb01f | 434 | $queryParams[4] = [$recurringEntityID, 'Integer']; |
0775436c TO |
435 | } |
436 | else { | |
437 | // something wrong, return empty | |
be2fb01f | 438 | return []; |
0775436c TO |
439 | } |
440 | } | |
441 | ||
442 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
443 | while ($dao->fetch()) { | |
444 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['table'] = $dao->entity_table; | |
445 | $entities["{$dao->entity_table}_{$dao->entity_id}"]['id'] = $dao->entity_id; | |
446 | } | |
447 | return $entities; | |
448 | } | |
449 | ||
450 | /** | |
451 | * This function when passed an entity id checks if it has parent and | |
452 | * returns all other entities that are connected to same parent. | |
453 | * | |
6a0b768e TO |
454 | * @param int $entityId |
455 | * Entity id . | |
456 | * @param string $entityTable | |
457 | * Entity table name . | |
458 | * @param bool $includeParent | |
459 | * Include parent in result set . | |
460 | * @param int $mode | |
461 | * 1. retrieve only one entity. 2. retrieve all future entities in the repeating set. 3. all entities in the repeating set. . | |
0775436c | 462 | * |
0775436c | 463 | * |
a6c01b45 CW |
464 | * @return array |
465 | * array of connected ids | |
0775436c | 466 | */ |
518fa0ee | 467 | public static function getEntitiesFor($entityId, $entityTable, $includeParent = TRUE, $mode = 3) { |
0775436c TO |
468 | $parentId = self::getParentFor($entityId, $entityTable); |
469 | if ($parentId) { | |
470 | return self::getEntitiesForParent($parentId, $entityTable, $includeParent, $mode, $entityId); | |
471 | } | |
be2fb01f | 472 | return []; |
0775436c TO |
473 | } |
474 | ||
475 | /** | |
fe482240 | 476 | * This function gets the parent for the entity id passed to it. |
0775436c | 477 | * |
6a0b768e TO |
478 | * @param int $entityId |
479 | * Entity ID . | |
480 | * @param string $entityTable | |
481 | * Entity table name . | |
482 | * @param bool $includeParent | |
483 | * Include parent in result set . | |
0775436c | 484 | * |
0775436c | 485 | * |
a6c01b45 CW |
486 | * @return int |
487 | * unsigned $parentId Parent ID | |
0775436c | 488 | */ |
518fa0ee | 489 | public static function getParentFor($entityId, $entityTable, $includeParent = TRUE) { |
0775436c TO |
490 | if (empty($entityId) || empty($entityTable)) { |
491 | return NULL; | |
492 | } | |
493 | ||
494 | $query = " | |
495 | SELECT parent_id | |
496 | FROM civicrm_recurring_entity | |
497 | WHERE entity_id = %1 AND entity_table = %2"; | |
498 | if (!$includeParent) { | |
499 | $query .= " AND parent_id != %1"; | |
500 | } | |
e7483cbe J |
501 | $parentId |
502 | = CRM_Core_DAO::singleValueQuery($query, | |
be2fb01f CW |
503 | [ |
504 | 1 => [$entityId, 'Integer'], | |
505 | 2 => [$entityTable, 'String'], | |
506 | ] | |
0775436c TO |
507 | ); |
508 | return $parentId; | |
509 | } | |
510 | ||
04374d9d CW |
511 | /** |
512 | * Finds the position of this entity as well as total count of the repeating set | |
513 | * | |
514 | * @param $entityId | |
515 | * @param $entityTable | |
516 | * @return array|null | |
517 | */ | |
518fa0ee | 518 | public static function getPositionAndCount($entityId, $entityTable) { |
04374d9d CW |
519 | $position = $count = 0; |
520 | ||
521 | $query = " | |
522 | SELECT entity_id | |
523 | FROM civicrm_recurring_entity | |
524 | WHERE parent_id = (SELECT parent_id FROM civicrm_recurring_entity WHERE entity_id = %1 AND entity_table = %2) AND entity_table = %2"; | |
525 | ||
526 | $dao = CRM_Core_DAO::executeQuery($query, | |
be2fb01f CW |
527 | [ |
528 | 1 => [$entityId, 'Integer'], | |
529 | 2 => [$entityTable, 'String'], | |
530 | ] | |
04374d9d CW |
531 | ); |
532 | ||
533 | while ($dao->fetch()) { | |
534 | ++$count; | |
535 | if ($dao->entity_id <= $entityId) { | |
536 | ++$position; | |
537 | } | |
538 | } | |
539 | if ($count) { | |
be2fb01f | 540 | return [$position, $count]; |
04374d9d CW |
541 | } |
542 | return NULL; | |
543 | } | |
544 | ||
0775436c | 545 | /** |
fe482240 | 546 | * This function copies the information from parent entity and creates other entities with same information. |
0775436c | 547 | * |
6a0b768e TO |
548 | * @param string $entityTable |
549 | * Entity table name . | |
550 | * @param array $fromCriteria | |
551 | * Array of all the fields & values on which basis to copy . | |
552 | * @param array $newParams | |
553 | * Array of all the fields & values to be copied besides the other fields . | |
554 | * @param bool $createRecurringEntity | |
555 | * If to create a record in recurring_entity table . | |
0775436c | 556 | * |
0775436c TO |
557 | * |
558 | * @return object | |
ac15829d | 559 | * @throws new CRM_Core_Exception |
0775436c | 560 | */ |
518fa0ee | 561 | public static function copyCreateEntity($entityTable, $fromCriteria, $newParams, $createRecurringEntity = TRUE) { |
0775436c TO |
562 | $daoName = self::$_tableDAOMapper[$entityTable]; |
563 | if (!$daoName) { | |
ac15829d | 564 | throw new CRM_Core_Exception("DAO Mapper missing for $entityTable."); |
0775436c TO |
565 | } |
566 | $newObject = CRM_Core_DAO::copyGeneric($daoName, $fromCriteria, $newParams); | |
567 | ||
568 | if (is_a($newObject, 'CRM_Core_DAO') && $newObject->id && $createRecurringEntity) { | |
353ffa53 | 569 | $object = new $daoName(); |
0775436c TO |
570 | foreach ($fromCriteria as $key => $value) { |
571 | $object->$key = $value; | |
572 | } | |
573 | $object->find(TRUE); | |
574 | ||
575 | CRM_Core_BAO_RecurringEntity::quickAdd($object->id, $newObject->id, $entityTable); | |
576 | } | |
c4517497 MW |
577 | |
578 | CRM_Utils_Hook::copy(CRM_Core_DAO_AllCoreTables::getBriefName($daoName), $newObject); | |
0775436c TO |
579 | return $newObject; |
580 | } | |
581 | ||
582 | /** | |
8eedd10a | 583 | * This function acts as a listener to dao->update whenever there is an update. |
0775436c | 584 | * |
8eedd10a | 585 | * It propagates any changes to all related entities present in recurring entity table |
0775436c | 586 | * |
8eedd10a | 587 | * @param object $event |
588 | * An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just updated. | |
0775436c | 589 | */ |
518fa0ee | 590 | public static function triggerUpdate($event) { |
0775436c TO |
591 | // if DB version is earlier than 4.6 skip any processing |
592 | static $currentVer = NULL; | |
593 | if (!$currentVer) { | |
594 | $currentVer = CRM_Core_BAO_Domain::version(); | |
595 | } | |
596 | if (version_compare($currentVer, '4.6.alpha1') < 0) { | |
597 | return; | |
598 | } | |
599 | ||
be2fb01f | 600 | static $processedEntities = []; |
0775436c TO |
601 | $obj =& $event->object; |
602 | if (empty($obj->id) || empty($obj->__table)) { | |
8eedd10a | 603 | return; |
0775436c TO |
604 | } |
605 | $key = "{$obj->__table}_{$obj->id}"; | |
606 | ||
607 | if (array_key_exists($key, $processedEntities)) { | |
608 | // already processed | |
8eedd10a | 609 | return; |
0775436c TO |
610 | } |
611 | ||
612 | // get related entities | |
613 | $repeatingEntities = self::getEntitiesFor($obj->id, $obj->__table, FALSE, NULL); | |
614 | if (empty($repeatingEntities)) { | |
615 | // return if its not a recurring entity parent | |
8eedd10a | 616 | return; |
0775436c TO |
617 | } |
618 | // mark being processed | |
619 | $processedEntities[$key] = 1; | |
620 | ||
621 | // to make sure we not copying to source itself | |
622 | unset($repeatingEntities[$key]); | |
623 | ||
624 | foreach ($repeatingEntities as $key => $val) { | |
625 | $entityID = $val['id']; | |
626 | $entityTable = $val['table']; | |
627 | ||
628 | $processedEntities[$key] = 1; | |
629 | ||
630 | if (array_key_exists($entityTable, self::$_tableDAOMapper)) { | |
353ffa53 | 631 | $daoName = self::$_tableDAOMapper[$entityTable]; |
0775436c | 632 | |
be2fb01f | 633 | $skipData = []; |
0775436c TO |
634 | if (array_key_exists($entityTable, self::$_updateSkipFields)) { |
635 | $skipFields = self::$_updateSkipFields[$entityTable]; | |
636 | foreach ($skipFields as $sfield) { | |
637 | $skipData[$sfield] = NULL; | |
638 | } | |
639 | } | |
640 | ||
641 | $updateDAO = CRM_Core_DAO::cascadeUpdate($daoName, $obj->id, $entityID, $skipData); | |
0775436c TO |
642 | } |
643 | else { | |
ac15829d | 644 | throw new CRM_Core_Exception("DAO Mapper missing for $entityTable."); |
0775436c TO |
645 | } |
646 | } | |
647 | // done with processing. lets unset static var. | |
648 | unset($processedEntities); | |
649 | } | |
650 | ||
651 | /** | |
652 | * This function acts as a listener to dao->save, | |
653 | * and creates entries for linked entities in recurring entity table | |
654 | * | |
6a0b768e | 655 | * @param object $event |
8eedd10a | 656 | * An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just inserted. |
0775436c | 657 | */ |
518fa0ee | 658 | public static function triggerInsert($event) { |
0775436c TO |
659 | $obj =& $event->object; |
660 | if (!array_key_exists($obj->__table, self::$_linkedEntitiesInfo)) { | |
8eedd10a | 661 | return; |
0775436c TO |
662 | } |
663 | ||
664 | // if DB version is earlier than 4.6 skip any processing | |
665 | static $currentVer = NULL; | |
666 | if (!$currentVer) { | |
667 | $currentVer = CRM_Core_BAO_Domain::version(); | |
668 | } | |
669 | if (version_compare($currentVer, '4.6.alpha1') < 0) { | |
670 | return; | |
671 | } | |
672 | ||
be2fb01f | 673 | static $processedEntities = []; |
0775436c | 674 | if (empty($obj->id) || empty($obj->__table)) { |
8eedd10a | 675 | return; |
0775436c TO |
676 | } |
677 | $key = "{$obj->__table}_{$obj->id}"; | |
678 | ||
679 | if (array_key_exists($key, $processedEntities)) { | |
680 | // already being processed. Exit recursive calls. | |
8eedd10a | 681 | return; |
0775436c TO |
682 | } |
683 | ||
684 | if (self::getStatus() == self::RUNNING) { | |
685 | // if recursion->generate() is doing some work, lets not intercept | |
8eedd10a | 686 | return; |
0775436c TO |
687 | } |
688 | ||
689 | // mark being processed | |
690 | $processedEntities[$key] = 1; | |
691 | ||
692 | // get related entities for table being saved | |
693 | $hasaRecurringRecord = self::getParentFor($obj->id, $obj->__table); | |
694 | ||
695 | if (empty($hasaRecurringRecord)) { | |
696 | // check if its a linked entity | |
697 | if (array_key_exists($obj->__table, self::$_linkedEntitiesInfo) && | |
b99f3e96 | 698 | empty(self::$_linkedEntitiesInfo[$obj->__table]['is_multirecord']) |
353ffa53 | 699 | ) { |
0775436c TO |
700 | $linkedDAO = new self::$_tableDAOMapper[$obj->__table](); |
701 | $linkedDAO->id = $obj->id; | |
702 | if ($linkedDAO->find(TRUE)) { | |
703 | $idCol = self::$_linkedEntitiesInfo[$obj->__table]['entity_id_col']; | |
704 | $tableCol = self::$_linkedEntitiesInfo[$obj->__table]['entity_table_col']; | |
705 | ||
353ffa53 | 706 | $pEntityID = $linkedDAO->$idCol; |
0775436c TO |
707 | $pEntityTable = $linkedDAO->$tableCol; |
708 | ||
709 | // find all parent recurring entity set | |
710 | $pRepeatingEntities = self::getEntitiesFor($pEntityID, $pEntityTable); | |
711 | ||
712 | if (!empty($pRepeatingEntities)) { | |
713 | // for each parent entity in the set, find out a similar linked entity, | |
714 | // if doesn't exist create one, and also create entries in recurring_entity table | |
715 | ||
716 | foreach ($pRepeatingEntities as $key => $val) { | |
717 | if (array_key_exists($key, $processedEntities)) { | |
718 | // this graph is already being processed | |
8eedd10a | 719 | return; |
0775436c TO |
720 | } |
721 | $processedEntities[$key] = 1; | |
722 | } | |
723 | ||
724 | // start with first entry with just itself | |
725 | CRM_Core_BAO_RecurringEntity::quickAdd($obj->id, $obj->id, $obj->__table); | |
726 | ||
727 | foreach ($pRepeatingEntities as $key => $val) { | |
728 | $rlinkedDAO = new self::$_tableDAOMapper[$obj->__table](); | |
729 | $rlinkedDAO->$idCol = $val['id']; | |
730 | $rlinkedDAO->$tableCol = $val['table']; | |
731 | if ($rlinkedDAO->find(TRUE)) { | |
732 | CRM_Core_BAO_RecurringEntity::quickAdd($obj->id, $rlinkedDAO->id, $obj->__table); | |
733 | } | |
734 | else { | |
735 | // linked entity doesn't exist. lets create them | |
be2fb01f | 736 | $newCriteria = [ |
353ffa53 | 737 | $idCol => $val['id'], |
e7483cbe | 738 | $tableCol => $val['table'], |
be2fb01f | 739 | ]; |
0775436c | 740 | $linkedObj = CRM_Core_BAO_RecurringEntity::copyCreateEntity($obj->__table, |
be2fb01f | 741 | ['id' => $obj->id], |
0775436c TO |
742 | $newCriteria, |
743 | TRUE | |
744 | ); | |
745 | if ($linkedObj->id) { | |
746 | CRM_Core_BAO_RecurringEntity::quickAdd($obj->id, $linkedObj->id, $obj->__table); | |
747 | } | |
748 | } | |
749 | } | |
750 | } | |
751 | } | |
752 | } | |
753 | } | |
754 | ||
755 | // done with processing. lets unset static var. | |
756 | unset($processedEntities); | |
757 | } | |
758 | ||
759 | /** | |
760 | * This function acts as a listener to dao->delete, and deletes an entry from recurring_entity table | |
761 | * | |
6a0b768e | 762 | * @param object $event |
8eedd10a | 763 | * An object of /Civi/Core/DAO/Event/PostUpdate containing dao object that was just deleted. |
0775436c | 764 | */ |
518fa0ee | 765 | public static function triggerDelete($event) { |
0775436c TO |
766 | $obj =& $event->object; |
767 | ||
768 | // if DB version is earlier than 4.6 skip any processing | |
769 | static $currentVer = NULL; | |
770 | if (!$currentVer) { | |
771 | $currentVer = CRM_Core_BAO_Domain::version(); | |
772 | } | |
773 | if (version_compare($currentVer, '4.6.alpha1') < 0) { | |
774 | return; | |
775 | } | |
776 | ||
be2fb01f | 777 | static $processedEntities = []; |
0775436c | 778 | if (empty($obj->id) || empty($obj->__table) || !$event->result) { |
8eedd10a | 779 | return; |
0775436c TO |
780 | } |
781 | $key = "{$obj->__table}_{$obj->id}"; | |
782 | ||
783 | if (array_key_exists($key, $processedEntities)) { | |
784 | // already processed | |
8eedd10a | 785 | return; |
0775436c TO |
786 | } |
787 | ||
788 | // mark being processed | |
789 | $processedEntities[$key] = 1; | |
790 | ||
791 | $parentID = self::getParentFor($obj->id, $obj->__table); | |
792 | if ($parentID) { | |
793 | CRM_Core_BAO_RecurringEntity::delEntity($obj->id, $obj->__table, TRUE); | |
794 | } | |
795 | } | |
796 | ||
797 | /** | |
ad37ac8e | 798 | * This function deletes main entity and related linked entities from recurring-entity table. |
0775436c | 799 | * |
6a0b768e | 800 | * @param int $entityId |
72b3a70c | 801 | * Entity id |
6a0b768e | 802 | * @param string $entityTable |
72b3a70c | 803 | * Name of the entity table |
0775436c | 804 | * |
ad37ac8e | 805 | * @param bool $isDelLinkedEntities |
0775436c | 806 | * |
ad37ac8e | 807 | * @return bool|\CRM_Core_DAO_RecurringEntity |
808 | * @throws \Exception | |
0775436c | 809 | */ |
518fa0ee | 810 | public static function delEntity($entityId, $entityTable, $isDelLinkedEntities = FALSE) { |
0775436c TO |
811 | if (empty($entityId) || empty($entityTable)) { |
812 | return FALSE; | |
813 | } | |
814 | $dao = new CRM_Core_DAO_RecurringEntity(); | |
815 | $dao->entity_id = $entityId; | |
816 | $dao->entity_table = $entityTable; | |
817 | if ($dao->find(TRUE)) { | |
818 | // make sure its not a linked entity thats being deleted | |
819 | if ($isDelLinkedEntities && !array_key_exists($entityTable, self::$_linkedEntitiesInfo)) { | |
820 | // delete all linked entities from recurring entity table | |
821 | foreach (self::$_linkedEntitiesInfo as $linkedTable => $linfo) { | |
822 | $daoName = self::$_tableDAOMapper[$linkedTable]; | |
823 | if (!$daoName) { | |
ac15829d | 824 | throw new CRM_Core_Exception("DAO Mapper missing for $linkedTable."); |
0775436c TO |
825 | } |
826 | ||
827 | $linkedDao = new $daoName(); | |
6ef04c72 MM |
828 | $linkedDao->{$linfo['entity_id_col']} = $entityId; |
829 | $linkedDao->{$linfo['entity_table_col']} = $entityTable; | |
0775436c TO |
830 | $linkedDao->find(); |
831 | while ($linkedDao->fetch()) { | |
832 | CRM_Core_BAO_RecurringEntity::delEntity($linkedDao->id, $linkedTable, FALSE); | |
833 | } | |
834 | } | |
835 | } | |
836 | // delete main entity | |
837 | return $dao->delete(); | |
838 | } | |
839 | return FALSE; | |
840 | } | |
841 | ||
842 | /** | |
fe482240 | 843 | * This function maps values posted from form to civicrm_action_schedule columns. |
0775436c | 844 | * |
6a0b768e TO |
845 | * @param array $formParams |
846 | * And array of form values posted . | |
0775436c TO |
847 | * |
848 | * @return array | |
849 | */ | |
be2fb01f CW |
850 | public function mapFormValuesToDB($formParams = []) { |
851 | $dbParams = []; | |
b53cbfbc | 852 | if (!empty($formParams['used_for'])) { |
0775436c TO |
853 | $dbParams['used_for'] = $formParams['used_for']; |
854 | } | |
855 | ||
b53cbfbc | 856 | if (!empty($formParams['entity_id'])) { |
0775436c TO |
857 | $dbParams['entity_value'] = $formParams['entity_id']; |
858 | } | |
859 | ||
b53cbfbc CW |
860 | if (!empty($formParams['repetition_start_date'])) { |
861 | if (!empty($formParams['repetition_start_date_display'])) { | |
0775436c TO |
862 | $repetitionStartDate = $formParams['repetition_start_date_display']; |
863 | } | |
864 | else { | |
865 | $repetitionStartDate = $formParams['repetition_start_date']; | |
866 | } | |
b53cbfbc | 867 | if (!empty($formParams['repetition_start_date_time'])) { |
0775436c TO |
868 | $repetitionStartDate = $repetitionStartDate . " " . $formParams['repetition_start_date_time']; |
869 | } | |
870 | $repetition_start_date = new DateTime($repetitionStartDate); | |
1043bac2 | 871 | $dbParams['start_action_date'] = $repetition_start_date->format('YmdHis'); |
0775436c TO |
872 | } |
873 | ||
b53cbfbc | 874 | if (!empty($formParams['repetition_frequency_unit'])) { |
0775436c TO |
875 | $dbParams['repetition_frequency_unit'] = $formParams['repetition_frequency_unit']; |
876 | } | |
877 | ||
b53cbfbc | 878 | if (!empty($formParams['repetition_frequency_interval'])) { |
0775436c TO |
879 | $dbParams['repetition_frequency_interval'] = $formParams['repetition_frequency_interval']; |
880 | } | |
881 | ||
882 | //For Repeats on:(weekly case) | |
883 | if ($formParams['repetition_frequency_unit'] == 'week') { | |
b53cbfbc | 884 | if (!empty($formParams['start_action_condition'])) { |
9c1bc317 | 885 | $repeats_on = $formParams['start_action_condition'] ?? NULL; |
0775436c TO |
886 | $dbParams['start_action_condition'] = implode(",", array_keys($repeats_on)); |
887 | } | |
888 | } | |
889 | ||
890 | //For Repeats By:(monthly case) | |
891 | if ($formParams['repetition_frequency_unit'] == 'month') { | |
892 | if ($formParams['repeats_by'] == 1) { | |
b53cbfbc | 893 | if (!empty($formParams['limit_to'])) { |
0775436c TO |
894 | $dbParams['limit_to'] = $formParams['limit_to']; |
895 | } | |
896 | } | |
897 | if ($formParams['repeats_by'] == 2) { | |
de6c59ca | 898 | if (!empty($formParams['entity_status_1']) && !empty($formParams['entity_status_2'])) { |
353ffa53 | 899 | $dbParams['entity_status'] = $formParams['entity_status_1'] . " " . $formParams['entity_status_2']; |
0775436c TO |
900 | } |
901 | } | |
902 | } | |
903 | ||
904 | //For "Ends" - After: | |
905 | if ($formParams['ends'] == 1) { | |
b53cbfbc | 906 | if (!empty($formParams['start_action_offset'])) { |
0775436c TO |
907 | $dbParams['start_action_offset'] = $formParams['start_action_offset']; |
908 | } | |
909 | } | |
910 | ||
911 | //For "Ends" - On: | |
912 | if ($formParams['ends'] == 2) { | |
b53cbfbc | 913 | if (!empty($formParams['repeat_absolute_date'])) { |
0775436c TO |
914 | $dbParams['absolute_date'] = CRM_Utils_Date::processDate($formParams['repeat_absolute_date']); |
915 | } | |
916 | } | |
917 | return $dbParams; | |
918 | } | |
919 | ||
920 | /** | |
921 | * This function gets all the columns of civicrm_action_schedule table based on id(primary key) | |
922 | * | |
6a0b768e TO |
923 | * @param int $scheduleReminderId |
924 | * Primary key of civicrm_action_schedule table . | |
0775436c | 925 | * |
0775436c TO |
926 | * |
927 | * @return object | |
928 | */ | |
518fa0ee | 929 | public static function getScheduleReminderDetailsById($scheduleReminderId) { |
0775436c TO |
930 | $query = "SELECT * |
931 | FROM civicrm_action_schedule WHERE 1"; | |
932 | if ($scheduleReminderId) { | |
933 | $query .= " | |
934 | AND id = %1"; | |
935 | } | |
936 | $dao = CRM_Core_DAO::executeQuery($query, | |
be2fb01f CW |
937 | [ |
938 | 1 => [$scheduleReminderId, 'Integer'], | |
939 | ] | |
0775436c TO |
940 | ); |
941 | $dao->fetch(); | |
942 | return $dao; | |
943 | } | |
944 | ||
945 | /** | |
fe482240 | 946 | * wrapper of getScheduleReminderDetailsById function. |
0775436c | 947 | * |
6a0b768e TO |
948 | * @param int $scheduleReminderId |
949 | * Primary key of civicrm_action_schedule table . | |
0775436c TO |
950 | * |
951 | * @return array | |
952 | */ | |
953 | public function getScheduleParams($scheduleReminderId) { | |
be2fb01f | 954 | $scheduleReminderDetails = []; |
0775436c TO |
955 | if ($scheduleReminderId) { |
956 | //Get all the details from schedule reminder table | |
957 | $scheduleReminderDetails = self::getScheduleReminderDetailsById($scheduleReminderId); | |
958 | $scheduleReminderDetails = (array) $scheduleReminderDetails; | |
959 | } | |
960 | return $scheduleReminderDetails; | |
961 | } | |
962 | ||
963 | /** | |
b44e3f84 | 964 | * This function takes criteria saved in civicrm_action_schedule table |
0775436c TO |
965 | * and creates recursion rule |
966 | * | |
6a0b768e | 967 | * @param array $scheduleReminderDetails |
b44e3f84 | 968 | * Array of repeat criteria saved in civicrm_action_schedule table . |
0775436c | 969 | * |
a6c01b45 CW |
970 | * @return object |
971 | * When object | |
0775436c | 972 | */ |
be2fb01f | 973 | public function getRecursionFromSchedule($scheduleReminderDetails = []) { |
0775436c TO |
974 | $r = new When(); |
975 | //If there is some data for this id | |
976 | if ($scheduleReminderDetails['repetition_frequency_unit']) { | |
977 | if ($scheduleReminderDetails['start_action_date']) { | |
978 | $currDate = date('Y-m-d H:i:s', strtotime($scheduleReminderDetails['start_action_date'])); | |
979 | } | |
980 | else { | |
981 | $currDate = date("Y-m-d H:i:s"); | |
982 | } | |
983 | $start = new DateTime($currDate); | |
984 | $this->recursion_start_date = $start; | |
30aa3e54 PZ |
985 | $repetition_frequency_unit = $scheduleReminderDetails['repetition_frequency_unit']; |
986 | if ($repetition_frequency_unit == "day") { | |
987 | $repetition_frequency_unit = "dai"; | |
0775436c | 988 | } |
30aa3e54 PZ |
989 | $repetition_frequency_unit = $repetition_frequency_unit . 'ly'; |
990 | $r->startDate($start) | |
991 | ->exclusions([$start]) | |
992 | ->freq($repetition_frequency_unit); | |
0775436c TO |
993 | |
994 | if ($scheduleReminderDetails['repetition_frequency_interval']) { | |
995 | $r->interval($scheduleReminderDetails['repetition_frequency_interval']); | |
996 | } | |
997 | else { | |
998 | $r->errors[] = 'Repeats every: is a required field'; | |
999 | } | |
1000 | ||
1001 | //week | |
1002 | if ($scheduleReminderDetails['repetition_frequency_unit'] == 'week') { | |
1003 | if ($scheduleReminderDetails['start_action_condition']) { | |
1004 | $startActionCondition = $scheduleReminderDetails['start_action_condition']; | |
1005 | $explodeStartActionCondition = explode(',', $startActionCondition); | |
be2fb01f | 1006 | $buildRuleArray = []; |
0775436c TO |
1007 | foreach ($explodeStartActionCondition as $key => $val) { |
1008 | $buildRuleArray[] = strtoupper(substr($val, 0, 2)); | |
1009 | } | |
1010 | $r->wkst('MO')->byday($buildRuleArray); | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | //month | |
1015 | if ($scheduleReminderDetails['repetition_frequency_unit'] == 'month') { | |
1016 | if ($scheduleReminderDetails['entity_status']) { | |
1017 | $startActionDate = explode(" ", $scheduleReminderDetails['entity_status']); | |
1018 | switch ($startActionDate[0]) { | |
353ffa53 TO |
1019 | case 'first': |
1020 | $startActionDate1 = 1; | |
1021 | break; | |
ea100cb5 | 1022 | |
353ffa53 TO |
1023 | case 'second': |
1024 | $startActionDate1 = 2; | |
1025 | break; | |
ea100cb5 | 1026 | |
353ffa53 TO |
1027 | case 'third': |
1028 | $startActionDate1 = 3; | |
1029 | break; | |
ea100cb5 | 1030 | |
353ffa53 TO |
1031 | case 'fourth': |
1032 | $startActionDate1 = 4; | |
1033 | break; | |
ea100cb5 | 1034 | |
353ffa53 TO |
1035 | case 'last': |
1036 | $startActionDate1 = -1; | |
1037 | break; | |
0775436c | 1038 | } |
353ffa53 | 1039 | $concatStartActionDateBits = $startActionDate1 . strtoupper(substr($startActionDate[1], 0, 2)); |
be2fb01f | 1040 | $r->byday([$concatStartActionDateBits]); |
0775436c TO |
1041 | } |
1042 | elseif ($scheduleReminderDetails['limit_to']) { | |
be2fb01f | 1043 | $r->bymonthday([$scheduleReminderDetails['limit_to']]); |
0775436c TO |
1044 | } |
1045 | } | |
1046 | ||
1047 | //Ends | |
1048 | if ($scheduleReminderDetails['start_action_offset']) { | |
1049 | if ($scheduleReminderDetails['start_action_offset'] > 30) { | |
1050 | $r->errors[] = 'Occurrences should be less than or equal to 30'; | |
1051 | } | |
1052 | $r->count($scheduleReminderDetails['start_action_offset']); | |
1053 | } | |
1054 | ||
b53cbfbc | 1055 | if (!empty($scheduleReminderDetails['absolute_date'])) { |
0775436c | 1056 | $absoluteDate = CRM_Utils_Date::setDateDefaults($scheduleReminderDetails['absolute_date']); |
2069e839 | 1057 | // absolute_date column of scheduled-reminder table is of type date (and not datetime) |
1058 | // and we always want the date to be included, and therefore appending 23:59 | |
1059 | $endDate = new DateTime($absoluteDate[0] . ' ' . '23:59'); | |
0775436c TO |
1060 | $r->until($endDate); |
1061 | } | |
1062 | ||
1063 | if (!$scheduleReminderDetails['start_action_offset'] && !$scheduleReminderDetails['absolute_date']) { | |
1064 | $r->errors[] = 'Ends: is a required field'; | |
1065 | } | |
1066 | } | |
1067 | else { | |
1068 | $r->errors[] = 'Repeats: is a required field'; | |
1069 | } | |
1070 | return $r; | |
1071 | } | |
1072 | ||
0775436c | 1073 | /** |
fe482240 | 1074 | * This function gets time difference between the two datetime object. |
0775436c | 1075 | * |
6a0b768e TO |
1076 | * @param DateTime $startDate |
1077 | * Start Date . | |
1078 | * @param DateTime $endDate | |
1079 | * End Date . | |
0775436c | 1080 | * |
0775436c | 1081 | * |
a6c01b45 CW |
1082 | * @return object |
1083 | * DateTime object which contain time difference | |
0775436c | 1084 | */ |
518fa0ee | 1085 | public static function getInterval($startDate, $endDate) { |
0775436c TO |
1086 | if ($startDate && $endDate) { |
1087 | $startDate = new DateTime($startDate); | |
353ffa53 | 1088 | $endDate = new DateTime($endDate); |
0775436c TO |
1089 | return $startDate->diff($endDate); |
1090 | } | |
1091 | } | |
1092 | ||
1093 | /** | |
fe482240 | 1094 | * This function gets all columns from civicrm_action_schedule on the basis of event id. |
0775436c | 1095 | * |
6a0b768e TO |
1096 | * @param int $entityId |
1097 | * Entity ID . | |
1098 | * @param string $used_for | |
1099 | * Specifies for which entity type it's used for . | |
0775436c | 1100 | * |
0775436c TO |
1101 | * |
1102 | * @return object | |
1103 | */ | |
1104 | public static function getReminderDetailsByEntityId($entityId, $used_for) { | |
1105 | if ($entityId) { | |
1106 | $query = " | |
1107 | SELECT * | |
1108 | FROM civicrm_action_schedule | |
1109 | WHERE entity_value = %1"; | |
1110 | if ($used_for) { | |
1111 | $query .= " AND used_for = %2"; | |
1112 | } | |
be2fb01f CW |
1113 | $params = [ |
1114 | 1 => [$entityId, 'Integer'], | |
1115 | 2 => [$used_for, 'String'], | |
1116 | ]; | |
0775436c TO |
1117 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
1118 | $dao->fetch(); | |
1119 | } | |
1120 | return $dao; | |
1121 | } | |
1122 | ||
1123 | /** | |
fe482240 | 1124 | * Update mode column in civicrm_recurring_entity table for event related tabs. |
0775436c | 1125 | * |
6a0b768e TO |
1126 | * @param int $entityId |
1127 | * Event id . | |
1128 | * @param string $linkedEntityTable | |
1129 | * Linked entity table name for this event . | |
ad37ac8e | 1130 | * @param string $mainEntityTable |
1131 | * | |
0775436c TO |
1132 | * @return array |
1133 | */ | |
1134 | public static function updateModeLinkedEntity($entityId, $linkedEntityTable, $mainEntityTable) { | |
be2fb01f | 1135 | $result = []; |
353ffa53 | 1136 | if ($entityId && $linkedEntityTable && $mainEntityTable) { |
f3acfdd9 | 1137 | if (!empty(self::$_tableDAOMapper[$linkedEntityTable])) { |
0775436c TO |
1138 | $dao = self::$_tableDAOMapper[$linkedEntityTable]; |
1139 | } | |
1140 | else { | |
1141 | CRM_Core_Session::setStatus('Could not update mode for linked entities'); | |
e7483cbe | 1142 | return NULL; |
0775436c TO |
1143 | } |
1144 | $entityTable = $linkedEntityTable; | |
be2fb01f | 1145 | $params = [ |
353ffa53 | 1146 | 'entity_id' => $entityId, |
e7483cbe | 1147 | 'entity_table' => $mainEntityTable, |
be2fb01f CW |
1148 | ]; |
1149 | $defaults = []; | |
0775436c | 1150 | CRM_Core_DAO::commonRetrieve($dao, $params, $defaults); |
b53cbfbc | 1151 | if (!empty($defaults['id'])) { |
0775436c TO |
1152 | $result['entityId'] = $defaults['id']; |
1153 | $result['entityTable'] = $entityTable; | |
1154 | } | |
1155 | } | |
1156 | return $result; | |
1157 | } | |
96025800 | 1158 | |
543aa76e AP |
1159 | /** |
1160 | * Update mode in civicrm_recurring_entity table for event related data and price set in civicrm_price_set_entity. | |
1161 | * | |
1162 | * @param int $entityId | |
1163 | * Event id . | |
1164 | * @param string $entityTable | |
1165 | * @param string $mode | |
1166 | * @param string $linkedEntityTable | |
1167 | * Linked entity table name for this event . | |
1168 | * @param string $priceSet | |
1169 | * Price set of the event . | |
1170 | * | |
1171 | * @return array | |
1172 | */ | |
1173 | public static function updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet) { | |
be2fb01f | 1174 | $finalResult = []; |
543aa76e AP |
1175 | |
1176 | if (!empty($linkedEntityTable)) { | |
1177 | $result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $linkedEntityTable, $entityTable); | |
1178 | } | |
1179 | ||
1180 | $dao = new CRM_Core_DAO_RecurringEntity(); | |
1181 | if (!empty($result)) { | |
1182 | $dao->entity_id = $result['entityId']; | |
1183 | $dao->entity_table = $result['entityTable']; | |
1184 | } | |
1185 | else { | |
1186 | $dao->entity_id = $entityId; | |
1187 | $dao->entity_table = $entityTable; | |
1188 | } | |
1189 | ||
1190 | if ($dao->find(TRUE)) { | |
1191 | $dao->mode = $mode; | |
1192 | $dao->save(); | |
1193 | ||
55d676f2 AP |
1194 | if ($priceSet) { |
1195 | //CRM-20787 Fix | |
1196 | //I am not sure about other fields, if mode = 3 apply for an event then other fields | |
1197 | //should be save for all other series events or not so applying for price set only for now here. | |
1198 | if (CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES === $mode) { | |
1199 | //Step-1: Get all events of series | |
1200 | $seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable); | |
1201 | foreach ($seriesEventRecords as $event) { | |
1202 | //Step-2: Save price set in other series events | |
518fa0ee SL |
1203 | //Remove existing priceset |
1204 | if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) { | |
55d676f2 AP |
1205 | CRM_Core_BAO_Discount::del($event['id'], $event['table']); |
1206 | } | |
518fa0ee SL |
1207 | //Add new price set |
1208 | CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); | |
543aa76e AP |
1209 | } |
1210 | } | |
55d676f2 AP |
1211 | |
1212 | if (CRM_Core_BAO_RecurringEntity::MODE_NEXT_ALL_ENTITY === $mode) { | |
1213 | //Step-1: Get all events of series | |
1214 | $seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable); | |
1215 | foreach ($seriesEventRecords as $event) { | |
1216 | //Step-2: Save price set in other series events | |
1217 | if ($entityId < $event["id"]) { | |
518fa0ee SL |
1218 | //Remove existing priceset |
1219 | if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) { | |
55d676f2 AP |
1220 | CRM_Core_BAO_Discount::del($event['id'], $event['table']); |
1221 | } | |
518fa0ee SL |
1222 | //Add new price set |
1223 | CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); | |
55d676f2 AP |
1224 | } |
1225 | } | |
1226 | } | |
543aa76e | 1227 | } |
55d676f2 | 1228 | |
543aa76e AP |
1229 | //CRM-20787 - Fix end |
1230 | $finalResult['status'] = 'Done'; | |
1231 | } | |
1232 | else { | |
1233 | $finalResult['status'] = 'Error'; | |
1234 | } | |
1235 | ||
1236 | return $finalResult; | |
1237 | } | |
1238 | ||
30aa3e54 PZ |
1239 | /** |
1240 | * Get next occurrence for the given date | |
1241 | * | |
1242 | * @param \DateTime $occurDate | |
1243 | * @param bool $strictly_after | |
1244 | * | |
1245 | * @return bool | |
1246 | */ | |
1247 | private function getNextOccurrence($occurDate, $strictly_after = TRUE) { | |
1248 | try { | |
1249 | return $this->recursion->getNextOccurrence($occurDate, $strictly_after); | |
1250 | } | |
1251 | catch (Exception $exception) { | |
1252 | CRM_Core_Session::setStatus(ts($exception->getMessage())); | |
1253 | } | |
1254 | return FALSE; | |
1255 | } | |
1256 | ||
0775436c | 1257 | } |