3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
14 * Class CRM_Contribute_ActionMapping_ByType
16 * This defines the scheduled-reminder functionality for contribution
17 * entities. It is useful for sending a reminder based on:
18 * - The receipt-date, cancel-date, or thankyou-date.
19 * - The type of contribution.
21 class CRM_Contribute_ActionMapping_ByType
implements \Civi\ActionSchedule\MappingInterface
{
24 * The value for civicrm_action_schedule.mapping_id which identifies the
25 * "Contribution Page" mapping.
27 const MAPPING_ID
= 'contribtype';
30 * Register Activity-related action mappings.
32 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
34 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent
$registrations) {
35 $registrations->register(new static());
41 public function getId() {
42 return self
::MAPPING_ID
;
48 public function getEntity() {
49 return 'civicrm_contribution';
53 * Get a printable label for this mapping type.
57 public function getLabel() {
58 return ts('Contribution Type');
62 * Get a printable label to use as the header on the 'value' filter.
66 public function getValueHeader() {
67 return ts('Financial Type');
71 * Get a printable label to use as the header on the 'status' filter.
75 public function getStatusHeader() {
76 return ts('Contribution Status');
80 * Get a list of value options.
83 * Array(string $value => string $label).
84 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
85 * @throws CRM_Core_Exception
87 public function getValueLabels() {
88 return CRM_Contribute_BAO_Contribution
::buildOptions('financial_type_id', 'get', []);
92 * Get a list of status options.
94 * @param string|int $value
95 * The list of status options may be contingent upon the selected filter value.
96 * This is the selected filter value.
98 * Array(string $value => string $label).
99 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
100 * @throws CRM_Core_Exception
102 public function getStatusLabels($value) {
103 return CRM_Contribute_BAO_Contribution
::buildOptions('contribution_status_id', 'get', []);
107 * Get a list of available date fields.
110 * Array(string $fieldName => string $fieldLabel).
112 public function getDateFields() {
114 'receive_date' => ts('Receive Date'),
115 'cancel_date' => ts('Cancel Date'),
116 'receipt_date' => ts('Receipt Date'),
117 'thankyou_date' => ts('Thank You Date'),
122 * Get a list of recipient types.
124 * Note: A single schedule may filter on *zero* or *one* recipient types.
125 * When an admin chooses a value, it's stored in $schedule->recipient.
128 * array(string $value => string $label).
129 * Ex: array('assignee' => 'Activity Assignee').
131 public function getRecipientTypes() {
133 'soft_credit_type' => ts('Soft Credit Role'),
138 * Get a list of recipients which match the given type.
140 * Note: A single schedule may filter on *multiple* recipients.
141 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
143 * @param string $recipientType
144 * Ex: 'participant_role'.
146 * Array(mixed $name => string $label).
147 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
148 * @see getRecipientTypes
150 public function getRecipientListing($recipientType) {
151 switch ($recipientType) {
152 case 'soft_credit_type':
153 return \CRM_Core_OptionGroup
::values('soft_credit_type', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
161 * Determine whether a schedule based on this mapping is sufficiently
164 * @param \CRM_Core_DAO_ActionSchedule $schedule
166 * Array (string $code => string $message).
167 * List of error messages.
169 public function validateSchedule($schedule) {
174 * Generate a query to locate contacts who match the given
177 * @param \CRM_Core_DAO_ActionSchedule $schedule
178 * @param string $phase
179 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
180 * @param array $defaultParams
181 * Default parameters that should be included with query.
182 * @return \CRM_Utils_SQL_Select
183 * @see RecipientBuilder
184 * @throws CRM_Core_Exception
186 public function createQuery($schedule, $phase, $defaultParams) {
187 $selectedValues = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_value
);
188 $selectedStatuses = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_status
);
190 $query = \CRM_Utils_SQL_Select
::from("civicrm_contribution e")->param($defaultParams);
191 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
192 $query['casContactIdField'] = 'e.contact_id';
193 $query['casEntityIdField'] = 'e.id';
194 $query['casContactTableAlias'] = NULL;
196 // $schedule->start_action_date is user-supplied data. validate.
197 if (!array_key_exists($schedule->start_action_date
, $this->getDateFields())) {
198 throw new CRM_Core_Exception("Invalid date field");
200 $query['casDateField'] = $schedule->start_action_date
;
202 // build where clause
203 if (!empty($selectedValues)) {
204 $query->where("e.financial_type_id IN (@selectedValues)")
205 ->param('selectedValues', $selectedValues);
207 if (!empty($selectedStatuses)) {
208 $query->where("e.contribution_status_id IN (#selectedStatuses)")
209 ->param('selectedStatuses', $selectedStatuses);
212 if ($schedule->recipient_listing
&& $schedule->limit_to
) {
213 switch ($schedule->recipient
) {
214 case 'soft_credit_type':
215 $query['casContactIdField'] = 'soft.contact_id';
216 $query->join('soft', 'INNER JOIN civicrm_contribution_soft soft ON soft.contribution_id = e.id')
217 ->where("soft.soft_credit_type_id IN (#recipList)")
218 ->param('recipList', \CRM_Utils_Array
::explodePadded($schedule->recipient_listing
));
227 * Determine whether a schedule based on this mapping should
228 * reset the reminder state if the trigger date changes.
232 * @param \CRM_Core_DAO_ActionSchedule $schedule
234 public function resetOnTriggerDateChange($schedule) {
239 * Determine whether a schedule based on this mapping should
240 * send to additional contacts.
242 public function sendToAdditional($entityId): bool {