<?php
	/*
	** File: UPDATE_ITEM
	** Description: update information about an item
	** Version: $Revision: 1.8 $
	** Created: 04/08/1999
	** Author: Leon Atkinson, Shannon -jj Behrens
	** Email: leon@clearink.com, jj@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:25 $
	**
	** Log : $Log: UPDATE_ITEM,v $
	** Log : Revision 1.8  1999/12/09 23:23:25  leon
	** Log : Changed ActionResults use so that it's an array instead of a
	** Log : continually-growing string.  Note the little function in standard_library
	** Log : for printing out all the action results.
	** Log :
	** Log : Revision 1.7  1999/11/09 22:03:38  jj
	** Log : Removed an unnecessary check.
	** Log :
	** Log : Revision 1.6  1999/11/02 19:09:46  leon
	** Log : Localized the UPDATE action modules
	** Log :
	** Log : Revision 1.5  1999/08/18 22:31:55  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.4  1999/08/03 22:51:55  jj
	** Log : Doing quality control on the department and item editors.
	** Log :
	** Log : Revision 1.3  1999/07/28 19:27:16  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	$continueScript = TRUE;



	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		$ActionResults[] = L_UPDATEITEM_DENIED;
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure we have some sort of valid input.
	*/
	if ($continueScript)
	{
		if (!isset($inputName) || $inputName=="")
		{
			$ActionResults[] = L_UPDATEITEM_NEEDNAME;
			$SCREEN = "admin_item";
			$continueScript = FALSE;
		}
		else if (!isset($item) || $item<=0)
		{
			$ActionResults[] = L_UPDATEITEM_PANIC;
			$SCREEN = "admin_menu";
			$continueScript = FALSE;
		}
	}

	
	
	/*
	** Make sure that the user isn't trying to use a name that has already 
	** been used.  
	*/
	if ($continueScript)
	{
		$repeatQuery = "SELECT ID FROM item ";
		$repeatQuery .= "WHERE Name = '" . addslashes($inputName) . "' ";
		if ( !($repeatRecords = mysql_query($repeatQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
		else
		{
			while ( list($id) = mysql_fetch_row($repeatRecords) )
			{
				if ($id != $item)
				{
					$ActionResults[] = "The item name you are trying to use has " . 
					"already been used: ". 
					"<A HREF=\"" . ScreenURL("admin_item") . 
					"&item=$id\">$inputName</A>.";
					$continueScript = FALSE;
				}
			}
		}
	}
				

	
	/*
	** Update the item's main information.
	*/
	if ($continueScript)
	{
		$Query = "UPDATE item ";
		$Query .= "SET Name='" . addslashes($inputName) . "', ";
		$Query .= "Description='" . addslashes($inputDescription) . "', ";
		$Query .= "Keywords='" . addslashes($inputKeywords) . "', ";
		$Query .= "Thumbnail='" . addslashes($inputThumbnail) . "', ";
		$Query .= "Graphic='" . addslashes($inputGraphic) . "', ";
		$Query .= "LargeGraphic='" . addslashes($inputLargeGraphic) . "', ";
		$Query .= "DisplayPrecedence=" . intval($inputDisplayPrecedence) . ", ";
		$Query .= "Active='" . addslashes($inputActive) . "' ";
		$Query .= "WHERE ID=" . intval($item);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Clear old department associations.
	*/
	if ($continueScript)
	{
		$Query = "DELETE FROM department_item where Item=" . intval($item);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $repeatQuery";
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Associate with departments.
	*/
	if ($continueScript)
	{
		for($index = 0; $index < count($inputDepartment); $index++)
		{
			$Query = "INSERT INTO department_item VALUES (";
			$Query .= intval($inputDepartment[$index]) . ", ";
			$Query .= intval($item);
			$Query .= ")";
			if(!mysql_query($Query, $DatabaseLink))
			{
				$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
					"   Query was: $repeatQuery";
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Send the user to the appropriate place (if an error occurs, 
	** $SCREEN will have already been set).
	*/
	if ($continueScript)
	{
		$ActionResults[] = L_UPDATEITEM_SUCCESS; 
		$SCREEN = "admin_item";

		/* $item is set */
		$department = $inputDepartment[0];
	}
	
?>
