3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
51 protected $_maxFields = 9;
54 * Variable to store redirect path.
56 protected $_userContext;
59 * Variable to store previous status id.
61 protected $_fromStatusIds;
64 * Build all the data structures needed to build the form.
68 public function preProcess() {
70 * initialize the task and row fields
74 //get the contact read only fields to display.
75 $readOnlyFields = array_merge(array('sort_name' => ts('Name')),
76 CRM_Core_BAO_Setting
::valueOptions(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
77 'contact_autocomplete_options',
78 TRUE, NULL, FALSE, 'name', TRUE
81 //get the read only field data.
82 $returnProperties = array_fill_keys(array_keys($readOnlyFields), 1);
83 $contactDetails = CRM_Contact_BAO_Contact_Utils
::contactDetails($this->_participantIds
,
84 'CiviEvent', $returnProperties
86 $this->assign('contactDetails', $contactDetails);
87 $this->assign('readOnlyFields', $readOnlyFields);
91 * Build the form object.
96 public function buildQuickForm() {
97 $ufGroupId = $this->get('ufGroupId');
100 CRM_Core_Error
::fatal('ufGroupId is missing');
103 $this->_title
= ts('Batch Update for Events') . ' - ' . CRM_Core_BAO_UFGroup
::getTitle($ufGroupId);
104 CRM_Utils_System
::setTitle($this->_title
);
105 $this->addDefaultButtons(ts('Save'));
106 $this->_fields
= array();
107 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($ufGroupId, FALSE, CRM_Core_Action
::VIEW
);
109 // remove file type field and then limit fields
110 $suppressFields = FALSE;
111 $removehtmlTypes = array('File', 'Autocomplete-Select');
112 foreach ($this->_fields
as $name => $field) {
113 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
114 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
116 $suppressFields = TRUE;
117 unset($this->_fields
[$name]);
120 //fix to reduce size as we are using this field in grid
121 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
122 //shrink class to "form-text-medium"
123 $this->_fields
[$name]['attributes']['size'] = 19;
127 $this->_fields
= array_slice($this->_fields
, 0, $this->_maxFields
);
129 $this->addButtons(array(
132 'name' => ts('Update Participant(s)'),
137 'name' => ts('Cancel'),
142 $this->assign('profileTitle', $this->_title
);
143 $this->assign('componentIds', $this->_participantIds
);
144 $fileFieldExists = FALSE;
146 //load all campaigns.
147 if (array_key_exists('participant_campaign_id', $this->_fields
)) {
148 $this->_componentCampaigns
= array();
149 CRM_Core_PseudoConstant
::populate($this->_componentCampaigns
,
150 'CRM_Event_DAO_Participant',
151 TRUE, 'campaign_id', 'id',
152 ' id IN (' . implode(' , ', array_values($this->_participantIds
)) . ' ) '
157 // get the option value for custom data type
158 $this->_roleCustomDataTypeID
= CRM_Core_OptionGroup
::getValue('custom_data_type', 'ParticipantRole', 'name');
159 $this->_eventNameCustomDataTypeID
= CRM_Core_OptionGroup
::getValue('custom_data_type', 'ParticipantEventName', 'name');
160 $this->_eventTypeCustomDataTypeID
= CRM_Core_OptionGroup
::getValue('custom_data_type', 'ParticipantEventType', 'name');
162 // build custom data getFields array
163 $customFieldsRole = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID
);
165 $customFieldsEvent = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventNameCustomDataTypeID
);
166 $customFieldsEventType = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, $this->_eventTypeCustomDataTypeID
);
168 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsRole,
169 CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
171 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsEventType, $customFields);
172 $this->_customFields
= CRM_Utils_Array
::crmArrayMerge($customFieldsEvent, $customFields);
174 foreach ($this->_participantIds
as $participantId) {
175 $roleId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'role_id');
176 $eventId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Participant", $participantId, 'event_id');
177 $eventTypeId = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $eventId, 'event_type_id');
178 foreach ($this->_fields
as $name => $field) {
179 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
180 $customValue = CRM_Utils_Array
::value($customFieldID, $this->_customFields
);
181 $entityColumnValue = array();
182 if (!empty($customValue['extends_entity_column_value'])) {
183 $entityColumnValue = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
184 $customValue['extends_entity_column_value']
187 $entityColumnValueRole = CRM_Utils_Array
::value($roleId, $entityColumnValue);
188 $entityColumnValueEventType = in_array($eventTypeId, $entityColumnValue) ?
$eventTypeId : NULL;
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 ($this->_eventTypeCustomDataTypeID
== $customValue['extends_entity_column_id'] &&
200 ($entityColumnValueEventType == $eventTypeId)
202 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
204 elseif (CRM_Utils_System
::isNull($entityColumnValueRole)) {
205 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
209 if ($field['name'] == 'participant_role') {
210 $field['is_multiple'] = TRUE;
212 // handle non custom fields
213 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, $participantId);
218 $this->assign('fields', $this->_fields
);
220 // don't set the status message when form is submitted.
221 $buttonName = $this->controller
->getButtonName('submit');
223 if ($suppressFields && $buttonName != '_qf_Batch_next') {
224 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');
227 $this->addDefaultButtons(ts('Update Participant(s)'));
231 * Set default values for the form.
236 public function setDefaultValues() {
237 if (empty($this->_fields
)) {
242 foreach ($this->_participantIds
as $participantId) {
243 $details[$participantId] = array();
245 $details[$participantId] = CRM_Event_BAO_Participant
::participantDetails($participantId);
246 CRM_Core_BAO_UFGroup
::setProfileDefaults(NULL, $this->_fields
, $defaults, FALSE, $participantId, 'Event');
248 //get the from status ids, CRM-4323
249 if (array_key_exists('participant_status', $this->_fields
)) {
250 $this->_fromStatusIds
[$participantId] = CRM_Utils_Array
::value("field[$participantId][participant_status]", $defaults);
252 if (array_key_exists('participant_role', $this->_fields
)) {
253 if ($defaults["field[{$participantId}][participant_role]"]) {
254 $roles = $defaults["field[{$participantId}][participant_role]"];
255 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $roles) as $k => $v) {
256 $defaults["field[$participantId][participant_role][{$v}]"] = 1;
258 unset($defaults["field[{$participantId}][participant_role]"]);
263 $this->assign('details', $details);
268 * Process the form after the input has been submitted and validated.
273 public function postProcess() {
274 $params = $this->exportValues();
275 $statusClasses = CRM_Event_PseudoConstant
::participantStatusClass();
276 if (isset($params['field'])) {
277 foreach ($params['field'] as $key => $value) {
279 //check for custom data
280 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
281 CRM_Core_DAO
::$_nullObject,
287 if (!empty($value['participant_register_date'])) {
288 $value['register_date'] = CRM_Utils_Date
::processDate($value['participant_register_date'], $value['participant_register_date_time']);
291 if (!empty($value['participant_role'])) {
292 $participantRoles = CRM_Event_PseudoConstant
::participantRole();
293 if (is_array($value['participant_role'])) {
294 $value['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($value['participant_role']));
297 $value['role_id'] = $value['participant_role'];
301 //need to send mail when status change
302 $statusChange = FALSE;
303 $relatedStatusChange = FALSE;
304 if (!empty($value['participant_status'])) {
305 $value['status_id'] = $value['participant_status'];
306 $fromStatusId = CRM_Utils_Array
::value($key, $this->_fromStatusIds
);
307 if (!$fromStatusId) {
308 $fromStatusId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
311 if ($fromStatusId != $value['status_id']) {
312 $relatedStatusChange = TRUE;
314 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
315 $statusChange = TRUE;
319 if (!empty($value['participant_source'])) {
320 $value['source'] = $value['participant_source'];
322 unset($value['participant_register_date']);
323 unset($value['participant_status']);
324 unset($value['participant_source']);
326 CRM_Event_BAO_Participant
::create($value);
328 //need to trigger mails when we change status
330 CRM_Event_BAO_Participant
::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
332 if ($relatedStatusChange) {
333 //update related contribution status, CRM-4395
334 self
::updatePendingOnlineContribution($key, $value['status_id']);
337 CRM_Core_Session
::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
340 CRM_Core_Session
::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
345 * @param int $participantId
346 * @param int $statusId
348 * @return Ambigous|void
350 public static function updatePendingOnlineContribution($participantId, $statusId) {
351 if (!$participantId ||
!$statusId) {
355 $contributionId = CRM_Contribute_BAO_Contribution
::checkOnlinePendingContribution($participantId,
358 if (!$contributionId) {
363 //1. participant - positive => contribution - completed.
364 //2. participant - negative => contribution - cancelled.
366 $positiveStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Positive'");
367 $negativeStatuses = CRM_Event_PseudoConstant
::participantStatus(NULL, "class = 'Negative'");
368 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
370 $contributionStatusId = NULL;
371 if (array_key_exists($statusId, $positiveStatuses)) {
372 $contributionStatusId = array_search('Completed', $contributionStatuses);
374 if (array_key_exists($statusId, $negativeStatuses)) {
375 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
378 if (!$contributionStatusId) {
383 'component_id' => $participantId,
384 'componentName' => 'Event',
385 'contribution_id' => $contributionId,
386 'contribution_status_id' => $contributionStatusId,
387 'skipComponentSync' => 1,
390 //change related contribution status.
391 $updatedStatusId = CRM_Core_Payment_BaseIPN
::updateContributionStatus($params);
393 return $updatedStatusId;