TimeTableCacheMgr.php 9.1 KB
<?php

/**
 * @class CatalogCacheMgr
 */

 class TimeTableCacheMgr
{
	protected $objectMgr = null;
	protected $cache = null;

  function __construct() {
		$this->objectMgr = new TimeTableMgr();
	}

  protected function getCacheFileName() {
    return "cacheTT";
  }

  protected function getCacheDirectory() {
    return USERTTDIR;
  }

  protected function resetCache($options = array()) {
    $this->cache = new TimeTableCacheObject();
    return array();
  }

  protected function loadAdditionalDescription($options) {
    return array();
  }

	public function initObjectCache($options = array()) {
		//Create new cache
		$info = $this->resetCache($options);

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

	public function initFromObject($id, $type) {
    $options = $this->loadAdditionalDescription(array('id' => $id, 'type' => $type));
		//Create new cache
		$info = $this->resetCache($options);

		//Load intervals from object file and add to cache
		$intervals_res = $this->objectMgr->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()) + $info;
	}

       public function initFromCatalog($id, $type) {
                //Create new cache
            $this->resetCache();

                //Load intervals from object file and add to cache
                $catMgr = new CatalogMgr();
                $intervals_res = $catMgr->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());
        }


	public function initFromTmpObject($folderId, $name) {
    $options = $this->loadAdditionalDescription(array('folder' => USERWORKINGDIR.$folderId, 'name' => $name));
		//Create new cache
		$info = $this->resetCache($options);

		//Load intervals from TmpObject file (DD_Search output)
		$intervals_res = $this->objectMgr->getTmpObject($folderId, $name);

		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Tmp Object');

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

		$this->cache->setIsModified(true);

		unset($intervals_res);

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

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

	public function initFromUploadedFile($name, $format) {
    $options = $this->loadAdditionalDescription(array('folder' => USERTEMPDIR, 'name' => $name));
		//Create new cache
		$info = $this->resetCache($options);

		//Load intervals from uploaded file
		$intervals_res = $this->objectMgr->getUploadedObject($name, $format);

		if (!isset($intervals_res))
			return array('success' => false, 'message' => 'Cannot get Tmp Object');

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

		$this->cache->setIsModified(true);

		unset($intervals_res);

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

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

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

		if ($token != $this->cache->getToken())
			return array('success' => false, 'message' => 'Cache token check error');

		$this->cache->updateIndexes();
		$this->saveToFile();

		$intervals = $this->cache->getIntervalsArray(NULL,NULL,true);

		$this->cache->reset();

    $options = $this->loadAdditionalDescription(array('id' => $id));

		return $this->objectMgr->saveIntervals($id, $intervals, $action, $options);
	}

	public function getIntervals($start,$limit,$sort_obj,$filter_json) {
		if (!$this->loadFromFile())
			return array('success' => false, 'message' => 'Cannot load cache file');

		$needToUpdate = false;
		if (isset($filter_json))
		{
			if (!$this->cache->getFilter()->isSameFromJSON($filter_json))
			{
				$needToUpdate = true;
				$this->cache->getFilter()->loadFromJSON($filter_json);
			}
		}
		else
		{
			if (!$this->cache->getFilter()->isEmpty())
			{
				$needToUpdate = true;
				$this->cache->getFilter()->reset();
			}
		}

		if (isset($sort_obj))
		{
			if (!$this->cache->getSort()->isSameFromObject($sort_obj))
			{
				$needToUpdate = true;
				$this->cache->getSort()->loadFromObject($sort_obj);
			}
		}
		else
		{
			if (!$this->cache->getSort()->isEmpty())
			{
				$needToUpdate = true;
				$this->cache->getSort()->reset();
			}
		}

		if ($needToUpdate)
		{
			$this->cache->updateIndexes();
			$this->saveToFile();
		}

		return array(
				'token'      => $this->cache->getToken(),
				'totalCount' => $this->cache->getLength(),
				'intervals'  => $this->cache->getIntervalsArray($start, $limit),
				'start' => isset($start) ? $start : 0,
				'limit' => isset($limit) ? $limit : 0,
				'status' => $this->cache->getStatus(),
				'success'    => true
		);
	}

  public function getIntervalsForChart() {
    return $this->getIntervals(NULL, NULL, NULL, NULL);
  }

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

		if (!isset($index))
			$index = 0;

		if (!isset($start))
			$start = date('Y-m-d\TH:i:s.u');

		if (!isset($stop))
			$stop = date('Y-m-d\TH:i:s.u');

		$this->cache->addInterval(array('start' => $start, 'stop' => $stop), true, $index);

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

		$this->saveToFile();

		return array('success' => true, 'index' => $index, 'status' => $this->cache->getStatus());
	}

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

		$this->cache->removeIntervalFromId($id);

		$this->cache->updateIndexes();

		$this->saveToFile();

		return array('success' => true, 'status' => $this->cache->getStatus());
	}

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

		$this->cache->modifyIntervalFromId($cacheId, $data);

		$this->saveToFile();

		return array('success' => true, 'status' => $this->cache->getStatus());
	}

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

		$this->cache->operationIntervals($extendTime, $shiftTime);

		$this->saveToFile();

		return array('success' => true, 'status' => $this->cache->getStatus());
	}

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

		$this->cache->mergeIntervals();

		$this->saveToFile();

		return array('success' => true, 'status' => $this->cache->getStatus());
	}

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

		return array('success' => true, "result" => $this->cache->getStatistics(), 'status' => $this->cache->getStatus());
	}

	public function dump() {
		if (!$this->loadFromFile())
		{
			echo "ERROR to load cache file : ".$this->getCacheFilePath().PHP_EOL;
			return;
		}
		$this->cache->dump();
	}

  protected function getCacheFilePath() {
		return $this->getCacheDirectory().'/'.$this->getCacheFileName();
	}

	protected function saveToFile() {
		if (!isset($this->cache))
			return false;
		$handle = fopen($this->getCacheFilePath(), 'wb');
		$result = false;
		if (flock($handle, LOCK_EX))
		{
			$this->cache->writeBin($handle);
			flock( $handle, LOCK_UN );
			$result = true;
		}
		fclose($handle);
		return $result;
	}

	protected function loadFromFile() {
		if (!file_exists($this->getCacheFilePath()))
			return false;
		$this->resetCache();
		$handle = fopen($this->getCacheFilePath(), 'rb');
		$result = false;
		if (flock($handle, LOCK_SH))
		{
			$this->cache->loadBin($handle);
			flock( $handle, LOCK_UN );
			$result = true;
		}
		fclose($handle);
		return $result;
	}
}

?>