<?php

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

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

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

							switch($action){

								case '':
									manage_functions();
									break;
									
								case 'edit':
									edit_function();
									break;
									
								case 'save':
									save_function();
									break;
									
								case 'delete':
									delete_function();
									break;

							}//switch

						///////////////////////////////////////
						
function manage_functions(){
	
	admin_validate();
	
	$data["section"] = "develop";
	$data["subsection"] = 3;
	$data["title_id"] = "12";	
	$data["themes"] = admin_get_themes();	
	$theme = $GLOBALS["Get"]->val("theme");	
	
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
		
	include(DOC_ROOT."/themes/".$theme."/system/tasks.inc.php");
	asort($tasks);
	
	while(list($key,$val) = each($tasks)){		
		$task["name"] = $val;
		$task["edit"] = "index.php?section=functions&action=edit&theme=".$theme."&funccode=".urlencode($key);
		$task["delete"] = "index.php?section=functions&action=delete&theme=".$theme."&funccode=".urlencode($key);
		$data["tasks"][] = $task;
	}//while
	
	$layout[0] = "functions_list";
	
	admin_output($layout,$data);
	
}//manage_functions

function edit_function(){
	
	admin_validate();
	
	$data["section"] = "develop";
	$data["subsection"] = 3;
	$data["title_id"] = "12";	
	$theme = $GLOBALS["Get"]->val("theme");
	$funccode = stripslashes(html_entity_decode(hs_ereg_replace("&#124;","|",$GLOBALS["Get"]->val("funccode"))));
	
	$code = explode("|",$funccode);
	
	$data["name"]     = task_name($theme,$funccode);
	$data["file"]     = $code[0].".php";
	$data["func"]     = $code[1];
	$data["theme"]    = $theme;
	$data["funccode"] = $funccode;
	$data["link"]     = "index.php?section=functions&theme=".$theme;

	$sql_query = "SELECT page FROM associations WHERE assoc_type='task' AND association='".$funccode."' AND theme='".$theme."'";
	$result = $GLOBALS["DB"]->result($sql_query);
	
	$task_pages = array();
	foreach($result as $res){
		$task_pages[] = $res["page"];
	}//foreach
		
	$site_pages  = admin_get_pages($theme);
	
	$data["pages"] = array();
	$data["npages"] = array();
	
	foreach($task_pages as $task_page){
		$data["pages"][$task_page] = $site_pages[$task_page];
	}//foreach
	
	$nontask_pages = array_diff(array_keys($site_pages),array_keys($data["pages"]));
	foreach($nontask_pages as $nontask_page){
		$data["npages"][$nontask_page] = $site_pages[$nontask_page];
	}//foreach

	$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;

	$task_pages = "";
	while(list($key,$val) = each($data["pages"])){
		$task_pages.= $key."|".$val."|";
	}//while
	$task_pages = rtrim($task_pages,"|");
	while(list($key,$val) = each($data["npages"])){
		$task_npages.= $key."|".$val."|";
	}//while
	$task_npages = rtrim($task_npages,"|");	
	
	$data["onload"].= "HS_LoadOptions(document.task_edit.t_pages,'".htmlspecialchars(ereg_replace("'","\'",$task_pages))."');";
	$data["onload"].= "HS_LoadOptions(document.task_edit.t_npages,'".htmlspecialchars(ereg_replace("'","\'",$task_npages))."');";
	
	$layout[0] = "edit_function";
	
	admin_output($layout,$data);
	
}//edit_function

function save_function(){
	
	admin_validate();
	
	$vals = array("theme","funccode","name","file","func","task_pages");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$theme = $values["theme"];
	
	while(list($key,$val) = each($values)){
		$values[$key] = stripslashes(html_entity_decode(hs_ereg_replace("&#124;","|",$val)));
	}//while
	
	if(empty($values["file"]) || empty($values["func"]) || empty($values["name"])){
		$GLOBALS["aerr_id"] = 10;
		edit_function();
		exit;
	}//if
	
	$funccode = ereg_replace("\.php$","",$values["file"])."|".$values["func"];
	
	include(DOC_ROOT."/themes/".$values["theme"]."/system/tasks.inc.php");
	unset($tasks[$values["funccode"]]);
	$tasks[$funccode] = $values["name"];	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$tasks",$tasks);
	$write_str.= "\n?>";
	$file = fopen(DOC_ROOT."/themes/".$values["theme"]."/system/tasks.inc.php","w");
	fwrite($file,$write_str);
	fclose($file);
	
	$task_pages = handle_empties(explode(",",$values["task_pages"]));
	
	$sql_query = "DELETE FROM associations WHERE `assoc_type`='task' AND `association`='".$funccode."' AND `theme`='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	foreach($task_pages as $p){
		$sql_query = "INSERT INTO associations (`assoc_type`,`page`,`association`,`theme`)
					  VALUES ('task','".$p."','".$funccode."','".$theme."')";
		$GLOBALS["DB"]->execute($sql_query);
	}//foreach
		
	load_page("index.php?section=functions&action=edit&theme=".$values["theme"]."&funccode=".urlencode($funccode));
	
}//save_function

function delete_function(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	$funccode = stripslashes(html_entity_decode(hs_ereg_replace("&#124;","|",$GLOBALS["Get"]->val("funccode"))));
	
	include(DOC_ROOT."/themes/".$theme."/system/tasks.inc.php");
	unset($tasks[$funccode]);
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$tasks",$tasks);
	$write_str.= "\n?>";
	$file = fopen(DOC_ROOT."/themes/".$theme."/system/tasks.inc.php","w");
	fwrite($file,$write_str);
	fclose($file);
	
	$sql_query = "DELETE FROM associations WHERE `assoc_type`='task' AND `association`='".$funccode."' AND `theme`='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=functions&theme=".$theme);
	
}//delete_function

function task_name($theme,$func){
	
	include(DOC_ROOT."/themes/".$theme."/system/tasks.inc.php");
	return $tasks[$func];
	
}//task_name
						
?>