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.
81 public function buildQuickForm() {
82 $ufGroupId = $this->get('ufGroupId');
84 CRM_Core_Error
::fatal('ufGroupId is missing');
87 $this->_title
= ts('Update multiple participants') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
88 CRM_Utils_System
::setTitle($this->_title
);
89 $this->addDefaultButtons(ts('Save'));
91 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
92 if (array_key_exists('participant_status', $this->_fields
)) {
93 $this->assign('statusProfile', 1);
94 $this->assignToTemplate();
97 // remove file type field and then limit fields
98 $suppressFields = FALSE;
99 $removehtmlTypes = ['File'];
100 foreach ($this->_fields
as $name => $field) {
101 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
102 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
104 $suppressFields = TRUE;
105 unset($this->_fields
[$name]);
108 //fix to reduce size as we are using this field in grid
109 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
110 //shrink class to "form-text-medium"
111 $this->_fields
[$name]['attributes']['size'] = 19;
115 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
120 'name' => ts('Update Participant(s)'),
125 'name' => ts('Cancel'),
129 $this->assign('profileTitle', $this->_title
);
130 $this->assign('componentIds', $this->_participantIds
);
131 $fileFieldExists = FALSE;
133 //load all campaigns.
134 if (array_key_exists('participant_campaign_id', $this->_fields
)) {
135 $this->_componentCampaigns
= [];
136 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
137 'CRM_Event_DAO_Participant',
138 TRUE, 'campaign_id', 'id',
139 ' id IN (' . implode(' , ', array_values($this->_participantIds
)) . ' ) '
144 // get the option value for custom data type
145 $customDataType = CRM_Core_OptionGroup
::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name');
146 $this->_roleCustomDataTypeID
= array_search('ParticipantRole', $customDataType);
147 $this->_eventNameCustomDataTypeID
= array_search('ParticipantEventName', $customDataType);
148 $this->_eventTypeCustomDataTypeID
= array_search('ParticipantEventType', $customDataType);
150 // build custom data getFields array
151 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
153 $customFieldsEvent = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID
);
154 $customFieldsEventType = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID
);
156 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsRole,
157 CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
159 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsEventType, $customFields);
160 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
162 foreach ($this->_participantIds
as $participantId) {
163 $roleId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id');
164 $eventId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id');
165 $eventTypeId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
166 foreach ($this->_fields
as $name => $field) {
167 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
168 $customValue = CRM_Utils_Array
::value($customFieldID, $this->_customFields
);
169 $entityColumnValue = [];
170 if (!empty($customValue['extends_entity_column_value'])) {
171 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
172 $customValue['extends_entity_column_value']
175 $entityColumnValueRole = CRM_Utils_Array
::value($roleId, $entityColumnValue);
176 $entityColumnValueEventType = in_array($eventTypeId, $entityColumnValue) ?
$eventTypeId : NULL;
177 if (($this->_roleCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
178 ($entityColumnValueRole)
180 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
182 elseif (($this->_eventNameCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
183 ($eventId == $entityColumnValueRole)
185 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
187 elseif ($this->_eventTypeCustomDataTypeID
== $customValue['extends_entity_column_id'] &&
188 ($entityColumnValueEventType == $eventTypeId)
190 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
192 elseif (CRM_Utils_System
::isNull($entityColumnValueRole)) {
193 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
197 if ($field['name'] == 'participant_role') {
198 $field['is_multiple'] = TRUE;
200 // handle non custom fields
201 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
206 $this->assign('fields', $this->_fields
);
208 // don't set the status message when form is submitted.
209 $buttonName = $this->controller
->getButtonName('submit');
211 if ($suppressFields && $buttonName != '_qf_Batch_next') {
212 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');
215 $this->addDefaultButtons(ts('Update Participant(s)'));
219 * Set default values for the form.
224 public function setDefaultValues() {
225 if (empty($this->_fields
)) {
230 foreach ($this->_participantIds
as $participantId) {
231 $details[$participantId] = [];
233 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
234 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
236 //get the from status ids, CRM-4323
237 if (array_key_exists('participant_status', $this->_fields
)) {
238 $this->_fromStatusIds
[$participantId] = CRM_Utils_Array
::value("field[$participantId][participant_status]", $defaults);
240 if (array_key_exists('participant_role', $this->_fields
)) {
241 if ($defaults["field[{$participantId}][participant_role]"]) {
242 $roles = $defaults["field[{$participantId}][participant_role]"];
243 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roles) as $k => $v) {
244 $defaults["field[$participantId][participant_role][{$v}]"] = 1;
246 unset($defaults["field[{$participantId}][participant_role]"]);
251 $this->assign('details', $details);
256 * Process the form after the input has been submitted and validated.
258 public function postProcess() {
259 $params = $this->exportValues();
260 $this->submit($params);
264 * @param int $participantId
265 * @param int $statusId
269 public static function updatePendingOnlineContribution($participantId, $statusId) {
270 if (!$participantId ||
!$statusId) {
274 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
277 if (!$contributionId) {
282 //1. participant - positive => contribution - completed.
283 //2. participant - negative => contribution - cancelled.
285 $positiveStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Positive'");
286 $negativeStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'");
287 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
289 $contributionStatusId = NULL;
290 if (array_key_exists($statusId, $positiveStatuses)) {
291 $contributionStatusId = array_search('Completed', $contributionStatuses);
293 if (array_key_exists($statusId, $negativeStatuses)) {
294 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
297 if (!$contributionStatusId) {
302 'component_id' => $participantId,
303 'componentName' => 'Event',
304 'contribution_id' => $contributionId,
305 'contribution_status_id' => $contributionStatusId,
306 'IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved' => 1,
309 //change related contribution status.
310 $updatedStatusId = self
::updateContributionStatus($params);
312 return $updatedStatusId;
316 * Update contribution status.
319 * This is only called from one place in the code &
320 * it is unclear whether it is a function on the way in or on the way out
322 * @param array $params
326 public static function updateContributionStatus($params) {
327 // get minimum required values.
328 $statusId = CRM_Utils_Array
::value('contribution_status_id', $params);
329 $componentId = CRM_Utils_Array
::value('component_id', $params);
330 $componentName = CRM_Utils_Array
::value('componentName', $params);
331 $contributionId = CRM_Utils_Array
::value('contribution_id', $params);
333 if (!$contributionId ||
!$componentId ||
!$componentName ||
!$statusId) {
337 $input = $ids = $objects = [];
339 //get the required ids.
340 $ids['contribution'] = $contributionId;
342 if (!$ids['contact'] = CRM_Utils_Array
::value('contact_id', $params)) {
343 $ids['contact'] = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution',
349 if ($componentName == 'Event') {
351 $ids['participant'] = $componentId;
353 if (!$ids['event'] = CRM_Utils_Array
::value('event_id', $params)) {
354 $ids['event'] = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant',
361 if ($componentName == 'Membership') {
362 $name = 'contribute';
363 $ids['membership'] = $componentId;
365 $ids['contributionPage'] = NULL;
366 $ids['contributionRecur'] = NULL;
367 $input['component'] = $name;
369 $baseIPN = new CRM_Core_Payment_BaseIPN();
370 $transaction = new CRM_Core_Transaction();
372 // reset template values.
373 $template = CRM_Core_Smarty
::singleton();
374 $template->clearTemplateVars();
376 if (!$baseIPN->validateData($input, $ids, $objects, FALSE)) {
377 CRM_Core_Error
::fatal();
380 $contribution = &$objects['contribution'];
382 $contributionStatuses = CRM_Core_PseudoConstant
::get('CRM_Contribute_DAO_Contribution', 'contribution_status_id', [
383 'labelColumn' => 'name',
386 $input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'] = CRM_Utils_Array
::value('IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved', $params);
387 if ($statusId == $contributionStatuses['Cancelled']) {
388 $baseIPN->cancelled($objects, $transaction, $input);
389 $transaction->commit();
392 elseif ($statusId == $contributionStatuses['Failed']) {
393 $baseIPN->failed($objects, $transaction, $input);
394 $transaction->commit();
398 // status is not pending
399 if ($contribution->contribution_status_id
!= $contributionStatuses['Pending']) {
400 $transaction->commit();
404 //set values for ipn code.
408 'payment_instrument_id',
410 if (!$input[$field] = CRM_Utils_Array
::value($field, $params)) {
411 $input[$field] = $contribution->$field;
414 if (!$input['trxn_id'] = CRM_Utils_Array
::value('trxn_id', $params)) {
415 $input['trxn_id'] = $contribution->invoice_id
;
417 if (!$input['amount'] = CRM_Utils_Array
::value('total_amount', $params)) {
418 $input['amount'] = $contribution->total_amount
;
420 $input['is_test'] = $contribution->is_test
;
421 $input['net_amount'] = $contribution->net_amount
;
422 if (!empty($input['fee_amount']) && !empty($input['amount'])) {
423 $input['net_amount'] = $input['amount'] - $input['fee_amount'];
426 //complete the contribution.
427 // @todo use the api - ie civicrm_api3('Contribution', 'completetransaction', $input);
428 // as this method is not preferred / supported.
429 $baseIPN->completeTransaction($input, $ids, $objects, $transaction, FALSE);
431 // reset template values before processing next transactions
432 $template->clearTemplateVars();
438 * Assign the minimal set of variables to the template.
440 public function assignToTemplate() {
441 $notifyingStatuses = ['Pending from waitlist', 'Pending from approval', 'Expired', 'Cancelled'];
442 $notifyingStatuses = array_intersect($notifyingStatuses, CRM_Event_PseudoConstant
::participantStatus());
443 $this->assign('status', TRUE);
444 if (!empty($notifyingStatuses)) {
445 $s = '<em>' . implode('</em>, <em>', $notifyingStatuses) . '</em>';
446 $this->assign('notifyingStatuses', $s);
453 public function submit($params) {
454 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
455 if (isset($params['field'])) {
456 foreach ($params['field'] as $key => $value) {
458 //check for custom data
459 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
463 foreach (array_keys($value) as $fieldName) {
464 // Unset the original custom field now that it has been formatting to the 'custom'
465 // array as it may not be in the right format for the api as is (notably for
466 // multiple checkbox values).
467 // @todo extract submit functions on other Batch update classes &
468 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test them.
469 if (substr($fieldName, 0, 7) === 'custom_') {
470 unset($value[$fieldName]);
476 if (!empty($value['participant_role'])) {
477 if (is_array($value['participant_role'])) {
478 $value['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($value['participant_role']));
481 $value['role_id'] = $value['participant_role'];
485 //need to send mail when status change
486 $statusChange = FALSE;
487 $relatedStatusChange = FALSE;
488 if (!empty($value['participant_status'])) {
489 $value['status_id'] = $value['participant_status'];
490 $fromStatusId = CRM_Utils_Array
::value($key, $this->_fromStatusIds
);
491 if (!$fromStatusId) {
492 $fromStatusId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
495 if ($fromStatusId != $value['status_id']) {
496 $relatedStatusChange = TRUE;
498 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
499 $statusChange = TRUE;
503 unset($value['participant_status']);
505 civicrm_api3('Participant', 'create', $value);
507 //need to trigger mails when we change status
509 CRM_Event_BAO_Participant
::transitionParticipants([$key], $value['status_id'], $fromStatusId);
511 if ($relatedStatusChange) {
512 //update related contribution status, CRM-4395
513 self
::updatePendingOnlineContribution($key, $value['status_id']);
516 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
519 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');