Merge remote-tracking branch 'origin/abtest' into master-abtest
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Registration.php
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_ManageEvent_Registration extends CRM_Event_Form_ManageEvent {
42
43 /**
44 * what blocks should we show and hide.
45 *
46 * @var CRM_Core_ShowHideBlocks
47 */
48 protected $_showHide;
49
50 protected $_profilePostMultiple = array();
51 protected $_profilePostMultipleAdd = array();
52
53 /**
54 * Function to set variables up before form is built
55 *
56 * @return void
57 * @access public
58 */
59 function preProcess() {
60 $this->_addProfileBottom = CRM_Utils_Array::value('addProfileBottom', $_GET, FALSE);
61 $this->_profileBottomNum = CRM_Utils_Array::value('addProfileNum', $_GET, 0);
62 $this->_addProfileBottomAdd = CRM_Utils_Array::value('addProfileBottomAdd', $_GET, FALSE);
63 $this->_profileBottomNumAdd = CRM_Utils_Array::value('addProfileNumAdd', $_GET, 0);
64
65 parent::preProcess();
66
67 $this->assign('addProfileBottom', $this->_addProfileBottom);
68 $this->assign('profileBottomNum', $this->_profileBottomNum);
69
70 $urlParams = "id={$this->_id}&addProfileBottom=1&qfKey={$this->controller->_key}";
71 $this->assign('addProfileParams', $urlParams);
72
73 if ($addProfileBottom = CRM_Utils_Array::value('custom_post_id_multiple', $_POST)) {
74 foreach (array_keys($addProfileBottom) as $profileNum) {
75 self::buildMultipleProfileBottom($this, $profileNum);
76 }
77 }
78
79 $this->assign('addProfileBottomAdd', $this->_addProfileBottomAdd);
80 $this->assign('profileBottomNumAdd', $this->_profileBottomNumAdd);
81
82 $urlParamsAdd = "id={$this->_id}&addProfileBottomAdd=1&qfKey={$this->controller->_key}";
83 $this->assign('addProfileParamsAdd', $urlParamsAdd);
84
85 if ($addProfileBottomAdd = CRM_Utils_Array::value('additional_custom_post_id_multiple', $_POST)) {
86 foreach (array_keys($addProfileBottomAdd) as $profileNum) {
87 self::buildMultipleProfileBottom($this, $profileNum, 'additional_', ts('Profile for Additional Participants'));
88 }
89 }
90 }
91
92 /**
93 * This function sets the default values for the form.
94 * the default values are retrieved from the database
95 *
96 * @access public
97 *
98 * @return void
99 */
100 function setDefaultValues() {
101 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
102 return;
103 }
104 $eventId = $this->_id;
105
106 $defaults = parent::setDefaultValues();
107
108 $this->setShowHide($defaults);
109 if (isset($eventId)) {
110 $params = array('id' => $eventId);
111 CRM_Event_BAO_Event::retrieve($params, $defaults);
112
113 $ufJoinParams = array(
114 'entity_table' => 'civicrm_event',
115 'module' => 'CiviEvent',
116 'entity_id' => $eventId,
117 );
118
119 list($defaults['custom_pre_id'],
120 $defaults['custom_post']
121 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
122
123 // Get the id for the event registration profile
124 $eventRegistrationIdParams = $eventRegistrationIdDefaults = array(
125 'name' => 'event_registration',
126 );
127 CRM_Core_BAO_UFGroup::retrieve($eventRegistrationIdParams, $eventRegistrationIdDefaults);
128
129 // Set event registration as the default profile if none selected
130 if (!$defaults['custom_pre_id'] && count($defaults['custom_post']) == 0) {
131 $defaults['custom_pre_id'] = CRM_Utils_Array::value('id', $eventRegistrationIdDefaults);
132 }
133 if (isset($defaults['custom_post']) && is_numeric($defaults['custom_post'])) {
134 $defaults['custom_post_id'] = $defaults['custom_post'];
135 }
136 elseif (!empty($defaults['custom_post'])) {
137 $defaults['custom_post_id'] = $defaults['custom_post'][0];
138 unset($defaults['custom_post'][0]);
139 $this->_profilePostMultiple = $defaults['custom_post'];
140 foreach ($defaults['custom_post'] as $key => $value) {
141 self::buildMultipleProfileBottom($this, $key);
142 $defaults["custom_post_id_multiple[$key]"] = $value;
143 }
144 }
145
146 $this->assign('profilePostMultiple', CRM_Utils_Array::value('custom_post', $defaults));
147
148 if (!empty($defaults['is_multiple_registrations'])) {
149 // CRM-4377: set additional participants’ profiles – set to ‘none’ if explicitly unset (non-active)
150
151 $ufJoinAddParams = array(
152 'entity_table' => 'civicrm_event',
153 'module' => 'CiviEvent_Additional',
154 'entity_id' => $eventId,
155 );
156
157 list($defaults['additional_custom_pre_id'],
158 $defaults['additional_custom_post']
159 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinAddParams);
160
161 if (isset($defaults['additional_custom_post']) && is_numeric($defaults['additional_custom_post'])) {
162 $defaults['additional_custom_post_id'] = $defaults['additional_custom_post'];
163 }
164 elseif (!empty($defaults['additional_custom_post'])) {
165 $defaults['additional_custom_post_id'] = $defaults['additional_custom_post'][0];
166 unset($defaults['additional_custom_post'][0]);
167
168 $this->_profilePostMultipleAdd = $defaults['additional_custom_post'];
169 foreach ($defaults['additional_custom_post'] as $key => $value) {
170 self::buildMultipleProfileBottom($this, $key, 'additional_', ts('Profile for Additional Participants'));
171 $defaults["additional_custom_post_id_multiple[$key]"] = $value;
172 }
173 }
174 $this->assign('profilePostMultipleAdd', CRM_Utils_Array::value('additional_custom_post', $defaults));
175 }
176 }
177 else {
178 $defaults['is_email_confirm'] = 0;
179 }
180
181 // provide defaults for required fields if empty (and as a 'hint' for approval message field)
182 $defaults['registration_link_text'] = CRM_Utils_Array::value('registration_link_text', $defaults, ts('Register Now'));
183 $defaults['confirm_title'] = CRM_Utils_Array::value('confirm_title', $defaults, ts('Confirm Your Registration Information'));
184 $defaults['thankyou_title'] = CRM_Utils_Array::value('thankyou_title', $defaults, ts('Thank You for Registering'));
185 $defaults['approval_req_text'] = CRM_Utils_Array::value('approval_req_text', $defaults, ts('Participation in this event requires approval. Submit your registration request here. Once approved, you will receive an email with a link to a web page where you can complete the registration process.'));
186
187 if (!empty($defaults['registration_start_date'])) {
188 list($defaults['registration_start_date'],
189 $defaults['registration_start_date_time']
190 ) = CRM_Utils_Date::setDateDefaults($defaults['registration_start_date'],
191 'activityDateTime'
192 );
193 }
194
195 if (!empty($defaults['registration_end_date'])) {
196 list($defaults['registration_end_date'],
197 $defaults['registration_end_date_time']
198 ) = CRM_Utils_Date::setDateDefaults($defaults['registration_end_date'],
199 'activityDateTime'
200 );
201 }
202
203 return $defaults;
204 }
205
206 /**
207 * Fix what blocks to show/hide based on the default values set
208 *
209 * @param array $defaults the array of default values
210 *
211 * @internal param bool $force should we set show hide based on input defaults
212 *
213 * @return void
214 */
215 function setShowHide($defaults) {
216 $this->_showHide = new CRM_Core_ShowHideBlocks(array('registration' => 1),
217 ''
218 );
219 if (empty($defaults)) {
220 $this->_showHide->addHide('registration');
221 $this->_showHide->addHide('id-approval-text');
222 }
223 else {
224 if (empty($defaults['requires_approval'])) {
225 $this->_showHide->addHide('id-approval-text');
226 }
227 }
228 $this->assign('defaultsEmpty', empty($defaults));
229 $this->_showHide->addToTemplate();
230 }
231
232 /**
233 * Function to build the form
234 *
235 * @return void
236 * @access public
237 */
238 public function buildQuickForm() {
239 if ($this->_addProfileBottom) {
240 return self::buildMultipleProfileBottom($this, $this->_profileBottomNum);
241 }
242
243 if ($this->_addProfileBottomAdd) {
244 return self::buildMultipleProfileBottom($this, $this->_profileBottomNumAdd, 'additional_', ts('Profile for Additional Participants'));
245 }
246
247 $this->applyFilter('__ALL__', 'trim');
248 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
249
250 $this->addElement('checkbox',
251 'is_online_registration',
252 ts('Allow Online Registration?'),
253 NULL,
254 array(
255 'onclick' => "return showHideByValue('is_online_registration',
256 '',
257 'registration_blocks',
258 'block',
259 'radio',
260 false );",
261 )
262 );
263
264 $this->add('text', 'registration_link_text', ts('Registration Link Text'));
265
266 if (!$this->_isTemplate) {
267 $this->addDateTime('registration_start_date', ts('Registration Start Date'), FALSE, array('formatType' => 'activityDateTime'));
268 $this->addDateTime('registration_end_date', ts('Registration End Date'), FALSE, array('formatType' => 'activityDateTime'));
269 }
270
271 $params = array(
272 'used' => 'Supervised',
273 'contact_type' => 'Individual',
274 );
275 $dedupeRuleFields = CRM_Dedupe_BAO_Rule::dedupeRuleFields($params);
276
277 foreach ($dedupeRuleFields as $key => $fields) {
278 $ruleFields[$key] = ucwords(str_replace('_', ' ', $fields));
279 }
280
281 $this->addElement('checkbox',
282 'is_multiple_registrations',
283 ts('Register multiple participants?')
284 );
285
286 $this->addElement('checkbox',
287 'allow_same_participant_emails',
288 ts('Same email address?')
289 );
290 $this->assign('ruleFields', json_encode($ruleFields));
291
292 $dedupeRules = array(
293 '' => '- Unsupervised rule -',
294 );
295 $dedupeRules += CRM_Dedupe_BAO_RuleGroup::getByType('Individual');
296 $this->add('select', 'dedupe_rule_group_id', ts('Duplicate matching rule'), $dedupeRules);
297
298 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
299 if (in_array('Awaiting approval', $participantStatuses) and in_array('Pending from approval', $participantStatuses) and in_array('Rejected', $participantStatuses)) {
300 $this->addElement('checkbox',
301 'requires_approval',
302 ts('Require participant approval?'),
303 NULL,
304 array('onclick' => "return showHideByValue('requires_approval', '', 'id-approval-text', 'table-row', 'radio', false);")
305 );
306 $this->add('textarea', 'approval_req_text', ts('Approval message'), $attributes['approval_req_text']);
307 }
308
309 $this->add('text', 'expiration_time', ts('Pending participant expiration (hours)'));
310 $this->addRule('expiration_time', ts('Please enter the number of hours (as an integer).'), 'integer');
311
312 self::buildRegistrationBlock($this);
313 self::buildConfirmationBlock($this);
314 self::buildMailBlock($this);
315 self::buildThankYouBlock($this);
316
317 parent::buildQuickForm();
318 }
319
320 /**
321 * Function to build Registration Block
322 *
323 * @param $form
324 *
325 * @internal param int $pageId
326 * @static
327 */
328 function buildRegistrationBlock(&$form) {
329 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
330 $attributes['intro_text']['click_wysiwyg'] = true;
331 $form->addWysiwyg('intro_text', ts('Introductory Text'), $attributes['intro_text']);
332 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
333 // explicit height and width.
334 $footerAttribs = array(
335 'rows' => 2,
336 'cols' => 40,
337 'click_wysiwyg' => true,
338 );
339 $form->addWysiwyg('footer_text', ts('Footer Text'), $footerAttribs);
340
341 extract( self::getProfileSelectorTypes() );
342 //CRM-15427
343 $form->addProfileSelector( 'custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE);
344 $form->addProfileSelector( 'custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE);
345
346 $form->addProfileSelector( 'additional_custom_pre_id', ts('Profile for Additional Participants') . '<br />' . ts('(top of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE);
347 $form->addProfileSelector( 'additional_custom_post_id', ts('Profile for Additional Participants') . '<br />' . ts('(bottom of page)'), $allowCoreTypes, $allowSubTypes, $profileEntities, TRUE);
348 }
349
350 /**
351 * Subroutine to insert a Profile Editor widget
352 * depends on getProfileSelectorTypes
353 *
354 * @param array &$form
355 * @param int $count unique index
356 * @param string $prefix dom element ID prefix
357 * @param string $label Label
358 * @param array $configs Optional, for addProfileSelector(), defaults to using getProfileSelectorTypes()
359 **/
360 function buildMultipleProfileBottom(&$form, $count, $prefix = '', $label = 'Include Profile', $configs = null) {
361 extract( ( is_null($configs) ) ? self::getProfileSelectorTypes() : $configs );
362 $element = $prefix . "custom_post_id_multiple[$count]";
363 $label .= '<br />'.ts('(bottom of page)');
364 $form->addProfileSelector( $element, $label, $allowCoreTypes, $allowSubTypes, $profileEntities);
365 }
366
367 /**
368 * Create initializers for addprofileSelector
369 *
370 * @return array( 'allowCoreTypes' => array(), 'allowSubTypes' => array(), 'profileEntities' => array() )
371 **/
372 static function getProfileSelectorTypes() {
373 $configs = array(
374 'allowCoreTypes' => array(),
375 'allowSubTypes' => array(),
376 'profileEntities' => array(),
377 );
378
379 $configs['allowCoreTypes'][] = 'Contact';
380 $configs['allowCoreTypes'][] = 'Individual';
381 $configs['allowCoreTypes'][] = 'Participant';
382 //CRM-15427
383 $participantEventType = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $_GET['id'], 'event_type_id', 'id');
384 $participantRole = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $_GET['id'], 'default_role_id');
385 $configs['allowSubTypes']['ParticipantEventName'] = array($_GET['id']);
386 $configs['allowSubTypes']['ParticipantEventType'] = array($participantEventType);
387 $configs['allowSubTypes']['ParticipantRole'] = array($participantRole);
388
389 $configs['profileEntities'][] = array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel');
390 $configs['profileEntities'][] = array('entity_name' => 'participant_1', 'entity_type' => 'ParticipantModel', 'entity_sub_type' => '*');
391
392 return $configs;
393 }
394
395 /**
396 * Function to build Confirmation Block
397 *
398 * @param $form
399 *
400 * @internal param int $pageId
401 * @static
402 */
403 function buildConfirmationBlock(&$form) {
404 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
405 $attributes['confirm_text']['click_wysiwyg'] = true;
406 // CRM-11182 - Optional confirmation page for free events
407 $is_monetary = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $form->_id, 'is_monetary');
408 $form->assign('is_monetary', $is_monetary);
409 if ($is_monetary == "0") {
410 $form->addYesNo('is_confirm_enabled', ts('Use a confirmation screen?'), NULL, NULL, array('onclick' => "return showHideByValue('is_confirm_enabled','','confirm_screen_settings','block','radio',false);"));
411 }
412 $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']);
413 $form->addWysiwyg('confirm_text', ts('Introductory Text'), $attributes['confirm_text']);
414 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
415 // explicit height and width.
416 $footerAttribs = array(
417 'rows' => 2,
418 'cols' => 40,
419 'click_wysiwyg' => true,
420 );
421 $form->addWysiwyg('confirm_footer_text', ts('Footer Text'), $footerAttribs);
422 }
423
424 /**
425 * Function to build Email Block
426 *
427 * @param $form
428 *
429 * @internal param int $pageId
430 * @static
431 */
432 function buildMailBlock(&$form) {
433 $form->registerRule('emailList', 'callback', 'emailList', 'CRM_Utils_Rule');
434 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
435 $form->addYesNo('is_email_confirm', ts('Send Confirmation Email?'), NULL, NULL, array('onclick' => "return showHideByValue('is_email_confirm','','confirmEmail','block','radio',false);"));
436 $form->add('textarea', 'confirm_email_text', ts('Text'), $attributes['confirm_email_text']);
437 $form->add('text', 'cc_confirm', ts('CC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'cc_confirm'));
438 $form->addRule('cc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
439 $form->add('text', 'bcc_confirm', ts('BCC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'bcc_confirm'));
440 $form->addRule('bcc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
441 $form->add('text', 'confirm_from_name', ts('Confirm From Name'));
442 $form->add('text', 'confirm_from_email', ts('Confirm From Email'));
443 $form->addRule('confirm_from_email', ts('Email is not valid.'), 'email');
444 }
445
446 /**
447 * @param $form
448 */
449 function buildThankYouBlock(&$form) {
450 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
451 $attributes['thankyou_text']['click_wysiwyg'] = true;
452 $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']);
453 $form->addWysiwyg('thankyou_text', ts('Introductory Text'), $attributes['thankyou_text']);
454 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
455 // explicit height and width.
456 $footerAttribs = array(
457 'rows' => 2,
458 'cols' => 40,
459 'click_wysiwyg' => true,
460 );
461 $form->addWysiwyg('thankyou_footer_text', ts('Footer Text'), $footerAttribs);
462 }
463
464 /**
465 * Add local and global form rules
466 *
467 * @access protected
468 *
469 * @return void
470 */
471 function addRules() {
472 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
473 return;
474 }
475 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Registration', 'formRule'), $this);
476 }
477
478 /**
479 * global validation rules for the form
480 *
481 * @param $values
482 * @param $files
483 * @param $form
484 *
485 * @internal param array $fields posted values of the form
486 *
487 * @return array list of errors to be posted back to the form
488 * @static
489 * @access public
490 */
491 static function formRule($values, $files, $form) {
492 if (!empty($values['is_online_registration'])) {
493
494 if (!$values['confirm_title']) {
495 $errorMsg['confirm_title'] = ts('Please enter a Title for the registration Confirmation Page');
496 }
497 if (!$values['thankyou_title']) {
498 $errorMsg['thankyou_title'] = ts('Please enter a Title for the registration Thank-you Page');
499 }
500 if ($values['is_email_confirm']) {
501 if (!$values['confirm_from_name']) {
502 $errorMsg['confirm_from_name'] = ts('Please enter Confirmation Email FROM Name.');
503 }
504
505 if (!$values['confirm_from_email']) {
506 $errorMsg['confirm_from_email'] = ts('Please enter Confirmation Email FROM Email Address.');
507 }
508 }
509
510 if (
511 isset($values['registration_start_date']) &&
512 isset($values['registration_end_date'])
513 ) {
514 $start = CRM_Utils_Date::processDate($values['registration_start_date']);
515 $end = CRM_Utils_Date::processDate($values['registration_end_date']);
516 if ($end < $start) {
517 $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
518 }
519 }
520
521 //check that the selected profiles have either firstname+lastname or email required
522 $profileIds = array(
523 CRM_Utils_Array::value('custom_pre_id', $values),
524 CRM_Utils_Array::value('custom_post_id', $values),
525 );
526 $additionalProfileIds = array(
527 CRM_Utils_Array::value('additional_custom_pre_id', $values),
528 CRM_Utils_Array::value('additional_custom_post_id', $values),
529 );
530 //additional profile fields default to main if not set
531 if (!is_numeric($additionalProfileIds[0])) {
532 $additionalProfileIds[0] = $profileIds[0];
533 }
534 if (!is_numeric($additionalProfileIds[1])) {
535 $additionalProfileIds[1] = $profileIds[1];
536 }
537 //add multiple profiles if set
538 self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple');
539 self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple');
540 $isProfileComplete = self::isProfileComplete($profileIds);
541 $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds);
542
543 //Check main profiles have an email address available if 'send confirmation email' is selected
544 if ($values['is_email_confirm']) {
545 $emailFields = self::getEmailFields($profileIds);
546 if (!count($emailFields)) {
547 $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected");
548 }
549 }
550 $additionalCustomPreId = $additionalCustomPostId = NULL;
551 $isPreError = $isPostError = TRUE;
552 if (!empty($values['allow_same_participant_emails']) && !empty($values['is_multiple_registrations'])) {
553 $types = array_merge(array('Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
554 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
555
556 //check for additional custom pre profile
557 $additionalCustomPreId = CRM_Utils_Array::value('additional_custom_pre_id', $values);
558 if (!empty($additionalCustomPreId)) {
559 if (!($additionalCustomPreId == 'none')) {
560 $customPreId = $additionalCustomPreId;
561 }
562 else {
563 $isPreError = FALSE;
564 }
565 }
566 else {
567 $customPreId = !empty($values['custom_pre_id']) ? $values['custom_pre_id'] : NULL;
568 }
569 //check whether the additional custom pre profile is of type 'Individual' and its subtypes
570 if (!empty($customPreId)) {
571 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId);
572 foreach ($types as $individualTypes) {
573 if (in_array($individualTypes, $profileTypes)) {
574 $isPreError = FALSE;
575 break;
576 }
577 }
578 }
579 else {
580 $isPreError = FALSE;
581 }
582
583 // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile
584 if ($isPreError) {
585 $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values);
586 if (!empty($additionalCustomPostId)) {
587 if (!($additionalCustomPostId == 'none')) {
588 $customPostId = $additionalCustomPostId;
589 }
590 else {
591 $isPostError = FALSE;
592 }
593 }
594 else {
595 $customPostId = !empty($values['custom_post_id']) ? $values['custom_post_id'] : NULL;
596 }
597 //check whether the additional custom post profile is of type 'Individual' and its subtypes
598 if (!empty($customPostId)) {
599 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
600 foreach ($types as $individualTypes) {
601 if (in_array($individualTypes, $profileTypes)) {
602 $isPostError = FALSE;
603 break;
604 }
605 }
606 }
607 else {
608 $isPostError = FALSE;
609 }
610
611 if (empty($customPreId) && empty($customPostId)) {
612 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
613 }
614 if ($isPostError) {
615 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
616 }
617 }
618 }
619 if (!$isProfileComplete) {
620 $errorMsg['custom_pre_id'] = ts("Please include a Profile for online registration that contains an Email Address field and / or First Name + Last Name fields.");
621 }
622 if (!$isAdditionalProfileComplete) {
623 $errorMsg['additional_custom_pre_id'] = ts("Please include a Profile for online registration of additional participants that contains an Email Address field and / or First Name + Last Name fields.");
624 }
625
626 // // CRM-8485
627 // $config = CRM_Core_Config::singleton();
628 // if ( $config->doNotAttachPDFReceipt ) {
629 // if (!empty($values['custom_post_id_multiple'])) {
630 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
631 // if ( $customPostMultiple ) {
632 // $errorMsg["custom_post_id_multiple[{$count}]"] = ts('Please disable PDF receipt as an attachment in <a href="%1">Miscellaneous Settings</a> if you want to add additional profiles.', array( 1 => CRM_Utils_System::url( 'civicrm/admin/setting/misc', 'reset=1' ) ) );
633 // break;
634 // }
635 // }
636 // }
637 //
638 // if (!empty($values['is_multiple_registrations']) &&
639 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
640 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
641 // if ( $customPostMultiple ) {
642 // $errorMsg["additional_custom_post_id_multiple[{$count}]"] = ts('Please disable PDF receipt as an attachment in <a href="%1">Miscellaneous Settings</a> if you want to add additional profiles.', array( 1 => CRM_Utils_System::url( 'civicrm/admin/setting/misc', 'reset=1' ) ) );
643 // break;
644 // }
645 // }
646 // }
647 // }
648
649 if (!empty($errorMsg)) {
650 if (!empty($values['custom_post_id_multiple'])) {
651 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
652 self::buildMultipleProfileBottom($form, $count);
653 }
654 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
655 }
656 if (!empty($values['additional_custom_post_id_multiple'])) {
657 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
658 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
659 }
660 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
661 }
662 }
663 }
664
665 if (!empty($errorMsg)) {
666 return $errorMsg;
667 }
668
669 return TRUE;
670 }
671
672 /**
673 * Collect all email fields for an array of profile ids
674 *
675 * @param $profileIds
676 * @return boolean
677 */
678 static function getEmailFields($profileIds) {
679 $emailFields = array();
680 foreach ($profileIds as $profileId) {
681 if ($profileId && is_numeric($profileId)) {
682 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
683 foreach ($fields as $field) {
684 if (substr_count($field['name'], 'email')) {
685 $emailFields[] = $field;
686 }
687 }
688 }
689 }
690 return $emailFields;
691 }
692
693 /**
694 * Check if a profile contains required fields
695 *
696 * @param $profileIds
697 * @return boolean
698 */
699 static function isProfileComplete($profileIds) {
700 $profileReqFields = array();
701 foreach ($profileIds as $profileId) {
702 if ($profileId && is_numeric($profileId)) {
703 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
704 foreach ($fields as $field) {
705 switch (TRUE) {
706 case substr_count($field['name'], 'email'):
707 $profileReqFields[] = 'email';
708 break;
709
710 case substr_count($field['name'], 'first_name'):
711 $profileReqFields[] = 'first_name';
712 break;
713
714 case substr_count($field['name'], 'last_name'):
715 $profileReqFields[] = 'last_name';
716 break;
717 }
718 }
719 }
720 }
721 $profileComplete = (in_array('email', $profileReqFields)
722 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
723 );
724 return $profileComplete;
725 }
726
727 /**
728 * Check if the profiles collect enough information to dedupe
729 *
730 * @param $profileIds
731 * @param int $rgId
732 * @return boolean
733 */
734
735 function canProfilesDedupe($profileIds, $rgId = 0) {
736
737 // find the unsupervised rule
738
739 $rgParams = array(
740 'used' => 'Unsupervised',
741 'contact_type' => 'Individual',
742 );
743 if ($rgId > 0) {
744 $rgParams['id'] = $rgId;
745 }
746 $activeRg = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($rgParams);
747
748 // get the combinations that could be a match for the rule
749 $okCombos = $combos = array();
750 CRM_Dedupe_BAO_RuleGroup::combos($activeRg[0], $activeRg[1], $combos);
751
752 // create an index of what combinations involve each field
753 $index = array();
754 foreach ($combos as $comboid => $combo) {
755 foreach ($combo as $cfield) {
756 $index[$cfield][$comboid] = true;
757 }
758 $combos[$comboid] = array_fill_keys($combo, 0);
759 $okCombos[$comboid] = array_fill_keys($combo, 2);
760 }
761
762 // get profiles and see if they have the necessary combos
763 $profileReqFields = array();
764 foreach ($profileIds as $profileId) {
765 if ($profileId && is_numeric($profileId)) {
766 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
767
768 // walk through the fields in the profile
769 foreach ($fields as $field) {
770
771 // check each of the fields in the index against the profile field
772 foreach ($index as $ifield => $icombos) {
773 if(strpos($field['name'], $ifield) !== false) {
774
775 // we found the field in the profile, now record it in the index
776 foreach ($icombos as $icombo => $dontcare) {
777 $combos[$icombo][$ifield] = ($combos[$icombo][$ifield] != 2 && !$field['is_required']) ? 1 : 2;
778
779 if ($combos[$icombo] == $okCombos[$icombo]) {
780 // if any combo is complete with 2s (all fields are present and required), we can go home
781 return 2;
782 }
783 }
784 }
785 }
786 }
787 }
788 }
789
790 // check the combos to see if everything is > 0
791 foreach ($combos as $comboid => $combo) {
792 $complete = false;
793 foreach ($combo as $cfield) {
794 if ($cfield > 0) {
795 $complete = true;
796 }
797 else {
798 // this combo isn't complete--skip to the next combo
799 continue 2;
800 }
801 }
802 if ($complete) { return 1; }
803 }
804
805 // no combo succeeded
806 return 0;
807 }
808
809 /**
810 * Add additional profiles from the form to an array of profile ids.
811 *
812 */
813 static function addMultipleProfiles(&$profileIds, $values, $field) {
814 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
815 foreach ($multipleProfiles as $profileId) {
816 $profileIds[] = $profileId;
817 }
818 }
819 }
820
821 /**
822 * Function to process the form
823 *
824 * @access public
825 *
826 * @return void
827 */
828 public function postProcess() {
829 $params = $this->exportValues();
830
831 $params['id'] = $this->_id;
832
833 // format params
834 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
835 $params['is_confirm_enabled'] = CRM_Utils_Array::value('is_confirm_enabled', $params, FALSE); // CRM-11182
836 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
837 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
838 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
839
840 // reset is_email confirm if not online reg
841 if (!$params['is_online_registration']) {
842 $params['is_email_confirm'] = FALSE;
843 }
844
845 if (!$this->_isTemplate) {
846 $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'],
847 $params['registration_start_date_time'],
848 TRUE
849 );
850 $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'],
851 $params['registration_end_date_time'],
852 TRUE
853 );
854 }
855
856 CRM_Event_BAO_Event::add($params);
857
858 // also update the ProfileModule tables
859 $ufJoinParams = array(
860 'is_active' => 1,
861 'module' => 'CiviEvent',
862 'entity_table' => 'civicrm_event',
863 'entity_id' => $this->_id,
864 );
865
866
867 // first delete all past entries
868 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
869
870 $uf = array();
871 $wt = 2;
872 if (!empty($params['custom_pre_id'])) {
873 $uf[1] = $params['custom_pre_id'];
874 $wt = 1;
875 }
876
877 if (!empty($params['custom_post_id'])) {
878 $uf[2] = $params['custom_post_id'];
879 }
880
881 if (!empty($params['custom_post_id_multiple'])) {
882 $uf = array_merge($uf, $params['custom_post_id_multiple']);
883 }
884 $uf = array_values($uf);
885 if (!empty($uf)) {
886 foreach ($uf as $weight => $ufGroupId) {
887 $ufJoinParams['weight'] = $weight + $wt;
888 $ufJoinParams['uf_group_id'] = $ufGroupId;
889 CRM_Core_BAO_UFJoin::create($ufJoinParams);
890 unset($ufJoinParams['id']);
891 }
892 }
893 // also update the ProfileModule tables
894 $ufJoinParamsAdd = array(
895 'is_active' => 1,
896 'module' => 'CiviEvent_Additional',
897 'entity_table' => 'civicrm_event',
898 'entity_id' => $this->_id,
899 );
900
901 // first delete all past entries
902 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
903 if (!empty($params['is_multiple_registrations'])) {
904 $ufAdd = array();
905 $wtAdd = 2;
906
907 if (array_key_exists('additional_custom_pre_id', $params)) {
908 if (empty($params['additional_custom_pre_id'])) {
909 $ufAdd[1] = $params['custom_pre_id'];
910 $wtAdd = 1;
911 }
912 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {}
913 else {
914 $ufAdd[1] = $params['additional_custom_pre_id'];
915 $wtAdd = 1;
916 }
917 }
918
919 if (array_key_exists('additional_custom_post_id', $params)) {
920 if (empty($params['additional_custom_post_id'])) {
921 $ufAdd[2] = $params['custom_post_id'];
922 }
923 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {}
924 else {
925 $ufAdd[2] = $params['additional_custom_post_id'];
926 }
927 }
928
929 if (!empty($params['additional_custom_post_id_multiple'])) {
930 $additionalPostMultiple = array();
931 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
932 if (!$value && !empty($params['custom_post_id'])) {
933 $additionalPostMultiple[$key] = $params['custom_post_id'];
934 }
935 elseif ($value == 'none') {
936 continue;
937 }
938 elseif ($value) {
939 $additionalPostMultiple[$key] = $value;
940 }
941 }
942 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
943 }
944
945 $ufAdd = array_values($ufAdd);
946 if (!empty($ufAdd)) {
947 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
948
949 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
950 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
951
952 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
953 unset($ufJoinParamsAdd['id']);
954 }
955 }
956 }
957
958 // get the profiles to evaluate what they collect
959 $profileIds = array(
960 CRM_Utils_Array::value('custom_pre_id', $params),
961 CRM_Utils_Array::value('custom_post_id', $params),
962 );
963 $additionalProfileIds = array(
964 CRM_Utils_Array::value('additional_custom_pre_id', $params),
965 CRM_Utils_Array::value('additional_custom_post_id', $params),
966 );
967 // additional profile fields default to main if not set
968 if (!is_numeric($additionalProfileIds[0])) {
969 $additionalProfileIds[0] = $profileIds[0];
970 }
971 if (!is_numeric($additionalProfileIds[1])) {
972 $additionalProfileIds[1] = $profileIds[1];
973 }
974 //add multiple profiles if set
975 self::addMultipleProfiles($profileIds, $params, 'custom_post_id_multiple');
976 self::addMultipleProfiles($additionalProfileIds, $params, 'additional_custom_post_id_multiple');
977
978 $cantDedupe = false;
979 $rgId = CRM_Utils_Array::value('dedupe_rule_group_id', $params, 0);
980
981 switch (self::canProfilesDedupe($profileIds, $rgId)) {
982 case 0:
983 $dedupeTitle = 'Duplicate Matching Impossible';
984 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match registrations with existing contacts. This means all anonymous registrations will result in a new contact.");
985 break;
986 case 1:
987 $dedupeTitle = 'Duplicate Contacts Possible';
988 $cantDedupe = ts("The selected profiles can collect enough information to match registrations with existing contacts, but not all of the relevant fields are required. Anonymous registrations may result in duplicate contacts.");
989 }
990 if (!empty($params['is_multiple_registrations'])) {
991 switch(self::canProfilesDedupe($additionalProfileIds, $rgId)) {
992 case 0:
993 $dedupeTitle = 'Duplicate Matching Impossible';
994 if ($cantDedupe) {
995 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match registrations with existing contacts. This means all anonymous registrations will result in a new contact.");
996 }
997 else {
998 $cantDedupe = ts("The selected profiles do not contain the fields necessary to match additional participants with existing contacts. This means all additional participants will result in a new contact.");
999 }
1000 break;
1001 case 1:
1002 if (!$cantDedupe) {
1003 $dedupeTitle = 'Duplicate Contacts Possible';
1004 $cantDedupe = ts("The selected profiles can collect enough information to match additional participants with existing contacts, but not all of the relevant fields are required. This may result in duplicate contacts.");
1005 }
1006 }
1007 }
1008 if ($cantDedupe) {
1009 CRM_Core_Session::setStatus($cantDedupe, $dedupeTitle, 'alert dedupenotify', array('expires' => 0));
1010 }
1011
1012 // Update tab "disabled" css class
1013 $this->ajaxResponse['tabValid'] = !empty($params['is_online_registration']);
1014
1015 parent::endPostProcess();
1016 }
1017 //end of function
1018
1019 /**
1020 * Return a descriptive name for the page, used in wizard header
1021 *
1022 * @return string
1023 * @access public
1024 */
1025 public function getTitle() {
1026 return ts('Online Registration');
1027 }
1028 }
1029