| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * |
| 31 | * @package CRM |
| 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
| 33 | * $Id$ |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * This class generates form components for processing Event |
| 39 | * |
| 40 | */ |
| 41 | class CRM_Event_Form_Registration extends CRM_Core_Form { |
| 42 | |
| 43 | /** |
| 44 | * how many locationBlocks should we display? |
| 45 | * |
| 46 | * @var int |
| 47 | * @const |
| 48 | */ |
| 49 | CONST LOCATION_BLOCKS = 1; |
| 50 | |
| 51 | /** |
| 52 | * the id of the event we are proceessing |
| 53 | * |
| 54 | * @var int |
| 55 | * @protected |
| 56 | */ |
| 57 | public $_eventId; |
| 58 | |
| 59 | /** |
| 60 | * the array of ids of all the participant we are proceessing |
| 61 | * |
| 62 | * @var int |
| 63 | * @protected |
| 64 | */ |
| 65 | protected $_participantIDS = NULL; |
| 66 | |
| 67 | /** |
| 68 | * the id of the participant we are proceessing |
| 69 | * |
| 70 | * @var int |
| 71 | * @protected |
| 72 | */ |
| 73 | protected $_participantId; |
| 74 | |
| 75 | /** |
| 76 | * is participant able to walk registration wizard. |
| 77 | * |
| 78 | * @var Boolean |
| 79 | * @protected |
| 80 | */ |
| 81 | public $_allowConfirmation; |
| 82 | |
| 83 | /** |
| 84 | * is participant requires approval |
| 85 | * |
| 86 | * @var Boolean |
| 87 | * @public |
| 88 | */ |
| 89 | public $_requireApproval; |
| 90 | |
| 91 | /** |
| 92 | * is event configured for waitlist. |
| 93 | * |
| 94 | * @var Boolean |
| 95 | * @public |
| 96 | */ |
| 97 | public $_allowWaitlist; |
| 98 | |
| 99 | /** |
| 100 | * store additional participant ids |
| 101 | * when there are pre-registered. |
| 102 | * |
| 103 | * @var array |
| 104 | * @public |
| 105 | */ |
| 106 | public $_additionalParticipantIds; |
| 107 | |
| 108 | /** |
| 109 | * the mode that we are in |
| 110 | * |
| 111 | * @var string |
| 112 | * @protect |
| 113 | */ |
| 114 | public $_mode; |
| 115 | |
| 116 | /** |
| 117 | * the values for the contribution db object |
| 118 | * |
| 119 | * @var array |
| 120 | * @protected |
| 121 | */ |
| 122 | public $_values; |
| 123 | |
| 124 | /** |
| 125 | * the paymentProcessor attributes for this page |
| 126 | * |
| 127 | * @var array |
| 128 | * @protected |
| 129 | */ |
| 130 | public $_paymentProcessor; |
| 131 | |
| 132 | /** |
| 133 | * The params submitted by the form and computed by the app |
| 134 | * |
| 135 | * @var array |
| 136 | * @protected |
| 137 | */ |
| 138 | protected $_params; |
| 139 | |
| 140 | /** |
| 141 | * The fields involved in this contribution page |
| 142 | * |
| 143 | * @var array |
| 144 | * @protected |
| 145 | */ |
| 146 | public $_fields; |
| 147 | |
| 148 | /** |
| 149 | * The billing location id for this contribiution page |
| 150 | * |
| 151 | * @var int |
| 152 | * @protected |
| 153 | */ |
| 154 | public $_bltID; |
| 155 | |
| 156 | /** |
| 157 | * Price Set ID, if the new price set method is used |
| 158 | * |
| 159 | * @var int |
| 160 | * @protected |
| 161 | */ |
| 162 | public $_priceSetId = NULL; |
| 163 | |
| 164 | /** |
| 165 | * Array of fields for the price set |
| 166 | * |
| 167 | * @var array |
| 168 | * @protected |
| 169 | */ |
| 170 | public $_priceSet; |
| 171 | |
| 172 | public $_action; |
| 173 | |
| 174 | public $_pcpId; |
| 175 | |
| 176 | /* Is event already full. |
| 177 | * |
| 178 | * @var boolean |
| 179 | * @protected |
| 180 | */ |
| 181 | |
| 182 | public $_isEventFull; |
| 183 | |
| 184 | public $_lineItem; |
| 185 | public $_lineItemParticipantsCount; |
| 186 | public $_availableRegistrations; |
| 187 | |
| 188 | public $_forcePayement; |
| 189 | |
| 190 | public $_isBillingAddressRequiredForPayLater; |
| 191 | |
| 192 | /** |
| 193 | * Function to set variables up before form is built |
| 194 | * |
| 195 | * @return void |
| 196 | * @access public |
| 197 | */ |
| 198 | function preProcess() { |
| 199 | $this->_eventId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); |
| 200 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); |
| 201 | |
| 202 | //CRM-4320 |
| 203 | $this->_participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); |
| 204 | |
| 205 | // current mode |
| 206 | $this->_mode = ($this->_action == 1024) ? 'test' : 'live'; |
| 207 | |
| 208 | $this->_values = $this->get('values'); |
| 209 | $this->_fields = $this->get('fields'); |
| 210 | $this->_bltID = $this->get('bltID'); |
| 211 | $this->_paymentProcessor = $this->get('paymentProcessor'); |
| 212 | $this->_priceSetId = $this->get('priceSetId'); |
| 213 | $this->_priceSet = $this->get('priceSet'); |
| 214 | $this->_lineItem = $this->get('lineItem'); |
| 215 | $this->_isEventFull = $this->get('isEventFull'); |
| 216 | $this->_lineItemParticipantsCount = $this->get('lineItemParticipants'); |
| 217 | if (!is_array($this->_lineItem)) { |
| 218 | $this->_lineItem = array(); |
| 219 | } |
| 220 | if (!is_array($this->_lineItemParticipantsCount)) { |
| 221 | $this->_lineItemParticipantsCount = array(); |
| 222 | } |
| 223 | $this->_availableRegistrations = $this->get('availableRegistrations'); |
| 224 | $this->_participantIDS = $this->get('participantIDs'); |
| 225 | |
| 226 | //check if participant allow to walk registration wizard. |
| 227 | $this->_allowConfirmation = $this->get('allowConfirmation'); |
| 228 | |
| 229 | // check for Approval |
| 230 | $this->_requireApproval = $this->get('requireApproval'); |
| 231 | |
| 232 | // check for waitlisting. |
| 233 | $this->_allowWaitlist = $this->get('allowWaitlist'); |
| 234 | |
| 235 | $this->_forcePayement = $this->get('forcePayement'); |
| 236 | |
| 237 | //get the additional participant ids. |
| 238 | $this->_additionalParticipantIds = $this->get('additionalParticipantIds'); |
| 239 | $config = CRM_Core_Config::singleton(); |
| 240 | |
| 241 | if (!$this->_values) { |
| 242 | // create redirect URL to send folks back to event info page is registration not available |
| 243 | $infoUrl = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_eventId}", |
| 244 | FALSE, NULL, FALSE, TRUE |
| 245 | ); |
| 246 | |
| 247 | // this is the first time we are hitting this, so check for permissions here |
| 248 | if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) { |
| 249 | CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event'), $infoUrl); |
| 250 | } |
| 251 | |
| 252 | // get all the values from the dao object |
| 253 | $this->_values = $this->_fields = array(); |
| 254 | $this->_forcePayement = FALSE; |
| 255 | |
| 256 | //retrieve event information |
| 257 | $params = array('id' => $this->_eventId); |
| 258 | CRM_Event_BAO_Event::retrieve($params, $this->_values['event']); |
| 259 | |
| 260 | $this->checkValidEvent($infoUrl); |
| 261 | // get the participant values, CRM-4320 |
| 262 | $this->_allowConfirmation = FALSE; |
| 263 | if ($this->_participantId) { |
| 264 | $this->processFirstParticipant($this->_participantId); |
| 265 | } |
| 266 | //check for additional participants. |
| 267 | if ($this->_allowConfirmation && $this->_values['event']['is_multiple_registrations']) { |
| 268 | $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_participantId); |
| 269 | $cnt = 1; |
| 270 | foreach ($additionalParticipantIds as $additionalParticipantId) { |
| 271 | $this->_additionalParticipantIds[$cnt] = $additionalParticipantId; |
| 272 | $cnt++; |
| 273 | } |
| 274 | $this->set('additionalParticipantIds', $this->_additionalParticipantIds); |
| 275 | } |
| 276 | |
| 277 | $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE, |
| 278 | CRM_Utils_Array::value('has_waitlist', $this->_values['event']) |
| 279 | ); |
| 280 | |
| 281 | $this->_allowWaitlist = $this->_isEventFull = FALSE; |
| 282 | if ($eventFull && !$this->_allowConfirmation) { |
| 283 | $this->_isEventFull = TRUE; |
| 284 | //lets redirecting to info only when to waiting list. |
| 285 | $this->_allowWaitlist = CRM_Utils_Array::value('has_waitlist', $this->_values['event']); |
| 286 | if (!$this->_allowWaitlist) { |
| 287 | CRM_Utils_System::redirect($infoUrl); |
| 288 | } |
| 289 | } |
| 290 | $this->set('isEventFull', $this->_isEventFull); |
| 291 | $this->set('allowWaitlist', $this->_allowWaitlist); |
| 292 | |
| 293 | //check for require requires approval. |
| 294 | $this->_requireApproval = FALSE; |
| 295 | if (!empty($this->_values['event']['requires_approval']) && !$this->_allowConfirmation) { |
| 296 | $this->_requireApproval = TRUE; |
| 297 | } |
| 298 | $this->set('requireApproval', $this->_requireApproval); |
| 299 | |
| 300 | if (isset($this->_values['event']['default_role_id'])) { |
| 301 | $participant_role = CRM_Core_OptionGroup::values('participant_role'); |
| 302 | $this->_values['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"]; |
| 303 | } |
| 304 | // check for is_monetary status |
| 305 | $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values['event']); |
| 306 | $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later'); |
| 307 | //check for variour combination for paylater, payment |
| 308 | //process with paid event. |
| 309 | if ($isMonetary && (!$isPayLater || !empty($this->_values['event']['payment_processor']))) { |
| 310 | $ppID = CRM_Utils_Array::value('payment_processor', |
| 311 | $this->_values['event'] |
| 312 | ); |
| 313 | if (!$ppID) { |
| 314 | CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).'), $infoUrl); |
| 315 | } |
| 316 | $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID); |
| 317 | $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds, |
| 318 | $this->_mode |
| 319 | ); |
| 320 | $this->set('paymentProcessors', $this->_paymentProcessors); |
| 321 | |
| 322 | //set default payment processor |
| 323 | if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) { |
| 324 | foreach ($this->_paymentProcessors as $ppId => $values) { |
| 325 | if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) { |
| 326 | $defaultProcessorId = $ppId; |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (isset($defaultProcessorId)) { |
| 333 | $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode); |
| 334 | } |
| 335 | // make sure we have a valid payment class, else abort |
| 336 | if ($this->_values['event']['is_monetary']) { |
| 337 | if (!CRM_Utils_System::isNull($this->_paymentProcessors)) { |
| 338 | foreach ($this->_paymentProcessors as $eachPaymentProcessor) { |
| 339 | // check selected payment processor is active |
| 340 | if (!$eachPaymentProcessor) { |
| 341 | CRM_Core_Error::fatal(ts('The site administrator must set a Payment Processor for this event in order to use online registration.')); |
| 342 | } |
| 343 | // ensure that processor has a valid config |
| 344 | $payment = CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this); |
| 345 | $error = $payment->checkConfig(); |
| 346 | if (!empty($error)) { |
| 347 | CRM_Core_Error::fatal($error); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | //init event fee. |
| 354 | self::initEventFee($this, $this->_eventId); |
| 355 | |
| 356 | // get the profile ids |
| 357 | $ufJoinParams = array( |
| 358 | 'entity_table' => 'civicrm_event', |
| 359 | // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants |
| 360 | 'module' => 'CiviEvent', |
| 361 | 'entity_id' => $this->_eventId, |
| 362 | ); |
| 363 | list($this->_values['custom_pre_id'], |
| 364 | $this->_values['custom_post_id'] |
| 365 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
| 366 | |
| 367 | // set profiles for additional participants |
| 368 | if ($this->_values['event']['is_multiple_registrations']) { |
| 369 | // CRM-4377:Â CiviEvent for the main participant, CiviEvent_Additional for additional participants |
| 370 | $ufJoinParams['module'] = 'CiviEvent_Additional'; |
| 371 | |
| 372 | list($this->_values['additional_custom_pre_id'], |
| 373 | $this->_values['additional_custom_post_id'], $preActive, $postActive |
| 374 | ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams); |
| 375 | |
| 376 | // CRM-4377:Â we need to maintain backward compatibility, hence if there is profile for main contact |
| 377 | // set same profile for additional contacts. |
| 378 | if ($this->_values['custom_pre_id'] && !$this->_values['additional_custom_pre_id']) { |
| 379 | $this->_values['additional_custom_pre_id'] = $this->_values['custom_pre_id']; |
| 380 | } |
| 381 | |
| 382 | if ($this->_values['custom_post_id'] && !$this->_values['additional_custom_post_id']) { |
| 383 | $this->_values['additional_custom_post_id'] = $this->_values['custom_post_id']; |
| 384 | } |
| 385 | // now check for no profile condition, in that case is_active = 0 |
| 386 | if (isset($preActive) && !$preActive) { |
| 387 | unset($this->_values['additional_custom_pre_id']); |
| 388 | } |
| 389 | if (isset($postActive) && !$postActive) { |
| 390 | unset($this->_values['additional_custom_post_id']); |
| 391 | } |
| 392 | } |
| 393 | // get the billing location type |
| 394 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
| 395 | // CRM-8108 remove ts from Billing as the location type can not be translated in CiviCRM! |
| 396 | //$this->_bltID = array_search( ts('Billing'), $locationTypes ); |
| 397 | $this->_bltID = array_search('Billing', $locationTypes); |
| 398 | if (!$this->_bltID) { |
| 399 | CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing'))); |
| 400 | } |
| 401 | $this->set('bltID', $this->_bltID); |
| 402 | |
| 403 | if ($this->_values['event']['is_monetary'] && |
| 404 | ($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM) |
| 405 | ) { |
| 406 | CRM_Core_Payment_Form::setCreditCardFields($this); |
| 407 | } |
| 408 | $params = array('entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event'); |
| 409 | $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE); |
| 410 | |
| 411 | $this->set('values', $this->_values); |
| 412 | $this->set('fields', $this->_fields); |
| 413 | |
| 414 | $this->_availableRegistrations = |
| 415 | CRM_Event_BAO_Participant::eventFull( |
| 416 | $this->_values['event']['id'], TRUE, |
| 417 | CRM_Utils_Array::value('has_waitlist', $this->_values['event']) |
| 418 | ); |
| 419 | $this->set('availableRegistrations', $this->_availableRegistrations); |
| 420 | } |
| 421 | $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor); |
| 422 | |
| 423 | // check if this is a paypal auto return and redirect accordingly |
| 424 | if (CRM_Core_Payment::paypalRedirect($this->_paymentProcessor)) { |
| 425 | $url = CRM_Utils_System::url('civicrm/event/register', |
| 426 | "_qf_ThankYou_display=1&qfKey={$this->controller->_key}" |
| 427 | ); |
| 428 | CRM_Utils_System::redirect($url); |
| 429 | } |
| 430 | $this->_contributeMode = $this->get('contributeMode'); |
| 431 | $this->assign('contributeMode', $this->_contributeMode); |
| 432 | |
| 433 | // setting CMS page title |
| 434 | CRM_Utils_System::setTitle($this->_values['event']['title']); |
| 435 | $this->assign('title', $this->_values['event']['title']); |
| 436 | |
| 437 | $this->assign('paidEvent', $this->_values['event']['is_monetary']); |
| 438 | |
| 439 | // we do not want to display recently viewed items on Registration pages |
| 440 | $this->assign('displayRecent', FALSE); |
| 441 | // Registration page values are cleared from session, so can't use normal Printer Friendly view. |
| 442 | // Use Browser Print instead. |
| 443 | $this->assign('browserPrint', TRUE); |
| 444 | |
| 445 | $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']); |
| 446 | $this->assign('isShowLocation', $isShowLocation); |
| 447 | // Handle PCP |
| 448 | $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this); |
| 449 | if ($pcpId) { |
| 450 | $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'event', $this->_values['event']); |
| 451 | $this->_pcpId = $pcp['pcpId']; |
| 452 | $this->_values['event']['intro_text'] = CRM_Utils_Array::value('intro_text', $pcp['pcpInfo']); |
| 453 | } |
| 454 | |
| 455 | // assign all event properties so wizard templates can display event info. |
| 456 | $this->assign('event', $this->_values['event']); |
| 457 | $this->assign('location', $this->_values['location']); |
| 458 | $this->assign('bltID', $this->_bltID); |
| 459 | $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']); |
| 460 | $this->assign('isShowLocation', $isShowLocation); |
| 461 | //CRM-6907 |
| 462 | $config->defaultCurrency = CRM_Utils_Array::value('currency', $this->_values['event'], |
| 463 | $config->defaultCurrency |
| 464 | ); |
| 465 | |
| 466 | //lets allow user to override campaign. |
| 467 | $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this); |
| 468 | if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) { |
| 469 | $this->_values['event']['campaign_id'] = $campID; |
| 470 | } |
| 471 | |
| 472 | // check if billing block is required for pay later |
| 473 | if (CRM_Utils_Array::value('is_pay_later', $this->_values['event'])) { |
| 474 | $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values['event']); |
| 475 | $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * assign the minimal set of variables to the template |
| 481 | * |
| 482 | * @return void |
| 483 | * @access public |
| 484 | */ |
| 485 | function assignToTemplate() { |
| 486 | //process only primary participant params |
| 487 | $this->_params = $this->get('params'); |
| 488 | if (isset($this->_params[0])) { |
| 489 | $params = $this->_params[0]; |
| 490 | } |
| 491 | $name = ''; |
| 492 | if (!empty($params['billing_first_name'])) { |
| 493 | $name = $params['billing_first_name']; |
| 494 | } |
| 495 | |
| 496 | if (!empty($params['billing_middle_name'])) { |
| 497 | $name .= " {$params['billing_middle_name']}"; |
| 498 | } |
| 499 | |
| 500 | if (!empty($params['billing_last_name'])) { |
| 501 | $name .= " {$params['billing_last_name']}"; |
| 502 | } |
| 503 | $this->assign('billingName', $name); |
| 504 | $this->set('name', $name); |
| 505 | |
| 506 | $vars = array( |
| 507 | 'amount', 'currencyID', 'credit_card_type', |
| 508 | 'trxn_id', 'amount_level', 'receive_date', |
| 509 | ); |
| 510 | |
| 511 | foreach ($vars as $v) { |
| 512 | if (!empty($params[$v])) { |
| 513 | if ($v == 'receive_date') { |
| 514 | $this->assign($v, CRM_Utils_Date::mysqlToIso($params[$v])); |
| 515 | } |
| 516 | else { |
| 517 | $this->assign($v, $params[$v]); |
| 518 | } |
| 519 | } |
| 520 | elseif (CRM_Utils_Array::value('amount', $params) == 0) { |
| 521 | $this->assign($v, CRM_Utils_Array::value($v, $params)); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // assign the address formatted up for display |
| 526 | $addressParts = array( |
| 527 | "street_address-{$this->_bltID}", |
| 528 | "city-{$this->_bltID}", |
| 529 | "postal_code-{$this->_bltID}", |
| 530 | "state_province-{$this->_bltID}", |
| 531 | "country-{$this->_bltID}", |
| 532 | ); |
| 533 | $addressFields = array(); |
| 534 | foreach ($addressParts as $part) { |
| 535 | list($n, $id) = explode('-', $part); |
| 536 | if (isset($params['billing_' . $part])) { |
| 537 | $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $params); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | $this->assign('address', CRM_Utils_Address::format($addressFields)); |
| 542 | |
| 543 | if ($this->_contributeMode == 'direct' && empty($params['is_pay_later'])) { |
| 544 | $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $params)); |
| 545 | $date = CRM_Utils_Date::mysqlToIso($date); |
| 546 | $this->assign('credit_card_exp_date', $date); |
| 547 | $this->assign('credit_card_number', |
| 548 | CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $params)) |
| 549 | ); |
| 550 | } |
| 551 | |
| 552 | // get the email that the confirmation would have been sent to |
| 553 | $session = CRM_Core_Session::singleton(); |
| 554 | |
| 555 | // assign is_email_confirm to templates |
| 556 | if (isset($this->_values['event']['is_email_confirm'])) { |
| 557 | $this->assign('is_email_confirm', $this->_values['event']['is_email_confirm']); |
| 558 | } |
| 559 | |
| 560 | // assign pay later stuff |
| 561 | $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE); |
| 562 | $this->assign('is_pay_later', $params['is_pay_later']); |
| 563 | if ($params['is_pay_later']) { |
| 564 | $this->assign('pay_later_text', $this->_values['event']['pay_later_text']); |
| 565 | $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']); |
| 566 | } |
| 567 | |
| 568 | // also assign all participantIDs to the template |
| 569 | // useful in generating confirmation numbers if needed |
| 570 | $this->assign('participantIDs', |
| 571 | $this->_participantIDS |
| 572 | ); |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Function to add the custom fields |
| 577 | * |
| 578 | * @param $id |
| 579 | * @param $name |
| 580 | * @param bool $viewOnly |
| 581 | * |
| 582 | * @return void |
| 583 | * @access public |
| 584 | */ |
| 585 | function buildCustom($id, $name, $viewOnly = FALSE) { |
| 586 | if ($id) { |
| 587 | $button = substr($this->controller->getButtonName(), -4); |
| 588 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 589 | $session = CRM_Core_Session::singleton(); |
| 590 | $contactID = $session->get('userID'); |
| 591 | |
| 592 | // we don't allow conflicting fields to be |
| 593 | // configured via profile |
| 594 | $fieldsToIgnore = array( |
| 595 | 'participant_fee_amount' => 1, |
| 596 | 'participant_fee_level' => 1, |
| 597 | ); |
| 598 | if ($contactID) { |
| 599 | //FIX CRM-9653 |
| 600 | if (is_array($id)) { |
| 601 | $fields = array(); |
| 602 | foreach ($id as $profileID) { |
| 603 | $field = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD, |
| 604 | NULL, NULL, FALSE, NULL, |
| 605 | FALSE, NULL, CRM_Core_Permission::CREATE, |
| 606 | 'field_name', TRUE |
| 607 | ); |
| 608 | $fields = array_merge($fields, $field); |
| 609 | } |
| 610 | } |
| 611 | else { |
| 612 | if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) { |
| 613 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, |
| 614 | NULL, NULL, FALSE, NULL, |
| 615 | FALSE, NULL, CRM_Core_Permission::CREATE, |
| 616 | 'field_name', TRUE |
| 617 | ); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | else { |
| 622 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD, |
| 623 | NULL, NULL, FALSE, NULL, |
| 624 | FALSE, NULL, CRM_Core_Permission::CREATE, |
| 625 | 'field_name', TRUE |
| 626 | ); |
| 627 | } |
| 628 | |
| 629 | if (array_intersect_key($fields, $fieldsToIgnore)) { |
| 630 | $fields = array_diff_key($fields, $fieldsToIgnore); |
| 631 | CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.')); |
| 632 | } |
| 633 | $addCaptcha = FALSE; |
| 634 | |
| 635 | if (!empty($this->_fields)) { |
| 636 | $fields = @array_diff_assoc($fields, $this->_fields); |
| 637 | } |
| 638 | |
| 639 | if (empty($this->_params[0]['additional_participants']) && |
| 640 | is_null($cid) |
| 641 | ) { |
| 642 | CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); |
| 643 | } |
| 644 | $this->assign($name, $fields); |
| 645 | if (is_array($fields)) { |
| 646 | foreach ($fields as $key => $field) { |
| 647 | if ($viewOnly && |
| 648 | isset($field['data_type']) && |
| 649 | $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL') |
| 650 | ) { |
| 651 | // ignore file upload fields |
| 652 | continue; |
| 653 | } |
| 654 | //make the field optional if primary participant |
| 655 | //have been skip the additional participant. |
| 656 | if ($button == 'skip') { |
| 657 | $field['is_required'] = FALSE; |
| 658 | } |
| 659 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor |
| 660 | elseif ($field['add_captcha'] && !$contactID) { |
| 661 | // only add captcha for first page |
| 662 | $addCaptcha = TRUE; |
| 663 | } |
| 664 | list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2); |
| 665 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE); |
| 666 | |
| 667 | $this->_fields[$key] = $field; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if ($addCaptcha && !$viewOnly) { |
| 672 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); |
| 673 | $captcha->add($this); |
| 674 | $this->assign('isCaptcha', TRUE); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @param $form |
| 681 | * @param $eventID |
| 682 | * |
| 683 | * @throws Exception |
| 684 | */ |
| 685 | static function initEventFee(&$form, $eventID) { |
| 686 | // get price info |
| 687 | |
| 688 | // retrive all active price set fields. |
| 689 | $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event'); |
| 690 | if (property_exists($form, '_discountId') && $form->_discountId) { |
| 691 | $discountId = $form->_discountId; |
| 692 | } |
| 693 | if ($discountId) { |
| 694 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id'); |
| 695 | $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId); |
| 696 | } |
| 697 | else { |
| 698 | $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE); |
| 699 | } |
| 700 | |
| 701 | if (property_exists($form, '_context') && ($form->_context == 'standalone' |
| 702 | || $form->_context == 'participant')) { |
| 703 | $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event'); |
| 704 | if (is_array( $discountedEvent)) { |
| 705 | foreach ($discountedEvent as $key => $priceSetId) { |
| 706 | $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId); |
| 707 | $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet); |
| 708 | $form->_values['discount'][$key] = CRM_Utils_Array::value('fields', $priceSet); |
| 709 | $fieldID = key($form->_values['discount'][$key]); |
| 710 | $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue( |
| 711 | 'CRM_Price_DAO_PriceSet', |
| 712 | $priceSetId, |
| 713 | 'title' |
| 714 | ); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | $eventFee = CRM_Utils_Array::value('fee', $form->_values); |
| 719 | if (!is_array($eventFee) || empty($eventFee)) { |
| 720 | $form->_values['fee'] = array(); |
| 721 | } |
| 722 | |
| 723 | //fix for non-upgraded price sets.CRM-4256. |
| 724 | if (isset($form->_isPaidEvent)) { |
| 725 | $isPaidEvent = $form->_isPaidEvent; |
| 726 | } |
| 727 | else { |
| 728 | $isPaidEvent = CRM_Utils_Array::value('is_monetary', $form->_values['event']); |
| 729 | } |
| 730 | if ($isPaidEvent && empty($form->_values['fee'])) { |
| 731 | if (CRM_Utils_System::getClassName($form) != 'CRM_Event_Form_Participant') { |
| 732 | CRM_Core_Error::fatal(ts('No Fee Level(s) or Price Set is configured for this event.<br />Click <a href=\'%1\'>CiviEvent >> Manage Event >> Configure >> Event Fees</a> to configure the Fee Level(s) or Price Set for this event.', array(1 => CRM_Utils_System::url('civicrm/event/manage/fee', 'reset=1&action=update&id=' . $form->_eventId)))); |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Function to handle process after the confirmation of payment by User |
| 739 | * |
| 740 | * @param null $contactID |
| 741 | * @param null $contribution |
| 742 | * @param null $payment |
| 743 | * |
| 744 | * @return void |
| 745 | * @access public |
| 746 | */ |
| 747 | function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) { |
| 748 | // add/update contact information |
| 749 | $fields = array(); |
| 750 | unset($this->_params['note']); |
| 751 | |
| 752 | //to avoid conflict overwrite $this->_params |
| 753 | $this->_params = $this->get('value'); |
| 754 | |
| 755 | //get the amount of primary participant |
| 756 | if (!empty($this->_params['is_primary'])) { |
| 757 | $this->_params['fee_amount'] = $this->get('primaryParticipantAmount'); |
| 758 | } |
| 759 | |
| 760 | // add participant record |
| 761 | $participant = CRM_Event_Form_Registration::addParticipant($this, $contactID); |
| 762 | $this->_participantIDS[] = $participant->id; |
| 763 | |
| 764 | //setting register_by_id field and primaryContactId |
| 765 | if (!empty($this->_params['is_primary'])) { |
| 766 | $this->set('registerByID', $participant->id); |
| 767 | $this->set('primaryContactId', $contactID); |
| 768 | |
| 769 | // CRM-10032 |
| 770 | $this->processFirstParticipant($participant->id); |
| 771 | } |
| 772 | |
| 773 | CRM_Core_BAO_CustomValueTable::postProcess($this->_params, |
| 774 | CRM_Core_DAO::$_nullArray, |
| 775 | 'civicrm_participant', |
| 776 | $participant->id, |
| 777 | 'Participant' |
| 778 | ); |
| 779 | |
| 780 | $createPayment = (CRM_Utils_Array::value('amount', $this->_params, 0) != 0) ? TRUE : FALSE; |
| 781 | |
| 782 | // force to create zero amount payment, CRM-5095 |
| 783 | // we know the amout is zero since createPayment is false |
| 784 | if (!$createPayment && |
| 785 | (isset($contribution) && $contribution->id) && |
| 786 | $this->_priceSetId && |
| 787 | $this->_lineItem |
| 788 | ) { |
| 789 | $createPayment = TRUE; |
| 790 | } |
| 791 | |
| 792 | if ($createPayment && $this->_values['event']['is_monetary'] && !empty($this->_params['contributionID'])) { |
| 793 | $paymentParams = array( |
| 794 | 'participant_id' => $participant->id, |
| 795 | 'contribution_id' => $contribution->id, |
| 796 | ); |
| 797 | $ids = array(); |
| 798 | $paymentPartcipant = CRM_Event_BAO_ParticipantPayment::create($paymentParams, $ids); |
| 799 | } |
| 800 | |
| 801 | //set only primary participant's params for transfer checkout. |
| 802 | if (($this->_contributeMode == 'checkout' || $this->_contributeMode == 'notify') && !empty($this->_params['is_primary'])) { |
| 803 | $this->_params['participantID'] = $participant->id; |
| 804 | $this->set('primaryParticipant', $this->_params); |
| 805 | } |
| 806 | |
| 807 | $this->assign('action', $this->_action); |
| 808 | |
| 809 | // create CMS user |
| 810 | if (!empty($this->_params['cms_create_account'])) { |
| 811 | $this->_params['contactID'] = $contactID; |
| 812 | |
| 813 | if (array_key_exists('email-5', $this->_params)) { |
| 814 | $mail = 'email-5'; |
| 815 | } else { |
| 816 | foreach ($this->_params as $name => $dontCare) { |
| 817 | if (substr($name, 0, 5) == 'email') { |
| 818 | $mail = $name; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | // we should use primary email for |
| 825 | // 1. free event registration. |
| 826 | // 2. pay later participant. |
| 827 | // 3. waiting list participant. |
| 828 | // 4. require approval participant. |
| 829 | if (!empty($this->_params['is_pay_later']) || |
| 830 | $this->_allowWaitlist || $this->_requireApproval || empty($this->_values['event']['is_monetary'])) { |
| 831 | $mail = 'email-Primary'; |
| 832 | } |
| 833 | |
| 834 | if (!CRM_Core_BAO_CMSUser::create($this->_params, $mail)) { |
| 835 | CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.')); |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Process the participant |
| 842 | * |
| 843 | * @param $params |
| 844 | * @param $contactID |
| 845 | * |
| 846 | * @return void |
| 847 | * @access public |
| 848 | */ |
| 849 | public static function addParticipant(&$form, $contactID) { |
| 850 | if (empty($form->_params)) { |
| 851 | return; |
| 852 | } |
| 853 | $params = $form->_params; |
| 854 | $transaction = new CRM_Core_Transaction(); |
| 855 | |
| 856 | $groupName = 'participant_role'; |
| 857 | $query = " |
| 858 | SELECT v.label as label ,v.value as value |
| 859 | FROM civicrm_option_value v, |
| 860 | civicrm_option_group g |
| 861 | WHERE v.option_group_id = g.id |
| 862 | AND g.name = %1 |
| 863 | AND v.is_active = 1 |
| 864 | AND g.is_active = 1 |
| 865 | "; |
| 866 | $p = array(1 => array($groupName, 'String')); |
| 867 | |
| 868 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
| 869 | if ($dao->fetch()) { |
| 870 | $roleID = $dao->value; |
| 871 | } |
| 872 | |
| 873 | // handle register date CRM-4320 |
| 874 | $registerDate = NULL; |
| 875 | if (!empty($form->_allowConfirmation) && $form->_participantId) { |
| 876 | $registerDate = $params['participant_register_date']; |
| 877 | } |
| 878 | elseif (!empty($params['participant_register_date']) && |
| 879 | is_array($params['participant_register_date']) && |
| 880 | !empty($params['participant_register_date']) |
| 881 | ) { |
| 882 | $registerDate = CRM_Utils_Date::format($params['participant_register_date']); |
| 883 | } |
| 884 | |
| 885 | $participantFields = CRM_Event_DAO_Participant::fields(); |
| 886 | $participantParams = array('id' => CRM_Utils_Array::value('participant_id', $params), |
| 887 | 'contact_id' => $contactID, |
| 888 | 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'], |
| 889 | 'status_id' => CRM_Utils_Array::value('participant_status', |
| 890 | $params, 1 |
| 891 | ), |
| 892 | 'role_id' => CRM_Utils_Array::value('participant_role_id', |
| 893 | $params, $roleID |
| 894 | ), |
| 895 | 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'), |
| 896 | 'source' => CRM_Utils_String::ellipsify( |
| 897 | isset($params['participant_source']) ? |
| 898 | CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params), |
| 899 | $participantFields['participant_source']['maxlength'] |
| 900 | ), |
| 901 | 'fee_level' => CRM_Utils_Array::value('amount_level', $params), |
| 902 | 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0), |
| 903 | 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params), |
| 904 | 'registered_by_id' => CRM_Utils_Array::value('registered_by_id', $params), |
| 905 | 'discount_id' => CRM_Utils_Array::value('discount_id', $params), |
| 906 | 'fee_currency' => CRM_Utils_Array::value('currencyID', $params), |
| 907 | 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params), |
| 908 | ); |
| 909 | |
| 910 | if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { |
| 911 | $participantParams['is_test'] = 1; |
| 912 | } |
| 913 | else { |
| 914 | $participantParams['is_test'] = 0; |
| 915 | } |
| 916 | |
| 917 | if (!empty($form->_params['note'])) { |
| 918 | $participantParams['note'] = $form->_params['note']; |
| 919 | } |
| 920 | elseif (!empty($form->_params['participant_note'])) { |
| 921 | $participantParams['note'] = $form->_params['participant_note']; |
| 922 | } |
| 923 | |
| 924 | // reuse id if one already exists for this one (can happen |
| 925 | // with back button being hit etc) |
| 926 | if (!$participantParams['id'] && !empty($params['contributionID'])) { |
| 927 | $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', |
| 928 | $params['contributionID'], |
| 929 | 'participant_id', |
| 930 | 'contribution_id' |
| 931 | ); |
| 932 | $participantParams['id'] = $pID; |
| 933 | } |
| 934 | $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event'); |
| 935 | |
| 936 | if (!$participantParams['discount_id']) { |
| 937 | $participantParams['discount_id'] = "null"; |
| 938 | } |
| 939 | |
| 940 | $participant = CRM_Event_BAO_Participant::create($participantParams); |
| 941 | |
| 942 | $transaction->commit(); |
| 943 | |
| 944 | return $participant; |
| 945 | } |
| 946 | |
| 947 | /* Calculate the total participant count as per params. |
| 948 | * |
| 949 | * @param array $params user params. |
| 950 | * |
| 951 | * @return $totalCount total participant count. |
| 952 | * @access public |
| 953 | */ |
| 954 | /** |
| 955 | * @param $form |
| 956 | * @param $params |
| 957 | * @param bool $skipCurrent |
| 958 | * |
| 959 | * @return int|string |
| 960 | */ |
| 961 | public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) { |
| 962 | $totalCount = 0; |
| 963 | if (!is_array($params) || empty($params)) { |
| 964 | return $totalCount; |
| 965 | } |
| 966 | |
| 967 | $priceSetId = $form->get('priceSetId'); |
| 968 | $addParticipantNum = substr($form->_name, 12); |
| 969 | $priceSetFields = $priceSetDetails = array(); |
| 970 | $hasPriceFieldsCount = FALSE; |
| 971 | if ($priceSetId) { |
| 972 | $priceSetDetails = $form->get('priceSet'); |
| 973 | if (isset($priceSetDetails['optionsCountTotal']) |
| 974 | && $priceSetDetails['optionsCountTotal'] |
| 975 | ) { |
| 976 | $hasPriceFieldsCount = TRUE; |
| 977 | $priceSetFields = $priceSetDetails['optionsCountDetails']['fields']; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | $singleFormParams = FALSE; |
| 982 | foreach ($params as $key => $val) { |
| 983 | if (!is_numeric($key)) { |
| 984 | $singleFormParams = TRUE; |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | //first format the params. |
| 990 | if ($singleFormParams) { |
| 991 | $params = self::formatPriceSetParams($form, $params); |
| 992 | $params = array($params); |
| 993 | } |
| 994 | |
| 995 | foreach ($params as $key => $values) { |
| 996 | if (!is_numeric($key) || |
| 997 | $values == 'skip' || |
| 998 | ($skipCurrent && ($addParticipantNum == $key)) |
| 999 | ) { |
| 1000 | continue; |
| 1001 | } |
| 1002 | $count = 1; |
| 1003 | |
| 1004 | $usedCache = FALSE; |
| 1005 | $cacheCount = CRM_Utils_Array::value($key, $form->_lineItemParticipantsCount); |
| 1006 | if ($cacheCount && is_numeric($cacheCount)) { |
| 1007 | $count = $cacheCount; |
| 1008 | $usedCache = TRUE; |
| 1009 | } |
| 1010 | |
| 1011 | if (!$usedCache && $hasPriceFieldsCount) { |
| 1012 | $count = 0; |
| 1013 | foreach ($values as $valKey => $value) { |
| 1014 | if (strpos($valKey, 'price_') === FALSE) { |
| 1015 | continue; |
| 1016 | } |
| 1017 | $priceFieldId = substr($valKey, 6); |
| 1018 | if (!$priceFieldId || |
| 1019 | !is_array($value) || |
| 1020 | !array_key_exists($priceFieldId, $priceSetFields) |
| 1021 | ) { |
| 1022 | continue; |
| 1023 | } |
| 1024 | foreach ($value as $optId => $optVal) { |
| 1025 | $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal; |
| 1026 | if ($currentCount) { |
| 1027 | $count += $currentCount; |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | if (!$count) { |
| 1032 | $count = 1; |
| 1033 | } |
| 1034 | } |
| 1035 | $totalCount += $count; |
| 1036 | } |
| 1037 | if (!$totalCount) { |
| 1038 | $totalCount = 1; |
| 1039 | } |
| 1040 | |
| 1041 | return $totalCount; |
| 1042 | } |
| 1043 | |
| 1044 | /* Format user submitted price set params. |
| 1045 | * Convert price set each param as an array. |
| 1046 | * |
| 1047 | * @param $params an array of user submitted params. |
| 1048 | * |
| 1049 | * |
| 1050 | * @return array $formatted, formatted price set params. |
| 1051 | * @access public |
| 1052 | */ |
| 1053 | /** |
| 1054 | * @param $form |
| 1055 | * @param $params |
| 1056 | * |
| 1057 | * @return mixed |
| 1058 | */ |
| 1059 | public static function formatPriceSetParams(&$form, $params) { |
| 1060 | if (!is_array($params) || empty($params)) { |
| 1061 | return $params; |
| 1062 | } |
| 1063 | |
| 1064 | $priceSetId = $form->get('priceSetId'); |
| 1065 | if (!$priceSetId) { |
| 1066 | return $params; |
| 1067 | } |
| 1068 | $priceSetDetails = $form->get('priceSet'); |
| 1069 | |
| 1070 | foreach ($params as $key => & $value) { |
| 1071 | $vals = array(); |
| 1072 | if (strpos($key, 'price_') !== FALSE) { |
| 1073 | $fieldId = substr($key, 6); |
| 1074 | if (!array_key_exists($fieldId, $priceSetDetails['fields']) || |
| 1075 | is_array($value) || |
| 1076 | !$value |
| 1077 | ) { |
| 1078 | continue; |
| 1079 | } |
| 1080 | $field = $priceSetDetails['fields'][$fieldId]; |
| 1081 | if ($field['html_type'] == 'Text') { |
| 1082 | $fieldOption = current($field['options']); |
| 1083 | $value = array($fieldOption['id'] => $value); |
| 1084 | } |
| 1085 | else { |
| 1086 | $value = array($value => TRUE); |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | return $params; |
| 1092 | } |
| 1093 | |
| 1094 | /* Calculate total count for each price set options. |
| 1095 | * those are currently selected by user. |
| 1096 | * |
| 1097 | * @param $form form object. |
| 1098 | * |
| 1099 | * |
| 1100 | * @return array $optionsCount, array of each option w/ count total. |
| 1101 | * @access public |
| 1102 | */ |
| 1103 | /** |
| 1104 | * @param $form |
| 1105 | * |
| 1106 | * @return array |
| 1107 | */ |
| 1108 | public static function getPriceSetOptionCount(&$form) { |
| 1109 | $params = $form->get('params'); |
| 1110 | $priceSet = $form->get('priceSet'); |
| 1111 | $priceSetId = $form->get('priceSetId'); |
| 1112 | |
| 1113 | $optionsCount = array(); |
| 1114 | if (!$priceSetId || |
| 1115 | !is_array($priceSet) || |
| 1116 | empty($priceSet) || |
| 1117 | !is_array($params) || |
| 1118 | empty($params) |
| 1119 | ) { |
| 1120 | return $optionsCount; |
| 1121 | } |
| 1122 | |
| 1123 | $priceSetFields = $priceMaxFieldDetails = array(); |
| 1124 | if (!empty($priceSet['optionsCountTotal'])) { |
| 1125 | $priceSetFields = $priceSet['optionsCountDetails']['fields']; |
| 1126 | } |
| 1127 | |
| 1128 | if (!empty($priceSet['optionsMaxValueTotal'])) { |
| 1129 | $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields']; |
| 1130 | } |
| 1131 | |
| 1132 | $addParticipantNum = substr($form->_name, 12); |
| 1133 | foreach ($params as $pCnt => $values) { |
| 1134 | if ($values == 'skip' || |
| 1135 | $pCnt == $addParticipantNum |
| 1136 | ) { |
| 1137 | continue; |
| 1138 | } |
| 1139 | |
| 1140 | foreach ($values as $valKey => $value) { |
| 1141 | if (strpos($valKey, 'price_') === FALSE) { |
| 1142 | continue; |
| 1143 | } |
| 1144 | |
| 1145 | $priceFieldId = substr($valKey, 6); |
| 1146 | if (!$priceFieldId || |
| 1147 | !is_array($value) || |
| 1148 | !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails)) |
| 1149 | ) { |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | foreach ($value as $optId => $optVal) { |
| 1154 | if (CRM_Utils_Array::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') { |
| 1155 | $currentCount = $optVal; |
| 1156 | } |
| 1157 | else { |
| 1158 | $currentCount = 1; |
| 1159 | } |
| 1160 | |
| 1161 | if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) { |
| 1162 | $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal; |
| 1163 | } |
| 1164 | |
| 1165 | $optionsCount[$optId] = $currentCount + CRM_Utils_Array::value($optId, $optionsCount, 0); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | return $optionsCount; |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * @param string $suffix |
| 1175 | * |
| 1176 | * @return null|string |
| 1177 | */ |
| 1178 | function checkTemplateFileExists($suffix = '') { |
| 1179 | if ($this->_eventId) { |
| 1180 | $templateName = $this->_name; |
| 1181 | if (substr($templateName, 0, 12) == 'Participant_') { |
| 1182 | $templateName = 'AdditionalParticipant'; |
| 1183 | } |
| 1184 | |
| 1185 | $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl"; |
| 1186 | $template = CRM_Core_Form::getTemplate(); |
| 1187 | if ($template->template_exists($templateFile)) { |
| 1188 | return $templateFile; |
| 1189 | } |
| 1190 | } |
| 1191 | return NULL; |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * @return null|string |
| 1196 | */ |
| 1197 | function getTemplateFileName() { |
| 1198 | $fileName = $this->checkTemplateFileExists(); |
| 1199 | return $fileName ? $fileName : parent::getTemplateFileName(); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * @return null|string |
| 1204 | */ |
| 1205 | function overrideExtraTemplateFileName() { |
| 1206 | $fileName = $this->checkTemplateFileExists('extra.'); |
| 1207 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); |
| 1208 | } |
| 1209 | |
| 1210 | /** |
| 1211 | * Validate price set submitted params for price option limit, |
| 1212 | * as well as user should select at least one price field option. |
| 1213 | * @param unknown_type $form |
| 1214 | * @param unknown_type $params |
| 1215 | * @return multitype:|Ambigous <multitype:, string, string> |
| 1216 | */ |
| 1217 | static function validatePriceSet(&$form, $params) { |
| 1218 | $errors = array(); |
| 1219 | $hasOptMaxValue = FALSE; |
| 1220 | if (!is_array($params) || empty($params)) { |
| 1221 | return $errors; |
| 1222 | } |
| 1223 | |
| 1224 | $currentParticipantNum = substr($form->_name, 12); |
| 1225 | if (!$currentParticipantNum) { |
| 1226 | $currentParticipantNum = 0; |
| 1227 | } |
| 1228 | |
| 1229 | $priceSetId = $form->get('priceSetId'); |
| 1230 | $priceSetDetails = $form->get('priceSet'); |
| 1231 | if ( |
| 1232 | !$priceSetId || |
| 1233 | !is_array($priceSetDetails) || |
| 1234 | empty($priceSetDetails) |
| 1235 | ) { |
| 1236 | return $errors; |
| 1237 | } |
| 1238 | |
| 1239 | $optionsCountDetails = $optionsMaxValueDetails = array(); |
| 1240 | if ( |
| 1241 | isset($priceSetDetails['optionsMaxValueTotal']) |
| 1242 | && $priceSetDetails['optionsMaxValueTotal'] |
| 1243 | ) { |
| 1244 | $hasOptMaxValue = TRUE; |
| 1245 | $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields']; |
| 1246 | } |
| 1247 | if ( |
| 1248 | isset($priceSetDetails['optionsCountTotal']) |
| 1249 | && $priceSetDetails['optionsCountTotal'] |
| 1250 | ) { |
| 1251 | $hasOptCount = TRUE; |
| 1252 | $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields']; |
| 1253 | } |
| 1254 | $feeBlock = $form->_feeBlock; |
| 1255 | |
| 1256 | if (empty($feeBlock)) { |
| 1257 | $feeBlock = $priceSetDetails['fields']; |
| 1258 | } |
| 1259 | |
| 1260 | $optionMaxValues = $fieldSelected = array(); |
| 1261 | foreach ($params as $pNum => $values) { |
| 1262 | if (!is_array($values) || $values == 'skip') { |
| 1263 | continue; |
| 1264 | } |
| 1265 | |
| 1266 | foreach ($values as $valKey => $value) { |
| 1267 | if (strpos($valKey, 'price_') === FALSE) { |
| 1268 | continue; |
| 1269 | } |
| 1270 | $priceFieldId = substr($valKey, 6); |
| 1271 | $noneOptionValueSelected = FALSE; |
| 1272 | if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) { |
| 1273 | $noneOptionValueSelected = TRUE; |
| 1274 | } |
| 1275 | |
| 1276 | if ( |
| 1277 | !$priceFieldId || |
| 1278 | (!$noneOptionValueSelected && !is_array($value)) |
| 1279 | ) { |
| 1280 | continue; |
| 1281 | } |
| 1282 | |
| 1283 | $fieldSelected[$pNum] = TRUE; |
| 1284 | |
| 1285 | if (!$hasOptMaxValue || !is_array($value)) { |
| 1286 | continue; |
| 1287 | } |
| 1288 | |
| 1289 | foreach ($value as $optId => $optVal) { |
| 1290 | if (CRM_Utils_Array::value('html_type', $feeBlock[$priceFieldId]) == 'Text') { |
| 1291 | $currentMaxValue = $optVal; |
| 1292 | } |
| 1293 | else { |
| 1294 | $currentMaxValue = 1; |
| 1295 | } |
| 1296 | |
| 1297 | if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) { |
| 1298 | $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal; |
| 1299 | } |
| 1300 | if (empty($optionMaxValues)) { |
| 1301 | $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue; |
| 1302 | } |
| 1303 | else { |
| 1304 | $optionMaxValues[$priceFieldId][$optId] = |
| 1305 | $currentMaxValue + CRM_Utils_Array::value($optId, CRM_Utils_Array::value($priceFieldId, $optionMaxValues), 0); |
| 1306 | } |
| 1307 | |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | //validate for option max value. |
| 1313 | foreach ($optionMaxValues as $fieldId => $values) { |
| 1314 | $options = CRM_Utils_Array::value('options', $feeBlock[$fieldId], array()); |
| 1315 | foreach ($values as $optId => $total) { |
| 1316 | $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId]; |
| 1317 | $opDbCount = CRM_Utils_Array::value('db_total_count', $options[$optId], 0); |
| 1318 | $total += $opDbCount; |
| 1319 | if ($optMax && $total > $optMax) { |
| 1320 | if ($opDbCount && ($opDbCount >= $optMax)) { |
| 1321 | $errors[$currentParticipantNum]["price_{$fieldId}"] = |
| 1322 | ts('Sorry, this option is currently sold out.'); |
| 1323 | } |
| 1324 | elseif (($optMax - $opDbCount) == 1) { |
| 1325 | $errors[$currentParticipantNum]["price_{$fieldId}"] = |
| 1326 | ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount))); |
| 1327 | } |
| 1328 | else { |
| 1329 | $errors[$currentParticipantNum]["price_{$fieldId}"] = |
| 1330 | ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount))); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | //validate for price field selection. |
| 1337 | foreach ($params as $pNum => $values) { |
| 1338 | if (!is_array($values) || $values == 'skip') { |
| 1339 | continue; |
| 1340 | } |
| 1341 | if (empty($fieldSelected[$pNum])) { |
| 1342 | $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).'); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | return $errors; |
| 1347 | } |
| 1348 | |
| 1349 | // set the first participant ID if not set, CRM-10032 |
| 1350 | /** |
| 1351 | * @param $participantID |
| 1352 | */ |
| 1353 | function processFirstParticipant($participantID) { |
| 1354 | $this->_participantId = $participantID; |
| 1355 | $this->set('participantId', $this->_participantId); |
| 1356 | |
| 1357 | $ids = $participantValues = array(); |
| 1358 | $participantParams = array('id' => $this->_participantId); |
| 1359 | CRM_Event_BAO_Participant::getValues($participantParams, $participantValues, $ids); |
| 1360 | $this->_values['participant'] = $participantValues[$this->_participantId]; |
| 1361 | $this->set('values', $this->_values); |
| 1362 | |
| 1363 | // also set the allow confirmation stuff |
| 1364 | if (array_key_exists( |
| 1365 | $this->_values['participant']['status_id'], |
| 1366 | CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'") |
| 1367 | )) { |
| 1368 | $this->_allowConfirmation = TRUE; |
| 1369 | $this->set('allowConfirmation', TRUE); |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | /** |
| 1374 | * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest |
| 1375 | * (probably extract relevant values here & call that with them & handle bounces & redirects here -as |
| 1376 | * those belong in the form layer) |
| 1377 | * |
| 1378 | * @param string $redirect |
| 1379 | */ |
| 1380 | function checkValidEvent($redirect = NULL) { |
| 1381 | // is the event active (enabled)? |
| 1382 | if (!$this->_values['event']['is_active']) { |
| 1383 | // form is inactive, die a fatal death |
| 1384 | CRM_Core_Error::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).')); |
| 1385 | } |
| 1386 | |
| 1387 | // is online registration is enabled? |
| 1388 | if (!$this->_values['event']['is_online_registration']) { |
| 1389 | CRM_Core_Error::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect); |
| 1390 | } |
| 1391 | |
| 1392 | // is this an event template ? |
| 1393 | if (!empty($this->_values['event']['is_template'])) { |
| 1394 | CRM_Core_Error::statusBounce(ts('Event templates are not meant to be registered.'), $redirect); |
| 1395 | } |
| 1396 | |
| 1397 | $now = date('YmdHis'); |
| 1398 | $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_start_date', |
| 1399 | $this->_values['event'] |
| 1400 | )); |
| 1401 | |
| 1402 | if ( |
| 1403 | $startDate && |
| 1404 | $startDate >= $now |
| 1405 | ) { |
| 1406 | CRM_Core_Error::statusBounce(ts('Registration for this event begins on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_start_date', $this->_values['event'])))), $redirect); |
| 1407 | } |
| 1408 | |
| 1409 | $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date', |
| 1410 | $this->_values['event'] |
| 1411 | )); |
| 1412 | if ( |
| 1413 | $endDate && |
| 1414 | $endDate < $now |
| 1415 | ) { |
| 1416 | CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event'])))), $redirect); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |