<?php
	/*****************************************************************************
	IP Reg, a PHP/MySQL IPAM tool
	Copyright (C) 2007-2009 Wietse Warendorff
	
	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 3 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, see <http://www.gnu.org/licenses/>.
	
	For more information, visit http://sourceforge.net/projects/ipreg,
	or contact me at wietsew@users.sourceforge.net
	*****************************************************************************/
	
	// start page
		// includes
		include("includes.php");
		
		// start output
		include("header.php");
		
		// set template
		$tp = new Template("tpl/search.tpl", $config_yapter_error);
		
		// set language variables
		$tp->setvars($lang);
	
	// get string that was searched for ($search is allready set in header.php)
	if (empty($search)) {
		// parse nosearch box
		$tp->parse("nosearch");
		
		// hide others
		$tp->hide("asset");
		$tp->hide("location");
		$tp->hide("node");
		$tp->hide("subnet");
		$tp->hide("vlan");
		$tp->hide("resultcount");
	} else {
		// hide nosearch box
		$tp->hide("nosearch");
		
		// set needle
		$needle = '%' . $search . '%';
		
		// set counter
		$resultcounter = 0;
		
		// setup asset
			// build query
			$query = "SELECT
					asset.asset_id AS asset_id,
					asset.asset_name AS asset_name
				FROM
					asset
				WHERE
					asset.asset_name LIKE '" . $needle . "'
					OR asset.asset_hostname LIKE '" . $needle . "'
					OR asset.asset_info LIKE '" . $needle . "'
				ORDER BY
					asset.asset_name";
			
			// run query
			$assets = $db->db_select($query);
			
			// count results
			$counter = count($assets);
			
			// counter to tpl
			$tp->set("counter", $counter);
		
			// any assets?
			if ($counter>0) {
				// get objects
				foreach($assets AS $asset) {
					// send to tpl
					$tp->set("item_name", $lang['lang_assets']);
					$tp->set("item", "asset");
					$tp->set("id", $asset['asset_id']);
					$tp->set("name", $asset['asset_name']);
					
					// parse block
					$tp->parse("row");
					
					// update counter
					$resultcounter++;
				}
				
				// parse block
				$tp->parse("asset");
			} else {
				// parse block
				$tp->hide("asset");
			}
			
			// clear row
			$tp->clear("row");
		
		// setup location
			// build query
			$query = "SELECT
					location.location_id AS location_id,
					location.location_name AS location_name
				FROM
					location
				WHERE
					location.location_name LIKE '" . $needle . "'
					OR location.location_info LIKE '" . $needle . "'
				ORDER BY
					location.location_name";
			
			// run query
			$locations = $db->db_select($query);
			
			// count results
			$counter = count($locations);
			
			// counter to tpl
			$tp->set("counter", $counter);
		
			// any locations?
			if ($counter>0) {
				// get objects
				foreach($locations AS $location) {
					// send to tpl
					$tp->set("item_name", $lang['lang_locations']);
					$tp->set("item", "location");
					$tp->set("id", $location['location_id']);
					$tp->set("name", $location['location_name']);
					
					// parse block
					$tp->parse("row");
					
					// update counter
					$resultcounter++;
				}
				
				// parse block
				$tp->parse("location");
			} else {
				// parse block
				$tp->hide("location");
			}
			
			// clear row
			$tp->clear("row");
		
		// setup node
			// build query
			$query = "SELECT
					node.node_id AS node_id,
					node.node_ip AS node_ip
				FROM
					node
				WHERE
					node.node_ip LIKE '" . $needle . "'
					OR node.node_mac LIKE '" . $needle . "'
					OR node.node_dns1 LIKE '" . $needle . "'
					OR node.node_dns2 LIKE '" . $needle . "'
					OR node.node_info LIKE '" . $needle . "'
				ORDER BY
					node.node_ip";
			
			// run query
			$nodes = $db->db_select($query);
			
			// count results
			$counter = count($nodes);
			
			// counter to tpl
			$tp->set("counter", $counter);
		
			// any nodes?
			if ($counter>0) {
				// get objects
				foreach($nodes AS $node) {
					// send to tpl
					$tp->set("item_name", $lang['lang_nodes']);
					$tp->set("item", "node");
					$tp->set("id", $node['node_id']);
					$tp->set("name", $node['node_ip']);
					
					// parse block
					$tp->parse("row");
					
					// update counter
					$resultcounter++;
				}
				
				// parse block
				$tp->parse("node");
			} else {
				// parse block
				$tp->hide("node");
			}
			
			// clear row
			$tp->clear("row");
		
		// setup subnet
			// build query
			$query = "SELECT
					subnet.subnet_id AS subnet_id,
					subnet.subnet_address AS subnet_address
				FROM
					subnet
				WHERE
					subnet.subnet_address LIKE '" . $needle . "'
					OR subnet.subnet_info LIKE '" . $needle . "'
				ORDER BY
					subnet.subnet_address";
			
			// run query
			$subnets = $db->db_select($query);
			
			// count results
			$counter = count($subnets);
			
			// counter to tpl
			$tp->set("counter", $counter);
		
			// any subnets?
			if ($counter>0) {
				// get objects
				foreach($subnets AS $subnet) {
					// send to tpl
					$tp->set("item_name", $lang['lang_subnets']);
					$tp->set("item", "subnet");
					$tp->set("id", $subnet['subnet_id']);
					$tp->set("name", $subnet['subnet_address']);
					
					// parse block
					$tp->parse("row");
					
					// update counter
					$resultcounter++;
				}
				
				// parse block
				$tp->parse("subnet");
			} else {
				// parse block
				$tp->hide("subnet");
			}
			
			// clear row
			$tp->clear("row");
		
		// setup vlan
			// build query
			$query = "SELECT
					vlan.vlan_id AS vlan_id,
					vlan.vlan_name AS vlan_name
				FROM
					vlan
				WHERE
					vlan.vlan_name LIKE '" . $needle . "'
					OR vlan.vlan_info LIKE '" . $needle . "'
				ORDER BY
					vlan.vlan_name";
			
			// run query
			$vlans = $db->db_select($query);
			
			// count results
			$counter = count($vlans);
			
			// counter to tpl
			$tp->set("counter", $counter);
		
			// any vlans?
			if ($counter>0) {
				// get objects
				foreach($vlans AS $vlan) {
					// send to tpl
					$tp->set("item_name", $lang['lang_vlans']);
					$tp->set("item", "vlan");
					$tp->set("id", $vlan['vlan_id']);
					$tp->set("name", $vlan['vlan_name']);
					
					// parse block
					$tp->parse("row");
					
					// update counter
					$resultcounter++;
				}
				
				// parse block
				$tp->parse("vlan");
			} else {
				// parse block
				$tp->hide("vlan");
			}
			
			// clear row
			$tp->clear("row");
		
		// to tpl
		$tp->set("resultcounter", $resultcounter);
		
		// parse block
		$tp->parse("resultcount");
	}
	
	// end page
		// output
		$tp->parse();
		$tp->spit();
		
		// end output
		include("footer.php");
?>