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