Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2013 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This class generates form components for processing a participation | |
39 | * in an event | |
40 | */ | |
41 | class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { | |
42 | ||
43 | /** | |
44 | * the values for the contribution db object | |
45 | * | |
46 | * @var array | |
47 | * @protected | |
48 | */ | |
49 | public $_values; | |
50 | ||
51 | /** | |
52 | * the values for the quickconfig for priceset | |
53 | * | |
54 | * @var boolean | |
55 | * @protected | |
56 | */ | |
57 | public $_quickConfig = NULL; | |
58 | ||
59 | /** | |
60 | * Price Set ID, if the new price set method is used | |
61 | * | |
62 | * @var int | |
63 | * @protected | |
64 | */ | |
65 | public $_priceSetId; | |
66 | ||
67 | /** | |
68 | * Array of fields for the price set | |
69 | * | |
70 | * @var array | |
71 | * @protected | |
72 | */ | |
73 | public $_priceSet; | |
74 | ||
75 | /** | |
76 | * the id of the participation that we are proceessing | |
77 | * | |
78 | * @var int | |
79 | * @protected | |
80 | */ | |
81 | public $_id; | |
82 | ||
83 | /** | |
84 | * the id of the note | |
85 | * | |
86 | * @var int | |
87 | * @protected | |
88 | */ | |
89 | protected $_noteId = NULL; | |
90 | ||
91 | /** | |
92 | * the id of the contact associated with this participation | |
93 | * | |
94 | * @var int | |
95 | * @protected | |
96 | */ | |
97 | public $_contactId; | |
98 | ||
99 | /** | |
100 | * array of event values | |
101 | * | |
102 | * @var array | |
103 | * @protected | |
104 | */ | |
105 | protected $_event; | |
106 | ||
107 | /** | |
108 | * Are we operating in "single mode", i.e. adding / editing only | |
109 | * one participant record, or is this a batch add operation | |
110 | * | |
111 | * @var boolean | |
112 | */ | |
113 | public $_single = FALSE; | |
114 | ||
115 | /** | |
116 | * If event is paid or unpaid | |
117 | */ | |
118 | public $_isPaidEvent; | |
119 | ||
120 | /** | |
121 | * Page action | |
122 | */ | |
123 | public $_action; | |
124 | ||
125 | /** | |
126 | * Role Id | |
127 | */ | |
128 | protected $_roleId = NULL; | |
129 | ||
130 | /** | |
131 | * Event Type Id | |
132 | */ | |
133 | protected $_eventTypeId = NULL; | |
134 | ||
135 | /** | |
136 | * participant status Id | |
137 | */ | |
138 | protected $_statusId = NULL; | |
139 | ||
140 | /** | |
141 | * cache all the participant statuses | |
142 | */ | |
143 | protected $_participantStatuses; | |
144 | ||
145 | /** | |
146 | * participant mode | |
147 | */ | |
148 | public $_mode = NULL; | |
149 | ||
150 | /** | |
151 | * event ID preselect | |
152 | */ | |
153 | public $_eID = NULL; | |
154 | ||
155 | /* | |
156 | *Line Item for Price Set | |
157 | */ | |
158 | ||
159 | public $_lineItem = NULL; | |
160 | /* | |
161 | *Contribution mode for event registration for offline mode | |
162 | */ | |
163 | ||
164 | public $_contributeMode = 'direct'; | |
165 | ||
166 | public $_online; | |
167 | ||
168 | /** | |
169 | * store id of role custom data type ( option value ) | |
170 | */ | |
171 | protected $_roleCustomDataTypeID; | |
172 | ||
173 | /** | |
174 | * store id of event Name custom data type ( option value) | |
175 | */ | |
176 | protected $_eventNameCustomDataTypeID; | |
177 | ||
178 | /** | |
179 | * selected discount id | |
180 | */ | |
181 | public $_originalDiscountId = NULL; | |
182 | ||
183 | /** | |
184 | * event id | |
185 | */ | |
186 | public $_eventId = NULL; | |
187 | ||
188 | /** | |
189 | * id of payment, if any | |
190 | */ | |
191 | public $_paymentId = NULL; | |
192 | ||
193 | /** | |
194 | * array of participant role custom data | |
195 | */ | |
196 | public $_participantRoleIds = array(); | |
197 | ||
198 | /** | |
199 | * Function to set variables up before form is built | |
200 | * | |
201 | * @return void | |
202 | * @access public | |
203 | */ | |
204 | public function preProcess() { | |
205 | $this->_showFeeBlock = CRM_Utils_Array::value('eventId', $_GET); | |
206 | $this->assign('showFeeBlock', FALSE); | |
207 | $this->assign('feeBlockPaid', FALSE); | |
208 | ||
209 | $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
210 | $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
211 | $this->_eID = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
212 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
213 | $this->assign('context', $this->_context); | |
214 | ||
215 | if ($this->_contactId) { | |
216 | $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); | |
217 | $this->assign('displayName', $displayName); | |
218 | // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container | |
219 | CRM_Utils_System::setTitle(ts('Event Registration for') . ' ' . $displayName); | |
220 | } | |
221 | ||
222 | // check the current path, if search based, then dont get participantID | |
223 | // CRM-5792 | |
224 | $path = CRM_Utils_System::currentPath(); | |
225 | if (strpos($path, 'civicrm/contact/search') === 0) { | |
226 | $this->_id = NULL; | |
227 | } | |
228 | else { | |
229 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
230 | } | |
231 | ||
232 | if ($this->_id) { | |
29c61b58 | 233 | $this->assign('participantId', $this->_id); |
29c61b58 PJ |
234 | $statusId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $this->_id, 'status_id', 'id'); |
235 | ||
6a488035 TO |
236 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
237 | $this->_id, 'id', 'participant_id' | |
238 | ); | |
a4e0d7b5 DG |
239 | // CRM-12615 - Get payment information from the primary registration |
240 | if ((!$this->_paymentId) && ($this->_action == CRM_Core_Action::UPDATE)) { | |
241 | $registered_by_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', | |
242 | $this->_id, 'registered_by_id', 'id' | |
243 | ); | |
244 | if ($registered_by_id) { | |
245 | $this->_paymentId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
246 | $registered_by_id, 'id', 'participant_id' | |
247 | ); | |
248 | $this->assign('registeredByParticipantId', $registered_by_id); | |
249 | } | |
250 | } | |
6a488035 TO |
251 | } |
252 | ||
253 | // get the option value for custom data type | |
254 | $this->_roleCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantRole', 'name'); | |
255 | $this->_eventNameCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventName', 'name'); | |
256 | $this->_eventTypeCustomDataTypeID = CRM_Core_OptionGroup::getValue('custom_data_type', 'ParticipantEventType', 'name'); | |
257 | $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); | |
258 | $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); | |
259 | $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); | |
260 | ||
261 | if ($this->_mode) { | |
262 | $this->assign('participantMode', $this->_mode); | |
263 | ||
264 | $this->_paymentProcessor = array('billing_mode' => 1); | |
265 | ||
266 | $validProcessors = array(); | |
267 | $processors = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, "billing_mode IN ( 1, 3 )"); | |
268 | ||
269 | foreach ($processors as $ppID => $label) { | |
270 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($ppID, $this->_mode); | |
271 | if ($paymentProcessor['payment_processor_type'] == 'PayPal' && !$paymentProcessor['user_name']) { | |
272 | continue; | |
273 | } | |
274 | elseif ($paymentProcessor['payment_processor_type'] == 'Dummy' && $this->_mode == 'live') { | |
275 | continue; | |
276 | } | |
277 | else { | |
278 | $paymentObject = CRM_Core_Payment::singleton($this->_mode, $paymentProcessor, $this); | |
279 | $error = $paymentObject->checkConfig(); | |
280 | if (empty($error)) { | |
281 | $validProcessors[$ppID] = $label; | |
282 | } | |
283 | $paymentObject = NULL; | |
284 | } | |
285 | } | |
286 | if (empty($validProcessors)) { | |
287 | CRM_Core_Error::fatal(ts('Could not find valid payment processor for this page')); | |
288 | } | |
289 | else { | |
290 | $this->_processors = $validProcessors; | |
291 | } | |
292 | // also check for billing information | |
293 | // get the billing location type | |
180409a4 | 294 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
6a488035 TO |
295 | // CRM-8108 remove ts around Billing location type |
296 | //$this->_bltID = array_search( ts('Billing'), $locationTypes ); | |
297 | $this->_bltID = array_search('Billing', $locationTypes); | |
298 | if (!$this->_bltID) { | |
299 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); | |
300 | } | |
301 | $this->set('bltID', $this->_bltID); | |
302 | $this->assign('bltID', $this->_bltID); | |
303 | ||
304 | $this->_fields = array(); | |
305 | ||
306 | CRM_Core_Payment_Form::setCreditCardFields($this); | |
307 | ||
308 | // this required to show billing block | |
309 | $this->assign_by_ref('paymentProcessor', $paymentProcessor); | |
310 | $this->assign('hidePayPalExpress', TRUE); | |
311 | } | |
312 | ||
313 | if ($this->_showFeeBlock) { | |
314 | $this->assign('showFeeBlock', TRUE); | |
315 | $isMonetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_showFeeBlock, 'is_monetary'); | |
316 | if ($isMonetary) { | |
317 | $this->assign('feeBlockPaid', TRUE); | |
318 | } | |
319 | return CRM_Event_Form_EventFees::preProcess($this); | |
320 | } | |
321 | ||
322 | //custom data related code | |
323 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
324 | $this->assign('cdType', FALSE); | |
325 | if ($this->_cdType) { | |
326 | $this->assign('cdType', TRUE); | |
63dbed83 | 327 | return CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, NULL, NULL, NULL, TRUE); |
6a488035 TO |
328 | } |
329 | ||
330 | //check the mode when this form is called either single or as | |
331 | //search task action | |
332 | if ($this->_id || $this->_contactId || $this->_context == 'standalone') { | |
333 | $this->_single = TRUE; | |
334 | $this->assign('urlPath', 'civicrm/contact/view/participant'); | |
335 | if (!$this->_id && !$this->_contactId) { | |
336 | $breadCrumbs = array(array('title' => ts('CiviEvent Dashboard'), | |
337 | 'url' => CRM_Utils_System::url('civicrm/event', 'reset=1'), | |
338 | )); | |
339 | ||
340 | CRM_Utils_System::appendBreadCrumb($breadCrumbs); | |
341 | } | |
342 | } | |
343 | else { | |
344 | //set the appropriate action | |
345 | $context = $this->get('context'); | |
346 | $urlString = 'civicrm/contact/search'; | |
347 | $this->_action = CRM_Core_Action::BASIC; | |
348 | switch ($context) { | |
349 | case 'advanced': | |
350 | $urlString = 'civicrm/contact/search/advanced'; | |
351 | $this->_action = CRM_Core_Action::ADVANCED; | |
352 | break; | |
353 | ||
354 | case 'builder': | |
355 | $urlString = 'civicrm/contact/search/builder'; | |
356 | $this->_action = CRM_Core_Action::PROFILE; | |
357 | break; | |
358 | ||
359 | case 'basic': | |
360 | $urlString = 'civicrm/contact/search/basic'; | |
361 | $this->_action = CRM_Core_Action::BASIC; | |
362 | break; | |
363 | ||
364 | case 'custom': | |
365 | $urlString = 'civicrm/contact/search/custom'; | |
366 | $this->_action = CRM_Core_Action::COPY; | |
367 | break; | |
368 | } | |
369 | parent::preProcess(); | |
370 | ||
371 | $this->_single = FALSE; | |
372 | $this->_contactId = NULL; | |
373 | ||
374 | //set ajax path, this used for custom data building | |
375 | $this->assign('urlPath', $urlString); | |
376 | $this->assign('urlPathVar', "_qf_Participant_display=true&qfKey={$this->controller->_key}"); | |
377 | } | |
378 | ||
379 | $this->assign('single', $this->_single); | |
380 | ||
381 | if (!$this->_id) { | |
382 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add'); | |
383 | } | |
384 | $this->assign('action', $this->_action); | |
385 | ||
386 | // check for edit permission | |
387 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { | |
388 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
389 | } | |
390 | ||
391 | if ($this->_action & CRM_Core_Action::DELETE) { | |
392 | // check delete permission for contribution | |
393 | if ($this->_id && $this->_paymentId && !CRM_Core_Permission::checkActionPermission('CiviContribute', $this->_action)) { | |
394 | CRM_Core_Error::fatal(ts("This Participant is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record.")); | |
395 | } | |
396 | return; | |
397 | } | |
398 | ||
399 | if ($this->_id) { | |
400 | // assign participant id to the template | |
401 | $this->assign('participantId', $this->_id); | |
402 | $this->_roleId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'role_id'); | |
403 | } | |
404 | ||
405 | // when fee amount is included in form | |
8cc574cf | 406 | if (!empty($_POST['hidden_feeblock']) || !empty($_POST['send_receipt'])) { |
6a488035 TO |
407 | CRM_Event_Form_EventFees::preProcess($this); |
408 | CRM_Event_Form_EventFees::buildQuickForm($this); | |
409 | CRM_Event_Form_EventFees::setDefaultValues($this); | |
410 | } | |
411 | ||
412 | // when custom data is included in this page | |
a7488080 | 413 | if (!empty($_POST['hidden_custom'])) { |
6a488035 | 414 | //custom data of type participant role |
a7488080 | 415 | if (!empty($_POST['role_id'])) { |
6a488035 TO |
416 | foreach ($_POST['role_id'] as $k => $val) { |
417 | $roleID = $val; | |
418 | CRM_Custom_Form_CustomData::preProcess($this, $this->_roleCustomDataTypeID, $k, 1, 'Participant', $this->_id); | |
419 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
420 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
421 | } | |
422 | } | |
423 | ||
424 | //custom data of type participant event | |
425 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventNameCustomDataTypeID, $_POST['event_id'], 1, 'Participant', $this->_id); | |
426 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
427 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
428 | ||
429 | // custom data of type participant event type | |
430 | $eventTypeId = NULL; | |
431 | if ($eventId = CRM_Utils_Array::value('event_id', $_POST)) { | |
432 | $eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'event_type_id', 'id'); | |
433 | } | |
434 | CRM_Custom_Form_CustomData::preProcess($this, $this->_eventTypeCustomDataTypeID, $eventTypeId, | |
435 | 1, 'Participant', $this->_id | |
436 | ); | |
437 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
438 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
439 | ||
440 | //custom data of type participant, ( we 'null' to reset subType and subName) | |
441 | CRM_Custom_Form_CustomData::preProcess($this, 'null', 'null', 1, 'Participant', $this->_id); | |
442 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
443 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
444 | } | |
445 | ||
446 | // CRM-4395, get the online pending contribution id. | |
447 | $this->_onlinePendingContributionId = NULL; | |
448 | if (!$this->_mode && $this->_id && ($this->_action & CRM_Core_Action::UPDATE)) { | |
449 | $this->_onlinePendingContributionId = CRM_Contribute_BAO_Contribution::checkOnlinePendingContribution($this->_id, | |
450 | 'Event' | |
451 | ); | |
452 | } | |
453 | $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId); | |
454 | $roleIds = CRM_Event_PseudoConstant::participantRole(); | |
455 | ||
456 | if (!empty($roleIds)) { | |
457 | $query = " | |
458 | SELECT civicrm_custom_group.name as name, | |
459 | civicrm_custom_group.id as id, | |
460 | extends_entity_column_value as value | |
461 | FROM civicrm_custom_group | |
462 | WHERE ( extends_entity_column_value REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', array_keys($roleIds)) . "[[:>:]]' | |
463 | OR extends_entity_column_value IS NULL ) | |
464 | AND extends_entity_column_id = '{$this->_roleCustomDataTypeID}' | |
465 | AND extends = 'Participant' | |
466 | AND is_active = 1"; | |
467 | ||
468 | $dao = CRM_Core_DAO::executeQuery($query); | |
469 | while ($dao->fetch()) { | |
470 | if ($dao->value) { | |
471 | $getRole = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->value); | |
472 | foreach ($getRole as $r) { | |
473 | if (!$r) { | |
474 | continue; | |
475 | } | |
476 | if (isset($this->_participantRoleIds[$r])) { | |
477 | $this->_participantRoleIds[$r] .= ',' . $dao->name; | |
478 | } | |
479 | else { | |
480 | $this->_participantRoleIds[$r] = $dao->name; | |
481 | } | |
482 | } | |
483 | } | |
484 | else { | |
485 | if (isset($this->_participantRoleIds[0])) { | |
486 | $this->_participantRoleIds[0] .= ',' . $dao->name; | |
487 | } | |
488 | else { | |
489 | $this->_participantRoleIds[0] = $dao->name; | |
490 | } | |
491 | } | |
492 | } | |
493 | $dao->free(); | |
494 | } | |
495 | foreach ($roleIds as $k => $v) { | |
496 | if (!isset($this->_participantRoleIds[$k])) { | |
497 | $this->_participantRoleIds[$k] = ''; | |
498 | } | |
499 | } | |
500 | $this->assign('participantRoleIds', $this->_participantRoleIds); | |
501 | } | |
502 | ||
503 | /** | |
504 | * This function sets the default values for the form in edit/view mode | |
505 | * the default values are retrieved from the database | |
506 | * | |
507 | * @access public | |
508 | * | |
355ba699 | 509 | * @return void |
6a488035 TO |
510 | */ |
511 | public function setDefaultValues() { | |
512 | if ($this->_showFeeBlock) { | |
513 | return CRM_Event_Form_EventFees::setDefaultValues($this); | |
514 | } | |
515 | ||
516 | if ($this->_cdType) { | |
517 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
518 | } | |
519 | ||
520 | $defaults = array(); | |
521 | ||
522 | if ($this->_action & CRM_Core_Action::DELETE) { | |
523 | return $defaults; | |
524 | } | |
525 | ||
526 | if ($this->_id) { | |
527 | $ids = array(); | |
528 | $params = array('id' => $this->_id); | |
529 | ||
530 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
531 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
532 | if ($defaults[$this->_id]['role_id']) { | |
533 | foreach (explode($sep, $defaults[$this->_id]['role_id']) as $k => $v) { | |
534 | $defaults[$this->_id]["role_id[{$v}]"] = 1; | |
535 | } | |
536 | unset($defaults[$this->_id]['role_id']); | |
537 | } | |
538 | $this->_contactId = $defaults[$this->_id]['contact_id']; | |
539 | $this->_statusId = $defaults[$this->_id]['participant_status_id']; | |
540 | ||
541 | //set defaults for note | |
542 | $noteDetails = CRM_Core_BAO_Note::getNote($this->_id, 'civicrm_participant'); | |
543 | $defaults[$this->_id]['note'] = array_pop($noteDetails); | |
544 | ||
545 | // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859) | |
546 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
547 | $this->assign('additionalParticipants', CRM_Event_BAO_Participant::getAdditionalParticipants($this->_id)); | |
548 | } | |
549 | ||
550 | // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859) | |
a7488080 | 551 | if (!empty($defaults[$this->_id]['participant_registered_by_id'])) { |
6a488035 TO |
552 | $registered_by_contact_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
553 | $defaults[$this->_id]['participant_registered_by_id'], | |
554 | 'contact_id', 'id' | |
555 | ); | |
556 | $this->assign('participant_registered_by_id', $defaults[$this->_id]['participant_registered_by_id']); | |
557 | $this->assign('registered_by_contact_id', $registered_by_contact_id); | |
558 | $this->assign('registered_by_display_name', CRM_Contact_BAO_Contact::displayName($registered_by_contact_id)); | |
559 | } | |
560 | } | |
561 | ||
562 | if ($this->_action & (CRM_Core_Action::VIEW | CRM_Core_Action::BROWSE)) { | |
563 | $inactiveNeeded = TRUE; | |
564 | $viewMode = TRUE; | |
565 | } | |
566 | else { | |
567 | $viewMode = FALSE; | |
568 | $inactiveNeeded = FALSE; | |
569 | } | |
570 | ||
571 | //setting default register date | |
572 | if ($this->_action == CRM_Core_Action::ADD) { | |
573 | $statuses = array_flip($this->_participantStatuses); | |
574 | $defaults[$this->_id]['status_id'] = CRM_Utils_Array::value(ts('Registered'), $statuses); | |
a7488080 | 575 | if (!empty($defaults[$this->_id]['event_id'])) { |
6a488035 TO |
576 | $contributionTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', |
577 | $defaults[$this->_id]['event_id'], | |
d96cf288 DG |
578 | 'financial_type_id' |
579 | ); | |
6a488035 | 580 | if ($contributionTypeId) { |
d96cf288 | 581 | $defaults[$this->_id]['financial_type_id'] = $contributionTypeId; |
6a488035 TO |
582 | } |
583 | } | |
584 | ||
585 | if ($this->_mode) { | |
586 | $fields["email-{$this->_bltID}"] = 1; | |
587 | $fields['email-Primary'] = 1; | |
588 | ||
589 | if ($this->_contactId) { | |
590 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $fields, $defaults); | |
591 | } | |
592 | ||
593 | if (empty($defaults["email-{$this->_bltID}"]) && | |
594 | !empty($defaults['email-Primary']) | |
595 | ) { | |
596 | $defaults[$this->_id]["email-{$this->_bltID}"] = $defaults['email-Primary']; | |
597 | } | |
598 | } | |
599 | ||
600 | $submittedRole = $this->getElementValue('role_id'); | |
a7488080 | 601 | if (!empty($submittedRole[0])) { |
6a488035 TO |
602 | $roleID = $submittedRole[0]; |
603 | } | |
604 | $submittedEvent = $this->getElementValue('event_id'); | |
605 | if ($submittedEvent[0]) { | |
606 | $eventID = $submittedEvent[0]; | |
607 | } | |
608 | } | |
609 | else { | |
610 | $defaults[$this->_id]['record_contribution'] = 0; | |
611 | ||
612 | if ($defaults[$this->_id]['participant_is_pay_later']) { | |
613 | $this->assign('participant_is_pay_later', TRUE); | |
614 | } | |
615 | ||
616 | $this->assign('participant_status_id', $defaults[$this->_id]['participant_status_id']); | |
617 | $roleID = $defaults[$this->_id]['participant_role_id']; | |
618 | $eventID = $defaults[$this->_id]['event_id']; | |
619 | ||
620 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
621 | ||
622 | $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); | |
623 | if ($this->_discountId) { | |
624 | $this->set('discountId', $this->_discountId); | |
625 | } | |
626 | } | |
627 | ||
628 | list($defaults[$this->_id]['register_date'], | |
629 | $defaults[$this->_id]['register_date_time'] | |
630 | ) = CRM_Utils_Date::setDateDefaults( | |
631 | CRM_Utils_Array::value('register_date', $defaults[$this->_id]), 'activityDateTime' | |
632 | ); | |
633 | ||
634 | //assign event and role id, this is needed for Custom data building | |
635 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
a7488080 | 636 | if (!empty($defaults[$this->_id]['participant_role_id'])) { |
6a488035 TO |
637 | $roleIDs = explode($sep, $defaults[$this->_id]['participant_role_id']); |
638 | } | |
639 | if (isset($roleIDs)) { | |
640 | $this->assign('roleID', $roleIDs); | |
641 | } | |
642 | if (isset($_POST['event_id'])) { | |
643 | $eventID = $_POST['event_id']; | |
644 | if ($eventID) { | |
645 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
646 | } | |
647 | } | |
648 | ||
649 | if (isset($eventID)) { | |
650 | $this->assign('eventID', $eventID); | |
651 | $this->set('eventId', $eventID); | |
652 | } | |
653 | ||
654 | if (isset($this->_eventTypeId)) { | |
655 | $this->assign('eventTypeID', $this->_eventTypeId); | |
656 | } | |
657 | ||
658 | $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults[$this->_id])); | |
659 | return $defaults[$this->_id]; | |
660 | } | |
661 | ||
662 | /** | |
663 | * Function to build the form | |
664 | * | |
355ba699 | 665 | * @return void |
6a488035 TO |
666 | * @access public |
667 | */ | |
668 | public function buildQuickForm() { | |
bc2eeabb | 669 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); |
e8cf3013 PJ |
670 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
671 | $partiallyPaidStatusId = array_search('Partially paid', $participantStatuses); | |
672 | CRM_Core_Resources::singleton()->addSetting(array('partiallyPaidStatusId' => $partiallyPaidStatusId)); | |
673 | ||
6a488035 TO |
674 | if ($this->_showFeeBlock) { |
675 | return CRM_Event_Form_EventFees::buildQuickForm($this); | |
676 | } | |
677 | ||
678 | if ($this->_cdType) { | |
679 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
680 | } | |
681 | ||
682 | //need to assign custom data type to the template | |
683 | $this->assign('customDataType', 'Participant'); | |
684 | ||
685 | $this->applyFilter('__ALL__', 'trim'); | |
686 | ||
687 | if ($this->_action & CRM_Core_Action::DELETE) { | |
688 | if ($this->_single) { | |
689 | $additionalParticipant = count(CRM_Event_BAO_Event::buildCustomProfile($this->_id, | |
690 | NULL, | |
691 | $this->_contactId, | |
692 | FALSE, | |
693 | TRUE | |
694 | )) - 1; | |
695 | if ($additionalParticipant) { | |
696 | $deleteParticipants = array(1 => ts('Delete this participant record along with associated participant record(s).'), | |
697 | 2 => ts('Delete only this participant record.'), | |
698 | ); | |
699 | $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />'); | |
700 | $this->setDefaults(array('delete_participant' => 1)); | |
701 | $this->assign('additionalParticipant', $additionalParticipant); | |
702 | } | |
703 | } | |
704 | $this->addButtons(array( | |
705 | array( | |
706 | 'type' => 'next', | |
707 | 'name' => ts('Delete'), | |
708 | 'spacing' => ' ', | |
709 | 'isDefault' => TRUE, | |
710 | ), | |
711 | array( | |
712 | 'type' => 'cancel', | |
713 | 'name' => ts('Cancel'), | |
714 | ), | |
715 | ) | |
716 | ); | |
717 | return; | |
718 | } | |
719 | ||
720 | ||
721 | if ($this->_single) { | |
722 | $urlPath = 'civicrm/contact/view/participant'; | |
723 | $urlParams = "reset=1&cid={$this->_contactId}&context=participant"; | |
724 | if ($this->_context == 'standalone') { | |
725 | CRM_Contact_Form_NewContact::buildQuickForm($this); | |
726 | $urlParams = 'reset=1&context=standalone'; | |
727 | $urlPath = 'civicrm/participant/add'; | |
728 | } | |
729 | ||
730 | if ($this->_id) { | |
731 | $urlParams .= "&action=update&id={$this->_id}"; | |
732 | } | |
733 | else { | |
734 | $urlParams .= "&action=add"; | |
735 | } | |
736 | ||
737 | if ($this->_mode) { | |
738 | $urlParams .= "&mode={$this->_mode}"; | |
739 | } | |
740 | ||
741 | $url = CRM_Utils_System::url($urlPath, $urlParams, | |
742 | FALSE, NULL, FALSE | |
743 | ); | |
744 | } | |
745 | else { | |
746 | $currentPath = CRM_Utils_System::currentPath(); | |
747 | ||
748 | $url = CRM_Utils_System::url($currentPath, '_qf_Participant_display=true', | |
749 | FALSE, NULL, FALSE | |
750 | ); | |
751 | } | |
752 | ||
753 | $this->assign('refreshURL', $url); | |
754 | ||
755 | $this->add('hidden', 'past_event'); | |
756 | ||
757 | $events = array(); | |
758 | if ( $this->_eID ) { | |
759 | $eventEndDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eID,'end_date'); | |
760 | } | |
c2be40dc | 761 | $this->assign('past', 0); |
6a488035 | 762 | if ($this->_action & CRM_Core_Action::UPDATE) { |
c2be40dc | 763 | $events = CRM_Event_BAO_Event::getEvents(1, FALSE, FALSE); |
6a488035 TO |
764 | } |
765 | elseif ($this->getElementValue('past_event') || (isset($eventEndDate) && (CRM_Utils_Date::currentDBDate() > CRM_Utils_Date::processDate($eventEndDate)))) { | |
c2be40dc AH |
766 | $pastval = $this->getElementValue('past_event'); |
767 | $events = CRM_Event_BAO_Event::getEvents($pastval); | |
768 | $this->assign('past', $pastval); | |
6a488035 TO |
769 | } |
770 | else { | |
771 | $events = CRM_Event_BAO_Event::getEvents(); | |
772 | } | |
773 | ||
774 | if ($this->_mode) { | |
775 | //unset the event which are not monetary when credit card | |
776 | //event registration is used | |
777 | foreach ($events as $key => $val) { | |
778 | $isPaid = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $key, 'is_monetary'); | |
779 | if (!$isPaid) { | |
780 | unset($events[$key]); | |
781 | } | |
782 | } | |
783 | $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, TRUE); | |
784 | } | |
785 | ||
786 | // build array(event -> eventType) mapper | |
787 | $query = " | |
788 | SELECT civicrm_event.id as id, civicrm_event.event_type_id as event_type_id | |
789 | FROM civicrm_event | |
790 | WHERE civicrm_event.is_template IS NULL OR civicrm_event.is_template = 0"; | |
791 | $dao = CRM_Core_DAO::executeQuery($query); | |
792 | $eventAndTypeMapping = array(); | |
793 | while ($dao->fetch()) { | |
794 | $eventAndTypeMapping[$dao->id] = $dao->event_type_id; | |
795 | } | |
796 | $eventAndTypeMapping = json_encode($eventAndTypeMapping); | |
797 | // building of mapping ends -- | |
798 | ||
799 | //inherit the campaign from event. | |
800 | $eventCampaigns = array(); | |
801 | $allEventIds = array_keys($events); | |
802 | if (!empty($allEventIds)) { | |
803 | CRM_Core_PseudoConstant::populate($eventCampaigns, | |
804 | 'CRM_Event_DAO_Event', | |
805 | TRUE, 'campaign_id' | |
806 | ); | |
807 | } | |
808 | $eventCampaigns = json_encode($eventCampaigns); | |
809 | ||
810 | $element = $this->add('select', 'event_id', ts('Event'), | |
811 | array( | |
812 | '' => ts('- select -')) + $events, | |
813 | TRUE, | |
814 | array('onchange' => "buildFeeBlock( this.value ); CRM.buildCustomData( 'Participant', this.value, {$this->_eventNameCustomDataTypeID} ); buildParticipantRole( this.value ); buildEventTypeCustomData( this.value, {$this->_eventTypeCustomDataTypeID}, '{$eventAndTypeMapping}' ); loadCampaign( this.value, {$eventCampaigns} );", 'class' => 'huge') | |
815 | ); | |
816 | ||
817 | // CRM-6111 | |
818 | // note that embedding JS within PHP files is quite awful, IMO | |
819 | // but we do the same for the onChange element and this form is complex | |
820 | // and i did not want to break it late in the 3.2 cycle | |
821 | $preloadJSSnippet = NULL; | |
a7488080 | 822 | if (!empty($_GET['reset']) && $this->_eID) { |
6a488035 TO |
823 | $preloadJSSnippet = " |
824 | cj(function() { | |
825 | cj('#event_id').val( '{$this->_eID}' ); | |
826 | buildFeeBlock( {$this->_eID} ); | |
827 | CRM.buildCustomData( 'Participant', {$this->_eID}, {$this->_eventNameCustomDataTypeID} ); | |
828 | buildEventTypeCustomData( {$this->_eID}, {$this->_eventTypeCustomDataTypeID}, '{$eventAndTypeMapping}' ); | |
829 | loadCampaign( {$this->_eID}, {$eventCampaigns} ); | |
830 | }); | |
831 | "; | |
832 | } | |
833 | ||
834 | $this->assign('preloadJSSnippet', $preloadJSSnippet); | |
835 | ||
836 | ||
837 | //frozen the field fix for CRM-4171 | |
838 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
839 | if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
840 | $this->_id, 'contribution_id', 'participant_id' | |
841 | )) { | |
842 | $element->freeze(); | |
843 | } | |
844 | } | |
845 | ||
846 | //CRM-7362 --add campaigns. | |
847 | $campaignId = NULL; | |
848 | if ($this->_id) { | |
849 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'campaign_id'); | |
850 | } | |
851 | if (!$campaignId) { | |
852 | $eventId = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
853 | if ($eventId) { | |
854 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'campaign_id'); | |
855 | } | |
856 | } | |
857 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
858 | ||
859 | $this->addDateTime('register_date', ts('Registration Date'), TRUE, array('formatType' => 'activityDateTime')); | |
860 | ||
861 | if ($this->_id) { | |
862 | $this->assign('entityID', $this->_id); | |
863 | } | |
864 | ||
865 | $roleids = CRM_Event_PseudoConstant::participantRole(); | |
866 | ||
867 | foreach ($roleids as $rolekey => $rolevalue) { | |
868 | $roleTypes[] = $this->createElement('checkbox', $rolekey, NULL, $rolevalue, | |
869 | array('onclick' => "showCustomData( 'Participant', {$rolekey}, {$this->_roleCustomDataTypeID} );") | |
870 | ); | |
871 | } | |
872 | ||
873 | $this->addGroup($roleTypes, 'role_id', ts('Participant Role')); | |
874 | $this->addRule('role_id', ts('Role is required'), 'required'); | |
875 | ||
876 | // CRM-4395 | |
877 | $checkCancelledJs = array('onchange' => "return sendNotification( );"); | |
878 | $confirmJS = NULL; | |
879 | if ($this->_onlinePendingContributionId) { | |
880 | $cancelledparticipantStatusId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus()); | |
881 | $cancelledContributionStatusId = array_search('Cancelled', | |
882 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
883 | ); | |
884 | $checkCancelledJs = array( | |
885 | 'onchange' => | |
886 | "checkCancelled( this.value, {$cancelledparticipantStatusId},{$cancelledContributionStatusId});", | |
887 | ); | |
888 | ||
889 | $participantStatusId = array_search('Pending from pay later', | |
890 | CRM_Event_PseudoConstant::participantStatus() | |
891 | ); | |
892 | $contributionStatusId = array_search('Completed', | |
893 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
894 | ); | |
895 | $confirmJS = array('onclick' => "return confirmStatus( {$participantStatusId}, {$contributionStatusId} );"); | |
896 | } | |
897 | ||
453ed6d1 KJ |
898 | // get the participant status names to build special status array which is used to show notification |
899 | // checkbox below participant status select | |
900 | $participantStatusName = CRM_Event_PseudoConstant::participantStatus(); | |
901 | $notificationStatuses = array( | |
902 | 'Cancelled', | |
903 | 'Pending from waitlist', | |
904 | 'Pending from approval', | |
905 | 'Expired', | |
906 | ); | |
907 | ||
908 | // get the required status and then implode only ids | |
909 | $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); | |
910 | $this->assign('notificationStatusIds', $notificationStatusIds); | |
911 | ||
6a488035 | 912 | $this->_participantStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); |
0f352c3a | 913 | $this->addSelect('status_id', $checkCancelledJs, TRUE); |
6a488035 TO |
914 | |
915 | $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); | |
916 | ||
917 | $this->add('text', 'source', ts('Event Source')); | |
918 | $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
919 | $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']); | |
920 | ||
921 | $buttons[] = array( | |
922 | 'type' => 'upload', | |
923 | 'name' => ts('Save'), | |
924 | 'isDefault' => TRUE, | |
925 | 'js' => $confirmJS, | |
926 | ); | |
927 | ||
928 | $path = CRM_Utils_System::currentPath(); | |
239a5027 PJ |
929 | $excludeForPaths = array( |
930 | 'civicrm/contact/search', | |
931 | 'civicrm/group/search' | |
932 | ); | |
933 | if (!in_array($path, $excludeForPaths)) { | |
6a488035 TO |
934 | $buttons[] = array( |
935 | 'type' => 'upload', | |
936 | 'name' => ts('Save and New'), | |
937 | 'subName' => 'new', | |
938 | 'js' => $confirmJS, | |
939 | ); | |
940 | } | |
239a5027 | 941 | |
6a488035 TO |
942 | $buttons[] = array( |
943 | 'type' => 'cancel', | |
944 | 'name' => ts('Cancel'), | |
945 | ); | |
946 | ||
947 | $this->addButtons($buttons); | |
948 | if ($this->_action == CRM_Core_Action::VIEW) { | |
949 | $this->freeze(); | |
950 | } | |
951 | } | |
952 | ||
953 | /** | |
954 | * Add local and global form rules | |
955 | * | |
956 | * @access protected | |
957 | * | |
958 | * @return void | |
959 | */ | |
960 | function addRules() { | |
961 | $this->addFormRule(array('CRM_Event_Form_Participant', 'formRule'), $this); | |
962 | } | |
963 | ||
964 | /** | |
965 | * global validation rules for the form | |
966 | * | |
967 | * @param array $fields posted values of the form | |
968 | * | |
969 | * @return array list of errors to be posted back to the form | |
970 | * @static | |
971 | * @access public | |
972 | */ | |
973 | static function formRule($values, $files, $self) { | |
974 | // If $values['_qf_Participant_next'] is Delete or | |
975 | // $values['event_id'] is empty, then return | |
976 | // instead of proceeding further. | |
977 | ||
978 | if ((CRM_Utils_Array::value('_qf_Participant_next', $values) == 'Delete') || | |
979 | (!$values['event_id']) | |
980 | ) { | |
981 | return TRUE; | |
982 | } | |
983 | ||
984 | $errorMsg = array(); | |
985 | //check if contact is selected in standalone mode | |
986 | if (isset($values['contact_select_id'][1]) && !$values['contact_select_id'][1]) { | |
987 | $errorMsg['contact[1]'] = ts('Please select a contact or create new contact'); | |
988 | } | |
989 | ||
a7488080 | 990 | if (!empty($values['payment_processor_id'])) { |
6a488035 | 991 | // make sure that credit card number and cvv are valid |
7cb3d4f0 | 992 | CRM_Core_Payment_Form::validateCreditCard($values, $errorMsg); |
6a488035 TO |
993 | } |
994 | ||
a7488080 CW |
995 | if (!empty($values['record_contribution'])) { |
996 | if (empty($values['financial_type_id'])) { | |
d96cf288 DG |
997 | $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); |
998 | } | |
a7488080 | 999 | if (empty($values['payment_instrument_id'])) { |
d96cf288 DG |
1000 | $errorMsg['payment_instrument_id'] = ts('Paid By is a required field.'); |
1001 | } | |
6a488035 TO |
1002 | } |
1003 | ||
1004 | // validate contribution status for 'Failed'. | |
8cc574cf | 1005 | if ($self->_onlinePendingContributionId && !empty($values['record_contribution']) && |
6a488035 TO |
1006 | (CRM_Utils_Array::value('contribution_status_id', $values) == |
1007 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
1008 | ) | |
1009 | ) { | |
1010 | $errorMsg['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
1011 | } | |
1012 | ||
1013 | // do the amount validations. | |
1014 | //skip for update mode since amount is freeze, CRM-6052 | |
8cc574cf | 1015 | if ((!$self->_id && empty($values['total_amount']) && |
6a488035 TO |
1016 | empty($self->_values['line_items']) |
1017 | ) || | |
1018 | ($self->_id && !$self->_paymentId && isset($self->_values['line_items']) && is_array($self->_values['line_items'])) | |
1019 | ) { | |
1020 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) { | |
9da8dc8c | 1021 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE); |
6a488035 TO |
1022 | } |
1023 | } | |
1024 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; | |
1025 | } | |
1026 | ||
1027 | /** | |
1028 | * Function to process the form | |
1029 | * | |
1030 | * @access public | |
1031 | */ | |
1032 | public function postProcess() { | |
1033 | // get the submitted form values. | |
1034 | $params = $this->controller->exportValues($this->_name); | |
1035 | ||
1036 | if ($this->_action & CRM_Core_Action::DELETE) { | |
1037 | if (CRM_Utils_Array::value('delete_participant', $params) == 2) { | |
1038 | $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id)); | |
1039 | $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId)); | |
1040 | } | |
1041 | if (CRM_Utils_Array::value('delete_participant', $params) == 1) { | |
1042 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
1043 | foreach ($additionalIds as $value) { | |
1044 | CRM_Event_BAO_Participant::deleteParticipant($value); | |
1045 | } | |
1046 | } | |
1047 | CRM_Event_BAO_Participant::deleteParticipant($this->_id); | |
1048 | CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success'); | |
1049 | if (!empty($participantLinks)) { | |
1050 | $status = ts('The following participants no longer have an event fee recorded. You can edit their registration and record a replacement contribution by clicking the links below:') . '<br>' . $participantLinks; | |
1051 | CRM_Core_Session::setStatus($status, ts('Group Payment Deleted')); | |
1052 | } | |
1053 | return; | |
1054 | } | |
1055 | ||
aac57c29 | 1056 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 1057 | // set the contact, when contact is selected |
a7488080 | 1058 | if (!empty($params['contact_select_id'])) { |
6a488035 TO |
1059 | $this->_contactId = $params['contact_select_id'][1]; |
1060 | } | |
9da8dc8c | 1061 | if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
1062 | $this->_quickConfig = $isQuickConfig; |
1063 | } | |
1064 | ||
1065 | if ($this->_id) { | |
1066 | $params['id'] = $this->_id; | |
1067 | } | |
1068 | ||
1069 | $config = CRM_Core_Config::singleton(); | |
1070 | if ($this->_isPaidEvent) { | |
1071 | ||
1072 | $contributionParams = array(); | |
1073 | $lineItem = array(); | |
1074 | $additionalParticipantDetails = array(); | |
1075 | if (($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId) { | |
1076 | $participantBAO = new CRM_Event_BAO_Participant(); | |
1077 | $participantBAO->id = $this->_id; | |
1078 | $participantBAO->find(TRUE); | |
1079 | $contributionParams['total_amount'] = $participantBAO->fee_amount; | |
1080 | ||
1081 | $params['discount_id'] = NULL; | |
1082 | //re-enter the values for UPDATE mode | |
1083 | $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level; | |
1084 | $params['fee_amount'] = $participantBAO->fee_amount; | |
1085 | if (isset($params['priceSetId'])) { | |
1086 | $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id); | |
1087 | } | |
1088 | //also add additional participant's fee level/priceset | |
1089 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
1090 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
1091 | $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE); | |
1092 | $additionalParticipantDetails = CRM_Event_BAO_Participant::getFeeDetails($additionalIds, | |
1093 | $hasLineItems | |
1094 | ); | |
1095 | } | |
1096 | } | |
1097 | else { | |
1098 | ||
1099 | //check if discount is selected | |
a7488080 | 1100 | if (!empty($params['discount_id'])) { |
6a488035 TO |
1101 | $discountId = $params['discount_id']; |
1102 | } | |
1103 | else { | |
1104 | $discountId = $params['discount_id'] = 'null'; | |
1105 | } | |
1106 | ||
1107 | //lets carry currency, CRM-4453 | |
1108 | $params['fee_currency'] = $config->defaultCurrency; | |
9da8dc8c | 1109 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], |
8567d0f8 PN |
1110 | $params, $lineItem[0] |
1111 | ); | |
6a488035 TO |
1112 | //CRM-11529 for quick config backoffice transactions |
1113 | //when financial_type_id is passed in form, update the | |
1114 | //lineitems with the financial type selected in form | |
1115 | $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $params); | |
1116 | $isPaymentRecorded = CRM_Utils_Array::value('record_contribution', $params); | |
1117 | if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) { | |
1118 | foreach ($lineItem[0] as &$values) { | |
1119 | $values['financial_type_id'] = $submittedFinancialType; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | $params['fee_level'] = $params['amount_level']; | |
1124 | $contributionParams['total_amount'] = $params['amount']; | |
8cc574cf | 1125 | if ($this->_quickConfig && !empty($params['total_amount']) && $params['status_id'] != array_search('Partially paid', $participantStatus)) { |
6a488035 TO |
1126 | $params['fee_amount'] = $params['total_amount']; |
1127 | } else { | |
1128 | //fix for CRM-3086 | |
1129 | $params['fee_amount'] = $params['amount']; | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | if (isset($params['priceSetId'])) { | |
1134 | if (!empty($lineItem[0])) { | |
1135 | $this->set('lineItem', $lineItem); | |
1136 | ||
1137 | $this->_lineItem = $lineItem; | |
1138 | $lineItem = array_merge($lineItem, $additionalParticipantDetails); | |
1139 | ||
1140 | $participantCount = array(); | |
1141 | foreach ($lineItem as $k) { | |
1142 | foreach ($k as $v) { | |
1143 | if (CRM_Utils_Array::value('participant_count', $v) > 0) { | |
1144 | $participantCount[] = $v['participant_count']; | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | if (isset($participantCount)) { | |
1150 | $this->assign('pricesetFieldsCount', $participantCount); | |
1151 | } | |
1152 | $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig? FALSE : $lineItem); | |
1153 | } | |
1154 | else { | |
1155 | $this->assign('amount_level', $params['amount_level']); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | $this->_params = $params; | |
fbc54416 PJ |
1160 | $amountOwed = NULL; |
1161 | if (isset($params['amount'])) { | |
1162 | $amountOwed = $params['amount']; | |
1163 | unset($params['amount']); | |
1164 | } | |
6a488035 TO |
1165 | $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']); |
1166 | $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params)); | |
1167 | $params['contact_id'] = $this->_contactId; | |
1168 | ||
1169 | // overwrite actual payment amount if entered | |
a7488080 | 1170 | if (!empty($params['total_amount'])) { |
6a488035 TO |
1171 | $contributionParams['total_amount'] = CRM_Utils_Array::value('total_amount', $params); |
1172 | } | |
1173 | ||
1174 | ||
1175 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email | |
1176 | $session = CRM_Core_Session::singleton(); | |
1177 | $userID = $session->get('userID'); | |
1178 | list($userName, | |
1179 | $userEmail | |
1180 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
1181 | ||
1182 | if ($this->_contactId) { | |
1183 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); | |
1184 | } | |
1185 | ||
1186 | if ($this->_mode) { | |
1187 | if (!$this->_isPaidEvent) { | |
1188 | CRM_Core_Error::fatal(ts('Selected Event is not Paid Event ')); | |
1189 | } | |
1190 | //modify params according to parameter used in create | |
1191 | //participant method (addParticipant) | |
1192 | $this->_params['participant_status_id'] = $params['status_id']; | |
1193 | $this->_params['participant_role_id'] = $params['role_id']; | |
1194 | $this->_params['participant_register_date'] = $params['register_date']; | |
1195 | ||
1196 | $eventTitle = | |
1197 | CRM_Core_DAO::getFieldValue( | |
1198 | 'CRM_Event_DAO_Event', | |
1199 | $params['event_id'], | |
1200 | 'title' | |
1201 | ); | |
1202 | ||
1203 | // set source if not set | |
1204 | if (empty($params['source'])) { | |
03e04002 | 1205 | $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', array(1 => $userName, 2 => $eventTitle)); |
6a488035 TO |
1206 | } |
1207 | else { | |
03e04002 | 1208 | $this->_params['participant_source'] = $params['source']; |
6a488035 TO |
1209 | } |
1210 | $this->_params['description'] = $this->_params['participant_source']; | |
1211 | ||
1212 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1213 | $this->_mode | |
1214 | ); | |
1215 | $now = date('YmdHis'); | |
1216 | $fields = array(); | |
1217 | ||
1218 | // set email for primary location. | |
1219 | $fields['email-Primary'] = 1; | |
1220 | $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail; | |
1221 | ||
1222 | $params['register_date'] = $now; | |
1223 | ||
1224 | // now set the values for the billing location. | |
1225 | foreach ($this->_fields as $name => $dontCare) { | |
1226 | $fields[$name] = 1; | |
1227 | } | |
1228 | ||
1229 | // also add location name to the array | |
1230 | $params["address_name-{$this->_bltID}"] = | |
1231 | CRM_Utils_Array::value('billing_first_name', $params) . ' ' . | |
1232 | CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . | |
1233 | CRM_Utils_Array::value('billing_last_name', $params); | |
1234 | ||
1235 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1236 | $fields["address_name-{$this->_bltID}"] = 1; | |
1237 | $fields["email-{$this->_bltID}"] = 1; | |
1238 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); | |
1239 | ||
1240 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1241 | ||
1242 | foreach ($nameFields as $name) { | |
1243 | $fields[$name] = 1; | |
1244 | if (array_key_exists("billing_$name", $params)) { | |
1245 | $params[$name] = $params["billing_{$name}"]; | |
1246 | $params['preserveDBName'] = TRUE; | |
1247 | } | |
1248 | } | |
1249 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); | |
1250 | } | |
1251 | ||
1252 | $roleAllIds = CRM_Utils_Array::value('role_id', $params); | |
1253 | if ($roleAllIds) { | |
1254 | foreach ($roleAllIds as $rkey => $rvalue) { | |
1255 | $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $rkey, $this->_roleCustomDataTypeID); | |
1256 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', | |
1257 | FALSE, | |
1258 | FALSE, | |
1259 | CRM_Utils_Array::value('event_id', $params), | |
1260 | $this->_eventNameCustomDataTypeID | |
1261 | ); | |
1262 | $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', | |
1263 | FALSE, | |
1264 | FALSE, | |
1265 | $this->_eventTypeId, | |
1266 | $this->_eventTypeCustomDataTypeID | |
1267 | ); | |
1268 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
1269 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
1270 | ); | |
1271 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
1272 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields); | |
1273 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
1274 | $customFields, | |
1275 | $this->_id, | |
1276 | 'Participant' | |
1277 | ); | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | //do cleanup line items if participant edit the Event Fee. | |
1282 | if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { | |
1283 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); | |
1284 | } | |
03e04002 | 1285 | |
6a488035 TO |
1286 | if ($this->_mode) { |
1287 | // add all the additioanl payment params we need | |
1288 | $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($this->_params["billing_state_province_id-{$this->_bltID}"]); | |
1289 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1290 | ||
1291 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
1292 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
1293 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
1294 | $this->_params['amount'] = $params['fee_amount']; | |
1295 | $this->_params['amount_level'] = $params['amount_level']; | |
1296 | $this->_params['currencyID'] = $config->defaultCurrency; | |
1297 | $this->_params['payment_action'] = 'Sale'; | |
1298 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1299 | ||
1300 | // at this point we've created a contact and stored its address etc | |
1301 | // all the payment processors expect the name and address to be in the | |
1302 | // so we copy stuff over to first_name etc. | |
1303 | $paymentParams = $this->_params; | |
a7488080 | 1304 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1305 | $paymentParams['email'] = $this->_contributorEmail; |
1306 | } | |
1307 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
1308 | ||
1309 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
1310 | ||
1311 | $result = &$payment->doDirectPayment($paymentParams); | |
1312 | ||
1313 | if (is_a($result, 'CRM_Core_Error')) { | |
1314 | CRM_Core_Error::displaySessionError($result); | |
1315 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1316 | "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}" | |
1317 | )); | |
1318 | } | |
1319 | ||
1320 | if ($result) { | |
1321 | $this->_params = array_merge($this->_params, $result); | |
1322 | } | |
1323 | ||
1324 | $this->_params['receive_date'] = $now; | |
1325 | ||
a7488080 | 1326 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1327 | $this->_params['receipt_date'] = $now; |
1328 | } | |
1329 | else { | |
1330 | $this->_params['receipt_date'] = NULL; | |
1331 | } | |
1332 | ||
1333 | $this->set('params', $this->_params); | |
1334 | $this->assign('trxn_id', $result['trxn_id']); | |
1335 | $this->assign('receive_date', | |
1336 | CRM_Utils_Date::processDate($this->_params['receive_date']) | |
1337 | ); | |
1338 | ||
1339 | //add contribution record | |
1340 | $this->_params['financial_type_id'] = | |
1341 | CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id'); | |
1342 | $this->_params['mode'] = $this->_mode; | |
1343 | ||
1344 | //add contribution reocord | |
1345 | $contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution($this, $this->_params, $result, $contactID, FALSE); | |
1346 | ||
1347 | // add participant record | |
1348 | $participants = array(); | |
a7488080 | 1349 | if (!empty($this->_params['participant_role_id']) && is_array($this->_params['participant_role_id'])) { |
6a488035 TO |
1350 | $this->_params['participant_role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, |
1351 | array_keys($this->_params['participant_role_id']) | |
1352 | ); | |
1353 | } | |
1354 | $participants[] = CRM_Event_Form_Registration::addParticipant($this->_params, $contactID); | |
1355 | ||
1356 | //add custom data for participant | |
1357 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
1358 | CRM_Core_DAO::$_nullArray, | |
1359 | 'civicrm_participant', | |
1360 | $participants[0]->id, | |
1361 | 'Participant' | |
1362 | ); | |
1363 | //add participant payment | |
1364 | $paymentParticipant = array( | |
1365 | 'participant_id' => $participants[0]->id, | |
1366 | 'contribution_id' => $contribution->id, | |
1367 | ); | |
1368 | $ids = array(); | |
1369 | ||
1370 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids); | |
1371 | $this->_contactIds[] = $this->_contactId; | |
1372 | } | |
1373 | else { | |
1374 | $participants = array(); | |
1375 | if ($this->_single) { | |
1376 | if ($params['role_id']) { | |
1377 | foreach ($params['role_id'] as $k => $v) { | |
1378 | $rolesIDS[] = $k; | |
1379 | } | |
1380 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; | |
1381 | $params['role_id'] = implode($seperator, $rolesIDS); | |
1382 | } | |
1383 | else { | |
1384 | $params['role_id'] = 'NULL'; | |
1385 | } | |
1386 | $participants[] = CRM_Event_BAO_Participant::create($params); | |
1387 | } | |
1388 | else { | |
1389 | foreach ($this->_contactIds as $contactID) { | |
1390 | $commonParams = $params; | |
1391 | $commonParams['contact_id'] = $contactID; | |
1392 | if ($commonParams['role_id']) { | |
1393 | $rolesIDS = array(); | |
1394 | foreach ($commonParams['role_id'] as $k => $v) { | |
1395 | $rolesIDS[] = $k; | |
1396 | } | |
1397 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; | |
1398 | $commonParams['role_id'] = implode($seperator, $rolesIDS); | |
1399 | $commonParams['participant_role_id'] = implode($seperator, $rolesIDS); | |
1400 | } | |
1401 | else { | |
1402 | $commonParams['role_id'] = 'NULL'; | |
1403 | } | |
1404 | $participants[] = CRM_Event_BAO_Participant::create($commonParams); | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | if (isset($params['event_id'])) { | |
1409 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', | |
1410 | $params['event_id'], | |
1411 | 'title' | |
1412 | ); | |
1413 | } | |
1414 | ||
1415 | if ($this->_single) { | |
1416 | $this->_contactIds[] = $this->_contactId; | |
1417 | } | |
1418 | ||
1419 | $contributions = array(); | |
a7488080 CW |
1420 | if (!empty($params['record_contribution'])) { |
1421 | if (!empty($params['id'])) { | |
6a488035 TO |
1422 | if ($this->_onlinePendingContributionId) { |
1423 | $ids['contribution'] = $this->_onlinePendingContributionId; | |
1424 | } | |
1425 | else { | |
1426 | $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
1427 | $params['id'], | |
1428 | 'contribution_id', | |
1429 | 'participant_id' | |
1430 | ); | |
1431 | } | |
1432 | } | |
1433 | unset($params['note']); | |
1434 | ||
1435 | //build contribution params | |
1436 | if (!$this->_onlinePendingContributionId) { | |
1437 | $contributionParams['source'] = "{$eventTitle}: Offline registration (by {$userName})"; | |
1438 | } | |
1439 | ||
1440 | $contributionParams['currency'] = $config->defaultCurrency; | |
1441 | $contributionParams['non_deductible_amount'] = 'null'; | |
0d8afee2 | 1442 | $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null'; |
6a488035 TO |
1443 | |
1444 | $recordContribution = array( 'contact_id', 'financial_type_id', | |
1445 | 'payment_instrument_id', 'trxn_id', | |
1446 | 'contribution_status_id', 'receive_date', | |
1447 | 'check_number', 'campaign_id', | |
1448 | ); | |
1449 | ||
1450 | foreach ($recordContribution as $f) { | |
1451 | $contributionParams[$f] = CRM_Utils_Array::value($f, $params); | |
1452 | if ($f == 'trxn_id') { | |
1453 | $this->assign('trxn_id', $contributionParams[$f]); | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | //insert financial type name in receipt. | |
1458 | $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id'])); | |
1459 | $contributionParams['skipLineItem'] = 1; | |
7030a002 PN |
1460 | if ($this->_id) { |
1461 | $contributionParams['contribution_mode'] = 'participant'; | |
1462 | $contributionParams['participant_id'] = $this->_id; | |
1463 | } | |
6a488035 TO |
1464 | // Set is_pay_later flag for back-office offline Pending status contributions |
1465 | if ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) { | |
1466 | $contributionParams['is_pay_later'] = 1; | |
1467 | } | |
f8325309 | 1468 | |
aac57c29 PJ |
1469 | if ($params['status_id'] == array_search('Partially paid', $participantStatus)) { |
1470 | // CRM-13964 partial_payment_total | |
1471 | if ($amountOwed > $params['total_amount']) { | |
1472 | // the owed amount | |
1473 | $contributionParams['partial_payment_total'] = $amountOwed; | |
1474 | // the actual amount paid | |
1475 | $contributionParams['partial_amount_pay'] = $params['total_amount']; | |
1476 | } | |
f8325309 | 1477 | } |
6a488035 | 1478 | if ($this->_single) { |
dbd48d4e | 1479 | if (empty($ids)) { |
1480 | $ids = array(); | |
1481 | } | |
6a488035 TO |
1482 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); |
1483 | } | |
1484 | else { | |
1485 | $ids = array(); | |
1486 | foreach ($this->_contactIds as $contactID) { | |
1487 | $contributionParams['contact_id'] = $contactID; | |
1488 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | //insert payment record for this participation | |
dbd48d4e | 1493 | if (empty($ids['contribution'])) { |
6a488035 TO |
1494 | foreach ($this->_contactIds as $num => $contactID) { |
1495 | $ppDAO = new CRM_Event_DAO_ParticipantPayment(); | |
1496 | $ppDAO->participant_id = $participants[$num]->id; | |
1497 | $ppDAO->contribution_id = $contributions[$num]->id; | |
1498 | $ppDAO->save(); | |
1499 | } | |
1500 | } | |
1501 | // next create the transaction record | |
1502 | $transaction = new CRM_Core_Transaction(); | |
1503 | ||
1504 | // CRM-11124 | |
1505 | if ($this->_quickConfig) { | |
a7488080 | 1506 | if (!empty($this->_params['amount_priceset_level_radio'])) { |
7030a002 PN |
1507 | $feeLevel = $this->_params['amount_priceset_level_radio']; |
1508 | } | |
1509 | else { | |
1510 | $feeLevel[] = $this->_params['fee_level'] ; | |
1511 | } | |
1512 | CRM_Event_BAO_Participant::createDiscountTrxn($this->_eventId, $contributionParams, $feeLevel); | |
6a488035 TO |
1513 | } |
1514 | $transaction->commit(); | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | // also store lineitem stuff here | |
1519 | if ((($this->_lineItem & $this->_action & CRM_Core_Action::ADD) || | |
1520 | ($this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId)) | |
1521 | ) { | |
1522 | foreach ($this->_contactIds as $num => $contactID) { | |
1523 | foreach ($this->_lineItem as $key => $value) { | |
1524 | if (is_array($value) && $value != 'skip') { | |
1525 | foreach ($value as $lineKey => $line) { | |
aac57c29 | 1526 | //10117 update the line items for participants if contribution amount is recorded |
8cc574cf | 1527 | if ($this->_quickConfig && !empty($params['total_amount']) && $params['status_id'] != array_search('Partially paid', $participantStatus) |
aac57c29 PJ |
1528 | ) { |
1529 | $line['unit_price'] = $line['line_total'] = $params['total_amount']; | |
1530 | } | |
6a488035 TO |
1531 | $lineItem[$this->_priceSetId][$lineKey] = $line; |
1532 | } | |
1533 | CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); | |
1534 | } | |
1535 | } | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | $updateStatusMsg = NULL; | |
1540 | //send mail when participant status changed, CRM-4326 | |
1541 | if ($this->_id && $this->_statusId && | |
8cc574cf | 1542 | $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify'])) { |
6a488035 TO |
1543 | |
1544 | $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, | |
1545 | $params['status_id'], | |
1546 | $this->_statusId | |
1547 | ); | |
1548 | } | |
1549 | ||
1550 | $sent = array(); | |
1551 | $notSent = array(); | |
a7488080 | 1552 | if (!empty($params['send_receipt'])) { |
6a488035 TO |
1553 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
1554 | $receiptFrom = $params['from_email_address']; | |
1555 | } | |
1556 | ||
1557 | $this->assign('module', 'Event Registration'); | |
1558 | //use of the message template below requires variables in different format | |
1559 | $event = $events = array(); | |
1560 | $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title'); | |
1561 | ||
1562 | //get all event details. | |
1563 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties); | |
1564 | $event = $events[$params['event_id']]; | |
1565 | unset($event['start_date']); | |
1566 | unset($event['end_date']); | |
1567 | ||
1568 | $role = CRM_Event_PseudoConstant::participantRole(); | |
1569 | $participantRoles = CRM_Utils_Array::value('role_id', $params); | |
1570 | if (is_array($participantRoles)) { | |
1571 | $selectedRoles = array(); | |
1572 | foreach (array_keys($participantRoles) as $roleId) { | |
1573 | $selectedRoles[] = $role[$roleId]; | |
1574 | } | |
1575 | $event['participant_role'] = implode(', ', $selectedRoles); | |
1576 | } | |
1577 | else { | |
1578 | $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role); | |
1579 | } | |
1580 | $event['is_monetary'] = $this->_isPaidEvent; | |
1581 | ||
1582 | if ($params['receipt_text']) { | |
1583 | $event['confirm_email_text'] = $params['receipt_text']; | |
1584 | } | |
1585 | ||
1586 | $this->assign('isAmountzero', 1); | |
1587 | $this->assign('event', $event); | |
1588 | ||
1589 | $this->assign('isShowLocation', $event['is_show_location']); | |
1590 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { | |
1591 | $locationParams = array( | |
1592 | 'entity_id' => $params['event_id'], | |
1593 | 'entity_table' => 'civicrm_event', | |
1594 | ); | |
1595 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
1596 | $this->assign('location', $location); | |
1597 | } | |
1598 | ||
1599 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1600 | if ($this->_isPaidEvent) { | |
1601 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1602 | if (!$this->_mode) { | |
1603 | if (isset($params['payment_instrument_id'])) { | |
1604 | $this->assign('paidBy', | |
1605 | CRM_Utils_Array::value($params['payment_instrument_id'], | |
1606 | $paymentInstrument | |
1607 | ) | |
1608 | ); | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | $this->assign('totalAmount', $contributionParams['total_amount']); | |
1613 | ||
1614 | $this->assign('isPrimary', 1); | |
1615 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); | |
1616 | } | |
1617 | if ($this->_mode) { | |
a7488080 | 1618 | if (!empty($params['billing_first_name'])) { |
6a488035 TO |
1619 | $name = $params['billing_first_name']; |
1620 | } | |
1621 | ||
a7488080 | 1622 | if (!empty($params['billing_middle_name'])) { |
6a488035 TO |
1623 | $name .= " {$params['billing_middle_name']}"; |
1624 | } | |
1625 | ||
a7488080 | 1626 | if (!empty($params['billing_last_name'])) { |
6a488035 TO |
1627 | $name .= " {$params['billing_last_name']}"; |
1628 | } | |
1629 | $this->assign('billingName', $name); | |
1630 | ||
1631 | // assign the address formatted up for display | |
1632 | $addressParts = array( | |
1633 | "street_address-{$this->_bltID}", | |
1634 | "city-{$this->_bltID}", | |
1635 | "postal_code-{$this->_bltID}", | |
1636 | "state_province-{$this->_bltID}", | |
1637 | "country-{$this->_bltID}", | |
1638 | ); | |
1639 | $addressFields = array(); | |
1640 | foreach ($addressParts as $part) { | |
1641 | list($n, $id) = explode('-', $part); | |
1642 | if (isset($this->_params['billing_' . $part])) { | |
1643 | $addressFields[$n] = $this->_params['billing_' . $part]; | |
1644 | } | |
1645 | } | |
1646 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
1647 | ||
1648 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); | |
1649 | $date = CRM_Utils_Date::mysqlToIso($date); | |
1650 | $this->assign('credit_card_exp_date', $date); | |
1651 | $this->assign('credit_card_number', | |
1652 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) | |
1653 | ); | |
1654 | $this->assign('credit_card_type', $params['credit_card_type']); | |
1655 | $this->assign('contributeMode', 'direct'); | |
1656 | $this->assign('isAmountzero', 0); | |
1657 | $this->assign('is_pay_later', 0); | |
1658 | $this->assign('isPrimary', 1); | |
1659 | } | |
1660 | ||
1661 | $this->assign('register_date', $params['register_date']); | |
1662 | if ($params['receive_date']) { | |
1663 | $this->assign('receive_date', $params['receive_date']); | |
1664 | } | |
1665 | ||
1666 | $participant = array(array('participant_id', '=', $participants[0]->id, 0, 0)); | |
1667 | // check whether its a test drive ref CRM-3075 | |
a7488080 | 1668 | if (!empty($this->_defaultValues['is_test'])) { |
6a488035 TO |
1669 | $participant[] = array('participant_test', '=', 1, 0, 0); |
1670 | } | |
1671 | ||
1672 | $template = CRM_Core_Smarty::singleton(); | |
1673 | $customGroup = array(); | |
1674 | //format submitted data | |
1675 | foreach ($params['custom'] as $fieldID => $values) { | |
1676 | foreach ($values as $fieldValue) { | |
1677 | $customValue = array('data' => $fieldValue['value']); | |
1678 | $customFields[$fieldID]['id'] = $fieldID; | |
1679 | $formattedValue = CRM_Core_BAO_CustomGroup::formatCustomValues($customValue, $customFields[$fieldID], TRUE); | |
1680 | $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); | |
1681 | } | |
1682 | } | |
1683 | ||
1684 | foreach ($this->_contactIds as $num => $contactID) { | |
1685 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
1686 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
1687 | ||
1688 | $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; | |
1689 | ||
1690 | $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
1691 | if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { | |
1692 | $this->assign('isOnWaitlist', TRUE); | |
1693 | } | |
1694 | ||
1695 | $this->assign('customGroup', $customGroup); | |
1696 | $this->assign('contactID', $contactID); | |
1697 | $this->assign('participantID', $participants[$num]->id); | |
1698 | ||
1699 | $this->_id = $participants[$num]->id; | |
1700 | ||
1701 | if ($this->_isPaidEvent) { | |
1702 | // fix amount for each of participants ( for bulk mode ) | |
1703 | $eventAmount = array(); | |
1704 | if (!empty($additionalParticipantDetails)) { | |
1705 | $params['amount_level'] = preg_replace('/\ 1/', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; | |
1706 | } | |
1707 | ||
1708 | $eventAmount[$num] = array( | |
1709 | 'label' => preg_replace('/\ 1/', '', $params['amount_level']), | |
1710 | 'amount' => $params['fee_amount'], | |
1711 | ); | |
1712 | //as we are using same template for online & offline registration. | |
1713 | //So we have to build amount as array. | |
1714 | $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); | |
1715 | $this->assign('amount', $eventAmount); | |
1716 | } | |
1717 | ||
1718 | $sendTemplateParams = array( | |
1719 | 'groupName' => 'msg_tpl_workflow_event', | |
1720 | 'valueName' => 'event_offline_receipt', | |
1721 | 'contactId' => $contactID, | |
1722 | 'isTest' => (bool) CRM_Utils_Array::value('is_test', $this->_defaultValues), | |
99deba35 | 1723 | 'PDFFilename' => ts('confirmation').'.pdf', |
6a488035 TO |
1724 | ); |
1725 | ||
1726 | // try to send emails only if email id is present | |
1727 | // and the do-not-email option is not checked for that contact | |
1728 | if ($this->_contributorEmail and !$this->_toDoNotEmail) { | |
1729 | $sendTemplateParams['from'] = $receiptFrom; | |
1730 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; | |
1731 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; | |
1732 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails); | |
1733 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails); | |
1734 | } | |
1735 | ||
c6327d7d | 1736 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
1737 | if ($mailSent) { |
1738 | $sent[] = $contactID; | |
1739 | foreach ($participants as $ids => $values) { | |
1740 | if ($values->contact_id == $contactID) { | |
1741 | CRM_Activity_BAO_Activity::addActivity($values, 'Email'); | |
1742 | break; | |
1743 | } | |
1744 | } | |
1745 | } | |
1746 | else { | |
1747 | $notSent[] = $contactID; | |
1748 | } | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | // set the participant id if it is not set | |
1753 | if (!$this->_id) { | |
1754 | $this->_id = $participants[0]->id; | |
1755 | } | |
1756 | ||
1757 | if (($this->_action & CRM_Core_Action::UPDATE)) { | |
1758 | $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1759 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1760 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', array(1 => $this->_contributorEmail)); |
1761 | } | |
1762 | ||
1763 | if ($updateStatusMsg) { | |
1764 | $statusMsg = "{$statusMsg} {$updateStatusMsg}"; | |
1765 | } | |
1766 | } | |
1767 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
1768 | if ($this->_single) { | |
1769 | $statusMsg = ts('Event registration for %1 has been added.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1770 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1771 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', array(1 => $this->_contributorEmail)); |
1772 | } | |
1773 | } | |
1774 | else { | |
1775 | $statusMsg = ts('Total Participant(s) added to event: %1.', array(1 => count($this->_contactIds))); | |
1776 | if (count($notSent) > 0) { | |
1777 | $statusMsg .= ' ' . ts('Email has NOT been sent to %1 contact(s) - communication preferences specify DO NOT EMAIL OR valid Email is NOT present. ', array(1 => count($notSent))); | |
1778 | } | |
1779 | elseif (isset($params['send_receipt'])) { | |
1780 | $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants'); | |
1781 | } | |
1782 | } | |
1783 | } | |
1784 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
1785 | ||
1786 | $buttonName = $this->controller->getButtonName(); | |
1787 | if ($this->_context == 'standalone') { | |
1788 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1789 | $urlParams = 'reset=1&action=add&context=standalone'; | |
1790 | if ($this->_mode) { | |
1791 | $urlParams .= '&mode=' . $this->_mode; | |
1792 | } | |
1793 | if ($this->_eID) { | |
1794 | $urlParams .= '&eid=' . $this->_eID; | |
1795 | } | |
1796 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); | |
1797 | } | |
1798 | else { | |
1799 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1800 | "reset=1&cid={$this->_contactId}&selectedChild=participant" | |
1801 | )); | |
1802 | } | |
1803 | } | |
1804 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1805 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1806 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" | |
1807 | )); | |
1808 | } | |
1809 | } | |
1810 | } | |
1811 |