INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[civicrm-core.git] / CRM / Event / Form / Registration / Confirm.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing Event
39 *
40 */
41class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
42
43 /**
100fef9d 44 * The values for the contribution db object
6a488035
TO
45 *
46 * @var array
6a488035
TO
47 */
48 public $_values;
49
50 /**
100fef9d 51 * The total amount
6a488035
TO
52 *
53 * @var float
6a488035
TO
54 */
55 public $_totalAmount;
56
57 /**
100fef9d 58 * Set variables up before form is built
6a488035
TO
59 *
60 * @return void
6a488035 61 */
00be9182 62 public function preProcess() {
6a488035
TO
63 parent::preProcess();
64
65 // lineItem isn't set until Register postProcess
66 $this->_lineItem = $this->get('lineItem');
67
68 $this->_params = $this->get('params');
d91b8b33 69 $this->_params[0]['tax_amount'] = $this->get('tax_amount');
6a488035
TO
70
71 $this->_params[0]['is_pay_later'] = $this->get('is_pay_later');
72 $this->assign('is_pay_later', $this->_params[0]['is_pay_later']);
73 if ($this->_params[0]['is_pay_later']) {
74 $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']);
75 }
76
77 CRM_Utils_Hook::eventDiscount($this, $this->_params);
78
8cc574cf 79 if (!empty($this->_params[0]['discount']) && !empty($this->_params[0]['discount']['applied'])) {
6a488035
TO
80 $this->set('hookDiscount', $this->_params[0]['discount']);
81 $this->assign('hookDiscount', $this->_params[0]['discount']);
82 }
83
6a488035
TO
84 if ($this->_contributeMode == 'express') {
85 $params = array();
86 // rfp == redirect from paypal
87 $rfp = CRM_Utils_Request::retrieve('rfp', 'Boolean',
88 CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET'
89 );
90
91 //we lost rfp in case of additional participant. So set it explicitly.
92 if ($rfp || CRM_Utils_Array::value('additional_participants', $this->_params[0], FALSE)) {
93 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
94 $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.');
0479b4c8 95 if (is_object($payment)) {
6a488035 96 $expressParams = $payment->getExpressCheckoutDetails($this->get('token'));
0479b4c8
TO
97 }
98 else {
6a488035 99 CRM_Core_Error::fatal($paymentObjError);
0479b4c8 100 }
6a488035 101
4233eb14 102 $params['payer'] = CRM_Utils_Array::value('payer', $expressParams);
6a488035
TO
103 $params['payer_id'] = $expressParams['payer_id'];
104 $params['payer_status'] = $expressParams['payer_status'];
105
106 CRM_Core_Payment_Form::mapParams($this->_bltID, $expressParams, $params, FALSE);
107
108 // fix state and country id if present
109 if (isset($params["billing_state_province_id-{$this->_bltID}"])) {
110 $params["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["billing_state_province_id-{$this->_bltID}"]);
111 }
112 if (isset($params['billing_country_id'])) {
113 $params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["billing_country_id-{$this->_bltID}"]);
114 }
115
116 // set a few other parameters for PayPal
117 $params['token'] = $this->get('token');
118 $params['amount'] = $this->_params[0]['amount'];
a7488080 119 if (!empty($this->_params[0]['discount'])) {
6a488035
TO
120 $params['discount'] = $this->_params[0]['discount'];
121 $params['discountAmount'] = $this->_params[0]['discountAmount'];
122 $params['discountMessage'] = $this->_params[0]['discountMessage'];
123 }
4233eb14 124 if (!empty($this->_params[0]['amount_priceset_level_radio'])) {
125 $params['amount_priceset_level_radio'] = $this->_params[0]['amount_priceset_level_radio'];
126 }
6a488035
TO
127 $params['amount_level'] = $this->_params[0]['amount_level'];
128 $params['currencyID'] = $this->_params[0]['currencyID'];
129 $params['payment_action'] = 'Sale';
130
131 // also merge all the other values from the profile fields
132 $values = $this->controller->exportValues('Register');
133 $skipFields = array(
134 'amount',
135 "street_address-{$this->_bltID}",
136 "city-{$this->_bltID}",
137 "state_province_id-{$this->_bltID}",
138 "postal_code-{$this->_bltID}",
139 "country_id-{$this->_bltID}",
140 );
141
142 foreach ($values as $name => $value) {
143 // skip amount field
144 if (!in_array($name, $skipFields)) {
145 $params[$name] = $value;
146 }
147 }
148 $this->set('getExpressCheckoutDetails', $params);
149 }
150 else {
151 $params = $this->get('getExpressCheckoutDetails');
152 }
153 $this->_params[0] = $params;
154 $this->_params[0]['is_primary'] = 1;
155 }
156 else {
157 //process only primary participant params.
158 $registerParams = $this->_params[0];
159 if (isset($registerParams["billing_state_province_id-{$this->_bltID}"])
160 && $registerParams["billing_state_province_id-{$this->_bltID}"]
161 ) {
162 $registerParams["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($registerParams["billing_state_province_id-{$this->_bltID}"]);
163 }
164
165 if (isset($registerParams["billing_country_id-{$this->_bltID}"]) && $registerParams["billing_country_id-{$this->_bltID}"]) {
166 $registerParams["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($registerParams["billing_country_id-{$this->_bltID}"]);
167 }
168 if (isset($registerParams['credit_card_exp_date'])) {
169 $registerParams['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($registerParams);
170 $registerParams['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($registerParams);
171 }
172 if ($this->_values['event']['is_monetary']) {
173 $registerParams['ip_address'] = CRM_Utils_System::ipAddress();
174 $registerParams['currencyID'] = $this->_params[0]['currencyID'];
175 $registerParams['payment_action'] = 'Sale';
176 }
177 //assign back primary participant params.
178 $this->_params[0] = $registerParams;
179 }
180
181 if ($this->_values['event']['is_monetary']) {
182 $this->_params[0]['invoiceID'] = $this->get('invoiceID');
183 }
184 $this->assign('defaultRole', FALSE);
185 if (CRM_Utils_Array::value('defaultRole', $this->_params[0]) == 1) {
186 $this->assign('defaultRole', TRUE);
187 }
188
a7488080 189 if (empty($this->_params[0]['participant_role_id']) &&
6a488035
TO
190 $this->_values['event']['default_role_id']
191 ) {
192 $this->_params[0]['participant_role_id'] = $this->_values['event']['default_role_id'];
193 }
194
195 if (isset($this->_values['event']['confirm_title'])) {
196 CRM_Utils_System::setTitle($this->_values['event']['confirm_title']);
197 }
198
199 if ($this->_pcpId) {
200 $params = CRM_Contribute_Form_Contribution_Confirm::processPcp($this, $this->_params[0]);
201 $this->_params[0] = $params;
202 }
203
204 $this->set('params', $this->_params);
205 }
206
207 /**
100fef9d 208 * Overwrite action, since we are only showing elements in frozen mode
6a488035
TO
209 * no help display needed
210 *
211 * @return int
6a488035 212 */
00be9182 213 public function getAction() {
6a488035
TO
214 if ($this->_action & CRM_Core_Action::PREVIEW) {
215 return CRM_Core_Action::VIEW | CRM_Core_Action::PREVIEW;
216 }
217 else {
218 return CRM_Core_Action::VIEW;
219 }
220 }
221
222 /**
c490a46a 223 * Build the form object
6a488035 224 *
355ba699 225 * @return void
6a488035
TO
226 */
227 public function buildQuickForm() {
228 $this->assignToTemplate();
1909126f 229
230 if ($this->_values['event']['is_monetary'] &&
231 ($this->_params[0]['amount'] || $this->_params[0]['amount'] == 0)
232 ) {
6a488035
TO
233 $this->_amount = array();
234
79d001a2 235 $taxAmount = 0;
6a488035 236 foreach ($this->_params as $k => $v) {
79d001a2
PB
237 //display tax amount on confirmation page
238 $taxAmount += $v['tax_amount'];
6a488035
TO
239 if (is_array($v)) {
240 foreach (array(
241 'first_name', 'last_name') as $name) {
242 if (isset($v['billing_' . $name]) &&
243 !isset($v[$name])
244 ) {
245 $v[$name] = $v['billing_' . $name];
246 }
247 }
248
8cc574cf 249 if (!empty($v['first_name']) && !empty($v['last_name'])) {
6a488035
TO
250 $append = $v['first_name'] . ' ' . $v['last_name'];
251 }
252 else {
253 //use an email if we have one
254 foreach ($v as $v_key => $v_val) {
255 if (substr($v_key, 0, 6) == 'email-') {
256 $append = $v[$v_key];
257 }
258 }
259 }
260
261 $this->_amount[$k]['amount'] = $v['amount'];
a7488080 262 if (!empty($v['discountAmount'])) {
6a488035
TO
263 $this->_amount[$k]['amount'] -= $v['discountAmount'];
264 }
265
266 $this->_amount[$k]['label'] = preg_replace('/\ 1/', '', $v['amount_level']) . ' - ' . $append;
267 $this->_part[$k]['info'] = CRM_Utils_Array::value('first_name', $v) . ' ' . CRM_Utils_Array::value('last_name', $v);
a7488080 268 if (empty($v['first_name'])) {
6a488035
TO
269 $this->_part[$k]['info'] = $append;
270 }
271 $this->_totalAmount = $this->_totalAmount + $this->_amount[$k]['amount'];
a7488080 272 if (!empty($v['is_primary'])) {
6a488035
TO
273 $this->set('primaryParticipantAmount', $this->_amount[$k]['amount']);
274 }
275 }
276 }
277
0479b4c8 278 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
03b412ae
PB
279 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
280 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
03b412ae
PB
281 if ($invoicing) {
282 $this->assign('totalTaxAmount', $taxAmount);
d9e4ebe7 283 $this->assign('taxTerm', $taxTerm);
03b412ae 284 }
6a488035
TO
285 $this->assign('part', $this->_part);
286 $this->set('part', $this->_part);
287 $this->assign('amounts', $this->_amount);
288 $this->assign('totalAmount', $this->_totalAmount);
289 $this->set('totalAmount', $this->_totalAmount);
290 }
291
9da8dc8c 292 if ($this->_priceSetId && !CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config')) {
6a488035 293 $lineItemForTemplate = array();
03b412ae 294 $getTaxDetails = FALSE;
6a488035
TO
295 foreach ($this->_lineItem as $key => $value) {
296 if (!empty($value)) {
297 $lineItemForTemplate[$key] = $value;
298 }
03b412ae
PB
299 if ($invoicing) {
300 foreach ($value as $v) {
301 if (isset($v['tax_rate'])) {
d9e4ebe7 302 $getTaxDetails = TRUE;
03b412ae
PB
303 }
304 }
305 }
6a488035
TO
306 }
307 if (!empty($lineItemForTemplate)) {
308 $this->assign('lineItem', $lineItemForTemplate);
309 }
d9e4ebe7 310 $this->assign('getTaxDetails', $getTaxDetails);
6a488035
TO
311 }
312
313 //display additional participants profile.
5bdcb00b 314 self::assignProfiles($this);
6a488035
TO
315
316 //consider total amount.
317 $this->assign('isAmountzero', ($this->_totalAmount <= 0) ? TRUE : FALSE);
318
8cc574cf 319 if ($this->_paymentProcessor['payment_processor_type'] == 'Google_Checkout' && empty($this->_params[0]['is_pay_later']) && !($this->_params[0]['amount'] == 0) &&
6a488035
TO
320 !$this->_allowWaitlist && !$this->_requireApproval
321 ) {
322 $this->_checkoutButtonName = $this->getButtonName('next', 'checkout');
323 $this->add('image',
324 $this->_checkoutButtonName,
325 $this->_paymentProcessor['url_button'],
97e557d7 326 array('class' => 'crm-form-submit')
6a488035
TO
327 );
328
329 $this->addButtons(array(
330 array(
331 'type' => 'back',
332 'name' => ts('<< Go Back'),
333 ),
334 )
335 );
336 }
337 else {
f2ce2ad2 338 $contribButton = ts('Continue >>');
6a488035 339 $this->addButtons(array(
f2ce2ad2
J
340 array(
341 'type' => 'back',
342 'name' => ts('<< Go Back'),
343 ),
6a488035
TO
344 array(
345 'type' => 'next',
346 'name' => $contribButton,
347 'isDefault' => TRUE,
348 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"),
349 ),
6a488035
TO
350 )
351 );
352 }
353
354 $defaults = array();
355 $fields = array();
356 if (!empty($this->_fields)) {
357 foreach ($this->_fields as $name => $dontCare) {
358 $fields[$name] = 1;
359 }
360 }
361 $fields["billing_state_province-{$this->_bltID}"] = $fields["billing_country-{$this->_bltID}"] = $fields["email-{$this->_bltID}"] = 1;
362 foreach ($fields as $name => $dontCare) {
363 if (isset($this->_params[0][$name])) {
364 $defaults[$name] = $this->_params[0][$name];
365 if (substr($name, 0, 7) == 'custom_') {
366 $timeField = "{$name}_time";
367 if (isset($this->_params[0][$timeField])) {
368 $defaults[$timeField] = $this->_params[0][$timeField];
369 }
370 if (isset($this->_params[0]["{$name}_id"])) {
371 $defaults["{$name}_id"] = $this->_params[0]["{$name}_id"];
372 }
373 }
374 elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)
375 && !empty($this->_params[0][$name . '_custom'])
376 ) {
377 $defaults[$name . '_custom'] = $this->_params[0][$name . '_custom'];
378 }
379 }
380 }
381
6a488035
TO
382 $this->setDefaults($defaults);
383 $this->freeze();
384
385 //lets give meaningful status message, CRM-4320.
386 $this->assign('isOnWaitlist', $this->_allowWaitlist);
387 $this->assign('isRequireApproval', $this->_requireApproval);
388
389 // Assign Participant Count to Lineitem Table
9da8dc8c 390 $this->assign('pricesetFieldsCount', CRM_Price_BAO_PriceSet::getPricesetCount($this->_priceSetId));
79b152ac 391 $this->addFormRule(array('CRM_Event_Form_Registration_Confirm', 'formRule'), $this);
6a488035
TO
392 }
393
00be9182 394 public static function formRule($fields, $files, $self) {
79b152ac 395 $errors = array();
396 $eventFull = CRM_Event_BAO_Participant::eventFull($self->_eventId, FALSE, CRM_Utils_Array::value('has_waitlist', $self->_values['event']));
397 if ($eventFull && empty($self->_allowConfirmation)) {
398 if (empty($self->_allowWaitlist)) {
0479b4c8 399 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "reset=1&id={$self->_eventId}", FALSE, NULL, FALSE, TRUE));
79b152ac 400 }
401 }
402 $self->_feeBlock = $self->_values['fee'];
403 CRM_Event_Form_Registration_Register::formatFieldsForOptionFull($self);
404
405 if (!empty($self->_priceSetId)) {
406 $priceSetErrors = self::validatePriceSet($self, $self->_params);
407 //get price set fields errors in.
408 $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, array()));
409 }
410
411 if (!empty($errors)) {
412 $soldOutOptions = implode("<br>", $priceSetErrors['soldOutOptions']);
0479b4c8
TO
413 CRM_Core_Session::setStatus(ts('You have been returned to the start of the registration process and any sold out events have been removed from your selections. You will not be able to continue until you review your booking and select different events if you wish. The following events were sold out:'), ts('Unfortunately some of your options have now sold out for one or more participants.'), 'error');
414 CRM_Core_Session::setStatus(ts("{$soldOutOptions}"), ts('Sold out:'), 'error');
79b152ac 415 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "_qf_Register_display=true&qfKey=" . $fields['qfKey']));
416 }
417 return empty($errors) ? TRUE : $errors;
418 }
6a488035 419 /**
c490a46a 420 * Process the form submission
6a488035 421 *
6a488035 422 *
355ba699 423 * @return void
6a488035
TO
424 */
425 public function postProcess() {
426 $now = date('YmdHis');
af72b8c7 427
6a488035 428 $this->_params = $this->get('params');
a7488080 429 if (!empty($this->_params[0]['contact_id'])) {
e1ce628e 430 // unclear when this would be set & whether it could be checked in getContactID.
431 // perhaps it relates to when cid is in the url
432 //@todo someone who knows add comments on the various contactIDs in this form
6a488035
TO
433 $contactID = $this->_params[0]['contact_id'];
434 }
435 else {
5c280496 436 $contactID = $this->getContactID();
6a488035
TO
437 }
438
439 // if a discount has been applied, lets now deduct it from the amount
440 // and fix the fee level
8cc574cf 441 if (!empty($this->_params[0]['discount']) && !empty($this->_params[0]['discount']['applied'])) {
6a488035 442 foreach ($this->_params as $k => $v) {
8cc574cf 443 if (CRM_Utils_Array::value('amount', $this->_params[$k]) > 0 && !empty($this->_params[$k]['discountAmount'])) {
6a488035
TO
444 $this->_params[$k]['amount'] -= $this->_params[$k]['discountAmount'];
445 $this->_params[$k]['amount_level'] .= CRM_Utils_Array::value('discountMessage', $this->_params[$k]);
446 }
447 }
448 $this->set('params', $this->_params);
449 }
450
451 // CRM-4320, lets build array of cancelled additional participant ids
452 // those are drop or skip by primary at the time of confirmation.
453 // get all in and then unset those we want to process.
454 $cancelledIds = $this->_additionalParticipantIds;
455
456 $params = $this->_params;
1909126f 457 if ($this->_values['event']['is_monetary']) {
458 $this->set('finalAmount', $this->_amount);
459 }
6a488035
TO
460 $participantCount = array();
461
462 //unset the skip participant from params.
463 //build the $participantCount array.
464 //maintain record for all participants.
465 foreach ($params as $participantNum => $record) {
466 if ($record == 'skip') {
467 unset($params[$participantNum]);
468 $participantCount[$participantNum] = 'skip';
469 }
470 elseif ($participantNum) {
471 $participantCount[$participantNum] = 'participant';
472 }
473
474 //lets get additional participant id to cancel.
475 if ($this->_allowConfirmation && is_array($cancelledIds)) {
476 $additonalId = CRM_Utils_Array::value('participant_id', $record);
477 if ($additonalId && $key = array_search($additonalId, $cancelledIds)) {
478 unset($cancelledIds[$key]);
479 }
480 }
481 }
482
483 $payment = $registerByID = $primaryCurrencyID = $contribution = NULL;
484 $paymentObjError = ts('The system did not record payment details for this payment and so could not process the transaction. Please report this error to the site administrator.');
485
486 $this->participantIDS = array();
487 $fields = array();
488 foreach ($params as $key => $value) {
a9f7d48b 489 CRM_Event_Form_Registration_Confirm::fixLocationFields($value, $fields, $this);
6a488035
TO
490 //unset the billing parameters if it is pay later mode
491 //to avoid creation of billing location
8ae4d0d3 492 if ($this->_allowWaitlist
493 || $this->_requireApproval
494 || (!empty($value['is_pay_later']) && !$this->_isBillingAddressRequiredForPayLater)
495 || empty($value['is_primary'])
496 ) {
6a488035
TO
497 $billingFields = array(
498 "email-{$this->_bltID}",
499 'billing_first_name',
500 'billing_middle_name',
501 'billing_last_name',
502 "billing_street_address-{$this->_bltID}",
503 "billing_city-{$this->_bltID}",
504 "billing_state_province-{$this->_bltID}",
505 "billing_state_province_id-{$this->_bltID}",
506 "billing_postal_code-{$this->_bltID}",
507 "billing_country-{$this->_bltID}",
508 "billing_country_id-{$this->_bltID}",
509 "address_name-{$this->_bltID}",
510 );
511 foreach ($billingFields as $field) {
512 unset($value[$field]);
513 }
a7488080 514 if (!empty($value['is_pay_later'])) {
6a488035
TO
515 $this->_values['params']['is_pay_later'] = TRUE;
516 }
517 }
518
519 //Unset ContactID for additional participants and set RegisterBy Id.
a7488080 520 if (empty($value['is_primary'])) {
6a488035
TO
521 $contactID = CRM_Utils_Array::value('contact_id', $value);
522 $registerByID = $this->get('registerByID');
523 if ($registerByID) {
524 $value['registered_by_id'] = $registerByID;
525 }
526 }
527 else {
528 $value['amount'] = $this->_totalAmount;
529 }
530
a9f7d48b 531 $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields, $this);
6a488035
TO
532
533 // lets store the contactID in the session
534 // we dont store in userID in case the user is doing multiple
535 // transactions etc
536 // for things like tell a friend
8cc574cf 537 if (!$this->getContactID() && !empty($value['is_primary'])) {
af72b8c7 538 $session = CRM_Core_Session::singleton();
6a488035
TO
539 $session->set('transaction.userID', $contactID);
540 }
541
542 $value['description'] = ts('Online Event Registration') . ': ' . $this->_values['event']['title'];
543 $value['accountingCode'] = CRM_Utils_Array::value('accountingCode',
544 $this->_values['event']
545 );
546
547 // required only if paid event
548 if ($this->_values['event']['is_monetary']) {
549 if (is_array($this->_paymentProcessor)) {
550 $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this);
551 }
552 $pending = FALSE;
553 $result = NULL;
554
555 if ($this->_allowWaitlist || $this->_requireApproval) {
556 //get the participant statuses.
557 $waitingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Waiting'");
558 if ($this->_allowWaitlist) {
559 $value['participant_status_id'] = $value['participant_status'] = array_search('On waitlist', $waitingStatuses);
560 }
561 else {
562 $value['participant_status_id'] = $value['participant_status'] = array_search('Awaiting approval', $waitingStatuses);
563 }
564
565 //there might be case user seleted pay later and
566 //now becomes part of run time waiting list.
567 $value['is_pay_later'] = FALSE;
568 }
a7488080 569 elseif (!empty($value['is_pay_later']) ||
6a488035
TO
570 $value['amount'] == 0 ||
571 $this->_contributeMode == 'checkout' ||
572 $this->_contributeMode == 'notify'
573 ) {
574 if ($value['amount'] != 0) {
575 $pending = TRUE;
576 //get the participant statuses.
577 $pendingStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'");
0d8afee2 578 $status = !empty($value['is_pay_later']) ? 'Pending from pay later' : 'Pending from incomplete transaction';
6a488035
TO
579 $value['participant_status_id'] = $value['participant_status'] = array_search($status, $pendingStatuses);
580 }
581 }
8cc574cf 582 elseif ($this->_contributeMode == 'express' && !empty($value['is_primary'])) {
0479b4c8 583 if (is_object($payment)) {
6a488035 584 $result = $payment->doExpressCheckout($value);
0479b4c8
TO
585 }
586 else {
6a488035 587 CRM_Core_Error::fatal($paymentObjError);
0479b4c8 588 }
6a488035 589 }
a7488080 590 elseif (!empty($value['is_primary'])) {
6a488035 591 CRM_Core_Payment_Form::mapParams($this->_bltID, $value, $value, TRUE);
bd7b39e7
PJ
592 // payment email param can be empty for _bltID mapping
593 // thus provide mapping for it with a different email value
594 if (empty($value['email'])) {
595 $value['email'] = CRM_Utils_Array::valueByRegexKey('/^email-/', $value);
596 }
597
6a488035
TO
598 if (is_object($payment)) {
599 $result = $payment->doDirectPayment($value);
600 }
601 else {
602 CRM_Core_Error::fatal($paymentObjError);
603 }
604 }
605
606 if (is_a($result, 'CRM_Core_Error')) {
607 CRM_Core_Error::displaySessionError($result);
608 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "id={$this->_eventId}"));
609 }
610
611 if ($result) {
612 $value = array_merge($value, $result);
613 }
614
615 $value['receive_date'] = $now;
616 if ($this->_allowConfirmation) {
617 $value['participant_register_date'] = $this->_values['participant']['register_date'];
618 }
619
620 $createContrib = ($value['amount'] != 0) ? TRUE : FALSE;
621 // force to create zero amount contribution, CRM-5095
622 if (!$createContrib && ($value['amount'] == 0)
623 && $this->_priceSetId && $this->_lineItem
624 ) {
625 $createContrib = TRUE;
626 }
627
8cc574cf 628 if ($createContrib && !empty($value['is_primary']) &&
6a488035
TO
629 !$this->_allowWaitlist && !$this->_requireApproval
630 ) {
631 // if paid event add a contribution record
632 //if primary participant contributing additional amount
633 //append (multiple participants) to its fee level. CRM-4196.
634 $isAdditionalAmount = FALSE;
635 if (count($params) > 1) {
636 $isAdditionalAmount = TRUE;
637 }
638
639 //passing contribution id is already registered.
640 $contribution =
641 self::processContribution($this, $value, $result, $contactID, $pending, $isAdditionalAmount);
642 $value['contributionID'] = $contribution->id;
643 $value['contributionTypeID'] = $contribution->financial_type_id;
644 $value['receive_date'] = $contribution->receive_date;
645 $value['trxn_id'] = $contribution->trxn_id;
646 $value['contributionID'] = $contribution->id;
647 $value['contributionTypeID'] = $contribution->financial_type_id;
648 }
649 $value['contactID'] = $contactID;
650 $value['eventID'] = $this->_eventId;
651 $value['item_name'] = $value['description'];
652 }
9a32a8fc
GC
653
654 if (!empty($value['contributionID'])) {
655 $this->_values['contributionId'] = $value['contributionID'];
656 }
6a488035
TO
657
658 //CRM-4453.
a7488080 659 if (!empty($value['is_primary'])) {
6a488035
TO
660 $primaryCurrencyID = CRM_Utils_Array::value('currencyID', $value);
661 }
a7488080 662 if (empty($value['currencyID'])) {
6a488035
TO
663 $value['currencyID'] = $primaryCurrencyID;
664 }
665
1909126f 666 // CRM-11182 - Confirmation page might not be monetary
667 if ($this->_values['event']['is_monetary']) {
668 if (!$pending && !empty($value['is_primary']) &&
669 !$this->_allowWaitlist && !$this->_requireApproval
670 ) {
671 // transactionID & receive date required while building email template
672 $this->assign('trxn_id', $value['trxn_id']);
673 $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($value['receive_date']));
674 $this->set('receiveDate', CRM_Utils_Date::mysqlToIso($value['receive_date']));
675 $this->set('trxnId', CRM_Utils_Array::value('trxn_id', $value));
676 }
6a488035 677 }
f55dc004 678
ffcec972 679 $value['fee_amount'] = CRM_Utils_Array::value('amount', $value);
6a488035
TO
680 $this->set('value', $value);
681
682 // handle register date CRM-4320
683 if ($this->_allowConfirmation) {
481a74f4 684 $registerDate = CRM_Utils_Array::value('participant_register_date', $params);
6a488035 685 }
a7488080 686 elseif (!empty($params['participant_register_date']) &&
6a488035
TO
687 is_array($params['participant_register_date']) &&
688 !empty($params['participant_register_date'])
689 ) {
690 $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
691 }
692 else {
693 $registerDate = date('YmdHis');
694 }
695 $this->assign('register_date', $registerDate);
696
697 $this->confirmPostProcess($contactID, $contribution, $payment);
698 }
699
700 //handle if no additional participant.
701 if (!$registerByID) {
702 $registerByID = $this->get('registerByID');
703 }
704
705 $this->set('participantIDs', $this->_participantIDS);
706
707 // create line items, CRM-5313
708 if ($this->_priceSetId &&
709 !empty($this->_lineItem)
710 ) {
711 // take all processed participant ids.
712 $allParticipantIds = $this->_participantIDS;
713
714 // when participant re-walk wizard.
715 if ($this->_allowConfirmation &&
716 !empty($this->_additionalParticipantIds)
717 ) {
718 $allParticipantIds = array_merge(array($registerByID), $this->_additionalParticipantIds);
719 }
720
721 $entityTable = 'civicrm_participant';
0479b4c8 722 $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
03b412ae 723 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
79d001a2
PB
724 $totalTaxAmount = 0;
725 $dataArray = array();
6a488035
TO
726 foreach ($this->_lineItem as $key => $value) {
727 if (($value != 'skip') &&
728 ($entityId = CRM_Utils_Array::value($key, $allParticipantIds))
729 ) {
730
731 // do cleanup line items if participant re-walking wizard.
732 if ($this->_allowConfirmation) {
733 CRM_Price_BAO_LineItem::deleteLineItems($entityId, $entityTable);
734 }
735 $lineItem[$this->_priceSetId] = $value;
736 CRM_Price_BAO_LineItem::processPriceSet($entityId, $lineItem, $contribution, $entityTable);
737 }
03b412ae
PB
738 if ($invoicing) {
739 foreach ($value as $line) {
740 if (isset($line['tax_amount']) && isset($line['tax_rate'])) {
741 $totalTaxAmount = $line['tax_amount'] + $totalTaxAmount;
742 if (isset($dataArray[$line['tax_rate']])) {
743 $dataArray[$line['tax_rate']] = $dataArray[$line['tax_rate']] + CRM_Utils_Array::value('tax_amount', $line);
744 }
745 else {
746 $dataArray[$line['tax_rate']] = CRM_Utils_Array::value('tax_amount', $line);
747 }
79d001a2
PB
748 }
749 }
750 }
6a488035 751 }
03b412ae
PB
752 if ($invoicing) {
753 $this->assign('dataArray', $dataArray);
754 $this->assign('totalTaxAmount', $totalTaxAmount);
755 }
6a488035
TO
756 }
757
758 //update status and send mail to cancelled additonal participants, CRM-4320
759 if ($this->_allowConfirmation && is_array($cancelledIds) && !empty($cancelledIds)) {
760 $cancelledId = array_search('Cancelled',
761 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'")
762 );
763 CRM_Event_BAO_Participant::transitionParticipants($cancelledIds, $cancelledId);
764 }
765
766 $isTest = FALSE;
767 if ($this->_action & CRM_Core_Action::PREVIEW) {
768 $isTest = TRUE;
769 }
770
771 // for Transfer checkout.
772 if (($this->_contributeMode == 'checkout' ||
773 $this->_contributeMode == 'notify'
8cc574cf 774 ) && empty($params[0]['is_pay_later']) &&
6a488035
TO
775 !$this->_allowWaitlist && !$this->_requireApproval &&
776 $this->_totalAmount > 0
777 ) {
778
779 $primaryParticipant = $this->get('primaryParticipant');
780
a7488080 781 if (empty($primaryParticipant['participantID'])) {
6a488035
TO
782 $primaryParticipant['participantID'] = $registerByID;
783 }
784
785 //build an array of custom profile and assigning it to template
786 $customProfile = CRM_Event_BAO_Event::buildCustomProfile($registerByID, $this->_values, NULL, $isTest);
787 if (count($customProfile)) {
788 $this->assign('customProfile', $customProfile);
789 $this->set('customProfile', $customProfile);
790 }
791
792 // do a transfer only if a monetary payment greater than 0
793 if ($this->_values['event']['is_monetary'] && $primaryParticipant) {
794 if ($payment && is_object($payment)) {
03665663 795 //CRM 14512 provide line items of all participants to payment gateway
796 $primaryContactId = $this->get('primaryContactId');
fb3b1f9a 797
03665663 798 //build an array of cId/pId of participants
799 $additionalIDs = CRM_Event_BAO_Event::buildCustomProfile($registerByID, NULL, $primaryContactId, $isTest, TRUE);
800
801 //need to copy, since we are unsetting on the way.
802 $copyParticipantCountLines = $participantCount;
803
804 //lets carry all participant params w/ values.
805 foreach ($additionalIDs as $participantID => $contactId) {
806 $participantNum = NULL;
807 $participantNum = $participantID;
808 if ($participantID == $registerByID) {
809 $participantNum = 0; // is primary particpant
810 }
811 else {
812 if ($participantNum = array_search('participant', $copyParticipantCountLines)) {
813 //if no participant found break.
814 if ($participantNum === NULL) {
815 break;
816 }
817 //unset current particpant so we don't check them again
818 unset($copyParticipantCountLines[$participantNum]);
819 }
820 }
821 // get values of line items
822 if ($this->_amount) {
823 $amount = array();
824 $amount[$participantNum]['label'] = preg_replace('/\ 1/', '', $params[$participantNum]['amount_level']);
825 $amount[$participantNum]['amount'] = $params[$participantNum]['amount'];
826 $params[$participantNum]['amounts'] = $amount;
827 }
828
829 if (!empty($this->_lineItem)) {
830 $lineItems = $this->_lineItem;
831 $lineItem = array();
832 if ($lineItemValue = CRM_Utils_Array::value($participantNum, $lineItems)) {
833 $lineItem[] = $lineItemValue;
834 }
835 $params[$participantNum]['lineItem'] = $lineItem;
836 }
837
838 //only add additional particpants and not the primary particpant as we already have that
839 //added to $primaryParticipant so that this change doesn't break or require changes to
840 //existing gateway implementations
841 $primaryParticipant['participants_info'][$participantID] = $params[$participantNum];
842 }
843
844 //get event custom field information
845 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Event', $this, $this->_eventId, 0, $this->_values['event']['event_type_id']);
846 $primaryParticipant['eventCustomFields'] = $groupTree;
fb3b1f9a 847
6a488035
TO
848 // call postprocess hook before leaving
849 $this->postProcessHook();
850 // this does not return
851 $payment->doTransferCheckout($primaryParticipant, 'event');
852 }
853 else {
854 CRM_Core_Error::fatal($paymentObjError);
855 }
856 }
857 }
858 else {
859 //otherwise send mail Confirmation/Receipt
860 $primaryContactId = $this->get('primaryContactId');
861
862 //build an array of cId/pId of participants
863 $additionalIDs = CRM_Event_BAO_Event::buildCustomProfile($registerByID,
864 NULL, $primaryContactId, $isTest,
865 TRUE
866 );
867 //lets send mails to all with meaningful text, CRM-4320.
868 $this->assign('isOnWaitlist', $this->_allowWaitlist);
869 $this->assign('isRequireApproval', $this->_requireApproval);
870
871 //need to copy, since we are unsetting on the way.
872 $copyParticipantCount = $participantCount;
873
874 //lets carry all paticipant params w/ values.
875 foreach ($additionalIDs as $participantID => $contactId) {
876 $participantNum = NULL;
877 if ($participantID == $registerByID) {
878 $participantNum = 0;
879 }
880 else {
881 if ($participantNum = array_search('participant', $copyParticipantCount)) {
882 unset($copyParticipantCount[$participantNum]);
883 }
884 }
0479b4c8
TO
885 if ($participantNum === NULL) {
886 break;
887 }
6a488035
TO
888
889 //carry the participant submitted values.
890 $this->_values['params'][$participantID] = $params[$participantNum];
891 }
892
893 foreach ($additionalIDs as $participantID => $contactId) {
894 $participantNum = 0;
895 if ($participantID == $registerByID) {
896 //set as Primary Participant
897 $this->assign('isPrimary', 1);
898 //build an array of custom profile and assigning it to template.
899 $customProfile = CRM_Event_BAO_Event::buildCustomProfile($participantID, $this->_values, NULL, $isTest);
900
901 if (count($customProfile)) {
902 $this->assign('customProfile', $customProfile);
903 $this->set('customProfile', $customProfile);
904 }
905 $this->_values['params']['additionalParticipant'] = FALSE;
906 }
907 else {
908 //take the Additional participant number.
909 if ($participantNum = array_search('participant', $participantCount)) {
910 unset($participantCount[$participantNum]);
911 }
912 $this->assign('isPrimary', 0);
913 $this->assign('customProfile', NULL);
914 //Additional Participant should get only it's payment information
1909126f 915 if (!empty($this->_amount)) {
6a488035
TO
916 $amount = array();
917 $params = $this->get('params');
918 $amount[$participantNum]['label'] = preg_replace('/\ 1/', '', $params[$participantNum]['amount_level']);
919 $amount[$participantNum]['amount'] = $params[$participantNum]['amount'];
920 $this->assign('amounts', $amount);
921 }
922 if ($this->_lineItem) {
923 $lineItems = $this->_lineItem;
924 $lineItem = array();
925 if ($lineItemValue = CRM_Utils_Array::value($participantNum, $lineItems)) {
926 $lineItem[] = $lineItemValue;
927 }
928 $this->assign('lineItem', $lineItem);
929 }
930 $this->_values['params']['additionalParticipant'] = TRUE;
7d92d486 931 $this->assign('isAdditionalParticipant', $this->_values['params']['additionalParticipant']);
6a488035
TO
932 }
933
934 //pass these variables since these are run time calculated.
935 $this->_values['params']['isOnWaitlist'] = $this->_allowWaitlist;
936 $this->_values['params']['isRequireApproval'] = $this->_requireApproval;
937
938 //send mail to primary as well as additional participants.
939 $this->assign('contactID', $contactId);
940 $this->assign('participantID', $participantID);
941 CRM_Event_BAO_Event::sendMail($contactId, $this->_values, $participantID, $isTest);
942 }
943 }
944 }
6a488035
TO
945
946 /**
947 * Process the contribution
948 *
c490a46a
CW
949 * @param CRM_Core_Form $form
950 * @param array $params
dd244018 951 * @param $result
100fef9d 952 * @param int $contactID
dd244018
EM
953 * @param bool $pending
954 * @param bool $isAdditionalAmount
955 *
6a488035 956 * @return void
6a488035 957 */
ddca8f33
TO
958 static function processContribution(
959 &$form, $params, $result, $contactID,
6a488035
TO
960 $pending = FALSE, $isAdditionalAmount = FALSE
961 ) {
962 $transaction = new CRM_Core_Transaction();
963
6a488035
TO
964 $now = date('YmdHis');
965 $receiptDate = NULL;
966
bf2c70af 967 if (!empty($form->_values['event']['is_email_confirm'])) {
6a488035
TO
968 $receiptDate = $now;
969 }
970 //CRM-4196
971 if ($isAdditionalAmount) {
972 $params['amount_level'] = $params['amount_level'] . ts(' (multiple participants)') . CRM_Core_DAO::VALUE_SEPARATOR;
973 }
974
975 $contribParams = array(
976 'contact_id' => $contactID,
0479b4c8 977 'financial_type_id' => !empty($form->_values['event']['financial_type_id']) ? $form->_values['event']['financial_type_id'] : $params['financial_type_id'],
6a488035
TO
978 'receive_date' => $now,
979 'total_amount' => $params['amount'],
d91b8b33 980 'tax_amount' => $params['tax_amount'],
6a488035
TO
981 'amount_level' => $params['amount_level'],
982 'invoice_id' => $params['invoiceID'],
983 'currency' => $params['currencyID'],
13bb29e4 984 'source' => !empty($params['participant_source']) ? $params['participant_source'] : $params['description'],
6a488035
TO
985 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
986 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
987 );
988
a7488080 989 if (empty($params['is_pay_later'])) {
6a488035
TO
990 $contribParams['payment_instrument_id'] = 1;
991 }
992
993 if (!$pending && $result) {
994 $contribParams += array(
995 'fee_amount' => CRM_Utils_Array::value('fee_amount', $result),
996 'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']),
997 'trxn_id' => $result['trxn_id'],
998 'receipt_date' => $receiptDate,
999 );
1000 }
1001
1002 $allStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
1003 $contribParams['contribution_status_id'] = array_search('Completed', $allStatuses);
1004 if ($pending) {
1005 $contribParams['contribution_status_id'] = array_search('Pending', $allStatuses);
1006 }
1007
1008 $contribParams['is_test'] = 0;
1009 if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
1010 $contribParams['is_test'] = 1;
1011 }
1012
1013 $contribID = NULL;
a7488080 1014 if (!empty($contribParams['invoice_id'])) {
6a488035
TO
1015 $contribID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
1016 $contribParams['invoice_id'],
1017 'id',
1018 'invoice_id'
1019 );
1020 }
1021
1022 $ids = array();
1023 if ($contribID) {
1024 $ids['contribution'] = $contribID;
1025 $contribParams['id'] = $contribID;
1026 }
1027
1028 //create an contribution address
8cc574cf 1029 if ($form->_contributeMode != 'notify' && empty($params['is_pay_later'])) {
6a488035
TO
1030 $contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID);
1031 }
1032
1033 // Prepare soft contribution due to pcp or Submit Credit / Debit Card Contribution by admin.
8cc574cf 1034 if (!empty($params['pcp_made_through_id']) || !empty($params['soft_credit_to'])) {
6a488035
TO
1035
1036 // if its due to pcp
a7488080 1037 if (!empty($params['pcp_made_through_id'])) {
6a488035
TO
1038 $contribSoftContactId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
1039 $params['pcp_made_through_id'],
1040 'contact_id'
1041 );
1042 }
1043 else {
1044 $contribSoftContactId = CRM_Utils_Array::value('soft_credit_to', $params);
1045 }
1046
1047 // Pass these details onto with the contribution to make them
1048 // available at hook_post_process, CRM-8908
1049 $contribParams['soft_credit_to'] = $params['soft_credit_to'] = $contribSoftContactId;
1050 }
1051 $contribParams['payment_processor'] = CRM_Utils_Array::value('payment_processor', $params);
1052 $contribParams['skipLineItem'] = 1;
1053 // create contribution record
1054 $contribution = CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
1055 // CRM-11124
b03a5fb5 1056 CRM_Event_BAO_Participant::createDiscountTrxn($form->_eventId, $contribParams, CRM_Utils_Array::value('amount_priceset_level_radio', $params, NULL));
6a488035
TO
1057
1058 // process soft credit / pcp pages
1059 CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution);
1060
1061 $transaction->commit();
1062
1063 return $contribution;
1064 }
1065
1066 /**
1067 * Fix the Location Fields
1068 *
c490a46a 1069 * @param array $params
dd244018 1070 * @param $fields
c490a46a 1071 * @param CRM_Core_Form $form
dd244018 1072 *
6a488035 1073 * @return void
6a488035 1074 */
4fb756af 1075 public static function fixLocationFields(&$params, &$fields, &$form) {
a9f7d48b
AS
1076 if (!empty($form->_fields)) {
1077 foreach ($form->_fields as $name => $dontCare) {
6a488035
TO
1078 $fields[$name] = 1;
1079 }
1080 }
1081
1082 if (is_array($fields)) {
1083 if (!array_key_exists('first_name', $fields)) {
1084 $nameFields = array('first_name', 'middle_name', 'last_name');
1085 foreach ($nameFields as $name) {
1086 $fields[$name] = 1;
1087 if (array_key_exists("billing_$name", $params)) {
1088 $params[$name] = $params["billing_{$name}"];
1089 $params['preserveDBName'] = TRUE;
1090 }
1091 }
1092 }
1093 }
1094
1095 // also add location name to the array
a9f7d48b
AS
1096 if ($form->_values['event']['is_monetary']) {
1097 $params["address_name-{$form->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $params) . ' ' . CRM_Utils_Array::value('billing_last_name', $params);
1098 $fields["address_name-{$form->_bltID}"] = 1;
6a488035 1099 }
a9f7d48b 1100 $fields["email-{$form->_bltID}"] = 1;
6a488035
TO
1101 $fields['email-Primary'] = 1;
1102
1103 //if its pay later or additional participant set email address as primary.
8cc574cf 1104 if ((!empty($params['is_pay_later']) || empty($params['is_primary']) ||
a9f7d48b
AS
1105 !$form->_values['event']['is_monetary'] ||
1106 $form->_allowWaitlist ||
1107 $form->_requireApproval
1108 ) && !empty($params["email-{$form->_bltID}"])) {
1109 $params['email-Primary'] = $params["email-{$form->_bltID}"];
6a488035
TO
1110 }
1111 }
1112
1113 /**
100fef9d 1114 * Update contact fields
6a488035 1115 *
100fef9d 1116 * @param int $contactID
c490a46a 1117 * @param array $params
dd244018 1118 * @param $fields
c490a46a 1119 * @param CRM_Core_Form $form
dd244018 1120 *
6a488035 1121 * @return void
6a488035 1122 */
4fb756af 1123 public static function updateContactFields($contactID, $params, $fields, &$form) {
6a488035
TO
1124 //add the contact to group, if add to group is selected for a
1125 //particular uf group
1126
1127 // get the add to groups
1128 $addToGroups = array();
1129
a9f7d48b
AS
1130 if (!empty($form->_fields)) {
1131 foreach ($form->_fields as $key => $value) {
a7488080 1132 if (!empty($value['add_to_group_id'])) {
6a488035
TO
1133 $addToGroups[$value['add_to_group_id']] = $value['add_to_group_id'];
1134 }
1135 }
1136 }
1137
1138 // check for profile double opt-in and get groups to be subscribed
1139 $subscribeGroupIds = CRM_Core_BAO_UFGroup::getDoubleOptInGroupIds($params, $contactID);
1140
1141 foreach ($addToGroups as $k) {
1142 if (array_key_exists($k, $subscribeGroupIds)) {
1143 unset($addToGroups[$k]);
1144 }
1145 }
1146
1147 // since we are directly adding contact to group lets unset it from mailing
1148 if (!empty($addToGroups)) {
1149 foreach ($addToGroups as $groupId) {
1150 if (isset($subscribeGroupIds[$groupId])) {
1151 unset($subscribeGroupIds[$groupId]);
1152 }
1153 }
1154 }
1155 if ($contactID) {
1156 $ctype = CRM_Core_DAO::getFieldValue(
1157 'CRM_Contact_DAO_Contact',
1158 $contactID,
1159 'contact_type'
1160 );
e1ce628e 1161
22e263ad 1162 if (array_key_exists('contact_id', $params) && empty($params['contact_id'])) {
e1ce628e 1163 // we unset this here because the downstream function ignores the contactID we give it
1164 // if it is set & it is difficult to understand the implications of 'fixing' this downstream
1165 // but if we are passing a contact id into this function it's reasonable to assume we don't
1166 // want it ignored
1167 unset($params['contact_id']);
1168 }
1169
6a488035
TO
1170 $contactID = CRM_Contact_BAO_Contact::createProfileContact(
1171 $params,
1172 $fields,
1173 $contactID,
1174 $addToGroups,
1175 NULL,
1176 $ctype,
1177 TRUE
1178 );
1179 }
1180 else {
1181
1182 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
1183 if (!isset($params[$greeting . '_id'])) {
1184 $params[$greeting . '_id'] = CRM_Contact_BAO_Contact_Utils::defaultGreeting('Individual', $greeting);
1185 }
1186 }
1187
1188 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params,
1189 $fields,
1190 NULL,
1191 $addToGroups,
1192 NULL,
1193 NULL,
1194 TRUE
1195 );
a9f7d48b 1196 $form->set('contactID', $contactID);
6a488035
TO
1197 }
1198
1199 //get email primary first if exist
1200 $subscribtionEmail = array('email' => CRM_Utils_Array::value('email-Primary', $params));
1201 if (!$subscribtionEmail['email']) {
a9f7d48b 1202 $subscribtionEmail['email'] = CRM_Utils_Array::value("email-{$form->_bltID}", $params);
6a488035
TO
1203 }
1204 // subscribing contact to groups
1205 if (!empty($subscribeGroupIds) && $subscribtionEmail['email']) {
1206 CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($subscribeGroupIds, $subscribtionEmail, $contactID);
1207 }
1208
1209 return $contactID;
1210 }
5bdcb00b 1211
0cf587a7
EM
1212 /**
1213 * @param $form
1214 */
5bdcb00b 1215 public static function assignProfiles(&$form) {
5bdcb00b
PJ
1216 $participantParams = $form->_params;
1217 $formattedValues = $profileFields = array();
1218 $count = 1;
1219 foreach ($participantParams as $participantNum => $participantValue) {
1220 if ($participantNum) {
0479b4c8
TO
1221 $prefix1 = 'additional';
1222 $prefix2 = 'additional_';
0db6c3e1
TO
1223 }
1224 else {
0479b4c8
TO
1225 $prefix1 = '';
1226 $prefix2 = '';
5bdcb00b
PJ
1227 }
1228 if ($participantValue != 'skip') {
1229 //get the customPre profile info
1230 if (!empty($form->_values[$prefix2 . 'custom_pre_id'])) {
1231 $values = $groupName = array();
1232 CRM_Event_BAO_Event::displayProfile($participantValue,
0479b4c8 1233 $form->_values[$prefix2 . 'custom_pre_id'],
5bdcb00b
PJ
1234 $groupName,
1235 $values,
1236 $profileFields
1237 );
1238
1239 if (count($values)) {
1240 $formattedValues[$count][$prefix1 . 'CustomPre'] = $values;
1241 }
1242 $formattedValues[$count][$prefix1 . 'CustomPreGroupTitle'] = CRM_Utils_Array::value('groupTitle', $groupName);
1243 }
1244 //get the customPost profile info
1245 if (!empty($form->_values[$prefix2 . 'custom_post_id'])) {
1246 $values = $groupName = array();
1247 foreach ($form->_values[$prefix2 . 'custom_post_id'] as $gids) {
1248 $val = array();
1249 CRM_Event_BAO_Event::displayProfile($participantValue,
1250 $gids,
1251 $group,
1252 $val,
1253 $profileFields
1254 );
1255 $values[$gids] = $val;
1256 $groupName[$gids] = $group;
1257 }
1258
1259 if (count($values)) {
1260 $formattedValues[$count][$prefix1 . 'CustomPost'] = $values;
1261 }
1262
1263 if (isset($formattedValues[$count][$prefix1 . 'CustomPre'])) {
1264 $formattedValues[$count][$prefix1 . 'CustomPost'] = array_diff_assoc($formattedValues[$count][$prefix1 . 'CustomPost'],
1265 $formattedValues[$count][$prefix1 . 'CustomPre']
1266 );
1267 }
1268
1269 $formattedValues[$count][$prefix1 . 'CustomPostGroupTitle'] = $groupName;
1270 }
1271 $count++;
1272 }
1273 $form->_fields = $profileFields;
1274 }
481a74f4 1275 if (!empty($formattedValues)) {
5bdcb00b 1276 $form->assign('primaryParticipantProfile', $formattedValues[1]);
0479b4c8 1277 $form->set('primaryParticipantProfile', $formattedValues[1]);
5bdcb00b
PJ
1278 if ($count > 2) {
1279 unset($formattedValues[1]);
1280 $form->assign('addParticipantProfile', $formattedValues);
0479b4c8 1281 $form->set('addParticipantProfile', $formattedValues);
5bdcb00b
PJ
1282 }
1283 }
1284 }
6a488035 1285}