<?php
!function_exists("adminmsg") && exit("Forbidden");

require_once(H_P."color.php");

if($action=="add" || $action=="edit"){
	InitGP(array('title','link','view','alt','color','b','i','u','pos','target','nid'),'P');
	empty($title) && adminmsg("nav_empty_title");
	empty($link) && adminmsg("nav_empty_link");
	$view	= (int)$view;
	$title	= Char_cv($title);
	$style	= $color."|".$b."|".$i."|".$u;
	$link	= Char_cv($link);
	$alt	= Char_cv($alt);
	if($action=="add"){
		$db->update("INSERT INTO pw_nav"
			. " SET " . pwSqlSingle(array(
				'title'		=> $title,
				'style'		=> $style,
				'link'		=> $link,
				'alt'		=> $alt,
				'target'	=> $target,
				'pos'		=> $pos,
				'view'		=> $view
		)));
	} elseif($action=="edit"){
		!isset($nid) && adminmsg("undefine_action");
		$db->update("UPDATE pw_nav"
			. " SET " . pwSqlSingle(array(
					'title'	=> $title,
					'style'	=> $style,
					'link'	=> $link,
					'alt'	=> $alt,
					'target'=> $target,
					'pos'	=> $pos,
					'view'	=> $view
				))
			. " WHERE nid=".pwEscape($nid)
		);
	}
	updatenav();
	$jumpto = $pos=="foot" ? "viewfoot" : "viewhead";
	adminmsg("operate_success","$basename&job=$jumpto");
} elseif($action=="del"){
	$nid = (int)GetGP('nid');
	$db->update("DELETE FROM pw_nav WHERE nid=".pwEscape($nid));
	updatenav();
	adminmsg("operate_success");
} elseif($action=="editview"){
	InitGP(array('view'),'P');
	foreach($view as $key=>$val){
		$val = (int)$val;
		$db->update("UPDATE pw_nav SET view=".pwEscape($val)."WHERE nid=".pwEscape($key));
	}
	updatenav();
	adminmsg("operate_success");
} elseif(!$job || $job=="add"){ ##job
	$actionvalue = "add";
	$head_check  = "checked";
	$blank_check = "checked";
	foreach($colors as $c){
		$color_select.="<option value=\"$c\" style=\"background-color:$c;color:$c\"></option>";
	}
} elseif($job=="edit"){
	InitGP(array('nid'));
	$actionvalue="edit";
	!isset($nid) && adminmsg("undefine_action");
	@extract($db->get_one("SELECT * FROM pw_nav WHERE nid=".pwEscape($nid)));
	$style_array=explode("|",$style);
	$style_array[1] && $b_check="checked";
	$style_array[2] && $i_check="checked";
	$style_array[3] && $u_check="checked";
	$pos=="foot" ? $foot_check="checked" : $head_check="checked";
	$target==1 ? $blank_check="checked" : $self_check="checked";
	foreach($colors as $c){
		$ifselect=$c==$style_array[0] ? "selected" : "";
		$color_select.="<option value=\"$c\" style=\"background-color:$c;color:$c\" $ifselect></option>";
	}
} elseif($job=="viewfoot" || $job=="viewhead"){
	if($job=="viewfoot"){
		$pos="foot";
	} else{
		$pos="head";
	}
	$rs	 = $db->query("SELECT * FROM pw_nav WHERE pos=".pwEscape($pos)."ORDER BY view");
	$nav = array();
	while($navdb=$db->fetch_array($rs)){
		$style_array=explode("|",$navdb['style']);
		$style_array[1] && $navdb['title']="<b>".$navdb['title']."</b>";
		$style_array[2] && $navdb['title']="<i>".$navdb['title']."</i>";
		$style_array[3] && $navdb['title']="<u>".$navdb['title']."</u>";
		$style_array[0] && $navdb['title']="<font color=\"$style_array[0]\">".$navdb['title']."</font>";
		$nav[]=$navdb;
	}
}
require PrintHack("admin");exit;

function updatenav(){
	global $db;
	$rs=$db->query("SELECT * FROM pw_nav ORDER BY view");
	$nav_head=$nav_foot=array();
	while ($navdb=$db->fetch_array($rs)){
		if($navdb['pos']=="foot"){
			$nav_foot[]=$navdb;
		} elseif($navdb['pos']=="head"){
			$nav_head[]=$navdb;
		}
	}
	$head = getnavcache($nav_head,'head');
	$foot = getnavcache($nav_foot,'foot');
	$db->update("REPLACE INTO pw_config SET db_name='db_head',db_value=".pwEscape($head));
	$db->update("REPLACE INTO pw_config SET db_name='db_foot',db_value=".pwEscape($foot));

	updatecache_c();
}

function getnavcache($arrayvalue,$type){
	$navigation = '';
	foreach($arrayvalue as $nav){
		$text="";
		$text=$nav['title'];
		$nav_style=explode("|",$nav['style']);
		$nav_style[1] && $text="<b>$text</b>";
		$nav_style[2] && $text="<i>$text</i>";
		$nav_style[3] && $text="<u>$text</u>";
		$nav_style[0] && $text="<font color=\"$nav_style[0]\">$text</font>";
		$target=$nav['target'] ? "target=\"_blank\"" : "";
		$navigation.="<li><a href=\"$nav[link]\" title=\"$nav[alt]\" $target>$text</a></li>";
	}
	return addslashes($navigation);
}
?>