<?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($_POST['save']) {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

  if(!isclanwaradmin($userID) || !isnewsadmin($userID)) die('no access!');

  $month = $_POST['month'];
  $day = $_POST['day'];
  $year = $_POST['year'];
  $squad = $_POST['squad'];
  $award = mysql_escape_string($_POST['award']);
	$homepage = mysql_escape_string($_POST['homepage']);
  $rang = mysql_escape_string($_POST['rang']);
  $info = mysql_escape_string($_POST['info']);

	$date=mktime(0,0,0,$month,$day,$year);
	safe_query("INSERT INTO ".PREFIX."awards ( date, squadID, award, homepage, rang, info ) VALUES( '$date', '$squad', '$award', '$homepage', '$rang', '$info' ) ");
	header("Location: index.php?site=awards");
}
elseif($_POST['saveedit']) {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");

  if(!isclanwaradmin($userID) || !isnewsadmin($userID)) die('no access!');
  $awardID = $_POST['awardID'];
  $month = $_POST['month'];
  $day = $_POST['day'];
  $year = $_POST['year'];
  $squad = $_POST['squad'];
  $award = $_POST['award'];
	$homepage = $_POST['homepage'];
  $rang = $_POST['rang'];
  $info = $_POST['info'];

	$date=mktime(0,0,0,$month,$day,$year);
	safe_query("UPDATE ".PREFIX."awards SET date='$date', squadID='$squad', award='$award', homepage='$homepage', rang='$rang', info='$info' WHERE awardID='$awardID'");
	header("Location: index.php?site=awards");
}
elseif($_GET['delete']) {
  if(!isclanwaradmin($userID) || !isnewsadmin($userID)) die('no access!');

  $awardID = $_GET['awardID'];
	safe_query("DELETE FROM ".PREFIX."awards WHERE awardID='$awardID'");
	header("Location: index.php?site=awards");
}

eval ("\$title_awards = \"".gettemplate("title_awards")."\";");
echo $title_awards;

if($_GET['action']=="new") {
    if(isclanwaradmin($userID) || isnewsadmin($userID)) {
		for($i=1; $i<32; $i++) {
		    if($i==date("d", time())) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", time())) $month.='<option value="'.$i.'" selected>'.date("M", time()).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", time())) $year.='<option value="'.$i.'" selected>'.date("Y", time()).'</option>';
			else $year.='<option value="'.$i.'">'.$i.'</option>';
		}
		$squads=getgamesquads();

		$bg1=BG_1;
		eval ("\$awards_new = \"".gettemplate("awards_new")."\";");
	    echo $awards_new;
	}
	else redirect('index.php?site=awards', 'no access!');
}
elseif($_GET['action']=="edit") {

    $awardID = $_GET['awardID'];
    if(isclanwaradmin($userID) || isnewsadmin($userID)) {
	    $ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."awards WHERE awardID='$awardID'"));
	    for($i=1; $i<32; $i++) {
		    if($i==date("d", $ds[date])) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", $ds[date])) $month.='<option value="'.$i.'" selected>'.date("M", $ds[date]).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", $ds[date])) $year.='<option selected>'.$i.'</option>';
			else $year.='<option>'.$i.'</option>';
		}
		$squads=getgamesquads();
		$squads=str_replace('value="'.$ds[squadID].'"', 'value="'.$ds[squadID].'" selected', $squads);
		$award=$ds[award];
    $homepage=$ds[homepage];
    $rang=$ds[rang];
    $info=$ds[info];
		$bg1=BG_1;
		eval ("\$awards_edit = \"".gettemplate("awards_edit")."\";");
	    echo $awards_edit;
	}
	else redirect('index.php?site=awards', 'no access!');
}
elseif($_GET['action']=="showsquad") {
  $squadID = $_GET['squadID'];
  $page = $_GET['page'];
  $sort = $_GET['page'];
  $type = $_GET['type'];

	if(isclanwaradmin($userID) || isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=awards&action=new\');return document.MM_returnValue" value="new award"><br><br>';
	$alle=safe_query("SELECT awardID FROM ".PREFIX."awards WHERE squadID='$squadID'");
	$gesamt = mysql_num_rows($alle);
	$pages=1;
	if(!isset($page)) $page = 1;
	if(!isset($sort)) $sort = "date";
	if(!isset($type)) $type = "DESC";

	$max=$maxawards;

    for ($n=$max; $n<=$gesamt; $n+=$max) {
	    if($gesamt>$n) $pages++;
	}

	if($pages>1) $page_link = makepagelink("index.php?site=awards&action=showsquad&squadID=$squadID&sort=$sort&type=$type", $page, $pages);

	if ($page == "1") {
        $ergebnis = safe_query("SELECT * FROM ".PREFIX."awards WHERE squadID='$squadID' ORDER BY $sort $type LIMIT 0,$max");
	    if($type=="DESC") $n=$gesamt;
		else $n=1;
	}
	else {
	    $start=$page*$max-$max;
	    $ergebnis = safe_query("SELECT * FROM ".PREFIX."awards WHERE squadID='$squadID' ORDER BY $sort $type LIMIT $start,$max");
	    if($type=="DESC") $n = ($gesamt)-$page*$max+$max;
		else $n = ($gesamt+1)-$page*$max+$max;
	}
	if($gesamt) {
	    if($type=="ASC")
		    echo'<a href="index.php?site=awards&action=showsquad&squadID='.$squadID.'&page='.$page.'&sort='.$sort.'&type=DESC">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
		else
		    echo'<a href="index.php?site=awards&action=showsquad&squadID='.$squadID.'&page='.$page.'&sort='.$sort.'&type=ASC">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';

		echo $page_link;
		echo'<br><br>';
		$headdate='<a class="titlelink" href="index.php?site=awards&action=showsquad&squadID='.$squadID.'&page='.$page.'&sort=date&type='.$type.'">Date:</a>';
		$headsquad='Squad:';

		eval ("\$awards_head = \"".gettemplate("awards_head")."\";");
	    echo $awards_head;
		$n=1;
		while($ds=mysql_fetch_array($ergebnis)) {
			if($n%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			$date=date("d.m.Y", $ds[date]);
			$squad=getsquadname($ds[squadID]);
			$award=cleartext($ds[award]);
      $homepage=$ds[homepage];
      $rang=$ds[rang];

			if(isclanwaradmin($userID) || isnewsadmin($userID))
	            $adminaction='<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=awards&action=edit&awardID='.$ds[awardID].'\');return document.MM_returnValue" value="edit">
							  <input type="button" class="button" onClick="MM_confirm(\'really delete this award?\',\'awards.php?delete=true&awardID='.$ds[awardID].'\')" value="delete">';

			eval ("\$awards_content = \"".gettemplate("awards_content")."\";");
	        echo $awards_content;
			unset($result);
			$n++;
		}
  		eval ("\$awards_foot = \"".gettemplate("awards_foot")."\";");
	    echo $awards_foot;
    } else echo'no entries';

} elseif($_GET['action']=="details") {
         $awardID = $_GET['awardID'];
         $ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."awards WHERE awardID='$awardID'"));

         $rang=$ds[rang];
         if($rang=='') $rang="-";
         $award=cleartext($ds[award]);
         if($award=='') $award="-";
         $squad=getsquadname($ds[squadID]);
         $squadID=$ds[squadID];
         $date=date("d.m.Y", $ds[date]);
         $info=htmloutput($ds[info]);
         if($info=='') $info="-";
         $homepage = '<a href="http://'.str_replace('http://','',$ds[homepage]).'" target="_blank">'.$ds[homepage].'</a>';

        $bg1=BG_1;
        $bg2=BG_2;
        $bg3=BG_3;
        $bg4=BG_4;
		eval ("\$awards_info = \"".gettemplate("awards_info")."\";");
	    echo $awards_info;

} else {
  $page = $_GET['page'];
  $sort = $_GET['page'];
  $type = $_GET['type'];
	if(isclanwaradmin($userID) || isnewsadmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=awards&action=new\');return document.MM_returnValue" value="new award"><br><br>';
	$alle=safe_query("SELECT awardID FROM ".PREFIX."awards");
	$gesamt = mysql_num_rows($alle);
	$pages=1;
	if(!isset($page)) $page = 1;
	if(!isset($sort)) $sort = "date";
	if(!isset($type)) $type = "DESC";

	$max=30;

    for ($n=$max; $n<=$gesamt; $n+=$max) {
	    if($gesamt>$n) $pages++;
	}

	if($pages>1) $page_link = makepagelink("index.php?site=awards&sort=$sort&type=$type", $page, $pages);

	if ($page == "1") {
        $ergebnis = safe_query("SELECT * FROM ".PREFIX."awards ORDER BY $sort $type LIMIT 0,$max");
	    if($type=="DESC") $n=$gesamt;
		else $n=1;
	}
	else {
	    $start=$page*$max-$max;
	    $ergebnis = safe_query("SELECT * FROM ".PREFIX."awards ORDER BY $sort $type LIMIT $start,$max");
	    if($type=="DESC") $n = ($gesamt)-$page*$max+$max;
		else $n = ($gesamt+1)-$page*$max+$max;
	}
	if($gesamt) {
	    if($type=="ASC")
		    echo'<a href="index.php?site=awards&page='.$page.'&sort='.$sort.'&type=DESC">Sort:</a> <img src="images/icons/asc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';
		else
		    echo'<a href="index.php?site=awards&page='.$page.'&sort='.$sort.'&type=ASC">Sort:</a> <img src="images/icons/desc.gif" width="9" height="7" border="0">&nbsp;&nbsp;&nbsp;';

		echo $page_link;
		echo'<br><br>';
		$headdate='<a class="titlelink" href="index.php?site=awards&page='.$page.'&sort=date&type='.$type.'">Date:</a>';
		$headsquad='<a class="titlelink" href="index.php?site=awards&page='.$page.'&sort=squadID&type='.$type.'">Squad:</a>';

		eval ("\$awards_head = \"".gettemplate("awards_head")."\";");
	    echo $awards_head;
		$n=1;
		while($ds=mysql_fetch_array($ergebnis)) {
			if($n%2) {
				$bg1=BG_1;
				$bg2=BG_2;
			}
			else {
				$bg1=BG_3;
				$bg2=BG_4;
			}
			$date=date("d.m.Y", $ds[date]);
			$squad='<a href="index.php?site=awards&action=showsquad&squadID='.$ds[squadID].'&page='.$page.'&sort='.$sort.'&type='.$type.'">'.getsquadname($ds[squadID]).'</a>';
			$award=cleartext($ds[award]);
			$homepage=$ds[homepage];
			$rang=$ds[rang];

			if(isclanwaradmin($userID) || isnewsadmin($userID))
	            $adminaction='<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=awards&action=edit&awardID='.$ds[awardID].'\');return document.MM_returnValue" value="edit">
							  <input type="button" class="button" onClick="MM_confirm(\'really delete this award?\',\'index.php?site=awards&delete=true&awardID='.$ds[awardID].'\')" value="delete">';

			eval ("\$awards_content = \"".gettemplate("awards_content")."\";");
	        echo $awards_content;
			$n++;
		}
 		eval ("\$awards_foot = \"".gettemplate("awards_foot")."\";");
	    echo $awards_foot;
	}
	else echo'no entries';
}


?>