<?php
/*===================================================+
|| # HoloCMS - Website and Content Management System
|+===================================================+
|| # Copyright © 2008 Meth0d. All rights reserved.
|| # http://www.meth0d.org
|+===================================================+
|| # HoloCMS is provided "as is" and comes without
|| # warrenty of any kind. HoloCMS is free software!
|+===================================================+
|| # Parts written by IzTheWiz of RaGEZONE forums
|| # Parts written by HandyAndy160 of RaGEZONE forums
|| # Sisija is NOT a fag
|+===================================================*/

require_once('../core.php');
if($hkzone !== true){ header("Location: index.php?throwBack=true"); exit; }
if(!session_is_registered(acp)){ header("Location: index.php?p=login"); exit; }

$pagename = "Manage the rare value guide";

if($do == "clear") {
	$column = $_GET['column'];
	if($column == "1")
	{
		mysql_query("UPDATE rarevalue SET `valuestate` = '0'");
		header("location:index.php?p=rare_manage");
	}
	if($column == "2")
	{
		mysql_query("UPDATE rarevalue SET `releasestate` = '1'");
		header("location:index.php?p=rare_manage");
	}
	else
	{
		header("location:index.php?p=rare_manage");
	}
}

if($do == "delete" && is_numeric($key)){

	$check = mysql_query("SELECT id FROM rarevalue WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		mysql_query("DELETE FROM rarevalue WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
		$msg = "Rare was removed from guide.";
	} else {
		$msg = "Error deleting rare; Can't find selected rare.";
	}

} elseif($do == "edit" && is_numeric($key)){

	$check = mysql_query("SELECT * FROM rarevalue WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){
		$rare = mysql_fetch_assoc($check);
		$editor_mode = true;
	} else {
		$msg = "Error editing rare; Can't find selected rare.";
	}

} elseif($do == "save" && is_numeric($key)){

	$check = mysql_query("SELECT id FROM rarevalue WHERE id = '".$key."' LIMIT 1") or die(mysql_error());
	$exists = mysql_num_rows($check);

	if($exists > 0){

		$id = $key;

		$name = addslashes($_POST['name']);
		$img = addslashes($_POST['img']);
		$value = addslashes($_POST['value']);
		$valuestate = addslashes($_POST['valuestate']);
		$releasestate = addslashes($_POST['releasestate']);
		$category = addslashes($_POST['category']);

		if($category != 1 && $category != 2 && $category != 3 && $category != 4) { $category = 1; }
		if($valuestate != 0 && $valuestate != 1 && $valuestate != 2) { $valuestate = 0; }
		if($releasestate != 0 && $releasestate != 1) { $releasestate = 0; }
		if(!is_numeric($value)) { $value = 1; }

		mysql_query("UPDATE rarevalue SET name = '".$name."', img = '".$img."', value = '".$value."', valuestate = '".$valuestate."', releasestate = '".$releasestate."', category = '".$category."', last_update = '".$date."?".$time."' WHERE id = '".$id."' LIMIT 1") or die(mysql_error());

		$msg = "Rare updated successfully.";
		$editor_mode = false;

	} else {

		$msg = "Error editing rare; Can't find selected rare.";

	}

} elseif($do == "bounce"){

	$msg = "Rare added successfully.";

}

@include('subheader.php');
@include('header.php');
?>
<table cellpadding='0' cellspacing='8' width='100%' id='tablewrap'>
<tr> <td width='22%' valign='top' id='leftblock'>
 <div>
 <!-- LEFT CONTEXT SENSITIVE MENU -->
<?php @include('sitemenu.php'); ?>
 <!-- / LEFT CONTEXT SENSITIVE MENU -->
 </div>
 </td>
 <td width='78%' valign='top' id='rightblock'>
 <div><!-- RIGHT CONTENT BLOCK -->

<?php if($editor_mode !== true){ ?>
<?php if(isset($msg)){ ?><p><strong><?php echo $msg; ?></strong></p><?php } ?>

<form action='index.php?p=rare_manage&do=save' method='post' name='theAdminForm' id='theAdminForm'>
 <div class='tableborder'>
 <div class='tableheaderalt'>Current Rares in guide [<a href='index.php?p=rare_manage&do=clear&column=1'>Reset Value States</a>] [<a href='index.php?p=rare_manage&do=clear&column=2'>Reset Release States</a>]</div>
 <table cellpadding='4' cellspacing='0' width='100%'>
 <tr>
  <td class='tablesubheader' width='1%' align='center'>ID</td>
  <td class='tablesubheader' width='28%'>Rare Name</td>
  <td class='tablesubheader' width='10%' align='center'>Image Filename</td>
  <td class='tablesubheader' width='10%' align='center'>Value</td>
  <td class='tablesubheader' width='10%' align='center'>Edit</td>
  <td class='tablesubheader' width='12%' align='center'>Delete</td>
 </tr>
<?php
$get_rares = mysql_query("SELECT id,name,img,value,valuestate,releasestate,category FROM rarevalue ORDER BY id DESC") or die(mysql_error());

while($row = mysql_fetch_assoc($get_rares)){

if($row['valuestate'] == 1) { $vs="Currently up in value"; }
else if($row['valuestate'] == 2) { $vs="Currently down in value"; }
else if($row['valuestate'] == 0) { $vs="No recent change in value"; }

if($row['releasestate'] == 0) { $rs=", new item"; }
else { $rs=""; }

$info = $vs.$rs.$hc.".";

	printf(" <tr>
  <td class='tablerow1' align='center'>%s</td>
  <td class='tablerow2'><strong>%s</strong><div class='desctext'>%s</div></td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'>%s</td>
  <td class='tablerow2' align='center'><a href='index.php?p=rare_manage&do=edit&key=%s'><img src='./images/edit.gif' alt='Edit'></a></td>
  <td class='tablerow2' align='center'><a href='index.php?p=rare_manage&do=delete&key=%s'><img src='./images/delete.gif' alt='Delete'></a></td>
</tr>", $row['id'], htmlspecialchars(stripslashes($row['name'])), htmlspecialchars($info), $row['img'], "<b>".$row['value']."</b>", $row['id'], $row['id']);
}
?>

 </table>
 <div class='tablefooter' align='center'><div class='fauxbutton-wrapper'><span class='fauxbutton'><a href='index.php?p=rare_add'>Create New Rare Value</a></span></div></div>
</div>
<?php } else { ?>
<form action='index.php?p=rare_manage&do=save&key=<?php echo $rare['id']; ?>' method='post' name='theAdminForm' id='theAdminForm'>
<div class='tableborder'>
<div class='tableheaderalt'>Editing Rare: <b>(<?php echo $rare['name']; ?>, ID: <?php echo $rare['id']; ?>)</b></div>

<table width='100%' cellspacing='0' cellpadding='5' align='center' border='0'>
<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Rare Name</b><div class='graytext'>The correct name of the rare.</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='name' value="<?php echo $rare['name']; ?>" size='30' class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Image Filename</b><div class='graytext'>The filename of the thumbnail image (in rareimg folder).</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='img' value="<?php echo $rare['img']; ?>" size='30' class='textinput'>.gif</td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Value</b><div class='graytext'>The starting value of this rare in HCs.<br>Eg. 2 or 2.5</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='value' value="<?php echo $rare['value']; ?>" size='5' class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Value State</b><div class='graytext'>Whether this rare has gone up or down in price.<br>0 = no change, 1 = gone up, 2 = gone down</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='valuestate' value="<?php echo $rare['valuestate']; ?>" size='1' maxlength='1'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Release State</b><div class='graytext'>Whether this rare is newly added or not.<br>0 = new, 1 = normal</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='releasestate' value="<?php echo $rare['releasestate']; ?>" size='1' maxlength='1'class='textinput'></td>
</tr>

<tr>
<td class='tablerow1'  width='40%'  valign='middle'><b>Category</b><div class='graytext'>What type of rare this is.<br>1 = normal rare, 2 = xmas, 3 = h'ween, 4 = HC</div></td>
<td class='tablerow2'  width='60%'  valign='middle'><input type='text' name='category' value="<?php echo $rare['category']; ?>" size='1' maxlength='1'class='textinput'></td>
</tr>

<tr>
<tr><td align='center' class='tablesubheader' colspan='2' ><input type='submit' value='Update Rare' class='realbutton' accesskey='s'></td></tr>
</form></table></div><br />
<?php } ?>
	 </div><!-- / RIGHT CONTENT BLOCK -->
	 </td></tr>
</table>
</div><!-- / OUTERDIV -->
<div align='center'><br />
<?php
$mtime = explode(' ', microtime());
$totaltime = $mtime[0] + $mtime[1] - $starttime;
printf('Time: %.3f', $totaltime);
?>
</div>