3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality for batch profile update for events.
21 class CRM_Event_Form_Task_Batch
extends CRM_Event_Form_Task
{
24 * The title of the group.
31 * Maximum profile fields that will be displayed.
34 protected $_maxFields = 9;
37 * Variable to store redirect path.
40 protected $_userContext;
43 * Variable to store previous status id.
46 protected $_fromStatusIds;
49 * Build all the data structures needed to build the form.
53 public function preProcess() {
55 * initialize the task and row fields
59 //get the contact read only fields to display.
60 $readOnlyFields = array_merge(['sort_name' => ts('Name')],
61 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
62 'contact_autocomplete_options',
63 TRUE, NULL, FALSE, 'name', TRUE
66 //get the read only field data.
67 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
68 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_participantIds
,
69 'CiviEvent', $returnProperties
71 $this->assign('contactDetails', $contactDetails);
72 $this->assign('readOnlyFields', $readOnlyFields);
76 * Build the form object.
78 * @throws \CRM_Core_Exception
80 public function buildQuickForm() {
81 $ufGroupId = $this->get('ufGroupId');
83 CRM_Core_Error
::statusBounce(ts('ufGroupId is missing'));
86 $this->_title
= ts('Update multiple participants') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
87 CRM_Utils_System
::setTitle($this->_title
);
88 $this->addDefaultButtons(ts('Save'));
90 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
91 if (array_key_exists('participant_status', $this->_fields
)) {
92 $this->assign('statusProfile', 1);
93 $this->assignToTemplate();
96 // remove file type field and then limit fields
97 $suppressFields = FALSE;
98 $removehtmlTypes = ['File'];
99 foreach ($this->_fields
as $name => $field) {
100 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
101 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
103 $suppressFields = TRUE;
104 unset($this->_fields
[$name]);
107 //fix to reduce size as we are using this field in grid
108 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
109 //shrink class to "form-text-medium"
110 $this->_fields
[$name]['attributes']['size'] = 19;
114 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
119 'name' => ts('Update Participant(s)'),
124 'name' => ts('Cancel'),
128 $this->assign('profileTitle', $this->_title
);
129 $this->assign('componentIds', $this->_participantIds
);
130 $fileFieldExists = FALSE;
132 //load all campaigns.
133 if (array_key_exists('participant_campaign_id', $this->_fields
)) {
134 $this->_componentCampaigns
= [];
135 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
136 'CRM_Event_DAO_Participant',
137 TRUE, 'campaign_id', 'id',
138 ' id IN (' . implode(' , ', array_values($this->_participantIds
)) . ' ) '
143 // get the option value for custom data type
144 $customDataType = CRM_Core_OptionGroup
::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name');
145 $this->_roleCustomDataTypeID
= array_search('ParticipantRole', $customDataType);
146 $this->_eventNameCustomDataTypeID
= array_search('ParticipantEventName', $customDataType);
147 $this->_eventTypeCustomDataTypeID
= array_search('ParticipantEventType', $customDataType);
149 // build custom data getFields array
150 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
152 $customFieldsEvent = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID
);
153 $customFieldsEventType = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID
);
155 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsRole,
156 CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
158 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsEventType, $customFields);
159 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
161 foreach ($this->_participantIds
as $participantId) {
162 $roleId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id');
163 $eventId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id');
164 $eventTypeId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
165 foreach ($this->_fields
as $name => $field) {
166 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
167 $customValue = $this->_customFields
[$customFieldID] ??
NULL;
168 $entityColumnValue = [];
169 if (!empty($customValue['extends_entity_column_value'])) {
170 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
171 $customValue['extends_entity_column_value']
174 if (($this->_roleCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
175 in_array($roleId, $entityColumnValue)
177 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
179 elseif (($this->_eventNameCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
180 in_array($eventId, $entityColumnValue)
182 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
184 elseif ($this->_eventTypeCustomDataTypeID
== $customValue['extends_entity_column_id'] &&
185 in_array($eventTypeId, $entityColumnValue)
187 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
189 elseif (CRM_Utils_System
::isNull($entityColumnValue)) {
190 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
194 if ($field['name'] === 'participant_role') {
195 $field['is_multiple'] = TRUE;
197 // handle non custom fields
198 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
203 $this->assign('fields', $this->_fields
);
205 // don't set the status message when form is submitted.
206 $buttonName = $this->controller
->getButtonName('submit');
208 if ($suppressFields && $buttonName !== '_qf_Batch_next') {
209 CRM_Core_Session
::setStatus(ts("File type field(s) in the selected profile are not supported for Update multiple participants."), ts('Unsupported Field Type'), 'info');
212 $this->addDefaultButtons(ts('Update Participant(s)'));
216 * Set default values for the form.
220 public function setDefaultValues() {
221 if (empty($this->_fields
)) {
226 foreach ($this->_participantIds
as $participantId) {
227 $details[$participantId] = [];
229 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
230 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
232 //get the from status ids, CRM-4323
233 if (array_key_exists('participant_status', $this->_fields
)) {
234 $this->_fromStatusIds
[$participantId] = $defaults["field[$participantId][participant_status]"] ??
NULL;
236 if (array_key_exists('participant_role', $this->_fields
)) {
237 if ($defaults["field[{$participantId}][participant_role]"]) {
238 $roles = $defaults["field[{$participantId}][participant_role]"];
239 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roles) as $k => $v) {
240 $defaults["field[$participantId][participant_role][{$v}]"] = 1;
242 unset($defaults["field[{$participantId}][participant_role]"]);
247 $this->assign('details', $details);
252 * Process the form after the input has been submitted and validated.
254 public function postProcess() {
255 $params = $this->exportValues();
256 $this->submit($params);
260 * @param int $participantId
261 * @param int $statusId
263 * @throws \CRM_Core_Exception
264 * @throws \CiviCRM_API3_Exception
266 public static function updatePendingOnlineContribution($participantId, $statusId) {
268 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
271 if (!$contributionId) {
276 //1. participant - positive => contribution - completed.
277 //2. participant - negative => contribution - cancelled.
279 $positiveStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Positive'");
280 $negativeStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'");
281 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
283 if (array_key_exists($statusId, $positiveStatuses)) {
285 'component_id' => $participantId,
286 'contribution_id' => $contributionId,
287 'IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved' => 1,
290 //change related contribution status.
291 self
::updateContributionStatus($params);
293 if (array_key_exists($statusId, $negativeStatuses)) {
294 civicrm_api3('Contribution', 'create', ['id' => $contributionId, 'contribution_status_id' => 'Cancelled']);
301 * Update contribution status.
303 * @param array $params
305 * @throws \CRM_Core_Exception
306 * @throws \CiviCRM_API3_Exception
310 * This is only called from one place in the code &
311 * it is unclear whether it is a function on the way in or on the way out
314 public static function updateContributionStatus($params) {
315 $input = ['component' => 'event'];
317 // reset template values.
318 $template = CRM_Core_Smarty
::singleton();
319 $template->clearTemplateVars();
321 $contribution = new CRM_Contribute_BAO_Contribution();
322 $contribution->id
= $params['contribution_id'];
323 $contribution->fetch();
325 $contributionStatuses = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', [
326 'labelColumn' => 'name',
329 $input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'] = $params['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'] ??
NULL;
331 // status is not pending
332 if ($contribution->contribution_status_id
!= $contributionStatuses['Pending']) {
336 //set values for ipn code.
340 'payment_instrument_id',
342 if (!$input[$field] = CRM_Utils_Array
::value($field, $params)) {
343 $input[$field] = $contribution->$field;
346 if (!$input['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $params)) {
347 $input['trxn_id'] = $contribution->invoice_id
;
349 if (!$input['amount'] = CRM_Utils_Array
::value('total_amount', $params)) {
350 $input['amount'] = $contribution->total_amount
;
352 $input['is_test'] = $contribution->is_test
;
353 $input['net_amount'] = $contribution->net_amount
;
354 if (!empty($input['fee_amount']) && !empty($input['amount'])) {
355 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
358 //complete the contribution.
359 // @todo use the api - ie civicrm_api3('Contribution', 'completetransaction', $input);
360 // as this method is not preferred / supported.
361 CRM_Contribute_BAO_Contribution
::completeOrder($input, NULL, $contribution->id ??
NULL);
363 // reset template values before processing next transactions
364 $template->clearTemplateVars();
368 * Assign the minimal set of variables to the template.
370 public function assignToTemplate() {
371 $notifyingStatuses = ['Pending from waitlist', 'Pending from approval', 'Expired', 'Cancelled'];
372 $notifyingStatuses = array_intersect($notifyingStatuses, CRM_Event_PseudoConstant
::participantStatus());
373 $this->assign('status', TRUE);
374 if (!empty($notifyingStatuses)) {
375 $s = '<em>' . implode('</em>, <em>', $notifyingStatuses) . '</em>';
376 $this->assign('notifyingStatuses', $s);
381 * @param array $params
383 * @throws \CRM_Core_Exception
384 * @throws \CiviCRM_API3_Exception
386 public function submit($params) {
387 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
388 if (isset($params['field'])) {
389 foreach ($params['field'] as $participantID => $value) {
391 //check for custom data
392 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
396 foreach (array_keys($value) as $fieldName) {
397 // Unset the original custom field now that it has been formatting to the 'custom'
398 // array as it may not be in the right format for the api as is (notably for
399 // multiple checkbox values).
400 // @todo extract submit functions on other Batch update classes &
401 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test them.
402 if (substr($fieldName, 0, 7) === 'custom_') {
403 unset($value[$fieldName]);
407 $value['id'] = $participantID;
409 if (!empty($value['participant_role'])) {
410 if (is_array($value['participant_role'])) {
411 $value['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($value['participant_role']));
414 $value['role_id'] = $value['participant_role'];
418 //need to send mail when status change
419 $statusChange = FALSE;
420 $relatedStatusChange = FALSE;
421 if (!empty($value['participant_status'])) {
422 $value['status_id'] = $value['participant_status'];
423 $fromStatusId = $this->_fromStatusIds
[$participantID] ??
NULL;
424 if (!$fromStatusId) {
425 $fromStatusId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id');
428 if ($fromStatusId != $value['status_id']) {
429 $relatedStatusChange = TRUE;
431 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
432 $statusChange = TRUE;
436 unset($value['participant_status']);
438 civicrm_api3('Participant', 'create', $value);
440 //need to trigger mails when we change status
442 CRM_Event_BAO_Participant
::transitionParticipants([$participantID], $value['status_id'], $fromStatusId);
444 if ($relatedStatusChange && $participantID && $value['status_id']) {
445 //update related contribution status, CRM-4395
446 self
::updatePendingOnlineContribution((int) $participantID, $value['status_id']);
449 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
452 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');