Merge pull request #7031 from saurabhbatra96/comment-fixes-activity
[civicrm-core.git] / Civi / ActionSchedule / RecipientBuilder.php
CommitLineData
c09bacfd 1<?php
546a1ecc
TO
2/*
3 +--------------------------------------------------------------------+
3435af9a 4 | CiviCRM version 4.7 |
546a1ecc
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
c09bacfd
TO
28namespace Civi\ActionSchedule;
29
546a1ecc
TO
30/**
31 * Class RecipientBuilder
32 * @package Civi\ActionSchedule
33 *
34 * The RecipientBuilder prepares a list of recipients based on an action-schedule.
35 *
36 * This is a four-step process, with different steps depending on:
37 *
38 * (a) How the recipient is identified. Sometimes recipients are identified based
39 * on their relations (e.g. selecting the assignees of an activity or the
40 * participants of an event), and sometimes they are manually added using
41 * a flat contact list (e.g. with a contact ID or group ID).
42 * (b) Whether this is the first reminder or a follow-up/repeated reminder.
43 *
44 * The permutations of these (a)+(b) produce four phases -- RELATION_FIRST,
45 * RELATION_REPEAT, ADDITION_FIRST, ADDITION_REPEAT.
46 *
47 * Each phase requires running a complex query. As a general rule,
48 * MappingInterface::createQuery() produces a base query, and the RecipientBuilder
49 * appends extra bits (JOINs/WHEREs/GROUP BYs) depending on which step is running.
50 *
51 * For example, suppose we want to send reminders to anyone who registers for
52 * a "Conference" or "Exhibition" event with the 'pay later' option, and we want
53 * to fire the reminders X days after the registration date. The
54 * MappingInterface::createQuery() could return a query like:
55 *
56 * @code
57 * CRM_Utils_SQL_Select::from('civicrm_participant e')
58 * ->join('event', 'INNER JOIN civicrm_event event ON e.event_id = event.id')
59 * ->where('e.is_pay_later = 1')
60 * ->where('event.event_type_id IN (#myEventTypes)')
61 * ->param('myEventTypes', array(2, 5))
62 * ->param('casDateField', 'e.register_date')
efc40454 63 * ->param($defaultParams)
546a1ecc
TO
64 * ...etc...
65 * @endcode
66 *
67 * In the RELATION_FIRST phase, RecipientBuilder adds a LEFT-JOIN+WHERE to find
68 * participants who have *not* yet received any reminder, and filters those
69 * participants based on whether X days have passed since "e.register_date".
70 *
71 * Notice that the query may define several SQL elements directly (eg
72 * via `from()`, `where()`, `join()`, `groupBy()`). Additionally, it
73 * must define some parameters (eg `casDateField`). These parameters will be
74 * read by RecipientBuilder and used in other parts of the query.
75 *
76 * At time of writing, these parameters are required:
77 * - casAddlCheckFrom: string, SQL FROM expression
78 * - casContactIdField: string, SQL column expression
79 * - casDateField: string, SQL column expression
80 * - casEntityIdField: string, SQL column expression
81 *
82 * Some parameters are optional:
83 * - casContactTableAlias: string, SQL table alias
84 * - casAnniversaryMode: bool
85 * - casUseReferenceDate: bool
86 *
87 * Additionally, some parameters are automatically predefined:
88 * - casNow
89 * - casMappingEntity: string, SQL table name
90 * - casMappingId: int
91 * - casActionScheduleId: int
92 *
93 * Note: Any parameters defined by the core Civi\ActionSchedule subsystem
94 * use the prefix `cas`. If you define new parameters (like `myEventTypes`
95 * above), then use a different name (to avoid conflicts).
96 */
c09bacfd
TO
97class RecipientBuilder {
98
99 private $now;
546a1ecc
TO
100
101 /**
102 * Generate action_log's for new, first-time alerts to related contacts.
103 *
104 * @see buildRelFirstPass
105 */
106 const PHASE_RELATION_FIRST = 'rel-first';
107
108 /**
109 * Generate action_log's for new, first-time alerts to additional contacts.
110 *
111 * @see buildAddlFirstPass
112 */
113 const PHASE_ADDITION_FIRST = 'addl-first';
114
115 /**
116 * Generate action_log's for repeated, follow-up alerts to related contacts.
117 *
118 * @see buildRelRepeatPass
119 */
120 const PHASE_RELATION_REPEAT = 'rel-repeat';
121
122 /**
123 * Generate action_log's for repeated, follow-up alerts to additional contacts.
124 *
125 * @see buildAddlRepeatPass
126 */
127 const PHASE_ADDITION_REPEAT = 'addl-repeat';
c09bacfd
TO
128
129 /**
130 * @var \CRM_Core_DAO_ActionSchedule
131 */
132 private $actionSchedule;
133
134 /**
546a1ecc 135 * @var MappingInterface
c09bacfd
TO
136 */
137 private $mapping;
138
546a1ecc
TO
139 /**
140 * @param $now
141 * @param \CRM_Core_DAO_ActionSchedule $actionSchedule
142 * @param MappingInterface $mapping
143 */
c09bacfd
TO
144 public function __construct($now, $actionSchedule, $mapping) {
145 $this->now = $now;
146 $this->actionSchedule = $actionSchedule;
147 $this->mapping = $mapping;
148 }
149
150 /**
151 * Fill the civicrm_action_log with any new/missing TODOs.
152 *
153 * @throws \CRM_Core_Exception
154 */
155 public function build() {
c09bacfd
TO
156 $this->buildRelFirstPass();
157
c09bacfd
TO
158 if ($this->prepareAddlFilter('c.id')) {
159 $this->buildAddlFirstPass();
160 }
161
c09bacfd
TO
162 if ($this->actionSchedule->is_repeat) {
163 $this->buildRelRepeatPass();
164 }
165
c09bacfd
TO
166 if ($this->actionSchedule->is_repeat && $this->prepareAddlFilter('c.id')) {
167 $this->buildAddlRepeatPass();
168 }
169 }
170
171 /**
546a1ecc
TO
172 * Generate action_log's for new, first-time alerts to related contacts.
173 *
c09bacfd
TO
174 * @throws \Exception
175 */
176 protected function buildRelFirstPass() {
546a1ecc 177 $query = $this->prepareQuery(self::PHASE_RELATION_FIRST);
c09bacfd 178
efc40454 179 $startDateClauses = $this->prepareStartDateClauses();
c09bacfd
TO
180
181 $firstQuery = $query->copy()
546a1ecc 182 ->merge($this->selectIntoActionLog(self::PHASE_RELATION_FIRST, $query))
c09bacfd
TO
183 ->merge($this->joinReminder('LEFT JOIN', 'rel', $query))
184 ->where("reminder.id IS NULL")
185 ->where($startDateClauses)
186 ->strict()
187 ->toSQL();
188 \CRM_Core_DAO::executeQuery($firstQuery);
189
190 // In some cases reference_date got outdated due to many reason e.g. In Membership renewal end_date got extended
191 // which means reference date mismatches with the end_date where end_date may be used as the start_action_date
192 // criteria for some schedule reminder so in order to send new reminder we INSERT new reminder with new reference_date
193 // value via UNION operation
194 if (!empty($query['casUseReferenceDate'])) {
195 $referenceQuery = $query->copy()
546a1ecc 196 ->merge($this->selectIntoActionLog(self::PHASE_RELATION_FIRST, $query))
c09bacfd
TO
197 ->merge($this->joinReminder('LEFT JOIN', 'rel', $query))
198 ->where("reminder.id IS NOT NULL")
199 ->where($startDateClauses)
200 ->where("reminder.action_date_time IS NOT NULL AND reminder.reference_date IS NOT NULL")
201 ->groupBy("reminder.id, reminder.reference_date")
202 ->having("reminder.id = MAX(reminder.id) AND reminder.reference_date <> !casDateField")
203 ->strict()
204 ->toSQL();
205 \CRM_Core_DAO::executeQuery($referenceQuery);
206 }
207 }
208
209 /**
546a1ecc
TO
210 * Generate action_log's for new, first-time alerts to additional contacts.
211 *
c09bacfd
TO
212 * @throws \Exception
213 */
214 protected function buildAddlFirstPass() {
546a1ecc 215 $query = $this->prepareQuery(self::PHASE_ADDITION_FIRST);
c09bacfd
TO
216
217 $insertAdditionalSql = \CRM_Utils_SQL_Select::from("civicrm_contact c")
efc40454 218 ->merge($query, array('params'))
546a1ecc 219 ->merge($this->selectIntoActionLog(self::PHASE_ADDITION_FIRST, $query))
c09bacfd
TO
220 ->merge($this->joinReminder('LEFT JOIN', 'addl', $query))
221 ->where("c.is_deleted = 0 AND c.is_deceased = 0")
222 ->merge($this->prepareAddlFilter('c.id'))
223 ->where("c.id NOT IN (
224 SELECT rem.contact_id
9e1bf145 225 FROM civicrm_action_log rem INNER JOIN {$this->mapping->getEntity()} e ON rem.entity_id = e.id
c09bacfd 226 WHERE rem.action_schedule_id = {$this->actionSchedule->id}
9e1bf145 227 AND rem.entity_table = '{$this->mapping->getEntity()}'
c09bacfd
TO
228 )")
229 // Where does e.id come from here? ^^^
230 ->groupBy("c.id")
231 ->strict()
232 ->toSQL();
233 \CRM_Core_DAO::executeQuery($insertAdditionalSql);
234 }
235
236 /**
546a1ecc
TO
237 * Generate action_log's for repeated, follow-up alerts to related contacts.
238 *
c09bacfd
TO
239 * @throws \CRM_Core_Exception
240 * @throws \Exception
241 */
242 protected function buildRelRepeatPass() {
546a1ecc 243 $query = $this->prepareQuery(self::PHASE_RELATION_REPEAT);
efc40454 244 $startDateClauses = $this->prepareStartDateClauses();
c09bacfd
TO
245
246 // CRM-15376 - do not send our reminders if original criteria no longer applies
247 // the first part of the startDateClause array is the earliest the reminder can be sent. If the
248 // event (e.g membership_end_date) has changed then the reminder may no longer apply
249 // @todo - this only handles events that get moved later. Potentially they might get moved earlier
250 $repeatInsert = $query
251 ->merge($this->joinReminder('INNER JOIN', 'rel', $query))
546a1ecc 252 ->merge($this->selectActionLogFields(self::PHASE_RELATION_REPEAT, $query))
c09bacfd
TO
253 ->select("MAX(reminder.action_date_time) as latest_log_time")
254 ->merge($this->prepareRepetitionEndFilter($query['casDateField']))
255 ->where($this->actionSchedule->start_action_date ? $startDateClauses[0] : array())
256 ->groupBy("reminder.contact_id, reminder.entity_id, reminder.entity_table")
281905e6 257 ->having("TIMESTAMPDIFF(HOUR, latest_log_time, CAST(!casNow AS datetime)) >= TIMESTAMPDIFF(HOUR, latest_log_time, DATE_ADD(latest_log_time, INTERVAL !casRepetitionInterval))")
c09bacfd 258 ->param(array(
281905e6 259 'casRepetitionInterval' => $this->parseRepetitionInterval(),
c09bacfd
TO
260 ))
261 ->strict()
262 ->toSQL();
263
264 // For unknown reasons, we manually insert each row. Why not change
265 // selectActionLogFields() to selectIntoActionLog() above?
266
267 $arrValues = \CRM_Core_DAO::executeQuery($repeatInsert)->fetchAll();
268 if ($arrValues) {
269 \CRM_Core_DAO::executeQuery(
270 \CRM_Utils_SQL_Insert::into('civicrm_action_log')
271 ->columns(array('contact_id', 'entity_id', 'entity_table', 'action_schedule_id'))
272 ->rows($arrValues)
273 ->toSQL()
274 );
275 }
276 }
546a1ecc 277
c09bacfd 278 /**
546a1ecc
TO
279 * Generate action_log's for repeated, follow-up alerts to additional contacts.
280 *
c09bacfd
TO
281 * @throws \CRM_Core_Exception
282 * @throws \Exception
283 */
284 protected function buildAddlRepeatPass() {
546a1ecc 285 $query = $this->prepareQuery(self::PHASE_ADDITION_REPEAT);
c09bacfd
TO
286
287 $addlCheck = \CRM_Utils_SQL_Select::from($query['casAddlCheckFrom'])
288 ->select('*')
289 ->merge($query, array('wheres'))// why only where? why not the joins?
290 ->merge($this->prepareRepetitionEndFilter($query['casDateField']))
291 ->limit(1)
292 ->strict()
293 ->toSQL();
294
295 $daoCheck = \CRM_Core_DAO::executeQuery($addlCheck);
296 if ($daoCheck->fetch()) {
297 $repeatInsertAddl = \CRM_Utils_SQL_Select::from('civicrm_contact c')
546a1ecc 298 ->merge($this->selectActionLogFields(self::PHASE_ADDITION_REPEAT, $query))
c09bacfd
TO
299 ->merge($this->joinReminder('INNER JOIN', 'addl', $query))
300 ->select("MAX(reminder.action_date_time) as latest_log_time")
301 ->merge($this->prepareAddlFilter('c.id'))
302 ->where("c.is_deleted = 0 AND c.is_deceased = 0")
303 ->groupBy("reminder.contact_id")
281905e6 304 ->having("TIMESTAMPDIFF(HOUR, latest_log_time, CAST(!casNow AS datetime)) >= TIMESTAMPDIFF(HOUR, latest_log_time, DATE_ADD(latest_log_time, INTERVAL !casRepetitionInterval)")
c09bacfd 305 ->param(array(
281905e6 306 'casRepetitionInterval' => $this->parseRepetitionInterval(),
c09bacfd
TO
307 ))
308 ->strict()
309 ->toSQL();
310
311 // For unknown reasons, we manually insert each row. Why not change
312 // selectActionLogFields() to selectIntoActionLog() above?
313
314 $addValues = \CRM_Core_DAO::executeQuery($repeatInsertAddl)->fetchAll();
315 if ($addValues) {
316 \CRM_Core_DAO::executeQuery(
317 \CRM_Utils_SQL_Insert::into('civicrm_action_log')
318 ->columns(array('contact_id', 'entity_id', 'entity_table', 'action_schedule_id'))
319 ->rows($addValues)
320 ->toSQL()
321 );
322 }
323 }
324 }
325
326 /**
327 * @param string $phase
328 * @return \CRM_Utils_SQL_Select
329 * @throws \CRM_Core_Exception
330 */
331 protected function prepareQuery($phase) {
efc40454 332 $defaultParams = array(
c09bacfd 333 'casActionScheduleId' => $this->actionSchedule->id,
9e1bf145
TO
334 'casMappingId' => $this->mapping->getId(),
335 'casMappingEntity' => $this->mapping->getEntity(),
546a1ecc 336 'casNow' => $this->now,
efc40454
TO
337 );
338
339 /** @var \CRM_Utils_SQL_Select $query */
340 $query = $this->mapping->createQuery($this->actionSchedule, $phase, $defaultParams);
c09bacfd
TO
341
342 if ($this->actionSchedule->limit_to /*1*/) {
343 $query->merge($this->prepareContactFilter($query['casContactIdField']));
344 }
345
346 if (empty($query['casContactTableAlias'])) {
347 $query['casContactTableAlias'] = 'c';
348 $query->join('c', "INNER JOIN civicrm_contact c ON c.id = !casContactIdField AND c.is_deleted = 0 AND c.is_deceased = 0 ");
349 }
350 $multilingual = \CRM_Core_I18n::isMultilingual();
351 if ($multilingual && !empty($this->actionSchedule->filter_contact_language)) {
352 $query->where($this->prepareLanguageFilter($query['casContactTableAlias']));
353 }
354
355 return $query;
356 }
357
358 /**
359 * @param $actionSchedule
360 * @return int|string
361 */
281905e6 362 protected function parseRepetitionInterval() {
c09bacfd
TO
363 $actionSchedule = $this->actionSchedule;
364 if ($actionSchedule->repetition_frequency_unit == 'day') {
281905e6 365 $interval = "{$actionSchedule->repetition_frequency_interval} DAY";
c09bacfd
TO
366 }
367 elseif ($actionSchedule->repetition_frequency_unit == 'week') {
281905e6 368 $interval = "{$actionSchedule->repetition_frequency_interval} WEEK";
c09bacfd
TO
369 }
370 elseif ($actionSchedule->repetition_frequency_unit == 'month') {
281905e6 371 $interval = "{$actionSchedule->repetition_frequency_interval} MONTH";
c09bacfd
TO
372 }
373 elseif ($actionSchedule->repetition_frequency_unit == 'year') {
281905e6 374 $interval = "{$actionSchedule->repetition_frequency_interval} YEAR";
c09bacfd
TO
375 }
376 else {
281905e6 377 $interval = "{$actionSchedule->repetition_frequency_interval} HOUR";
c09bacfd 378 }
281905e6 379 return $interval;
c09bacfd
TO
380 }
381
382 /**
383 * Prepare filter options for limiting by contact ID or group ID.
384 *
385 * @param string $contactIdField
386 * @return \CRM_Utils_SQL_Select
387 */
388 protected function prepareContactFilter($contactIdField) {
389 $actionSchedule = $this->actionSchedule;
390
391 if ($actionSchedule->group_id) {
392 if ($this->isSmartGroup($actionSchedule->group_id)) {
393 // Check that the group is in place in the cache and up to date
394 \CRM_Contact_BAO_GroupContactCache::check($actionSchedule->group_id);
395 return \CRM_Utils_SQL_Select::fragment()
396 ->join('grp', "INNER JOIN civicrm_group_contact_cache grp ON {$contactIdField} = grp.contact_id")
397 ->where(" grp.group_id IN ({$actionSchedule->group_id})");
398 }
399 else {
400 return \CRM_Utils_SQL_Select::fragment()
401 ->join('grp', " INNER JOIN civicrm_group_contact grp ON {$contactIdField} = grp.contact_id AND grp.status = 'Added'")
402 ->where(" grp.group_id IN ({$actionSchedule->group_id})");
403 }
404 }
405 elseif (!empty($actionSchedule->recipient_manual)) {
406 $rList = \CRM_Utils_Type::escape($actionSchedule->recipient_manual, 'String');
407 return \CRM_Utils_SQL_Select::fragment()
408 ->where("{$contactIdField} IN ({$rList})");
409 }
410 return NULL;
411 }
412
413 /**
414 * @param $actionSchedule
415 * @param $contactTableAlias
416 * @return string
417 */
418 protected function prepareLanguageFilter($contactTableAlias) {
419 $actionSchedule = $this->actionSchedule;
420
421 // get language filter for the schedule
422 $filter_contact_language = explode(\CRM_Core_DAO::VALUE_SEPARATOR, $actionSchedule->filter_contact_language);
423 $w = '';
424 if (($key = array_search(\CRM_Core_I18n::NONE, $filter_contact_language)) !== FALSE) {
425 $w .= "{$contactTableAlias}.preferred_language IS NULL OR {$contactTableAlias}.preferred_language = '' OR ";
426 unset($filter_contact_language[$key]);
427 }
428 if (count($filter_contact_language) > 0) {
429 $w .= "{$contactTableAlias}.preferred_language IN ('" . implode("','", $filter_contact_language) . "')";
430 }
431 $w = "($w)";
432 return $w;
433 }
434
435 /**
c09bacfd
TO
436 * @return array
437 */
efc40454 438 protected function prepareStartDateClauses() {
c09bacfd 439 $actionSchedule = $this->actionSchedule;
c09bacfd
TO
440 $startDateClauses = array();
441 if ($actionSchedule->start_action_date) {
442 $op = ($actionSchedule->start_action_condition == 'before' ? '<=' : '>=');
443 $operator = ($actionSchedule->start_action_condition == 'before' ? 'DATE_SUB' : 'DATE_ADD');
efc40454
TO
444 $date = $operator . "(!casDateField, INTERVAL {$actionSchedule->start_action_offset} {$actionSchedule->start_action_unit})";
445 $startDateClauses[] = "'!casNow' >= {$date}";
c09bacfd 446 // This is weird. Waddupwidat?
efc40454
TO
447 if ($this->mapping->getEntity() == 'civicrm_participant') {
448 $startDateClauses[] = $operator . "(!casNow, INTERVAL 1 DAY ) {$op} " . '!casDateField';
c09bacfd
TO
449 }
450 else {
efc40454 451 $startDateClauses[] = "DATE_SUB(!casNow, INTERVAL 1 DAY ) <= {$date}";
c09bacfd
TO
452 }
453 }
454 elseif ($actionSchedule->absolute_date) {
efc40454 455 $startDateClauses[] = "DATEDIFF(DATE('!casNow'),'{$actionSchedule->absolute_date}') = 0";
c09bacfd
TO
456 }
457 return $startDateClauses;
458 }
459
460 /**
461 * @param int $groupId
462 * @return bool
463 */
464 protected function isSmartGroup($groupId) {
465 // Then decide which table to join onto the query
466 $group = \CRM_Contact_DAO_Group::getTableName();
467
468 // Get the group information
469 $sql = "
470SELECT $group.id, $group.cache_date, $group.saved_search_id, $group.children
471FROM $group
472WHERE $group.id = {$groupId}
473";
474
475 $groupDAO = \CRM_Core_DAO::executeQuery($sql);
476 if (
477 $groupDAO->fetch() &&
478 !empty($groupDAO->saved_search_id)
479 ) {
480 return TRUE;
481 }
482 return FALSE;
483 }
484
485 /**
486 * @param string $dateField
487 * @return \CRM_Utils_SQL_Select
488 */
489 protected function prepareRepetitionEndFilter($dateField) {
490 $repeatEventDateExpr = ($this->actionSchedule->end_action == 'before' ? 'DATE_SUB' : 'DATE_ADD')
491 . "({$dateField}, INTERVAL {$this->actionSchedule->end_frequency_interval} {$this->actionSchedule->end_frequency_unit})";
492
493 return \CRM_Utils_SQL_Select::fragment()
546a1ecc 494 ->where("@casNow <= !repetitionEndDate")
c09bacfd 495 ->param(array(
c09bacfd
TO
496 '!repetitionEndDate' => $repeatEventDateExpr,
497 ));
498 }
499
c09bacfd
TO
500 /**
501 * @param string $contactIdField
502 * @return \CRM_Utils_SQL_Select|null
503 */
504 protected function prepareAddlFilter($contactIdField) {
505 $contactAddlFilter = NULL;
506 if ($this->actionSchedule->limit_to !== NULL && !$this->actionSchedule->limit_to /*0*/) {
507 $contactAddlFilter = $this->prepareContactFilter($contactIdField);
508 }
509 return $contactAddlFilter;
510 }
511
c09bacfd
TO
512 /**
513 * Generate a query fragment like for populating
514 * action logs, e.g.
515 *
516 * "SELECT contact_id, entity_id, entity_table, action schedule_id"
517 *
518 * @param string $phase
519 * @param \CRM_Utils_SQL_Select $query
520 * @return \CRM_Utils_SQL_Select
521 * @throws \CRM_Core_Exception
522 */
523 protected function selectActionLogFields($phase, $query) {
524 switch ($phase) {
546a1ecc
TO
525 case self::PHASE_RELATION_FIRST:
526 case self::PHASE_RELATION_REPEAT:
c09bacfd
TO
527 $fragment = \CRM_Utils_SQL_Select::fragment();
528 // CRM-15376: We are not tracking the reference date for 'repeated' schedule reminders.
529 if (!empty($query['casUseReferenceDate'])) {
530 $fragment->select($query['casDateField']);
531 }
532 $fragment->select(
533 array(
534 "!casContactIdField as contact_id",
535 "!casEntityIdField as entity_id",
536 "@casMappingEntity as entity_table",
537 "#casActionScheduleId as action_schedule_id",
538 )
539 );
540 break;
541
546a1ecc
TO
542 case self::PHASE_ADDITION_FIRST:
543 case self::PHASE_ADDITION_REPEAT:
c09bacfd
TO
544 $fragment = \CRM_Utils_SQL_Select::fragment();
545 $fragment->select(
546 array(
547 "c.id as contact_id",
548 "c.id as entity_id",
549 "'civicrm_contact' as entity_table",
550 "#casActionScheduleId as action_schedule_id",
551 )
552 );
553 break;
554
555 default:
556 throw new \CRM_Core_Exception("Unrecognized phase: $phase");
557 }
558 return $fragment;
559 }
560
561 /**
562 * Generate a query fragment like for populating
563 * action logs, e.g.
564 *
565 * "INSERT INTO civicrm_action_log (...) SELECT (...)"
566 *
567 * @param string $phase
568 * @param \CRM_Utils_SQL_Select $query
569 * @return \CRM_Utils_SQL_Select
570 * @throws \CRM_Core_Exception
571 */
572 protected function selectIntoActionLog($phase, $query) {
573 $actionLogColumns = array(
574 "contact_id",
575 "entity_id",
576 "entity_table",
577 "action_schedule_id",
578 );
546a1ecc 579 if ($phase === self::PHASE_RELATION_FIRST || $phase === self::PHASE_RELATION_REPEAT) {
c09bacfd
TO
580 if (!empty($query['casUseReferenceDate'])) {
581 array_unshift($actionLogColumns, 'reference_date');
582 }
583 }
584
585 return $this->selectActionLogFields($phase, $query)
586 ->insertInto('civicrm_action_log', $actionLogColumns);
587 }
588
589 /**
590 * Add a JOIN clause like "INNER JOIN civicrm_action_log reminder ON...".
591 *
592 * @param string $joinType
593 * Join type (eg INNER JOIN, LEFT JOIN).
594 * @param string $for
595 * Ex: 'rel', 'addl'.
596 * @param \CRM_Utils_SQL_Select $query
597 * @return \CRM_Utils_SQL_Select
598 * @throws \CRM_Core_Exception
599 */
600 protected function joinReminder($joinType, $for, $query) {
601 switch ($for) {
602 case 'rel':
603 $contactIdField = $query['casContactIdField'];
9e1bf145 604 $entityName = $this->mapping->getEntity();
c09bacfd
TO
605 $entityIdField = $query['casEntityIdField'];
606 break;
607
608 case 'addl':
609 $contactIdField = 'c.id';
610 $entityName = 'civicrm_contact';
611 $entityIdField = 'c.id';
612 break;
613
614 default:
615 throw new \CRM_Core_Exception("Unrecognized 'for': $for");
616 }
617
618 $joinClause = "civicrm_action_log reminder ON reminder.contact_id = {$contactIdField} AND
619reminder.entity_id = {$entityIdField} AND
620reminder.entity_table = '{$entityName}' AND
621reminder.action_schedule_id = {$this->actionSchedule->id}";
622
623 // Why do we only include anniversary clause for 'rel' queries?
624 if ($for === 'rel' && !empty($query['casAnniversaryMode'])) {
625 // only consider reminders less than 11 months ago
546a1ecc 626 $joinClause .= " AND reminder.action_date_time > DATE_SUB(!casNow, INTERVAL 11 MONTH)";
c09bacfd
TO
627 }
628
629 return \CRM_Utils_SQL_Select::fragment()->join("reminder", "$joinType $joinClause");
630 }
631
632}