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(); | |
6a488035 TO |
455 | if (!empty($roleIds)) { |
456 | $query = " | |
457 | SELECT civicrm_custom_group.name as name, | |
458 | civicrm_custom_group.id as id, | |
459 | extends_entity_column_value as value | |
460 | FROM civicrm_custom_group | |
461 | WHERE ( extends_entity_column_value REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', array_keys($roleIds)) . "[[:>:]]' | |
462 | OR extends_entity_column_value IS NULL ) | |
463 | AND extends_entity_column_id = '{$this->_roleCustomDataTypeID}' | |
464 | AND extends = 'Participant' | |
465 | AND is_active = 1"; | |
466 | ||
467 | $dao = CRM_Core_DAO::executeQuery($query); | |
468 | while ($dao->fetch()) { | |
469 | if ($dao->value) { | |
470 | $getRole = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->value); | |
471 | foreach ($getRole as $r) { | |
472 | if (!$r) { | |
473 | continue; | |
474 | } | |
475 | if (isset($this->_participantRoleIds[$r])) { | |
476 | $this->_participantRoleIds[$r] .= ',' . $dao->name; | |
477 | } | |
478 | else { | |
479 | $this->_participantRoleIds[$r] = $dao->name; | |
480 | } | |
481 | } | |
482 | } | |
483 | else { | |
484 | if (isset($this->_participantRoleIds[0])) { | |
485 | $this->_participantRoleIds[0] .= ',' . $dao->name; | |
486 | } | |
487 | else { | |
488 | $this->_participantRoleIds[0] = $dao->name; | |
489 | } | |
490 | } | |
491 | } | |
492 | $dao->free(); | |
493 | } | |
494 | foreach ($roleIds as $k => $v) { | |
495 | if (!isset($this->_participantRoleIds[$k])) { | |
496 | $this->_participantRoleIds[$k] = ''; | |
497 | } | |
498 | } | |
499 | $this->assign('participantRoleIds', $this->_participantRoleIds); | |
500 | } | |
501 | ||
502 | /** | |
503 | * This function sets the default values for the form in edit/view mode | |
504 | * the default values are retrieved from the database | |
505 | * | |
506 | * @access public | |
507 | * | |
355ba699 | 508 | * @return void |
6a488035 TO |
509 | */ |
510 | public function setDefaultValues() { | |
511 | if ($this->_showFeeBlock) { | |
512 | return CRM_Event_Form_EventFees::setDefaultValues($this); | |
513 | } | |
514 | ||
515 | if ($this->_cdType) { | |
516 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
517 | } | |
518 | ||
519 | $defaults = array(); | |
520 | ||
521 | if ($this->_action & CRM_Core_Action::DELETE) { | |
522 | return $defaults; | |
523 | } | |
524 | ||
525 | if ($this->_id) { | |
526 | $ids = array(); | |
527 | $params = array('id' => $this->_id); | |
528 | ||
529 | CRM_Event_BAO_Participant::getValues($params, $defaults, $ids); | |
530 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
531 | if ($defaults[$this->_id]['role_id']) { | |
532 | foreach (explode($sep, $defaults[$this->_id]['role_id']) as $k => $v) { | |
533 | $defaults[$this->_id]["role_id[{$v}]"] = 1; | |
534 | } | |
535 | unset($defaults[$this->_id]['role_id']); | |
536 | } | |
537 | $this->_contactId = $defaults[$this->_id]['contact_id']; | |
538 | $this->_statusId = $defaults[$this->_id]['participant_status_id']; | |
539 | ||
540 | //set defaults for note | |
541 | $noteDetails = CRM_Core_BAO_Note::getNote($this->_id, 'civicrm_participant'); | |
542 | $defaults[$this->_id]['note'] = array_pop($noteDetails); | |
543 | ||
544 | // Check if this is a primaryParticipant (registered for others) and retrieve additional participants if true (CRM-4859) | |
545 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
546 | $this->assign('additionalParticipants', CRM_Event_BAO_Participant::getAdditionalParticipants($this->_id)); | |
547 | } | |
548 | ||
549 | // Get registered_by contact ID and display_name if participant was registered by someone else (CRM-4859) | |
a7488080 | 550 | if (!empty($defaults[$this->_id]['participant_registered_by_id'])) { |
6a488035 TO |
551 | $registered_by_contact_id = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', |
552 | $defaults[$this->_id]['participant_registered_by_id'], | |
553 | 'contact_id', 'id' | |
554 | ); | |
555 | $this->assign('participant_registered_by_id', $defaults[$this->_id]['participant_registered_by_id']); | |
556 | $this->assign('registered_by_contact_id', $registered_by_contact_id); | |
557 | $this->assign('registered_by_display_name', CRM_Contact_BAO_Contact::displayName($registered_by_contact_id)); | |
558 | } | |
559 | } | |
560 | ||
561 | if ($this->_action & (CRM_Core_Action::VIEW | CRM_Core_Action::BROWSE)) { | |
562 | $inactiveNeeded = TRUE; | |
563 | $viewMode = TRUE; | |
564 | } | |
565 | else { | |
566 | $viewMode = FALSE; | |
567 | $inactiveNeeded = FALSE; | |
568 | } | |
569 | ||
570 | //setting default register date | |
571 | if ($this->_action == CRM_Core_Action::ADD) { | |
572 | $statuses = array_flip($this->_participantStatuses); | |
573 | $defaults[$this->_id]['status_id'] = CRM_Utils_Array::value(ts('Registered'), $statuses); | |
a7488080 | 574 | if (!empty($defaults[$this->_id]['event_id'])) { |
6a488035 TO |
575 | $contributionTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', |
576 | $defaults[$this->_id]['event_id'], | |
d96cf288 DG |
577 | 'financial_type_id' |
578 | ); | |
6a488035 | 579 | if ($contributionTypeId) { |
d96cf288 | 580 | $defaults[$this->_id]['financial_type_id'] = $contributionTypeId; |
6a488035 TO |
581 | } |
582 | } | |
583 | ||
584 | if ($this->_mode) { | |
585 | $fields["email-{$this->_bltID}"] = 1; | |
586 | $fields['email-Primary'] = 1; | |
587 | ||
588 | if ($this->_contactId) { | |
589 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactId, $fields, $defaults); | |
590 | } | |
591 | ||
592 | if (empty($defaults["email-{$this->_bltID}"]) && | |
593 | !empty($defaults['email-Primary']) | |
594 | ) { | |
595 | $defaults[$this->_id]["email-{$this->_bltID}"] = $defaults['email-Primary']; | |
596 | } | |
597 | } | |
598 | ||
599 | $submittedRole = $this->getElementValue('role_id'); | |
a7488080 | 600 | if (!empty($submittedRole[0])) { |
6a488035 TO |
601 | $roleID = $submittedRole[0]; |
602 | } | |
603 | $submittedEvent = $this->getElementValue('event_id'); | |
604 | if ($submittedEvent[0]) { | |
605 | $eventID = $submittedEvent[0]; | |
606 | } | |
607 | } | |
608 | else { | |
609 | $defaults[$this->_id]['record_contribution'] = 0; | |
610 | ||
611 | if ($defaults[$this->_id]['participant_is_pay_later']) { | |
612 | $this->assign('participant_is_pay_later', TRUE); | |
613 | } | |
614 | ||
615 | $this->assign('participant_status_id', $defaults[$this->_id]['participant_status_id']); | |
616 | $roleID = $defaults[$this->_id]['participant_role_id']; | |
617 | $eventID = $defaults[$this->_id]['event_id']; | |
618 | ||
619 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
620 | ||
621 | $this->_discountId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'discount_id'); | |
622 | if ($this->_discountId) { | |
623 | $this->set('discountId', $this->_discountId); | |
624 | } | |
625 | } | |
626 | ||
627 | list($defaults[$this->_id]['register_date'], | |
628 | $defaults[$this->_id]['register_date_time'] | |
629 | ) = CRM_Utils_Date::setDateDefaults( | |
630 | CRM_Utils_Array::value('register_date', $defaults[$this->_id]), 'activityDateTime' | |
631 | ); | |
632 | ||
633 | //assign event and role id, this is needed for Custom data building | |
634 | $sep = CRM_Core_DAO::VALUE_SEPARATOR; | |
a7488080 | 635 | if (!empty($defaults[$this->_id]['participant_role_id'])) { |
6a488035 TO |
636 | $roleIDs = explode($sep, $defaults[$this->_id]['participant_role_id']); |
637 | } | |
638 | if (isset($roleIDs)) { | |
639 | $this->assign('roleID', $roleIDs); | |
640 | } | |
641 | if (isset($_POST['event_id'])) { | |
642 | $eventID = $_POST['event_id']; | |
643 | if ($eventID) { | |
644 | $this->_eventTypeId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventID, 'event_type_id', 'id'); | |
645 | } | |
646 | } | |
647 | ||
648 | if (isset($eventID)) { | |
649 | $this->assign('eventID', $eventID); | |
650 | $this->set('eventId', $eventID); | |
651 | } | |
652 | ||
653 | if (isset($this->_eventTypeId)) { | |
654 | $this->assign('eventTypeID', $this->_eventTypeId); | |
655 | } | |
656 | ||
657 | $this->assign('event_is_test', CRM_Utils_Array::value('event_is_test', $defaults[$this->_id])); | |
658 | return $defaults[$this->_id]; | |
659 | } | |
660 | ||
661 | /** | |
662 | * Function to build the form | |
663 | * | |
355ba699 | 664 | * @return void |
6a488035 TO |
665 | * @access public |
666 | */ | |
667 | public function buildQuickForm() { | |
bc2eeabb | 668 | CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'js/crm.livePage.js'); |
e8cf3013 PJ |
669 | $participantStatuses = CRM_Event_PseudoConstant::participantStatus(); |
670 | $partiallyPaidStatusId = array_search('Partially paid', $participantStatuses); | |
81f3d017 | 671 | $this->assign('partiallyPaidStatusId', $partiallyPaidStatusId); |
e8cf3013 | 672 | |
6a488035 TO |
673 | if ($this->_showFeeBlock) { |
674 | return CRM_Event_Form_EventFees::buildQuickForm($this); | |
675 | } | |
676 | ||
677 | if ($this->_cdType) { | |
678 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
679 | } | |
680 | ||
681 | //need to assign custom data type to the template | |
682 | $this->assign('customDataType', 'Participant'); | |
683 | ||
684 | $this->applyFilter('__ALL__', 'trim'); | |
685 | ||
686 | if ($this->_action & CRM_Core_Action::DELETE) { | |
687 | if ($this->_single) { | |
688 | $additionalParticipant = count(CRM_Event_BAO_Event::buildCustomProfile($this->_id, | |
689 | NULL, | |
690 | $this->_contactId, | |
691 | FALSE, | |
692 | TRUE | |
693 | )) - 1; | |
694 | if ($additionalParticipant) { | |
695 | $deleteParticipants = array(1 => ts('Delete this participant record along with associated participant record(s).'), | |
696 | 2 => ts('Delete only this participant record.'), | |
697 | ); | |
698 | $this->addRadio('delete_participant', NULL, $deleteParticipants, NULL, '<br />'); | |
699 | $this->setDefaults(array('delete_participant' => 1)); | |
700 | $this->assign('additionalParticipant', $additionalParticipant); | |
701 | } | |
702 | } | |
703 | $this->addButtons(array( | |
704 | array( | |
705 | 'type' => 'next', | |
706 | 'name' => ts('Delete'), | |
707 | 'spacing' => ' ', | |
708 | 'isDefault' => TRUE, | |
709 | ), | |
710 | array( | |
711 | 'type' => 'cancel', | |
712 | 'name' => ts('Cancel'), | |
713 | ), | |
714 | ) | |
715 | ); | |
716 | return; | |
717 | } | |
718 | ||
719 | ||
720 | if ($this->_single) { | |
721 | $urlPath = 'civicrm/contact/view/participant'; | |
722 | $urlParams = "reset=1&cid={$this->_contactId}&context=participant"; | |
723 | if ($this->_context == 'standalone') { | |
479696ed | 724 | $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE), TRUE); |
6a488035 TO |
725 | $urlParams = 'reset=1&context=standalone'; |
726 | $urlPath = 'civicrm/participant/add'; | |
727 | } | |
728 | ||
729 | if ($this->_id) { | |
730 | $urlParams .= "&action=update&id={$this->_id}"; | |
731 | } | |
732 | else { | |
733 | $urlParams .= "&action=add"; | |
734 | } | |
735 | ||
736 | if ($this->_mode) { | |
737 | $urlParams .= "&mode={$this->_mode}"; | |
738 | } | |
739 | ||
740 | $url = CRM_Utils_System::url($urlPath, $urlParams, | |
741 | FALSE, NULL, FALSE | |
742 | ); | |
743 | } | |
744 | else { | |
745 | $currentPath = CRM_Utils_System::currentPath(); | |
746 | ||
747 | $url = CRM_Utils_System::url($currentPath, '_qf_Participant_display=true', | |
748 | FALSE, NULL, FALSE | |
749 | ); | |
750 | } | |
751 | ||
752 | $this->assign('refreshURL', $url); | |
753 | ||
1beb268a CW |
754 | $eventFieldParams = array( |
755 | 'entity' => 'event', | |
756 | 'select' => array('minimumInputLength' => 0), | |
757 | 'api' => array( | |
758 | 'extra' => array('campaign_id', 'default_role_id', 'event_type_id'), | |
759 | ) | |
760 | ); | |
6a488035 TO |
761 | |
762 | if ($this->_mode) { | |
1beb268a CW |
763 | // exclude events which are not monetary when credit card registration is used |
764 | $eventFieldParams['api']['params']['is_monetary'] = 1; | |
6a488035 TO |
765 | $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, TRUE); |
766 | } | |
767 | ||
1beb268a | 768 | $element = $this->addEntityRef('event_id', ts('Event'), $eventFieldParams, TRUE); |
6a488035 TO |
769 | |
770 | //frozen the field fix for CRM-4171 | |
771 | if ($this->_action & CRM_Core_Action::UPDATE && $this->_id) { | |
772 | if (CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
773 | $this->_id, 'contribution_id', 'participant_id' | |
774 | )) { | |
775 | $element->freeze(); | |
776 | } | |
777 | } | |
778 | ||
779 | //CRM-7362 --add campaigns. | |
780 | $campaignId = NULL; | |
781 | if ($this->_id) { | |
782 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_id, 'campaign_id'); | |
783 | } | |
784 | if (!$campaignId) { | |
785 | $eventId = CRM_Utils_Request::retrieve('eid', 'Positive', $this); | |
786 | if ($eventId) { | |
787 | $campaignId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $eventId, 'campaign_id'); | |
788 | } | |
789 | } | |
790 | CRM_Campaign_BAO_Campaign::addCampaign($this, $campaignId); | |
791 | ||
792 | $this->addDateTime('register_date', ts('Registration Date'), TRUE, array('formatType' => 'activityDateTime')); | |
793 | ||
794 | if ($this->_id) { | |
795 | $this->assign('entityID', $this->_id); | |
796 | } | |
797 | ||
798 | $roleids = CRM_Event_PseudoConstant::participantRole(); | |
799 | ||
800 | foreach ($roleids as $rolekey => $rolevalue) { | |
1beb268a | 801 | $roleTypes[] = $this->createElement('checkbox', $rolekey, NULL, $rolevalue); |
6a488035 TO |
802 | } |
803 | ||
804 | $this->addGroup($roleTypes, 'role_id', ts('Participant Role')); | |
805 | $this->addRule('role_id', ts('Role is required'), 'required'); | |
806 | ||
807 | // CRM-4395 | |
808 | $checkCancelledJs = array('onchange' => "return sendNotification( );"); | |
809 | $confirmJS = NULL; | |
810 | if ($this->_onlinePendingContributionId) { | |
811 | $cancelledparticipantStatusId = array_search('Cancelled', CRM_Event_PseudoConstant::participantStatus()); | |
812 | $cancelledContributionStatusId = array_search('Cancelled', | |
813 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
814 | ); | |
815 | $checkCancelledJs = array( | |
816 | 'onchange' => | |
817 | "checkCancelled( this.value, {$cancelledparticipantStatusId},{$cancelledContributionStatusId});", | |
818 | ); | |
819 | ||
820 | $participantStatusId = array_search('Pending from pay later', | |
821 | CRM_Event_PseudoConstant::participantStatus() | |
822 | ); | |
823 | $contributionStatusId = array_search('Completed', | |
824 | CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name') | |
825 | ); | |
826 | $confirmJS = array('onclick' => "return confirmStatus( {$participantStatusId}, {$contributionStatusId} );"); | |
827 | } | |
828 | ||
453ed6d1 KJ |
829 | // get the participant status names to build special status array which is used to show notification |
830 | // checkbox below participant status select | |
831 | $participantStatusName = CRM_Event_PseudoConstant::participantStatus(); | |
832 | $notificationStatuses = array( | |
833 | 'Cancelled', | |
834 | 'Pending from waitlist', | |
835 | 'Pending from approval', | |
836 | 'Expired', | |
837 | ); | |
838 | ||
839 | // get the required status and then implode only ids | |
840 | $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); | |
841 | $this->assign('notificationStatusIds', $notificationStatusIds); | |
842 | ||
6a488035 | 843 | $this->_participantStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); |
f6bae84f PJ |
844 | $this->addSelect('status_id', $checkCancelledJs, TRUE); |
845 | ||
81f3d017 PJ |
846 | $enableCart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, |
847 | 'enable_cart' | |
848 | ); | |
f6bae84f PJ |
849 | $pendingInCartStatusId = array_search('Pending in cart', $participantStatusName); |
850 | $this->assign('pendingInCartStatusId', $pendingInCartStatusId); | |
851 | $this->assign('enableCart', $enableCart); | |
6a488035 TO |
852 | |
853 | $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); | |
854 | ||
855 | $this->add('text', 'source', ts('Event Source')); | |
856 | $noteAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
857 | $this->add('textarea', 'note', ts('Notes'), $noteAttributes['note']); | |
858 | ||
859 | $buttons[] = array( | |
860 | 'type' => 'upload', | |
861 | 'name' => ts('Save'), | |
862 | 'isDefault' => TRUE, | |
863 | 'js' => $confirmJS, | |
864 | ); | |
865 | ||
866 | $path = CRM_Utils_System::currentPath(); | |
239a5027 PJ |
867 | $excludeForPaths = array( |
868 | 'civicrm/contact/search', | |
869 | 'civicrm/group/search' | |
870 | ); | |
871 | if (!in_array($path, $excludeForPaths)) { | |
6a488035 TO |
872 | $buttons[] = array( |
873 | 'type' => 'upload', | |
874 | 'name' => ts('Save and New'), | |
875 | 'subName' => 'new', | |
876 | 'js' => $confirmJS, | |
877 | ); | |
878 | } | |
239a5027 | 879 | |
6a488035 TO |
880 | $buttons[] = array( |
881 | 'type' => 'cancel', | |
882 | 'name' => ts('Cancel'), | |
883 | ); | |
884 | ||
885 | $this->addButtons($buttons); | |
886 | if ($this->_action == CRM_Core_Action::VIEW) { | |
887 | $this->freeze(); | |
888 | } | |
889 | } | |
890 | ||
891 | /** | |
892 | * Add local and global form rules | |
893 | * | |
894 | * @access protected | |
895 | * | |
896 | * @return void | |
897 | */ | |
898 | function addRules() { | |
899 | $this->addFormRule(array('CRM_Event_Form_Participant', 'formRule'), $this); | |
900 | } | |
901 | ||
902 | /** | |
903 | * global validation rules for the form | |
904 | * | |
905 | * @param array $fields posted values of the form | |
906 | * | |
907 | * @return array list of errors to be posted back to the form | |
908 | * @static | |
909 | * @access public | |
910 | */ | |
911 | static function formRule($values, $files, $self) { | |
912 | // If $values['_qf_Participant_next'] is Delete or | |
913 | // $values['event_id'] is empty, then return | |
914 | // instead of proceeding further. | |
915 | ||
916 | if ((CRM_Utils_Array::value('_qf_Participant_next', $values) == 'Delete') || | |
917 | (!$values['event_id']) | |
918 | ) { | |
919 | return TRUE; | |
920 | } | |
921 | ||
922 | $errorMsg = array(); | |
6a488035 | 923 | |
a7488080 | 924 | if (!empty($values['payment_processor_id'])) { |
6a488035 | 925 | // make sure that credit card number and cvv are valid |
7cb3d4f0 | 926 | CRM_Core_Payment_Form::validateCreditCard($values, $errorMsg); |
6a488035 TO |
927 | } |
928 | ||
a7488080 CW |
929 | if (!empty($values['record_contribution'])) { |
930 | if (empty($values['financial_type_id'])) { | |
d96cf288 DG |
931 | $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); |
932 | } | |
a7488080 | 933 | if (empty($values['payment_instrument_id'])) { |
d96cf288 DG |
934 | $errorMsg['payment_instrument_id'] = ts('Paid By is a required field.'); |
935 | } | |
6a488035 TO |
936 | } |
937 | ||
938 | // validate contribution status for 'Failed'. | |
8cc574cf | 939 | if ($self->_onlinePendingContributionId && !empty($values['record_contribution']) && |
6a488035 TO |
940 | (CRM_Utils_Array::value('contribution_status_id', $values) == |
941 | array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name')) | |
942 | ) | |
943 | ) { | |
944 | $errorMsg['contribution_status_id'] = ts('Please select a valid payment status before updating.'); | |
945 | } | |
946 | ||
947 | // do the amount validations. | |
948 | //skip for update mode since amount is freeze, CRM-6052 | |
8cc574cf | 949 | if ((!$self->_id && empty($values['total_amount']) && |
6a488035 TO |
950 | empty($self->_values['line_items']) |
951 | ) || | |
952 | ($self->_id && !$self->_paymentId && isset($self->_values['line_items']) && is_array($self->_values['line_items'])) | |
953 | ) { | |
954 | if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) { | |
9da8dc8c | 955 | CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $values, $errorMsg, TRUE); |
6a488035 TO |
956 | } |
957 | } | |
958 | return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? TRUE : $errorMsg; | |
959 | } | |
960 | ||
961 | /** | |
962 | * Function to process the form | |
963 | * | |
964 | * @access public | |
965 | */ | |
966 | public function postProcess() { | |
967 | // get the submitted form values. | |
968 | $params = $this->controller->exportValues($this->_name); | |
969 | ||
970 | if ($this->_action & CRM_Core_Action::DELETE) { | |
971 | if (CRM_Utils_Array::value('delete_participant', $params) == 2) { | |
972 | $additionalId = (CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id)); | |
973 | $participantLinks = (CRM_Event_BAO_Participant::getAdditionalParticipantUrl($additionalId)); | |
974 | } | |
975 | if (CRM_Utils_Array::value('delete_participant', $params) == 1) { | |
976 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
977 | foreach ($additionalIds as $value) { | |
978 | CRM_Event_BAO_Participant::deleteParticipant($value); | |
979 | } | |
980 | } | |
981 | CRM_Event_BAO_Participant::deleteParticipant($this->_id); | |
982 | CRM_Core_Session::setStatus(ts('Selected participant was deleted successfully.'), ts('Record Deleted'), 'success'); | |
983 | if (!empty($participantLinks)) { | |
984 | $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; | |
985 | CRM_Core_Session::setStatus($status, ts('Group Payment Deleted')); | |
986 | } | |
987 | return; | |
988 | } | |
989 | ||
aac57c29 | 990 | $participantStatus = CRM_Event_PseudoConstant::participantStatus(); |
6a488035 | 991 | // set the contact, when contact is selected |
479696ed CW |
992 | if (!empty($params['contact_id'])) { |
993 | $this->_contactId = $params['contact_id']; | |
6a488035 | 994 | } |
9da8dc8c | 995 | if ($this->_priceSetId && $isQuickConfig = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) { |
6a488035 TO |
996 | $this->_quickConfig = $isQuickConfig; |
997 | } | |
998 | ||
999 | if ($this->_id) { | |
1000 | $params['id'] = $this->_id; | |
1001 | } | |
1002 | ||
1003 | $config = CRM_Core_Config::singleton(); | |
1004 | if ($this->_isPaidEvent) { | |
1005 | ||
1006 | $contributionParams = array(); | |
1007 | $lineItem = array(); | |
1008 | $additionalParticipantDetails = array(); | |
1009 | if (($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId) { | |
1010 | $participantBAO = new CRM_Event_BAO_Participant(); | |
1011 | $participantBAO->id = $this->_id; | |
1012 | $participantBAO->find(TRUE); | |
1013 | $contributionParams['total_amount'] = $participantBAO->fee_amount; | |
1014 | ||
1015 | $params['discount_id'] = NULL; | |
1016 | //re-enter the values for UPDATE mode | |
1017 | $params['fee_level'] = $params['amount_level'] = $participantBAO->fee_level; | |
1018 | $params['fee_amount'] = $participantBAO->fee_amount; | |
1019 | if (isset($params['priceSetId'])) { | |
1020 | $lineItem[0] = CRM_Price_BAO_LineItem::getLineItems($this->_id); | |
1021 | } | |
1022 | //also add additional participant's fee level/priceset | |
1023 | if (CRM_Event_BAO_Participant::isPrimaryParticipant($this->_id)) { | |
1024 | $additionalIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_id); | |
1025 | $hasLineItems = CRM_Utils_Array::value('priceSetId', $params, FALSE); | |
1026 | $additionalParticipantDetails = CRM_Event_BAO_Participant::getFeeDetails($additionalIds, | |
1027 | $hasLineItems | |
1028 | ); | |
1029 | } | |
1030 | } | |
1031 | else { | |
1032 | ||
1033 | //check if discount is selected | |
a7488080 | 1034 | if (!empty($params['discount_id'])) { |
6a488035 TO |
1035 | $discountId = $params['discount_id']; |
1036 | } | |
1037 | else { | |
1038 | $discountId = $params['discount_id'] = 'null'; | |
1039 | } | |
1040 | ||
1041 | //lets carry currency, CRM-4453 | |
1042 | $params['fee_currency'] = $config->defaultCurrency; | |
9da8dc8c | 1043 | CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], |
8567d0f8 PN |
1044 | $params, $lineItem[0] |
1045 | ); | |
6a488035 TO |
1046 | //CRM-11529 for quick config backoffice transactions |
1047 | //when financial_type_id is passed in form, update the | |
1048 | //lineitems with the financial type selected in form | |
1049 | $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $params); | |
1050 | $isPaymentRecorded = CRM_Utils_Array::value('record_contribution', $params); | |
1051 | if ($isPaymentRecorded && $this->_quickConfig && $submittedFinancialType) { | |
1052 | foreach ($lineItem[0] as &$values) { | |
1053 | $values['financial_type_id'] = $submittedFinancialType; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | $params['fee_level'] = $params['amount_level']; | |
1058 | $contributionParams['total_amount'] = $params['amount']; | |
f6bae84f PJ |
1059 | if ($this->_quickConfig && !empty($params['total_amount']) && |
1060 | ($params['status_id'] != array_search('Partially paid', $participantStatus) && | |
1061 | $params['status_id'] != array_search('Pending refund', $participantStatus))) { | |
6a488035 TO |
1062 | $params['fee_amount'] = $params['total_amount']; |
1063 | } else { | |
1064 | //fix for CRM-3086 | |
1065 | $params['fee_amount'] = $params['amount']; | |
1066 | } | |
1067 | } | |
1068 | ||
1069 | if (isset($params['priceSetId'])) { | |
1070 | if (!empty($lineItem[0])) { | |
1071 | $this->set('lineItem', $lineItem); | |
1072 | ||
1073 | $this->_lineItem = $lineItem; | |
1074 | $lineItem = array_merge($lineItem, $additionalParticipantDetails); | |
1075 | ||
1076 | $participantCount = array(); | |
1077 | foreach ($lineItem as $k) { | |
1078 | foreach ($k as $v) { | |
1079 | if (CRM_Utils_Array::value('participant_count', $v) > 0) { | |
1080 | $participantCount[] = $v['participant_count']; | |
1081 | } | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | if (isset($participantCount)) { | |
1086 | $this->assign('pricesetFieldsCount', $participantCount); | |
1087 | } | |
1088 | $this->assign('lineItem', empty($lineItem[0]) || $this->_quickConfig? FALSE : $lineItem); | |
1089 | } | |
1090 | else { | |
1091 | $this->assign('amount_level', $params['amount_level']); | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | $this->_params = $params; | |
fbc54416 PJ |
1096 | $amountOwed = NULL; |
1097 | if (isset($params['amount'])) { | |
1098 | $amountOwed = $params['amount']; | |
1099 | unset($params['amount']); | |
1100 | } | |
6a488035 TO |
1101 | $params['register_date'] = CRM_Utils_Date::processDate($params['register_date'], $params['register_date_time']); |
1102 | $params['receive_date'] = CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $params)); | |
1103 | $params['contact_id'] = $this->_contactId; | |
1104 | ||
1105 | // overwrite actual payment amount if entered | |
a7488080 | 1106 | if (!empty($params['total_amount'])) { |
6a488035 TO |
1107 | $contributionParams['total_amount'] = CRM_Utils_Array::value('total_amount', $params); |
1108 | } | |
1109 | ||
1110 | ||
1111 | // Retrieve the name and email of the current user - this will be the FROM for the receipt email | |
1112 | $session = CRM_Core_Session::singleton(); | |
1113 | $userID = $session->get('userID'); | |
1114 | list($userName, | |
1115 | $userEmail | |
1116 | ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); | |
1117 | ||
1118 | if ($this->_contactId) { | |
1119 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($this->_contactId); | |
1120 | } | |
1121 | ||
1122 | if ($this->_mode) { | |
1123 | if (!$this->_isPaidEvent) { | |
1124 | CRM_Core_Error::fatal(ts('Selected Event is not Paid Event ')); | |
1125 | } | |
1126 | //modify params according to parameter used in create | |
1127 | //participant method (addParticipant) | |
1128 | $this->_params['participant_status_id'] = $params['status_id']; | |
1129 | $this->_params['participant_role_id'] = $params['role_id']; | |
1130 | $this->_params['participant_register_date'] = $params['register_date']; | |
1131 | ||
1132 | $eventTitle = | |
1133 | CRM_Core_DAO::getFieldValue( | |
1134 | 'CRM_Event_DAO_Event', | |
1135 | $params['event_id'], | |
1136 | 'title' | |
1137 | ); | |
1138 | ||
1139 | // set source if not set | |
1140 | if (empty($params['source'])) { | |
03e04002 | 1141 | $this->_params['participant_source'] = ts('Offline Registration for Event: %2 by: %1', array(1 => $userName, 2 => $eventTitle)); |
6a488035 TO |
1142 | } |
1143 | else { | |
03e04002 | 1144 | $this->_params['participant_source'] = $params['source']; |
6a488035 TO |
1145 | } |
1146 | $this->_params['description'] = $this->_params['participant_source']; | |
1147 | ||
1148 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($this->_params['payment_processor_id'], | |
1149 | $this->_mode | |
1150 | ); | |
1151 | $now = date('YmdHis'); | |
1152 | $fields = array(); | |
1153 | ||
1154 | // set email for primary location. | |
1155 | $fields['email-Primary'] = 1; | |
1156 | $params['email-Primary'] = $params["email-{$this->_bltID}"] = $this->_contributorEmail; | |
1157 | ||
1158 | $params['register_date'] = $now; | |
1159 | ||
1160 | // now set the values for the billing location. | |
1161 | foreach ($this->_fields as $name => $dontCare) { | |
1162 | $fields[$name] = 1; | |
1163 | } | |
1164 | ||
1165 | // also add location name to the array | |
1166 | $params["address_name-{$this->_bltID}"] = | |
1167 | CRM_Utils_Array::value('billing_first_name', $params) . ' ' . | |
1168 | CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . | |
1169 | CRM_Utils_Array::value('billing_last_name', $params); | |
1170 | ||
1171 | $params["address_name-{$this->_bltID}"] = trim($params["address_name-{$this->_bltID}"]); | |
1172 | $fields["address_name-{$this->_bltID}"] = 1; | |
1173 | $fields["email-{$this->_bltID}"] = 1; | |
1174 | $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); | |
1175 | ||
1176 | $nameFields = array('first_name', 'middle_name', 'last_name'); | |
1177 | ||
1178 | foreach ($nameFields as $name) { | |
1179 | $fields[$name] = 1; | |
1180 | if (array_key_exists("billing_$name", $params)) { | |
1181 | $params[$name] = $params["billing_{$name}"]; | |
1182 | $params['preserveDBName'] = TRUE; | |
1183 | } | |
1184 | } | |
1185 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); | |
1186 | } | |
1187 | ||
1188 | $roleAllIds = CRM_Utils_Array::value('role_id', $params); | |
1189 | if ($roleAllIds) { | |
1190 | foreach ($roleAllIds as $rkey => $rvalue) { | |
1191 | $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $rkey, $this->_roleCustomDataTypeID); | |
1192 | $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', | |
1193 | FALSE, | |
1194 | FALSE, | |
1195 | CRM_Utils_Array::value('event_id', $params), | |
1196 | $this->_eventNameCustomDataTypeID | |
1197 | ); | |
1198 | $customFieldsEventType = CRM_Core_BAO_CustomField::getFields('Participant', | |
1199 | FALSE, | |
1200 | FALSE, | |
1201 | $this->_eventTypeId, | |
1202 | $this->_eventTypeCustomDataTypeID | |
1203 | ); | |
1204 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsRole, | |
1205 | CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, TRUE) | |
1206 | ); | |
1207 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEvent, $customFields); | |
1208 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsEventType, $customFields); | |
1209 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
1210 | $customFields, | |
1211 | $this->_id, | |
1212 | 'Participant' | |
1213 | ); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | //do cleanup line items if participant edit the Event Fee. | |
1218 | if (($this->_lineItem || !isset($params['proceSetId'])) && !$this->_paymentId && $this->_id) { | |
1219 | CRM_Price_BAO_LineItem::deleteLineItems($this->_id, 'civicrm_participant'); | |
1220 | } | |
03e04002 | 1221 | |
6a488035 TO |
1222 | if ($this->_mode) { |
1223 | // add all the additioanl payment params we need | |
1224 | $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}"]); | |
1225 | $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($this->_params["billing_country_id-{$this->_bltID}"]); | |
1226 | ||
1227 | $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($this->_params); | |
1228 | $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($this->_params); | |
1229 | $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); | |
1230 | $this->_params['amount'] = $params['fee_amount']; | |
1231 | $this->_params['amount_level'] = $params['amount_level']; | |
1232 | $this->_params['currencyID'] = $config->defaultCurrency; | |
1233 | $this->_params['payment_action'] = 'Sale'; | |
1234 | $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
1235 | ||
1236 | // at this point we've created a contact and stored its address etc | |
1237 | // all the payment processors expect the name and address to be in the | |
1238 | // so we copy stuff over to first_name etc. | |
1239 | $paymentParams = $this->_params; | |
a7488080 | 1240 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1241 | $paymentParams['email'] = $this->_contributorEmail; |
1242 | } | |
1243 | CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); | |
1244 | ||
1245 | $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); | |
1246 | ||
1247 | $result = &$payment->doDirectPayment($paymentParams); | |
1248 | ||
1249 | if (is_a($result, 'CRM_Core_Error')) { | |
1250 | CRM_Core_Error::displaySessionError($result); | |
1251 | CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1252 | "reset=1&action=add&cid={$this->_contactId}&context=participant&mode={$this->_mode}" | |
1253 | )); | |
1254 | } | |
1255 | ||
1256 | if ($result) { | |
1257 | $this->_params = array_merge($this->_params, $result); | |
1258 | } | |
1259 | ||
1260 | $this->_params['receive_date'] = $now; | |
1261 | ||
a7488080 | 1262 | if (!empty($this->_params['send_receipt'])) { |
6a488035 TO |
1263 | $this->_params['receipt_date'] = $now; |
1264 | } | |
1265 | else { | |
1266 | $this->_params['receipt_date'] = NULL; | |
1267 | } | |
1268 | ||
1269 | $this->set('params', $this->_params); | |
1270 | $this->assign('trxn_id', $result['trxn_id']); | |
1271 | $this->assign('receive_date', | |
1272 | CRM_Utils_Date::processDate($this->_params['receive_date']) | |
1273 | ); | |
1274 | ||
1275 | //add contribution record | |
1276 | $this->_params['financial_type_id'] = | |
1277 | CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'financial_type_id'); | |
1278 | $this->_params['mode'] = $this->_mode; | |
1279 | ||
1280 | //add contribution reocord | |
1281 | $contributions[] = $contribution = CRM_Event_Form_Registration_Confirm::processContribution($this, $this->_params, $result, $contactID, FALSE); | |
1282 | ||
1283 | // add participant record | |
1284 | $participants = array(); | |
a7488080 | 1285 | if (!empty($this->_params['participant_role_id']) && is_array($this->_params['participant_role_id'])) { |
6a488035 TO |
1286 | $this->_params['participant_role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, |
1287 | array_keys($this->_params['participant_role_id']) | |
1288 | ); | |
1289 | } | |
1290 | $participants[] = CRM_Event_Form_Registration::addParticipant($this->_params, $contactID); | |
1291 | ||
1292 | //add custom data for participant | |
1293 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, | |
1294 | CRM_Core_DAO::$_nullArray, | |
1295 | 'civicrm_participant', | |
1296 | $participants[0]->id, | |
1297 | 'Participant' | |
1298 | ); | |
1299 | //add participant payment | |
1300 | $paymentParticipant = array( | |
1301 | 'participant_id' => $participants[0]->id, | |
1302 | 'contribution_id' => $contribution->id, | |
1303 | ); | |
1304 | $ids = array(); | |
1305 | ||
1306 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids); | |
1307 | $this->_contactIds[] = $this->_contactId; | |
1308 | } | |
1309 | else { | |
1310 | $participants = array(); | |
1311 | if ($this->_single) { | |
1312 | if ($params['role_id']) { | |
1313 | foreach ($params['role_id'] as $k => $v) { | |
1314 | $rolesIDS[] = $k; | |
1315 | } | |
1316 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; | |
1317 | $params['role_id'] = implode($seperator, $rolesIDS); | |
1318 | } | |
1319 | else { | |
1320 | $params['role_id'] = 'NULL'; | |
1321 | } | |
1322 | $participants[] = CRM_Event_BAO_Participant::create($params); | |
1323 | } | |
1324 | else { | |
1325 | foreach ($this->_contactIds as $contactID) { | |
1326 | $commonParams = $params; | |
1327 | $commonParams['contact_id'] = $contactID; | |
1328 | if ($commonParams['role_id']) { | |
1329 | $rolesIDS = array(); | |
1330 | foreach ($commonParams['role_id'] as $k => $v) { | |
1331 | $rolesIDS[] = $k; | |
1332 | } | |
1333 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; | |
1334 | $commonParams['role_id'] = implode($seperator, $rolesIDS); | |
1335 | $commonParams['participant_role_id'] = implode($seperator, $rolesIDS); | |
1336 | } | |
1337 | else { | |
1338 | $commonParams['role_id'] = 'NULL'; | |
1339 | } | |
1340 | $participants[] = CRM_Event_BAO_Participant::create($commonParams); | |
1341 | } | |
1342 | } | |
1343 | ||
1344 | if (isset($params['event_id'])) { | |
1345 | $eventTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', | |
1346 | $params['event_id'], | |
1347 | 'title' | |
1348 | ); | |
1349 | } | |
1350 | ||
1351 | if ($this->_single) { | |
1352 | $this->_contactIds[] = $this->_contactId; | |
1353 | } | |
1354 | ||
1355 | $contributions = array(); | |
a7488080 CW |
1356 | if (!empty($params['record_contribution'])) { |
1357 | if (!empty($params['id'])) { | |
6a488035 TO |
1358 | if ($this->_onlinePendingContributionId) { |
1359 | $ids['contribution'] = $this->_onlinePendingContributionId; | |
1360 | } | |
1361 | else { | |
1362 | $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', | |
1363 | $params['id'], | |
1364 | 'contribution_id', | |
1365 | 'participant_id' | |
1366 | ); | |
1367 | } | |
1368 | } | |
1369 | unset($params['note']); | |
1370 | ||
1371 | //build contribution params | |
1372 | if (!$this->_onlinePendingContributionId) { | |
1373 | $contributionParams['source'] = "{$eventTitle}: Offline registration (by {$userName})"; | |
1374 | } | |
1375 | ||
1376 | $contributionParams['currency'] = $config->defaultCurrency; | |
1377 | $contributionParams['non_deductible_amount'] = 'null'; | |
0d8afee2 | 1378 | $contributionParams['receipt_date'] = !empty($params['send_receipt']) ? CRM_Utils_Array::value('receive_date', $params) : 'null'; |
6a488035 TO |
1379 | |
1380 | $recordContribution = array( 'contact_id', 'financial_type_id', | |
1381 | 'payment_instrument_id', 'trxn_id', | |
1382 | 'contribution_status_id', 'receive_date', | |
1383 | 'check_number', 'campaign_id', | |
1384 | ); | |
1385 | ||
1386 | foreach ($recordContribution as $f) { | |
1387 | $contributionParams[$f] = CRM_Utils_Array::value($f, $params); | |
1388 | if ($f == 'trxn_id') { | |
1389 | $this->assign('trxn_id', $contributionParams[$f]); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | //insert financial type name in receipt. | |
1394 | $this->assign('contributionTypeName', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', $contributionParams['financial_type_id'])); | |
1395 | $contributionParams['skipLineItem'] = 1; | |
7030a002 PN |
1396 | if ($this->_id) { |
1397 | $contributionParams['contribution_mode'] = 'participant'; | |
1398 | $contributionParams['participant_id'] = $this->_id; | |
1399 | } | |
6a488035 TO |
1400 | // Set is_pay_later flag for back-office offline Pending status contributions |
1401 | if ($contributionParams['contribution_status_id'] == CRM_Core_OptionGroup::getValue('contribution_status', 'Pending', 'name')) { | |
1402 | $contributionParams['is_pay_later'] = 1; | |
1403 | } | |
f8325309 | 1404 | |
aac57c29 PJ |
1405 | if ($params['status_id'] == array_search('Partially paid', $participantStatus)) { |
1406 | // CRM-13964 partial_payment_total | |
1407 | if ($amountOwed > $params['total_amount']) { | |
1408 | // the owed amount | |
1409 | $contributionParams['partial_payment_total'] = $amountOwed; | |
1410 | // the actual amount paid | |
1411 | $contributionParams['partial_amount_pay'] = $params['total_amount']; | |
1412 | } | |
f8325309 | 1413 | } |
f6bae84f PJ |
1414 | elseif ($params['status_id'] == array_search('Pending refund', $participantStatus)) { |
1415 | $totalPaid = $params['total_amount']; | |
1416 | $updatedAmt = $amountOwed; | |
1417 | } | |
6a488035 | 1418 | if ($this->_single) { |
dbd48d4e | 1419 | if (empty($ids)) { |
1420 | $ids = array(); | |
1421 | } | |
6a488035 TO |
1422 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); |
1423 | } | |
1424 | else { | |
1425 | $ids = array(); | |
1426 | foreach ($this->_contactIds as $contactID) { | |
1427 | $contributionParams['contact_id'] = $contactID; | |
1428 | $contributions[] = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | //insert payment record for this participation | |
dbd48d4e | 1433 | if (empty($ids['contribution'])) { |
6a488035 TO |
1434 | foreach ($this->_contactIds as $num => $contactID) { |
1435 | $ppDAO = new CRM_Event_DAO_ParticipantPayment(); | |
1436 | $ppDAO->participant_id = $participants[$num]->id; | |
1437 | $ppDAO->contribution_id = $contributions[$num]->id; | |
1438 | $ppDAO->save(); | |
1439 | } | |
1440 | } | |
1441 | // next create the transaction record | |
1442 | $transaction = new CRM_Core_Transaction(); | |
1443 | ||
1444 | // CRM-11124 | |
1445 | if ($this->_quickConfig) { | |
a7488080 | 1446 | if (!empty($this->_params['amount_priceset_level_radio'])) { |
7030a002 PN |
1447 | $feeLevel = $this->_params['amount_priceset_level_radio']; |
1448 | } | |
1449 | else { | |
1450 | $feeLevel[] = $this->_params['fee_level'] ; | |
1451 | } | |
1452 | CRM_Event_BAO_Participant::createDiscountTrxn($this->_eventId, $contributionParams, $feeLevel); | |
6a488035 TO |
1453 | } |
1454 | $transaction->commit(); | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | // also store lineitem stuff here | |
1459 | if ((($this->_lineItem & $this->_action & CRM_Core_Action::ADD) || | |
1460 | ($this->_lineItem && CRM_Core_Action::UPDATE && !$this->_paymentId)) | |
1461 | ) { | |
1462 | foreach ($this->_contactIds as $num => $contactID) { | |
1463 | foreach ($this->_lineItem as $key => $value) { | |
1464 | if (is_array($value) && $value != 'skip') { | |
1465 | foreach ($value as $lineKey => $line) { | |
aac57c29 | 1466 | //10117 update the line items for participants if contribution amount is recorded |
f6bae84f PJ |
1467 | if ($this->_quickConfig && !empty($params['total_amount']) && |
1468 | ($params['status_id'] != array_search('Partially paid', $participantStatus) && | |
1469 | $params['status_id'] != array_search('Pending refund', $participantStatus)) | |
aac57c29 PJ |
1470 | ) { |
1471 | $line['unit_price'] = $line['line_total'] = $params['total_amount']; | |
1472 | } | |
6a488035 TO |
1473 | $lineItem[$this->_priceSetId][$lineKey] = $line; |
1474 | } | |
1475 | CRM_Price_BAO_LineItem::processPriceSet($participants[$num]->id, $lineItem, CRM_Utils_Array::value($num, $contributions, NULL), 'civicrm_participant'); | |
1476 | } | |
1477 | } | |
1478 | } | |
1479 | } | |
1480 | ||
f6bae84f PJ |
1481 | // record adjusted trxn entry for refund case |
1482 | if ($this->_isPaidEvent && $params['status_id'] == array_search('Pending refund', $participantStatus) && | |
1483 | !empty($totalPaid) && !empty($updatedAmt)) { | |
1484 | $contributionDetail = $contributions[0]; | |
1485 | $contributionId = $contributionDetail->id; | |
1486 | CRM_Event_BAO_Participant::recordAdjustedAmt($updatedAmt, $totalPaid, $contributionId); | |
1487 | } | |
6a488035 TO |
1488 | $updateStatusMsg = NULL; |
1489 | //send mail when participant status changed, CRM-4326 | |
1490 | if ($this->_id && $this->_statusId && | |
8cc574cf | 1491 | $this->_statusId != CRM_Utils_Array::value('status_id', $params) && !empty($params['is_notify'])) { |
6a488035 TO |
1492 | |
1493 | $updateStatusMsg = CRM_Event_BAO_Participant::updateStatusMessage($this->_id, | |
1494 | $params['status_id'], | |
1495 | $this->_statusId | |
1496 | ); | |
1497 | } | |
1498 | ||
1499 | $sent = array(); | |
1500 | $notSent = array(); | |
a7488080 | 1501 | if (!empty($params['send_receipt'])) { |
6a488035 TO |
1502 | if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { |
1503 | $receiptFrom = $params['from_email_address']; | |
1504 | } | |
1505 | ||
1506 | $this->assign('module', 'Event Registration'); | |
1507 | //use of the message template below requires variables in different format | |
1508 | $event = $events = array(); | |
1509 | $returnProperties = array('fee_label', 'start_date', 'end_date', 'is_show_location', 'title'); | |
1510 | ||
1511 | //get all event details. | |
1512 | CRM_Core_DAO::commonRetrieveAll('CRM_Event_DAO_Event', 'id', $params['event_id'], $events, $returnProperties); | |
1513 | $event = $events[$params['event_id']]; | |
1514 | unset($event['start_date']); | |
1515 | unset($event['end_date']); | |
1516 | ||
1517 | $role = CRM_Event_PseudoConstant::participantRole(); | |
1518 | $participantRoles = CRM_Utils_Array::value('role_id', $params); | |
1519 | if (is_array($participantRoles)) { | |
1520 | $selectedRoles = array(); | |
1521 | foreach (array_keys($participantRoles) as $roleId) { | |
1522 | $selectedRoles[] = $role[$roleId]; | |
1523 | } | |
1524 | $event['participant_role'] = implode(', ', $selectedRoles); | |
1525 | } | |
1526 | else { | |
1527 | $event['participant_role'] = CRM_Utils_Array::value($participantRoles, $role); | |
1528 | } | |
1529 | $event['is_monetary'] = $this->_isPaidEvent; | |
1530 | ||
1531 | if ($params['receipt_text']) { | |
1532 | $event['confirm_email_text'] = $params['receipt_text']; | |
1533 | } | |
1534 | ||
1535 | $this->assign('isAmountzero', 1); | |
1536 | $this->assign('event', $event); | |
1537 | ||
1538 | $this->assign('isShowLocation', $event['is_show_location']); | |
1539 | if (CRM_Utils_Array::value('is_show_location', $event) == 1) { | |
1540 | $locationParams = array( | |
1541 | 'entity_id' => $params['event_id'], | |
1542 | 'entity_table' => 'civicrm_event', | |
1543 | ); | |
1544 | $location = CRM_Core_BAO_Location::getValues($locationParams, TRUE); | |
1545 | $this->assign('location', $location); | |
1546 | } | |
1547 | ||
1548 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1549 | if ($this->_isPaidEvent) { | |
1550 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1551 | if (!$this->_mode) { | |
1552 | if (isset($params['payment_instrument_id'])) { | |
1553 | $this->assign('paidBy', | |
1554 | CRM_Utils_Array::value($params['payment_instrument_id'], | |
1555 | $paymentInstrument | |
1556 | ) | |
1557 | ); | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | $this->assign('totalAmount', $contributionParams['total_amount']); | |
81f3d017 PJ |
1562 | if (isset($contributionParams['partial_payment_total'])) { |
1563 | // balance amount | |
1564 | $balanceAmount = $contributionParams['partial_payment_total'] - $contributionParams['partial_amount_pay']; | |
1565 | $this->assign('balanceAmount', $balanceAmount ); | |
1566 | } | |
6a488035 TO |
1567 | $this->assign('isPrimary', 1); |
1568 | $this->assign('checkNumber', CRM_Utils_Array::value('check_number', $params)); | |
1569 | } | |
1570 | if ($this->_mode) { | |
a7488080 | 1571 | if (!empty($params['billing_first_name'])) { |
6a488035 TO |
1572 | $name = $params['billing_first_name']; |
1573 | } | |
1574 | ||
a7488080 | 1575 | if (!empty($params['billing_middle_name'])) { |
6a488035 TO |
1576 | $name .= " {$params['billing_middle_name']}"; |
1577 | } | |
1578 | ||
a7488080 | 1579 | if (!empty($params['billing_last_name'])) { |
6a488035 TO |
1580 | $name .= " {$params['billing_last_name']}"; |
1581 | } | |
1582 | $this->assign('billingName', $name); | |
1583 | ||
1584 | // assign the address formatted up for display | |
1585 | $addressParts = array( | |
1586 | "street_address-{$this->_bltID}", | |
1587 | "city-{$this->_bltID}", | |
1588 | "postal_code-{$this->_bltID}", | |
1589 | "state_province-{$this->_bltID}", | |
1590 | "country-{$this->_bltID}", | |
1591 | ); | |
1592 | $addressFields = array(); | |
1593 | foreach ($addressParts as $part) { | |
1594 | list($n, $id) = explode('-', $part); | |
1595 | if (isset($this->_params['billing_' . $part])) { | |
1596 | $addressFields[$n] = $this->_params['billing_' . $part]; | |
1597 | } | |
1598 | } | |
1599 | $this->assign('address', CRM_Utils_Address::format($addressFields)); | |
1600 | ||
1601 | $date = CRM_Utils_Date::format($params['credit_card_exp_date']); | |
1602 | $date = CRM_Utils_Date::mysqlToIso($date); | |
1603 | $this->assign('credit_card_exp_date', $date); | |
1604 | $this->assign('credit_card_number', | |
1605 | CRM_Utils_System::mungeCreditCard($params['credit_card_number']) | |
1606 | ); | |
1607 | $this->assign('credit_card_type', $params['credit_card_type']); | |
1608 | $this->assign('contributeMode', 'direct'); | |
1609 | $this->assign('isAmountzero', 0); | |
1610 | $this->assign('is_pay_later', 0); | |
1611 | $this->assign('isPrimary', 1); | |
1612 | } | |
1613 | ||
1614 | $this->assign('register_date', $params['register_date']); | |
1615 | if ($params['receive_date']) { | |
1616 | $this->assign('receive_date', $params['receive_date']); | |
1617 | } | |
1618 | ||
1619 | $participant = array(array('participant_id', '=', $participants[0]->id, 0, 0)); | |
1620 | // check whether its a test drive ref CRM-3075 | |
a7488080 | 1621 | if (!empty($this->_defaultValues['is_test'])) { |
6a488035 TO |
1622 | $participant[] = array('participant_test', '=', 1, 0, 0); |
1623 | } | |
1624 | ||
1625 | $template = CRM_Core_Smarty::singleton(); | |
1626 | $customGroup = array(); | |
1627 | //format submitted data | |
1628 | foreach ($params['custom'] as $fieldID => $values) { | |
1629 | foreach ($values as $fieldValue) { | |
1630 | $customValue = array('data' => $fieldValue['value']); | |
1631 | $customFields[$fieldID]['id'] = $fieldID; | |
1632 | $formattedValue = CRM_Core_BAO_CustomGroup::formatCustomValues($customValue, $customFields[$fieldID], TRUE); | |
1633 | $customGroup[$customFields[$fieldID]['groupTitle']][$customFields[$fieldID]['label']] = str_replace(' ', '', $formattedValue); | |
1634 | } | |
1635 | } | |
1636 | ||
1637 | foreach ($this->_contactIds as $num => $contactID) { | |
1638 | // Retrieve the name and email of the contact - this will be the TO for receipt email | |
1639 | list($this->_contributorDisplayName, $this->_contributorEmail, $this->_toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($contactID); | |
1640 | ||
1641 | $this->_contributorDisplayName = ($this->_contributorDisplayName == ' ') ? $this->_contributorEmail : $this->_contributorDisplayName; | |
1642 | ||
1643 | $waitStatus = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'"); | |
1644 | if ($waitingStatus = CRM_Utils_Array::value($params['status_id'], $waitStatus)) { | |
1645 | $this->assign('isOnWaitlist', TRUE); | |
1646 | } | |
1647 | ||
1648 | $this->assign('customGroup', $customGroup); | |
1649 | $this->assign('contactID', $contactID); | |
1650 | $this->assign('participantID', $participants[$num]->id); | |
1651 | ||
1652 | $this->_id = $participants[$num]->id; | |
1653 | ||
1654 | if ($this->_isPaidEvent) { | |
1655 | // fix amount for each of participants ( for bulk mode ) | |
1656 | $eventAmount = array(); | |
1657 | if (!empty($additionalParticipantDetails)) { | |
1658 | $params['amount_level'] = preg_replace('/\ 1/', '', $params['amount_level']) . ' - ' . $this->_contributorDisplayName; | |
1659 | } | |
1660 | ||
1661 | $eventAmount[$num] = array( | |
1662 | 'label' => preg_replace('/\ 1/', '', $params['amount_level']), | |
1663 | 'amount' => $params['fee_amount'], | |
1664 | ); | |
1665 | //as we are using same template for online & offline registration. | |
1666 | //So we have to build amount as array. | |
1667 | $eventAmount = array_merge($eventAmount, $additionalParticipantDetails); | |
1668 | $this->assign('amount', $eventAmount); | |
1669 | } | |
1670 | ||
1671 | $sendTemplateParams = array( | |
1672 | 'groupName' => 'msg_tpl_workflow_event', | |
1673 | 'valueName' => 'event_offline_receipt', | |
1674 | 'contactId' => $contactID, | |
1675 | 'isTest' => (bool) CRM_Utils_Array::value('is_test', $this->_defaultValues), | |
99deba35 | 1676 | 'PDFFilename' => ts('confirmation').'.pdf', |
6a488035 TO |
1677 | ); |
1678 | ||
1679 | // try to send emails only if email id is present | |
1680 | // and the do-not-email option is not checked for that contact | |
1681 | if ($this->_contributorEmail and !$this->_toDoNotEmail) { | |
1682 | $sendTemplateParams['from'] = $receiptFrom; | |
1683 | $sendTemplateParams['toName'] = $this->_contributorDisplayName; | |
1684 | $sendTemplateParams['toEmail'] = $this->_contributorEmail; | |
1685 | $sendTemplateParams['cc'] = CRM_Utils_Array::value('cc', $this->_fromEmails); | |
1686 | $sendTemplateParams['bcc'] = CRM_Utils_Array::value('bcc', $this->_fromEmails); | |
1687 | } | |
1688 | ||
c6327d7d | 1689 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); |
6a488035 TO |
1690 | if ($mailSent) { |
1691 | $sent[] = $contactID; | |
1692 | foreach ($participants as $ids => $values) { | |
1693 | if ($values->contact_id == $contactID) { | |
1694 | CRM_Activity_BAO_Activity::addActivity($values, 'Email'); | |
1695 | break; | |
1696 | } | |
1697 | } | |
1698 | } | |
1699 | else { | |
1700 | $notSent[] = $contactID; | |
1701 | } | |
1702 | } | |
1703 | } | |
1704 | ||
1705 | // set the participant id if it is not set | |
1706 | if (!$this->_id) { | |
1707 | $this->_id = $participants[0]->id; | |
1708 | } | |
1709 | ||
1710 | if (($this->_action & CRM_Core_Action::UPDATE)) { | |
1711 | $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1712 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1713 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1', array(1 => $this->_contributorEmail)); |
1714 | } | |
1715 | ||
1716 | if ($updateStatusMsg) { | |
1717 | $statusMsg = "{$statusMsg} {$updateStatusMsg}"; | |
1718 | } | |
1719 | } | |
1720 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
1721 | if ($this->_single) { | |
1722 | $statusMsg = ts('Event registration for %1 has been added.', array(1 => $this->_contributorDisplayName)); | |
a7488080 | 1723 | if (!empty($params['send_receipt']) && count($sent)) { |
6a488035 TO |
1724 | $statusMsg .= ' ' . ts('A confirmation email has been sent to %1.', array(1 => $this->_contributorEmail)); |
1725 | } | |
1726 | } | |
1727 | else { | |
1728 | $statusMsg = ts('Total Participant(s) added to event: %1.', array(1 => count($this->_contactIds))); | |
1729 | if (count($notSent) > 0) { | |
1730 | $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))); | |
1731 | } | |
1732 | elseif (isset($params['send_receipt'])) { | |
1733 | $statusMsg .= ' ' . ts('A confirmation email has been sent to ALL participants'); | |
1734 | } | |
1735 | } | |
1736 | } | |
1737 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
1738 | ||
1739 | $buttonName = $this->controller->getButtonName(); | |
1740 | if ($this->_context == 'standalone') { | |
1741 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
1742 | $urlParams = 'reset=1&action=add&context=standalone'; | |
1743 | if ($this->_mode) { | |
1744 | $urlParams .= '&mode=' . $this->_mode; | |
1745 | } | |
1746 | if ($this->_eID) { | |
1747 | $urlParams .= '&eid=' . $this->_eID; | |
1748 | } | |
1749 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); | |
1750 | } | |
1751 | else { | |
1752 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
1753 | "reset=1&cid={$this->_contactId}&selectedChild=participant" | |
1754 | )); | |
1755 | } | |
1756 | } | |
1757 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
1758 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', | |
1759 | "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" | |
1760 | )); | |
1761 | } | |
1762 | } | |
f6bae84f | 1763 | } |