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