CatalogCacheMgr.php 5.91 KB
<?php

/**
 * @class CatalogCacheMgr
 */

class CatalogCacheMgr extends TimeTableCacheMgr
{
	const DEFAULT_PARAM_ID_PREFIX = 'cat_param_id_';
	const DEFAULT_NEW_PARAM_ID_PREFIX = 'added_param_id_'; 
	private $isForVisu = FALSE;

	function __construct($isForVisu = FALSE) {
		$this->objectMgr  = new CatalogMgr();
	}

	protected function getCacheFileName() {
		if ($this->isForVisu) {
			return "cacheVisuCat";
		}
	  return "cacheCat";
	}

	protected function resetCache($options = array()) {
    $this->cache = new CatalogCacheObject();
		if (!empty($options['nparams'])) {
			for ($i = 0; $i < (int)$options['nparams']; $i++) {
				$this->cache->addParameter(CatalogCacheMgr::DEFAULT_PARAM_ID_PREFIX.(string)($i+1), 'column_'.(string)($i+1), 1, 0, "", FALSE);
			}
		}
		else if (!empty($options['parameters'])) {
			$index = 0;
			foreach ($options['parameters'] as $parameter) {
				if (array_key_exists('id',$parameter) && !empty($parameter['id'])) {
					$id = $parameter['id'];
				}
				else if (array_key_exists('ID',$parameter) && !empty($parameter['ID'])) {
					$id = $parameter['ID'];
				}
				else {
					$id = 'cat_param_id_'.$index;
				}
				$description = "";
				if (isset($parameter['description'])) {
					$description = $parameter['description'];
				}
				$this->cache->addParameter($id, $parameter['name'], intval($parameter['size']), intval($parameter['type']), $description, FALSE);
				++$index;
			}
		}
		return array('parameters' => $this->cache->getParametersInfo());
  }

	protected function loadAdditionalDescription($options) {
		$params_desc = $this->objectMgr->getCatalogParamDescription($options);
		if (!$params_desc['success']) {
			return array();
		}
		return array('parameters' => $params_desc['parameters']);
	}


	public function initFromUploadedFile($name, $format)
	{
		$result = parent::initFromUploadedFile($name, $format);
		if (!$result['success']) {
			return $result;
		}

		$info = $this->objectMgr->getUploadedObject($name, $format,TRUE);
		foreach ($info['parameters'] as $parameter) {
			$description = "";
			if (isset($parameter['description'])) {
				$description = $parameter['description'];
			}
			$this->cache->addParameter($parameter['id'], $parameter['name'], intval($parameter['size']), intval($parameter['type']), $parameter['description'], FALSE);
		}

		return $result+ array('parameters' => $info['parameters']);
	}

	public function initFromTimeTable($id, $nbParams, $type)
	{

          $params = $this->resetCache(array('nparams' => $nbParams));
          $ttMgr= new TimeTableMgr();
          $intervals_res = $ttMgr->loadIntervalsFromObject($id, $type);

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

          foreach ($intervals_res['intervals'] as $interval)
          {
                  //Add interval
                  $this->cache->addInterval($interval);
           }

          unset($intervals_res);

          //Update cache
          $this->cache->updateIndexes();

          //Save cache file
          return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(), 'parameters' => $this->cache->getParametersInfo());
 	}

	 public function getIntervalsForChart() {
		 $result = $this->getIntervals(NULL, NULL, NULL, NULL);
		 if (!$result['success']) {
			 return FALSE;
		 }

		 $parameters = $this->cache->getParametersInfo();
		 $parameters_to_split = array();
		 $parameters_chart = array();
		 foreach ($parameters as $parameter) {
			 if ($parameter['size'] > 1) {
				 $parameters_to_split[] = $parameter;
				 for ($i = 0; $i < $parameter['size']; ++$i) {
					 $comp_param = $parameter;
					 $comp_param['id'] = $parameter['id'].'_COMPONENT_'.$i;
					 $comp_param['name'] = $parameter['name'].'['.$i.']';
					 unset ($comp_param['size']);
				 	$parameters_chart[] = $comp_param;
				}
			 }
			 else {
				 $comp_param = $parameter;
				 unset ($comp_param['size']);
				 $parameters_chart[] = $comp_param;
			 }
		 }

		 if (empty($parameters_to_split)) {
			 return $result + array('parameters' => $parameters_chart);
		 }

		 //split parameters
		 foreach ($result[intervals] as &$interval) {
			 foreach ($parameters_to_split as $parameter) {
				 $values = explode (',',$interval[$parameter['id']]);
				 unset($interval[$parameter['id']]);
				 foreach ($values as $key => $value) {
					 $interval[$parameter['id'].'_COMPONENT_'.$key] = $value;
				 }
  		 }
		 }

		 return $result + array('parameters' => $parameters_chart);
   }

   public function deleteColumn($id){
		if (!$this->loadFromFile())
		return array('success' => false, 'message' => 'Cannot load cache file');

		$result = $this->cache->deleteParameter($id);
		if ($result){
			$this->saveToFile();
		}
		return array('success' => $result , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
   }

   public function addColumn($id,$name, $type,$size, $description){
		if (!$this->loadFromFile())
		return array('success' => false, 'message' => 'Cannot load cache file');
		$isNew=true;
		$this->cache->addParameter($id,$name, $size, $type, $description , $isNew);
		$this->saveToFile();
		return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
   }

   public function getCatColumnInfo($id){
	if (!$this->loadFromFile())
		return array('success' => false, 'message' => 'Cannot load cache file');
	
	return $this->cache->getParameterInfo($id);
   }

   public function editColumn($id,$name, $type,$size,  $description){
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		$this->cache->editParameter($id,$name, $type, $size,  $description);

		$this->saveToFile();
		return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
	}
}
?>