<?php
	/*
	** File: ADD_DEPARTMENT
	** Description: Adds a department to the database
	** Version: $Revision: 1.9 $
	** 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:16 $
	**
	** Log : $Log: ADD_DEPARTMENT,v $
	** Log : Revision 1.9  1999/12/09 23:23:16  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.8  1999/11/18 18:42:13  leon
	** Log : Missing confirmation message added
	** Log :
	** Log : Revision 1.7  1999/11/09 21:47:10  leon
	** Log : More localization.
	** Log :
	** Log : Revision 1.6  1999/08/18 22:31:44  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.5  1999/08/03 00:28:05  jj
	** Log : Added stricter error checking and more friendly SCREEN redirection.
	** Log :
	** Log : Revision 1.4  1999/07/28 19:27:10  leon
	** Log : Mass update based on code donation from a live project
	** Log :
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	**
	** Expected input: $inputName, $inputParent, $inputGraphic,
	**   $inputDescription, $inputDisplayPrecedence
	*/

	$continueInsertion = TRUE;
	
	/* 
	** Make sure the user has admin rights. 
	*/
	if( !$UserInfo["Permission"]["Administrate"] )
	{
		$ActionResults[] = L_ADDDEPT_DENIED;
		$continueInsertion = FALSE;
	}
	
	
	
	/*
	** Validate input as much as possible.
	*/
	if ($continueInsertion)
	{
		if ($inputName == "")
		{
			$ActionResults[] = L_ADDDEPT_NEEDNAME;
			$continueInsertion = FALSE;
		}
	}
		
		
		
	/*
	** Make sure that this is not a repeat entry.
	*/
	if ($continueInsertion)
	{
		$checkQuery = "SELECT ID FROM department WHERE ";
		$checkQuery .= "Name = '" . addslashes($inputName) . "' ";
		$checkQuery .= "AND Parent = " . intval($inputParent);
		if ( !($checkRecords = mysql_query($checkQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $checkQuery";
			$continueInsertion = FALSE;
		}
		else
		{
			/* If there's even one record, this is a repeat. */
			if (mysql_num_rows($checkRecords))
			{
				$ActionResults[] = L_ADDDEPT_DUPLICATE;
				$continueInsertion = FALSE;
			}
		}
	}
	
	
	
	/*
	** Go ahead and do the insertion.
	*/
	if ($continueInsertion)
	{
		$Query = "INSERT INTO department (Name, Parent, Description, " . 
			"DisplayPrecedence) ";
		$Query .= "VALUES (";
		$Query .= "'" . addslashes($inputName) . "', ";
		$Query .= intval($inputParent) . ", ";
		$Query .= "'" . addslashes($inputDescription) . "', ";
		$Query .= intval($inputDisplayPrecedence) . ")";
		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $Query";
			$continueInsertion = FALSE;
		}
		else
		{
			$ActionResults[] = L_ADDDEPT_SUCCESS;
		}
	}
	
	
	
	/*
	** Get the department number that has been assigned to us, that way we 
	** can allow the user to immediately begin adding items or sub-departments.
	*/
	if ($continueInsertion)
	{
		$idQuery = "SELECT ID FROM department WHERE ";
		$idQuery .= "Name = '" . addslashes($inputName) . "' ";
		$idQuery .= "AND Parent = " . intval($inputParent);
		if ( !($idRecords = mysql_query($idQuery, $DatabaseLink)) )
		{
			$ActionResults[] = mysql_errno() . ": " . mysql_error() . 
				"   Query was $idQuery";
			$continueInsertion = FALSE;
		}
		else
		{
			if (mysql_num_rows($idRecords) != 1)
			{
				$ActionResults[] = "Hmm, I though I inserted 1 unique record. " . 
					"Something must be broken here in ADD_DEPARTMENT.";
				$continueInsertion = FALSE;
			}
			else
			{
				list($department) = mysql_fetch_row($idRecords);
			}
		}
	}
		
		
		
	/*
	** Send the user to the right screen based on his success in this 
	** action module.
	*/
	if ($continueInsertion)
	{
		$SCREEN = "admin_departments";
		/* $department is set. */
	}
	else
	{
		$SCREEN = "add_department";
		$department = $inputParent;
	}
	
?>
