<?php
	/*
	** File: department_selector
	** Description: functions for building a selector of departments
	** Version: 1.0
	** Created: 04/08/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/10/11 23:04:03 $
	**
	** Log : $Log: department_selector,v $
	** Log : Revision 1.7  1999/10/11 23:04:03  leon
	** Log : Removed all require()'s, added block to stop double include()'s
	** Log :
	** Log : Require wasn't giving any performance advantage because it was always
	** Log : being called with a variable.  Futhermore, using return inside a
	** Log : require()'d file does not work in PHP4.
	** Log :
	** Log : Revision 1.6  1999/09/24 00:07:51  jj
	** Log : Added new constant ITEMS_IN_MULTIPLE_DEPARTMENTS in global_settings so that items may or may not belong to multiple departments. Got rid of deprecated function DepartmentSelectorForItem.
	** Log :
	** Log : Revision 1.5  1999/08/18 22:32:04  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.4  1999/08/13 18:20:01  leon
	** Log : Added interface for setting sales prices per the requirements specification.
	** Log : (See "Add promotion/discount offers").  Since admin_sales needs to use
	** Log : DepartmentSelector, I tweaked it a bit.
	** Log :
	** Log : Revision 1.3  1999/07/28 19:27:21  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	/*
	** Ensure that module isn't included twice
	*/

	$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 DepartmentOptions($root, $level, $selected, $multiple=TRUE)
	{
		global $DatabaseLink;

	
		$Query = "SELECT ID, Name ";
		$Query .= "FROM department ";
		$Query .= "WHERE Parent=$root ORDER BY DisplayPrecedence";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		while($DatabaseRow = mysql_fetch_row($DatabaseResult))
		{
			$department_ID = $DatabaseRow[0];
			$department_Name = $DatabaseRow[1];
	
			print("<OPTION VALUE=\"$department_ID\"");
			if( (is_array($selected) && 
				isset($selected[$department_ID]) &&
				($selected[$department_ID])) || 
				($department_ID == $selected))
			{
				print(" SELECTED");
			}
			print(">");
			for($index=0; $index < $level; $index++)
			{
				print("&nbsp;&nbsp;");
			}
			print("$department_Name\n");
			
			DepartmentOptions($department_ID, $level+1, $selected);
		}
		
		
	}

	function DepartmentSelector($name, $selected=0, $multiple=TRUE, $size=5)
	{
		print("<SELECT NAME=\"$name\"");
		if($multiple)
		{
			print(" MULTIPLE");
		}
		print(" SIZE=\"$size\"");
		print(">\n");
		
		//I'm not sure why this ended up here, but since there can't
		//be any department with an ID of zero, I'm taking it out.
		//--Leon
		//print("<OPTION VALUE=\"0\">None");
		
		DepartmentOptions(0, 0, $selected);
		
		print("</SELECT>\n");
	}

?>
