Exemplo n.º 1
0
 /**
  * Show the contact page
  *
  * Parse a contact form submit request and show the contact page
  * @see _getContactFormData(), _checkValues(), _insertIntoDatabase(), sendMail(), _showError(), _showFeedback(), \Cx\Core\Html\Sigma::get(), \Cx\Core\Html\Sigma::blockExists(), \Cx\Core\Html\Sigma::hideBlock(), \Cx\Core\Html\Sigma::touchBlock()
  * @return string Parse contact form page
  */
 function getContactPage()
 {
     global $_ARRAYLANG, $_LANGID, $objDatabase;
     \JS::activate('cx');
     $formId = isset($_GET['cmd']) ? intval($_GET['cmd']) : 0;
     $arrFields = $this->getFormFields($formId);
     $isLoggedin = $this->setProfileData();
     $useCaptcha = !$isLoggedin && $this->getContactFormCaptchaStatus($formId);
     $this->handleUniqueId();
     $uploaderCode = '';
     // load requested form's source code if required
     if ($this->objTemplate->placeholderExists('APPLICATION_DATA')) {
         // load form's source code
         $applicationTemplate = $this->getSourceCode($formId, $_LANGID);
         \LinkGenerator::parseTemplate($applicationTemplate);
         $this->objTemplate->addBlock('APPLICATION_DATA', 'application_data', $applicationTemplate);
     }
     $this->objTemplate->setVariable(array('TXT_NEW_ENTRY_ERORR' => $_ARRAYLANG['TXT_NEW_ENTRY_ERORR'], 'TXT_CONTACT_SUBMIT' => $_ARRAYLANG['TXT_CONTACT_SUBMIT'], 'TXT_CONTACT_RESET' => $_ARRAYLANG['TXT_CONTACT_RESET']));
     if ($this->objTemplate->blockExists('contact_form')) {
         $recipients = $this->getRecipients($formId);
         foreach ($arrFields as $fieldId => $arrField) {
             /*
              * Set values for special field types if the user is authenticated
              */
             if ($isLoggedin && empty($_GET[$fieldId]) && empty($_POST['contactFormField_' . $fieldId])) {
                 switch ($arrField['special_type']) {
                     case 'access_email':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_USER_EMAIL]]';
                         break;
                     case 'access_gender':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_GENDER]]';
                         break;
                     case 'access_title':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_TITLE]]';
                         break;
                     case 'access_firstname':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_FIRSTNAME]]';
                         break;
                     case 'access_lastname':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_LASTNAME]]';
                         break;
                     case 'access_company':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_COMPANY]]';
                         break;
                     case 'access_address':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_ADDRESS]]';
                         break;
                     case 'access_city':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_CITY]]';
                         break;
                     case 'access_zip':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_ZIP]]';
                         break;
                     case 'access_country':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_COUNTRY]]';
                         break;
                     case 'access_phone_office':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_PHONE_OFFICE]]';
                         break;
                     case 'access_phone_private':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_PHONE_PRIVATE]]';
                         break;
                     case 'access_phone_mobile':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_PHONE_MOBILE]]';
                         break;
                     case 'access_phone_fax':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_PHONE_FAX]]';
                         break;
                     case 'access_birthday':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_BIRTHDAY]]';
                         break;
                     case 'access_website':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_WEBSITE]]';
                         break;
                     case 'access_profession':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_PROFESSION]]';
                         break;
                     case 'access_interests':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_INTERESTS]]';
                         break;
                     case 'access_signature':
                         $arrField['lang'][$_LANGID]['value'] = '[[ACCESS_PROFILE_ATTRIBUTE_SIGNATURE]]';
                         break;
                     default:
                         break;
                 }
             }
             $arrField['lang'][$_LANGID]['value'] = preg_replace('/\\[\\[([A-Z0-9_]+)\\]\\]/', '{$1}', $arrField['lang'][$_LANGID]['value']);
             $this->objTemplate->setVariable(array($formId . '_FORM_NAME' => wordwrap($this->arrForms[$formId]['lang'][$_LANGID]['name'], 90, "<br/>\n", true), $formId . '_FORM_TEXT' => $this->arrForms[$formId]['lang'][$_LANGID]['text'], $fieldId . '_LABEL' => $arrField['lang'][$_LANGID]['name'] != "" ? wordwrap($arrField['lang'][$_LANGID]['name'], 90, "<br/>\n", true) : "&nbsp;"));
             /*
              * Generate values for dropdown checkbox and radio fields
              */
             $userProfileRegExp = '/\\{([A-Z_]+)\\}/';
             $accessAttributeId = null;
             $fieldType = $arrField['type'] != 'special' ? $arrField['type'] : $arrField['special_type'];
             switch ($fieldType) {
                 case 'checkbox':
                     if ($arrField['lang'][$_LANGID]['value'] == 1 || !empty($_POST['contactFormField_' . $fieldId])) {
                         $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'checked="checked"');
                     }
                     break;
                 case 'checkboxGroup':
                 case 'radio':
                     $options = explode(',', $arrField['lang'][$_LANGID]['value']);
                     foreach ($options as $index => $option) {
                         if (preg_match($userProfileRegExp, $option)) {
                             $valuePlaceholderBlock = 'contact_value_placeholder_block_' . $fieldId . '_' . $index;
                             $this->objTemplate->addBlock($fieldId . '_' . $index . '_VALUE', $valuePlaceholderBlock, contrexx_raw2xhtml($option));
                         } else {
                             $this->objTemplate->setVariable($fieldId . '_' . $index . '_VALUE', contrexx_raw2xhtml($option));
                         }
                         if (!empty($_POST['contactFormField_' . $fieldId])) {
                             if (in_array($option, $_POST['contactFormField_' . $fieldId]) || $option == $_POST['contactFormField_' . $fieldId]) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId . '_' . $index, 'checked="checked"');
                             }
                         } elseif (!empty($_GET[$fieldId])) {
                             if ($option == $_GET[$fieldId]) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId . '_' . $index, 'checked="checked"');
                             }
                         }
                     }
                     break;
                 case 'access_title':
                 case 'access_gender':
                     // collect user attribute options
                     $arrOptions = array();
                     $accessAttributeId = str_replace('access_', '', $fieldType);
                     $objAttribute = \FWUser::getFWUserObject()->objUser->objAttribute->getById($accessAttributeId);
                     // get options
                     $arrAttribute = $objAttribute->getChildren();
                     foreach ($arrAttribute as $attributeId) {
                         // in case the selection of the field is mandatory, we shall skip the unknown option of the user profile attribute
                         if ($arrField['is_required'] && strpos($attributeId, '_undefined')) {
                             continue;
                         }
                         $objAttribute = \FWUser::getFWUserObject()->objUser->objAttribute->getById($attributeId);
                         $arrOptions[] = $objAttribute->getName(FRONTEND_LANG_ID);
                     }
                     // options will be used for select input generation
                     $arrField['lang'][FRONTEND_LANG_ID]['value'] = implode(',', $arrOptions);
                     // intentionally no break here!!
                 // intentionally no break here!!
                 case 'select':
                     $options = explode(',', $arrField['lang'][$_LANGID]['value']);
                     $inexOffset = 0;
                     if ($arrField['is_required']) {
                         $options = array_merge(array($_ARRAYLANG['TXT_CONTACT_PLEASE_SELECT']), $options);
                         $inexOffset = 1;
                     }
                     foreach ($options as $index => $option) {
                         if (preg_match($userProfileRegExp, $option)) {
                             $valuePlaceholderBlock = 'contact_value_placeholder_block_' . $fieldId . '_' . $index;
                             $this->objTemplate->addBlock($fieldId . '_VALUE', $valuePlaceholderBlock, contrexx_raw2xhtml($option));
                         } else {
                             $this->objTemplate->setVariable($fieldId . '_VALUE', contrexx_raw2xhtml($option));
                         }
                         // pre-selection, based on $_POST value
                         if (!empty($_POST['contactFormField_' . $fieldId])) {
                             if ($index == array_search($_POST['contactFormField_' . $fieldId], explode(',', $arrField['lang'][$_LANGID]['value'])) + $inexOffset) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                             }
                             // pre-selection, based on $_GET value
                         } elseif (!empty($_GET[$fieldId])) {
                             if ($index == array_search(contrexx_input2raw($_GET[$fieldId]), explode(',', $arrField['lang'][$_LANGID]['value']))) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                             }
                             // pre-selection, based on profile data of currently signed in user
                         } elseif (isset($this->objTemplate->_globalVariables['ACCESS_PROFILE_ATTRIBUTE_' . strtoupper($accessAttributeId)]) && $option == $this->objTemplate->_globalVariables['ACCESS_PROFILE_ATTRIBUTE_' . strtoupper($accessAttributeId)]) {
                             $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                         }
                         $this->objTemplate->parse('field_' . $fieldId);
                     }
                     break;
                 case 'recipient':
                     foreach ($recipients as $index => $recipient) {
                         $recipient['lang'][$_LANGID] = preg_replace('/\\[\\[([A-Z0-9_]+)\\]\\]/', '{$1}', $recipient['lang'][$_LANGID]);
                         if (preg_match($userProfileRegExp, $recipient['lang'][$_LANGID])) {
                             $valuePlaceholderBlock = 'contact_value_placeholder_block_' . $fieldId . '_' . $index;
                             $this->objTemplate->addBlock($fieldId . '_VALUE', $valuePlaceholderBlock, $recipient['lang'][$_LANGID]);
                         } else {
                             $this->objTemplate->setVariable(array($fieldId . '_VALUE' => $recipient['lang'][$_LANGID]));
                         }
                         $this->objTemplate->setVariable(array($fieldId . '_VALUE_ID' => $index));
                         if (!empty($_POST['contactFormField_' . $fieldId]) && $recipient['lang'][$_LANGID] == $_POST['contactFormField_' . $fieldId]) {
                             $this->objTemplate->setVariable(array('SELECTED_' . $fieldId => 'selected = "selected"'));
                         } elseif (!empty($_GET[$fieldId]) && $recipient['lang'][$_LANGID] == $_GET[$fieldId]) {
                             $this->objTemplate->setVariable(array('SELECTED_' . $fieldId => 'selected = "selected"'));
                         }
                         $this->objTemplate->parse('field_' . $fieldId);
                     }
                     break;
                 case 'access_country':
                 case 'country':
                     if (preg_match($userProfileRegExp, $arrField['lang'][$_LANGID]['value'])) {
                         $arrField['lang'][$_LANGID]['value'] = $this->objTemplate->_globalVariables[trim($arrField['lang'][$_LANGID]['value'], '{}')];
                     }
                     $lang = $arrField['lang'][$_LANGID]['value'];
                     $country = \Cx\Core\Country\Controller\Country::getNameArray(true, $lang);
                     foreach ($country as $id => $name) {
                         $this->objTemplate->setVariable($fieldId . '_VALUE', $name);
                         if (!empty($_POST['contactFormField_' . $fieldId])) {
                             if (strcasecmp($name, $_POST['contactFormField_' . $fieldId]) == 0) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                             }
                         } elseif (!empty($_GET[$fieldId])) {
                             if (strcasecmp($name, $_GET[$fieldId]) == 0) {
                                 $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                             }
                         } elseif ($name == $arrField['lang'][$_LANGID]['value']) {
                             $this->objTemplate->setVariable('SELECTED_' . $fieldId, 'selected = "selected"');
                         }
                         $this->objTemplate->parse('field_' . $fieldId);
                     }
                     $this->objTemplate->setVariable(array('TXT_CONTACT_PLEASE_SELECT' => $_ARRAYLANG['TXT_CONTACT_PLEASE_SELECT'], 'TXT_CONTACT_NOT_SPECIFIED' => $_ARRAYLANG['TXT_CONTACT_NOT_SPECIFIED']));
                     break;
                 case 'file':
                     $this->hasFileField = true;
                     $uploaderCode .= $this->initUploader($fieldId, true);
                     break;
                 case 'multi_file':
                     $this->hasFileField = true;
                     $uploaderCode .= $this->initUploader($fieldId, false);
                     break;
                 default:
                     /*
                      * Set default field value through User profile attribute
                      */
                     $arrField['lang'][$_LANGID]['value'] = preg_replace('/\\[\\[([A-Z0-9_]+)\\]\\]/', '{$1}', $arrField['lang'][$_LANGID]['value']);
                     if (preg_match($userProfileRegExp, $arrField['lang'][$_LANGID]['value'])) {
                         $valuePlaceholderBlock = 'contact_value_placeholder_block_' . $fieldId;
                         $this->objTemplate->addBlock($fieldId . '_VALUE', $valuePlaceholderBlock, contrexx_raw2xhtml($arrField['lang'][$_LANGID]['value']));
                     } elseif (!empty($_POST['contactFormField_' . $fieldId])) {
                         $this->objTemplate->setVariable($fieldId . '_VALUE', contrexx_raw2xhtml($_POST['contactFormField_' . $fieldId]));
                     } elseif (!empty($_GET[$fieldId])) {
                         $this->objTemplate->setVariable($fieldId . '_VALUE', contrexx_raw2xhtml($_GET[$fieldId]));
                     } else {
                         $this->objTemplate->setVariable($fieldId . '_VALUE', contrexx_raw2xhtml($arrField['lang'][$_LANGID]['value']));
                     }
                     break;
             }
             /*
              * Parse the blocks created for parsing user profile data using addBlock()
              */
             if (!empty($valuePlaceholderBlock) && $this->objTemplate->blockExists($valuePlaceholderBlock)) {
                 $this->objTemplate->touchBlock($valuePlaceholderBlock);
             }
         }
     }
     if (isset($_POST['submitContactForm']) || isset($_POST['Submit'])) {
         //form submitted
         $this->checkLegacyMode();
         $showThanks = isset($_GET['cmd']) && $_GET['cmd'] == 'thanks' ? true : false;
         $arrFormData = $this->_getContactFormData();
         if ($arrFormData) {
             if ($this->_checkValues($arrFormData, $useCaptcha) && $this->_insertIntoDatabase($arrFormData)) {
                 //validation ok
                 if (!empty($arrFormData['saveDataInCRM'])) {
                     $objCrmLibrary = new \Cx\Modules\Crm\Controller\CrmLibrary('Crm');
                     $objCrmLibrary->addCrmContact($arrFormData);
                 }
                 $this->sendMail($arrFormData);
                 if (isset($arrFormData['showForm']) && !$arrFormData['showForm']) {
                     $this->objTemplate->hideBlock("formText");
                     $this->objTemplate->hideBlock('contact_form');
                 }
             } else {
                 //found errors while validating
                 $this->setCaptcha($useCaptcha);
                 return $this->_showError();
             }
             if (!$showThanks) {
                 $this->_showFeedback($arrFormData);
             } else {
                 if ($this->objTemplate->blockExists("formText")) {
                     $this->objTemplate->hideBlock("formText");
                 }
             }
         }
     } else {
         //fresh display
         if ($this->objTemplate->blockExists('formText')) {
             $this->objTemplate->touchBlock('formText');
         }
         $this->setCaptcha($useCaptcha);
     }
     $this->objTemplate->setVariable('CONTACT_JAVASCRIPT', $this->_getJsSourceCode($formId, $arrFields) . $uploaderCode);
     return $this->objTemplate->get();
 }