<?php

						/*************************************\
						
						    Handshakes Professional (3.2)
						            by DZOIC LTD
						       Copyright(c) 2005-2006
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////

						admin_validate_section("customize",6);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case '':
									forms();
									break;
									
								case 'edit':
									edit_form();
									break;
									
								case 'repos_item':
									reposition_item();
									break;
									
								case 'edit_item':
									edit_form_item();
									break;
									
								case 'save_item':
									save_form_item();
									break;
									
								case 'edit_button':
									edit_form_button();
									break;
									
								case 'save_button':
									save_form_button();
									break;
									
								case 'save_pages':
									save_form_pages();
									break;

							}//switch

						///////////////////////////////////////
						
function forms(){
	
	admin_validate();
	
	$data["themes"] = admin_get_themes();
	$data["forms"]  = array();

	$theme = $GLOBALS["Get"]->val("theme");
	if(!$theme)
		$theme = $GLOBALS["THEME"];
	
	if($theme){
		$form_names = forms_scan($theme);
		foreach($form_names as $form_name){
			$form_data = form_data($theme,$form_name);
			$form["name"] = ucfirst(hs_ereg_replace("_"," ",$form_name));
			$form["fields"] = count($form_data["names"]);
			$form["buttons"] = count($form_data["txt_buttons"]);
			$form["edit"] = "index.php?section=forms&action=edit&theme=".$theme."&form=".$form_name;
			$data["forms"][] = $form;
			$sort_array[] = strtolower($form["name"]);
		}//foreach
		$layout[0] = "forms";
	}//if
	
	array_multisort($sort_array,$data["forms"]);
	
	$data["theme"] = $theme;
	
	$data["section"] = "customize";
	$data["subsection"] = 6;
	$data["title_id"] = "11";	
	admin_output($layout,$data);
	
}//forms

function forms_scan($theme){
	
	$forms = array();
	
	$folder = DOC_ROOT."/themes/".$theme."/forms";
	$dir = opendir($folder);
	while($file = readdir($dir)){
		if(eregi("\.inc\.php",$file)){
			$form_name = ereg_replace("\.inc\.php","",$file);
			$forms[] = $form_name;
		}//if
	}//while
	closedir($dir);
	
	return $forms;
	
}//forms_scan

function edit_form(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$form_name = $GLOBALS["Get"]->val("form");
	
	$data["form_name"] = ucfirst(hs_ereg_replace("_"," ",$form_name));

	$sql_query = "SELECT page FROM associations WHERE assoc_type='form'
				  AND theme='".$theme."' AND association='".$form_name."'";
	
	$result = $GLOBALS["DB"]->result($sql_query);
	$form_pages = array();
	foreach($result as $res){
		$form_pages[] = $res["page"];
	}//foreach
	
	$form_data = form_data($theme,$form_name);
	
	$data["items"] = array();
	$data["buttons"] = array();
	
	$keys = array_keys($form_data["names"]);
	
	$start = $keys[0];
	$i = count($keys)-1;
	$end = $keys[$i];
	while(list($key,$val) = each($form_data["names"])){
		$field = form_extract_field($form_data["fields"][$key],$theme);
		$item["label"]     = $form_data["labels"][$key];		
		$item["field"]     = $field["type"];
		$item["edit"]      = "index.php?section=forms&action=edit_item&form=".$form_name."&theme=".$theme."&item=".$key;
		if($key != $start)
			$item["move_up"] = "index.php?section=forms&action=repos_item&theme=".$theme."&form=".$form_name."&item=".$key."&direction=up&r=".time();
		else
			$item["move_up"] = "";
		if($key != $end)
			$item["move_down"] = "index.php?section=forms&action=repos_item&theme=".$theme."&form=".$form_name."&item=".$key."&direction=down&r=".time();
		else
			$item["move_down"] = "";
		$data["items"][] = $item;		
	}//while
	
	while(list($key,$val) = each($form_data["txt_buttons"])){
		$button["text"] = $val;
		$button["image"] = $form_data["img_buttons"][$key];
		$button["edit"] = "index.php?section=forms&action=edit_button&theme=".$theme."&form=".$form_name."&button=".$key;
		$data["buttons"][] = $button;
	}//while
	
	$data["form"] = $form_name;
	$data["theme"] = $theme;
	$data["link"] = "index.php?section=forms&theme=".$theme;
	$site_pages  = admin_get_pages($theme);
	
	$data["pages"] = array();
	$data["npages"] = array();
	
	foreach($form_pages as $form_page){
		$data["pages"][$form_page] = $site_pages[$form_page];
	}//foreach
	
	$nonform_pages = array_diff(array_keys($site_pages),array_keys($data["pages"]));
	foreach($nonform_pages as $nonform_page){
		$data["npages"][$nonform_page] = $site_pages[$nonform_page];
	}//foreach
	
	$layout[0] = "edit_form";	

	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	function HS_LoadOptions(FieldHandler, OptionsLine){
		if(OptionsLine != ''){
			ListItems = OptionsLine.split("|");
			OptionsHandler = FieldHandler.options;
			for(i=0;i<(ListItems.length/2);i++){
				j = i*2;
				OptionsHandler[i] = new Option(ListItems[j+1],ListItems[j]);
			}//for
		}//if
	}//HS_LoadOptions
	function HS_MoveOptions(FieldHandlerFrom, FieldHandlerTo){
		var TransferOptions = new Array();
		var j=0;
		var k=0;		
		OptionsHandlerFrom = FieldHandlerFrom.options;
		OptionsHandlerTo = FieldHandlerTo.options;		
		for(i=0;i<OptionsHandlerFrom.length;i++){
			if(OptionsHandlerFrom[i].selected == true){
				OptionsHandlerTo[OptionsHandlerTo.length] = 
				new Option(OptionsHandlerFrom[i].text,OptionsHandlerFrom[i].value);
				OptionsHandlerFrom[i] = null;
				TransferOptions[j] = i;
				j++;
				i--;				
			}//if
		}//for
	}//HS_MoveOptions
	function HS_PrepareData(FieldHandlerSrc, FieldHandlerResult){
		var ResultLine = "";
		OptionsHandlerSrc = FieldHandlerSrc.options;
		for(i=0;i<OptionsHandlerSrc.length;i++){
			ResultLine+= OptionsHandlerSrc[i].value + ",";
		}//for
		FieldHandlerResult.value = ResultLine;		
	}//HS_PrepareData	
//-->
</script>
JAVASCRIPTCODE;

	$form_pages = "";
	while(list($key,$val) = each($data["pages"])){
		$form_pages.= $key."|".$val."|";
	}//while
	$form_pages = rtrim($form_pages,"|");
	while(list($key,$val) = each($data["npages"])){
		$form_npages.= $key."|".$val."|";
	}//while
	$form_npages = rtrim($form_npages,"|");	
	
	$data["onload"].= "HS_LoadOptions(document.form_edit.m_pages,'".htmlspecialchars(ereg_replace("'","\'",$form_pages))."');";
	$data["onload"].= "HS_LoadOptions(document.form_edit.m_npages,'".htmlspecialchars(ereg_replace("'","\'",$form_npages))."');";
	
	$data["section"] = "customize";
	$data["subsection"] = 6;
	$data["title_id"] = "11";	
	admin_output($layout,$data);
	
}//edit_form

function reposition_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$form_name = $GLOBALS["Get"]->val("form");
	$item = $GLOBALS["Get"]->val("item");
	$direction = $GLOBALS["Get"]->val("direction");
	
	include(DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php");
	
	$keys = array_keys($form["names"]);
	$currkey = array_search($item,$keys);
	if($direction == "up")
		$newposition = $keys[$currkey-1];
	elseif($direction == "down")
		$newposition = $keys[$currkey+1];
		
	list($form["labels"][$item],$form["labels"][$newposition]) = array ($form["labels"][$newposition],$form["labels"][$item]);
	list($form["names"][$item],$form["names"][$newposition]) = array ($form["names"][$newposition],$form["names"][$item]);
	list($form["fields"][$item],$form["fields"][$newposition]) = array ($form["fields"][$newposition],$form["fields"][$item]);
	list($form["types"][$item],$form["types"][$newposition]) = array ($form["types"][$newposition],$form["types"][$item]);
	
	save_form_data($theme,$form_name,$form);
	
	load_page($GLOBALS["Get"]->referer());
	
}//reposition_name

function edit_form_item(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$form_name = $GLOBALS["Get"]->val("form");
	$item = $GLOBALS["Get"]->val("item");
	
	$data["theme"] = $theme;
	$data["form"]  = $form_name;
	$data["item"]  = $item;
	$data["form_name"] = ucfirst(hs_ereg_replace("_"," ",$form_name));
	
	$form_data = form_data($theme,$form_name);
	
	$data["label"] = $form_data["labels"][$item];
	$data["field"] = form_extract_field($form_data["fields"][$item],$theme);
	$data["link"]  = "index.php?section=forms&action=edit&theme=".$theme."&form=".$form_name;
	
	$data["section"] = "customize";
	$data["subsection"] = 6;
	$data["title_id"] = "11";	
	$layout[0] = "edit_form_item";
	
	admin_output($layout,$data);
	
}//edit_form_item

function save_form_item(){
	
	admin_validate();
	
	$vals = array("theme","form","item","label","properties");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	include(DOC_ROOT."/themes/".$values["theme"]."/forms/".$values["form"].".inc.php");
	
	$form["labels"][$values["item"]] = $values["label"];
	
	if(!is_array($values["properties"]))
		$values["properties"] = array();
	
	while(list($key,$val) = each($values["properties"])){
		$key = stripslashes(html_entity_decode($key));
		$val = stripslashes(html_entity_decode($val));
		$values["properties"][$key] = $val;
	}//while
	
	if(is_array($values["properties"])){
		$field = explode("|",$form["fields"][$values["item"]]);	
		$field_type = array_shift($field);		
		$new_properties = array_keys($values["properties"]);
		$allkeys = array_merge($new_properties,array_keys($field));
		$settings_end = max($allkeys);
		for($i=0;$i<=$settings_end;$i++){
			if(in_array($i,$new_properties))
				$field[$i] = $values["properties"][$i];
		}//for
		$new_field = $field_type."|".implode("|",$field);
		$form["fields"][$values["item"]] = $new_field;
	}//if
	
	save_form_data($values["theme"],$values["form"],$form);
	
	load_page("index.php?section=forms&action=edit_item&theme=".$values["theme"]."&form=".$values["form"]."&item=".$values["item"]);
	
}//save_form_item

function edit_form_button(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$form_name = $GLOBALS["Get"]->val("form");
	$button = $GLOBALS["Get"]->val("button");
	
	$data["theme"]  = $theme;
	$data["form"]   = $form_name;
	$data["button"] = $button;
	$data["form_name"] = ucfirst(hs_ereg_replace("_"," ",$form_name));
	
	$form_data = form_data($theme,$form_name);
	
	$data["text"] = $form_data["txt_buttons"][$button];
	$data["image"] = $form_data["img_buttons"][$button];
	$data["link"]  = "index.php?section=forms&action=edit&theme=".$theme."&form=".$form_name;
	
	$data["section"] = "customize";
	$data["subsection"] = 6;
	$data["title_id"] = "11";	
	$layout[0] = "edit_form_button";
	
	admin_output($layout,$data);
	
}//edit_form_button

function save_form_button(){
	
	admin_validate();
	
	$vals = array("theme","form","button","text","image");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	include(DOC_ROOT."/themes/".$values["theme"]."/forms/".$values["form"].".inc.php");
	
	$form["txt_buttons"][$values["button"]] = $values["text"];
	$form["img_buttons"][$values["button"]] = $values["image"];
	
	save_form_data($values["theme"],$values["form"],$form);
	
	load_page("index.php?section=forms&action=edit_button&theme=".$values["theme"]."&form=".$values["form"]."&button=".$values["button"]);
	
}//save_form_button

function save_form_pages(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$form_name = $GLOBALS["Get"]->val("form");
	
	$form_pages = $GLOBALS["Get"]->val("form_pages");
	$form_pages = handle_empties(explode(",",$form_pages));
	
	$sql_query = "DELETE FROM associations WHERE `assoc_type`='form' AND `association`='".$form_name."' AND `theme`='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	foreach($form_pages as $p){
		$sql_query = "INSERT INTO associations (`assoc_type`,`page`,`association`,`theme`)
					  VALUES ('form','".$p."','".$form_name."','".$theme."')";
		$GLOBALS["DB"]->execute($sql_query);
	}//foreach
	
	load_page("index.php?section=forms&action=edit&theme=".$theme."&form=".$form_name);
	
}//save_form_pages

function save_form_data($theme,$form_name,$form){
	
	$form_str = "<?php\n\n";
	$form_str.= go_through_array("\$form",$form);
	$form_str.= "\n?>";
	
	$form_file = DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php";
	$file_handler = fopen($form_file,"w");
	fwrite($file_handler,$form_str);
	fclose($file_handler);
	
}//save_form_data

function form_data($theme,$form_name){
	
	include(DOC_ROOT."/themes/".$theme."/forms/".$form_name.".inc.php");
	return $form;
	
}//form_data

function form_extract_field($field_code,$theme){
	
	$details = explode("|",$field_code);
	$field_type = array_shift($details);
	$field = array();
	
	switch($field_type){
		case 'textfield':
			$field["type"] = "Text Field";
			$field["properties"]["Field Length"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			if($details[1]){
				$options = array (
					"invite" => "General Invitation Subject",
					"blogs" => "Blogs Invitation Subject",
					"classifieds" => "Classifieds Invitation Subject",
					"events" => "Events Invitation Subject",
					"clubs" => "CLubs Invitation Subjcet"					
				);
				$field["properties"]["Default Subject"] = array ("type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2]);
			}//if
			break;
		case 'textfield_set':
			$field["type"] = "Set of Text Fields";
			$field["properties"]["Fields Length"] = array ( "type" => "input", "size" => "10", "name" => "properties[0]", "value" => $details[0] );
			$field["properties"]["Number of Fields"] = array ( "type" => "input", "size" => "10", "name" => "properties[1]", "value" => $details[1] );
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2] );
			break;
		case 'public_url':
			$field["type"] = "Public URL Field";
			$field["properties"]["Field Length"] = array ("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'passfield':
			$field["type"] = "Password Field";
			$field["properties"]["Field Length"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'checkbox':
			$field["type"] = "Checkbox";
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Ticked by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'options':
			$field["type"] = "Dropdown List";
			$options[""] = "Please, select...";
			include(DOC_ROOT."/themes/".$theme."/options/".$details[0].".inc.php");
			$field["properties"]["Selected by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[3]", "value" => $details[3]);
			break;
		case 'countries':
			$field["type"] = "Countries Dropdown List";
			$sql_query = "SELECT * FROM geo_countries ORDER BY name";
			$result = $GLOBALS["DB"]->result($sql_query);
			$options[""] = "Please, select...";
			foreach($result as $res){
				$options[$res["con_id"]] = $res["name"];
			}//foreach
			$field["properties"]["Selected by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[0]", "value" => $details[0]);
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'states':
			$field["type"] = "States Dropdown List";
			$sql_query = "SELECT * FROM geo_countries ORDER BY name";
			$result = $GLOBALS["DB"]->result($sql_query);
			$options[""] = "Please, select...";
			foreach($result as $res){
				$options[$res["con_id"]] = $res["name"];
			}//foreach
			$field["properties"]["Default Country"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'cities':
			$field["type"] = "Cities Dropdown List";
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[0]", "value" => $details[0]);
			break;
		case 'items':
			if($details[0] == "blogs")
				$field["type"] = "Member Blogs Dropdown List";
			elseif($details[0] == "classifieds")
				$field["type"] = "Member Classifieds Dropdown List";
			elseif($details[0] == "events")
				$field["type"] = "Member Events Dropdown List";
			elseif($details[0] == "clubs")
				$field["type"] = "Member Clubs Dropdown List";
			$field["properties"] = array();
			break;
		case 'club_members':
			$field["type"] = "Club Members Dropdown List";
			$field["properties"] = array();
			break;
		case 'handshakes_set':
			$field["type"] = "Member Friends Select Field";
			$field["properties"] = array();
			break;
		case 'categories':
			$field["type"] = "Categories Dropdown List";
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'subcategories':
			$field["type"] = "Subcategories Dropdown List";
			$options = array (
				"No",
				"Yes"
			);
			$field["properties"]["Enable Empty Option"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'payment_type':
			$field["type"] = "Payment Systems Dropdown List";
			$field["properties"] = array();
			break;
		case 'radio':
			$field["type"] = "Set of Radio Buttons";
			$options[""] = "Please, select...";
			include(DOC_ROOT."/themes/".$theme."/options/".$details[0].".inc.php");
			$field["properties"]["Selected by Default"] = array ("type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1]);
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[2]", "value" => $details[2] );
			break;
		case 'membership_level':
			$field["type"] = "Membership Packages Set of Radio Buttons";
			$field["properties"] = array();
			break;
		case 'price_plans':
			$field["type"] = "Price Plans Set of Radio Buttons";
			$field["properties"] = array();
			break;
		case 'set':
			$field["type"] = "Set of Checkboxes";
			$options = array(
				"Fields One by One",
				"Multiple Lines"
			);
			$field["properties"]["Output Style"] = array ( "type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1] );
			break;
		case 'date':
			$field["type"] = "Date Fields";
			$options = array(
				"Date in Past",
				"Date in Future"
			);
			$field["properties"]["Date Type"] = array ( "type" => "select", "options" => $options, "name" => "properties[1]", "value" => $details[1] );
			break;
		case 'date_time':
			$field["type"] = "Date &amp; Time Fields";
			$field["properties"] = array();
			break;
		case 'time':
			$field["type"] = "Time Fields";
			$field["properties"] = array();
			break;
		case 'file':
			$field["type"] = "File Upload Field";
			$field["properties"] = array();
			break;
		case 'textarea':
			$field["type"] = "Text Box";
			$field["properties"]["Rows"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			$field["properties"]["Cols"] = array ("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			if($details[2]){
				$options = array (
					"invite" => "General Invitation Subject",
					"blogs" => "Blogs Invitation Subject",
					"classifieds" => "Classifieds Invitation Subject",
					"events" => "Events Invitation Subject",
					"clubs" => "CLubs Invitation Subjcet"					
				);
				$field["properties"]["Default Subject"] = array ("type" => "select", "options" => $options, "name" => "properties[3]", "value" => $details[3]);
			}//if
			break;
		case 'htmlarea':
			$field["type"] = "WYSIWYG HTML Editor";
			$field["properties"]["Width"] = array ("type" => "input", "size" => 10, "name" => "properties[0]", "value" => $details[0]);
			$field["properties"]["Height"] = array ("type" => "input", "size" => 10, "name" => "properties[1]", "value" => $details[1]);
			break;
		case 'rate_scale':
			$field["type"] = "HotOrNot Rating Scale";
			$field["properties"] = array();
			break;
		case 'import_services':
			$field["type"] = "Supported Mail Services";
			$field["properties"] = array();
			break;
	}//switch
	
	$field["preview"] = build_field($field_type,array("dets" => $details),$theme);
	return $field;
	
}//form_extract_field
						
?>