Source

Selador / brain / build-editor.php

<?php

/* build-editor.php - Front-end code for the building editor in admin pages
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

// Don't push - this code is crude

if ("buildings" == $module)
  $uploaddir = "/users/selador/trunk/gfx/b/";

if ("research" == $module)
  $uploaddir = "/users/selador/trunk/gfx/r/";

$img_size = "256x256";

$build_types = array (1 => "bergfried", "marktplatz", "garnison", "ratshalle", "zunfthaus", "rohstofflager",
  "waffenschmied", "r�stungsschmiede", "werkstatt", "alchimist", "stadtmauer", "wachturm", "taverne",
  "karavanserei", "musikakademie", "milit�rakademie", "zeughaus", "sch�tzenhof", "stallungen", "burg", "kloster");

$res_types = array (1 => "handel", "fuss", "fernkampf", "kommandant", "reiterei", "belagerung", "burgtruppen", "off-forschung", "def-forschung", "ackerbau", "waldbau", "bergbau", "tagebau", "spio", "gegenspio", "diplo", "mauer", "logistik");

$debug=true;

// Wurde L�schen geklickt?
if (isset ($_REQUEST['delete']))
{
  // Aber erst Sicherheitsabfrage abfragen
  if ($_REQUEST['sure'] == "on")
  {
    $query = "delete from $module where id=".$_REQUEST['id'];
    mysql_query ($query);
    $cmd = "overview";  // Und wieder die �bersicht anzeigen
  }
}
else    // Wird ausgef�hrt, wenn nicht gel�scht werden soll
{
  // Nur wenn uns schon Werte �bergeben wurden, wollen wir auch wirklich speichern, gelle :)
  if (isset ($_REQUEST['submit']))
  {
    print_r ($_REQUEST);
  	// Hier Auswertung des Arrays der �bergebenen Werte

  	// Da mit dem $_REQUEST-Array nicht so sch�n zu arbeiten ist,
  	// kopieren wir die Werte aus dem Formular in was menschlich lesbares.
    if (isset ($_REQUEST['id']))
     	$id = $_REQUEST['id'];
  	$name = $_REQUEST['name'];
  	$oneliner = $_REQUEST['oneliner'];
  	$sdescription = $_REQUEST['kbeschreibung'];
  	$description=$_REQUEST['beschreibung'];
  	$kosten = $_REQUEST['kosten'];
  	$bdep = $_REQUEST['bdep'];
  	$bdeplevel = $_REQUEST['bdeplevel'];
  	$ddep = $_REQUEST['ddep'];
  	$ddeplevel = $_REQUEST['ddeplevel'];
  	$race = $_REQUEST['raceid'];
  	$type = $_REQUEST['type'];
  	$display_order = $_REQUEST['display_order'];


    if (isset ($_FILES))
    {
	  	if ($_FILES['imagefile']['size'] > 0)
	    {
	      $uploadfile = $uploaddir . basename($_FILES['imagefile']['name']);
	      if ($debug)
	      {
	        echo "Hochladen von ";
	        print_r ($_FILES);
	        echo " nach ".$uploadfile."<br>\n";
	      }
	      if (move_uploaded_file($_FILES['imagefile']['tmp_name'], $uploadfile))
	      {
	        chmod ($uploadfile, 0755);
	        if ($debug) echo "<b>Bild wurde gespeichert!</b><br>\n";
	        $img = basename($_FILES['imagefile']['name']);
	      }
	      else
	      {
	        echo "<b>Fehler beim Hochladen!</b><br>\n";
	        die ();
	      }
	      $res = finfo_open(FILEINFO_MIME); /* return mime type ala mimetype extension */
	  		$mime = finfo_file($res, $uploadfile);

			  if (!strstr ("jpeg", $mime))
			    $pic_ext = $id."jpg";
			  if (!strstr ("gif", $mime))
			    $pic_ext = $id."gif";
			  if (!strstr ("png", $mime))
			    $pic_ext = $id."png";

			  finfo_close($res);
	    }
	  }

    if (isset ($id))
    {
      $query = "update $module set ";

      for ($temp = 0; $temp < 3; $temp++)
      {
        if (!isset ($bdep[$temp]))
          $bdep[$temp] = 0;
        if (!isset ($bdeplevel[$temp]))
          $bdeplevel[$temp] = 0;
    		$query .= "bdep".($temp+1)."=".$bdep[$temp].", bdeplevel".($temp+1)."=".$bdeplevel[$temp].", ";
    	}
      for ($temp = 0; $temp < 3; $temp++)
      {
        if (!$ddep[$temp])
          $ddep[$temp] = 0;
        if (!$ddeplevel[$temp])
          $ddeplevel[$temp] = 0;
    		$query .= "ddep".($temp+1)."=".$ddep[$temp].", ddeplevel".($temp+1)."=".$ddeplevel[$temp].", ";
    	}


    	$query .= "name=\"".$name."\", oneliner=\"".$oneliner."\", type=".$type.", race=".$race.", ldescription=\"".$description."\", description=\"".$sdescription."\", display_order=\"".$display_order."\"";

    	if (isset ($img))
    	  $query .= ", pic=\"".$img."\"";

    	foreach ($kosten as $idx => $thisone)
    	{
    		$query .= ", costs".($idx+1)."=".(int)$thisone;
    	}
    	$query .= " where id=".$id;
    }
    else
    {
    	// Hier werden die Daten in die DB gespeichert - genauer gesagt formulieren
    	// wir eine SQL-Anfrage, die die Daten in der Datenbank ablegt
    	$query = "insert into $module (name, oneliner, display_order, race, type, description, ldescription, costs1, costs2, costs3, costs4";

    	foreach ($bdep as $idx => $thisone)
    	{
    		if ($thisone != 0)
    		{
    		  $query .= ", bdep".($idx+1).", bdeplevel".($idx+1);
    		  $query_add .= ", ".$thisone.", ".$bdeplevel[$idx];
    		}
    	}
    	foreach ($ddep as $idx => $thisone)
    	{
    		if ($thisone != 0)
    		{
    		  $query .= ", ddep".($idx+1).", ddeplevel".($idx+1);
    		  $query_add .= ", ".$thisone.", ".$ddeplevel[$idx];
    		}
    	}
    	$query .= ') values ("'.$name.'", "'.$oneliner.'", '.$display_order.', '.$race.', '.$type.', "'.mysql_real_escape_string ($sdescription).'", "'.mysql_real_escape_string ($description).'"';

    	foreach ($kosten as $idx => $thisone)
    	{
    		$query .= ', '.(int)$thisone;
    	}
    	$query .= $query_add.")";
    }

  	// Hier wird die Anfrage an die Datenbank gesendet.
  	echo $query;
  	if (!mysql_query ($query))
  	  echo "<b>Fehler beim Einf&uuml;gen von $query: ".mysql_error()."</b><br>\n";

  	if (!isset ($id))
  	{
  	  $id = mysql_insert_id ();
  	}



	  // rename ($uploadfile, $uploaddir.$pic_new);
  	$cmd = "overview";
  //	echo "<a href=\"gebliste.php\">Zur&uuml;ck zur Liste</a>.";
  }
  else
  {
  	// Wurde uns was �bergeben?
  	if (isset ($id))
  	{
  		$query = "select * from $module where id=".$id;
  		$res = mysql_query ($query);
  		$dbrow = mysql_fetch_array ($res);

  		$name = $dbrow['name'];
  		$description=stripslashes ($dbrow['ldescription']);
  		$sdescription=stripslashes ($dbrow['description']);
  		$oneliner=$dbrow['oneliner'];
  		$kosten[] = $dbrow['costs1'];
  		$kosten[]=$dbrow['costs2'];
  		$kosten[]=$dbrow['costs3'];
  		$kosten[]=$dbrow['costs4'];
  		$race=$dbrow['race'];
  		$type=$dbrow['type'];
  		$pic = $dbrow['pic'];
  		$display_order = $dbrow['display_order'];
  	}
  	else
  	  $race = -1;

  	echo '<form name="editor" action="admin.php" method="POST" enctype="multipart/form-data">';
  	if (isset ($id))
  	  echo '<input name="id" type="hidden" value="'.$id.'">';

  	echo "<input name=\"module\" type=\"hidden\" value=\"$module\">\n";
  	echo '<input name="cmd" type="hidden" value="edit">';
  	echo '<table><tr>';
  	echo "<td>Rasse</td><td colspan=\"3\"><select name=\"raceid\" size=\"1\">\n";

 		$race_query = "select * from races order by raceid";
		$res = mysql_query ($race_query);

		echo "<option value=\"0\">Egal</option>\n";
 		while ($race_row = mysql_fetch_array ($res))
 		{
 			$races[$race_row['raceid']] = $race_row['name_plural'];
 		  echo "<option value=\"".$race_row['raceid']."\"";
 		  if (isset ($race))
 		    if ($race_row['raceid'] == $race)
 		      echo " selected";

 		  echo ">".$race_row['name_plural']."</option>\n";
 		}

 		if ($module == "buildings")
 		{
 			$types = $build_types;
			$filling[0] = "Geb&auml;ude bauen";
			$filling[1] = "Geb&auml;ude";
 		}
 		elseif ($module == "research")
 		{
 		  $types = $res_types;
			$filling[0] = "erforschen";
			$filling[1] = "Forschungs";
 		}

 		echo "</select> <small>Nur diese Rasse kann das ". $filling[0] . "</small>\n</td></tr><tr><td>" . $filling[1] . "typ</td><td colspan=\"3\"><select name=\"type\" size=\"1\">\n";

		unset ($filling);

 		foreach ($types as $idx => $this_type)
 		{
 		  echo "<option value=\"".$idx."\"";
 		  if (isset ($type))
 		    if ($idx == $type)
 		      echo " selected";
 		   echo ">".$this_type."</option>\n";
 		}

    if ($module == "buildings")
    {
    	$filling[0] = "Geb&auml;ude der einzelnen Rassen (die ja anders heissen als die der Menschen)";
    	$filling[1] = "Geb&auml;ude";

  	}
  	elseif  ($module == "research")
  	{
  		$filling[0] = "Forschungen";
  		$filling[1] = "Forschungs";
		}

		echo "</select> <small>F&uuml;r die interne Zuordnung der " . $filling[0] . "</small>\n";
		echo "</td></tr><tr><td>" . $filling[1] ."namen</td>\n";

		unset ($filling);

  	$ress = array ("Holz", "Stein", "Eisen", "Mana");

  	echo "<td align=\"left\" colspan=\"2\"><input name=\"name\" size=\"30\" value=\"".$name."\"></td><td rowspan=\"3\">";

		// picture
		echo "<img width=\"188\" height=\"160\" src=\"../gfx/";

 	  if ($module == "buildings")
 	    echo "b/" . $race . "/w/" . $type . "-1.jpg";
 	  if ($module == "research")
 	  	echo "r/" . $id;

 	  echo "\"/>\n";

  	echo "</td></tr><tr>";
  	echo "<td align=\"left\" valign=\"top\">Einzeiler</td><td colspan=\"2\"><input name=\"oneliner\" size=\"60\" length=\"254\" value=\"".$oneliner."\"></td></tr>\n<tr>";
  	echo "<td align=\"left\" valign=\"top\">Kurzinfo</td>";
  	echo "<td colspan=\"3\"><textarea name=\"kbeschreibung\" cols=\"60\" rows=\"8\">".$sdescription."</textarea></td>";

  	echo "</tr><tr>\n";

  	echo "<td align=\"left\" valign=\"top\">Beschreibung</td>";
  	echo "<td colspan=\"3\"><textarea name=\"beschreibung\" cols=\"60\" rows=\"8\">".$description."</textarea></td>";

  	echo "</tr><tr>\n";
	  echo "<td>Anzeige-Reihenfolge: <input type=\"text\" name=\"display_order\" value=\"".$display_order."\"></td><td>&nbsp;</td>";
  	echo "</tr><tr>\n";
  	echo "<td>Kosten Stufe 1</td><td>Geb&auml;ude-Abh&auml;ngigkeiten</td><td colspan=\"2\">Forschungs-Abh&auml;ngigkeiten</td></tr>\n";

  	echo "<tr><td align=\"left\" valign=\"top\"><table>\n";
  	// Es ist einfacher, eine Schleife um den Array mit den Ressourcen kreisen zu lassen,
  	// als die vier Zeilen von Hand zu schreiben. Diese �bung lassen wir f�r den Leser ;)
  	foreach ($ress as $idx => $thisone)
  	{
  		echo "<tr><td>&nbsp;".$thisone."</td>";
  		echo "<td><input name=\"kosten[]\" size=\"5\" value=\"".$kosten[$idx]."\"></td></tr>\n";
  	}

  	echo "</table></td><td align=\"left\" valign=\"top\" ><table>\n";
  	for ($temp = 1; $temp < 4; $temp++)
  	{
  		$bdep = $dbrow['bdep'.$temp];
  		$bdeplevel = $dbrow['bdeplevel'.$temp];

  		echo "<tr><td><select name=\"bdep[]\" size=\"1\">";
  		if ($bdep == 0)
  		  echo "<option selected value=\"0\">Nix</option>\n";
  		else
  		  echo "<option value=\"0\">Nix</option>\n";

  		$query = "select id, name, race from buildings";
  		$gebres = mysql_query ($query);
  		while ($gebrow = mysql_fetch_array ($gebres))
  		{
  			if ($bdep == $gebrow['id'])
  			  echo "<option value=\"".$gebrow['id']."\" selected>".$gebrow['name']." (".$races[$gebrow['race']].") </option>\n";
  			else
  			  echo "<option value=\"".$gebrow['id']."\">".$gebrow['name']." (".$races[$gebrow['race']].") </option>\n";
  		}
  		echo "</select>&nbsp;\n";

  	  echo "<input name=\"bdeplevel[]\" size=\"3\" value=\"".$bdeplevel."\"></td></tr>\n";
  	}
    echo "</table></td><td align=\"left\" valign=\"top\" >";

    echo "<table>\n";
  	for ($temp = 1; $temp < 4; $temp++)
  	{
  		$ddep = $dbrow['ddep'.$temp];
  		$ddeplevel = $dbrow['ddeplevel'.$temp];

  		echo "<tr><td><select name=\"ddep[]\" size=\"1\">";
  		if ($ddep == 0)
  		  echo "<option selected value=\"0\">Nix</option>\n";
  		else
  		  echo "<option value=\"0\">Nix</option>\n";

  		$query = "select id, name, race from research";
  		$gebres = mysql_query ($query);
  		while ($gebrow = mysql_fetch_array ($gebres))
  		{
  			if ($ddep == $gebrow['id'])
  			  echo "<option value=\"".$gebrow['id']."\" selected>".$gebrow['name']." (".$races[$gebrow['race']].") </option>\n";
  			else
  			  echo "<option value=\"".$gebrow['id']."\">".$gebrow['name']." (".$races[$gebrow['race']].") </option>\n";
  		}
  		echo "</select>&nbsp;\n";

  	  echo "<input name=\"ddeplevel[]\" size=\"3\" value=\"".$ddeplevel."\"></td></tr>\n";
  	}
    echo "</table>";

    echo "</td>\n";
  	echo "<td align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Speichern\">";  // Der Submit-Button sendet das Formular ab
    echo "&nbsp;<input type=\"submit\" name=\"delete\" value=\"L&ouml;schen\">\n";  // Der Submit-Button sendet das Formular ab
    echo "<br><input type=\"checkbox\" name=\"sure\"> Wirklich l&ouml;schen?</td><td>&nbsp;</td></tr>\n";
  	echo "</table></form>\n";
  }
}
?>