From efc404541c06e47b7e11dd66b93322e13d336bb1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 3 Aug 2015 21:50:52 -0700 Subject: [PATCH] CRM-13244 - RecipientBuilder::prepareStartDateClauses - Less stringiness --- CRM/Activity/ActionMapping.php | 4 +-- CRM/Contact/ActionMapping.php | 8 +++--- CRM/Event/ActionMapping.php | 4 +-- CRM/Member/ActionMapping.php | 4 +-- Civi/ActionSchedule/Mapping.php | 3 ++- Civi/ActionSchedule/MappingInterface.php | 4 ++- Civi/ActionSchedule/RecipientBuilder.php | 32 ++++++++++++------------ 7 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CRM/Activity/ActionMapping.php b/CRM/Activity/ActionMapping.php index 6026061f7d..f379b35df0 100644 --- a/CRM/Activity/ActionMapping.php +++ b/CRM/Activity/ActionMapping.php @@ -41,11 +41,11 @@ class CRM_Activity_ActionMapping extends \Civi\ActionSchedule\Mapping { * @see RecipientBuilder * @throws \CRM_Core_Exception */ - public function createQuery($schedule, $phase) { + public function createQuery($schedule, $phase, $defaultParams) { $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); - $query = \CRM_Utils_SQL_Select::from("{$this->entity} e"); + $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams); $query['casAddlCheckFrom'] = 'civicrm_activity e'; $query['casContactIdField'] = 'r.contact_id'; $query['casEntityIdField'] = 'e.id'; diff --git a/CRM/Contact/ActionMapping.php b/CRM/Contact/ActionMapping.php index b448ef2e44..39ed66f9f8 100644 --- a/CRM/Contact/ActionMapping.php +++ b/CRM/Contact/ActionMapping.php @@ -55,7 +55,7 @@ class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping { * @see RecipientBuilder * @throws \CRM_Core_Exception */ - public function createQuery($schedule, $phase) { + public function createQuery($schedule, $phase, $defaultParams) { $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); @@ -66,7 +66,7 @@ class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping { } elseif (in_array($selectedValues[0], $this->contactDateFields)) { $dateDBField = $selectedValues[0]; - $query = \CRM_Utils_SQL_Select::from("{$this->entity} e"); + $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams); $query->param(array( 'casAddlCheckFrom' => 'civicrm_contact e', 'casContactIdField' => 'e.id', @@ -83,7 +83,7 @@ class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping { $dateDBField = $customField['column_name']; $customGroupParams = array('id' => $customField['custom_group_id'], $customGroup); \CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroup); - $query = \CRM_Utils_SQL_Select::from("{$customGroup['table_name']} e"); + $query = \CRM_Utils_SQL_Select::from("{$customGroup['table_name']} e")->param($defaultParams); $query->param(array( 'casAddlCheckFrom' => "{$customGroup['table_name']} e", 'casContactIdField' => 'e.entity_id', @@ -97,7 +97,7 @@ class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping { if (in_array(2, $selectedStatuses)) { $query['casAnniversaryMode'] = 1; - $query['casDateField'] = 'DATE_ADD(' . $query['casDateField'] . ', INTERVAL ROUND(DATEDIFF(DATE(!casNow), ' . $query['casDateField'] . ') / 365) YEAR)'; + $query['casDateField'] = 'DATE_ADD(' . $query['casDateField'] . ', INTERVAL ROUND(DATEDIFF(DATE(' . $query['casNow'] . '), ' . $query['casDateField'] . ') / 365) YEAR)'; } return $query; diff --git a/CRM/Event/ActionMapping.php b/CRM/Event/ActionMapping.php index ecb24a2e1e..fbae301c90 100644 --- a/CRM/Event/ActionMapping.php +++ b/CRM/Event/ActionMapping.php @@ -48,11 +48,11 @@ class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping { * @return \CRM_Utils_SQL_Select * @see RecipientBuilder */ - public function createQuery($schedule, $phase) { + public function createQuery($schedule, $phase, $defaultParams) { $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); - $query = \CRM_Utils_SQL_Select::from("{$this->entity} e"); + $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);; $query['casAddlCheckFrom'] = 'civicrm_event r'; $query['casContactIdField'] = 'e.contact_id'; $query['casEntityIdField'] = 'e.id'; diff --git a/CRM/Member/ActionMapping.php b/CRM/Member/ActionMapping.php index 03654cc4cc..a6161ed4a5 100644 --- a/CRM/Member/ActionMapping.php +++ b/CRM/Member/ActionMapping.php @@ -47,11 +47,11 @@ class CRM_Member_ActionMapping extends \Civi\ActionSchedule\Mapping { * @return \CRM_Utils_SQL_Select * @see RecipientBuilder */ - public function createQuery($schedule, $phase) { + public function createQuery($schedule, $phase, $defaultParams) { $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); - $query = \CRM_Utils_SQL_Select::from("{$this->entity} e"); + $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);; $query['casAddlCheckFrom'] = 'civicrm_membership e'; $query['casContactIdField'] = 'e.contact_id'; $query['casEntityIdField'] = 'e.id'; diff --git a/Civi/ActionSchedule/Mapping.php b/Civi/ActionSchedule/Mapping.php index 4674b8fa14..a22908de7a 100644 --- a/Civi/ActionSchedule/Mapping.php +++ b/Civi/ActionSchedule/Mapping.php @@ -345,8 +345,9 @@ abstract class Mapping implements MappingInterface { * @param \CRM_Core_DAO_ActionSchedule $schedule * @param string $phase * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. + * @param array $defaultParams * @return \CRM_Utils_SQL_Select */ - public abstract function createQuery($schedule, $phase); + public abstract function createQuery($schedule, $phase, $defaultParams); } diff --git a/Civi/ActionSchedule/MappingInterface.php b/Civi/ActionSchedule/MappingInterface.php index efcdd53942..b193e709d8 100644 --- a/Civi/ActionSchedule/MappingInterface.php +++ b/Civi/ActionSchedule/MappingInterface.php @@ -118,9 +118,11 @@ interface MappingInterface { * @param \CRM_Core_DAO_ActionSchedule $schedule * @param string $phase * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. + * @param array $defaultParams + * Default parameters that should be included with query. * @return \CRM_Utils_SQL_Select * @see RecipientBuilder */ - public function createQuery($schedule, $phase); + public function createQuery($schedule, $phase, $defaultParams); } diff --git a/Civi/ActionSchedule/RecipientBuilder.php b/Civi/ActionSchedule/RecipientBuilder.php index 08efd822b3..fc221316b9 100644 --- a/Civi/ActionSchedule/RecipientBuilder.php +++ b/Civi/ActionSchedule/RecipientBuilder.php @@ -60,6 +60,7 @@ namespace Civi\ActionSchedule; * ->where('event.event_type_id IN (#myEventTypes)') * ->param('myEventTypes', array(2, 5)) * ->param('casDateField', 'e.register_date') + * ->param($defaultParams) * ...etc... * @endcode * @@ -175,7 +176,7 @@ class RecipientBuilder { protected function buildRelFirstPass() { $query = $this->prepareQuery(self::PHASE_RELATION_FIRST); - $startDateClauses = $this->prepareStartDateClauses($query['casDateField']); + $startDateClauses = $this->prepareStartDateClauses(); $firstQuery = $query->copy() ->merge($this->selectIntoActionLog(self::PHASE_RELATION_FIRST, $query)) @@ -214,6 +215,7 @@ class RecipientBuilder { $query = $this->prepareQuery(self::PHASE_ADDITION_FIRST); $insertAdditionalSql = \CRM_Utils_SQL_Select::from("civicrm_contact c") + ->merge($query, array('params')) ->merge($this->selectIntoActionLog(self::PHASE_ADDITION_FIRST, $query)) ->merge($this->joinReminder('LEFT JOIN', 'addl', $query)) ->where("c.is_deleted = 0 AND c.is_deceased = 0") @@ -239,7 +241,7 @@ class RecipientBuilder { */ protected function buildRelRepeatPass() { $query = $this->prepareQuery(self::PHASE_RELATION_REPEAT); - $startDateClauses = $this->prepareStartDateClauses($query['casDateField']); + $startDateClauses = $this->prepareStartDateClauses(); // CRM-15376 - do not send our reminders if original criteria no longer applies // the first part of the startDateClause array is the earliest the reminder can be sent. If the @@ -327,14 +329,15 @@ class RecipientBuilder { * @throws \CRM_Core_Exception */ protected function prepareQuery($phase) { - /** @var \CRM_Utils_SQL_Select $query */ - $query = $this->mapping->createQuery($this->actionSchedule, $phase); - $query->param(array( + $defaultParams = array( 'casActionScheduleId' => $this->actionSchedule->id, 'casMappingId' => $this->mapping->getId(), 'casMappingEntity' => $this->mapping->getEntity(), 'casNow' => $this->now, - )); + ); + + /** @var \CRM_Utils_SQL_Select $query */ + $query = $this->mapping->createQuery($this->actionSchedule, $phase, $defaultParams); if ($this->actionSchedule->limit_to /*1*/) { $query->merge($this->prepareContactFilter($query['casContactIdField'])); @@ -430,29 +433,26 @@ class RecipientBuilder { } /** - * @param $dateField * @return array */ - protected function prepareStartDateClauses($dateField) { + protected function prepareStartDateClauses() { $actionSchedule = $this->actionSchedule; - $mapping = $this->mapping; - $now = $this->now; $startDateClauses = array(); if ($actionSchedule->start_action_date) { $op = ($actionSchedule->start_action_condition == 'before' ? '<=' : '>='); $operator = ($actionSchedule->start_action_condition == 'before' ? 'DATE_SUB' : 'DATE_ADD'); - $date = $operator . "({$dateField}, INTERVAL {$actionSchedule->start_action_offset} {$actionSchedule->start_action_unit})"; - $startDateClauses[] = "'{$now}' >= {$date}"; + $date = $operator . "(!casDateField, INTERVAL {$actionSchedule->start_action_offset} {$actionSchedule->start_action_unit})"; + $startDateClauses[] = "'!casNow' >= {$date}"; // This is weird. Waddupwidat? - if ($mapping->getEntity() == 'civicrm_participant') { - $startDateClauses[] = $operator . "({$now}, INTERVAL 1 DAY ) {$op} " . $dateField; + if ($this->mapping->getEntity() == 'civicrm_participant') { + $startDateClauses[] = $operator . "(!casNow, INTERVAL 1 DAY ) {$op} " . '!casDateField'; } else { - $startDateClauses[] = "DATE_SUB({$now}, INTERVAL 1 DAY ) <= {$date}"; + $startDateClauses[] = "DATE_SUB(!casNow, INTERVAL 1 DAY ) <= {$date}"; } } elseif ($actionSchedule->absolute_date) { - $startDateClauses[] = "DATEDIFF(DATE('{$now}'),'{$actionSchedule->absolute_date}') = 0"; + $startDateClauses[] = "DATEDIFF(DATE('!casNow'),'{$actionSchedule->absolute_date}') = 0"; } return $startDateClauses; } -- 2.25.1