Import from SVN (r45945, r596)
[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 //check for additional custom post profile
533 $additionalCustomPostId = CRM_Utils_Array::value('additional_custom_post_id', $values);
534 if (!empty($additionalCustomPostId)) {
535 if (!($additionalCustomPostId == 'none')) {
536 $customPostId = $additionalCustomPostId;
537 }
538 else {
539 $isPostError = FALSE;
540 }
541 }
542 else {
543 $customPostId = CRM_Utils_Array::value('custom_post_id', $values) ? $values['custom_post_id'] : NULL;
544 }
545 //check whether the additional custom post profile is of type 'Individual' and its subtypes
546 if (!empty($customPostId)) {
547 $profileTypes = CRM_Core_BAO_UFGroup::profileGroups($customPostId);
548 foreach ($types as $individualTypes) {
549 if (in_array($individualTypes, $profileTypes)) {
550 $isPostError = FALSE;
551 break;
552 }
553 }
554 }
555 else {
556 $isPostError = FALSE;
557 }
558 if ($isPreError || (empty($customPreId) && empty($customPostId))) {
559 $errorMsg['additional_custom_pre_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
560 }
561 if ($isPostError) {
562 $errorMsg['additional_custom_post_id'] = ts("Allow multiple registrations from the same email address requires a profile of type 'Individual'");
563 }
564 }
565 if (!$isProfileComplete) {
566 $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.");
567 }
568 if (!$isAdditionalProfileComplete) {
569 $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.");
570 }
571
572 // // CRM-8485
573 // $config = CRM_Core_Config::singleton();
574 // if ( $config->doNotAttachPDFReceipt ) {
575 // if ( CRM_Utils_Array::value('custom_post_id_multiple', $values) ) {
576 // foreach( $values['custom_post_id_multiple'] as $count => $customPostMultiple ) {
577 // if ( $customPostMultiple ) {
578 // $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' ) ) );
579 // break;
580 // }
581 // }
582 // }
583 //
584 // if ( CRM_Utils_Array::value('is_multiple_registrations', $values) &&
585 // CRM_Utils_Array::value('additional_custom_post_id_multiple', $values) ) {
586 // foreach( $values['additional_custom_post_id_multiple'] as $count => $customPostMultiple ) {
587 // if ( $customPostMultiple ) {
588 // $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' ) ) );
589 // break;
590 // }
591 // }
592 // }
593 // }
594
595 if (!empty($errorMsg)) {
596 if (CRM_Utils_Array::value('custom_post_id_multiple', $values)) {
597 foreach ($values['custom_post_id_multiple'] as $count => $customPostMultiple) {
598 self::buildMultipleProfileBottom($form, $count);
599 }
600 $form->assign('profilePostMultiple', $values['custom_post_id_multiple']);
601 }
602 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $values)) {
603 foreach ($values['additional_custom_post_id_multiple'] as $count => $customPostMultiple) {
604 self::buildMultipleProfileBottom($form, $count, 'additional_', ts('Profile for Additional Participants'));
605 }
606 $form->assign('profilePostMultipleAdd', $values['additional_custom_post_id_multiple']);
607 }
608 }
609 }
610
611 if (!empty($errorMsg)) {
612 return $errorMsg;
613 }
614
615 return TRUE;
616 }
617
618 /**
619 * Collect all email fields for an array of profile ids
620 *
621 * @return boolean
622 */
623 static function getEmailFields($profileIds) {
624 $emailFields = array();
625 foreach ($profileIds as $profileId) {
626 if ($profileId && is_numeric($profileId)) {
627 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
628 foreach ($fields as $field) {
629 if (substr_count($field['name'], 'email')) {
630 $emailFields[] = $field;
631 }
632 }
633 }
634 }
635 return $emailFields;
636 }
637
638 /**
639 * Check if a profile contains required fields
640 *
641 * @return boolean
642 */
643 static function isProfileComplete($profileIds) {
644 $profileReqFields = array();
645 foreach ($profileIds as $profileId) {
646 if ($profileId && is_numeric($profileId)) {
647 $fields = CRM_Core_BAO_UFGroup::getFields($profileId);
648 foreach ($fields as $field) {
649 switch (TRUE) {
650 case substr_count($field['name'], 'email'):
651 $profileReqFields[] = 'email';
652 break;
653
654 case substr_count($field['name'], 'first_name'):
655 $profileReqFields[] = 'first_name';
656 break;
657
658 case substr_count($field['name'], 'last_name'):
659 $profileReqFields[] = 'last_name';
660 break;
661 }
662 }
663 }
664 }
665 $profileComplete = (in_array('email', $profileReqFields)
666 || (in_array('first_name', $profileReqFields) && in_array('last_name', $profileReqFields))
667 );
668 return $profileComplete;
669 }
670
671 /**
672 * Add additional profiles from the form to an array of profile ids.
673 *
674 */
675 static function addMultipleProfiles(&$profileIds, $values, $field) {
676 if ($multipleProfiles = CRM_Utils_Array::value($field, $values)) {
677 foreach ($multipleProfiles as $profileId) {
678 $profileIds[] = $profileId;
679 }
680 }
681 }
682
683 /**
684 * Function to process the form
685 *
686 * @access public
687 *
688 * @return None
689 */
690 public function postProcess() {
691 $params = array();
692 $params = $this->exportValues();
693
694 $params['id'] = $this->_id;
695
696 //format params
697 $params['is_online_registration'] = CRM_Utils_Array::value('is_online_registration', $params, FALSE);
698 $params['is_multiple_registrations'] = CRM_Utils_Array::value('is_multiple_registrations', $params, FALSE);
699 $params['allow_same_participant_emails'] = CRM_Utils_Array::value('allow_same_participant_emails', $params, FALSE);
700 $params['requires_approval'] = CRM_Utils_Array::value('requires_approval', $params, FALSE);
701
702 // reset is_email confirm if not online reg
703 if (!$params['is_online_registration']) {
704 $params['is_email_confirm'] = FALSE;
705 }
706
707 if (!$this->_isTemplate) {
708 $params['registration_start_date'] = CRM_Utils_Date::processDate($params['registration_start_date'],
709 $params['registration_start_date_time'],
710 TRUE
711 );
712 $params['registration_end_date'] = CRM_Utils_Date::processDate($params['registration_end_date'],
713 $params['registration_end_date_time'],
714 TRUE
715 );
716 }
717
718 CRM_Event_BAO_Event::add($params);
719
720 // also update the ProfileModule tables
721 $ufJoinParams = array(
722 'is_active' => 1,
723 'module' => 'CiviEvent',
724 'entity_table' => 'civicrm_event',
725 'entity_id' => $this->_id,
726 );
727
728
729 // first delete all past entries
730 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
731
732 $uf = array();
733 $wt = 2;
734 if (!empty($params['custom_pre_id'])) {
735 $uf[1] = $params['custom_pre_id'];
736 $wt = 1;
737 }
738
739 if (!empty($params['custom_post_id'])) {
740 $uf[2] = $params['custom_post_id'];
741 }
742
743 if (CRM_Utils_Array::value('custom_post_id_multiple', $params)) {
744 $uf = array_merge($uf, $params['custom_post_id_multiple']);
745 }
746 $uf = array_values($uf);
747 if (!empty($uf)) {
748 foreach ($uf as $weight => $ufGroupId) {
749 $ufJoinParams['weight'] = $weight + $wt;
750 $ufJoinParams['uf_group_id'] = $ufGroupId;
751 CRM_Core_BAO_UFJoin::create($ufJoinParams);
752 unset($ufJoinParams['id']);
753 }
754 }
755 // also update the ProfileModule tables
756 $ufJoinParamsAdd = array(
757 'is_active' => 1,
758 'module' => 'CiviEvent_Additional',
759 'entity_table' => 'civicrm_event',
760 'entity_id' => $this->_id,
761 );
762
763 // first delete all past entries
764 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParamsAdd);
765 if (CRM_Utils_Array::value('is_multiple_registrations', $params)) {
766 $ufAdd = array();
767 $wtAdd = 2;
768
769 if (array_key_exists('additional_custom_pre_id', $params)) {
770 if (!CRM_Utils_Array::value('additional_custom_pre_id', $params)) {
771 $ufAdd[1] = $params['custom_pre_id'];
772 $wtAdd = 1;
773 }
774 elseif (CRM_Utils_Array::value('additional_custom_pre_id', $params) == 'none') {}
775 else {
776 $ufAdd[1] = $params['additional_custom_pre_id'];
777 $wtAdd = 1;
778 }
779 }
780
781 if (array_key_exists('additional_custom_post_id', $params)) {
782 if (!CRM_Utils_Array::value('additional_custom_post_id', $params)) {
783 $ufAdd[2] = $params['custom_post_id'];
784 }
785 elseif (CRM_Utils_Array::value('additional_custom_post_id', $params) == 'none') {}
786 else {
787 $ufAdd[2] = $params['additional_custom_post_id'];
788 }
789 }
790
791 if (CRM_Utils_Array::value('additional_custom_post_id_multiple', $params)) {
792 $additionalPostMultiple = array();
793 foreach ($params['additional_custom_post_id_multiple'] as $key => $value) {
794 if (!$value && CRM_Utils_Array::value('custom_post_id', $params)) {
795 $additionalPostMultiple[$key] = $params['custom_post_id'];
796 }
797 elseif ($value == 'none') {
798 continue;
799 }
800 elseif ($value) {
801 $additionalPostMultiple[$key] = $value;
802 }
803 }
804 $ufAdd = array_merge($ufAdd, $additionalPostMultiple);
805 }
806
807 $ufAdd = array_values($ufAdd);
808 if (!empty($ufAdd)) {
809 foreach ($ufAdd as $weightAdd => $ufGroupIdAdd) {
810
811 $ufJoinParamsAdd['weight'] = $weightAdd + $wtAdd;
812 $ufJoinParamsAdd['uf_group_id'] = $ufGroupIdAdd;
813
814 CRM_Core_BAO_UFJoin::create($ufJoinParamsAdd);
815 unset($ufJoinParamsAdd['id']);
816 }
817 }
818 }
819
820 parent::endPostProcess();
821 }
822 //end of function
823
824 /**
825 * Return a descriptive name for the page, used in wizard header
826 *
827 * @return string
828 * @access public
829 */
830 public function getTitle() {
831 return ts('Online Registration');
832 }
833 }
834