3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 use Civi\ActionSchedule\RecipientBuilder
;
31 * Class CRM_Contribute_ActionMapping_ByPage
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.
38 class CRM_Contribute_ActionMapping_ByPage
implements \Civi\ActionSchedule\MappingInterface
{
41 * The value for civicrm_action_schedule.mapping_id which identifies the
42 * "Contribution Page" mapping.
44 const MAPPING_ID
= 'contribpage';
47 * Register Activity-related action mappings.
49 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
51 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent
$registrations) {
52 $registrations->register(new static());
58 public function getId() {
59 return self
::MAPPING_ID
;
65 public function getEntity() {
66 return 'civicrm_contribution';
70 * Get a printable label for this mapping type.
74 public function getLabel() {
75 return ts('Contribution Page');
79 * Get a printable label to use as the header on the 'value' filter.
83 public function getValueHeader() {
84 return ts('Contribution Page');
88 * Get a printable label to use as the header on the 'status' filter.
92 public function getStatusHeader() {
93 return ts('Contribution Status');
97 * Get a list of value options.
100 * Array(string $value => string $label).
101 * Ex: array(123 => 'Phone Call', 456 => 'Meeting').
102 * @throws CRM_Core_Exception
104 public function getValueLabels() {
105 return CRM_Contribute_BAO_Contribution
::buildOptions('contribution_page_id', 'get', []);
109 * Get a list of status options.
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.
115 * Array(string $value => string $label).
116 * Ex: Array(123 => 'Completed', 456 => 'Scheduled').
117 * @throws CRM_Core_Exception
119 public function getStatusLabels($value) {
120 return CRM_Contribute_BAO_Contribution
::buildOptions('contribution_status_id', 'get', []);
124 * Get a list of available date fields.
127 * Array(string $fieldName => string $fieldLabel).
129 public function getDateFields() {
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'),
139 * Get a list of recipient types.
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.
145 * array(string $value => string $label).
146 * Ex: array('assignee' => 'Activity Assignee').
148 public function getRecipientTypes() {
153 * Get a list of recipients which match the given type.
155 * Note: A single schedule may filter on *multiple* recipients.
156 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
158 * @param string $recipientType
159 * Ex: 'participant_role'.
161 * Array(mixed $name => string $label).
162 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
163 * @see getRecipientTypes
165 public function getRecipientListing($recipientType) {
170 * Determine whether a schedule based on this mapping is sufficiently
173 * @param \CRM_Core_DAO_ActionSchedule $schedule
175 * Array (string $code => string $message).
176 * List of error messages.
178 public function validateSchedule($schedule) {
183 * Generate a query to locate contacts who match the given
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
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
);
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;
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");
209 $query['casDateField'] = $schedule->start_action_date
;
211 // build where clause
212 if (!empty($selectedValues)) {
213 $query->where("e.contribution_page_id IN (@selectedValues)")
214 ->param('selectedValues', $selectedValues);
216 if (!empty($selectedStatuses)) {
217 $query->where("e.contribution_status_id IN (#selectedStatuses)")
218 ->param('selectedStatuses', $selectedStatuses);