HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.form.php
<?php
/**
 * This file contains the iwp_form class
 *
 * @version $Id$
 * 
 *
 * @package IWP
 * @subpackage IWP_FormFields
 */

/**
 * IWP Form Class
 * This class is used to generate a form using the fields classes in the form folder.
 * It loads them up into groups and then into tabs.
 *
 * @package IWP
 * @subpackage IWP_FormFields
 */
class iwp_form extends iwp_base {
	/**
	 * This is an array of groups where each element is an associative array for a group of fields.
	 * The key is the group name and the value is the array of fields.
	 *
	 * @var array
	 */
	public $Groups = array();

	/**
	 * This is an array of tabs, with the values being the groups that are to be placed into the tabs.
	 *
	 * @var array
	 */
	public $Tabs = array();

	/**
	 * This is a flat array of the fields. The key is the fieldname and the value is the field object which
	 * can be any one of the classes from the form folder, all sharing a common interface.
	 *
	 * @var array
	 */
	public $Fields = array();

	/**
	 * This is an array of javascript commands to run on the document ready event in jQuery.
	 * Use the RunJsOnPageLoad() function to set a new element in the array. They are set by the field objects.
	 *
	 * @var array
	 * @see RunJsOnPageLoad()
	 */
	public $JsOnLoad = array();

	/**
	 * This is a toggle option to enable tabs, by default it is off because most forms don't use tabs
	 *
	 * @var boolean
	 */

	public $EnableTabs = false;

	/**
	 * This function adds a new group to the list of current groups and adds it into a tab.
	 *
	 * @param string $name The name of the group to add
	 * @param string $parent The name of the parent tab to add the group into. If this is null the group is added to the last tab created.
	 *
	 * @return void Doesn't return anything
	 */
	public function AddGroup($name,$parent=null){
		// ensure the name is not blank
		if($this->valid->IsBlank($name)){
			return;
		}

		if($this->EnableTabs){
			if(is_null($parent)){
				// if no parent tab was specified then we just add it to the last tab added
				$CurrentTab = array_last_key($this->Tabs);
				$this->Tabs[$CurrentTab][] = $name;
			}else{
				// they specified a parent, so lets make sure its valid before we add it
				if(!isset($this->Tabs[$parent])){
					return;
				}
				$this->Tabs[$parent][] = $name;
			}
		}

		$this->Groups[$name] = array();
	}

	/**
	 * This adds in a new tab to the form design
	 *
	 * @param string $name The name of the tab to be added
	 *
	 * @return void Doesn't return anything
	 */
	public function AddTab($name){
		// ensure the name is not blank
		if($this->valid->IsBlank($name)){
			return;
		}

		// all is good, add the tab
		$this->Tabs[$name] = array();
	}

	/**
	 * This adds a new field into the form. It takes in a type and selects the appropriate class from the forms folder
	 * and instantiates a new object and stores it in a member array.
	 *
	 * @param string $type The type of field to use, e.g. textbox, wysiwyg, checkbox or a custom module field. Determines what field class is used.
	 * @param string $name The name of the field being created
	 * @param string $group The name of the group to add the field to. If this null, the last group added will be used.
	 * @param object $lang The language object to be used for this field's template parsing. This is commonly used for module fields.
	 *
	 * @return iwp_field Returns a reference to the field object that was created and added, most likely an extension of iwp_field.
	 */
	public function AddField($type, $name, $group=null, $lang=null) {
		// make sure there are no bad characters
		$name = $this->valid->FilterFilename($name);

		// ensure the name is not blank
		if($this->valid->IsBlank($name)){
			return;
		}

		if(is_array($type)){
			// if the type is an array, then its not a standard field type, but rather a module field
			if(substr($type[0],0, strlen('module_custom')) == "module_custom"){
				$type[0] = str_replace('module_custom:', '', $type[0]);
				$modules = iwp_modules::getInstance();
				$this->Fields[$name] = $modules->GetModule($type[1])->GetModuleFieldCustom($type[0]);
			}
		}else{
			switch($type){
				case "textbox":
				case "password":
				case "textarea":
				case "radio":
				case "checkbox":
				case "select":
				case "multiselect":
				case "wysiwyg":
				case "button":
				case "hidden":
				case "time":
				case "sortorder":
				case "date":
				case "datetime":
				case "multiplepages":
				case "imagedir":
				case "view":
				case "fileupload":
				case "empty":
				case "grouppermissions":
				case "nestedcontent":
					$class = "iwp_field_" . $type;
					$this->Fields[$name] = new $class($name);
					break;
				default:
					return;
			}
		}

		// if there is a language object, set the field's language variable to it
		if($lang !== null){
			$this->Fields[$name]->lang = $lang;
		}

		if($type == "hidden"){
			return $this->Fields[$name];
		}

		if(!is_null($group) && isset($this->Groups[$group])){
			$this->Groups[$group][] = $name;
		}else{
			// if there is no group specified add the field to the last group that was created
			$groupname = array_last_key($this->Groups);
			$this->Groups[$groupname][] = $name;
		}

		return $this->Fields[$name];
	}

	/**
	 * This function returns the final form consisting of the tabs, groups and form fields with javascript validation etc.
	 * It does not return the actual <form></form> tags.
	 *
	 * @return string The HTML of the final form fields layout.
	 */
	public function GetOutput(){
		// prep the variables
		$tabs = $divs = $output = $jsoutput = $TabOutput = $jsload = $hiddenFields = '';
		$GroupOutput = $jsfiles = $wysiwygs = array();

		foreach ($this->Fields as $name => $obj) {
			// loop through the fields and pull out the hidden fields,
			// we'll group them together at the beginning of the form
			if($obj->type === "hidden"){
				$hiddenFields .= $obj->GetFieldOutput();
			}

			// we also need to know about all the wysiwyg fields for the page load
			// as we can't turn the tabs on until they've loaded because devedit will die.
			if($obj->type === "wysiwyg"){
				$wysiwygs[] = $name;
			}
		}

		foreach ($this->Groups as $groupName => $thisGroup) {
			$thisGroupOutput = '';
			foreach($thisGroup as $_key=>$name) {
				// skip over the hidden fields, we've already got them
				if( $this->Field($name)->type == "hidden"){
					continue;
				}

				$thisGroupOutput .= $this->Field($name)->GetFieldOutput();
				$jsfiles = array_merge($jsfiles, $this->Field($name)->RequiredJs);
			}
			$this->template->Assign('GroupName', $groupName);
			$this->template->Assign('fieldrows', $thisGroupOutput);

			if(!isset($GroupOutput[$groupName])){
				$GroupOutput[$groupName] = '';
			}

			$GroupOutput[$groupName] .= $this->template->ParseTemplate('form.group', true);
		}

		$jsfiles = array_unique($jsfiles);

		foreach($jsfiles as $_key=>$val) {
			if(is_array($val)){
				$file = $val[0];
				$req = $val[1];
			}else{
				$file = $val;
				$req = false;
			}

			if($req !== false){
				$jsoutput .= '<!--[if '.$req.']><script type="text/javascript" src="'.$file.'"></script><![endif]-->'."\n";
			}else{
				$jsoutput .= '<script type="text/javascript" src="'.$file.'"></script>'."\n";
			}
		}
		$i = 0;

		if (isset($_GET['formtab'])) {
			$requestedTab = (int)$_GET['formtab'];
		} else {
			$requestedTab = 0;
		}

		if($this->EnableTabs && count($this->Tabs) > 1) {
			foreach($this->Tabs as $tab => $arrGroups){
				$TabOutput ='';
				foreach($arrGroups as $_k => $groupName){
					$TabOutput .= $GroupOutput[$groupName];
				}
				$tabs .= '<li id="tabitem-'.$i.'"><a href="#tab-'.$i.'">'.iwp_htmlspecialchars($tab).'</a></li>'."\n";
				$divs .= '<div id="tab-'.$i.'"';
				if ($requestedTab != $i) {
					$divs .= ' class="HideTabsDuringLoad" ';
				}
				$divs .= '>'.$TabOutput.'</div>'."\n\n";
				++$i;
			}

			$output = '<div id="tabMenu"><ul id="tabList" class="tabnav">'.$tabs.'</ul>'.$divs.'</div>';
		}else{
			foreach($GroupOutput as $gname=>$html){
				$output .= $html;
			}
		}

		// check for onload
		$jsload = '<script type="text/javascript">
					$(document).ready(function() {';
		if(sizeof($this->JsOnLoad) > 0){
			foreach($this->JsOnLoad as $k=>$code){
				$jsload .= "\n\n".$code ."\n\n";
			}
		}
		if($this->EnableTabs){
		$jsload .= 'tryToAddTabs();	});var totalTries = 0;function tryToAddTabs() {';

		$jsload .= '$("#tabMenu").tabs({ selected: '. $requestedTab .' }); $(".HideTabsDuringLoad").removeClass("HideTabsDuringLoad");';

		$jsload .= '}';
		}else{
			$jsload .= '});';
		}
		$jsload .= '</script>';

		return $jsoutput.$jsload.$hiddenFields.$output;
	}

	/**
	 * This function pools together javascript commands to run in the jQuery $(document).ready(); Each entry is added to an array
	 *
	 * @param string $js The javascript command(s) to run on the page load
	 *
	 * @return void
	 */
	public function RunJsOnPageLoad($js){
		$this->JsOnLoad[] = $js;
	}

	/**
	*
	* @param array $hideRow
	* @param string $checkboxFieldId
	* @return iwp_form
	*/
	public function HideRowIfThisChecked ($hideRow, $checkboxFieldId){
		if (!is_array($hideRow)) {
			$hideRow = array($hideRow);
		}

		foreach ($hideRow as $thisHideRow) {
			$this->RunJsOnPageLoad('$("#'.$checkboxFieldId.'").bind("change", function() { ShowField("'.$checkboxFieldId.'", "'.$thisHideRow.'_row", true); }); ShowField("'.$checkboxFieldId.'", "'.$thisHideRow.'_row", true); ');
		}

		return $this;
	}

	/**
	*
	* @param array $hideRow
	* @param string $checkboxFieldId
	* @return iwp_form
	*/
	public function HideRowIfThisNotChecked ($hideRow, $checkboxFieldId){
		if (!is_array($hideRow)) {
			$hideRow = array($hideRow);
		}

		foreach ($hideRow as $thisHideRow) {
			$this->RunJsOnPageLoad('$("#'.$checkboxFieldId.'").bind("change blur click", function() { ShowField("'.$checkboxFieldId.'", "'.$thisHideRow.'_row"); }); ShowField("'.$checkboxFieldId.'", "'.$thisHideRow.'_row"); ');
		}

		return $this;
	}

	/**
	 * This function returns a field object
	 *
	 * @param string $name The name of the field object to retrieve
	 *
	 * @return iwp_field The form field object of the requested field
	 *
	 * @see $Fields
	 */
	public function Field($name){
		if(isset($this->Fields[$name])){
			return $this->Fields[$name];
		}
	}

	/**
	 * Returns the $Fields array
	 *
	 * @return array The array of field objects stored in this class instance
	 *
	 * @see $Fields
	 */
	public function Fields(){
		return $this->Fields;
	}

	/**
	 * Gets the javascript validation code from every field object loaded
	 *
	 * @return string All the javascript code for the validation functions for each field
	 */

	public function GetJSFieldValidation(){
		$Validation = '';
		foreach($this->Fields as $_key=>$obj) {
			$Validation .= $obj->GetFieldValidation();
		}
		return $Validation;
	}

	/**
	 * Gets all the language variable values for the field names.
	 *
	 * @return string The javascript array with the language values for each field
	 */
	public function GetFieldNamesJavascript(){
		$output = array();

		foreach($this->Fields as $_key=>$obj) {
			$str = $obj->GetNameJavascript();
			if(is_array($str)){
				$output[] = "'" . implode("': '", $str) . "'";
			}
		}

		return implode(", ", $output);
	}

	/**
	 * Validates this form against the provided data. Returns a value indicating a valid form and populates provided arrays with error information.
	 *
	 * @param array $data An associative array of data to check (usually $_POST)
	 * @param array &$errorMsgs An array, passed in by reference, which will be populated with error messages if the form is not valid.
	 * @param array &$errorFields An array, passed in by reference, which will be populated with invalid field names if the form is not valid.
	 * @return boolean Returns true if the form was validated OK, otherwise false.
	 */
	public function Validate ($data, &$errorMsgs, &$errorFields) {
		$valid = true;

		foreach($this->Fields as $_key=>$obj) {
			// Loop through each fields' validations
			$validations = $obj->GetValidations();
			foreach($validations as $_key=>$func) {
				// Check that the posted entry is valid
				if(is_array($obj->GetFieldOptions()) && sizeof($obj->GetFieldOptions()) > 0) {
					if(!isset($data[$obj->PostFieldName()]) || !$this->valid->$func($data[$obj->PostFieldName()], $obj->GetFieldOptions())) {
						// If it isn't, add to the errors list
						$name = $obj->GetNameJavascript();
						$errorMsgs[] = sprintf(GetLang('ValidError_'.$func), $name[1]);
						$errorFields[] = $obj->FieldName();
						$valid = false;
					}

				} else {
					if(!$this->valid->$func($data[$obj->PostFieldName()], $obj->PostFieldName())) {
						// If it isn't, add to the errors list
						$name = $obj->GetNameJavascript();
						$errorMsgs[] = sprintf(GetLang('ValidError_'.$func), $name[1]);
						$errorFields[] = $obj->FieldName();
						$valid = false;
					}
				}
			}
		}

		return $valid;
	}
}