<?php

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

						///////////////////////////////////////
							
						admin_validate_section("getpaid",1);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									getpaid_manager();
									break;
									
								case 'getpaid_switch':
									getpaid_switch();
									break;
									
								case 'getpaid_create_package':
									getpaid_create_package();
									break;
									
								case 'getpaid_save_package':
									getpaid_save_package();
									break;
									
								case 'getpaid_system':
									getpaid_system();
									break;
									
								case 'getpaid_save_system':
									getpaid_save_system();
									break;
									
								case 'getpaid_delete_package':
									getpaid_delete_package();
									break;
									
								case 'getpaid_delete_system':
									getpaid_delete_system();
									break;
									
								case 'getpaid_featured':
									getpaid_featured();
									break;
									
								case 'getpaid_save_featured':
									getpaid_save_featured();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function getpaid_manager(){
	
	admin_validate();
	
	$data["section"] = "getpaid";
	$data["subsection"] = 1;
	$data["title_id"] = "14";	
	$data["themes"] = admin_get_themes();	
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	$data["getpaid"] = $getpaid;
	
	$layout[0] = "getpaid_settings";
	
	admin_output($layout,$data);
	
}//getpaid_manager
						
function getpaid_switch(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$getpaid["enabled"] = abs($getpaid["enabled"]-1);
	
	module_save_settings($theme,"getpaid",$getpaid);
	
	load_page("index.php?section=getpaid&theme=".$data["theme"]);
	
}//getpaid_switch

function getpaid_create_package(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$data["title_id"] = "15";	$data["section"] = "getpaid";
	$data["subsection"] = 1;
	
	$data["pages"]   = admin_get_pages($theme);	
	$data["modules"] = admin_get_modules();
	
	$package_id = $GLOBALS["Get"]->type_val("package_id","integer");
	
	if(!$package_id){
	
		$vals = array(
			"name",
			"price",
			"currency",
			"period",
			"forbidden_pages",
			"forbidden_modules",
			"additional"
		);
	
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
	
		$interval = $GLOBALS["Get"]->val("interval");
		if(!empty($data["values"]["period"])){
			$ind = strtolower($data["values"]["period"]);
			$data["values"]["interval"] = $interval[$ind];
		}//if
		
	}//if
	else{
		
		$data["package_id"] = $package_id;
		$data["values"] = $getpaid["membership"][$package_id];
		
	}//else
	
	$data["link"] = "index.php?section=getpaid&theme=".$data["theme"];
	
	$fpages = handle_empties(explode(",",$data["values"][forbidden_pages]));
	$apages = handle_empties(array_diff(array_keys($data["pages"]),$fpages));
	
	$fmodules = handle_empties(explode(",",$data["values"][forbidden_modules]));
	$amodules = handle_empties(array_diff(array_keys($data["modules"]),$fmodules));
	
	$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;

	while(list($key,$val) = each($apages)){
		$allowed_pages_line.= $val."|".$data["pages"][$val]."|";
	}//while
	$allowed_pages_line = rtrim($allowed_pages_line,"|");
	while(list($key,$val) = each($fpages)){
		$forbidden_pages_line.= $val."|".$data["pages"][$val]."|";
	}//while
	$forbidden_pages_line = rtrim($forbidden_pages_line,"|");
	while(list($key,$val) = each($amodules)){
		$allowed_modules_line.= $val."|".$data["modules"][$val]."|";
	}//while
	$allowed_modules_line = rtrim($allowed_modules_line,"|");
	while(list($key,$val) = each($fmodules)){
		$forbidden_modules_line.= $val."|".$data["modules"][$val]."|";
	}//while
	$forbidden_modules_line = rtrim($forbidden_modules_line,"|");
	
	$data["onload"] = "HS_LoadOptions(document.package.allow_pages,'".htmlspecialchars(ereg_replace("'","\'",$allowed_pages_line))."');";
	$data["onload"].= "HS_LoadOptions(document.package.forbid_pages,'".htmlspecialchars(ereg_replace("'","\'",$forbidden_pages_line))."');";
	$data["onload"].= "HS_LoadOptions(document.package.allow_modules,'".htmlspecialchars(ereg_replace("'","\'",$allowed_modules_line))."');";
	$data["onload"].= "HS_LoadOptions(document.package.forbid_modules,'".htmlspecialchars(ereg_replace("'","\'",$forbidden_modules_line))."');";
	
	$layout[0] = "getpaid_create_package";
	
	admin_output($layout,$data);
	
}//getpaid_create_package

function getpaid_save_package(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	$package_id = $GLOBALS["Get"]->type_val("package_id","integer");
	if(empty($package_id))
		$package_id = @max(array_keys($getpaid["membership"])) + 1;
	
	$free_account_available = 1;
	while(list($key,$val) = each($getpaid["membership"])){
		if($key != $package_id){
			if(empty($val["price"])){
				$free_account_available = 0;
				break;
			}//if
		}//if
	}//while
	
	$vals = array(
		"name",
		"price",
		"currency",
		"period",
		"forbidden_pages",
		"forbidden_modules",
		"additional"
	);
	
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$interval = $GLOBALS["Get"]->val("interval");
	if(!empty($values["period"])){
		$ind = strtolower($values["period"]);
		$values["interval"] = $interval[$ind];
	}//if
	
	if(empty($values["name"])){
		$GLOBALS["aerr_id"] = 5;
		getpaid_create_package();
		exit;
	}//if
	if(empty($values["price"]) && !$free_account_available){
		$GLOBALS["aerr_id"] = 6;
		getpaid_create_package();
		exit;
	}//if	
	if(!empty($values["period"]) && empty($values["interval"])){
		$GLOBALS["aerr_id"] = 7;
		getpaid_create_package();
		exit;
	}//if
	
	$getpaid["membership"][$package_id] = $values;
	
	module_save_settings($theme,"getpaid",$getpaid);
	
	load_page("index.php?section=getpaid&theme=".$data["theme"]);
	
}//getpaid_save_package

function getpaid_system(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	$data["section"] = "getpaid";
	$data["subsection"] = 1;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$data["title_id"] = "15";	
	$system_id = $GLOBALS["Get"]->type_val("system_id","integer");
	
	if(!$system_id){
	
		$vals = array(
			"name",
			"payment_processor",
			"cancel_processor"
		);
	
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
		
	}//if
	else{
		
		$data["system_id"] = $system_id;
		$data["values"] = $getpaid["systems"][$system_id];
		
	}//else
	
	if(empty($data["values"]["payment_processor"]))
		$data["values"]["payment_processor"] = URL."/psystems/paypal/payment_processor.php";
		
	if(empty($data["values"][cancel_processor]))
		$data["values"][cancel_processor] = URL."/psystems/paypal/cancel_processor.php";
	
	$data["link"] = "index.php?section=getpaid&theme=".$data["theme"];
	
	$layout[0] = "getpaid_system";
	
	admin_output($layout,$data);
	
}//getpaid_system

function getpaid_save_system(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$vals = array(
		"name",
		"payment_processor",
		"cancel_processor"
	);
	
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["name"])){
		$GLOBALS["aerr_id"] = 3;
		getpaid_system();
		exit;
	}//if
	if(empty($values["payment_processor"])){
		$GLOBALS["aerr_id"] = 4;
		getpaid_system();
		exit;
	}//if
	
	$system_id = $GLOBALS["Get"]->type_val("system_id","integer");
	if(empty($system_id)){
		$system_id = @max(array_keys($getpaid["systems"])) + 1;
	}//if
	
	$getpaid["systems"][$system_id] = $values;
	
	module_save_settings($theme,"getpaid",$getpaid);
	
	load_page("index.php?section=getpaid&theme=".$theme);
	
}//getpaid_save_system

function getpaid_delete_package(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$package_id = $GLOBALS["Get"]->type_val("package_id","integer");
	if($package_id)
		unset($getpaid["membership"][$package_id]);
	
	module_save_settings($theme,"getpaid",$getpaid);
	
	load_page("index.php?section=getpaid&theme=".$data["theme"]);
	
}//getpaid_delete_package

function getpaid_delete_system(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/modules/getpaid.inc.php");
	
	$system_id = $GLOBALS["Get"]->type_val("system_id","integer");
	if($system_id)
		unset($getpaid["systems"][$system_id]);
	
	module_save_settings($theme,"getpaid",$getpaid);
	
	load_page("index.php?section=getpaid&theme=".$theme);
	
}//getpaid_delete_system

function getpaid_featured(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	$data["section"] = "getpaid";
	$data["subsection"] = 1;
	
	$data["title_id"] = "15";	$data["link"]  = "index.php?section=getpaid&theme=".$theme;	
	
	$module = $GLOBALS["Get"]->val("module");
	$data["module"] = $module;
	$data["modules"] = admin_get_modules();
	
	switch($module){
		case 'blogs':
			include(DOC_ROOT."/themes/".$theme."/modules/blogs.inc.php");			
			$data["featured"] = $blogs["featured"];
			$layout[0] = "getpaid_blogs";
			break;
		case 'classifieds':
			include(DOC_ROOT."/themes/".$theme."/modules/classifieds.inc.php");
			include(DOC_ROOT."/themes/".$theme."/system/cls_categories.inc.php");
			$data["featured"] = $classifieds["featured"];
			$data["categories"] = $categories;
			$data["subcategories"] = $subcategories;
			$layout[0] = "getpaid_categories";
			break;
		case 'events':
			include(DOC_ROOT."/themes/".$theme."/modules/events.inc.php");
			include(DOC_ROOT."/themes/".$theme."/system/evn_categories.inc.php");
			$data["featured"] = $events["featured"];
			$data["categories"] = $categories;
			$data["subcategories"] = $subcategories;
			$layout[0] = "getpaid_categories";
			break;
		case 'clubs':
			include(DOC_ROOT."/themes/".$theme."/modules/clubs.inc.php");
			include(DOC_ROOT."/themes/".$theme."/system/clb_categories.inc.php");
			$data["featured"] = $clubs["featured"];
			$data["categories"] = $categories;
			$data["subcategories"] = $subcategories;
			$layout[0] = "getpaid_categories";
			break;
	}//switch
	
	admin_output($layout,$data);
	
}//getpaid_featured

function getpaid_save_featured(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;
	
	$module = $GLOBALS["Get"]->val("module");
	switch($module){
		case 'blogs':
			include(DOC_ROOT."/themes/".$theme."/modules/blogs.inc.php");
			$vals = array("daily","weekly","monthly","quarterly","yearly");
			$values = $GLOBALS["Get"]->arr($vals,1);
			while(list($key,$val) = each($values)){
				if(extract_null_value($val["price"]))
					unset($values[$key]);
			}//while
			$blogs["featured"] = $values;
			module_save_settings($theme,"blogs",$blogs);
			break;
		default:
			include(DOC_ROOT."/themes/".$theme."/modules/".$module.".inc.php");
			$vals = array("daily","weekly","monthly","quarterly","yearly");
			$values = $GLOBALS["Get"]->arr($vals,1);
			while(list($key,$val) = each($values)){
				while(list($k,$v) = each($val)){
					if(extract_null_value($v["price"]))
						unset($values[$key][$k]);
				}//while
			}//while
			${$module}["featured"] = $values;
			module_save_settings($theme,$module,${$module});
			break;
	}//switch
	
	load_page("index.php?section=getpaid&action=getpaid_featured&module=".$module."&theme=".$data["theme"]);
	
}//getpaid_save_featured
						
?>