<?php
	/*
	** File: set_relationships
	** Description: interface for setting up item relationships
	** Version: $Revision: 1.4 $
	** Created: 9/28/99
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/07 21:45:11 $
	** Log : $Log: set_relationships,v $
	** Log : Revision 1.4  1999/12/07 21:45:11  leon
	** Log : All forms should be using StartForm now...
	** Log :
	** Log : Revision 1.3  1999/10/13 22:19:08  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.2  1999/09/29 23:06:47  leon
	** Log : Completed Admin side of item to item relationships.
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	// Make sure the user has admin rights.
	if(!$UserInfo["Permission"]["Administrate"])
	{
		print(L_SETRELATIONSHIPS_DENIED . "<BR>\n");
	}
	else
	{
	
		//get relationship types
		$Query = "SELECT ID, Name ";
		$Query .= "FROM relationship ";
		$Query .= "ORDER BY DisplayPrecedence, Name ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$Relationship_ID[] = $DatabaseRow[0];
			$Relationship_Name[] = $DatabaseRow[1];
		}
		$RelationshipCount = count($Relationship_ID);
	
	
		//get relationships for this item
		$Query = "SELECT Related_Item, Relationship ";
		$Query .= "FROM item_item ";
		$Query .= "WHERE Item=$item ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$RelatedItem[($DatabaseRow[0])][($DatabaseRow[1])] = TRUE;
		}
	
	
		//get departments and items
		//if an item appears in more than one department,
		//then it will just get placed in one
		$Query = "SELECT i.ID, d.Name, i.Name ";
		$Query .= "FROM department d, department_item di, item i ";
		$Query .= "WHERE d.ID = di.Department ";
		$Query .= "AND di.Item = i.ID ";
		$Query .= "ORDER BY d.DisplayPrecedence, d.Name, i.DisplayPrecedence, i.Name ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			if(!isset($ItemInfo[($DatabaseRow[0])]['Department']))
			{
				$ItemInfo[($DatabaseRow[0])]['Department'] = $DatabaseRow[1];
				$ItemInfo[($DatabaseRow[0])]['Name'] = $DatabaseRow[2];
			}
		}
	
	
		print("<TABLE BORDER=\"0\" CELLPADDING=\"5\" CELLSPACING=\"0\">\n");
	
		print(StartForm('admin_item', 'post', 'SET_RELATIONSHIPS', FALSE, 
			array('item'=>$item,'department'=>$department)));
		
	
		$LastDepartment = "";
		for(reset($ItemInfo); $key = key($ItemInfo); next($ItemInfo))
		{
			if($LastDepartment != $ItemInfo[$key]['Department'])
			{
				print("<TR BGCOLOR=\"".$SiteColor['Highlight1']."\">\n");
	
				//department header
				print("<TD><B>".$ItemInfo[$key]['Department']."</B></TD>\n");
	
				//guides for checkboxes
				for($n=0; $n<$RelationshipCount; $n++)
				{
					print("<TD>$Relationship_Name[$n]</TD>\n");
				}
	
				print("</TR>");
				$LastDepartment = $ItemInfo[$key]['Department'];
			}
			
			
			//show item
			print("<TR BGCOLOR=\"".$SiteColor['Highlight2']."\">\n");
	
			print("<TD>".$ItemInfo[$key]['Name']."</TD>\n");
			
			for($n=0; $n<$RelationshipCount; $n++)
			{
				print("<TD ALIGN=\"center\">");
				print("<INPUT TYPE=\"checkbox\" ");
				print("NAME=\"input_Relationship[]\" ");
				print("VALUE=\"$key ".$Relationship_ID[$n]."\"");
				if($RelatedItem[$key][($Relationship_ID[$n])])
				{
					print(" CHECKED");
				}
				print(">");
				print("</TD>\n");
			}
					
			print("</TR>");
			
		}
	
		print("<TR BGCOLOR=\"".$SiteColor['Highlight1']."\">\n");
		print("<TD COLSPAN=\"".($RelationshipCount+1)."\">");
		print("<INPUT TYPE=\"submit\" VALUE=\"" . L_SETRELATIONSHIPS_UPDATERELATIONSHIPS . "\">");
	
		print(" <A HREF=\"".ScreenURL('admin_item', FALSE, array('item'=>$item,'department'=>$department))."\">" . L_SETRELATIONSHIPS_CANCEL . "</A>\n");
		print("</TD>");
		print("</TR>");
	
		print("</FORM>\n");
	
		print("</TABLE>\n");
			
	}
?>
