<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if(!isnewsadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_POST['save']) {
  $pic = $_FILES[pic];
	safe_query("INSERT INTO ".PREFIX."news_rubrics ( rubric ) values( '".$_POST['name']."' ) ");
    $id=mysql_insert_id();
		
    $filepath = "../images/news-rubrics/";
	if ($pic[name] != "") {
        move_uploaded_file($pic[tmp_name], $filepath.$pic[name]);
		@chmod($filepath.$pic[name], 0755);
		$file_ext=strtolower(substr($pic[name], strrpos($pic[name], ".")));
		$file=$id.$file_ext;
	    rename($filepath.$pic[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."news_rubrics SET pic='$file' WHERE rubricID='$id' ");
	}
}
elseif($_POST['saveedit']) {
  $pic = $_FILES[pic];
	safe_query("UPDATE ".PREFIX."news_rubrics SET rubric='".$_POST['name']."' WHERE rubricID='".$_POST['rubricID']."'");

  $id=$_POST['rubricID'];
	$filepath = "../images/news-rubrics/";
    $filepath = "../images/news-rubrics/";
	if ($pic[name] != "") {
    move_uploaded_file($pic[tmp_name], $filepath.$pic[name]);
		@chmod($filepath.$pic[name], 0755);
		$file_ext=strtolower(substr($pic[name], strrpos($pic[name], ".")));
		$file=$id.$file_ext;
    if(file_exists($filepath.$file)) @unlink($filepath.$file);
    rename($filepath.$pic[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."news_rubrics SET pic='$file' WHERE rubricID='$id' ");
	}
}
elseif($_GET['delete']) {
  $rubricID = $_GET['rubricID'];
  $filepath = "../images/news-rubrics/";
	safe_query("DELETE FROM ".PREFIX."news_rubrics WHERE rubricID='$rubricID'");
	if(file_exists($filepath.$rubricID.'.jpg')) @unlink($filepath.$rubricID.'.jpg');
	if(file_exists($filepath.$rubricID.'.gif')) @unlink($filepath.$rubricID.'.gif');
}

echo'<h2>news-rubrics</h2>';
if($_GET['action']=="add") {
    echo'<form method="post" action="admincenter.php?site=rubrics" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Rubric-Name:</td>
		   <td><input type="text" name="name" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Rubric-Picture:</td>
		   <td><input name="pic" type="file"></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Rubric"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {
  $rubricID = $_GET['rubricID'];
  $ergebnis=safe_query("SELECT * FROM ".PREFIX."news_rubrics WHERE rubricID='$rubricID'");
	$ds=mysql_fetch_array($ergebnis);
	
	echo'<form method="post" action="admincenter.php?site=rubrics" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Rubric-Name:</td>
		   <td><input type="text" name="name" value="'.$ds[rubric].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Rubric-Picture:</td>
		   <td><img src="../images/news-rubrics/'.$ds[pic].'"></td>
		 </tr>
		 <tr>
		   <td>Rubric-Picture:</td>
		   <td><input name="pic" type="file"></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="rubricID" value="'.$ds[rubricID].'"></td>
		   <td><input type="submit" name="saveedit" value="update rubric"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=rubrics&action=add\');return document.MM_returnValue" value="new Rubric"><br><br>';

	$ergebnis=safe_query("SELECT * FROM ".PREFIX."news_rubrics ORDER BY rubric");
	echo'<table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
   		<tr bgcolor="#CCCCCC">
   		<td class="title" align="center">Rubric:</td>
 		<td class="title" align="center">Picture:</td>
 		<td class="title" align="center" colspan="2">Actions:</td>
   		</tr>
		<tr bgcolor="#FFFFFF"><td colspan="4"></td></tr>';
		
	while($ds=mysql_fetch_array($ergebnis)) {
    	echo'<tr bgcolor="#FFFFFF">
	       		<td align="center"><b>'.$ds[rubric].'</b></td>
		   		<td align="center"><img src="../images/news-rubrics/'.$ds[pic].'"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=rubrics&action=edit&rubricID='.$ds[rubricID].'\');return document.MM_returnValue" value="edit"></td>
		   		<td align="center"><input type="button" class="button" onClick="MM_confirm(\'really delete this rubric?\', \'admincenter.php?site=rubrics&delete=true&rubricID='.$ds[rubricID].'\')" value="delete"></td>
		 	</tr>';
	}
	echo'</table>';
}	
?>	
