Merge pull request #15438 from demeritcowboy/emailprocessor-still-hungry-alt
[civicrm-core.git] / CRM / Contribute / ActionMapping / ByType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
28
29 /**
30 * Class CRM_Contribute_ActionMapping_ByType
31 *
32 * This defines the scheduled-reminder functionality for contribution
33 * entities. It is useful for sending a reminder based on:
34 * - The receipt-date, cancel-date, or thankyou-date.
35 * - The type of contribution.
36 */
37 class CRM_Contribute_ActionMapping_ByType implements \Civi\ActionSchedule\MappingInterface {
38
39 /**
40 * The value for civicrm_action_schedule.mapping_id which identifies the
41 * "Contribution Page" mapping.
42 */
43 const MAPPING_ID = 'contribtype';
44
45 /**
46 * Register Activity-related action mappings.
47 *
48 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
49 */
50 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
51 $registrations->register(new static());
52 }
53
54 /**
55 * @return mixed
56 */
57 public function getId() {
58 return self::MAPPING_ID;
59 }
60
61 /**
62 * @return string
63 */
64 public function getEntity() {
65 return 'civicrm_contribution';
66 }
67
68 /**
69 * Get a printable label for this mapping type.
70 *
71 * @return string
72 */
73 public function getLabel() {
74 return ts('Contribution Type');
75 }
76
77 /**
78 * Get a printable label to use as the header on the 'value' filter.
79 *
80 * @return string
81 */
82 public function getValueHeader() {
83 return ts('Financial Type');
84 }
85
86 /**
87 * Get a printable label to use as the header on the 'status' filter.
88 *
89 * @return string
90 */
91 public function getStatusHeader() {
92 return ts('Contribution Status');
93 }
94
95 /**
96 * Get a list of value options.
97 *
98 * @return array
99 * Array(string $value => string $label).
100 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
101 * @throws CRM_Core_Exception
102 */
103 public function getValueLabels() {
104 return CRM_Contribute_BAO_Contribution::buildOptions('financial_type_id', 'get', []);
105 }
106
107 /**
108 * Get a list of status options.
109 *
110 * @param string|int $value
111 * The list of status options may be contingent upon the selected filter value.
112 * This is the selected filter value.
113 * @return array
114 * Array(string $value => string $label).
115 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
116 * @throws CRM_Core_Exception
117 */
118 public function getStatusLabels($value) {
119 return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', []);
120 }
121
122 /**
123 * Get a list of available date fields.
124 *
125 * @return array
126 * Array(string $fieldName => string $fieldLabel).
127 */
128 public function getDateFields() {
129 return [
130 'receive_date' => ts('Receive Date'),
131 'cancel_date' => ts('Cancel Date'),
132 'receipt_date' => ts('Receipt Date'),
133 'thankyou_date' => ts('Thank You Date'),
134 ];
135 }
136
137 /**
138 * Get a list of recipient types.
139 *
140 * Note: A single schedule may filter on *zero* or *one* recipient types.
141 * When an admin chooses a value, it's stored in $schedule->recipient.
142 *
143 * @return array
144 * array(string $value => string $label).
145 * Ex: array('assignee' => 'Activity Assignee').
146 */
147 public function getRecipientTypes() {
148 return [
149 'soft_credit_type' => ts('Soft Credit Role'),
150 ];
151 }
152
153 /**
154 * Get a list of recipients which match the given type.
155 *
156 * Note: A single schedule may filter on *multiple* recipients.
157 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
158 *
159 * @param string $recipientType
160 * Ex: 'participant_role'.
161 * @return array
162 * Array(mixed $name => string $label).
163 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
164 * @see getRecipientTypes
165 */
166 public function getRecipientListing($recipientType) {
167 switch ($recipientType) {
168 case 'soft_credit_type':
169 return \CRM_Core_OptionGroup::values('soft_credit_type', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
170
171 default:
172 return [];
173 }
174 }
175
176 /**
177 * Determine whether a schedule based on this mapping is sufficiently
178 * complete.
179 *
180 * @param \CRM_Core_DAO_ActionSchedule $schedule
181 * @return array
182 * Array (string $code => string $message).
183 * List of error messages.
184 */
185 public function validateSchedule($schedule) {
186 return [];
187 }
188
189 /**
190 * Generate a query to locate contacts who match the given
191 * schedule.
192 *
193 * @param \CRM_Core_DAO_ActionSchedule $schedule
194 * @param string $phase
195 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
196 * @param array $defaultParams
197 * Default parameters that should be included with query.
198 * @return \CRM_Utils_SQL_Select
199 * @see RecipientBuilder
200 * @throws CRM_Core_Exception
201 */
202 public function createQuery($schedule, $phase, $defaultParams) {
203 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
204 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
205
206 $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);;
207 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
208 $query['casContactIdField'] = 'e.contact_id';
209 $query['casEntityIdField'] = 'e.id';
210 $query['casContactTableAlias'] = NULL;
211
212 // $schedule->start_action_date is user-supplied data. validate.
213 if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
214 throw new CRM_Core_Exception("Invalid date field");
215 }
216 $query['casDateField'] = $schedule->start_action_date;
217
218 // build where clause
219 if (!empty($selectedValues)) {
220 $query->where("e.financial_type_id IN (@selectedValues)")
221 ->param('selectedValues', $selectedValues);
222 }
223 if (!empty($selectedStatuses)) {
224 $query->where("e.contribution_status_id IN (#selectedStatuses)")
225 ->param('selectedStatuses', $selectedStatuses);
226 }
227
228 if ($schedule->recipient_listing && $schedule->limit_to) {
229 switch ($schedule->recipient) {
230 case 'soft_credit_type':
231 $query['casContactIdField'] = 'soft.contact_id';
232 $query->join('soft', 'INNER JOIN civicrm_contribution_soft soft ON soft.contribution_id = e.id')
233 ->where("soft.soft_credit_type_id IN (#recipList)")
234 ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing));
235 break;
236 }
237 }
238
239 return $query;
240 }
241
242 /**
243 * Determine whether a schedule based on this mapping should
244 * reset the reminder state if the trigger date changes.
245 *
246 * @return bool
247 *
248 * @param \CRM_Core_DAO_ActionSchedule $schedule
249 */
250 public function resetOnTriggerDateChange($schedule) {
251 return FALSE;
252 }
253
254 }