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