3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
71 public function preProcess() {
73 * initialize the task and row fields
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
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
89 $this->assign('contactDetails', $contactDetails);
90 $this->assign('readOnlyFields', $readOnlyFields);
94 * Build the form object
99 public function buildQuickForm() {
100 $ufGroupId = $this->get('ufGroupId');
103 CRM_Core_Error
::fatal('ufGroupId is missing');
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
);
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)
119 $suppressFields = TRUE;
120 unset($this->_fields
[$name]);
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;
130 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
132 $this->addButtons(array(
135 'name' => ts('Update Participant(s)'),
140 'name' => ts('Cancel'),
146 $this->assign('profileTitle', $this->_title
);
147 $this->assign('componentIds', $this->_participantIds
);
148 $fileFieldExists = FALSE;
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
)) . ' ) '
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');
164 $this->_eventTypeCustomDataTypeID
= CRM_Core_OptionGroup
::getValue('custom_data_type', 'ParticipantEventType', 'name');
166 // build custom data getFields array
167 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
169 $customFieldsEvent = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID
);
170 $customFieldsEventType = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID
);
172 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsRole,
173 CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
175 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsEventType, $customFields);
176 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
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');
181 $eventTypeId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
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();
186 if (!empty($customValue['extends_entity_column_value'])) {
187 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
188 $customValue['extends_entity_column_value']
191 $entityColumnValueRole = CRM_Utils_Array
::value($roleId, $entityColumnValue);
192 $entityColumnValueEventType = in_array($eventTypeId, $entityColumnValue) ?
$eventTypeId : NULL;
193 if (($this->_roleCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
194 ($entityColumnValueRole)
196 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
198 elseif (($this->_eventNameCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
199 ($eventId == $entityColumnValueRole)
201 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
203 elseif ($this->_eventTypeCustomDataTypeID
== $customValue['extends_entity_column_id'] &&
204 ($entityColumnValueEventType == $eventTypeId)
206 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
208 elseif (CRM_Utils_System
::isNull($entityColumnValueRole)) {
209 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
213 if ($field['name'] == 'participant_role') {
214 $field['is_multiple'] = TRUE;
216 // handle non custom fields
217 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
222 $this->assign('fields', $this->_fields
);
224 // don't set the status message when form is submitted.
225 $buttonName = $this->controller
->getButtonName('submit');
227 if ($suppressFields && $buttonName != '_qf_Batch_next') {
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');
231 $this->addDefaultButtons(ts('Update Participant(s)'));
235 * Set default values for the form.
240 public function setDefaultValues() {
241 if (empty($this->_fields
)) {
246 foreach ($this->_participantIds
as $participantId) {
247 $details[$participantId] = array();
249 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
250 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
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);
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;
262 unset($defaults["field[{$participantId}][participant_role]"]);
267 $this->assign('details', $details);
272 * Process the form after the input has been submitted and validated
277 public function postProcess() {
278 $params = $this->exportValues();
279 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
280 if (isset($params['field'])) {
281 foreach ($params['field'] as $key => $value) {
283 //check for custom data
284 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
285 CRM_Core_DAO
::$_nullObject,
291 if (!empty($value['participant_register_date'])) {
292 $value['register_date'] = CRM_Utils_Date
::processDate($value['participant_register_date'], $value['participant_register_date_time']);
295 if (!empty($value['participant_role'])) {
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']));
301 $value['role_id'] = $value['participant_role'];
305 //need to send mail when status change
306 $statusChange = FALSE;
307 $relatedStatusChange = FALSE;
308 if (!empty($value['participant_status'])) {
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');
315 if ($fromStatusId != $value['status_id']) {
316 $relatedStatusChange = TRUE;
318 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
319 $statusChange = TRUE;
323 if (!empty($value['participant_source'])) {
324 $value['source'] = $value['participant_source'];
326 unset($value['participant_register_date']);
327 unset($value['participant_status']);
328 unset($value['participant_source']);
330 CRM_Event_BAO_Participant
::create($value);
332 //need to trigger mails when we change status
334 CRM_Event_BAO_Participant
::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
336 if ($relatedStatusChange) {
337 //update related contribution status, CRM-4395
338 self
::updatePendingOnlineContribution($key, $value['status_id']);
341 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
344 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
349 * @param int $participantId
350 * @param int $statusId
352 * @return Ambigous|void
354 public static function updatePendingOnlineContribution($participantId, $statusId) {
355 if (!$participantId ||
!$statusId) {
359 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
362 if (!$contributionId) {
367 //1. participant - positive => contribution - completed.
368 //2. participant - negative => contribution - cancelled.
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');
374 $contributionStatusId = NULL;
375 if (array_key_exists($statusId, $positiveStatuses)) {
376 $contributionStatusId = array_search('Completed', $contributionStatuses);
378 if (array_key_exists($statusId, $negativeStatuses)) {
379 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
382 if (!$contributionStatusId) {
387 'component_id' => $participantId,
388 'componentName' => 'Event',
389 'contribution_id' => $contributionId,
390 'contribution_status_id' => $contributionStatusId,
391 'skipComponentSync' => 1
394 //change related contribution status.
395 $updatedStatusId = CRM_Core_Payment_BaseIPN
::updateContributionStatus($params);
397 return $updatedStatusId;