<?
$page = "admin_templates.php";
include "admin_header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }




if($task == "addtemplate") {
echo $head;
echo "
<h2>$admin_templates1</h2>
$admin_templates2
<br><br>
<form action='admin_templates.php' method='POST'>

<b>$admin_templates3</b><br>
<input type='text' class='text' size='50' name='name' maxlength='30'>
<br><br>

<b>$admin_templates4</b> $admin_templates5<br>
<input type='text' class='text' size='50' name='image' value='http://' maxlength='250'>
<br>
$admin_templates6
<br><br>

<b>$admin_templates9</b>
<textarea name='html' rows='30' cols='60' style='width: 100%; font-family: \"Courier New\", courier, arial, tahoma;'></textarea>
$admin_templates10
<br><br>

<input type='submit' name='submit' class='button' value='$admin_templates11'>
<input type='submit' name='submit' class='button' value='$admin_templates12'>
<input type='hidden' name='task' value='doadd'>
</form>
";

echo $foot;
}




if($task == "doadd") {
$name = $_POST['name'];
$image = $_POST['image'];
$html = $_POST['html'];
$submit = $_POST['submit'];

if($submit == "$admin_templates12") { header("Location: admin_templates.php"); exit(); }

//CAPITALIZE ALL VARS AND CONTAINERS
$html = variable_replace($html);


//CHANGE <, >, and "
$html = str_replace("<", "&lt;", $html);
$html = str_replace(">", "&gt;", $html);
$html = str_replace("\"", "&quot;", $html);

if($name == "") { 
 echo $head; 
 echo "<h2>$admin_templates7</h2>
 $admin_templates21<br><br><input type='button' class='button' value='$admin_templates8' onClick='history.go(-1);'>";
 echo $foot; 
 exit(); 
}

mysql_query("INSERT INTO bhost_templates (name, image, html) VALUES ('$name', '$image', '$html')");
header("Location: admin_templates.php"); exit();
}







if($task == "edit") {
$t_id = $_GET['t_id'];
if($t_id == "") { exit(); }
$thistemplate = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_templates WHERE t_id='$t_id'"));

echo $head;
echo "
<h2>$admin_templates13</h2>
$admin_templates2
<br><br>
<form action='admin_templates.php' method='POST'>

<b>$admin_templates3</b><br>
<input type='text' class='text' size='50' name='name' value='$thistemplate[name]' maxlength='25'>
<br><br>

<b>$admin_templates4</b> $admin_templates5<br>
<input type='text' class='text' size='50' name='image' value='$thistemplate[image]' maxlength='250'>
<br>
$admin_templates6
<br><br>

<b>$admin_templates9</b>
<textarea name='html' rows='30' cols='60' style='width: 100%; font-family: \"Courier New\", courier, arial, tahoma;'>$thistemplate[html]</textarea>
$admin_templates10
<br><br>

<input type='submit' name='submit' class='button' value='$admin_templates14'>
<input type='submit' name='submit' class='button' value='$admin_templates15'>
<input type='submit' name='submit' class='button' value='$admin_templates12'>
<input type='hidden' name='task' value='doedit'>
<input type='hidden' name='t_id' value='$t_id'>
</form>
";

echo $foot;
}





if($task == "doedit") {
$name = $_POST['name'];
$image = $_POST['image'];
$html = $_POST['html'];
$submit = $_POST['submit'];
$t_id = $_POST['t_id'];

if($submit == "$admin_templates12") { header("Location: admin_templates.php"); exit(); }

if($name == "") { 
 echo $head; 
 echo "<h2>$admin_templates7</h2>
 $admin_templates21<br><br><input type='button' class='button' value='$admin_templates8' onClick='history.go(-1);'>";
 echo $foot; 
 exit(); 
}

//CAPITALIZE ALL VARS AND CONTAINERS
$html = variable_replace($html);

//CHANGE <, >, and "
$html = str_replace("<", "&lt;", $html);
$html = str_replace(">", "&gt;", $html);
$html = str_replace("\"", "&quot;", $html);

if($submit == "$admin_templates15") {
if(mysql_num_rows(mysql_query("SELECT t_id FROM bhost_templates")) == 1) { 
 echo $head;
 echo "<h2>$admin_templates7</h2>
 $admin_templates22<br><br><input type='button' class='button' value='$admin_templates8' onClick='history.go(-1);'>";
 echo $foot; 
 exit(); 
}
echo $head;
echo "
<h2>$admin_templates16</h2>
$admin_templates17
<br><br>
<form action='admin_templates.php' method='POST'>
<input type='submit' name='submit' class='button' value='$admin_templates15'>
<input type='submit' name='submit' class='button' value='$admin_templates12'>
<input type='hidden' name='task' value='dodelete'>
<input type='hidden' name='t_id' value='$t_id'>
</form>
";
echo $foot;
exit();
}

mysql_query("UPDATE bhost_templates SET name='$name', image='$image', html='$html' WHERE t_id='$t_id'");
header("Location: admin_templates.php"); 
exit();
}










if($task == "dodelete") {
$submit = $_POST['submit'];
$t_id = $_POST['t_id'];
if($submit == "$admin_templates12") {
header("Location: admin_templates.php"); 
exit();
}
if($submit == "$admin_templates15") {
mysql_query("DELETE FROM bhost_templates WHERE t_id='$t_id'");
header("Location: admin_templates.php"); 
exit();
}
}












if($task == "main") {
echo $head;
echo "
<h2>$admin_templates18</h2>
$admin_templates19<br>
<br>

<form action='admin_templates.php' method='POST'>
<input type='submit' class='button' value='$admin_templates11'>
<input type='hidden' name='task' value='addtemplate'>
</form>
";

$templates = mysql_query("SELECT * FROM bhost_templates");
if(mysql_num_rows($templates) == 0) {
echo "
<br>
<table cellpadding='0' cellspacing='0'>
<tr><td style='padding: 7px; background: #FFFFFF; border: 1px solid #AAAAAA;'>
<b>$admin_templates20</b>
</td></tr>
</table>
";
} else {
echo "

<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td valign='top'>
";
}

while($template = mysql_fetch_assoc($templates)) {

if($template[image] != "" AND $template[image] != "http://") { 
$image = "<a href='admin_templates.php?task=edit&t_id=$template[t_id]'><img src='$template[image]' border='0'></a>"; 
} else {
$image = "<a href='admin_templates.php?task=edit&t_id=$template[t_id]'><img src='../images/noimage.gif' border='0'></a>"; 
}

echo "
<div class='float'><br>
<b><a href='admin_templates.php?task=edit&t_id=$template[t_id]'>$template[name]</a></b>
<div style='padding: 5px; background: #FFFFFF; border: 1px solid #666666; margin-top: 5px; margin-right: 15px;' valign='top'>$image</div>
</div>
";
}

if(mysql_num_rows($templates) == 0) {
echo "
</td>
</tr>
</table>";
}



echo $foot;
}
?>