<?php


function fn_aipointsl_a($aql)
{
	global $db;
	
	$list = $aql->Consume(1);
	$set = array();	
	
	$alist = $list[0]->value;
	if (!is_array($alist))
	{
		$alist = array($alist);
	}
	
	$allprops = Properties::GetAllProperties();
	//print_r($alist);
		
	$sa = count($alist);
	for($x=0;$x<$sa;$x++)
	{
		//Get all of the assets that are location parents of this one...
		$query = $db->query("SELECT parentasset FROM assetrelclosures WHERE childasset=".$alist[$x]." AND relationship=2");
					
		while($row = $query->fetch(PDO::FETCH_ASSOC)) 
		{
			if (!in_array($row['parentasset'],$alist))
			{
				$alist[] = $row['parentasset'];
			}
		}
		
	}

	$indx = -1;
	foreach($alist as $a)
	{
		$indx += 1;
		
		//Get all the points that belong to these assets.
		$ass = new Asset($a);
		$props = $ass->GetProperties();
		
		if ($indx >= $sa)
		{
			//All parents only include their LOCAL information, not their upstream data.
			$finalprops = array();
			foreach($props as $p)
			{
				if (($p->origintype == "local") || ($p->origintype == "template"))
				{
					$finalprops[] = $p;
				}
			}
			$props = $finalprops;
		}
		
		$ass->GetValues($props);
				
		if (count($props) > 0)
		{
			$ass->Fetch($props);
		}
		
		foreach($props as $p)
		{
			$propinfo = FALSE;
			foreach($allprops as $px)
			{
				if ($px->id == $p->id)
				{
					$propinfo = $px;
					break;
				}
			}
			
			//Skip all non-measurement channels.
			if (($propinfo !== FALSE) && ($propinfo->options !== FALSE))
			{
				if (in_array('Code',$propinfo->options )) continue;
				if (in_array('Analytic',$propinfo->options )) continue;
			}
			
			$px = new AQLPoint();
			$px->processed = True;
			$px->assetid = $a;
			if ($p->origintype == "template") $p->origin = $a;
			$px->sourceid = $p->origin;
			$px->propertyid = $p->id;
			
			$active = "text";
			if ($p->type == "MEASUREMENT")
				$active = "measurement";
			if ($p->type == "STATUS")
				$active = "state";
			if (($p->type == "ENUM") || ($p->type == "LOOKUP"))
				$active = "value";
			
			if (isset($p->value[$active])) 
			{
				$px->rawvalue = $p->value[$active];
				$px->value = $p->value[$active];
			}			
			else
			{
				if (count($p->value) == 1)
				{
					$px->rawvalue = $p->value[array_keys($p->value)[0]];
					$px->value = $px->rawvalue;
				}
				else
				{
					$st = "";
					foreach($p->value as $k => $v)
					{
						if ($k == "_dynamics") continue;
						if ($st != "") $st .= '|';
						$st .= $v;
					}
					$px->rawvalue = $st;
					$px->value = $px->rawvalue;
				}
			}
			
			if (($p->type == "ENUM") || ($p->type == "STATUS") || ($p->type == "LOOKUP"))
			{
				foreach($allprops as $pq)
				{
					if ($pq->id == $p->id)
					{
						$map = $pq->GetValueMap($p->value);
						if (isset($map[$px->value]))
						{
							$px->value = $map[$px->value];
						}
						else
						{
							$px->value = "Unknown";
						}
					}
				}
			}
			
			//print_r($p->value);
			if (!isset($p->value['_dynamics']))
			{
				//This function only returns DYNAMIC properties, not fixed ones.
				continue;
			}
			else
			{
				if (isset($p->value['_dynamics'][$active]))
				{
					$px->dynamic = $p->value['_dynamics'][$active];
				}
				if (in_array($active,$p->value['_dynamics']))
				{
					$px->dynamic = $p->origin.':'.$p->id.':'.$active;
				}								
			}
			
			$str = "";			
			foreach($p->value as $k=> $v)
			{
				if ($k[0] == "_") continue;
				if ($str != "") $str .= "|";
				$str .= $v;
			}
			$px->fullvalue = $str;
			$px->type = $p->type;
			$px->name = $p->name;
			
			$set[] = $px;
		}
	}
	
	//print_r($set);
	
	$a = new AQLStackElement();
	$a->typename = "pointlist";
	$a->islist = true;
	$a->value = $set;
	array_push($aql->stack,$a);
}
?>