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