<?php
	/*
	** File: ADD_ITEM
	** Description: Adds an item to the database
	** Version: $Revision: 1.10 $
	** 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:17 $
	**
	** Log : $Log: ADD_ITEM,v $
	** Log : Revision 1.10  1999/12/09 23:23:17  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.9  1999/11/09 21:47:11  leon
	** Log : More localization.
	** Log :
	** Log : Revision 1.8  1999/09/30 19:22:20  jj
	** Log : ADD_ITEM now takes the admin directly to "add_sku" on success.
	** Log :
	** Log : Revision 1.7  1999/09/30 19:13:43  jj
	** Log : Items added to the root department now have records in department_item associating them with the root department (this was a bug).
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:45  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/03 22:51:54  jj
	** Log : Doing quality control on the department and item editors.
	** Log :
	** Log : Revision 1.4  1999/08/03 00:28:05  jj
	** Log : Added stricter error checking and more friendly SCREEN redirection.
	** Log :
	** Log : Revision 1.3  1999/07/28 19:27:11  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"] )
	{
		print(L_ADDITEM_DENIED . "<BR>\n");
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure that the user has given the item a name. Everything else is 
	** relatively optional. Also, if the user didn't associate an item with any
	** departments, associate the item with the main department.
	*/
	if ($continueScript)
	{
		if (!isset($inputName) || $inputName=="")
		{
			$ActionResults[] = L_ADDITEM_NEEDNAME;
			$SCREEN = "add_item";
			$continueScript = FALSE;
		}
		else if ( !isset($inputDepartment) )
		{
			$inputDepartment[] = "0";
		}
	}
	
	
	
	/* 
	** Make sure that the user isn't trying to use a name that has already 
	** been used.  If this is the case, they should probably be trying to add
	** a sku instead. It's also possible that the correct item exists, but just
	** needs to be added to the department they are trying to add it to.
	*/
	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
		{
			if ( list($id) = mysql_fetch_row($repeatRecords) )
			{
				$ActionResults[] = L_ADDITEM_DUPLICATE;
				$SCREEN = "admin_item";
				$item = $id;
				$department = $inputDepartment;
				$continueScript = FALSE;
			}
		}
	}
				
		
		
	/*	
	** Add item record.
	*/
	if ($continueScript)
	{
		$Query = "INSERT INTO item (Name, Description, Keywords, " .
			"Thumbnail, Graphic, LargeGraphic, DisplayPrecedence, " . 
			"Active) ";
		$Query .= "VALUES (";
		$Query .= "'" . addslashes($inputName) . "', ";
		$Query .= "'" . addslashes($inputDescription) . "', ";
		$Query .= "'" . addslashes($inputKeywords) . "', ";
		$Query .= "'" . addslashes($inputThumbnail) . "', ";
		$Query .= "'" . addslashes($inputGraphic) . "', ";
		$Query .= "'" . addslashes($inputLargeGraphic) . "', ";
		$Query .= intval($inputDisplayPrecedence) . ", ";
		$Query .= "'" . addslashes($inputActive) . "')";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $Query";
			$continueScript = FALSE;
		}
		else
		{
			$newID = mysql_insert_id($DatabaseLink);
		}
	}
	
	
			
	/*
	** If the item insertion went okay, we can associate it with departments.
	*/
	if ($continueScript)
	{
		for($index = 0; $index < count($inputDepartment); $index++)
		{
			$Query = "INSERT INTO department_item VALUES (";
			$Query .= intval($inputDepartment[$index]) . ", ";
			$Query .= intval($newID) . ")";
			if(!mysql_query($Query, $DatabaseLink))
			{
				$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
					"   Query was: $Query";
				$continueScript = FALSE;
			}
		}
	}

			

	/*
	** Send them to the appropriate place (if an error occurred, $SCREEN will
	** already have been set).
	*/
	if ($continueScript)
	{
		$ActionResults[] = L_ADDITEM_SUCCESS;
		$SCREEN = "add_sku";
		$item = $newID;
	}

?>
