CRM-13422 - CRM_Contribute_ActionMapping_ByPage
[civicrm-core.git] / CRM / Contribute / ActionMapping / ByPage.php
CommitLineData
2045389a
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
28use 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 */
38class 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 /**
139 * FIXME: Unsure. Not sure how it differs from getRecipientTypes... but it does...
140 *
141 * @param string $recipientType
142 * @return array
143 * Array(mixed $name => string $label).
144 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
145 */
146 public function getRecipientListing($recipientType) {
147 return array();
148 }
149
150 /**
151 * FIXME: Unsure. Not sure how it differs from getRecipientListing... but it does...
152 *
153 * @param bool|NULL $noThanksJustKidding
154 * This is ridiculous and should not exist.
155 * If true, don't do our main job.
156 * @return array
157 * array(mixed $value => string $label).
158 * Ex: array('assignee' => 'Activity Assignee').
159 */
160 public function getRecipientTypes($noThanksJustKidding = FALSE) {
161 return array();
162 }
163
164 /**
165 * Generate a query to locate contacts who match the given
166 * schedule.
167 *
168 * @param \CRM_Core_DAO_ActionSchedule $schedule
169 * @param string $phase
170 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
171 * @param array $defaultParams
172 * Default parameters that should be included with query.
173 * @return \CRM_Utils_SQL_Select
174 * @see RecipientBuilder
175 * @throws CRM_Core_Exception
176 */
177 public function createQuery($schedule, $phase, $defaultParams) {
178 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
179 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
180
181 $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);;
182 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
183 $query['casContactIdField'] = 'e.contact_id';
184 $query['casEntityIdField'] = 'e.id';
185 $query['casContactTableAlias'] = NULL;
186
187 // $schedule->start_action_date is user-supplied data. validate.
188 if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
189 throw new CRM_Core_Exception("Invalid date field");
190 }
191 $query['casDateField'] = $schedule->start_action_date;
192
193 // build where clause
194 if (!empty($selectedValues)) {
195 $query->where("e.contribution_page_id IN (@selectedValues)")
196 ->param('selectedValues', $selectedValues);
197 }
198 if (!empty($selectedStatuses)) {
199 $query->where("e.contribution_status_id IN (#selectedStatuses)")
200 ->param('selectedStatuses', $selectedStatuses);
201 }
202
203 return $query;
204 }
205
206}