IHMUserParamLoaderClass.php 12.2 KB
<?php

/**
 * @class IHMUserParamLoaderClass
 * @brief Loader for IHM user parameter properties
 * @details
 */
class IHMUserParamLoaderClass
{
	protected $userParamsList = null;
	
	//derived parameter in manager file
	private static $mgrDerivedNode               = 'paramList';
	private static $mgrDerivedParamNode          = 'param';
	private static $mgrDerivedParamIdAtt         = 'xml:id';
	private static $mgrDerivedParamNameAtt       = 'name';
	private static $mgrDerivedParamExpressionAtt = 'buildchain';
	private static $mgrDerivedParamTimeStepAtt   = 'timestep';
	
	//additional info for derived parameter
	private static $infoDerivedUnitsNode       = 'units';
	private static $infoDerivedYTitleNode      = 'ytitle';
	private static $infoDerivedDescriptionNode = 'description';

	private static $infoDerivedUndefined       = 'undefined';
	
	//uploaded parameters in manager file
	private static $mgrUploadedNode           = 'mydataList';
	private static $mgrUploadedParamNode      = 'mydata';
	private static $mgrUploadedParamIdAtt     = 'xml:id';
	private static $mgrUploadedParamNameAtt   = 'name';
	
	//additional info for uploaded parameter
	private static $infoUploadedMinSamplingNode = 'minsampling';
	private static $infoUploadedMaxSamplingNode = 'maxsampling';
	private static $infoUploadedRealVarNode     = 'realvar';
	private static $infoUploadedTypeNode        = 'type';
	private static $infoUploadedSizeNode        = 'size';
	private static $infoUploadedVIIdNode        = 'vi';
	private static $infoUploadedPlotTypeNode    = 'plottype';
	private static $infoUploadedTableDefNode    = 'tableDef';
	private static $infoUploadedUnitsNode       = 'units';
	private static $infoUploadedYTitleNode      = 'ytitle';
	
	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}
	
	/*
	 * @brief Get derived parameter info from name ("ws_***")
	 */
	public function getDerivedParameterFromName($paramName)
	{
		if (!preg_match("#^ws_#",$paramName))
			return array("success" => false, "message" => "Bad derived parameter name");
		
		//extract real parameter name
		$realName = substr($paramName , 3);
		
		//try to load user parameters definition if not already done
		if (!isset($this->userParamsList))
			$this->userParamsList = $this->loadUserParamManagerFile();
		
		if (!$this->userParamsList["success"])
			return $this->userParamsList["success"];
		
		if (isset($this->userParamsList["params"]) && isset($this->userParamsList["params"]["derived"]))
		{
			//find the parameter info
			foreach($this->userParamsList["params"]["derived"] as $paramInfo)
			{
				if ($paramInfo["name"] == $realName)
					return array("success" => true, "param" => $paramInfo);
			}
		}
		
		return array("success" => false, "message" => "Cannot find derived parameter");
	}
	
	/*
	 * @brief Get derived parameter name from id
	*/
	public function getDerivedParameterNameFromId($paramId)
	{
		if (!preg_match("#^ws_#",$paramId))
			return array("success" => false, "message" => "Bad derived parameter id");
		
		//try to load user parameters definition if not already done
		if (!isset($this->userParamsList))
			$this->userParamsList = $this->loadUserParamManagerFile();
		
		if (!$this->userParamsList["success"])
			return $this->userParamsList["success"];
		
		if (isset($this->userParamsList["params"]) && isset($this->userParamsList["params"]["derived"]))
		{
			//find the parameter name
			foreach($this->userParamsList["params"]["derived"] as $paramInfo)
			{
				if ($paramInfo["id"] == $paramId)
					return array("success" => true, "name" => $paramInfo["name"]);
			}
		}

		return array("success" => false, "message" => "Cannot find derived parameter");
	}
	
	/*
	 * @brief Get uploaded parameter info from name ("wsd_***")
	*/
	public function getUploadedParameterFromName($paramName)
	{
		if (!preg_match("#^wsd_#",$paramName))
			return array("success" => false, "message" => "Bad uploaded parameter name");
	
		//extract real parameter name
		$realName = substr($paramName , 4);
	
		//try to load user parameters definition if not already done
		if (!isset($this->userParamsList))
			$this->userParamsList = $this->loadUserParamManagerFile();
	
		if (!$this->userParamsList["success"])
			return $this->userParamsList["success"];
	
		if (isset($this->userParamsList["params"]) && isset($this->userParamsList["params"]["uploaded"]))
		{
			//find the parameter info
			foreach($this->userParamsList["params"]["uploaded"] as $paramInfo)
			{
				if ($paramInfo["name"] == $realName)
					return array("success" => true, "param" => $paramInfo);
			}
		}
	
		return array("success" => false, "message" => "Cannot find uploaded parameter");
	}
	
	/*
	 * @brief Get uploaded parameter name from id
	*/
	public function getUploadedParameterNameFromId($paramId)
	{
		if (!preg_match("#^wsd_#",$paramId))
			return array("success" => false, "message" => "Bad uploaded parameter id");
	
		//try to load user parameters definition if not already done
		if (!isset($this->userParamsList))
			$this->userParamsList = $this->loadUserParamManagerFile();
	
		if (!$this->userParamsList["success"])
			return $this->userParamsList["success"];
	
		if (isset($this->userParamsList["params"]) && isset($this->userParamsList["params"]["uploaded"]))
		{
			//find the parameter info
			foreach($this->userParamsList["params"]["uploaded"] as $paramInfo)
			{
				if ($paramInfo["id"] == $paramId)
					return array("success" => true, "name" => $paramInfo["name"]);
			}
		}
	
		return array("success" => false, "message" => "Cannot find uploaded parameter");
	}
	
	/*
	 * @brief Load derived parameter manager file
	 */
	private function loadUserParamManagerFile()
	{
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load(IHMConfigClass::getUserParamManagerFilePath()))
			return array("success" => false, "message" => "Cannot load user parameter manager file");
		
		$userParams = array();
		
		//get derived parameter node
		$derivedNodes = $dom->getElementsByTagName(self::$mgrDerivedNode);
				
		$derivedParams = array();
		if ($derivedNodes->length > 0)
		{	
			$derivedParamNodes = $derivedNodes->item(0)->getElementsByTagName(self::$mgrDerivedParamNode);
			foreach ($derivedParamNodes as $derivedParamNode)
			{	
				//id
				$paramId         = $derivedParamNode->getAttribute(self::$mgrDerivedParamIdAtt);
				
				//modification date
				$dateModif       = filemtime(IHMConfigClass::getUserDerivedParamFilePath($paramId));
				
				//name
				$paramName       = $derivedParamNode->getAttribute(self::$mgrDerivedParamNameAtt);
				
				//expression
				$paramExpression = $derivedParamNode->getAttribute(self::$mgrDerivedParamExpressionAtt);
				
				//timestep
				$paramTimeStep   = $derivedParamNode->getAttribute(self::$mgrDerivedParamTimeStepAtt);
				
				array_push($derivedParams,array(
					"id"         => $paramId,
					"name"       => $paramName,
					"expression" => $paramExpression,
					"timestep"   => $paramTimeStep,
					"info"       => $this->loadDerivedParameterInfo($paramId),
					"dateModif"  => $dateModif
				));
			}
		}
		
		$userParams["derived"] = $derivedParams;
		
		//get uploaded parameter node
		$uploadedNodes = $dom->getElementsByTagName(self::$mgrUploadedNode);
		
		$uploadedParams = array();
		if ($uploadedNodes->length > 0)
		{
			$uploadedParamNodes = $uploadedNodes->item(0)->getElementsByTagName(self::$mgrUploadedParamNode);
			foreach ($uploadedParamNodes as $uploadedParamNode)
			{
				//id
				$paramId         = $uploadedParamNode->getAttribute(self::$mgrUploadedParamIdAtt);
		
				//modification date
				$dateModif       = filemtime(IHMConfigClass::getUserUploadedParamFilePath($paramId));
				
				//name
				$paramName       = $uploadedParamNode->getAttribute(self::$mgrUploadedParamNameAtt);
		
				array_push($uploadedParams,array(
				"id"         => $paramId,
				"name"       => $paramName,
				"info"       => $this->loadUploadedParameterInfo($paramId),
				"dateModif"  => $dateModif
				));
			}
		}
		
		$userParams["uploaded"] = $uploadedParams;
		
		return array("success" => true, "params" => $userParams);
	}
	
	/*
	 * @brief Load additionnal derived parameter info from paramId
	*/
	private function loadDerivedParameterInfo($paramId)
	{
		//get full path
		$path = IHMConfigClass::getUserDerivedParamFilePath($paramId);
	
		$result = array(
				"units"       => "",
				"yTitle"      => "",
				"description" => ""
		);
		
		if (!file_exists($path))
			return $result;
	
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load($path))
			return $result;
	
		//get parameter units
		$unitsNodes = $dom->getElementsByTagName(self::$infoDerivedUnitsNode);
		if ($unitsNodes->length > 0) {
			$result["units"] = $unitsNodes->item(0)->nodeValue;
			if ($result["units"] == self::$infoDerivedUndefined) {
				$result["units"] = "";
			}
		}

		//get parameter y title
		$yTitleNodes = $dom->getElementsByTagName(self::$infoDerivedYTitleNode);
		if ($yTitleNodes->length > 0) {
			$result["yTitle"] = $yTitleNodes->item(0)->nodeValue;
			if ($result["yTitle"] == self::$infoDerivedUndefined) {
				$result["yTitle"] = "";
			}
		}
	
		//get parameter description
		$descNodes = $dom->getElementsByTagName(self::$infoDerivedDescriptionNode);
		if ($descNodes->length > 0)
			$result["description"] = $descNodes->item(0)->nodeValue;
	
		return $result;
	}
	
	/*
	 * @brief Load additionnal uploaded parameter info from paramId
	*/
	private function loadUploadedParameterInfo($paramId)
	{
		//get full path
		$path = IHMConfigClass::getUserUploadedParamFilePath($paramId);
	
		$result = array();
		
		if (!file_exists($path))
			return $result;
		
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load($path))
			return $result;
	
		//get parameter min sampling
		$minSamplingNodes = $dom->getElementsByTagName(self::$infoUploadedMinSamplingNode);
		if ($minSamplingNodes->length > 0)
			$result["minSampling"] = $minSamplingNodes->item(0)->nodeValue;
		
		//get parameter max sampling
		$maxSamplingNodes = $dom->getElementsByTagName(self::$infoUploadedMaxSamplingNode);
		if ($maxSamplingNodes->length > 0)
			$result["maxSampling"] = $maxSamplingNodes->item(0)->nodeValue;
		else
			$result["maxSampling"] = $result["minSampling"];
		
		//get parameter real var
		$realVarNodes = $dom->getElementsByTagName(self::$infoUploadedRealVarNode);
		if ($realVarNodes->length > 0)
			$result["realVar"] = $realVarNodes->item(0)->nodeValue;
		
		//get parameter type
		$typeNodes = $dom->getElementsByTagName(self::$infoUploadedTypeNode);
		if ($typeNodes->length > 0)
			$result["type"] = $typeNodes->item(0)->nodeValue;
		
		//get parameter size
		$sizeNodes = $dom->getElementsByTagName(self::$infoUploadedSizeNode);
		if ($sizeNodes->length > 0)
			$result["size"] = $sizeNodes->item(0)->nodeValue;
		
		//get parameter VirtualInstrument ID
		$viIdNodes = $dom->getElementsByTagName(self::$infoUploadedVIIdNode);
		if ($viIdNodes->length > 0)
			$result["viId"] = $viIdNodes->item(0)->nodeValue;
		
		//get parameter plot type
		$plotTypeNodes = $dom->getElementsByTagName(self::$infoUploadedPlotTypeNode);
		if ($plotTypeNodes->length > 0)
			$result["plotType"] = $plotTypeNodes->item(0)->nodeValue;
		
		//get parameter y units
		$unitsNodes = $dom->getElementsByTagName(self::$infoUploadedUnitsNode);
		if ($unitsNodes->length > 0)
			$result["units"] = $unitsNodes->item(0)->nodeValue;
		
		//get parameter y title
		$yTitleNodes = $dom->getElementsByTagName(self::$infoUploadedYTitleNode);
		if ($yTitleNodes->length > 0)
			$result["yTitle"] = $yTitleNodes->item(0)->nodeValue;
		
		//get table definition
		$tableDefNodes = $dom->getElementsByTagName(self::$infoUploadedTableDefNode);
		if ($tableDefNodes->length > 0)
		{
			$tableDefNode = $tableDefNodes->item(0);
			$tableDefType = $tableDefNode->getAttribute('tableDefType');
			$channelsDefType = $tableDefNode->getAttribute('channelsDefType');
			$tableName = $tableDefNode->getAttribute('tableName');
			$tableUnits = $tableDefNode->getAttribute('tableUnits');
			$tableData = array();
			foreach ($tableDefNode->childNodes as $tableDataNode)
			{
				if ($tableDataNode->nodeType != XML_ELEMENT_NODE)
					continue;
		    	$tableData[$tableDataNode->tagName] = $tableDataNode->nodeValue;
			}
			$result["tableDef"] = array('tableDefType' => $tableDefType, 'channelsDefType' => $channelsDefType, 'tableName' => $tableName, 'tableUnits' => $tableUnits, 'data' => $tableData);
		}
		
		return $result;
	}
}