Merge pull request #11735 from mukeshcompucorp/CRM-21814-add-proper-container-to...
[civicrm-core.git] / CRM / Contribute / ActionMapping / ByType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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_ByType
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 type of contribution.
37 */
38 class CRM_Contribute_ActionMapping_ByType 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 = 'contribtype';
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 Type');
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('Financial Type');
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('financial_type_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 * 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.
143 *
144 * @return array
145 * array(string $value => string $label).
146 * Ex: array('assignee' => 'Activity Assignee').
147 */
148 public function getRecipientTypes() {
149 return array(
150 'soft_credit_type' => ts('Soft Credit Role'),
151 );
152 }
153
154 /**
155 * Get a list of recipients which match the given type.
156 *
157 * Note: A single schedule may filter on *multiple* recipients.
158 * When an admin chooses value(s), it's stored in $schedule->recipient_listing.
159 *
160 * @param string $recipientType
161 * Ex: 'participant_role'.
162 * @return array
163 * Array(mixed $name => string $label).
164 * Ex: array(1 => 'Attendee', 2 => 'Volunteer').
165 * @see getRecipientTypes
166 */
167 public function getRecipientListing($recipientType) {
168 switch ($recipientType) {
169 case 'soft_credit_type':
170 return \CRM_Core_OptionGroup::values('soft_credit_type', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name');
171
172 default:
173 return array();
174 }
175 }
176
177 /**
178 * Determine whether a schedule based on this mapping is sufficiently
179 * complete.
180 *
181 * @param \CRM_Core_DAO_ActionSchedule $schedule
182 * @return array
183 * Array (string $code => string $message).
184 * List of error messages.
185 */
186 public function validateSchedule($schedule) {
187 return array();
188 }
189
190 /**
191 * Generate a query to locate contacts who match the given
192 * schedule.
193 *
194 * @param \CRM_Core_DAO_ActionSchedule $schedule
195 * @param string $phase
196 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
197 * @param array $defaultParams
198 * Default parameters that should be included with query.
199 * @return \CRM_Utils_SQL_Select
200 * @see RecipientBuilder
201 * @throws CRM_Core_Exception
202 */
203 public function createQuery($schedule, $phase, $defaultParams) {
204 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
205 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
206
207 $query = \CRM_Utils_SQL_Select::from("civicrm_contribution e")->param($defaultParams);;
208 $query['casAddlCheckFrom'] = 'civicrm_contribution e';
209 $query['casContactIdField'] = 'e.contact_id';
210 $query['casEntityIdField'] = 'e.id';
211 $query['casContactTableAlias'] = NULL;
212
213 // $schedule->start_action_date is user-supplied data. validate.
214 if (!array_key_exists($schedule->start_action_date, $this->getDateFields())) {
215 throw new CRM_Core_Exception("Invalid date field");
216 }
217 $query['casDateField'] = $schedule->start_action_date;
218
219 // build where clause
220 if (!empty($selectedValues)) {
221 $query->where("e.financial_type_id IN (@selectedValues)")
222 ->param('selectedValues', $selectedValues);
223 }
224 if (!empty($selectedStatuses)) {
225 $query->where("e.contribution_status_id IN (#selectedStatuses)")
226 ->param('selectedStatuses', $selectedStatuses);
227 }
228
229 if ($schedule->recipient_listing && $schedule->limit_to) {
230 switch ($schedule->recipient) {
231 case 'soft_credit_type':
232 $query['casContactIdField'] = 'soft.contact_id';
233 $query->join('soft', 'INNER JOIN civicrm_contribution_soft soft ON soft.contribution_id = e.id')
234 ->where("soft.soft_credit_type_id IN (#recipList)")
235 ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing));
236 break;
237 }
238 }
239
240 return $query;
241 }
242
243 }