IHMTTCatLoaderClass.php 6.8 KB
<?php

/**
 * @class IHMTTCatLoaderClass
 * @brief Loader for IHM TimeTables and Catalogs properties
 * @details
 */
class IHMTTCatLoaderClass
{
	protected $ttCatList = null;

	private static $userTTCatFile               = 'Tt.xml';
	
	private static $mgrUserTTListNode           = 'timetabList';
	private static $mgrUserTTEltNode            = 'timetab';

	private static $mgrUserCatListNode          = 'catalogList';
	private static $mgrUserCatEltNode           = 'catalog';

	private static $mgrUserTTCatEltIdAtt        = 'xml:id';
	private static $mgrUserTTCatEltNameAtt      = 'name';
	private static $mgrUserTTCatEltNbIntAtt     = 'intervals';

	private static $mgrUserTTCatFolderNode      = 'folder';
	private static $mgrUserTTCatFolderIdAtt     = 'xml:id';
	private static $mgrUserTTCatFolderNameAtt   = 'name';

	private static $mgrSharedTTListNode         = 'timeTableList';
	private static $mgrSharedTTEltNode          = 'timeTable';

	private static $mgrSharedCatListNode        = 'catalogList';
	private static $mgrSharedCatEltNode         = 'catalog';

	private static $mgrSharedTTCatEltIdAtt      = 'xml:id';
	private static $mgrSharedTTCatEltNameAtt    = 'name';
	private static $mgrSharedTTCatEltNbIntAtt   = 'nbIntervals';

	private static $mgrSharedTTCatFolderNode    = 'folder';
	private static $mgrSharedTTCatFolderIdAtt   = 'xml:id';
	private static $mgrSharedTTCatFolderNameAtt = 'name';

	/*
	 * @brief Constructor
	*/
	function __construct()
	{
	}
	
	/*
	 * @brief Get info about a TT or a catalog from id
	 */
	public function getTTCatInfoFromId($id)
	{
		if (!isset($this->ttCatList)) {
			$this->ttCatList = $this->loadTTCatManagerFiles();
		}
		if (!array_key_exists($id, $this->ttCatList)) {
			return array("success" => false, "message" => "Cannot retrieve TT or catalog");
		}
		return array("success" => true, "info" => $this->ttCatList[$id]);
	}

	/*
	 * @brief Get parameters from id (empty for a TT)
	 */
	public function getCatalogParameters($id)
	{
		$info = $this->getTTCatInfoFromId($id);
		if (!$info["success"]) {
			return $info;
		}

		if (!$info["info"]["isCatalog"]) {
			return array("success" => TRUE, "parameters" => array());
		}

		$dom = new DomDocument("1.0");
                if (!$dom->load($info["info"]["path"])) {
			return array("success" => FALSE, "message" => "Cannot open catalog ".$id);
		}

		$parametersNodes = $dom->getElementsByTagName("parameters");
		if ($parametersNodes->length == 0) {
			return array("success" => FALSE, "message" => "Cannot retrieve parameters node in catalog ".$id);
		}

		$parametersNode = $parametersNodes->item(0);

		$parameterNodes = $parametersNode->getElementsByTagName("parameter");

		$parameters = array();
		foreach ($parameterNodes as $parameterNode) {
			$parameters[] = array(
				"id" => $parameterNode->getAttribute("id"),
				"name" => $parameterNode->getAttribute("name"),
				"size" => $parameterNode->getAttribute("size"),
				"type" => $parameterNode->getAttribute("type"),
				"unit" => $parameterNode->getAttribute("unit"),
				"description" => $parameterNode->getAttribute("description"),
				"ucd" => $parameterNode->getAttribute("ucd"),
				"utype" => $parameterNode->getAttribute("utype"),
			);
		}

		return array("success" => TRUE, "parameters" => $parameters);
	}
	

	/*
	 * @brief Load all TT and catalogs from manager files
	 */
	private function loadTTCatManagerFiles()
	{
		if (isset($this->ttCatList)) {
			return $this->ttCatList;
		}

		return $this->loadUserTTCatManagerFile() + $this->loadSharedTTCatManagerFile();
	}

	/*
	 * @brief Load user TT and cat manager file
	 */
	private function loadUserTTCatManagerFile()
	{
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load(IHMConfigClass::getUserWSPath() . '/' . self::$userTTCatFile))
			return array();

		$ttCat = array();

		//get TT
		$listNodes = $dom->getElementsByTagName(self::$mgrUserTTListNode);
		if ($listNodes->length > 0) {
			$this->getEltRecursively($listNodes->item(0), FALSE, FALSE, array(), $ttCat);
		}

		//get catalogs
		$listNodes = $dom->getElementsByTagName(self::$mgrUserCatListNode);
		if ($listNodes->length > 0) {
			$this->getEltRecursively($listNodes->item(0), FALSE, TRUE, array(), $ttCat);
		}

		return $ttCat;
	}
	
	/*
	 * @brief Load shared objects manager file
	 */
	private function loadSharedTTCatManagerFile()
	{
		//load xml file
		$dom = new DomDocument("1.0");
		if (!$dom->load(IHMConfigClass::getSharedTreeFilePath()))
			return array();

		$ttCat = array();

		//get TT
		$listNodes = $dom->getElementsByTagName(self::$mgrSharedTTListNode);
		if ($listNodes->length > 0) {
			$this->getEltRecursively($listNodes->item(0), TRUE, FALSE, array(), $ttCat);
		}

		//get catalogs
		$listNodes = $dom->getElementsByTagName(self::$mgrSharedCatListNode);
		if ($listNodes->length > 0) {
			$this->getEltRecursively($listNodes->item(0), TRUE, TRUE, array(), $ttCat);
		}

		return $ttCat;
	}

	private function getEltRecursively($baseNode, $isShared, $isCatalog, $folders, &$elts) {
		if ($isShared) {
			$basePath = IHMConfigClass::getSharedPath();
			if ($isCatalog) {
				$eltNode = self::$mgrSharedCatEltNode;
				$basePath .= "/CAT";
			}
			else {
				$eltNode = self::$mgrSharedTTEltNode;
				$basePath .= "/TT";
			}
			$eltIdAtt      = self::$mgrSharedTTCatEltIdAtt;
			$eltNameAtt    = self::$mgrSharedTTCatEltNameAtt;
			$eltNbIntAtt   = self::$mgrSharedTTCatEltNbIntAtt;
			$folderNode    = self::$mgrSharedTTCatFolderNode;
			$folderIdAtt   = self::$mgrSharedTTCatFolderIdAtt;
			$folderNameAtt = self::$mgrSharedTTCatFolderNameAtt;
			if (!empty($folders)) {
				$basePath .= "/".$folders[0]["name"]."/data";
			}
		}
		else {
			$basePath = IHMConfigClass::getUserTTPath();
			if ($isCatalog) {
				$eltNode = self::$mgrUserCatEltNode;
			}
			else {
				$eltNode = self::$mgrUserTTEltNode;
			}
			$eltIdAtt      = self::$mgrUserTTCatEltIdAtt;
			$eltNameAtt    = self::$mgrUserTTCatEltNameAtt;
			$eltNbIntAtt   = self::$mgrUserTTCatEltNbIntAtt;
			$folderNode    = self::$mgrUserTTCatFolderNode;
			$folderIdAtt   = self::$mgrUserTTCatFolderIdAtt;
			$folderNameAtt = self::$mgrUserTTCatFolderNameAtt;
		}

		$eltNodes = $baseNode->getElementsByTagName($eltNode);
		foreach ($eltNodes as $node) {
			$elts[$node->getAttribute($eltIdAtt)] = array(
				"id" => $node->getAttribute($eltIdAtt),
				"name" => $node->getAttribute($eltNameAtt),
				"nbIntervals" => $node->getAttribute($eltNbIntAtt),
				"folders" => $folders,
				"path" => $basePath . "/" . $node->getAttribute($eltIdAtt). ".xml",
				"isShared" => $isShared,
				"isCatalog" => $isCatalog,
			);
		}

		$folderNodes = $baseNode->getElementsByTagName($folderNode);
		foreach ($folderNodes as $node) {
			$crt_folders = $folders;
			$crt_folders[] = array(
				"id" => $node->getAttribute($folderIdAtt),
				"name" => $node->getAttribute($folderNameAtt),
			);
			$this->getEltRecursively($node, $isShared, $isCatalog, $crt_folders, $elts);
		}
	}
}