3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class provides the functionality for batch profile update for events
39 class CRM_Event_Form_Task_Batch
extends CRM_Event_Form_Task
{
42 * the title of the group
49 * maximum profile fields that will be displayed
52 protected $_maxFields = 9;
55 * variable to store redirect path
58 protected $_userContext;
61 * variable to store previous status id.
64 protected $_fromStatusIds;
67 * build all the data structures needed to build the form
72 function preProcess() {
74 * initialize the task and row fields
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
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
90 $this->assign('contactDetails', $contactDetails);
91 $this->assign('readOnlyFields', $readOnlyFields);
101 function buildQuickForm() {
102 $ufGroupId = $this->get('ufGroupId');
105 CRM_Core_Error
::fatal('ufGroupId is missing');
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
);
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)
121 $suppressFields = TRUE;
122 unset($this->_fields
[$name]);
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;
132 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
134 $this->addButtons(array(
137 'name' => ts('Update Participant(s)'),
142 'name' => ts('Cancel'),
148 $this->assign('profileTitle', $this->_title
);
149 $this->assign('componentIds', $this->_participantIds
);
150 $fileFieldExists = FALSE;
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
)) . ' ) '
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');
167 // build custom data getFields array
168 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
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)
174 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
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 (!empty($customValue['extends_entity_column_value'])) {
184 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
185 $customValue['extends_entity_column_value']
188 $entityColumnValueRole = CRM_Utils_Array
::value($roleId, $entityColumnValue);
189 if (($this->_roleCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
190 ($entityColumnValueRole)
192 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
194 elseif (($this->_eventNameCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
195 ($eventId == $entityColumnValueRole)
197 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
199 elseif (CRM_Utils_System
::isNull($entityColumnValueRole)) {
200 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
204 if ($field['name'] == 'participant_role') {
205 $field['is_multiple'] = TRUE;
207 // handle non custom fields
208 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
213 $this->assign('fields', $this->_fields
);
215 // don't set the status message when form is submitted.
216 $buttonName = $this->controller
->getButtonName('submit');
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');
222 $this->addDefaultButtons(ts('Update Participant(s)'));
226 * This function sets the default values for the form.
232 function setDefaultValues() {
233 if (empty($this->_fields
)) {
238 foreach ($this->_participantIds
as $participantId) {
239 $details[$participantId] = array();
241 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
242 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
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);
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;
254 unset($defaults["field[{$participantId}][participant_role]"]);
259 $this->assign('details', $details);
264 * process the form after the input has been submitted and validated
270 public function postProcess() {
271 $params = $this->exportValues();
272 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
273 if (isset($params['field'])) {
274 foreach ($params['field'] as $key => $value) {
276 //check for custom data
277 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
278 CRM_Core_DAO
::$_nullObject,
284 if (!empty($value['participant_register_date'])) {
285 $value['register_date'] = CRM_Utils_Date
::processDate($value['participant_register_date'], $value['participant_register_date_time']);
288 if (!empty($value['participant_role'])) {
289 $participantRoles = CRM_Event_PseudoConstant
::participantRole();
290 if (is_array($value['participant_role'])) {
291 $value['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($value['participant_role']));
294 $value['role_id'] = $value['participant_role'];
298 //need to send mail when status change
299 $statusChange = FALSE;
300 $relatedStatusChange = FALSE;
301 if (!empty($value['participant_status'])) {
302 $value['status_id'] = $value['participant_status'];
303 $fromStatusId = CRM_Utils_Array
::value($key, $this->_fromStatusIds
);
304 if (!$fromStatusId) {
305 $fromStatusId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
308 if ($fromStatusId != $value['status_id']) {
309 $relatedStatusChange = TRUE;
311 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
312 $statusChange = TRUE;
316 if (!empty($value['participant_source'])) {
317 $value['source'] = $value['participant_source'];
319 unset($value['participant_register_date']);
320 unset($value['participant_status']);
321 unset($value['participant_source']);
323 CRM_Event_BAO_Participant
::create($value);
325 //need to trigger mails when we change status
327 CRM_Event_BAO_Participant
::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
329 if ($relatedStatusChange) {
330 //update related contribution status, CRM-4395
331 self
::updatePendingOnlineContribution($key, $value['status_id']);
334 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
337 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
343 * @param $participantId
346 * @return Ambigous|void
348 static function updatePendingOnlineContribution($participantId, $statusId) {
349 if (!$participantId ||
!$statusId) {
353 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
356 if (!$contributionId) {
361 //1. participant - positive => contribution - completed.
362 //2. participant - negative => contribution - cancelled.
364 $positiveStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Positive'");
365 $negativeStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'");
366 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
368 $contributionStatusId = NULL;
369 if (array_key_exists($statusId, $positiveStatuses)) {
370 $contributionStatusId = array_search('Completed', $contributionStatuses);
372 if (array_key_exists($statusId, $negativeStatuses)) {
373 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
376 if (!$contributionStatusId) {
381 'component_id' => $participantId,
382 'componentName' => 'Event',
383 'contribution_id' => $contributionId,
384 'contribution_status_id' => $contributionStatusId,
385 'skipComponentSync' => 1
388 //change related contribution status.
389 $updatedStatusId = CRM_Core_Payment_BaseIPN
::updateContributionStatus($params);
391 return $updatedStatusId;