示例#1
0
文件: forms.php 项目: hungnv0789/vhtm
	/**
	* GetHTML
	* Gets the html for the particular form type and form design that is loaded.
	* This will also load up custom fields, put them into the form, add format choice dropdown (if applicable) and finally return the form html for displaying or putting on a website.
	* If it's a modify details form, then there are placeholders put into the form so the calling object/method can pre-fill the form as necessary.
	*
	* @param Boolean $inside_sendstudio Pass in whether we are viewing the form from inside the application or not. This allows us to include/exclude information accordingly. This stops a problem where viewing a form will log you out of the admin control panel.
	*
	* @see GetFormDesign
	* @see FetchFile
	* @see formtype
	* @see chooseformat
	* @see changeformat
	* @see lists
	* @see customfields
	*
	* @return String Returns the form's html content.
	*/
	function GetHTML($inside_sendstudio=false)
	{
		/**
		* This file lets us get api's, load language files and parse templates.
		*/
		if (!class_exists('sendstudio_functions', false)) {
			require_once(SENDSTUDIO_FUNCTION_DIRECTORY . '/sendstudio_functions.php');
		}

		$sendstudio_functions = new Sendstudio_Functions();
		$sendstudio_functions->LoadLanguageFile('frontend');
		$sendstudio_functions->LoadLanguageFile('forms');

		$content = $this->GetFormDesign($this->design, $this->formtype, true);

		$displayoption = $this->FetchFile($this->design, $this->formtype, '_options');

		$requiredoption = $this->FetchFile($this->design, false, 'required');
		$notrequiredoption = $this->FetchFile($this->design, false, 'notrequired');

		$javascript = '
			function CheckMultiple' . $this->formid . '(frm, name) {
				for (var i=0; i < frm.length; i++)
				{
					fldObj = frm.elements[i];
					fldId = fldObj.id;
					if (fldId) {
						var fieldnamecheck=fldObj.id.indexOf(name);
						if (fieldnamecheck != -1) {
							if (fldObj.checked) {
								return true;
							}
						}
					}
				}
				return false;
			}
		';

		$javascript .= 'function CheckForm' . $this->formid . '(f) {';

		$email_placeholder = '';
		if ($this->formtype == 'm' || $this->formtype == 'f') {
			$email_placeholder = '%%Email%%';
		}

		$alert = GetLang('Form_Javascript_EnterEmailAddress');

		$javascript .= '
			var email_re = /[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
			if (!email_re.test(f.email.value)) {
				alert("' . $alert . '");
				f.email.focus();
				return false;
			}
		';

		$formatlist = '';
		if ($this->formtype != 'u') {
			if ($this->formtype == 'm' && $this->changeformat) {
				$optionname = $requiredoption . GetLang('Form_ChooseFormat') . ':';

				$option = '<select name="format">';
				$option .= '<option value="h"%%Format_html%%>' . GetLang('Format_HTML') . '</option>';
				$option .= '<option value="t"%%Format_text%%>' . GetLang('Format_Text') . '</option>';
				$option .= '</select>';

				$formatlist = str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);
			} elseif ($this->formtype != 'm') {
				if ($this->chooseformat == 'c') {
					$optionname = $requiredoption . GetLang('Form_ChooseFormat') . ':';

					$option = '<select name="format">';
					$option .= '<option value="h">' . GetLang('Format_HTML') . '</option>';
					$option .= '<option value="t">' . GetLang('Format_Text') . '</option>';
					$option .= '</select>';

					$alert = GetLang('Form_Javascript_ChooseFormat');
					$javascript .= '
						if (f.format.selectedIndex == -1) {
							alert("' . $alert . '");
							f.format.focus();
							return false;
						}
					';

					$formatlist = str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);
				} else {
					$formatlist = '<input type="hidden" name="format" value="' . str_replace('f', '', $this->chooseformat) . '" />';
				}
			}
		}

		if ($this->usecaptcha) {
			$alert = GetLang('Form_Javascript_EnterCaptchaAnswer');

			$javascript .= '
				if (f.captcha.value == "") {
					alert("' . $alert . '");
					f.captcha.focus();
					return false;
				}
			';
		}

		$placeholder_lists = '';

		$list_intro_shown = false;

		if (sizeof($this->lists) > 1) {
			if (!class_exists('Lists_API', false)) {
				require_once(dirname(__FILE__) . '/lists.php');
			}
			$lists_api = new Lists_API(0, false);
			$lists_api->Set('Db', $this->Db);
			$listlist = '';

			foreach ($this->lists as $p => $listid) {
				$lists_api->Load($listid);
				$optionname = '';

				if (!$list_intro_shown) {
					$optionname = $notrequiredoption . GetLang('MailingLists') . ':';
					$list_intro_shown = true;
				} else {
					$optionname = '&nbsp;';
				}

				if ($this->formtype == 'm') {
					$placeholder_lists = '%%Lists_' . $listid . '%%';
				}

				$option = '<label for="lists_' . $listid . '"><input type="checkbox" id="lists_' . $listid . '" name="lists[]" value="' . $listid . '"' . $placeholder_lists . ' />&nbsp;' . $lists_api->Get('name') . '</label>';

				$listlist .= str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);

				$alert = GetLang('Form_Javascript_ChooseLists');
				$javascript .= '
					lists_chosen = CheckMultiple' . $this->formid . '(f, "lists");
					if (!lists_chosen) {
						alert("' . $alert . '");
						return false;
					}
				';

			}
		} else {
			$listid = current($this->lists);

			if ($this->formtype == 'm') {
				$placeholder_lists = '%%Lists_' . $listid . '%%';
			}
			$listlist = '<input type="hidden" name="lists" value="' . $listid . '" />';
		}

		$formcontents = '';

		// custom fields is a multidimensional array with list as the key.
		// The subarray contains fields to show for that list.
		// $displayfields = array();
		$displayfields = $this->fieldorder;

		foreach ($this->customfields as $p => $field) {
			if (!in_array($field, $displayfields)) {
				$displayfields[] = $field;
			}
		}

		if (!class_exists('CustomFields_API', false)) {
			require_once(dirname(__FILE__) . '/customfields.php');
		}

		$customfields_api = new CustomFields_API(0, false);
		$customfields_api->Db = $this->Db;

		$shown_list_options = false;

		foreach ($displayfields as $p => $field) {
			if ($field == 'e') {
				$optionname = $requiredoption . GetLang('Form_EmailAddress') . ':';

				$option = '<input type="text" name="email" value="' . $email_placeholder . '" />';
				$formcontents .= str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);
				continue;
			}

			if ($field == 'cl') {
				$shown_list_options = true;
				$formcontents .= $listlist;
				continue;
			}

			if ($this->formtype == 'u') {
				continue;
			}

			if ($field == 'cf') {
				$formcontents .= $formatlist;
				continue;
			}

			$option = '';
			$optionvalue = '';

			$loaded = $customfields_api->Load($field);

			if (!$loaded) {
				continue;
			}

			$subfield = $customfields_api->LoadSubField();

			$javascript .= $subfield->CreateJavascript($this->formid);

			if ($subfield->IsRequired()) {
				$optionname = $requiredoption;
			} else {
				$optionname = $notrequiredoption;
			}
			$optionname .= $subfield->GetFieldName() . ':';
			$option = $subfield->DisplayFieldOptions($customfields_api->Settings['DefaultValue'], true, $this->formid);

			if ($this->formtype == 'm') {
				switch ($subfield->fieldtype) {
					case 'dropdown':
						$option = preg_replace('/<option value="(.*?)">/', "<option value=\"\${1}\"%%CustomField_".$field."_\${1}%%>", $option);
					break;

					case 'checkbox':
						$option = preg_replace('/name="(.*?)" value="(.*?)">/', "name=\"\${1}\" value=\"\${2}\"%%CustomField_".$field."_\${2}%%>", $option);
					break;

					case 'radiobutton':
						$option = preg_replace('/value="(.*?)">/', "value=\"\${1}\"%%CustomField_".$field."_\${1}%%>", $option);
					break;

					case 'date':
						foreach (array('dd', 'mm', 'yy') as $p => $datepart) {
							$match_string = preg_quote('<select name="CustomFields[' . $field . '][' . $datepart . ']"', '%') . '.*?\>(.*?)' . preg_quote('</select>', '%');
							if (preg_match('%'.$match_string.'%i', $option, $matches)) {
								$orig_text = $full_text = $matches[0];

								$full_text = preg_replace('/value="(.*?)">/', "value=\"\${1}\"%%CustomField_" . $field . "_\${1}_" . $datepart . "%%>", $full_text);

								$option = str_replace($orig_text, $full_text, $option);
							}
						}
					break;

					case 'textarea':
						$option = str_replace('</textarea>', '%%CustomField_' . $field . '%%</textarea>', $option);
					break;
										
					case 'number':
						$option = str_replace('value="0"', 'value="%%CustomField_' . $field . '%%"', $option);
					break;

					default:
						$option = str_replace('value=""', 'value="%%CustomField_' . $field . '%%"', $option);
					break;
				}
			}

			$formcontents .= str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);
		}

		if ($this->formtype == 'm' && !$shown_list_options) {
			$formcontents .= $listlist;
		}

		switch ($this->formtype) {
			case 's':
				$formaction = SENDSTUDIO_APPLICATION_URL . '/form.php?form=' . $this->formid;
			break;
			case 'u':
				$formaction = SENDSTUDIO_APPLICATION_URL . '/unsubform.php?form=' . $this->formid;
			break;

			case 'f':
			case 'm':
				/**
				* We don't hardcode the form action in case we are generating the form. Since a modify details form stays inside sendstudio, we don't want to hardcode the url (instead it's generated by the modifydetails.php file).
				* Why? In case we change the url - we don't want to have to change database values at the same time.
				*/
				$formaction = '%%FORMACTION%%';
			break;
			default:
				$formaction = false;
		}

		if (!in_array('cf', $displayfields)) {
			$formcontents .= $formatlist;
		}

		if (!class_exists('captcha_api', false)) {
			require_once(dirname(__FILE__) . '/captcha.php');
		}

		$captcha_api = new Captcha_API($inside_sendstudio);

		if ($this->usecaptcha) {
			$optionname = $requiredoption . GetLang('Form_EnterCaptcha') . ':';

			if ($this->formtype == 'm') {
				$option = '%%captchaimage%%';
			} else {
				$option = $captcha_api->ShowCaptcha();
			}

			$option .= '<br/><input type="text" name="captcha" value="" />';

			$formcontents .= str_replace(array('%%GLOBAL_OptionName%%', '%%GLOBAL_Option%%'), array($optionname, $option), $displayoption);
		}

		$javascript .= '
				return true;
			}
		';

		$content = str_replace(array('%%FormContents%%', '%%FormAction%%', '%%FormID%%', '%%Javascript%%'), array($formcontents, $formaction, $this->formid, $javascript), $content);

		$content = $sendstudio_functions->ReplaceLanguageVariables($content);
		return $content;
	}
示例#2
0
IEM::sessionSet('Form', $form);

$formhtml = $formapi->Get('formhtml');

$placeholders[] = '%%FORMACTION%%';
$placeholder_values[] = SENDSTUDIO_APPLICATION_URL . '/updatedetails.php';

if ($formapi->usecaptcha) {
	$captcha_api = $sendstudio_functions->GetApi('Captcha');

	$captcha_api = new Captcha_API();
	// so we don't include the session stuff in the captcha image, we set this flag for now.
	// this stops the session from being blanked out when you submit and causing an error.
	$captcha_api->Set('modify_details', true);
	$captcha_api->CreateSecret();

	$placeholders[] = '%%captchaimage%%';
	$placeholder_values[] = $captcha_api->ShowCaptcha();
}

// pre-fill the form.
$formhtml = str_replace($placeholders, $placeholder_values, $formhtml);

// get rid of anything we don't need.
$formhtml = preg_replace('/%%CustomField_(.*?)%%/', '', $formhtml);

$formhtml = preg_replace('/%%Lists_(.*?)%%/', '', $formhtml);

// print 'er out!
echo $formhtml;