<?php

function fn_sourcedfrom_a($aql)
{
	$list = $aql->Consume(1);	

	$alist = array();
	if ($list->islist == true)
	{
		foreach($list->value as $q)
		{
			$alist[] = $q;
		}
	}
	
	print_r($alist);
	
	$alist = [631];
	
	global $db;
	
	$apool = array();
	
	$propset = Properties::GetAllProperties();
	$lookups = array();
	foreach($propset as $px)
	{
		$lookups[$px->id] = $px;
	}
	
	$qry = "SELECT propertyid, assetid FROM datalinks WHERE sourceid IN (".implode(',',$alist).")";
	$query = $db->query($qry);
	while($row = $query->fetch(PDO::FETCH_ASSOC)) 
	{				
		$ass = new Asset($row['assetid']);
		$pnt = new AQLPoint();
		$pnt->assetid = $row['assetid'];
		$pnt->sourceid = $row['assetid'];
		$pnt->propertyid = $row['propertyid'];
		$pnt->type = $lookups[$pnt->propertyid]->type;
		if ($pnt->type == "MEASUREMENT")			
			$pnt->dynamic = $pnt->assetid.':'.$pnt->propertyid.':measurement';
		if ($pnt->type == "STATUS")			
			$pnt->dynamic = $pnt->assetid.':'.$pnt->propertyid.':state';
		if (($pnt->type == "ENUM") || ($pnt->type == "LOOKUP"))
			$pnt->dynamic = $pnt->assetid.':'.$pnt->propertyid.':value';
		$pnt->value = "";	
		$pnt->name = $ass->name.' '.$lookups[$pnt->propertyid]->name;
		$apool[] = $pnt;
	}		
	
	$a = new AQLStackElement();
	$a->typename = "pointlist";
	$a->islist = true;
	$a->value = $apool;
	array_push($aql->stack,$a);
}

?>