<?php

if ( !eregi("admin.php", $PHP_SELF) ) { 
	die ("Access Denied"); 
}
if ( $xoopsUser->isAdmin($xoopsModule->mid()) ) {
function RankForumAdmin() {
    	global $xoopsDB, $xoopsModule;
    	xoops_cp_header();
    	//$xoopsModule->printAdminMenu();
	//echo "<br />";
    	OpenTable();
   	echo "<h4 style='text-align:left;'>"._AM_RANKSSETTINGS."</h4>
	<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
	<table width='100%' border='0' cellpadding='4' cellspacing='1'>
	<tr class='bg3' align='center'>
	<td align='left'><b>"._AM_TITLE."</b></td>
	<td><b>"._AM_MINPOST."</b></td>
	<td><b>"._AM_MAXPOST."</b></td>
	<td><b>"._AM_IMAGE."</b></td>
	<td><b>"._AM_SPERANK."</b></td>
	<td><b>"._AM_ACTION."</b></td></tr>";

    	$result = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("ranks")." ORDER BY rank_id");
    	while($rank = $xoopsDB->fetchArray($result)) {
    		echo "<tr class='bg1' align='center'>
		<td align='left'>".$rank['rank_title']."</td>
		<td>".$rank['rank_min']."</td>
		<td>".$rank['rank_max']."</td>
		<td>";
		if($rank['rank_image']){
			echo "<img src='".XOOPS_URL."/images/ranks/".$rank['rank_image']."'></td>";
		}else{
			echo "&nbsp;";
		}
		if ($rank['rank_special'] ==1) {
			echo"<td>"._AM_ON."</td>";
		} else {
			echo"<td>"._AM_OFF."</td>";
		}
		echo"<td><a href='admin.php?fct=userrank&amp;op=RankForumEdit&amp;rank_id=".$rank['rank_id']."'>"._AM_EDIT."</a> | <a href='admin.php?fct=userrank&amp;op=RankForumDel&amp;rank_id=".$rank['rank_id']."&amp;ok=0'>"._AM_DEL."</a></td></tr>";
    	}
    	echo "</table></td></tr></table>
    	<br /><br />
    	<h4 style='text-align:left;'>"._AM_ADDNEWRANK."</h4>
    	<form action='admin.php' method='post'>
	<table border='0' cellpadding='0' cellspacing='0' valign='top' width='100%'><tr><td class='bg2'>
    	<table width='100%' border='0' cellpadding='4' cellspacing='1'><tr valign='top'><td class='bg3'><b>
    	"._AM_RANKTITLE."&nbsp;</b></td><td class='bg1'><input type='text' name='rank_title' size='31' maxlength='50' /></td></tr><tr valign='top'><td class='bg3'><b>
    	"._AM_MINPOST."&nbsp;</b></td><td class='bg1'><input type='text' name='rank_min' size='4' maxsize='5' /></td></tr><tr valign='top'><td class='bg3'><b>
    	"._AM_MAXPOST."&nbsp;</b></td><td class='bg1'><input type='text' name='rank_max' size='4' maxsize='5' /></td></tr><tr valign='top'><td class='bg3'><b>
    	"._AM_IMAGE."&nbsp;</b></td><td class='bg1'><input type='text' name='rank_image' size='50' maxlength='255' /><br />";
	$rankimgdir = XOOPS_URL."/images/ranks/";
	printf(_AM_VALIDUNDER,$rankimgdir);
	echo "</td></tr>";
    	echo "<tr valign='top'><td class='bg3'><b>"._AM_SPECIAL."&nbsp;</b></td><td class='bg1'><input type='checkbox' name='rank_special' value='1' /><br />";
	echo _AM_SPECIALCAN."</td></tr>";
	echo "<tr valign='top'><td class='bg3'><b>&nbsp;</td><td class='bg1'><input type='submit' value='"._AM_ADD."' /></td></tr>";
    	echo "</table></td></tr></table>
    	<input type='hidden' name='op' value='RankForumAdd' />
    	<input type='hidden' name='fct' value='userrank' />
    	</form>\n";
    	CloseTable();
    	xoops_cp_footer();
}

function RankForumAdd($rank_title,$rank_min,$rank_max,$rank_image,$rank_special) {
	global $xoopsDB;
	$myts =& MyTextSanitizer::getInstance();
	$rank_title = $myts->makeTboxData4Save($rank_title);
	$rank_image = $myts->makeTboxData4Save($rank_image);
	$newid = $xoopsDB->genId($xoopsDB->prefix("ranks")."_rank_id_seq");
    	if ($rank_special == 1) {
    		$xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("ranks")." (rank_id, rank_title, rank_min, rank_max, rank_special, rank_image) VALUES ($newid, '$rank_title', -1 ,-1 ,1,'$rank_image')");
    	} else {
    		$xoopsDB->query("INSERT INTO ".$xoopsDB->prefix("ranks")." (rank_id, rank_title, rank_min, rank_max, rank_special, rank_image) VALUES ($newid, '$rank_title', '$rank_min' ,'$rank_max' , 0, '$rank_image')");
    	}
    	redirect_header("admin.php?fct=userrank&op=RankForumAdmin",1,_AM_DBUPDATED);
}

function RankForumEdit($rank_id) {
    	global $xoopsDB, $xoopsModule;
	$myts =& MyTextSanitizer::getInstance();
    	xoops_cp_header();
	//$xoopsModule->printAdminMenu();
	//echo "<br />";
   	$result = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("ranks")." WHERE rank_id=".$rank_id."");
    	$rank = $xoopsDB->fetchArray($result);
	$rank['rank_title'] = $myts->makeTboxData4Edit($rank['rank_title']);
	$rank['rank_image'] = $myts->makeTboxData4Edit($rank['rank_image']);
    	OpenTable();
    	echo "
    	<h4 style='text-align:left;'>"._AM_EDITRANK."</h4>
    	<form action='admin.php' method='post'>
	<table border='0' cellpadding='0' cellspacing='0' align='center' valign='top' width='100%'><tr><td class='bg2'>
    	<input type='hidden' name='rank_id' value='".$rank['rank_id']."' />
    	<table width='100%' border='0' cellpadding='4' cellspacing='1'><tr valign='top' align='left'><td class='bg3'>
    	<b>"._AM_RANKTITLE."</b></td><td class='bg1'><input type='text' name='rank_title' size='31' value='".$rank['rank_title']."' maxlength='50' /></td></tr>";
	echo "<tr valign='top' align='left'><td class='bg3'><b>"._AM_IMAGE."</b></td><td class='bg1'><input type='text' name='rank_image' size='50' maxlength='255' value='".$rank['rank_image']."' /><br />";
	$rankimgdir = XOOPS_URL."/images/ranks/";
	printf(_AM_VALIDUNDER,$rankimgdir);
	echo "</td></tr>";
    	echo "<tr valign='top' align='left'><td class='bg3'><b>"._AM_MINPOST."</b></td><td class='bg1'><input type='text' name='rank_min' size='4' value='".$rank['rank_min']."' /></td></tr>
    	<tr valign='top' align='left'><td class='bg3'><b>"._AM_MAXPOST."</b></td><td class='bg1'><input type='text' name='rank_max' size='4' value='".$rank['rank_max']."' /></td></tr>
    	<tr valign='top' align='left'><td class='bg3'><b>"._AM_SPECIAL."</b></td><td class='bg1'><input type='checkbox' name='rank_special' value='1'";
	if ($rank['rank_special'] == 1){
		echo " checked='checked'";
	}
	echo " /></td></tr>";
    	echo "
    	<tr valign='top' align='left'><td class='bg3'>&nbsp;</td><td class='bg1'><input type='submit' value='"._AM_SAVECHANGE."' /></td></tr></table></td></tr></table>
    	<input type='hidden' name='op' value='RankForumSave' />
    	<input type='hidden' name='fct' value='userrank' />
    	</form></div>";
	CloseTable();
    	xoops_cp_footer();
}

function RankForumSave($rank_id, $rank_title, $rank_min, $rank_max, $rank_image, $rank_special) {
	global $xoopsDB;
	$myts =& MyTextSanitizer::getInstance();
	$rank_title = $myts->makeTboxData4Save($rank_title);
	$rank_image = $myts->makeTboxData4Save($rank_image);
	if($rank_special != 1){
		$sql = "UPDATE ".$xoopsDB->prefix("ranks")." SET rank_title='$rank_title',rank_min='$rank_min',rank_max='$rank_max',rank_special=0,rank_image='$rank_image' WHERE rank_id=".$rank_id."";
	}else{
		$sql = "UPDATE ".$xoopsDB->prefix("ranks")." SET rank_title='$rank_title',rank_min=-1,rank_max=-1,rank_special=1,rank_image='$rank_image' WHERE rank_id=".$rank_id."";
	}
    	$xoopsDB->query($sql);
    	redirect_header("admin.php?fct=userrank&op=RankForumAdmin",1,_AM_DBUPDATED);
}

function RankForumDel($rank_id, $ok=0) {
	global $xoopsDB, $xoopsModule;
    	if($ok==1) {
		$xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("ranks")." WHERE rank_id=".$rank_id."");
		redirect_header("admin.php?fct=userrank&op=ForumAdmin",1,_AM_DBUPDATED);
		exit();
    	} else {
		xoops_cp_header();
		//$xoopsModule->printAdminMenu();
		//echo "<br />";
		OpenTable();
		echo "<div><br />";
		echo "<h4 style='color:#ff0000'>"._AM_WAYSYWTDTR."</h4><br />";
		echo "<table><tr><td>\n";
		echo myTextForm("admin.php?fct=userrank&amp;op=RankForumDel&amp;rank_id=$rank_id&amp;ok=1", _AM_YES);
		echo "</td><td>\n";
		echo myTextForm("admin.php?fct=userrank&amp;op=RankForumAdmin", _AM_NO);
		echo "</td></tr></table></div>\n";
		CloseTable();
    	}
	xoops_cp_footer();	
}
} else {
    	echo "Access Denied";
}
?>