Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality for batch profile update for events | |
38 | */ | |
39 | class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task { | |
40 | ||
41 | /** | |
42 | * the title of the group | |
43 | * | |
44 | * @var string | |
45 | */ | |
46 | protected $_title; | |
47 | ||
48 | /** | |
49 | * maximum profile fields that will be displayed | |
50 | * | |
51 | */ | |
52 | protected $_maxFields = 9; | |
53 | ||
54 | /** | |
55 | * variable to store redirect path | |
56 | * | |
57 | */ | |
58 | protected $_userContext; | |
59 | ||
60 | /** | |
61 | * variable to store previous status id. | |
62 | * | |
63 | */ | |
64 | protected $_fromStatusIds; | |
65 | ||
66 | /** | |
67 | * build all the data structures needed to build the form | |
68 | * | |
69 | * @return void | |
70 | * @access public | |
03e04002 | 71 | */ |
6a488035 TO |
72 | function preProcess() { |
73 | /* | |
74 | * initialize the task and row fields | |
75 | */ | |
76 | parent::preProcess(); | |
77 | ||
78 | //get the contact read only fields to display. | |
79 | $readOnlyFields = array_merge(array('sort_name' => ts('Name')), | |
80 | CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
81 | 'contact_autocomplete_options', | |
82 | TRUE, NULL, FALSE, 'name', TRUE | |
83 | ) | |
84 | ); | |
85 | //get the read only field data. | |
86 | $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1); | |
87 | $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails($this->_participantIds, | |
88 | 'CiviEvent', $returnProperties | |
89 | ); | |
90 | $this->assign('contactDetails', $contactDetails); | |
91 | $this->assign('readOnlyFields', $readOnlyFields); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Build the form | |
96 | * | |
97 | * @access public | |
98 | * | |
99 | * @return void | |
100 | */ | |
101 | function buildQuickForm() { | |
102 | $ufGroupId = $this->get('ufGroupId'); | |
103 | ||
104 | if (!$ufGroupId) { | |
105 | CRM_Core_Error::fatal('ufGroupId is missing'); | |
106 | } | |
107 | ||
108 | $this->_title = ts('Batch Update for Events') . ' - ' . CRM_Core_BAO_UFGroup::getTitle($ufGroupId); | |
109 | CRM_Utils_System::setTitle($this->_title); | |
110 | $this->addDefaultButtons(ts('Save')); | |
111 | $this->_fields = array(); | |
112 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($ufGroupId, FALSE, CRM_Core_Action::VIEW); | |
113 | ||
114 | // remove file type field and then limit fields | |
115 | $suppressFields = FALSE; | |
116 | $removehtmlTypes = array('File', 'Autocomplete-Select'); | |
117 | foreach ($this->_fields as $name => $field) { | |
118 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($name) && | |
119 | in_array($this->_fields[$name]['html_type'], $removehtmlTypes) | |
120 | ) { | |
121 | $suppressFields = TRUE; | |
122 | unset($this->_fields[$name]); | |
123 | } | |
124 | ||
125 | //fix to reduce size as we are using this field in grid | |
126 | if (is_array($field['attributes']) && $this->_fields[$name]['attributes']['size'] > 19) { | |
127 | //shrink class to "form-text-medium" | |
128 | $this->_fields[$name]['attributes']['size'] = 19; | |
129 | } | |
130 | } | |
131 | ||
132 | $this->_fields = array_slice($this->_fields, 0, $this->_maxFields); | |
133 | ||
134 | $this->addButtons(array( | |
135 | array( | |
136 | 'type' => 'submit', | |
137 | 'name' => ts('Update Participant(s)'), | |
138 | 'isDefault' => TRUE, | |
139 | ), | |
140 | array( | |
141 | 'type' => 'cancel', | |
142 | 'name' => ts('Cancel'), | |
143 | ), | |
144 | ) | |
145 | ); | |
146 | ||
147 | ||
148 | $this->assign('profileTitle', $this->_title); | |
149 | $this->assign('componentIds', $this->_participantIds); | |
150 | $fileFieldExists = FALSE; | |
151 | ||
152 | //load all campaigns. | |
153 | if (array_key_exists('participant_campaign_id', $this->_fields)) { | |
154 | $this->_componentCampaigns = array(); | |
155 | CRM_Core_PseudoConstant::populate($this->_componentCampaigns, | |
156 | 'CRM_Event_DAO_Participant', | |
157 | TRUE, 'campaign_id', 'id', | |
158 | ' id IN (' . implode(' , ', array_values($this->_participantIds)) . ' ) ' | |
159 | ); | |
160 | } | |
161 | ||
162 | //fix for CRM-2752 | |
163 | // get the option value for custom data type | |
164 | $this->_roleCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantRole', 'name'); | |
165 | $this->_eventNameCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventName', 'name'); | |
166 | ||
167 | // build custom data getFields array | |
168 | $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID); | |
169 | ||
170 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID); | |
171 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
172 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
173 | ); | |
174 | $this->_customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
175 | ||
176 | foreach ($this->_participantIds as $participantId) { | |
177 | $roleId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id'); | |
178 | $eventId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id'); | |
179 | foreach ($this->_fields as $name => $field) { | |
180 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { | |
181 | $customValue = CRM_Utils_Array::value($customFieldID, $this->_customFields); | |
182 | $entityColumnValue = array(); | |
a7488080 | 183 | if (!empty($customValue['extends_entity_column_value'])) { |
6a488035 TO |
184 | $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
185 | $customValue['extends_entity_column_value'] | |
186 | ); | |
187 | } | |
188 | $entityColumnValueRole = CRM_Utils_Array::value($roleId, $entityColumnValue); | |
189 | if (($this->_roleCustomDataTypeID == $customValue['extends_entity_column_id']) && | |
190 | ($entityColumnValueRole) | |
191 | ) { | |
192 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId); | |
193 | } | |
194 | elseif (($this->_eventNameCustomDataTypeID == $customValue['extends_entity_column_id']) && | |
195 | ($eventId == $entityColumnValueRole) | |
196 | ) { | |
197 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId); | |
198 | } | |
199 | elseif (CRM_Utils_System::isNull($entityColumnValueRole)) { | |
200 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId); | |
201 | } | |
202 | } | |
203 | else { | |
f2f98854 | 204 | if ($field['name'] == 'participant_role') { |
6a488035 TO |
205 | $field['is_multiple'] = TRUE; |
206 | } | |
207 | // handle non custom fields | |
208 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId); | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | $this->assign('fields', $this->_fields); | |
214 | ||
215 | // don't set the status message when form is submitted. | |
216 | $buttonName = $this->controller->getButtonName('submit'); | |
217 | ||
218 | if ($suppressFields && $buttonName != '_qf_Batch_next') { | |
219 | CRM_Core_Session::setStatus(ts("FILE or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update and have been excluded."), ts('Unsupported Field Type'), 'info'); | |
220 | } | |
221 | ||
222 | $this->addDefaultButtons(ts('Update Participant(s)')); | |
223 | } | |
224 | ||
225 | /** | |
226 | * This function sets the default values for the form. | |
227 | * | |
228 | * @access public | |
229 | * | |
355ba699 | 230 | * @return void |
6a488035 TO |
231 | */ |
232 | function setDefaultValues() { | |
233 | if (empty($this->_fields)) { | |
234 | return; | |
235 | } | |
236 | ||
237 | $defaults = array(); | |
238 | foreach ($this->_participantIds as $participantId) { | |
239 | $details[$participantId] = array(); | |
240 | ||
241 | $details[$participantId] = CRM_Event_BAO_Participant::participantDetails($participantId); | |
242 | CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $participantId, 'Event'); | |
243 | ||
244 | //get the from status ids, CRM-4323 | |
245 | if (array_key_exists('participant_status', $this->_fields)) { | |
246 | $this->_fromStatusIds[$participantId] = CRM_Utils_Array::value("field[$participantId][participant_status]", $defaults); | |
247 | } | |
248 | if (array_key_exists('participant_role', $this->_fields)) { | |
249 | if ($defaults["field[{$participantId}][participant_role]"]) { | |
250 | $roles = $defaults["field[{$participantId}][participant_role]"]; | |
251 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $roles) as $k => $v) { | |
252 | $defaults["field[$participantId][participant_role][{$v}]"] = 1; | |
253 | } | |
254 | unset($defaults["field[{$participantId}][participant_role]"]); | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | $this->assign('details', $details); | |
260 | return $defaults; | |
261 | } | |
262 | ||
263 | /** | |
264 | * process the form after the input has been submitted and validated | |
265 | * | |
266 | * @access public | |
267 | * | |
355ba699 | 268 | * @return void |
6a488035 TO |
269 | */ |
270 | public function postProcess() { | |
271 | $params = $this->exportValues(); | |
2179c63d | 272 | $statusClasses = CRM_Event_PseudoConstant::participantStatusClass(); |
6a488035 TO |
273 | if (isset($params['field'])) { |
274 | foreach ($params['field'] as $key => $value) { | |
275 | ||
276 | //check for custom data | |
277 | $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value, | |
278 | CRM_Core_DAO::$_nullObject, | |
279 | $key, | |
280 | 'Participant' | |
281 | ); | |
282 | ||
283 | $value['id'] = $key; | |
a7488080 | 284 | if (!empty($value['participant_register_date'])) { |
6a488035 TO |
285 | $value['register_date'] = CRM_Utils_Date::processDate($value['participant_register_date'], $value['participant_register_date_time']); |
286 | } | |
287 | ||
a7488080 | 288 | if (!empty($value['participant_role'])) { |
6a488035 TO |
289 | $participantRoles = CRM_Event_PseudoConstant::participantRole(); |
290 | if (is_array($value['participant_role'])) { | |
291 | $value['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value['participant_role'])); | |
292 | } | |
293 | else { | |
294 | $value['role_id'] = $value['participant_role']; | |
295 | } | |
296 | } | |
297 | ||
298 | //need to send mail when status change | |
299 | $statusChange = FALSE; | |
2179c63d | 300 | $relatedStatusChange = FALSE; |
a7488080 | 301 | if (!empty($value['participant_status'])) { |
6a488035 TO |
302 | $value['status_id'] = $value['participant_status']; |
303 | $fromStatusId = CRM_Utils_Array::value($key, $this->_fromStatusIds); | |
304 | if (!$fromStatusId) { | |
305 | $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id'); | |
306 | } | |
307 | ||
308 | if ($fromStatusId != $value['status_id']) { | |
2179c63d | 309 | $relatedStatusChange = TRUE; |
310 | } | |
311 | if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) { | |
6a488035 TO |
312 | $statusChange = TRUE; |
313 | } | |
314 | } | |
315 | ||
a7488080 | 316 | if (!empty($value['participant_source'])) { |
6a488035 TO |
317 | $value['source'] = $value['participant_source']; |
318 | } | |
319 | unset($value['participant_register_date']); | |
320 | unset($value['participant_status']); | |
321 | unset($value['participant_source']); | |
322 | ||
323 | CRM_Event_BAO_Participant::create($value); | |
324 | ||
325 | //need to trigger mails when we change status | |
326 | if ($statusChange) { | |
327 | CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId); | |
2179c63d | 328 | } |
329 | if ($relatedStatusChange) { | |
6a488035 TO |
330 | //update related contribution status, CRM-4395 |
331 | self::updatePendingOnlineContribution($key, $value['status_id']); | |
332 | } | |
333 | } | |
c7692a87 | 334 | CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success'); |
6a488035 TO |
335 | } |
336 | else { | |
c7692a87 | 337 | CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert'); |
6a488035 TO |
338 | } |
339 | } | |
340 | //end of function | |
341 | ||
342 | static function updatePendingOnlineContribution($participantId, $statusId) { | |
343 | if (!$participantId || !$statusId) { | |
344 | return; | |
345 | } | |
346 | ||
347 | $contributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($participantId, | |
348 | 'Event' | |
349 | ); | |
350 | if (!$contributionId) { | |
351 | return; | |
352 | } | |
353 | ||
354 | //status rules. | |
355 | //1. participant - positive => contribution - completed. | |
356 | //2. participant - negative => contribution - cancelled. | |
357 | ||
358 | $positiveStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Positive'"); | |
359 | $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'"); | |
360 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
361 | ||
362 | $contributionStatusId = NULL; | |
363 | if (array_key_exists($statusId, $positiveStatuses)) { | |
364 | $contributionStatusId = array_search('Completed', $contributionStatuses); | |
365 | } | |
366 | if (array_key_exists($statusId, $negativeStatuses)) { | |
367 | $contributionStatusId = array_search('Cancelled', $contributionStatuses); | |
368 | } | |
369 | ||
370 | if (!$contributionStatusId) { | |
371 | return; | |
372 | } | |
373 | ||
374 | $params = array( | |
375 | 'component_id' => $participantId, | |
376 | 'componentName' => 'Event', | |
377 | 'contribution_id' => $contributionId, | |
378 | 'contribution_status_id' => $contributionStatusId, | |
379 | 'skipComponentSync' => 1 | |
380 | ); | |
381 | ||
382 | //change related contribution status. | |
383 | $updatedStatusId = CRM_Core_Payment_BaseIPN::updateContributionStatus($params); | |
384 | ||
385 | return $updatedStatusId; | |
386 | } | |
387 | } | |
388 |