CRM-15139 QA fix
[civicrm-core.git] / CRM / Event / Form / Task / Batch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
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/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class provides the functionality for batch profile update for events
38 */
39class 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');
d623de42 166 $this->_eventTypeCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventType', 'name');
6a488035
TO
167
168 // build custom data getFields array
169 $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID);
170
171 $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID);
d623de42
M
172 $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID);
173
6a488035
TO
174 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole,
175 CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
176 );
d623de42 177 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
6a488035
TO
178 $this->_customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
179
180 foreach ($this->_participantIds as $participantId) {
181 $roleId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id');
182 $eventId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id');
d623de42 183 $eventTypeId = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
6a488035
TO
184 foreach ($this->_fields as $name => $field) {
185 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
186 $customValue = CRM_Utils_Array::value($customFieldID, $this->_customFields);
187 $entityColumnValue = array();
a7488080 188 if (!empty($customValue['extends_entity_column_value'])) {
6a488035
TO
189 $entityColumnValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
190 $customValue['extends_entity_column_value']
191 );
192 }
193 $entityColumnValueRole = CRM_Utils_Array::value($roleId, $entityColumnValue);
9f8dff81 194 $entityColumnValueEventType = in_array($eventTypeId, $entityColumnValue) ? $eventTypeId : NULL;
6a488035
TO
195 if (($this->_roleCustomDataTypeID == $customValue['extends_entity_column_id']) &&
196 ($entityColumnValueRole)
197 ) {
198 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
199 }
200 elseif (($this->_eventNameCustomDataTypeID == $customValue['extends_entity_column_id']) &&
201 ($eventId == $entityColumnValueRole)
202 ) {
203 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
204 }
d623de42
M
205 elseif ($this->_eventTypeCustomDataTypeID == $customValue['extends_entity_column_id'] &&
206 ($entityColumnValueEventType == $eventTypeId)
207 ) {
208 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
209 }
6a488035
TO
210 elseif (CRM_Utils_System::isNull($entityColumnValueRole)) {
211 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
212 }
213 }
214 else {
f2f98854 215 if ($field['name'] == 'participant_role') {
6a488035
TO
216 $field['is_multiple'] = TRUE;
217 }
218 // handle non custom fields
219 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
220 }
221 }
222 }
223
224 $this->assign('fields', $this->_fields);
225
226 // don't set the status message when form is submitted.
227 $buttonName = $this->controller->getButtonName('submit');
228
229 if ($suppressFields && $buttonName != '_qf_Batch_next') {
230 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');
231 }
232
233 $this->addDefaultButtons(ts('Update Participant(s)'));
234 }
235
236 /**
237 * This function sets the default values for the form.
238 *
239 * @access public
240 *
355ba699 241 * @return void
6a488035
TO
242 */
243 function setDefaultValues() {
244 if (empty($this->_fields)) {
245 return;
246 }
247
248 $defaults = array();
249 foreach ($this->_participantIds as $participantId) {
250 $details[$participantId] = array();
251
252 $details[$participantId] = CRM_Event_BAO_Participant::participantDetails($participantId);
253 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $participantId, 'Event');
254
255 //get the from status ids, CRM-4323
256 if (array_key_exists('participant_status', $this->_fields)) {
257 $this->_fromStatusIds[$participantId] = CRM_Utils_Array::value("field[$participantId][participant_status]", $defaults);
258 }
259 if (array_key_exists('participant_role', $this->_fields)) {
260 if ($defaults["field[{$participantId}][participant_role]"]) {
261 $roles = $defaults["field[{$participantId}][participant_role]"];
262 foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $roles) as $k => $v) {
263 $defaults["field[$participantId][participant_role][{$v}]"] = 1;
264 }
265 unset($defaults["field[{$participantId}][participant_role]"]);
266 }
267 }
268 }
269
270 $this->assign('details', $details);
271 return $defaults;
272 }
273
274 /**
275 * process the form after the input has been submitted and validated
276 *
277 * @access public
278 *
355ba699 279 * @return void
6a488035
TO
280 */
281 public function postProcess() {
282 $params = $this->exportValues();
2179c63d 283 $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
6a488035
TO
284 if (isset($params['field'])) {
285 foreach ($params['field'] as $key => $value) {
286
287 //check for custom data
288 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
289 CRM_Core_DAO::$_nullObject,
290 $key,
291 'Participant'
292 );
293
294 $value['id'] = $key;
a7488080 295 if (!empty($value['participant_register_date'])) {
6a488035
TO
296 $value['register_date'] = CRM_Utils_Date::processDate($value['participant_register_date'], $value['participant_register_date_time']);
297 }
298
a7488080 299 if (!empty($value['participant_role'])) {
6a488035
TO
300 $participantRoles = CRM_Event_PseudoConstant::participantRole();
301 if (is_array($value['participant_role'])) {
302 $value['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value['participant_role']));
303 }
304 else {
305 $value['role_id'] = $value['participant_role'];
306 }
307 }
308
309 //need to send mail when status change
310 $statusChange = FALSE;
2179c63d 311 $relatedStatusChange = FALSE;
a7488080 312 if (!empty($value['participant_status'])) {
6a488035
TO
313 $value['status_id'] = $value['participant_status'];
314 $fromStatusId = CRM_Utils_Array::value($key, $this->_fromStatusIds);
315 if (!$fromStatusId) {
316 $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
317 }
318
319 if ($fromStatusId != $value['status_id']) {
2179c63d 320 $relatedStatusChange = TRUE;
321 }
322 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
6a488035
TO
323 $statusChange = TRUE;
324 }
325 }
326
a7488080 327 if (!empty($value['participant_source'])) {
6a488035
TO
328 $value['source'] = $value['participant_source'];
329 }
330 unset($value['participant_register_date']);
331 unset($value['participant_status']);
332 unset($value['participant_source']);
333
334 CRM_Event_BAO_Participant::create($value);
335
336 //need to trigger mails when we change status
337 if ($statusChange) {
338 CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
2179c63d 339 }
340 if ($relatedStatusChange) {
6a488035
TO
341 //update related contribution status, CRM-4395
342 self::updatePendingOnlineContribution($key, $value['status_id']);
343 }
344 }
c7692a87 345 CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
6a488035
TO
346 }
347 else {
c7692a87 348 CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
6a488035
TO
349 }
350 }
351 //end of function
352
0cf587a7
EM
353 /**
354 * @param $participantId
355 * @param $statusId
356 *
357 * @return Ambigous|void
358 */
6a488035
TO
359 static function updatePendingOnlineContribution($participantId, $statusId) {
360 if (!$participantId || !$statusId) {
361 return;
362 }
363
364 $contributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($participantId,
365 'Event'
366 );
367 if (!$contributionId) {
368 return;
369 }
370
371 //status rules.
372 //1. participant - positive => contribution - completed.
373 //2. participant - negative => contribution - cancelled.
374
375 $positiveStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Positive'");
376 $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
377 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
378
379 $contributionStatusId = NULL;
380 if (array_key_exists($statusId, $positiveStatuses)) {
381 $contributionStatusId = array_search('Completed', $contributionStatuses);
382 }
383 if (array_key_exists($statusId, $negativeStatuses)) {
384 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
385 }
386
387 if (!$contributionStatusId) {
388 return;
389 }
390
391 $params = array(
392 'component_id' => $participantId,
393 'componentName' => 'Event',
394 'contribution_id' => $contributionId,
395 'contribution_status_id' => $contributionStatusId,
396 'skipComponentSync' => 1
397 );
398
399 //change related contribution status.
400 $updatedStatusId = CRM_Core_Payment_BaseIPN::updateContributionStatus($params);
401
402 return $updatedStatusId;
403 }
404}
405