IHMUserParamLoaderClass.php 3.98 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 $infoDerivedDescriptionNode = 'description';
	
	/*
	 * @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 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 (count($derivedNodes) > 0)
		{	
			$derivedParamNodes = $derivedNodes->item(0)->getElementsByTagName(self::$mgrDerivedParamNode);
			foreach ($derivedParamNodes as $derivedParamNode)
			{	
				//id
				$paramId         = $derivedParamNode->getAttribute(self::$mgrDerivedParamIdAtt);
				
				//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($id)
				));
			}
		}
		
		$userParams["derived"] = $derivedParams;
		
		return array("success" => true, "params" => $userParams);
	}
	
	/*
	 * @brief Load additionnal derived parameter info from paramId
	*/
	private function loadDerivedParameterInfo($paramId)
	{
		//get full path
		$path = IHMConfigClass::getUserWSPath().$paramId.".xml";
	
		$result = array(
				"units"       => "",
				"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 (count($unitsNodes) > 0)
			$result["units"] = $unitsNodes->item(0)->nodeValue;
	
		//get parameter description
		$descNodes = $dom->getElementsByTagName(self::$infoDerivedDescriptionNode);
		if (count($descNodes) > 0)
			$result["description"] = $descNodes->item(0)->nodeValue;
	
		return $result;
	}
}