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');
166 $this->_eventTypeCustomDataTypeID
= CRM_Core_OptionGroup
::getValue('custom_data_type', 'ParticipantEventType', 'name');
168 // build custom data getFields array
169 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
171 $customFieldsEvent = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID
);
172 $customFieldsEventType = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID
);
174 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsRole,
175 CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
177 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsEventType, $customFields);
178 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
180 foreach ($this->_participantIds
as $participantId) {
181 $roleId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id');
182 $eventId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id');
183 $eventTypeId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
184 foreach ($this->_fields
as $name => $field) {
185 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
186 $customValue = CRM_Utils_Array
::value($customFieldID, $this->_customFields
);
187 $entityColumnValue = array();
188 if (!empty($customValue['extends_entity_column_value'])) {
189 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
190 $customValue['extends_entity_column_value']
193 $entityColumnValueRole = CRM_Utils_Array
::value($roleId, $entityColumnValue);
194 $entityColumnValueEventType = in_array($eventTypeId, $entityColumnValue) ?
$eventTypeId : NULL;
195 if (($this->_roleCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
196 ($entityColumnValueRole)
198 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
200 elseif (($this->_eventNameCustomDataTypeID
== $customValue['extends_entity_column_id']) &&
201 ($eventId == $entityColumnValueRole)
203 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
205 elseif ($this->_eventTypeCustomDataTypeID
== $customValue['extends_entity_column_id'] &&
206 ($entityColumnValueEventType == $eventTypeId)
208 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
210 elseif (CRM_Utils_System
::isNull($entityColumnValueRole)) {
211 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
215 if ($field['name'] == 'participant_role') {
216 $field['is_multiple'] = TRUE;
218 // handle non custom fields
219 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
224 $this->assign('fields', $this->_fields
);
226 // don't set the status message when form is submitted.
227 $buttonName = $this->controller
->getButtonName('submit');
229 if ($suppressFields && $buttonName != '_qf_Batch_next') {
230 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');
233 $this->addDefaultButtons(ts('Update Participant(s)'));
237 * This function sets the default values for the form.
243 function setDefaultValues() {
244 if (empty($this->_fields
)) {
249 foreach ($this->_participantIds
as $participantId) {
250 $details[$participantId] = array();
252 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
253 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
255 //get the from status ids, CRM-4323
256 if (array_key_exists('participant_status', $this->_fields
)) {
257 $this->_fromStatusIds
[$participantId] = CRM_Utils_Array
::value("field[$participantId][participant_status]", $defaults);
259 if (array_key_exists('participant_role', $this->_fields
)) {
260 if ($defaults["field[{$participantId}][participant_role]"]) {
261 $roles = $defaults["field[{$participantId}][participant_role]"];
262 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roles) as $k => $v) {
263 $defaults["field[$participantId][participant_role][{$v}]"] = 1;
265 unset($defaults["field[{$participantId}][participant_role]"]);
270 $this->assign('details', $details);
275 * process the form after the input has been submitted and validated
281 public function postProcess() {
282 $params = $this->exportValues();
283 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
284 if (isset($params['field'])) {
285 foreach ($params['field'] as $key => $value) {
287 //check for custom data
288 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
289 CRM_Core_DAO
::$_nullObject,
295 if (!empty($value['participant_register_date'])) {
296 $value['register_date'] = CRM_Utils_Date
::processDate($value['participant_register_date'], $value['participant_register_date_time']);
299 if (!empty($value['participant_role'])) {
300 $participantRoles = CRM_Event_PseudoConstant
::participantRole();
301 if (is_array($value['participant_role'])) {
302 $value['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($value['participant_role']));
305 $value['role_id'] = $value['participant_role'];
309 //need to send mail when status change
310 $statusChange = FALSE;
311 $relatedStatusChange = FALSE;
312 if (!empty($value['participant_status'])) {
313 $value['status_id'] = $value['participant_status'];
314 $fromStatusId = CRM_Utils_Array
::value($key, $this->_fromStatusIds
);
315 if (!$fromStatusId) {
316 $fromStatusId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
319 if ($fromStatusId != $value['status_id']) {
320 $relatedStatusChange = TRUE;
322 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
323 $statusChange = TRUE;
327 if (!empty($value['participant_source'])) {
328 $value['source'] = $value['participant_source'];
330 unset($value['participant_register_date']);
331 unset($value['participant_status']);
332 unset($value['participant_source']);
334 CRM_Event_BAO_Participant
::create($value);
336 //need to trigger mails when we change status
338 CRM_Event_BAO_Participant
::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
340 if ($relatedStatusChange) {
341 //update related contribution status, CRM-4395
342 self
::updatePendingOnlineContribution($key, $value['status_id']);
345 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
348 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
354 * @param $participantId
357 * @return Ambigous|void
359 static function updatePendingOnlineContribution($participantId, $statusId) {
360 if (!$participantId ||
!$statusId) {
364 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
367 if (!$contributionId) {
372 //1. participant - positive => contribution - completed.
373 //2. participant - negative => contribution - cancelled.
375 $positiveStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Positive'");
376 $negativeStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'");
377 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
379 $contributionStatusId = NULL;
380 if (array_key_exists($statusId, $positiveStatuses)) {
381 $contributionStatusId = array_search('Completed', $contributionStatuses);
383 if (array_key_exists($statusId, $negativeStatuses)) {
384 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
387 if (!$contributionStatusId) {
392 'component_id' => $participantId,
393 'componentName' => 'Event',
394 'contribution_id' => $contributionId,
395 'contribution_status_id' => $contributionStatusId,
396 'skipComponentSync' => 1
399 //change related contribution status.
400 $updatedStatusId = CRM_Core_Payment_BaseIPN
::updateContributionStatus($params);
402 return $updatedStatusId;