<?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(!ispageadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_GET['delete']) {
  $partnerID = $_GET['partnerID'];
	
  safe_query(" DELETE FROM ".PREFIX."partners WHERE partnerID='$partnerID' ");
	$filepath = "../images/partners/";
	if(file_exists($filepath.$partnerID.'.jpg')) @unlink($filepath.$partnerID.'.jpg');
	if(file_exists($filepath.$partnerID.'.gif')) @unlink($filepath.$partnerID.'.gif');
}
elseif($_POST['sortieren']) {
  $sort = $_POST['sort'];
	foreach($sort as $sortstring) {
	    $sorter=explode("-", $sortstring);
		safe_query("UPDATE ".PREFIX."partners SET sort='$sorter[1]' WHERE partnerID='$sorter[0]' ");
	}
}
elseif($_POST['save']) {
  $name = $_POST['name'];
  $url = $_POST['url'];
  $banner = $_FILES[banner];
  
    safe_query("INSERT INTO ".PREFIX."partners ( name, url, sort )
	             values( '$name', '$url', '1' )");
	$id=mysql_insert_id();			 
				 
	$filepath = "../images/partners/";
	if ($banner[name] != "") {
        move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
		@chmod($filepath.$banner[name], 0755);
		$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
		$file=$id.$file_ext;
		if(file_exists($filepath.$file)) @unlink($filepath.$file);
		rename($filepath.$banner[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."partners SET banner='$file' WHERE partnerID='$id' ");
	}
}
elseif($_POST['saveedit']) {
  $name = $_POST['name'];
  $url = $_POST['url'];
  $banner = $_FILES[banner];

	$partnerID = $_POST['partnerID'];
	$id=$partnerID;
	$filepath = "../images/partners/";
	if ($banner[name] != "") {
        move_uploaded_file($banner[tmp_name], $filepath.$banner[name]);
		@chmod($filepath.$banner[name], 0755);
		$file_ext=strtolower(substr($banner[name], strrpos($banner[name], ".")));
		$file=$id.$file_ext;
    if(file_exists($filepath.$file)) @unlink($filepath.$file);
		rename($filepath.$banner[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."partners SET banner='$file' WHERE partnerID='$id' ");
	}
	safe_query("UPDATE ".PREFIX."partners SET name='$name', url='$url' WHERE partnerID='$partnerID' ");
}


echo'<h2>partners</h2>';
if($_GET['action']=="add") {
	echo'<form method="post" action="admincenter.php?site=partners" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Partner-Name:</td>
		   <td><input type="text" name="name" size="30" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Banner:</td>
		   <td><input name="banner" type="file"> max 88px/31px</td>
		 </tr>
		 <tr>
		   <td>Partner-Homepage:</td>
		   <td><input type="text" name="url" size="30" value="http://" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Partner"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") {

  $partnerID = $_GET['partnerID'];
  $ergebnis=safe_query("SELECT * FROM ".PREFIX."partners WHERE partnerID='$partnerID'");
	$ds=mysql_fetch_array($ergebnis);
	
    echo'<form method="post" action="admincenter.php?site=partners" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Partner-Name:</td>
		   <td><input type="text" name="name" size="30" value="'.$ds[name].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Banner:</td>
		   <td><input name="banner" type="file"> max 88px/31px</td>
		 </tr>
		 <tr>
		   <td>Partner-Homepage:</td>
		   <td><input type="text" name="url" size="30" value="'.$ds[url].'" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="partnerID" value="'.$partnerID.'"></td>
		   <td><input type="submit" name="saveedit" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=partners&action=add\');return document.MM_returnValue" value="new Partner">';	
	echo'<form method="post" action="admincenter.php?site=partners">
		     <table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
		     <tr bgcolor="#CCCCCC">
			   <td class="title" width="30">banner:</td>
			   <td class="title">Partner-Name:</td>
			   <td class="title">Homepage:</td>
			   <td class="title" colspan="2">Actions:</td>
			   <td class="title">Sort:</td>
			 </tr>
			 <tr bgcolor="#FFFFFF"><td colspan="6"></td></tr>';
			 
	$partners=safe_query("SELECT * FROM ".PREFIX."partners ORDER BY sort");
	$anzpartners=safe_query("SELECT count(partnerID) FROM ".PREFIX."partners");
	$anzpartners=mysql_result($anzpartners, 0);
	
	while($db=mysql_fetch_array($partners)) {
		echo'<tr bgcolor="#FFFFFF">
		       <td><img src="../images/partners/'.$db[banner].'"></td>
			   <td>'.$db[name].'</td>
			   <td>'.$db[url].'</td>
			   <td><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=partners&action=edit&partnerID='.$db[partnerID].'\');return document.MM_returnValue" value="edit"></td>
			   <td><input type="button" class="button" onClick="MM_confirm(\'really delete this partner?\', \'admincenter.php?site=partners&delete=true&partnerID='.$db[partnerID].'\')" value="delete"></td>
			   <td>
			   <select name="sort[]">';
		for($j=1; $j<=$anzpartners; $j++) {
		    if($db[sort] == $j) echo'<option value="'.$db[partnerID].'-'.$j.'" selected>'.$j.'</option>';
			else echo'<option value="'.$db[partnerID].'-'.$j.'">'.$j.'</option>';
        }		
	    echo'  </select>
		       </td>
		     </tr>';
	}
	echo'<tr bgcolor="#FFFFFF"><td colspan="6"></td></tr>
	     <tr bgcolor="#CCCCCC">
         <td colspan="6" align="right"><input type="submit" name="sortieren" value="sort partners"></td>
		 </tr>
		 </table></form>';
}
?>	
