<?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','icon'),'P');
	empty($title) && adminmsg("nav_empty_title");
	empty($link) && adminmsg("nav_empty_link");
	$view	= (int)$view;
	$style	= $color."|".$b."|".$i."|".$u;
	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,
				'icon'		=> $icon
		)));
	} 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,
					'icon'	=> $icon
				))
			. " 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;
	$query = $db->query("SELECT * FROM pw_nav ORDER BY view");
	$nav_head = $nav_foot = array();
	while ($navdb=$db->fetch_array($query)){
		list($navdb['color'],$navdb['b'],$navdb['i'],$navdb['u']) = explode('|',$navdb['style']);
		if($navdb['pos']=="foot"){
			$nav_foot[] = array('icon'=>$navdb['icon'],'link'=>$navdb['link'],'html'=>getHtmlText($navdb),'image'=>getHtmlImage($navdb));;
		} elseif($navdb['pos']=="head"){
			$nav_head[] = array('icon'=>$navdb['icon'],'link'=>$navdb['link'],'html'=>getHtmlText($navdb),'image'=>getHtmlImage($navdb));;
		}
	}

	$head = getnavcache($nav_head);
	$foot = getnavcache($nav_foot);

	$headnav = serialize($nav_head);
	$footnav = serialize($nav_foot);
	$db->update("REPLACE INTO pw_config SET db_name='db_head',db_value=".pwEscape($head,false));
	$db->update("REPLACE INTO pw_config SET db_name='db_foot',db_value=".pwEscape($foot,false));
	$db->update("REPLACE INTO pw_config SET db_name='db_headnav',db_value=".pwEscape($headnav,false));
	$db->update("REPLACE INTO pw_config SET db_name='db_footnav',db_value=".pwEscape($footnav,false));

	updatecache_c();
}

function getnavcache($array){
	$navigation = '';
	!is_array($array) && $array =array();
	foreach($array as $nav){
		$navigation .= "<li>".$nav['html']."</li>";
	}
	return $navigation;
}

function getHtmlText($nav) {
	$html = $nav['title'];
	$nav['b'] && $html = "<b>$html</b>";
	$nav['i'] && $html = "<i>$html</i>";
	$nav['u'] && $html = "<u>$html</u>";
	$nav['color'] && $html = "<font color=\"$nav[color]\">$html</font>";
	$target = $nav['target'] ? "target=\"_blank\"" : "";
	$html = "<a href=\"$nav[link]\" title=\"$nav[alt]\" $target>$html</a>";
	return $html;
}
function getHtmlImage($nav) {
	$html = '';
	if ($nav['icon']) {
		$target = $nav['target'] ? "target=\"_blank\"" : "";
		$html = "<a href=\"$nav[link]\" title=\"$nav[alt]\" $target><img src=\"$nav[icon]\" /></a>";
	}
	return $html;
}
?>