Commit | Line | Data |
---|---|---|
2045389a TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
2045389a | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
2045389a TO |
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 | ||
2045389a TO |
28 | |
29 | /** | |
30 | * Class CRM_Contribute_ActionMapping_ByPage | |
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 page on which the contribution was made. | |
36 | */ | |
37 | class CRM_Contribute_ActionMapping_ByPage 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 = 'contribpage'; | |
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 Page'); | |
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('Contribution Page'); | |
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() { | |
be2fb01f | 104 | return CRM_Contribute_BAO_Contribution::buildOptions('contribution_page_id', 'get', []); |
2045389a TO |
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) { | |
be2fb01f | 119 | return CRM_Contribute_BAO_Contribution::buildOptions('contribution_status_id', 'get', []); |
2045389a TO |
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() { | |
be2fb01f | 129 | return [ |
2045389a TO |
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'), | |
be2fb01f | 134 | ]; |
2045389a TO |
135 | } |
136 | ||
137 | /** | |
0effed37 TO |
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. | |
2045389a | 142 | * |
2045389a | 143 | * @return array |
0effed37 TO |
144 | * array(string $value => string $label). |
145 | * Ex: array('assignee' => 'Activity Assignee'). | |
2045389a | 146 | */ |
0effed37 | 147 | public function getRecipientTypes() { |
be2fb01f | 148 | return []; |
2045389a TO |
149 | } |
150 | ||
151 | /** | |
0effed37 | 152 | * Get a list of recipients which match the given type. |
2045389a | 153 | * |
0effed37 TO |
154 | * Note: A single schedule may filter on *multiple* recipients. |
155 | * When an admin chooses value(s), it's stored in $schedule->recipient_listing. | |
156 | * | |
157 | * @param string $recipientType | |
158 | * Ex: 'participant_role'. | |
2045389a | 159 | * @return array |
0effed37 TO |
160 | * Array(mixed $name => string $label). |
161 | * Ex: array(1 => 'Attendee', 2 => 'Volunteer'). | |
162 | * @see getRecipientTypes | |
2045389a | 163 | */ |
0effed37 | 164 | public function getRecipientListing($recipientType) { |
be2fb01f | 165 | return []; |
2045389a TO |
166 | } |
167 | ||
7f0141d8 TO |
168 | /** |
169 | * Determine whether a schedule based on this mapping is sufficiently | |
170 | * complete. | |
171 | * | |
172 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
173 | * @return array | |
174 | * Array (string $code => string $message). | |
175 | * List of error messages. | |
176 | */ | |
177 | public function validateSchedule($schedule) { | |
be2fb01f | 178 | return []; |
7f0141d8 TO |
179 | } |
180 | ||
2045389a TO |
181 | /** |
182 | * Generate a query to locate contacts who match the given | |
183 | * schedule. | |
184 | * | |
185 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
186 | * @param string $phase | |
187 | * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. | |
188 | * @param array $defaultParams | |
189 | * Default parameters that should be included with query. | |
190 | * @return \CRM_Utils_SQL_Select | |
191 | * @see RecipientBuilder | |
192 | * @throws CRM_Core_Exception | |
193 | */ | |
194 | public function createQuery($schedule, $phase, $defaultParams) { | |
195 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); | |
196 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
197 | ||
198 | $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);; | |
199 | $query['casAddlCheckFrom'] = 'civicrm_contribution e'; | |
200 | $query['casContactIdField'] = 'e.contact_id'; | |
201 | $query['casEntityIdField'] = 'e.id'; | |
202 | $query['casContactTableAlias'] = NULL; | |
203 | ||
204 | // $schedule->start_action_date is user-supplied data. validate. | |
205 | if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) { | |
206 | throw new CRM_Core_Exception("Invalid date field"); | |
207 | } | |
208 | $query['casDateField'] = $schedule->start_action_date; | |
209 | ||
210 | // build where clause | |
211 | if (!empty($selectedValues)) { | |
212 | $query->where("e.contribution_page_id IN (@selectedValues)") | |
213 | ->param('selectedValues', $selectedValues); | |
214 | } | |
215 | if (!empty($selectedStatuses)) { | |
216 | $query->where("e.contribution_status_id IN (#selectedStatuses)") | |
217 | ->param('selectedStatuses', $selectedStatuses); | |
218 | } | |
219 | ||
220 | return $query; | |
221 | } | |
222 | ||
223 | } |