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