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