Location: PHPKode > projects > SWG Resource Tracker > distro/upload/inventory.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.
 */

$idx = new Inventory();

class Inventory
{
	var $output = "";
	
	function Inventory()
	{
		global $DB, $display, $baseurl;
		
		$display['title'] = "Resource Tracker - Inventory";
		
		if (!LOGGED_IN)
		{
			header("Location: {$baseurl}");
			die();
		}
		
		switch ($_REQUEST['act'])
		{
			case "inventory":
				$this->showInventory();
				break;
			case "addinventory":
				if ($_REQUEST['CODE'] == 1)
				{
					$this->showAddInventoryList();
				}
				else if ($_REQUEST['CODE'] == 2)
				{
					$this->finishAddInventory();
				}
				else
				{
					$this->showAddInventoryForm();
				}
				break;
			case "editinventory":
				if (isset($_REQUEST['ibtn']))
				{
					$this->finishEdit();
				}
				else
				{
					$this->showEditInventoryForm();
				}
				break;
			case "deleteinventory":
				$this->deleteInventory();
				break;
			case "exportinventory":
				$this->genExport();
				break;
			case "importinventory":
				if (isset($_REQUEST['ibtn']))
				{
					$this->finishImport();
				}
				else
				{
					$this->showImportForm();
				}
				
				break;
		}
		
		
		$this->output .= "";
		
		
		$display['output'] = $this->output;
	}
	
	function finishImport()
	{
		global $DB, $baseurl;
		
		$error = "";
		
		
		if ($_FILES['userfile']['tmp_name'] != "")
		{
			if ($data = file($_FILES['userfile']['tmp_name']))
			{
				/*$data = addslashes(implode("", $data));
				$data = str_replace("\x00", '\0', $data);
				$data = str_replace("\x08", '\b', $data);
				$data = str_replace("\x0a", '\n', $data);
				$data = str_replace("\x0d", '\r', $data);
				$data = str_replace("\x1a", '\Z', $data);
				
				$type = $_FILES[$key]['type'];*/
				
				
				$this->output .= "<div class=\"block\">
					<div class=\"header\">Successfully Imported:</div>
					<div class=\"body\">\n";
				
				$query = "DELETE FROM `inventory` WHERE `user`=" . USERID;
				$DB->query($query);
				
				$failed = array();
				
				for ($i = 0; $i < count($data); $i++)
				{
					$data[$i] = ereg_replace("[^a-zA-Z0-9\t]", "", $data[$i]);
					$r = explode("\t", $data[$i]);
					
					if (is_numeric($r[0]) && strlen($r[1]) > 1)
					{
						$query = "SELECT * FROM `resources` WHERE `name`='{$r[1]}'";
						$DB->query($query);
						
						if ($DB->rowCount() == 1)
						{
							$row = $DB->fetchAssoc();
							
							$query = "INSERT INTO `inventory` (`user`, `resource`, `quantity`, `date`) VALUES (" . USERID . ", {$row['id']}, " . intval($r[0]) . ", NOW())";
							$DB->query($query);
							
							$this->output .= "{$r[1]}<br />\n";
						}
						else
						{
							$failed[] = $r[1];
						}
					}
				}
				
				$this->output .= "</div>
				</div>";
				
				if (count($failed) > 0)
				{
					$this->output .= "<br /><br /><div class=\"block\">
					<div class=\"header\">Failed:</div>
					<div class=\"body\">" . implode("<br />", $failed) . "
					</div>
				</div>";
				}
			}
			else
			{
				$error = "Failed to load file.";
			}
		}
		else
		{
			$error = "No filename given.";
		}
	}
	
	function showImportForm()
	{
		global $DB, $baseurl;
		
		$this->output .= "<form action=\"{$baseurl}\" method=\"post\" enctype=\"multipart/form-data\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ltb\">
					<tr>
						<th class=\"rbb\">Data File:</th>
					</tr>
					<tr>
						<td class=\"rbb\">
							<input name=\"userfile\" type=\"file\" /><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"300000\" />
						</td>
					</tr>
					<tr>
						<th class=\"rbb\" align=\"right\">
							<input type=\"hidden\" name=\"act\" value=\"importinventory\" />
							<input type=\"submit\" name=\"ibtn\" value=\"Save\" />
						</th>
					</tr>
					<tr>
						<th class=\"rbb\">
							<em>This will override ALL existing inventory data.</em>
						</th>
					</tr>
				</table></form>
				
				<br>
				<div style=\"width:300px; font-size: 9px; text-align: left;\">The format of the file must be <em>&lt;quantity&gt;&lt;tab&gt;&lt;resource&nbsp;name&gt;</em>.  Each entry on a single line.  All other data will be ignored.  The inventory export feature generates a compatible file for comparison.</div>";
	}
	
	function genExport()
	{
		global $DB;
		
		$query = "SELECT r.*, i.`quantity`, g.`long_name`, g.`path`, g.`id` AS `owner` FROM `resources` AS r, `inventory` AS i LEFT JOIN `categories` AS g ON r.`category`=g.`id` WHERE r.`id`=i.`resource` AND i.`user`=" . USERID;
		$DB->query($query);
		
		$results = $DB->fetchAll();
		
		header("Content-type: text/plain");
		
		echo "Quantity\tName\tCategory\tER\tCR\tCD\tDR\tFL\tHR\tMA\tPE\tOQ\tSR\tUT\n";
		
		for ($i = 0; $i < count($results); $i++)
		{
			$r = $results[$i];
			
			echo "{$r['quantity']}\t{$r['name']}\t{$r['long_name']}\t{$r['ER']}\t{$r['CR']}\t{$r['CD']}\t{$r['DR']}\t{$r['FL']}\t{$r['HR']}\t{$r['MA']}\t{$r['PE']}\t{$r['OQ']}\t{$r['SR']}\t{$r['UT']}\n";
		}
		
		die();
	}
	
	function deleteInventory()
	{
		global $DB, $baseurl;
		
		$i = intval($_REQUEST['i']);
		
		$query = "DELETE FROM `inventory` WHERE `id`={$i} AND `user`=" . USERID;
		$DB->query($query);
		
		header("Location: {$baseurl}?act=inventory");
		die();
	}
	
	function finishEdit()
	{
		global $DB, $baseurl;
		
		$i = intval($_REQUEST['i']);
		$q = intval($_REQUEST['quantity']);
		
		$query = "UPDATE `inventory` SET `quantity`=$q WHERE `id`=$i AND `user`=" . USERID;
		$DB->query($query);
		
		header("Location: {$baseurl}?act=inventory");
		die();
	}
	
	function showEditInventoryForm()
	{
		global $DB, $baseurl;
		
		$this->output .= "<form action=\"{$baseurl}\" method=\"post\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ltb\">
					<tr>
						<th class=\"rbb\">Name</th>
						<th class=\"rbb\">Quantity</th>
						<th class=\"rbb\">Category</th>
						<th class=\"rbb\">ER</th>
						<th class=\"rbb\">CR</th>
						<th class=\"rbb\">CD</th>
						<th class=\"rbb\">DR</th>
						<th class=\"rbb\">FL</th>
						<th class=\"rbb\">HR</th>
						<th class=\"rbb\">MA</th>
						<th class=\"rbb\">PE</th>
						<th class=\"rbb\">OQ</th>
						<th class=\"rbb\">SR</th>
						<th class=\"rbb\">UT</th>
					</tr>";
		
		$query = "SELECT %e%, r.*, i.`quantity`, g.`long_name`, g.`path`, g.`id` AS `owner` FROM `resources` AS r, `inventory` AS i LEFT JOIN `categories` AS g ON r.`category`=g.`id` WHERE r.`id`=i.`resource` AND i.`id`={$_REQUEST['i']}";
		$query = getQuery($query);
		$DB->query($query);
		
		if ($DB->rowCount() == 0)
		{
			$this->output .= "<tr>
								<td class=\"rbb\" colspan=\"14\" align=\"center\"><em>No data found.</em></td>
							</tr>
						</table></form>";
		}
		else
		{
			$results = $DB->fetchAssoc();
			$this->output .= "<tr>
								<td class=\"rbb\">{$results['name']}</td>
								<td class=\"rbb\"><input type=\"text\" size=\"7\" name=\"quantity\" value=\"{$results['quantity']}\" /></td>
								<td class=\"rbb\"><a href=\"{$baseurl}?act=tree&amp;parent={$results['owner']}&amp;hl={$results['id']}\">{$results['long_name']}</a></td>
								<td class=\"rbb\">" . colorize($results['ER'], 0, $results['eER']) . "</td>
								<td class=\"rbb\">" . colorize($results['CR'], 0, $results['eCR']) . "</td>
								<td class=\"rbb\">" . colorize($results['CD'], 0, $results['eCD']) . "</td>
								<td class=\"rbb\">" . colorize($results['DR'], 0, $results['eDR']) . "</td>
								<td class=\"rbb\">" . colorize($results['FL'], 0, $results['eFL']) . "</td>
								<td class=\"rbb\">" . colorize($results['HR'], 0, $results['eHR']) . "</td>
								<td class=\"rbb\">" . colorize($results['MA'], 0, $results['eMA']) . "</td>
								<td class=\"rbb\">" . colorize($results['PE'], 0, $results['ePE']) . "</td>
								<td class=\"rbb\">" . colorize($results['OQ'], 0, $results['eOQ']) . "</td>
								<td class=\"rbb\">" . colorize($results['SR'], 0, $results['eSR']) . "</td>
								<td class=\"rbb\">" . colorize($results['UT'], 0, $results['eUT']) . "</td>
							</tr>
							<tr>
								<th class=\"rbb\" colspan=\"14\" align=\"right\">
									<input type=\"hidden\" name=\"i\" value=\"{$_REQUEST['i']}\" />
									<input type=\"hidden\" name=\"act\" value=\"editinventory\" />
									<input type=\"submit\" name=\"ibtn\" value=\"Save\" />
								</th>
							</tr>
						</table></form>\n";
		}
	}
	
	function finishAddInventory()
	{
		global $DB, $baseurl;
		
		$resources = array();
		$failed = array();
		
		foreach ($_REQUEST as $r => $q)
		{
			if (!(strpos($r, "invname") === false))
			{
				$t = ereg_replace("[^a-zA-Z]", "", str_replace("invname", "", $r));
				
				$query = "SELECT * FROM `resources` WHERE `name`='{$t}'";
				$DB->query($query);
				
				if ($DB->rowCount() > 0)
				{
					$row = $DB->fetchAssoc();
					
					$resources[] = $t;//array("name" => $t, "quantity" => intval(ereg_replace("[^0-9]", "", $q)), "id" => $row['id']);
					
					$query = "INSERT INTO `inventory` (`user`, `resource`, `quantity`, `date`) VALUES (" . USERID . ", {$row['id']}, " . intval(ereg_replace("[^0-9]", "", $q)) . ", NOW())";
					$DB->query($query);
				}
				else
				{
					$failed[] = $t;
				}
			}
		}
		
		if (count($resources) > 0)
		{
			$this->output .= "<div class=\"block\">
				<div class=\"header\">Added:</div>
				<div class=\"body\">
					" . implode("<br />", $resources) . "
				</div>
			</div>
			<br /><br />";
		}
		
		if (count($failed) > 0)
		{
			$this->output .= "<div class=\"block\">
				<div class=\"header\">Failed:</div>
				<div class=\"body\">
					" . implode("<br />", $failed) . "
				</div>
			</div>";
		}
	}
	
	function showAddInventoryList()
	{
		global $DB, $baseurl;
		
		$resources = trim($_REQUEST['search']);
		
		while (ereg("[\n\r]{2}", $resources))
		{
			$resources = ereg_replace("[\n\r]{2}", "\n", $resources);
		}
		
		$resources = explode("\n", $resources);
		
		$this->output .= "<form action=\"{$baseurl}\" method=\"post\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ltb\">
					<tr>
						<th class=\"rbb\">Name</th>
						<th class=\"rbb\">Quantity</th>
						<th class=\"rbb\">Category</th>
						<th class=\"rbb\">ER</th>
						<th class=\"rbb\">CR</th>
						<th class=\"rbb\">CD</th>
						<th class=\"rbb\">DR</th>
						<th class=\"rbb\">FL</th>
						<th class=\"rbb\">HR</th>
						<th class=\"rbb\">MA</th>
						<th class=\"rbb\">PE</th>
						<th class=\"rbb\">OQ</th>
						<th class=\"rbb\">SR</th>
						<th class=\"rbb\">UT</th>
					</tr>";
					
		$notfound = array();
		
		for ($i = 0; $i < count($resources); $i++)
		{
			$query = "SELECT %e%, r.*, g.`long_name`, g.`path`, g.`id` AS `owner` FROM `resources` AS r LEFT JOIN `categories` AS g ON r.`category`=g.`id` WHERE r.`name`='{$resources[$i]}'";
			$query = getQuery($query);
			$DB->query($query);
			
			$results = $DB->fetchAssoc();
			
			if ($DB->rowCount() == 0)
			{
				$notfound[] = $resources[$i];
			}
			else
			{
				$this->output .= "<tr>
								<td class=\"rbb\">{$results['name']}</td>
								<td class=\"rbb\"><input type=\"text\" size=\"7\" name=\"invname{$results['name']}\" value=\"1\" /></td>
								<td class=\"rbb\"><a href=\"{$baseurl}?act=tree&amp;parent={$results['owner']}&amp;hl={$results['id']}\">{$results['long_name']}</a></td>
								<td class=\"rbb\">" . colorize($results['ER'], 0, $results['eER']) . "</td>
								<td class=\"rbb\">" . colorize($results['CR'], 0, $results['eCR']) . "</td>
								<td class=\"rbb\">" . colorize($results['CD'], 0, $results['eCD']) . "</td>
								<td class=\"rbb\">" . colorize($results['DR'], 0, $results['eDR']) . "</td>
								<td class=\"rbb\">" . colorize($results['FL'], 0, $results['eFL']) . "</td>
								<td class=\"rbb\">" . colorize($results['HR'], 0, $results['eHR']) . "</td>
								<td class=\"rbb\">" . colorize($results['MA'], 0, $results['eMA']) . "</td>
								<td class=\"rbb\">" . colorize($results['PE'], 0, $results['ePE']) . "</td>
								<td class=\"rbb\">" . colorize($results['OQ'], 0, $results['eOQ']) . "</td>
								<td class=\"rbb\">" . colorize($results['SR'], 0, $results['eSR']) . "</td>
								<td class=\"rbb\">" . colorize($results['UT'], 0, $results['eUT']) . "</td>
							</tr>\n";
			}
		}
		
		$this->output .= "<tr>
			<th class=\"rbb\" colspan=\"14\"><input type=\"submit\" value=\"Add\" />
											<input type=\"hidden\" name=\"act\" value=\"addinventory\" />
											<input type=\"hidden\" name=\"CODE\" value=\"2\" /></th>
			</tr>
		</table></form>\n";
		
		if (count($notfound) > 0)
		{
			$this->output .= "<br /><div class=\"block\">
				<div class=\"header\">Not Found:</div>
				<div class=\"body\">
							" . implode("<br />", $notfound) . "
				</div>
			</div>";
		}
	}
	
	function showAddInventoryForm()
	{
		global $DB, $baseurl;
		
		$this->output .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ltb\">
				<tr>
					<th class=\"rbb\">Enter the resource name(s):</th>
				</tr>
				<tr>
					<td class=\"rbb\"><form action=\"{$baseurl}\" method=\"post\">
											<textarea name=\"search\" rows=\"8\"></textarea>
											<br />
											<input type=\"submit\" value=\"Search\" />
											<input type=\"hidden\" name=\"act\" value=\"addinventory\" />
											<input type=\"hidden\" name=\"CODE\" value=\"1\" />
										</form>
					</td>
				</tr>
				<tr>
					<th class=\"rbb\"><h6><em>Enter multiple items on separate lines.</em></h6></th>
				</tr>
			</table>";
	}
	
	function showInventory()
	{
		global $DB, $baseurl;
		
		$query = "SELECT %c%, %e%, r.*, i.`quantity`, i.`id` AS `iid`, g.`long_name`, g.`path`, g.`id` AS `owner` FROM `inventory` AS i LEFT JOIN `resources` AS r ON i.`resource`=r.`id` LEFT JOIN `categories` AS g ON r.`category`=g.`id` WHERE i.`user`=" . USERID . " ORDER BY `path` ASC";
		$query = getQuery($query);
		$DB->query($query);
		
		$results = $DB->fetchAll();
		
		$currentpath = "";
		
		for ($i = 0; $i < count($results); $i++)
		{
			$path = $results[$i]['path'];
			
			$compare = pathCompare($currentpath, minusLastPathComponent($path));
			
			if ($i > 0 && $compare != 0)
			{
					$this->output .= "</table>";
			}
			
			//echo "Path: " . $path . "<br>Current: " . $currentpath . "<br>MinusLast: " . minusLastPathComponent($path) . "<br>Compare: {$compare}<br>";
			
			if ($compare > 0) // Need to adjust $currentpath to be in line with $path
			{
				while (pathCompare($currentpath, minusLastPathComponent($path)) > 0)
				{
					$currentpath = minusLastPathComponent($currentpath);
					//echo "Current and last: " . $currentpath . "<br>" . minusLastPathComponent($path) . "<br><br>";
					$this->output .= "</div></div>\n";
				}
			}
			
			
			while (pathCompare($currentpath, minusLastPathComponent($path)) < 0)
			{
				$currentpath = addNextPathComponent($currentpath, $path);
				//echo "Current: " . $currentpath . "<br>";
				$query = "SELECT * FROM `categories` WHERE `path`='{$currentpath}'";
				$DB->query($query);
				
				$row = $DB->fetchAssoc();
				
				$this->output .= "<div class=\"inventory\" id=\"{$row['id']}\"><span class=\"catname\"><a href=\"#\" onclick=\"toggleCollapse('{$row['id']}')\">-</a> {$row['long_name']}</span><div style=\"display: inherit;\">\n";
			}
			
			if ($compare != 0)
			{
				$this->output .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ltb\">
					<tr>
						<th class=\"rbb\">Name</th>
						<th class=\"rbb\">Category</th>
						<th class=\"rbb\">Quantity</th>
						<th class=\"rbb\">ER</th>
						<th class=\"rbb\">CR</th>
						<th class=\"rbb\">CD</th>
						<th class=\"rbb\">DR</th>
						<th class=\"rbb\">FL</th>
						<th class=\"rbb\">HR</th>
						<th class=\"rbb\">MA</th>
						<th class=\"rbb\">PE</th>
						<th class=\"rbb\">OQ</th>
						<th class=\"rbb\">SR</th>
						<th class=\"rbb\">UT</th>
						<th class=\"rbb\">Calc</th>
						<th class=\"rbb\">&nbsp;</th>
					</tr>";
			}
			
			$this->output .= "<tr class=\"resource\">
								<td class=\"rbb\">{$results[$i]['name']}</td>
								<td class=\"rbb\"><div><div class=\"fl\"><a href=\"{$baseurl}?act=tree&amp;parent={$results[$i]['owner']}&amp;hl={$results[$i]['id']}\">{$results[$i]['long_name']}</a> </div><div class=\"fr\"><a href=\"{$baseurl}?act=usage&amp;c={$results[$i]['owner']}\"><img src=\"usage.png\" alt=\"u\" /></a></div></div></td>
								<td class=\"rbb\">" . number_format($results[$i]['quantity']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['ER'], 0, $results[$i]['eER']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['CR'], 0, $results[$i]['eCR']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['CD'], 0, $results[$i]['eCD']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['DR'], 0, $results[$i]['eDR']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['FL'], 0, $results[$i]['eFL']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['HR'], 0, $results[$i]['eHR']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['MA'], 0, $results[$i]['eMA']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['PE'], 0, $results[$i]['ePE']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['OQ'], 0, $results[$i]['eOQ']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['SR'], 0, $results[$i]['eSR']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['UT'], 0, $results[$i]['eUT']) . "</td>
								<td class=\"rbb\">" . colorize($results[$i]['calc'], 100) . "</td>
								<td class=\"rbb\"><a href=\"{$baseurl}?act=editinventory&amp;i={$results[$i]['iid']}\">Edit</a> | <a href=\"{$baseurl}?act=deleteinventory&amp;i={$results[$i]['iid']}\" onclick=\"return confirm('Delete {$results[$i]['name']}?')\">Delete</a></td>
							</tr>\n";
			
			//echo "<br><br><br>";
			$compare = pathCompare($currentpath, minusLastPathComponent($path));
		}
		
		$this->output .= "</table>";
		
		while ($currentpath != "")
		{
			$currentpath = minusLastPathComponent($currentpath);
			$this->output .= "</div></div>\n";
		}
	}
}
?>
Return current item: SWG Resource Tracker