<?php

function _joinAdditionalFields($custom_fields_table, $student_fields_join_categories, $fieldsArray, $what = 'TITLE,ID,TYPE', $options = false)
{

	if(!$options)
	{
		$opt1 = array();
		$opt2 = array('CATEGORY_ID');
	}	
	else 
	{
		$opt1 = array('CATEGORY_ID'=>'_make','SEARCH'=>'_make','DISPLAY'=>'_make');
		$opt2 = array('CATEGORY_ID');
	}
	
	/*
	$additionalFieldsArray = db_request(db_execute("SELECT ".$what." ,sfjc.CATEGORY_ID
				 FROM ".$custom_fields_table." cf,
				 ".$student_fields_join_categories." sfjc
		WHERE ID IN (
				sfjc.FIELD_ID
				)"), $opt1, $opt2);
	*/

//	$additionalFieldsArray = db_request(db_execute("SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
//									ON sfjc.FIELD_ID = cf.ID AND sfjc.CATEGORY_ID = cf.CATEGORY_ID WHERE cf.FOR_APPLICANTS='Y';"), $opt1, $opt2);
	$additionalFieldsArray = db_request(db_execute("SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
									ON sfjc.FIELD_ID = cf.ID AND sfjc.CATEGORY_ID = cf.CATEGORY_ID WHERE cf.ID IN (sfjc.FIELD_ID)"), $opt1, $opt2);
//
//	echo "SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
//									ON sfjc.FIELD_ID = cf.ID AND sfjc.CATEGORY_ID = cf.CATEGORY_ID WHERE cf.ID IN (sfjc.FIELD_ID)";
//	var_dump($additionalFieldsArray);
//	die();
	if($additionalFieldsArray)
	{
		foreach($additionalFieldsArray as $catKey => $items)
		{
			if(array_key_exists($catKey,$fieldsArray))
			{
				foreach ($items as $item)
				{
					array_push($fieldsArray[$catKey], $item);
				}
			}
			else
			{
				$fieldsArray[$catKey] = array();
				foreach ($items as $item)
					array_push($fieldsArray[$catKey], $item);
			}
		}
	}
//	print_r($fieldsArray);	
	return $fieldsArray;
}

function _joinAppAddlFields($custom_fields_table, $student_fields_join_categories, $fieldsArray, $what = 'TITLE,ID,TYPE', $options = false)
{

	if(!$options)
	{
		$opt1 = array();
		$opt2 = array('CATEGORY_ID');
	}	
	else 
	{
		$opt1 = array('CATEGORY_ID'=>'_make','SEARCH'=>'_make','DISPLAY'=>'_make');
		$opt2 = array('CATEGORY_ID');
	}
//echo var_dump($opt2);
//var_dump($fieldsArray);
//	$additionalFieldsArray = db_request(db_execute("SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
//									ON sfjc.FIELD_ID = cf.ID AND sfjc.CATEGORY_ID = cf.CATEGORY_ID WHERE cf.FOR_APPLICANTS='Y'"),array(),array('CATEGORY_ID'));
	$additionalFieldsArray = db_request(db_execute("SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
									ON (sfjc.FIELD_ID = cf.ID) WHERE cf.FOR_APPLICANTS='Y'"),array(),array('CATEGORY_ID'));

//echo "SELECT cf.*,sfjc.CATEGORY_ID FROM ".$custom_fields_table." cf LEFT JOIN ".$student_fields_join_categories." sfjc 
//									ON sfjc.FIELD_ID = cf.ID AND sfjc.CATEGORY_ID = cf.CATEGORY_ID WHERE cf.FOR_APPLICANTS='Y'";
//	var_dump($additionalFieldsArray);
	if($additionalFieldsArray)
	{
		foreach($additionalFieldsArray as $catKey => $items)
		{
//			echo "-->".$catKey."<br>";
			if(array_key_exists($catKey,$fieldsArray))
			{
				foreach ($items as $item)
				{
//var_dump($item);				echo $item."<br><br><br><br>";
					array_push($fieldsArray[$catKey], $item);
				}
			}
			else
			{
				$fieldsArray[$catKey] = array();
				foreach ($items as $item)
					array_push($fieldsArray[$catKey], $item);
			}			
		}
	}
//	echo "count after: ".count($fieldsArray);
//	print_r($fieldsArray);	
	return $fieldsArray;
}

function _changeSortOrder($arr)
{
	if(!$arr) $arr = array();
	foreach($arr as $item)
	{
		$idArray[$item['ID']] = $item['SORT_ORDER'];
		$valuesArray[$item['ID']] = $item;
	}
	if(!$idArray) $idArray = array();
	asort($idArray, SORT_NUMERIC);
	
	$i = 1;
	foreach($idArray as $key => $sortorder)
	{
		$newArr[$i] = $valuesArray[$key];
		$i++;
	}
	
	return $newArr;
}

?>