<?php

function fn_allpoints_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);
		$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]))
			if (isset($p->value['_dynamics']))
			{
				if (in_array($active,$p->value['_dynamics']))
				{
					$px->dynamic = $px->dynamic = $p->origin.':'.$p->id.':'.$active;//$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);
}

function fn_allpoints_p($aql)
{
	$list = $aql->Consume(1);
	$set = array();
	
	global $siteroot;
	
	echo 'Doing This!';
	print_r($list);
	
	global $db;
	
	$allprops = Properties::GetAllProperties();
	//echo 'All Points (Property)';
	foreach($list[0]->value as $a)
	{
		echo 'Found '.$a;
		foreach($allprops as $px)
		{			
			
			if ($px->id == $a)
			{
				//echo 'Scanning '.$px->id;
				//OK - find all assets that use this property...
				$parentassets = array();
				$scanassets = array();				
				$query = $db->query("SELECT assetid,type,value FROM assetvalues JOIN assets ON (assets.id = assetvalues.assetid) WHERE propertyid=".$px->id);
				
				$values = array();
				
				while($row = $query->fetch(PDO::FETCH_ASSOC)) 
				{
					if ($row['type'] == 1)
					{
						$parentassets[] = $row['assetid'];
					}
					else
					{
						$scanassets[] = $row['assetid'];
					}
					$values[$row['assetid']] = $row['value'];
				}
				
				if (count($parentassets) > 0)
				{
					foreach($parentassets as $par)
					{
						$query = $db->query("SELECT childasset FROM assetrelclosures JOIN assets ON (assets.id = assetrelclosures.childasset) WHERE parentasset=".$par." AND assets.type=0");
					
						while($row = $query->fetch(PDO::FETCH_ASSOC)) 
						{
							if (!in_array($row['childasset'],$scanassets))
								$scanassets[] = $row['childasset'];
							
							if (!isset($values[$row['childasset']]))
								$values[$row['childasset']] = $values[$par];
						}
					}
				}
				
				sort($scanassets);				
				
				$ahit = array();
				
				//print_r($values);
				
				global $installfolder;
				include($installfolder.'/modules/inputs/'.strtolower($px->type).'.php');
				//echo $installfolder.'/modules/inputs/'.strtolower($px->type).'.php';				
								
				//foreach($keys as $ky)
				//{
				foreach($scanassets as $sca)
				{
					$asset = array();
					global $assetdata;
					$assetdata = array();
					
					$url = $siteroot.'/properties/'.$sca;
					
					$ass = new Asset($sca);
					if ($ass->tag == true) continue;
					$value = "";
					if (isset($values[$sca])) $value = $values[$sca];
					
					//Check for a monitors relationship....
					//print_r($asset[$sca]);
					$monitored = false;
					if (isset($asset[$sca]['relationships'][6]))
					{
						//echo 'Monitors Relationship Detected on '.$sca;
						//print_r($asset[$sca]['relationships'][6]);
						foreach($asset[$sca]['relationships'][6] as $r)
						{
							if ($r['upstream'] == true)
							{
								$other = new Asset($r['target']);
								$prp = $other->GetLocalProperties();
								$other->GetValues($prp,true,true);
								//print_r($prp);
								foreach($prp as $o)
								{
									if ($o->id == $propid)
									{
										//echo 'Ding!';
										$monitored = true;
										break;
									}
								}
							}
						}
					}
					
					if ($monitored == true)
					{
						continue;
					}
					
					$ahit[] = $sca;
						
					//echo 'Checking For '.$sca.'<br/>';
					//print_r($values);
					if (isset($values[$sca]))
					{
						//echo 'Using Value!';
						$value = $values[$sca];
					}
					
					//print("Value for ".$sca.": ".$value);
					
					//Split Value...
					$bits = explode('|',$value);
					
					$finalvalues = array();
					for($v = 0;$v<count($inputs);$v++)
					{
						$finalvalues[$inputs[$v]['name']] = $bits[$v];
					}
					$value = $finalvalues;
										
					$pp = new AQLPoint();
					$pp->processed = False;
					$pp->assetid = $ass->id;
					//if ($pp->origintype == "template") $pp->origin = $ass->id;
					$pp->sourceid = $ass->id;
					$pp->propertyid = $px->id;
					
					$active = "text";
					if ($px->type == "MEASUREMENT")
						$active = "measurement";
					if ($px->type == "STATUS")
						$active = "state";
					if (($px->type == "ENUM") || ($px->type == "LOOKUP"))
						$active = "value";
					
					if (isset($value)) 
					{
						$pp->rawvalue = $value[$active];
						$pp->value = $value[$active];
					}			
					else
					{
						if (count($value) == 1)
						{
							$pp->rawvalue = $value[array_keys($p->value)[0]];
							$pp->value = $px->rawvalue;
						}
						else
						{
							$pp->rawvalue = implode('|',array_values($p->value));
							$pp->value = $pp->rawvalue;
						}
					}
					
					if (($pp->type == "ENUM") || ($pp->type == "STATUS") || ($pp->type == "LOOKUP"))
					{
						
						$map = $px->GetValueMap($p->value);
						if (isset($map[$pp->value]))
						{
							$pp->value = $map[$pp->value];
						}
						else
						{
							$pp->value = "Unknown";
						}
													
					}
					
					if (isset($value['_dynamics'][$active]))
					{
						$pp->dynamic = $value['_dynamics'][$active];
					}
					
					$str = "";			
					foreach($value as $k=> $v)
					{
						if ($k[0] == "_") continue;
						if ($str != "") $str .= "|";
						$str .= $v;
					}
					$pp->fullvalue = $str;
					$pp->type = $px->type;
					$pp->name = $px->name;
					
					$set[] = $pp;
				}
			}
				//}								
		}
	}
			
	
	//print_r($set);
	
	$a = new AQLStackElement();
	$a->typename = "pointlist";
	$a->islist = true;
	$a->value = $set;
	array_push($aql->stack,$a);
}
?>