CatalogCacheObject.php 4.79 KB
<?php

class CatalogCacheObject extends TimeTableCacheObject
{
	private $paramsNumber;
	private $paramsSizes = array();
	private $paramsTypes = array();

	public function addInterval($startIso, $stopIso, $params, $isNew = false, $index = -1)
	{
		$interval = new CatIntervalCacheObject($this->lastId, count($this->intervals));
		++$this->lastId;
		$interval->setStartFromISO($startIso);
		$interval->setStopFromISO($stopIso);
		// for catalog
		$interval->setParams($params);

		$interval->setIsNew($isNew);
		array_push($this->intervals, $interval);

		if ($index < 0)
			array_push($this->indexes, count($this->intervals) - 1);
		else
			array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1));

		if ($isNew)
			$this->isModified = true;

		return $interval;
	}

	public function setParamsNumber($number)
	{
	    $this->paramsNumber = $number;
	}

	public function setParamsSizes($params)
	{
		for ($i = 0; $i < $this->paramsNumber; $i++)
			$this->paramsSizes[$i] = $params[$i]['size'];
	}

	public function setParamsTypes($params)
	{
		for ($i = 0; $i < $this->paramsNumber; $i++)
			$this->paramsTypes[$i] = $params[$i]['type'];
	}

	public function writeBin($handle)
	{
		//Magic key ("TTC")
		fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C')));

		//Version
		fwrite($handle,pack('L',TimeTableCacheObject::$format_version));

		//Token
		for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
			fwrite($handle,pack('C',ord($this->token[$i])));

		//Modified
		fwrite($handle,pack('L',$this->isModified));

		//Filter
		$this->filter->writeBin($handle);

		//Sort
		$this->sort->writeBin($handle);

		//Params Number
		fwrite($handle,pack('L',$this->paramsNumber));

		//Params Sizes
		for ($i = 0; $i < $this->paramsNumber; $i++)
			fwrite($handle,pack('L',$this->paramsSizes[$i]));

		//Params Types
		for ($i = 0; $i < $this->paramsNumber; $i++)
			fwrite($handle,pack('L',$this->paramsTypes[$i]));

		//Intervals
		fwrite($handle,pack('L2', count($this->intervals), $this->lastId));


		foreach($this->intervals as $interval)
			$interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes);

		//Indexes
		fwrite($handle,pack('L',count($this->indexes)));
		foreach($this->indexes as $index)
			fwrite($handle,pack('L',$index));
	}

    public function loadBin($handle) {
		//Magic key ("TTC")
		if (!$res = unpack('C3key',fread($handle,3)))
			return false;

		if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C')))
			return false;

		//Version
		if (!$res = unpack('Lversion',fread($handle,4)))
			return false;
		if (($res['version'] != TimeTableCacheObject::$format_version))
			return false;

		//Token
		$token = "";
		for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
		{
			if (!$res = unpack('Ctoken',fread($handle,1)))
				return false;
			$token .= chr($res['token']);
		}
		$this->token = $token;

		//Modified
		if (!$res = unpack('Lmodified',fread($handle,4)))
			return false;
		$this->isModified = $res['modified'];

		//Filter
		$this->filter->loadBin($handle);

		//Sort
		$this->sort->loadBin($handle);

		//ParamsNumber
		if (!$res = unpack('Lnumber',fread($handle,4)))
			return false;
		$this->paramsNumber = $res['number'];

		//ParamsSizes
		for ($i = 0; $i < $this->paramsNumber; $i++) {
		  if (!$res = unpack('Lsize',fread($handle,4)))
			  return false;
		  $this->paramsSizes[$i] = $res['size'];
		}
		//ParamsTypes
		for ($i = 0; $i < $this->paramsNumber; $i++) {
		  if (!$res = unpack('Ltype',fread($handle,4)))
			  return false;
		  $this->paramsTypes[$i] = $res['type'];
		}
		//Intervals
		$res = unpack('L2data',fread($handle,2*4));
		$nbIntervals  = $res['data1'];
		$this->lastId = $res['data2'];

		for ($i = 0; $i < $nbIntervals; ++$i)
		{
			$interval = new CatIntervalCacheObject(-1);
			$interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
			array_push($this->intervals, $interval);
		}

		//Indexes
		$res = unpack('Ldata',fread($handle,4));
		$nbIndexes  = $res['data'];
		for ($i = 0; $i < $nbIndexes; ++$i)
		{
			$res = unpack('Lindex',fread($handle,4));
			array_push($this->indexes, $res['index']);
		}

		return true;
	}

	public function modifyIntervalFromId($obj) {

		foreach ($this->intervals as $interval)
		{
			if ($interval->getId() == $obj->cacheId)
			{
				foreach((array)$obj as $key => $val) {

					if ($key == 'start')
						$interval->setStartFromISO($val);
					else if ($key == 'stop')
						$interval->setStopFromISO($val);
					else {
					       if (strpos($key, 'param') === false)
							continue;
						$params = $interval->getParams();
						$paramIndex = (int)substr($key,5);
						$params[$paramIndex-2] = $val;
						$interval->setParams($params);
					}
				}
				$interval->setIsModified(true);
				$this->isModified = true;
				return true;
			}
		}

		return false;
	}
}

 ?>