From b5302d4e45bd49a11ecd4b2904ba8cdaffdb4f47 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 4 Aug 2015 22:23:17 -0700 Subject: [PATCH] CRM-13422 - CRM_Contribute_ActionMapping_ByType --- CRM/Contribute/ActionMapping/ByType.php | 206 ++++++++++++++++++++++++ Civi/Core/Container.php | 1 + 2 files changed, 207 insertions(+) create mode 100644 CRM/Contribute/ActionMapping/ByType.php diff --git a/CRM/Contribute/ActionMapping/ByType.php b/CRM/Contribute/ActionMapping/ByType.php new file mode 100644 index 0000000000..abb2c322e9 --- /dev/null +++ b/CRM/Contribute/ActionMapping/ByType.php @@ -0,0 +1,206 @@ +register(new static()); + } + + /** + * @return mixed + */ + public function getId() { + return self::MAPPING_ID; + } + + /** + * @return string + */ + public function getEntity() { + return 'civicrm_contribution'; + } + + /** + * Get a printable label for this mapping type. + * + * @return string + */ + public function getLabel() { + return ts('Contribution Type'); + } + + /** + * Get a printable label to use as the header on the 'value' filter. + * + * @return string + */ + public function getValueHeader() { + return ts('Financial Type'); + } + + /** + * Get a printable label to use as the header on the 'status' filter. + * + * @return string + */ + public function getStatusHeader() { + return ts('Contribution Status'); + } + + /** + * Get a list of value options. + * + * @return array + * Array(string $value => string $label). + * Ex: array(123 => 'Phone Call', 456 => 'Meeting'). + * @throws CRM_Core_Exception + */ + public function getValueLabels() { + return CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'get', array()); + } + + /** + * Get a list of status options. + * + * @param string|int $value + * The list of status options may be contingent upon the selected filter value. + * This is the selected filter value. + * @return array + * Array(string $value => string $label). + * Ex: Array(123 => 'Completed', 456 => 'Scheduled'). + * @throws CRM_Core_Exception + */ + public function getStatusLabels($value) { + return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', array()); + } + + /** + * Get a list of available date fields. + * + * @return array + * Array(string $fieldName => string $fieldLabel). + */ + public function getDateFields() { + return array( + 'receive_date' => ts('Receive Date'), + 'cancel_date' => ts('Cancel Date'), + 'receipt_date' => ts('Receipt Date'), + 'thankyou_date' => ts('Thank You Date'), + ); + } + + /** + * FIXME: Unsure. Not sure how it differs from getRecipientTypes... but it does... + * + * @param string $recipientType + * @return array + * Array(mixed $name => string $label). + * Ex: array(1 => 'Attendee', 2 => 'Volunteer'). + */ + public function getRecipientListing($recipientType) { + return array(); + } + + /** + * FIXME: Unsure. Not sure how it differs from getRecipientListing... but it does... + * + * @param bool|NULL $noThanksJustKidding + * This is ridiculous and should not exist. + * If true, don't do our main job. + * @return array + * array(mixed $value => string $label). + * Ex: array('assignee' => 'Activity Assignee'). + */ + public function getRecipientTypes($noThanksJustKidding = FALSE) { + return array(); + } + + /** + * Generate a query to locate contacts who match the given + * schedule. + * + * @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 + * @throws CRM_Core_Exception + */ + 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("civicrm_contribution e")->param($defaultParams);; + $query['casAddlCheckFrom'] = 'civicrm_contribution e'; + $query['casContactIdField'] = 'e.contact_id'; + $query['casEntityIdField'] = 'e.id'; + $query['casContactTableAlias'] = NULL; + + // $schedule->start_action_date is user-supplied data. validate. + if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) { + throw new CRM_Core_Exception("Invalid date field"); + } + $query['casDateField'] = $schedule->start_action_date; + + // build where clause + if (!empty($selectedValues)) { + $query->where("e.financial_type_id IN (@selectedValues)") + ->param('selectedValues', $selectedValues); + } + if (!empty($selectedStatuses)) { + $query->where("e.contribution_status_id IN (#selectedStatuses)") + ->param('selectedStatuses', $selectedStatuses); + } + + return $query; + } + +} diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index ea18bd198a..2057eef332 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -238,6 +238,7 @@ class Container { $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Activity_ActionMapping', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contact_ActionMapping', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByPage', 'onRegisterActionMappings')); + $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Contribute_ActionMapping_ByType', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Event_ActionMapping', 'onRegisterActionMappings')); $dispatcher->addListener(\Civi\ActionSchedule\Events::MAPPINGS, array('CRM_Member_ActionMapping', 'onRegisterActionMappings')); -- 2.25.1