Merge pull request #1285 from kurund/CRM-12814
[civicrm-core.git] / CRM / Event / Form / Task / Batch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
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');
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();
183 if (CRM_Utils_Array::value('extends_entity_column_value', $customValue)) {
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 *
230 * @return None
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 *
268 * @return None
269 */
270 public function postProcess() {
271 $params = $this->exportValues();
272 if (isset($params['field'])) {
273 foreach ($params['field'] as $key => $value) {
274
275 //check for custom data
276 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
277 CRM_Core_DAO::$_nullObject,
278 $key,
279 'Participant'
280 );
281
282 $value['id'] = $key;
283 if (CRM_Utils_Array::value('participant_register_date', $value)) {
284 $value['register_date'] = CRM_Utils_Date::processDate($value['participant_register_date'], $value['participant_register_date_time']);
285 }
286
287 if (CRM_Utils_Array::value('participant_role', $value)) {
288 $participantRoles = CRM_Event_PseudoConstant::participantRole();
289 if (is_array($value['participant_role'])) {
290 $value['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($value['participant_role']));
291 }
292 else {
293 $value['role_id'] = $value['participant_role'];
294 }
295 }
296
297 //need to send mail when status change
298 $statusChange = FALSE;
299 if (CRM_Utils_Array::value('participant_status', $value)) {
300 $value['status_id'] = $value['participant_status'];
301 $fromStatusId = CRM_Utils_Array::value($key, $this->_fromStatusIds);
302 if (!$fromStatusId) {
303 $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
304 }
305
306 if ($fromStatusId != $value['status_id']) {
307 $statusChange = TRUE;
308 }
309 }
310
311 if (CRM_Utils_Array::value('participant_source', $value)) {
312 $value['source'] = $value['participant_source'];
313 }
314 unset($value['participant_register_date']);
315 unset($value['participant_status']);
316 unset($value['participant_source']);
317
318 CRM_Event_BAO_Participant::create($value);
319
320 //need to trigger mails when we change status
321 if ($statusChange) {
322 CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
323
324 //update related contribution status, CRM-4395
325 self::updatePendingOnlineContribution($key, $value['status_id']);
326 }
327 }
c7692a87 328 CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
6a488035
TO
329 }
330 else {
c7692a87 331 CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
6a488035
TO
332 }
333 }
334 //end of function
335
336 static function updatePendingOnlineContribution($participantId, $statusId) {
337 if (!$participantId || !$statusId) {
338 return;
339 }
340
341 $contributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($participantId,
342 'Event'
343 );
344 if (!$contributionId) {
345 return;
346 }
347
348 //status rules.
349 //1. participant - positive => contribution - completed.
350 //2. participant - negative => contribution - cancelled.
351
352 $positiveStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Positive'");
353 $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
354 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
355
356 $contributionStatusId = NULL;
357 if (array_key_exists($statusId, $positiveStatuses)) {
358 $contributionStatusId = array_search('Completed', $contributionStatuses);
359 }
360 if (array_key_exists($statusId, $negativeStatuses)) {
361 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
362 }
363
364 if (!$contributionStatusId) {
365 return;
366 }
367
368 $params = array(
369 'component_id' => $participantId,
370 'componentName' => 'Event',
371 'contribution_id' => $contributionId,
372 'contribution_status_id' => $contributionStatusId,
373 'skipComponentSync' => 1
374 );
375
376 //change related contribution status.
377 $updatedStatusId = CRM_Core_Payment_BaseIPN::updateContributionStatus($params);
378
379 return $updatedStatusId;
380 }
381}
382