CatalogMgr.php 7.71 KB
<?php

/**
 * @class CatalogMgr
 */

class CatalogMgr extends TimeTableMgr
{
	function __construct($sharedObject = FALSE) {
		parent::__construct('Tt.xml', $sharedObject);
		$this->contentRootId = 'catalog-treeRootNode';
		$this->contentRootTag = 'catalogList';
		$this->attributes = array('name' => '', 'intervals' => ''); // +  'parameters'
		$this->optionalAttributes = array();
		$this->objTagName =   'catalog';
		$this->id_prefix = 'cat_'; // 'tt_' ?

		if (!$sharedObject && !file_exists($this->xmlName)) {
			  $this->createDom();
			  $this->xp = new domxpath($this->contentDom);
		}
	}

	public function getTmpObject($folderId, $name, $onlyDescription = false)
	{
		$result = parent::getTmpObject($folderId, $name, $onlyDescription);
		if (!isset($result['success']) || !$result['success']) {
			return $result;
		}
		return $result + array('parameters' => $this->getCatalogParamDescription(array('folder' => $folderId, 'name' => $name)));
	}


	public function loadIntervalsFromObject($id, $type, $start = NULL, $limit = NULL)
	{
		$result = parent::loadIntervalsFromObject($id, $type, $start, $limit);
		if (!isset($result['success']) || !$result['success']) {
			return $result;
		}

		return $result + array('parameters' => $this->getCatalogParamDescription(array('id' => $id, 'type' => $type)));
  }

        /*
        *   catalog header
        */
	protected function setParamDescription($params)
	{
		$paramsElement = $this->objectDom->createElement('parameters');
		foreach ($params as $param)
		{
			$paramElement = $this->objectDom->createElement('parameter');
			$attrArray = (array)$param;
			foreach ($attrArray as $key => $value)
				$paramElement->setAttribute($key, $value);
			$paramsElement->appendChild($paramElement);
		}

	return $paramsElement;
	}

      protected function createIntervalElement($interval, $options = array())
      {
		$newInterval = $this->objectDom->createElement('intervals');
		$newInterval->appendChild($this->objectDom->createElement('start',$interval['start']));
		$newInterval->appendChild($this->objectDom->createElement('stop',$interval['stop']));
		if (!empty($options['parameters'])) {
			foreach ($options['parameters'] as $parameter) {
				$newInterval->appendChild($this->objectDom->createElement('param', $interval[$parameter['id']]));
			}
		}
	return $newInterval;
      }

	public function createObject($p, $folder)
	{
		if ($p -> leaf)
		{
			$result = $this->createParameter($p, $folder);
			if ($result['error'])
				return $result;

			$cacheMgr = new CatalogCacheMgr();

			if (isset($p->cacheToken) && ($p->cacheToken != ''))
			{
				$resultSaveInt = $cacheMgr->saveInObject($result['id'], "update", $p->cacheToken);
				if (!$resultSaveInt['success'])
				{
					if ($resultSaveInt['message'])
						return array('error' => $resultSaveInt['message']);
					else
						return array('error' => 'Unknown error during intervals save');
				}
			}
			return $result;
		}
		//      else return $this->createFolder($p);
		//TODO check if this is possible?
		else return array('error' => 'createFolder should be called from RENAME');

	}

	public function initForChart($id, $name, $isTmpObject, $type)
	{
	       $intervals_res = $this->getCatalogParamDescription($id, $name, $isTmpObject, $type);

		if (!$intervals_res['success'])
			return $intervals_res;

		$paramHeaders = array();

		foreach ( $intervals_res['parameters'] as $param ) {

		  if ($param['size'] > 1) {

			for ($i = 0; $i < $param['size']; $i++) {
				$paramComp = array();
				$paramComp['id'] = $param['id'].'_'.$i;
				$paramComp['name'] = $param['name'].'_'.$i;
			//	$paramComp['size'] = 1;

				$paramHeaders[] = $paramComp;
			}
		  }
		  else {
		       $paramHeaders[] = $param;
		  }
		}

	//	unset($intervals_res);

		return array('success' => true, 'parameters' => $paramHeaders,
				'totalCount' =>  $intervals_res['totalCount'], 'name' => $intervals_res['name']);
	}


	public function getIntervalsForChart($id, $name, $isTmpObject, $type) {

		if ($isTmpObject)
			$intervals_res = $this->getTmpObject($id, $name);
		else
			$intervals_res = $this->loadIntervalsFromObject($id,$type);

		if (!$intervals_res['success'])
			return $intervals_res;

		$newIntervals = array();

		foreach ($intervals_res['intervals'] as $interval)
		{
			$newIntervalComp = array();
			$k = 0;

			for ( $j = 0; $j < count($interval['paramTable']); $j++ ) {

				$param = $interval['paramTable'][$j];
				$tempArr = explode(',',$param);

				if (count($tempArr) > 1) {
					for ($i = 0; $i < count($tempArr); $i++) {
						$newIntervalComp['param'.$k] = $tempArr[$i];
						$k++;
					}
				}
				else {
				       $newIntervalComp['param'.$k] = $param;
					$k++;
				}
			}
			$newIntervals[] = $newIntervalComp;
		}

		return array('success' => true, 'intervals' => $newIntervals);

	}

	public function getCatalogParamDescription($options)
	{
		if (!empty($options['id'])) {
			if (isset($options['type']) && ($options['type'] == 'sharedcatalog')) {
				$sharedObjMgr = new SharedObjectsMgr();
				$path = $sharedObjMgr->getDataFilePath('catalog', $options['id']);
			}
			else {
				$path = USERTTDIR.$options['id'].'.xml';
			}
		}
		else {
			$path = $options['folder'].'/'.$options['name'].'.xml';
		}

      //load intervals from Catalog id
		if (!file_exists($path))
			return array('success' => false, 'message' => "Cannot find Catalog file ".$path);

		if (!$this->objectDom->load($path))
			return array('success' => false, 'message' => "Cannot load Catalog file");

    $xpath = new DOMXPath($this->objectDom);

		// params header
		$paramsNodes =  $xpath->query('//parameters/parameter');
		$paramsArray = array();

		if ($paramsNodes->length > 0)
		{
			foreach ($paramsNodes as $paramNode)
			{
				$oneParam = array();
				foreach ($paramNode->attributes as $attr)
					$oneParam[$attr->nodeName] = $attr->nodeValue;
				$paramsArray[] = $oneParam;
			}
		}

		return array(
			  'parameters' => $paramsArray,
			  'success'    => true
    );

    }

			public function getUploadedObject($name, $format, $onlyDescription = false)
			{
				/*$result = parent::getUploadedObject($name, $format, $onlyDescription);
				if (!isset($result['success']) || !$result['success']) {

					return array('success' => FALSE, 'error' => 'Error to retrieve catalog info');
				}


					return $result + array('parameters' => $this->getCatalogParamDescription(array('folder' => $folderId, 'name' => $name));*/
			}

			/**
			 * Load interval from DOMElement
			 * @param DOMElement
			 * @return interval
			 */
			protected function loadIntervalElement($intervalNode)
			{
				$interval = parent::loadIntervalElement($intervalNode);
				if ($interval === FALSE) {
					return FALSE;
				}
				$paramNodes = $intervalNode->getElementsByTagName('param');
				$interval['paramTable'] = array();
				if ($paramNodes->length > 0)
        	foreach ( $paramNodes as $paramNode ) $interval['paramTable'][] = $paramNode->nodeValue;

				return $interval;
			}

// 	public function modifyObject($p) {
// 		$folder = $this->getObjectFolder($p->id);
//
// 		//Copy TT in a tempory file
// 		$ttFilePath = USERTTDIR.$p->id.'.xml';
// 		$tmpFileExist = FALSE;
// 		if (file_exists($ttFilePath))
// 			$tmpFileExist = copy($ttFilePath,$ttFilePath.".tmp");
//
// 		//Delete TT
// 		$this->deleteObject($p);
//
// 		//Save modifications
// 		try {
// 			$result = $this->createObject($p, $folder);
// 			if ($result['error'])
// 				throw new Exception($result['error']);
// 			if ($tmpFileExist)
// 				unlink($ttFilePath.".tmp");
// 			return array('id' => $p->id, 'info' => $result['nbIntervals'].' intervals' );
// 		}
// 		catch (Exception $e) {
// 			//Restore TT file
// 			if ($tmpFileExist)
// 			{
// 				copy($ttFilePath.".tmp", $ttFilePath);
// 				unlink($ttFilePath.".tmp");
// 			}
// 			return array ('error' => $e->getMessage());
// 		}
// 	}


}
?>