<?php
	/*
	** File: admin_item
	** Description: Everything (hopefully) the admin ever wanted for
	**   administrating items (including access to the skus).
	** Version: $Revision: 1.16 $
	** 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/07 21:45:06 $
	** Log : $Log: admin_item,v $
	** Log : Revision 1.16  1999/12/07 21:45:06  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.15  1999/11/08 22:32:22  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.14  1999/11/04 21:35:38  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.13  1999/10/22 21:59:05  jj
	** Log : Added link to item screen (useful here).
	** Log :
	** Log : Revision 1.12  1999/10/19 18:20:06  jj
	** Log : Inactive skus are now shown in brakets (to make them more noticeable).
	** Log :
	** Log : Revision 1.11  1999/10/12 23:14:47  leon
	** Log : Moved localization constants into 'english' module.  Added constant to
	** Log : control whether unregistered shoppers may checkout.
	** Log :
	** Log : Revision 1.10  1999/10/12 00:39:07  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.9  1999/10/01 01:25:32  jj
	** Log : Used <UL>'s to make the page look a bit nicer.
	** Log :
	** Log : Revision 1.8  1999/09/29 23:06:46  leon
	** Log : Completed Admin side of item to item relationships.
	** Log :
	**
	** Log : Revision 1.7  1999/09/29 18:43:46  jj
	** Log : Rebuilt admin_departments to use an expandable tree-like GUI.
	** Log :
	** Log : Revision 1.6  1999/09/28 20:44:10  leon
	** Log : Preliminary changes to support relationships.  It's not all ready yet.
	** Log :
	** Log : Revision 1.5  1999/09/24 00:06:43  jj
	** Log : Used ITEMS_IN_MULTIPLE_DEPARTMENTS so that items may or may not belong to multiple departments. Also, got rid of any reference to DepartmentSelectorForItem, since it has been deprecated.
	** Log :
	** Log : Revision 1.4  1999/08/18 22:32:12  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.3  1999/08/04 16:13:37  jj
	** Log : Changed all instances of skues to skus (but not in comments or variable names).
	** Log :
	** Log : Revision 1.2  1999/08/03 22:50:57  jj
	** Log : Doing quality control on department and items editors.
	** Log :
	** Log : Revision 1.1  1999/08/03 00:26:39  jj
	** Log : A large amount of programming changing the whole department and item admin process.
	** Log :
	** Log : Revision 1.5  1999/07/28 19:27:31  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	**
	** Expected Input: $item, $department
	*/

	/* We'll need this for DepartmentSelectorForItem(). */
	include(APPLICATION_ROOT . "/modules/include/department_selector");

	/* We need this to draw pretty boxes */
	include(APPLICATION_ROOT . "/modules/include/box");


	/* Our error flag. */
	$continueScript = TRUE;
	


	/* Make sure the user has admin rights. */
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		print(L_ADMINITEM_DENIED . "<BR>\n");
		$continueScript = FALSE;
	}
	else if ($item <= 0)
	{
		print("Either the $item variable was missing, or it had an invalid value.<BR>\n");
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Get the main information for this item.
	*/
	if ($continueScript)
	{
		$item = intval($item);
		$Query = "SELECT Name, Description, Keywords, Thumbnail, " . 
			"Graphic, LargeGraphic, DisplayPrecedence, Active ";
		$Query .= "FROM item ";
		$Query .= "WHERE ID=$item";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;	
		}
		else
		{
			if ( !($DatabaseRow = mysql_fetch_row($DatabaseResult)) )
			{
				print("Item $item does not exist.<BR>\n");
				$continueScript = FALSE;
			}
			else
			{
				$itemName = $DatabaseRow[0]; 
				$itemDescription = $DatabaseRow[1];
				$itemKeywords = $DatabaseRow[2];
				$itemThumbnail = $DatabaseRow[3];
				$itemGraphic = $DatabaseRow[4];
				$itemLargeGraphic = $DatabaseRow[5];
				$itemDisplayPrecedence = $DatabaseRow[6];
				$itemActive = $DatabaseRow[7];
			}
		}
	}



	/*
	** Get list of departments to which this item belongs.
	*/
	if ($continueScript)
	{
		$Query = "SELECT Department ";
		$Query .= "FROM department_item ";
		$Query .= "WHERE Item = $item ";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;	
		}		
		else
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$department_ID = $DatabaseRow[0];
				$itemDepartment[$department_ID] = TRUE;
			}
		}
	}



	/*
	** Start by giving the user some navigation links, and telling him where
	** he is.
	*/
	if ($continueScript)
	{
		/* Wrap this section in a nice border, and output the heading. */
		startBox();
		
		print("<B>" . L_ADMINITEM_EDITINGITEM . "</B> " . prepareText($itemName) . "<BR>\n");

		/* Give navigational links. */
		print("<UL>\n");
		print("<LI><A HREF=\"" . ScreenURL("admin_menu") . 
			"\">" . L_ADMINITEM_RETURNTOADMINMENU . "</A>\n");
		print("<LI><A HREF=\"" . ScreenURL("admin_departments") . 
			"&department=$department" . 
			"&expandDepartments=0|$department" . 
			"\">" . L_ADMINITEM_RETURNTOITEMSDEPARTMENT . "</A>\n");
		print("</UL>\n");

		/* Finish up this section. */
		endBox();
		
		print("<BR>\n");
	}
	
	
	
	/* 
	** Give the user the list of skus for this item. 
	*/
	if ($continueScript)
	{
		/* Wrap this section in a nice border, and output the heading. */
		startBox();

		print("<B>" . L_ADMINITEM_LISTOFSKUS . "</B> &nbsp;\n");
		print("<A HREF=\"" . ScreenURL("add_sku") . "&item=$item\">" . 
			L_ADMINITEM_ADDSKU . "</A><BR>\n");

		/* Get the list of skus from the database and output them. */
		$Query = "SELECT ID, ExternalSKU, Name, Active ";
		$Query .= "FROM sku ";
		$Query .= "WHERE Item = $item ";
		$Query .= "ORDER BY DisplayPrecedence, Name ";
		if ( !($DatabaseResult = mysql_query($Query, $DatabaseLink)) )
		{
			print("<!-- " . mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query -->\n");
			$continueScript = FALSE;	
		}
		else
		{
			print("<UL>\n");
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				$sku_ID = $DatabaseRow[0];
				$sku_ExternalSKU = $DatabaseRow[1];
				$sku_Name = $DatabaseRow[2];
				$sku_Active = $DatabaseRow[3];

				print("<LI>");
				if ($sku_Active == "N") print("["); /* Leon will KILL me! :) */
				print("<A HREF=\"".ScreenURL("edit_sku")."&sku=$sku_ID\">");
				if (isset($sku_ExternalSKU) && $sku_ExternalSKU!="")
				{
					print("(" . prepareText($sku_ExternalSKU) . ") ");
				}
				print(prepareText($sku_Name));
				print("</A> ");
				if ($sku_Active == "N") print("]");
				print("<BR>\n");
			}
			print("</UL>\n");
		}
		
		/* Finish up this section. */
		endBox();
		
		print("<BR>\n");
	}



	/*
	** Give the admin the main information for this item.
	*/
	if ($continueScript)
	{
		/* Wrap this section in a nice border, and output the heading. */
		startBox();
		
		/* Start the form and the table that will organize it. */
		print(StartForm("admin_item", 'post', 'UPDATE_ITEM', FALSE, 
			array("item"=>$item)));

	
		print("<TABLE>\n");
		print("<TR><TD><B>" . L_ADMINITEM_MAINITEMINFO . "</B></TD>\n");
		print("<TD ALIGN=\"right\"><A HREF=\"" . ScreenURL("item") . "&item=$item" . 
			"\">" . L_ADMINITEM_VIEWITEM . "</A></TD></TR>\n");
			
		/* Output the form entities. */
		print("<TR><TD>" . L_ADMINITEM_NAME . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputName\" SIZE=\"32\" ");
		print("VALUE=\"" . prepareText($itemName) . "\"></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_DEPARTMENT . "</TD>\n<TD>");
		DepartmentSelector("inputDepartment[]", $itemDepartment, 
			ITEMS_IN_MULTIPLE_DEPARTMENTS);
		print("</TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_DESCRIPTION . "</TD>\n");
		print("<TD><TEXTAREA NAME=\"inputDescription\" WRAP=\"soft\" ");
		print("ROWS=\"3\" COLS=\"40\">");
		print(prepareText($itemDescription));
		print("</TEXTAREA></TD></TR>");

		print("<TR><TD>" . L_ADMINITEM_KEYWORDS . "</TD>\n");
		print("<TD><TEXTAREA NAME=\"inputKeywords\" WRAP=\"soft\" ROWS=\"3\" ");
		print("COLS=\"40\">");
		print(prepareText($itemKeywords));
		print("</TEXTAREA></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_THUMBNAIL . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputThumbnail\" SIZE=\"32\" ");
		print("VALUE=\"$itemThumbnail\"></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_GRAPHIC . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputGraphic\" SIZE=\"32\" ");
		print("VALUE=\"$itemGraphic\"></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_LARGEGRAPHIC . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputLargeGraphic\" SIZE=\"32\" ");
		print("VALUE=\"$itemLargeGraphic\"></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_DISPLAYPRECEDENCE . "</TD>\n");
		print("<TD><INPUT TYPE=\"text\" NAME=\"inputDisplayPrecedence\" ");
		print("SIZE=\"2\"  VALUE=\"$itemDisplayPrecedence\"></TD></TR>\n");

		print("<TR><TD>" . L_ADMINITEM_ACTIVE . "</TD>\n");
		print("<TD><SELECT NAME=\"inputActive\">");
		print("<OPTION VALUE=\"Y\"");
		if($itemActive=="Y")
		{
			print(" SELECTED");
		}
		print(">Yes");
		print("<OPTION VALUE=\"N\"");
		if($itemActive=="N")
		{
			print(" SELECTED");
		}
		print(">No");
		print("</SELECT></TD></TR>\n");

		print("<TR>\n");
		print("<TD><INPUT TYPE=\"submit\" VALUE=\"" . L_ADMINITEM_UPDATEPRODUCT . "\">\n");
		print("<INPUT TYPE=\"reset\" VALUE=\"" . L_ADMINITEM_RESET . "\"></TD>\n");
		
		print("<TD ALIGN=\"right\">");
		print("<A HREF=\"" . ScreenURL("admin_menu") . 
			"&item=$item&department=$department&ACTION=DELETE_ITEM\">");
		print(L_ADMINITEM_DELETEITEM);
		print("</A>");
		print("</TD>\n");
		print("</TR>\n");
		
		/* Finish the inner table and the form. */
		print("</TABLE>\n");
		print("</FORM>\n");
		
		/* Finish up this section. */
		endBox();
		
		print("<BR>\n");


		/*
		** box showing relationships
		*/
		startBox();
		print("<B>Relationships</B>");
		
		print(" <A HREF=\"".ScreenURL('set_relationships', FALSE, array('item'=>$item, 'department'=>$department))."\">");
		print(L_ADMINITEM_EDIT);
		print("</A>");
		
		print("<BR>\n<BR>\n");

		$Query = "SELECT i.Name, r.Name ";
		$Query .= "FROM relationship r, item_item ii, item i ";
		$Query .= "WHERE r.ID = ii.Relationship ";
		$Query .= "AND ii.Item = $item ";
		$Query .= "AND ii.Related_Item = i.ID ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		if(mysql_numrows($DatabaseResult) > 0)
		{
			while($DatabaseRow = mysql_fetch_row($DatabaseResult))
			{
				print($DatabaseRow[0]);
				print("<FONT SIZE=\"1\">(" . $DatabaseRow[1] . ")</FONT><BR>\n ");
			}

		}
		else
		{
			print("<I>". L_ADMINITEM_NONE . "</I>");
		}

		endBox();
		print("<BR>\n");

	}


?>
