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