<?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(!isuseradmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');
// Squads löschen
if($_GET['delete']) {
    // squad-members & user-rechte
  $squadID = $_GET['squadID'];
	$ergebnis=safe_query("SELECT userID FROM ".PREFIX."squads_members WHERE squadID='$squadID'");
	while($ds=mysql_fetch_array($ergebnis)) {
		$squads=mysql_num_rows(safe_query("SELECT userID FROM ".PREFIX."squads_members WHERE userID='$ds[userID]'"));
    if($squads<2 AND !issuperadmin($ds[userID])) safe_query("DELETE FROM ".PREFIX."user_groups WHERE userID='$ds[userID]'");
	}
	safe_query("DELETE FROM ".PREFIX."squads_members WHERE squadID='$squadID' ");
	safe_query("DELETE FROM ".PREFIX."squads WHERE squadID='$squadID' ");
	
	$ergebnis=safe_query("SELECT upID FROM ".PREFIX."upcoming WHERE squad='$squadID'");
	while($ds=mysql_fetch_array($ergebnis)) {  // ANNOUNCEMENTS LÖSCHEN
		safe_query("DELETE FROM ".PREFIX."upcoming_announce WHERE upID='$ds[upID]'");
	}
	safe_query("DELETE FROM ".PREFIX."upcoming WHERE squad='$squadID' ");
	
	$ergebnis=safe_query("SELECT cwID FROM ".PREFIX."clanwars WHERE squad='$squadID'");
	while($ds=mysql_fetch_array($ergebnis)) {  // COMMENTS LÖSCHEN
		safe_query("DELETE FROM ".PREFIX."comments WHERE type='cw' AND parentID='$ds[cwID]'");
	}
	safe_query("DELETE FROM ".PREFIX."clanwars WHERE squad='$squadID' ");
}
	
// Sortierung
if($_POST['sortieren']) {
  $sort = $_POST['sort'];
	if(is_array($sort)) {
		foreach($sort as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."squads SET sort='$sorter[1]' WHERE squadID='$sorter[0]' ");
		}
	}
}
	
// Neuen Squad speichern
if($_POST['save']) {
	safe_query("INSERT INTO ".PREFIX."squads ( gamesquad, name, icon, info, sort )
	             values( '".$_POST['gamesquad']."', '".$_POST['name']."', '".$_POST['pic']."', '".$_POST['info']."', '1' )");
  $icon = $_FILES[icon];
	$id=mysql_insert_id();
	$filepath = "../images/squadicons/";
	if ($icon[name] != "") {
        move_uploaded_file($icon[tmp_name], $filepath.$icon[name]);
		@chmod($filepath.$icon[name], 0755);
		$file_ext=strtolower(substr($icon[name], strrpos($icon[name], ".")));
		$file=$id.$file_ext;
	    rename($filepath.$icon[name], $filepath.$file);
		safe_query("UPDATE ".PREFIX."squads SET icon='$file' WHERE squadID='$id' ");
	}
}
	
// Squad updaten
if($_POST['saveedit']) {
	safe_query("UPDATE ".PREFIX."squads SET gamesquad='".$_POST['gamesquad']."', name='".$_POST['name']."', info='".$_POST['info']."' WHERE squadID='".$_POST['squadID']."' ");
	$filepath = "../images/squadicons/";
  $icon = $_FILES[icon];
	if ($icon[name] != "") {
    if(file_exists($filepath.$icon[name])) @unlink($filepath.$icon[name]);
    move_uploaded_file($icon[tmp_name], $filepath.$icon[name]);
		@chmod($filepath.$icon[name], 0755);
		$file = $icon[name];
	} else $file = $_POST['pic'];
	safe_query("UPDATE ".PREFIX."squads SET icon='$file' WHERE squadID='".$_POST['squadID']."' ");
}
	
echo'<h2>squads</h2>';	
// neuen Squad ertstellen
if($_GET['action']=="add") {
	$filepath="../images/squadicons/";
	$folder=opendir($filepath);
	while($file=readdir($folder)) {
		if($file!="." && $file!="..") $files[]=$file;
	}
	closedir($folder);
	if(is_array($files)) {
		foreach($files as $file) {
			$icons.='<input class="input" type="radio" name="pic" value="'.$file.'"> <img src="../images/squadicons/'.$file.'"> ';
		}
	}
		
	echo'<form method="post" action="admincenter.php?site=squads" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Squadicon:</td>
		   <td>'.$icons.'</td>
		 </tr>
		 <tr>
		   <td>Icon-Upload:</td>
		   <td><input name="icon" type="file"></td>
		 </tr>
		 <tr>
		   <td>Squadname:</td>
		   <td><input type="text" name="name" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Squadtype:</td>
		   <td><input class="input" type="radio" name="gamesquad" value="1" checked>gaming squad <input class="input" type="radio" name="gamesquad" value="0">non gaming squad </td>
		 </tr>
		 <tr>
      <td>Squad Info:</td>
      <td><textarea cols="60" rows="5" name="info" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">'.$ds[info].'</textarea></td>
     </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add Squad"></td>
		 </tr>
		 </table>
		 </form>';
}
elseif($_GET['action']=="edit") { // Squad bearbeiten
  $squadID = $_GET['squadID'];
	$filepath="../images/squadicons/";
	$folder=opendir($filepath);
	while($file=readdir($folder)) {
		if($file!="." && $file!="..") $files[]=$file;
	}
	closedir($folder);
	if(is_array($files)) {
		foreach($files as $file) {
			$icons.='<input class="input" type="radio" name="pic" value="'.$file.'"> <img src="../images/squadicons/'.$file.'"> ';
		}
	}
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."squads WHERE squadID='$squadID'");
	$ds=mysql_fetch_array($ergebnis);
	
	if($ds[gamesquad]) $type='<input class="input" type="radio" name="gamesquad" value="1" checked>gaming squad <input class="input" type="radio" name="gamesquad" value="0">non gaming squad';
	else $type='<input class="input" type="radio" name="gamesquad" value="1">gaming squad <input class="input" type="radio" name="gamesquad" value="0" checked>non gaming squad';
	$icons=str_replace('value="'.$ds[icon].'"', 'value="'.$ds[icon].'" checked', $icons);
		
	echo'<form method="post" action="admincenter.php?site=squads" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Squadicon:</td>
		   <td>'.$icons.'</td>
		 </tr>
		 <tr>
		   <td>Icon-Upload:</td>
		   <td><input name="icon" type="file"></td>
		 </tr>
		 <tr>
		   <td>Squadname:</td>
		   <td><input type="text" name="name" value="'.$ds[name].'" size="60" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Squadtype:</td>
		   <td>'.$type.'</td>
		 </tr>
     <tr>
      <td>Squad Info:</td>
      <td><textarea cols="60" rows="5" name="info" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">'.$ds[info].'</textarea></td>
     </tr>
		 <tr>
		   <td><input type="hidden" name="squadID" value="'.$squadID.'"></td>
		   <td><input type="submit" name="saveedit" value="update squad"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=squads&action=add\');return document.MM_returnValue" value="new Squad">
		 <h2>existing squads</h2>';
	echo'<form method="post" action="admincenter.php?site=squads">
		     <table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
		     <tr bgcolor="#CCCCCC">
			   <td class="title">Icon:</td>
			   <td class="title">Squadname:</td>
			   <td class="title">Squadtype:</td>
			   <td class="title">Info:</td>
			   <td class="title">Actions:</td>
			   <td class="title">Sort:</td>
			 </tr>
			 <tr bgcolor="#FFFFFF"><td colspan="7"></td></tr>';
		 
	$squads=safe_query("SELECT * FROM ".PREFIX."squads ORDER BY sort");
	$anzsquads=mysql_num_rows($squads);
	if($anzsquads) {
		$i=1;
		while($db=mysql_fetch_array($squads)) {
			if($db[gamesquad]) $type="gaming squad";
			else $type="non gaming squad";
			echo'<tr bgcolor="#FFFFFF">
			       <td><img src="../images/squadicons/'.$db[icon].'"></td>
				   <td><b>'.$db[name].'</b></td>
				   <td>'.$type.'</td>
				   <td width="120">'.$db[info].'</td>
				   <td>
				   <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=squads&action=edit&squadID='.$db[squadID].'\');return document.MM_returnValue" value="edit">
				   <input type="button" class="button" onClick="MM_confirm(\'really delete this squad?\', \'admincenter.php?site=squads&delete=true&squadID='.$db[squadID].'\')" value="delete"></td>
				   <td><select name="sort[]">';
			for($j=1; $j<=$anzsquads; $j++) {
			    if($db[sort] == $j) echo'<option value="'.$db[squadID].'-'.$j.'" selected>'.$j.'</option>';
				else echo'<option value="'.$db[squadID].'-'.$j.'">'.$j.'</option>';
	        }		
		    echo'  </select>
			       </td>
			     </tr>';
		}
	}
	echo'<tr bgcolor="#FFFFFF"><td colspan="7"></td></tr>
		 <tr bgcolor="#CCCCCC">
	      <td colspan="7" align="right"><input type="submit" name="sortieren" value="sort"></td>
			 </tr>
			 </table></form>';
}
?>	
