<?php

function ReRootCompareStrings($a,$b)
{
	$hits = 0;
	$abits = explode(' ',$a);
	$bbits = explode(' ',$b);
	if (count($abits) != count($bbits)) return false;
	
	for($x=0;$x<count($abits);$x++)
	{
		if ($abits[$x] == $bbits[$x])
		{
			$hits++;
		}
	}
	if ($hits >= count($abits)-1) return true;
	return false;
}


function fn_reroot_aaa($aql)
{
	global $db;
	
	$list = $aql->Consume(3);
	$set = array();	
	
	if (is_array($list[1]->value))
		$from = $list[1]->value[0];
	else
		$from = $list[1]->value;
	
	//print_r($from);
	
	if (is_array($list[0]->value))
		$to = $list[0]->value[0];
	else
		$to = $list[0]->value;	
	
	$toworkon = array();
	foreach($list[2]->value as $l)
	{
		if ($l == $from)
			$set[] = $to;
		else
			$toworkon[] = $l;
	}
	
	$loadedassets = array();
	
	foreach($toworkon as $ele)
	{
		//Check for any relationships with the FROM asset...
		$query = $db->query("SELECT relationship,childasset,depth FROM assetrelclosures WHERE parentasset=".$from." and childasset=".$ele);		
		$hits = array();
		while($row = $query->fetch(PDO::FETCH_ASSOC)) 
		{
			$hits[] = array($row['relationship'],true);		
		}
		
		if (count($hits) == 0)
		{
			$query = $db->query("SELECT relationship,parentasset,depth FROM assetrelclosures WHERE childasset=".$ele." and parentasset=".$from);		
			$hits = array();
			while($row = $query->fetch(PDO::FETCH_ASSOC)) 
			{
				$hits[] = array($row['relationship'],false);	
			}
		}
		
		global $asset;
		
		if (count($hits) == 0)
		{
			$set[] = $ele;
		}
		else
		{
			#echo $ele.' Connected To '.$from.' via '.$hits[0][0]."<br/>";
			
			$relno = $hits[0][0];
			
			if (!isset($loadedassets[$to]))
				$loadedassets[$to] = new Asset($to);
						
			foreach($hits as $ht)
			{
				if (isset($asset[$to]['relationships'][$ht[0]]))
				{
					$relno = $ht[0];
					$direction = $ht[1];
					break;
				}
			}
			
			//Figuring out route back to parent....
			if (!isset($loadedassets[$ele]))
				$loadedassets[$ele] = new Asset($ele);
			
			$activepath = "";
			
			$paths = [$loadedassets[$ele]->name];
			$toexplore = [$ele];
			$explored = array();
			while(count($toexplore) > 0)
			{
				$ex = array_pop($toexplore);
				$path = array_pop($paths);
				
				if (in_array($ex,$explored))
					continue;
				
				if (!isset($loadedassets[$ex]))
					$loadedassets[$ex] = new Asset($ex);
				
				//echo '  Checking '.$loadedassets[$ex]->name."<br/>";
				
				$matched = false;
				foreach($asset[$ex]['relationships'][$relno] as $r)
				{
					if ($r['upstream'] == $direction)
					{
						if ($r['target'] == $from)
						{
							//echo 'Found!';
							$matched = true;
							$activepath = $path;
							break;
						}
						else
						{
							$toexplore[] = $r['target'];
							$paths[] = $path.":".$loadedassets[ex]->name;
						}
					}
				}
				if ($matched == true)				
					break;
			}
			
			//print("Found Hits Along...");
			//print_r($hits);		
			
			
			
			$bits = explode(':',$activepath);
			//$direction = $hits[0][1];
			
			$pieces = array_reverse($bits);
			if ($direction == true)
			{
				$newdir = false;
			}
			else
			{
				$newdir = true;
			}
			//Search Downstream
			$current = $to;
			$piececount = 0;
			
			if (!isset($loadedassets[$current]))
				$loadedassets[$current] = new Asset($current);
			
			#print("Checking Asset ".$current.' - '.$loadedassets[$current]->name.' on relationship '.$relno);
			#print_r($asset[$current]);
			
			if (isset($asset[$current]['relationships'][$relno]))
			{
				foreach($asset[$current]['relationships'][$relno] as $r)
				{
					if ($r['upstream'] == $newdir)
					{
						$target = $r['target'];
						if (!isset($loadedassets[$target]))
							$loadedassets[$target] = new Asset($target);
						
						//Names are close...
						#print("Comparing ".$loadedassets[$target]->name.' to '.$pieces[$piececount]);
						if (ReRootCompareStrings($loadedassets[$target]->name,$pieces[$piececount]))
						{
							$piececount++;
							if ($piececount >= count($pieces))
							{
								$set[] = $target;
								break;
							}
						}
					}
				}
			}
			
			
			
		}
	}
	
	$a = new AQLStackElement();
	$a->typename = "assetlist";
	$a->islist = true;
	$a->value = $set;
	array_push($aql->stack,$a);
}
?>