<?php


function fn_localpoints_a($aql)
{
	$list = $aql->Consume(1);
	$set = array();
	
	$allprops = Properties::GetAllProperties();

	foreach($list[0]->value as $a)
	{
		$ass = new Asset($a);
		$props = $ass->GetProperties();
		$ass->GetValues($props);
		
		$finalset = array();
		foreach($props as $pr)
		{
			if (($pr->origintype == "template") || ($pr->origintype == "local"))
			{
				$finalset[] = $pr;
			}
		}
		$props = $finalset;
		if (count($props) > 0)
		{
			$ass->Fetch($props);
		}
		
		foreach($props as $p)
		{
			$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
				{
					$px->rawvalue = implode('|',array_values($p->value));
					$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";
						}
					}
				}
			}
			
			if (isset($p->value['_dynamics'][$active]))
			{
				$px->dynamic = $p->value['_dynamics'][$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);
}
?>