<?php
	/*
	** File: UPDATE_DEPARTMENT
	** Description: update information about a department
	** Version: $Revision: 1.8 $
	** Created: 04/13/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_DEPARTMENT,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/02 19:09:45  leon
	** Log : Localized the UPDATE action modules
	** Log :
	** Log : Revision 1.6  1999/08/30 17:36:56  jj
	** Log : No longer allowing the admin to create infinite loops in the department structure via setting a department's parent = to one of its descendents.
	** Log :
	** Log : Revision 1.5  1999/08/18 22:31:54  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"] )
	{
		print(L_UPDATEDEPT_DENIED . "<BR>\n");
		$continueScript = FALSE;
	}
	
	
	
	/*
	** Make sure we have some form of valid data.
	*/
	if ($continueScript)
	{
		if ( !isset($department) || $department<=0 )
		{
			$ActionResults[] = "I can't figure out which department you " . 
				"are trying to change. That's a weird bug.";
			$continueScript = FALSE;
		}
		else if ( !isset($inputName) || $inputName=="" )
		{
			$ActionResults[] = L_UPDATEDEPT_NEEDNAME;
			$continueScript = FALSE;
		}
	}
	
	
	
	/*
	** Let's go ahead and get most of the data from the department table.
	** We'll need it later, and getting it here allows us to trim off a
	** database access.
	*/
	if ($continueScript)
	{
		$query = "SELECT ID, Name, Parent FROM department";
		$databaseResults = mysql_query($query, $DatabaseLink);
		
		/* 
		** Organize them: note that we're indexing by the integral value of $id.
		*/
		while (list($id, $name, $parent) = mysql_fetch_row($databaseResults))
		{
			$departmentsArray[intval($id)] = array(
				"NAME" => $name, 
				"PARENT" => $parent);
		}
	}	



	/*
	** Make sure that they aren't changing the name to be the same as some 
	** other department. 
	*/
	if ($continueScript)
	{
		for (reset($departmentsArray) ; $key = key($departmentsArray) ; 
			next($departmentsArray))
		{
			if (($departmentsArray[$key]["NAME"] == $inputName)
				&& ($key != $department))
			{
				/* Same name, different ID => invalid change. */
				$ActionResults[] = L_UPDATEDEPT_DUPLICATE;
				$continueScript = FALSE;
			}
		}
	}
	
	
	
	/*
	** Let's make sure that they aren't setting their parent in such a manner
	** as to create an infinite loop (for instance, by setting their parent
	** = to their grandchild). This is the "data integrity" thing that I 
	** mentioned earlier.
	*/
	if ($continueScript)
	{
		/* 
		** Here's the algorithm: Given $inputParent and $departmentsArray,
		** trace $inputParent's ancestry all the way back to department 0. 
		** If at any point we hit $department, then the user is trying set
		** this department's parent = to one of this departments descendents
		** (thus creating an infinite loop).
		*/
		for ($tempId = $inputParent ; $tempId != 0 && $tempId != $department ; 
			$tempId = $departmentsArray[intval($tempId)]["PARENT"])
		{/* nothing to do */} 
		if ($tempId == $department)
		{
			$ActionResults[] = L_UPDATEDEPT_PARENT;
			$continueScript = FALSE;
		}
	}



	/*
	** Okay, let's go ahead and do the update.
	*/
	if ($continueScript)
	{
		$Query = "UPDATE department ";
		$Query .= "SET Name='" . addslashes($inputName) . "', ";
		$Query .= "Graphic='" . addslashes($inputGraphic) . "', ";
		$Query .= "Parent=" . intval($inputParent) . ", ";
		$Query .= "Description='" . addslashes($inputDescription) . "', ";
		$Query .= "DisplayPrecedence=" . intval($inputDisplayPrecedence) . " ";
		$Query .= "WHERE ID=" . intval($department);
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was: $Query";			
		}
		else
		{
			$ActionResults[] = L_UPDATEDEPT_SUCCESS;
		}
	}
	
	
	
	/*
	** Send the user back to the most appropriate place.
	*/
	$SCREEN = "admin_departments";
	/* $department is set. */
	
?>
