<?php

/**
* getLookupMainTableSettings - tests whether the lookup link exists between the tables
*
*  returns array with ProjectSettings class for main table if the link exists in project settings.
*  returns NULL otherwise
*/
function getLookupMainTableSettings($lookupTable, $mainTableShortName, $mainField, $desiredPage = "")
{
	global $lookupTableLinks;
	if(!isset($lookupTableLinks[$lookupTable]))
		return null;
	if(!isset($lookupTableLinks[$lookupTable][$mainTableShortName.".".$mainField]))
		return null;
	$arr = &$lookupTableLinks[$lookupTable][$mainTableShortName.".".$mainField];
	$effectivePage = $desiredPage;
	if(!isset($arr[$effectivePage]))
	{
		$effectivePage = PAGE_EDIT;
		if(!isset($arr[$effectivePage]))
		{
			if($desiredPage == "" && 0 < count($arr))
			{
				$effectivePage = $arr[0];
			}
			else
				return null;
		}
	}
	return new ProjectSettings($arr[$effectivePage]["table"], $effectivePage);
}

/** 
* $lookupTableLinks array stores all lookup links between tables in the project
*/
function InitLookupLinks()
{
	global $lookupTableLinks;

	$lookupTableLinks = array();

		if( !isset( $lookupTableLinks["items"] ) ) {
			$lookupTableLinks["items"] = array();
		}
		if( !isset( $lookupTableLinks["items"]["spare_parts.NAME"] )) {
			$lookupTableLinks["items"]["spare_parts.NAME"] = array();
		}
		$lookupTableLinks["items"]["spare_parts.NAME"]["edit"] = array("table" => "spare_parts", "field" => "NAME", "page" => "edit");
		if( !isset( $lookupTableLinks["categories"] ) ) {
			$lookupTableLinks["categories"] = array();
		}
		if( !isset( $lookupTableLinks["categories"]["ac.CATEGORY"] )) {
			$lookupTableLinks["categories"]["ac.CATEGORY"] = array();
		}
		$lookupTableLinks["categories"]["ac.CATEGORY"]["edit"] = array("table" => "ac", "field" => "CATEGORY", "page" => "edit");
		if( !isset( $lookupTableLinks["types"] ) ) {
			$lookupTableLinks["types"] = array();
		}
		if( !isset( $lookupTableLinks["types"]["ac.TYPE"] )) {
			$lookupTableLinks["types"]["ac.TYPE"] = array();
		}
		$lookupTableLinks["types"]["ac.TYPE"]["edit"] = array("table" => "ac", "field" => "TYPE", "page" => "edit");
		if( !isset( $lookupTableLinks["btu"] ) ) {
			$lookupTableLinks["btu"] = array();
		}
		if( !isset( $lookupTableLinks["btu"]["ac.BTU/HR"] )) {
			$lookupTableLinks["btu"]["ac.BTU/HR"] = array();
		}
		$lookupTableLinks["btu"]["ac.BTU/HR"]["edit"] = array("table" => "ac", "field" => "BTU/HR", "page" => "edit");
}

?>