Merge branch '4.3' of github.com:civicrm/civicrm-core
[civicrm-core.git] / CRM / Event / Form / ManageEvent / Registration.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 None
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 (CRM_Utils_Array::value('is_multiple_registrations', $defaults)) {
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 (CRM_Utils_Array::value('registration_start_date', $defaults)) {
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 (CRM_Utils_Array::value('registration_end_date', $defaults)) {
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 * @param boolean $force should we set show hide based on input defaults
211 *
212 * @return void
213 */
214 function setShowHide($defaults) {
215 $this->_showHide = new CRM_Core_ShowHideBlocks(array('registration' => 1),
216 ''
217 );
218 if (empty($defaults)) {
219 $this->_showHide->addHide('registration');
220 $this->_showHide->addHide('additional_profile_pre');
221 $this->_showHide->addHide('additional_profile_post');
222 $this->_showHide->addHide('id-approval-text');
223 }
224 else {
225 if (!CRM_Utils_Array::value('is_multiple_registrations', $defaults)) {
226 $this->_showHide->addHide('additional_profile_pre');
227 $this->_showHide->addHide('additional_profile_post');
228 }
229 if (!CRM_Utils_Array::value('requires_approval', $defaults)) {
230 $this->_showHide->addHide('id-approval-text');
231 }
232 }
233 $this->assign('defaultsEmpty', empty($defaults));
234 $this->_showHide->addToTemplate();
235 }
236
237 /**
238 * Function to build the form
239 *
240 * @return None
241 * @access public
242 */
243 public function buildQuickForm() {
244 if ($this->_addProfileBottom) {
245 return self::buildMultipleProfileBottom($this, $this->_profileBottomNum);
246 }
247
248 if ($this->_addProfileBottomAdd) {
249 return self::buildMultipleProfileBottom($this, $this->_profileBottomNumAdd, 'additional_', ts('Profile for Additional Participants'));
250 }
251
252 $this->applyFilter('__ALL__', 'trim');
253 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
254
255 $this->addElement('checkbox',
256 'is_online_registration',
257 ts('Allow Online Registration?'),
258 NULL,
259 array(
260 'onclick' => "return showHideByValue('is_online_registration',
261 '',
262 'registration_blocks',
263 'block',
264 'radio',
265 false );",
266 )
267 );
268
269 $this->add('text', 'registration_link_text', ts('Registration Link Text'));
270
271 if (!$this->_isTemplate) {
272 $this->addDateTime('registration_start_date', ts('Registration Start Date'), FALSE, array('formatType' => 'activityDateTime'));
273 $this->addDateTime('registration_end_date', ts('Registration End Date'), FALSE, array('formatType' => 'activityDateTime'));
274 }
275
276 $params = array(
277 'used' => 'Supervised',
278 'contact_type' => 'Individual',
279 );
280 $dedupeRuleFields = CRM_Dedupe_BAO_Rule::dedupeRuleFields($params);
281
282 foreach ($dedupeRuleFields as $key => $fields) {
283 $ruleFields[$key] = ucwords(str_replace('_', ' ', $fields));
284 }
285
286 $this->addElement('checkbox',
287 'is_multiple_registrations',
288 ts('Register multiple participants?'),
289 NULL,
290 array(
291 'onclick' => "return (showHideByValue('is_multiple_registrations', '', 'additional_profile_pre|additional_profile_post', 'table-row', 'radio', false) ||
292 showRuleFields( " . json_encode($ruleFields) . " ));")
293 );
294
295 $this->addElement('checkbox',
296 'allow_same_participant_emails',
297 ts('Allow multiple registrations from the same email address?'),
298 NULL,
299 array('onclick' => "return showRuleFields( " . json_encode($ruleFields) . " );")
300 );
301 $this->assign('ruleFields', json_encode($ruleFields));
302
303 $participantStatuses = CRM_Event_PseudoConstant::participantStatus();
304 if (in_array('Awaiting approval', $participantStatuses) and in_array('Pending from approval', $participantStatuses) and in_array('Rejected', $participantStatuses)) {
305 $this->addElement('checkbox',
306 'requires_approval',
307 ts('Require participant approval?'),
308 NULL,
309 array('onclick' => "return showHideByValue('requires_approval', '', 'id-approval-text', 'table-row', 'radio', false);")
310 );
311 $this->add('textarea', 'approval_req_text', ts('Approval message'), $attributes['approval_req_text']);
312 }
313
314 $this->add('text', 'expiration_time', ts('Pending participant expiration (hours)'));
315 $this->addRule('expiration_time', ts('Please enter the number of hours (as an integer).'), 'integer');
316
317 self::buildRegistrationBlock($this);
318 self::buildConfirmationBlock($this);
319 self::buildMailBlock($this);
320 self::buildThankYouBlock($this);
321
322 parent::buildQuickForm();
323 }
324
325 /**
326 * Function to build Registration Block
327 *
328 * @param int $pageId
329 * @static
330 */
331 function buildRegistrationBlock(&$form) {
332 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
333 $form->addWysiwyg('intro_text', ts('Introductory Text'), $attributes['intro_text']);
334 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
335 // explicit height and width.
336 $form->addWysiwyg('footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
337
338 $types = array_merge(array('Contact', 'Individual', 'Participant'),
339 CRM_Contact_BAO_ContactType::subTypes('Individual')
340 );
341
342 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
343
344 $mainProfiles = array(
345 '' => ts('- select -')) + $profiles;
346 $addtProfiles = array(
347 '' => ts('- same as for main contact -')) + $profiles;
348
349 $form->add('select', 'custom_pre_id', ts('Include Profile') . '<br />' . ts('(top of page)'), $mainProfiles);
350 $form->add('select', 'custom_post_id', ts('Include Profile') . '<br />' . ts('(bottom of page)'), $mainProfiles);
351
352 $form->add('select', 'additional_custom_pre_id', ts('Profile for Additional Participants') . '<br />' . ts('(top of page)'), $addtProfiles);
353 $form->add('select', 'additional_custom_post_id', ts('Profile for Additional Participants') . '<br />' . ts('(bottom of page)'), $addtProfiles);
354 }
355
356 function buildMultipleProfileBottom(&$form, $count, $prefix = '', $name = 'Include Profile') {
357 $types = array_merge(array('Contact', 'Individual', 'Participant'),
358 CRM_Contact_BAO_ContactType::subTypes('Individual')
359 );
360
361 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
362
363 if ($prefix == 'additional_') {
364 $mainProfiles = array(
365 '' => ts('- same as for main contact -')) + $profiles;
366 }
367 else {
368 $mainProfiles = array(
369 '' => ts('- select -')) + $profiles;
370 }
371
372 $element = $prefix . "custom_post_id_multiple[$count]";
373 $form->add('select', $element, $name . '<br />' . ts('(bottom of page)'), $mainProfiles);
374 }
375
376 /**
377 * Function to build Confirmation Block
378 *
379 * @param int $pageId
380 * @static
381 */
382 function buildConfirmationBlock(&$form) {
383 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
384 $form->add('text', 'confirm_title', ts('Title'), $attributes['confirm_title']);
385 $form->addWysiwyg('confirm_text', ts('Introductory Text'), $attributes['confirm_text']);
386 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
387 // explicit height and width.
388 $form->addWysiwyg('confirm_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
389 }
390
391 /**
392 * Function to build Email Block
393 *
394 * @param int $pageId
395 * @static
396 */
397 function buildMailBlock(&$form) {
398 $form->registerRule('emailList', 'callback', 'emailList', 'CRM_Utils_Rule');
399 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
400 $form->addYesNo('is_email_confirm', ts('Send Confirmation Email?'), NULL, NULL, array('onclick' => "return showHideByValue('is_email_confirm','','confirmEmail','block','radio',false);"));
401 $form->add('textarea', 'confirm_email_text', ts('Text'), $attributes['confirm_email_text']);
402 $form->add('text', 'cc_confirm', ts('CC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'cc_confirm'));
403 $form->addRule('cc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
404 $form->add('text', 'bcc_confirm', ts('BCC Confirmation To'), CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event', 'bcc_confirm'));
405 $form->addRule('bcc_confirm', ts('Please enter a valid list of comma delimited email addresses'), 'emailList');
406 $form->add('text', 'confirm_from_name', ts('Confirm From Name'));
407 $form->add('text', 'confirm_from_email', ts('Confirm From Email'));
408 $form->addRule('confirm_from_email', ts('Email is not valid.'), 'email');
409 }
410
411 function buildThankYouBlock(&$form) {
412 $attributes = CRM_Core_DAO::getAttribute('CRM_Event_DAO_Event');
413 $form->add('text', 'thankyou_title', ts('Title'), $attributes['thankyou_title']);
414 $form->addWysiwyg('thankyou_text', ts('Introductory Text'), $attributes['thankyou_text']);
415 // FIXME: This hack forces height of editor to 175px. Need to modify QF classes for editors to allow passing
416 // explicit height and width.
417 $form->addWysiwyg('thankyou_footer_text', ts('Footer Text'), array('rows' => 2, 'cols' => 40));
418 }
419
420 /**
421 * Add local and global form rules
422 *
423 * @access protected
424 *
425 * @return void
426 */
427 function addRules() {
428 if ($this->_addProfileBottom || $this->_addProfileBottomAdd) {
429 return;
430 }
431 $this->addFormRule(array('CRM_Event_Form_ManageEvent_Registration', 'formRule'), $this);
432 }
433
434 /**
435 * global validation rules for the form
436 *
437 * @param array $fields posted values of the form
438 *
439 * @return array list of errors to be posted back to the form
440 * @static
441 * @access public
442 */
443 static function formRule($values, $files, $form) {
444 if (CRM_Utils_Array::value('is_online_registration', $values)) {
445
446 if (!$values['confirm_title']) {
447 $errorMsg['confirm_title'] = ts('Please enter a Title for the registration Confirmation Page');
448 }
449 if (!$values['thankyou_title']) {
450 $errorMsg['thankyou_title'] = ts('Please enter a Title for the registration Thank-you Page');
451 }
452 if ($values['is_email_confirm']) {
453 if (!$values['confirm_from_name']) {
454 $errorMsg['confirm_from_name'] = ts('Please enter Confirmation Email FROM Name.');
455 }
456
457 if (!$values['confirm_from_email']) {
458 $errorMsg['confirm_from_email'] = ts('Please enter Confirmation Email FROM Email Address.');
459 }
460 }
461
462 if ($values['registration_start_date'] && $values['registration_end_date']) {
463 $start = CRM_Utils_Date::processDate($values['registration_start_date']);
464 $end = CRM_Utils_Date::processDate($values['registration_end_date']);
465 if ($end < $start) {
466 $errorMsg['registration_end_date'] = ts('Registration end date should be after Registration start date');
467 }
468 }
469
470 //check that the selected profiles have either firstname+lastname or email required
471 $profileIds = array(
472 CRM_Utils_Array::value('custom_pre_id', $values),
473 CRM_Utils_Array::value('custom_post_id', $values),
474 );
475 $additionalProfileIds = array(
476 CRM_Utils_Array::value('additional_custom_pre_id', $values),
477 CRM_Utils_Array::value('additional_custom_post_id', $values),
478 );
479 //additional profile fields default to main if not set
480 if (!is_numeric($additionalProfileIds[0])) {
481 $additionalProfileIds[0] = $profileIds[0];
482 }
483 if (!is_numeric($additionalProfileIds[1])) {
484 $additionalProfileIds[1] = $profileIds[1];
485 }
486 //add multiple profiles if set
487 self::addMultipleProfiles($profileIds, $values, 'custom_post_id_multiple');
488 self::addMultipleProfiles($additionalProfileIds, $values, 'additional_custom_post_id_multiple');
489 $isProfileComplete = self::isProfileComplete($profileIds);
490 $isAdditionalProfileComplete = self::isProfileComplete($additionalProfileIds);
491 //Check main profiles have an email address available if 'send confirmation email' is selected
492 if ($values['is_email_confirm']) {
493 $emailFields = self::getEmailFields($profileIds);
494 if (!count($emailFields)) {
495 $errorMsg['is_email_confirm'] = ts("Please add a profile with an email address if 'Send Confirmation Email?' is selected");
496 }
497 }
498 $additionalCustomPreId = $additionalCustomPostId = NULL;
499 $isPreError = $isPostError = TRUE;
500 if (CRM_Utils_Array::value('allow_same_participant_emails', $values) &&
501 CRM_Utils_Array::value('is_multiple_registrations', $values)
502 ) {
503 $types = array_merge(array('Individual'), CRM_Contact_BAO_ContactType::subTypes('Individual'));
504 $profiles = CRM_Core_BAO_UFGroup::getProfiles($types);
505
506 //check for additional custom pre profile
507 $additionalCustomPreId = CRM_Utils_Array::value('additional_custom_pre_id', $values);
508 if (!empty($additionalCustomPreId)) {
509 if (!($additionalCustomPreId == 'none')) {
510 $customPreId = $additionalCustomPreId;
511 }
512 else {
513 $isPreError = FALSE;
514 }
515 }
516 else {
517 $customPreId = CRM_Utils_Array::value('custom_pre_id', $values) ? $values['custom_pre_id'] : NULL;
518 }
519 //check whether the additional custom pre profile is of type 'Individual' and its subtypes
520 if (!empty($customPreId)) {
521 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPreId);
522 foreach ($types as $individualTypes) {
523 if (in_array($individualTypes, $profileTypes)) {
524 $isPreError = FALSE;
525 break;
526 }
527 }
528 }
529 else {
530 $isPreError = FALSE;
531 }
532
533 // We don't have required Individual fields in the pre-custom profile, so now check the post-custom profile
534 if ($isPreError) {
535 $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values);
536 if (!empty($additionalCustomPostId)) {
537 if (!($additionalCustomPostId == 'none')) {
538 $customPostId = $additionalCustomPostId;
539 }
540 else {
541 $isPostError = FALSE;
542 }
543 }
544 else {
545 $customPostId = CRM_Utils_Array::value('custom_post_id', $values) ? $values['custom_post_id'] : NULL;
546 }
547 //check whether the additional custom post profile is of type 'Individual' and its subtypes
548 if (!empty($customPostId)) {
549 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
550 foreach ($types as $individualTypes) {
551 if (in_array($individualTypes, $profileTypes)) {
552 $isPostError = FALSE;
553 break;
554 }
555 }
556 }
557 else {
558 $isPostError = FALSE;
559 }
560
561 if (empty($customPreId) && empty($customPostId)) {
562 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
563 }
564 if ($isPostError) {
565 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
566 }
567 }
568 }
569 if (!$isProfileComplete) {
570 $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.");
571 }
572 if (!$isAdditionalProfileComplete) {
573 $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.");
574 }
575
576 // // CRM-8485
577 // $config = CRM_Core_Config::singleton();
578 // if ( $config->doNotAttachPDFReceipt ) {
579 // if ( CRM_Utils_Array::value('custom_post_id_multiple', $values) ) {
580 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
581 // if ( $customPostMultiple ) {
582 // $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' ) ) );
583 // break;
584 // }
585 // }
586 // }
587 //
588 // if ( CRM_Utils_Array::value('is_multiple_registrations', $values) &&
589 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
590 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
591 // if ( $customPostMultiple ) {
592 // $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' ) ) );
593 // break;
594 // }
595 // }
596 // }
597 // }
598
599 if (!empty($errorMsg)) {
600 if (CRM_Utils_Array::value('custom_post_id_multiple', $values)) {
601 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
602 self::buildMultipleProfileBottom($form, $count);
603 }
604 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
605 }
606 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $values)) {
607 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
608 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
609 }
610 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
611 }
612 }
613 }
614
615 if (!empty($errorMsg)) {
616 return $errorMsg;
617 }
618
619 return TRUE;
620 }
621
622 /**
623 * Collect all email fields for an array of profile ids
624 *
625 * @return boolean
626 */
627 static function getEmailFields($profileIds) {
628 $emailFields = array();
629 foreach ($profileIds as $profileId) {
630 if ($profileId && is_numeric($profileId)) {
631 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
632 foreach ($fields as $field) {
633 if (substr_count($field['name'], 'email')) {
634 $emailFields[] = $field;
635 }
636 }
637 }
638 }
639 return $emailFields;
640 }
641
642 /**
643 * Check if a profile contains required fields
644 *
645 * @return boolean
646 */
647 static function isProfileComplete($profileIds) {
648 $profileReqFields = array();
649 foreach ($profileIds as $profileId) {
650 if ($profileId && is_numeric($profileId)) {
651 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
652 foreach ($fields as $field) {
653 switch (TRUE) {
654 case substr_count($field['name'], 'email'):
655 $profileReqFields[] = 'email';
656 break;
657
658 case substr_count($field['name'], 'first_name'):
659 $profileReqFields[] = 'first_name';
660 break;
661
662 case substr_count($field['name'], 'last_name'):
663 $profileReqFields[] = 'last_name';
664 break;
665 }
666 }
667 }
668 }
669 $profileComplete = (in_array('email', $profileReqFields)
670 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
671 );
672 return $profileComplete;
673 }
674
675 /**
676 * Add additional profiles from the form to an array of profile ids.
677 *
678 */
679 static function addMultipleProfiles(&$profileIds, $values, $field) {
680 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
681 foreach ($multipleProfiles as $profileId) {
682 $profileIds[] = $profileId;
683 }
684 }
685 }
686
687 /**
688 * Function to process the form
689 *
690 * @access public
691 *
692 * @return None
693 */
694 public function postProcess() {
695 $params = array();
696 $params = $this->exportValues();
697
698 $params['id'] = $this->_id;
699
700 //format params
701 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
702 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
703 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
704 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
705
706 // reset is_email confirm if not online reg
707 if (!$params['is_online_registration']) {
708 $params['is_email_confirm'] = FALSE;
709 }
710
711 if (!$this->_isTemplate) {
712 $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'],
713 $params['registration_start_date_time'],
714 TRUE
715 );
716 $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'],
717 $params['registration_end_date_time'],
718 TRUE
719 );
720 }
721
722 CRM_Event_BAO_Event::add($params);
723
724 // also update the ProfileModule tables
725 $ufJoinParams = array(
726 'is_active' => 1,
727 'module' => 'CiviEvent',
728 'entity_table' => 'civicrm_event',
729 'entity_id' => $this->_id,
730 );
731
732
733 // first delete all past entries
734 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
735
736 $uf = array();
737 $wt = 2;
738 if (!empty($params['custom_pre_id'])) {
739 $uf[1] = $params['custom_pre_id'];
740 $wt = 1;
741 }
742
743 if (!empty($params['custom_post_id'])) {
744 $uf[2] = $params['custom_post_id'];
745 }
746
747 if (CRM_Utils_Array::value('custom_post_id_multiple', $params)) {
748 $uf = array_merge($uf, $params['custom_post_id_multiple']);
749 }
750 $uf = array_values($uf);
751 if (!empty($uf)) {
752 foreach ($uf as $weight => $ufGroupId) {
753 $ufJoinParams['weight'] = $weight + $wt;
754 $ufJoinParams['uf_group_id'] = $ufGroupId;
755 CRM_Core_BAO_UFJoin::create($ufJoinParams);
756 unset($ufJoinParams['id']);
757 }
758 }
759 // also update the ProfileModule tables
760 $ufJoinParamsAdd = array(
761 'is_active' => 1,
762 'module' => 'CiviEvent_Additional',
763 'entity_table' => 'civicrm_event',
764 'entity_id' => $this->_id,
765 );
766
767 // first delete all past entries
768 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
769 if (CRM_Utils_Array::value('is_multiple_registrations', $params)) {
770 $ufAdd = array();
771 $wtAdd = 2;
772
773 if (array_key_exists('additional_custom_pre_id', $params)) {
774 if (!CRM_Utils_Array::value('additional_custom_pre_id', $params)) {
775 $ufAdd[1] = $params['custom_pre_id'];
776 $wtAdd = 1;
777 }
778 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {}
779 else {
780 $ufAdd[1] = $params['additional_custom_pre_id'];
781 $wtAdd = 1;
782 }
783 }
784
785 if (array_key_exists('additional_custom_post_id', $params)) {
786 if (!CRM_Utils_Array::value('additional_custom_post_id', $params)) {
787 $ufAdd[2] = $params['custom_post_id'];
788 }
789 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {}
790 else {
791 $ufAdd[2] = $params['additional_custom_post_id'];
792 }
793 }
794
795 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $params)) {
796 $additionalPostMultiple = array();
797 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
798 if (!$value && CRM_Utils_Array::value('custom_post_id', $params)) {
799 $additionalPostMultiple[$key] = $params['custom_post_id'];
800 }
801 elseif ($value == 'none') {
802 continue;
803 }
804 elseif ($value) {
805 $additionalPostMultiple[$key] = $value;
806 }
807 }
808 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
809 }
810
811 $ufAdd = array_values($ufAdd);
812 if (!empty($ufAdd)) {
813 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
814
815 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
816 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
817
818 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
819 unset($ufJoinParamsAdd['id']);
820 }
821 }
822 }
823
824 parent::endPostProcess();
825 }
826 //end of function
827
828 /**
829 * Return a descriptive name for the page, used in wizard header
830 *
831 * @return string
832 * @access public
833 */
834 public function getTitle() {
835 return ts('Online Registration');
836 }
837 }
838