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