Merge remote-tracking branch 'origin/4.5' into 4.5-master-2015-02-24-17-24-05
[civicrm-core.git] / CRM / Event / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality for batch profile update for events
38 */
39 class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
40
41 /**
42 * The title of the group.
43 *
44 * @var string
45 */
46 protected $_title;
47
48 /**
49 * Maximum profile fields that will be displayed.
50 */
51 protected $_maxFields = 9;
52
53 /**
54 * Variable to store redirect path.
55 */
56 protected $_userContext;
57
58 /**
59 * Variable to store previous status id.
60 */
61 protected $_fromStatusIds;
62
63 /**
64 * Build all the data structures needed to build the form.
65 *
66 * @return void
67 */
68 public function preProcess() {
69 /*
70 * initialize the task and row fields
71 */
72 parent::preProcess();
73
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
79 )
80 );
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
85 );
86 $this->assign('contactDetails', $contactDetails);
87 $this->assign('readOnlyFields', $readOnlyFields);
88 }
89
90 /**
91 * Build the form object.
92 *
93 *
94 * @return void
95 */
96 public function buildQuickForm() {
97 $ufGroupId = $this->get('ufGroupId');
98
99 if (!$ufGroupId) {
100 CRM_Core_Error::fatal('ufGroupId is missing');
101 }
102
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);
108
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)
115 ) {
116 $suppressFields = TRUE;
117 unset($this->_fields[$name]);
118 }
119
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;
124 }
125 }
126
127 $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
128
129 $this->addButtons(array(
130 array(
131 'type' => 'submit',
132 'name' => ts('Update Participant(s)'),
133 'isDefault' => TRUE,
134 ),
135 array(
136 'type' => 'cancel',
137 'name' => ts('Cancel'),
138 ),
139 )
140 );
141
142 $this->assign('profileTitle', $this->_title);
143 $this->assign('componentIds', $this->_participantIds);
144 $fileFieldExists = FALSE;
145
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)) . ' ) '
153 );
154 }
155
156 //fix for CRM-2752
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');
161
162 // build custom data getFields array
163 $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, $this->_roleCustomDataTypeID);
164
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);
167
168 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole,
169 CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE)
170 );
171 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields);
172 $this->_customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields);
173
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']
185 );
186 }
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)
191 ) {
192 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
193 }
194 elseif (($this->_eventNameCustomDataTypeID == $customValue['extends_entity_column_id']) &&
195 ($eventId == $entityColumnValueRole)
196 ) {
197 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
198 }
199 elseif ($this->_eventTypeCustomDataTypeID == $customValue['extends_entity_column_id'] &&
200 ($entityColumnValueEventType == $eventTypeId)
201 ) {
202 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
203 }
204 elseif (CRM_Utils_System::isNull($entityColumnValueRole)) {
205 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
206 }
207 }
208 else {
209 if ($field['name'] == 'participant_role') {
210 $field['is_multiple'] = TRUE;
211 }
212 // handle non custom fields
213 CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $participantId);
214 }
215 }
216 }
217
218 $this->assign('fields', $this->_fields);
219
220 // don't set the status message when form is submitted.
221 $buttonName = $this->controller->getButtonName('submit');
222
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');
225 }
226
227 $this->addDefaultButtons(ts('Update Participant(s)'));
228 }
229
230 /**
231 * Set default values for the form.
232 *
233 *
234 * @return void
235 */
236 public function setDefaultValues() {
237 if (empty($this->_fields)) {
238 return;
239 }
240
241 $defaults = array();
242 foreach ($this->_participantIds as $participantId) {
243 $details[$participantId] = array();
244
245 $details[$participantId] = CRM_Event_BAO_Participant::participantDetails($participantId);
246 CRM_Core_BAO_UFGroup::setProfileDefaults(NULL, $this->_fields, $defaults, FALSE, $participantId, 'Event');
247
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);
251 }
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;
257 }
258 unset($defaults["field[{$participantId}][participant_role]"]);
259 }
260 }
261 }
262
263 $this->assign('details', $details);
264 return $defaults;
265 }
266
267 /**
268 * Process the form after the input has been submitted and validated.
269 *
270 *
271 * @return void
272 */
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) {
278
279 //check for custom data
280 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
281 CRM_Core_DAO::$_nullObject,
282 $key,
283 'Participant'
284 );
285
286 $value['id'] = $key;
287 if (!empty($value['participant_register_date'])) {
288 $value['register_date'] = CRM_Utils_Date::processDate($value['participant_register_date'], $value['participant_register_date_time']);
289 }
290
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']));
295 }
296 else {
297 $value['role_id'] = $value['participant_role'];
298 }
299 }
300
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');
309 }
310
311 if ($fromStatusId != $value['status_id']) {
312 $relatedStatusChange = TRUE;
313 }
314 if ($statusClasses[$fromStatusId] != $statusClasses[$value['status_id']]) {
315 $statusChange = TRUE;
316 }
317 }
318
319 if (!empty($value['participant_source'])) {
320 $value['source'] = $value['participant_source'];
321 }
322 unset($value['participant_register_date']);
323 unset($value['participant_status']);
324 unset($value['participant_source']);
325
326 CRM_Event_BAO_Participant::create($value);
327
328 //need to trigger mails when we change status
329 if ($statusChange) {
330 CRM_Event_BAO_Participant::transitionParticipants(array($key), $value['status_id'], $fromStatusId);
331 }
332 if ($relatedStatusChange) {
333 //update related contribution status, CRM-4395
334 self::updatePendingOnlineContribution($key, $value['status_id']);
335 }
336 }
337 CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');
338 }
339 else {
340 CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
341 }
342 }
343
344 /**
345 * @param int $participantId
346 * @param int $statusId
347 *
348 * @return Ambigous|void
349 */
350 public static function updatePendingOnlineContribution($participantId, $statusId) {
351 if (!$participantId || !$statusId) {
352 return NULL;
353 }
354
355 $contributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($participantId,
356 'Event'
357 );
358 if (!$contributionId) {
359 return;
360 }
361
362 //status rules.
363 //1. participant - positive => contribution - completed.
364 //2. participant - negative => contribution - cancelled.
365
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');
369
370 $contributionStatusId = NULL;
371 if (array_key_exists($statusId, $positiveStatuses)) {
372 $contributionStatusId = array_search('Completed', $contributionStatuses);
373 }
374 if (array_key_exists($statusId, $negativeStatuses)) {
375 $contributionStatusId = array_search('Cancelled', $contributionStatuses);
376 }
377
378 if (!$contributionStatusId) {
379 return;
380 }
381
382 $params = array(
383 'component_id' => $participantId,
384 'componentName' => 'Event',
385 'contribution_id' => $contributionId,
386 'contribution_status_id' => $contributionStatusId,
387 'skipComponentSync' => 1,
388 );
389
390 //change related contribution status.
391 $updatedStatusId = CRM_Core_Payment_BaseIPN::updateContributionStatus($params);
392
393 return $updatedStatusId;
394 }
395
396 }