Location: PHPKode > projects > SWG Resource Tracker > distro/upload/functions.php
<?php
/*
 *	SWG Resource Tracker
 *	Copyright (C) 2004 Enigma
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 2
 *	of the License, or (at your option) any later version.
 *	
 *	This program 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 General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

	function printAttributeBox($key, $name, $array)
	{
		if ($key == "")
		{
			$key = "attribute";
		}
		
		if ($name == "")
		{
			$name = "attribute";
		}
		
		return "<select name=\"{$name}\">
									<option value=\"0\"" . (($array[$key] == 0) ? " selected" : "") . ">Effectiveness</option>
									<option value=\"1\"" . (($array[$key] == 1) ? " selected" : "") . ">Durability</option>
									<option value=\"2\"" . (($array[$key] == 2) ? " selected" : "") . ">Dathomir</option>
									<option value=\"3\"" . (($array[$key] == 3) ? " selected" : "") . ">Endor</option>
									<option value=\"4\"" . (($array[$key] == 4) ? " selected" : "") . ">Lok</option>
									<option value=\"5\"" . (($array[$key] == 5) ? " selected" : "") . ">Naboo</option>
									<option value=\"6\"" . (($array[$key] == 6) ? " selected" : "") . ">Rori</option>
									<option value=\"7\"" . (($array[$key] == 7) ? " selected" : "") . ">Talus</option>
									<option value=\"8\"" . (($array[$key] == 8) ? " selected" : "") . ">Tatooine</option>
									<option value=\"9\"" . (($array[$key] == 9) ? " selected" : "") . ">Yavin 4</option>
								</select>";
	}
	
	function printPlanetBox($key, $name, $array)
	{
		$planets = array("Corellia", "Dantooine", "Dathomir", "Endor", "Lok", "Naboo", "Rori", "Talus", "Tatooine", "Yavin 4");
		
		if ($key == "")
		{
			$key = "planet";
		}
		
		if ($name == "")
		{
			$name = "planet";
		}
		
		
		return "<select name=\"{$name}\">
									<option value=\"-1\"> </option>
									<option value=\"0\"" . (($array[$key] == 0) ? " selected" : "") . ">Corellia</option>
									<option value=\"1\"" . (($array[$key] == 1) ? " selected" : "") . ">Dantooine</option>
									<option value=\"2\"" . (($array[$key] == 2) ? " selected" : "") . ">Dathomir</option>
									<option value=\"3\"" . (($array[$key] == 3) ? " selected" : "") . ">Endor</option>
									<option value=\"4\"" . (($array[$key] == 4) ? " selected" : "") . ">Lok</option>
									<option value=\"5\"" . (($array[$key] == 5) ? " selected" : "") . ">Naboo</option>
									<option value=\"6\"" . (($array[$key] == 6) ? " selected" : "") . ">Rori</option>
									<option value=\"7\"" . (($array[$key] == 7) ? " selected" : "") . ">Talus</option>
									<option value=\"8\"" . (($array[$key] == 8) ? " selected" : "") . ">Tatooine</option>
									<option value=\"9\"" . (($array[$key] == 9) ? " selected" : "") . ">Yavin 4</option>
								</select>";
	}
	
	function printUpdatingPlanetBox($key, $name, $array)
	{
		$planets = array("Corellia", "Dantooine", "Dathomir", "Endor", "Lok", "Naboo", "Rori", "Talus", "Tatooine", "Yavin 4");
		
		if ($key == "")
		{
			$key = "planet";
		}
		
		if ($name == "")
		{
			$name = "planet";
		}
		
		if (!isset($array[$key]))
			$array[$key] = -1;
		
		
		return "<select name=\"{$name}\" onChange=\"form.submit()\">
									<option value=\"-1\"> </option>
									<option value=\"0\"" . (($array[$key] == 0 && isset($array[$key])) ? " selected" : "") . ">Corellia</option>
									<option value=\"1\"" . (($array[$key] == 1) ? " selected" : "") . ">Dantooine</option>
									<option value=\"2\"" . (($array[$key] == 2) ? " selected" : "") . ">Dathomir</option>
									<option value=\"3\"" . (($array[$key] == 3) ? " selected" : "") . ">Endor</option>
									<option value=\"4\"" . (($array[$key] == 4) ? " selected" : "") . ">Lok</option>
									<option value=\"5\"" . (($array[$key] == 5) ? " selected" : "") . ">Naboo</option>
									<option value=\"6\"" . (($array[$key] == 6) ? " selected" : "") . ">Rori</option>
									<option value=\"7\"" . (($array[$key] == 7) ? " selected" : "") . ">Talus</option>
									<option value=\"8\"" . (($array[$key] == 8) ? " selected" : "") . ">Tatooine</option>
									<option value=\"9\"" . (($array[$key] == 9) ? " selected" : "") . ">Yavin 4</option>
								</select>";
	}
	
	function printUpdatingDayBox($key, $name, $array)
	{
		if ($key == "")
		{
			$key = "age";
		}
		
		if ($name == "")
		{
			$name = "age";
		}
		
		if (!isset($array[$key]))
			$array[$key] = -1;
			
		
		$value = "<select name=\"{$name}\" onChange=\"form.submit()\">
									<option value=\"-1\"> </option>
									<option value=\"0\"" . (($array[$key] == 0) ? " selected" : "") . ">Today</option>
									<option value=\"1\"" . (($array[$key] == 1) ? " selected" : "") . ">Yesterday</option>\n";
		
		for ($i = 2; $i < AUTO_TRIM; $i++)
		{
			$value .= 				"<option value=\"{$i}\"" . (($array[$key] == $i) ? " selected" : "") . ">{$i} days ago</option>\n";
		}
		
		
		return  $value . "</select>";
	}
	
	function printDayBox($key, $name, $array)
	{
		if ($key == "")
		{
			$key = "age";
		}
		
		if ($name == "")
		{
			$name = "age";
		}
		
		if (!isset($array[$key]))
			$array[$key] = -1;
			
		
		$value = "<select name=\"{$name}\">
									<option value=\"-1\"> </option>
									<option value=\"0\"" . (($array[$key] == 0) ? " selected" : "") . ">Today</option>
									<option value=\"1\"" . (($array[$key] == 1) ? " selected" : "") . ">Yesterday</option>\n";
		
		for ($i = 2; $i < AUTO_TRIM; $i++)
		{
			$value .= 				"<option value=\"{$i}\"" . (($array[$key] == $i) ? " selected" : "") . ">{$i} days ago</option>\n";
		}
		
		
		return  $value . "</select>";
	}
	
	function printStatBoxes($css, $key, $name, $array)
	{
		$keys = array(	"ER" => $key . "ER",
						"CR" => $key . "CR",
						"CD" => $key . "CD",
						"DR" => $key . "DR",
						"FL" => $key . "FL",
						"HR" => $key . "HR",
						"MA" => $key . "MA",
						"PE" => $key . "PE",
						"OQ" => $key . "OQ",
						"SR" => $key . "SR",
						"UT" => $key . "UT");
		return "<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}ER\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['ER']])) ? $array[$keys['ER']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}CR\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['CR']])) ? $array[$keys['CR']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}CD\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['CD']])) ? $array[$keys['CD']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}DR\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['DR']])) ? $array[$keys['DR']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}FL\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['FL']])) ? $array[$keys['FL']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}HR\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['HR']])) ? $array[$keys['HR']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}MA\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['MA']])) ? $array[$keys['MA']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}PE\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['PE']])) ? $array[$keys['PE']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}OQ\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['OQ']])) ? $array[$keys['OQ']] : "0") . "\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"{$name}SR\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['SR']])) ? $array[$keys['SR']] : "0") . "\" /></td>
				<td class=\"{$css[1]}\"><input type=\"text\" name=\"{$name}UT\" size=\"4\" maxlength=\"4\" value=\"" . ((isset($array[$keys['UT']])) ? $array[$keys['UT']] : "0") . "\" /></td>";
	}
	
	function printStatBoxesArray($css, $key, $name)
	{
		$keys = array(	"ER" => "ER" . $key,
						"CR" => "CR" . $key,
						"CD" => "CD" . $key,
						"DR" => "DR" . $key,
						"FL" => "FL" . $key,
						"HR" => "HR" . $key,
						"MA" => "MA" . $key,
						"PE" => "PE" . $key,
						"OQ" => "OQ" . $key,
						"SR" => "SR" . $key,
						"UT" => "UT" . $key);
		return "<td class=\"{$css[0]}\"><input type=\"text\" name=\"ER{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"CR{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"CD{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"DR{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"FL{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"HR{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"MA{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"PE{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"OQ{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[0]}\"><input type=\"text\" name=\"SR{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>
				<td class=\"{$css[1]}\"><input type=\"text\" name=\"UT{$name}\" size=\"4\" maxlength=\"4\" value=\"0\" /></td>";
	}
	
	function getStatArray($key, &$array)
	{
		$keys = array(	"ER" => $key . "ER",
						"CR" => $key . "CR",
						"CD" => $key . "CD",
						"DR" => $key . "DR",
						"FL" => $key . "FL",
						"HR" => $key . "HR",
						"MA" => $key . "MA",
						"PE" => $key . "PE",
						"OQ" => $key . "OQ",
						"SR" => $key . "SR",
						"UT" => $key . "UT");
		
		$stats = array(	"ER" => intval($array[$keys['ER']]),
						"CR" => intval($array[$keys['CR']]),
						"CD" => intval($array[$keys['CD']]),
						"DR" => intval($array[$keys['DR']]),
						"FL" => intval($array[$keys['FL']]),
						"HR" => intval($array[$keys['HR']]),
						"MA" => intval($array[$keys['MA']]),
						"PE" => intval($array[$keys['PE']]),
						"OQ" => intval($array[$keys['OQ']]),
						"SR" => intval($array[$keys['SR']]),
						"UT" => intval($array[$keys['UT']]));
		return $stats;
	}
	
	function getStatPercentageString(&$array)
	{
		foreach ($array as $key => $value)
		{
			if ($value == 0)
			{
				unset($array[$key]);
			}
			else
			{
				$array[$key] .= "% {$key}";
			}
		}
		
		return implode(", ", $array);
	}
	
	function arrayEquals(&$a1, &$a2)
	{
		if (count($a1) != count($a2))
		{
			return FALSE;
		}
		
		$equals = TRUE;
		
		foreach ($a1 as $key => $value)
		{
			if (isset($a2[$key]) && $a2[$key] == $value)
			{
				/* do nothing */
			}
			else
			{
				$equals = FALSE;
			}
		}
		
		return $equals;
	}
	
	function printUpdatingCategoryBox($name, $array)
	{
		global $DB;
		
		$query = "SELECT `long_name`, `id` FROM `categories` ORDER BY `long_name` ASC";
		$DB->query($query);
		
		$results = $DB->fetchAll();
		
		$s = "<select name=\"{$name}\" onChange=\"form.submit()\">
				<option value=\"-1\"> </option>\n";
		
		for ($i = 0; $i < count($results); $i++)
		{
			if ($results[$i]['id'] == $array[$name])
			{
				$sel = " selected";
			}
			else
			{
				$sel = "";
			}
			
			$s .= "<option value=\"{$results[$i]['id']}\"$sel>{$results[$i]['long_name']}</option>\n";
		}
		
		$s .= "</select>\n";
		
		return $s;
	}
	
	function printCategoryBox($name, $array)
	{
		global $DB;
		
		$query = "SELECT `long_name`, `id` FROM `categories` ORDER BY `long_name` ASC";
		$DB->query($query);
		
		$results = $DB->fetchAll();
		
		$s = "<select name=\"{$name}\">
				<option value=\"-1\"> </option>\n";
		
		for ($i = 0; $i < count($results); $i++)
		{
			if ($results[$i]['id'] == $array[$name])
			{
				$sel = " selected";
			}
			else
			{
				$sel = "";
			}
			
			$s .= "<option value=\"{$results[$i]['id']}\"$sel>{$results[$i]['long_name']}</option>\n";
		}
		
		$s .= "</select>\n";
		
		return $s;
	}
	
	function printLastTierCategoryBox($name, $array)
	{
		global $DB;
		
		$query = "SELECT `long_name`, `id` FROM `categories` WHERE `has_child`=0 ORDER BY `long_name` ASC";
		$DB->query($query);
		
		$results = $DB->fetchAll();
		
		$s = "<select name=\"{$name}\">
				<option value=\"-1\"> </option>\n";
		
		for ($i = 0; $i < count($results); $i++)
		{
			if ($results[$i]['id'] == $array[$name])
			{
				$sel = " selected";
			}
			else
			{
				$sel = "";
			}
			
			$s .= "<option value=\"{$results[$i]['id']}\"$sel>{$results[$i]['long_name']}</option>\n";
		}
		
		$s .= "</select>\n";
		
		return $s;
	}
	
	function printUpdatingComboBoxes($name, &$array)
	{
		global $DB;
		
		$key = "{$name}1";
		$c = 1;
		$p = 0;
		
		if (isset($array[$key]) == false)
		{
			$array[$key] = -1;
		}
		
		
		while (isset($array[$key]) && $p != -1)
		{
			$selected = false;
			
			$query = "SELECT `long_name`, `id` FROM `categories` WHERE `level`={$c} AND `parent`={$p} ORDER BY `long_name` ASC";
			$DB->query($query);
		
			$results = $DB->fetchAll();
		
			$s .= "<select name=\"{$key}\" onChange=\"form.submit()\">
					<option value=\"-1\"> </option>\n";
			
			for ($i = 0; $i < count($results); $i++)
			{
				if ($results[$i]['id'] == $array[$key])
				{
					$sel = " selected";
					$selected = true;
				}
				else
				{
					$sel = "";
				}
				
				$s .= "<option value=\"{$results[$i]['id']}\"{$sel}>{$results[$i]['long_name']}</option>\n";
			}
			
			$s .= "</select>\n";
			
			if (!$selected)
			{
				$array[$key] = -1;
			}
			
			$c++;
			$p = $array[$key];
			$key = "{$name}{$c}";
		}
		
		$key = "{$name}" . ($c - 1);
		$query = "SELECT `long_name`, `id` FROM `categories` WHERE `level`={$c} AND `parent`={$p} ORDER BY `long_name` ASC";
		$DB->query($query);
	
		$results = $DB->fetchAll();
		
		if ($array[$key] > -1 && count($results) > 0)
		{
			$s .= "<select name=\"{$name}{$c}\" onChange=\"form.submit()\">
					<option value=\"-1\"> </option>\n";
			
			for ($i = 0; $i < count($results); $i++)
			{
				$s .= "<option value=\"{$results[$i]['id']}\">{$results[$i]['long_name']}</option>\n";
			}
			
			$s .= "</select>\n";
		}
			
		return $s;
	}
	
	function htmlFormat($str)
	{
		$str = htmlentities($str);
		$str = nl2br($str);
		
		return $str;
	}
	
	function createTempTable()
	{
		global $DB;
		
		$query = "CREATE TEMPORARY TABLE `categories`  (
					  `id` tinyint(3) unsigned NOT NULL,
					  `table` tinyint(1) unsigned NOT NULL default '0',
					  `category` tinyint(3) unsigned NOT NULL default '0',
					  `name` varchar(255) NOT NULL default '',
					  `longname` varchar(255) NOT NULL default '',
					  KEY `category` (`category`, `table`)
					) TYPE=MyISAM";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 1 AS `table`, `category`, `name`, `longname` FROM `category1`";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 2 AS `table`, `category`, `name`, `longname` FROM `category2`";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 3 AS `table`, `category`, `name`, `longname` FROM `category3`";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 4 AS `table`, `category`, `name`, `longname` FROM `category4`";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 5 AS `table`, `category`, `name`, `longname` FROM `category5`";
		$DB->query($query);
		
		$query = "INSERT INTO `categories` (`id`, `table`, `category`, `name`, `longname`) SELECT `id`, 6 AS `table`, `category`, `name`, `longname` FROM `category6`";
		$DB->query($query);
	}
	
	function colorize($stat, $cap = 1000, $eff = 0)
	{
		if ($cap <= 0)
		{
			$cap = 1000;
		}
		
		if ($eff <= 0)
		{
			$eff = $stat;
		}
		
		if ($cap == 100)
		{
			$stat = round($stat, 2);
			if ($stat > 70)
			{
				return "<span class=\"green\">$stat</span>";
			}
			else
			{
				return "{$stat}";
			}
		}
		
		if ($cap == 100)
			return $stat;
		
		if ($eff > ($cap - 101))
		{
			return "<a title=\"{$eff}\" class=\"red\">$stat</a>";
		}
		
		if ($eff > ($cap - 201))
		{
			return "<a title=\"{$eff}\" class=\"blue\">$stat</a>";
		}
		
		if ($stat == 0)
		{
			return "-";
		}
		
		return $stat;
	}
	
	function printArrayURL($array, $prefix = "")
	{
		$s = "";
		
		if (count($array) == 0 || !is_array($array))
		{
			return $s;
		}
		
		foreach ($array as $key=>$value)
		{
			$s .= "&amp;{$key}=" . urlencode($value);
		}
		
		return $s;
	}
	
	function error($msg)
	{
		echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
	<head>
		<title>Resource Tracker - Error</title>
		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
		<link href=\"tracker.css\" rel=\"stylesheet\" type=\"text/css\">
	</head>
	<body>
	<p>An error has been encountered while loading the page.  The full text is displayed below:</p>
	<p><textarea cols=100 rows=30>" . htmlentities($msg, ENT_QUOTES) . "\n\n\$_REQUEST dump:\n";
	var_dump($_REQUEST);
	echo "\n\n" . "</textarea></p>
	</body>
</html>";
		die();
	}
	
	function ifempty($val1, $val2)
	{
		if ($val1 == "")
		{
			return $val2;
		}
		else
		{
			return $val1;
		}
	}
	
	function ifelse($bool, $val1, $val2)
	{
		if ($bool)
		{
			return $val1;
		}
		else
		{
			return $val2;
		}
	}
	
	function getStats()
	{
		return array("ER", "CR", "CD", "DR", "FL", "HR", "MA", "PE", "OQ", "SR", "UT");
	}
	
	function bsort($a, $b)
	{
		if ($a['id'] == $b['id'])
			return 0;
		
		if ($a['id'] < $b['id'])
			return -1;
		
		return 1;
	}
	
	function minusLastPathComponent($path)
	{
		$path = explode("/", $path);
		unset($path[count($path) - 1]);
		
		$path = implode("/", $path);
		
		return $path;
	}
	
	function minusFirstPathComponent($path)
	{
		$path = explode("/", $path);
		unset($path[0]);
		
		$path = implode("/", $path);
		
		return $path;
	}
	
	function pathCompare($p1, $p2)
	{
		if ($p1 == $p2)
		{
			return 0;
		}
		
		if ($p1 == "")
		{
			return -1;
		}
		else if ($p2 == "")
		{
			return 1;
		}
		
		if (strpos($p2, $p1) === false)
		{
			return 1;
		}
		
		return -1;
	}
	
	function addNextPathComponent($path, $components)
	{
		if ($path != "")
		{
			$components = str_replace($path . "/", "", $components);
			$path = explode("/", $path);
		}
		else
		{
			$path = array();
		}
		
		$components = explode("/", $components);
		
		$path[] = $components[0];
		
		return implode("/", $path);
	}
	
	/***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************
	 ***************************************************************************************************************/
	 
	function getQuery($query, $maxes = "")
	{
		if (!is_array($maxes) && CALC_METHOD == 1)
		{
			$s = getStats();
			$maxes = array();
			
			foreach ($s as $stat)
			{
				$maxes[$stat] = 1000;
			}
		}
	
		$sort = getSort();
		$filter = getFilter();
		$calc = getCalculation($maxes);
		$eff = getEffectiveStats();
		
		
		
		$keys = array("%s%", "%f%", "%c%", "%e%");
		$values = array($sort, $filter, $calc, $eff);
		$query = str_replace($keys, $values, $query);
		
		return $query;
	}
	
	function getEffectiveStats()
	{
		$stats = getStats();
		
		foreach ($stats as $key => $value)
		{
			$stats[$key] = "IF(g.`{$value}` > 0, r.`{$value}` / g.`{$value}`, 0) * 1000 AS `e{$value}`";
		}
		
		return implode(", ", $stats);
	}
	
	function getCalculation($maxes)
	{
		global $DB;
		$c = array("ER" => 0, "div" => 1);
		$stats = getStats();
		
		$query = "SELECT * FROM `calculations` WHERE `id`=" . ACTIVE_CALC . " AND `user`=" . USERID;
		$DB->query($query);
		
		if (isset($_REQUEST['ER']))
		{
			$c = $_REQUEST;
		}
		else if (ACTIVE_CALC > 0 && $DB->rowCount() == 1)
		{
			$c = $DB->fetchAssoc();
			$c['div'] = 1;
		}
		
		foreach ($stats as $key => $value)
		{
			if (isset($c[$value]))
			{
				if (!is_array($maxes))
				{
					$stats[$key] = "IF(g.`{$value}` > 0, r.`{$value}` / g.`{$value}`, 0) * {$c[$value]}";
				}
				else
				{
					$stats[$key] = "IF({$maxes[$value]} > 0, r.`{$value}` / {$maxes[$value]}, 0) * {$c[$value]}";
				}
			}
			else
			{
				unset($stats[$key]);
			}
		}
		
		$stats = implode(" + ", $stats);
		
		$calc = "(({$stats}) / {$c['div']}) AS `calc`";
		
		return $calc;
	}
	
	function getFilter()
	{
		$planet = $_SESSION['filter_planet'];
		$time = $_SESSION['filter_time'];
		$cat = $_SESSION['filter_cat'];
		$pathlen = strlen($cat);
		
		//$timediff = mktime() - ($time * 86400);
		
		//echo "{$time}<br>" . mktime() . "<br>" . ($time * 86400) . "<br>";
		
		$filter = ifelse($pathlen > 2 && $_SESSION['filter_cat'] != "deleted", "LEFT(g.`path`, $pathlen) = '$cat'", "1");
		$filter .= ifelse(isset($_SESSION['filter_time']) && $time > -1 && is_numeric($_SESSION['filter_time']), " AND UNIX_TIMESTAMP(`date`) >= $time", "");
		$filter .= ifelse(isset($_SESSION['filter_planet']) && $planet > -1 && is_numeric($_SESSION['filter_planet']), " AND `planet` = $planet", "");
		
		return $filter;
	}
	
	function getSort()
	{
		if (!isset($_REQUEST['sort']))
		{
			$_REQUEST['sort'] = DEFAULT_SORT;
		}
		
		if (!isset($_REQUEST['dir']))
		{
			$_REQUEST['dir'] = DEFAULT_SORT_DIR;
		}
		
		switch ($_REQUEST['sort'])
		{
			case "name":
				$sort = "`name`";
				break;
			case "cat":
				$sort = "`long_name`";
				break;
			case "er":
				$sort = "r.`ER`";
				break;
			case "cr":
				$sort = "r.`CR`";
				break;
			case "cd":
				$sort = "r.`CD`";
				break;
			case "dr":
				$sort = "r.`DR`";
				break;
			case "fl":
				$sort = "r.`FL`";
				break;
			case "hr":
				$sort = "r.`HR`";
				break;
			case "ma":
				$sort = "r.`MA`";
				break;
			case "pe":
				$sort = "r.`PE`";
				break;
			case "oq":
				$sort = "r.`OQ`";
				break;
			case "sr":
				$sort = "r.`SR`";
				break;
			case "ut":
				$sort = "r.`UT`";
				break;
			case "date":
				$sort = "`date`";
				break;
			case "calc":
				$sort = "`calc`";
				break;
			case "conc":
				$sort = "`concentration`";
				break;
			default:
				$sort = "`name`";
				break;
		}
		
		if ($_REQUEST['dir'] == 1) //Sort it descending
		{
			$sort .= " DESC";
		}
		else
		{
			$sort .= " ASC";
		}
		
		if ($_REQUEST['sort'] != "name")
		{
			$sort .= ", `name`";
		}
		
		return $sort;
	}
?>
Return current item: SWG Resource Tracker