<?php
	/*
	** File: item_functions
	** Description: functions needed for get info about items
	** Version: $Revision: 1.12 $
	** Created: 6/10/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/20 17:21:27 $
	**
	** Log : $Log: item_functions,v $
	** Log : Revision 1.12  1999/12/20 17:21:27  leon
	** Log : Only pick random items that are in stock.
	** Log :
	** Log : Revision 1.11  1999/10/30 22:07:14  leon
	** Log : Fixed some problems with images associated with items and departments.
	** Log : Defined the directories for items and departments inside global_settings.
	** Log :
	** Log : Revision 1.10  1999/10/16 01:02:57  jj
	** Log : Finally got the random item generator, etc. to work without error under all of
	** Log : the strange cases.
	** Log :
	** Log : Revision 1.9  1999/10/14 21:15:29  leon
	** Log : Fixing bugs introduced when error checking as added.
	** Log :
	** Log : Revision 1.8  1999/10/14 01:49:37  jj
	** Log : Added some error handling (MySQL errors were being generated on the homepage of
	** Log : newly created sites).
	** Log :
	** Log : Revision 1.7  1999/10/05 23:58:45  leon
	** Log : Added double-include protection to item functions.  Added examples of using
	** Log : RandomItem and SpecialItem to the welcome page.
	** Log :
	** Log : Revision 1.6  1999/09/29 23:51:14  leon
	** Log : GetItemInfo gets relationships now.  Added code to item to show Cross-Sell
	** Log : and Accessory related items.
	** Log :
	** Log : Revision 1.5  1999/08/18 22:32:06  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.4  1999/07/28 19:27:22  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	$included_flag = 'INCLUDE_' . basename(__FILE__);
	
	if(defined($included_flag))
		//if this module has been included already, then the
		//constant will be defined.  Note the unusual use of
		//the return statement here.  Control will return to
		//the file that included it.
		return;
	
	define($included_flag, TRUE);	


	/*
	** Function: GetItemInfo
	** Input: INTEGER Item_ID
	** Output: ARRAY
	** Description: Returns associative array describing an item, or 0 on error.
	*/
	function GetItemInfo($Item_ID)
	{
		global $DatabaseLink;
		$Item_ID = intval($Item_ID);
	
		//Get main item information
		$Query = "SELECT Name, Description, Keywords, Graphic, LargeGraphic, Thumbnail ";
		$Query .= "FROM item ";
		$Query .= "WHERE ID = $Item_ID ";
		$Query .= "AND Active = 'Y'";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);

		if(mysql_num_rows($DatabaseResult))
		{
			//get the row
			$DatabaseRow = mysql_fetch_object($DatabaseResult);

			//put results into array			
			$Item["ID"] = $Item_ID;
			$Item["Name"] = $DatabaseRow->Name;
			$Item["Description"] = $DatabaseRow->Description;
			$Item["Keywords"] = $DatabaseRow->Keywords;

			//if graphics don't exist, then set them to empty,
			//otherwise, add path to front
			if($DatabaseRow->Graphic != "")
			{
				$Item["Graphic"] = ITEM_IMAGES_DIR . "/" . $DatabaseRow->Graphic;
				if(!file_exists($Item["Graphic"]))
				{
					$Item["Graphic"] = "";
				}
			}

			if($DatabaseRow->LargeGraphic != "")
			{
				$Item["LargeGraphic"] = ITEM_IMAGES_DIR . "/" . $DatabaseRow->LargeGraphic;
				if(!file_exists($Item["LargeGraphic"]))
				{
					$Item["LargeGraphic"] = "";
				}
			}

			if($DatabaseRow->Thumbnail != "")
			{
				$Item["Thumbnail"] = ITEM_IMAGES_DIR . "/" . $DatabaseRow->Thumbnail;
				if(!file_exists($Item["Thumbnail"]))
				{
					$Item["Thumbnail"] = "";
				}
			}

			//get SKUs associated with this item, if any
			$Query = "SELECT s.ID, s.ExternalSKU, s.Name, s.ListPrice, s.SalePrice, s.Shipping, ";
			$Query .= "i. Available, i.Reserve, i.Backorder ";
			$Query .= "FROM sku s, inventory i ";
			$Query .= "WHERE s.Item = $Item_ID ";	
			$Query .= "AND s.Active = 'y' ";
			$Query .= "AND s.ID = i.SKU ";
			$Query .= "ORDER BY DisplayPrecedence ASC, SalePrice DESC, ListPrice DESC ";
			$DatabaseResult = mysql_query($Query, $DatabaseLink);
			
			while($DatabaseRow = mysql_fetch_object($DatabaseResult))
			{
				$Item["SKU"]["ID"][] = $DatabaseRow->ID;
				$Item["SKU"]["ExternalSKU"][] = $DatabaseRow->ExternalSKU;
				$Item["SKU"]["Name"][] = $DatabaseRow->Name;
				$Item["SKU"]["ListPrice"][] = $DatabaseRow->ListPrice;
				$Item["SKU"]["SalePrice"][] = $DatabaseRow->SalePrice;
				$Item["SKU"]["Shipping"][] = $DatabaseRow->Shipping;
				$Item["SKU"]["Available"][] = $DatabaseRow->Available;
				$Item["SKU"]["Reserve"][] = $DatabaseRow->Reserve;
				$Item["SKU"]["Backorder"][] = $DatabaseRow->Backorder;
			}
	
			//Get departments to which this item belongs
			$Query = "SELECT Department ";
			$Query .= "FROM department_item ";
			$Query .= "WHERE Item = $Item_ID ";
			$DatabaseResult = mysql_query($Query, $DatabaseLink);
			while($DatabaseRow = mysql_fetch_object($DatabaseResult))
			{
				$Item["Department"][] = $DatabaseRow->Department;
			}			
	
			//Get related items, store array of item per relationship
			$Query = "SELECT ii.Related_Item, r.Name ";
			$Query .= "FROM item_item ii, relationship r ";
			$Query .= "WHERE ii.Item = $Item_ID ";
			$Query .= "AND ii.Relationship = r.ID ";
			$DatabaseResult = mysql_query($Query, $DatabaseLink);
			while($DatabaseRow = mysql_fetch_object($DatabaseResult))
			{
				$Item['Relationship'][($DatabaseRow->Name)][] = $DatabaseRow->Related_Item;
			}
			
		}
		else
		{
			//Item ID wasn't recogined, so return FALSE
			$Item = FALSE;
		}
		
		return($Item);
	}
	


	/*
	** Function: RandomItem
	** Input:
	** Output: INTEGER
	** Description: Returns ID of a randomly-chosen item, or 0 on error.
	*/
	function RandomItem()
	{
		global $DatabaseLink;

		$Query = "SELECT item.id from item,sku,inventory ";
		$Query .= "WHERE item.id = sku.item  ";
		$Query .= "AND sku.id = inventory.sku  ";
		$Query .= "AND inventory.available > 0 ";
		
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		$numItems = mysql_num_rows($DatabaseResult);
		switch ($numItems)
		{
			case 0:
				$retItem = 0;
				break;
			case 1: /* Note: this case will break if you let the default handle it. */
				list($retItem) = mysql_fetch_row($DatabaseResult);
				break;
			default:
				$retItem = mysql_result($DatabaseResult, rand(0, $numItems-1), 0);
				break;
		}
		return $retItem;
	}
	


	/*
	** Function: SpecialItem
	** Input: 
	** Output: INTEGER
	** Description: returns ID of one of the featured items. 
	*/
	function SpecialItem()
	{
		global $DatabaseLink;
		
		//how often to change item
		$ChangeDays =7;

		//create array of item IDs
		$Item_ID = array(1,2,3);

		//get day of the year (1-365)
		$DayOfYear = date("z");

		//return item ID
		return($Item_ID[(floor($DayOfYear/$ChangeDays)%(count($Item_ID)))]);
	}


?>
