CatalogCacheObject.php 5.75 KB
<?php

class CatalogCacheObject extends TimeTableCacheObject
{
	private $parameters = array();

	public function reset() {
		parent::reset();
		$this->parameters = array();
		unset($this->filter);
		$this->filter = new CatalogCacheFilterObject($this);
		unset($this->sort);
		$this->sort = new CatalogCacheSortObject($this);
	}

	protected function createNewIntervalObject($id, $index = -1) {
		return new CatalogCacheIntervalObject($this, $id, $index);
	}

	public function addInterval($interval, $isNew = false, $index = -1)
	{
		$intervalObj = parent::addInterval($interval, $isNew, $index);
		for ($i = 0; $i < count($interval['paramTable']); ++$i) {
			$intervalObj->setParamValue($this->parameters[$i]['id'], $interval['paramTable'][$i]);
		}
		return $intervalObj;
	}

	public function addParameter($id, $name, $size, $type, $description, $isNew = false)
	{
	    $this->parameters[] = array(
				'id' => $id,
				'name' => $name,
				'size' => $size,
				'type' => $type,
				'description' => $description,
			);
		if($isNew){
			$this->isModified = $isNew;
		}
	}

	public function deleteParameter($id){
		foreach($this->parameters as $index=>$param){
			if($id == $param['id']){
				unset($this->parameters[$index]);
				$this->parameters = array_values($this->parameters);
				$this->isModified = TRUE;
				return true;
			}
		}
		return false;
	}

	public function editParameter($id, $name, $type, $size, $description)
	{
		foreach ($this->parameters as $index=>$param){
			if($id == $param['id']){
				if(isset($name))
					$this->parameters[$index]['name'] = $name;
				if(isset($type))
					$this->parameters[$index]['type'] = $type;
				if(isset($size))
					$this->parameters[$index]['size'] = $size;
				if(isset($description))
					$this->parameters[$index]['description'] = $description;
				$this->isModified = TRUE;
				return true;
			}
		}
		return false;
	}

	public function getParametersInfo() {
		return $this->parameters;
	}

	public function getParameterInfo($id){
		foreach ($this->parameters as $index=>$param){
			if($id == $param['id']){
				return $param;
			}
		}
		return false;
	}

	public function writeAdditionalHeaderBin($handle)
	{
			//Params Number
		fwrite($handle,pack('L',count($this->parameters)));
		foreach ($this->parameters as $parameter) {
				//Param Id length
			fwrite($handle,pack('L',strlen($parameter['id'])));
		  	//Param name length
			fwrite($handle,pack('L',strlen($parameter['name'])));
			//Param Size
			fwrite($handle,pack('L',$parameter['size']));
			//Param Type
			fwrite($handle,pack('L',$parameter['type']));
			//Description Size
			fwrite($handle,pack('L',strlen($parameter['description'])));
			//Param Id
			for ($i = 0; $i < strlen($parameter['id']); ++$i)
				fwrite($handle,pack('C',ord($parameter['id'][$i])));
			//Param name:
			for ($i = 0; $i < strlen($parameter['name']); ++$i)
				fwrite($handle,pack('C',ord($parameter['name'][$i])));
			//Param description
			for ($i = 0; $i < strlen($parameter['description']); ++$i)
				fwrite($handle,pack('C',ord($parameter['description'][$i])));
		}
	}

	protected function loadAdditionalHeaderBin($handle) {
			//Params Number
			if (!$res = unpack('Lnumber',fread($handle,4)))
				return false;
			$nbParams = $res['number'];

			for ($i = 0; $i < $nbParams; ++$i) {
				//Param Id length
		 		if (!$res = unpack('Lidlength',fread($handle,4)))
					return false;
				$idlength = $res['idlength'];

				//Param Name length
			  	if (!$res = unpack('Lnamelength',fread($handle,4)))
					return false;
				$namelength = $res['namelength'];

				//Param Size
			 	if (!$res = unpack('Lsize',fread($handle,4)))
					return false;
			 	$size = $res['size'];

				 //Param Type
				if (!$res = unpack('Ltype',fread($handle,4)))
					return false;
				$type = $res['type'];

				//Param Description lenfth
				if (!$res = unpack('Ldescriptionlength',fread($handle,4)))
					return false;
				$descriptionlength = $res['descriptionlength'];

				//Param Id
				$id = "";
				for ($j = 0; $j < $idlength; ++$j)
				{
						if (!$res = unpack('Cid',fread($handle,1)))
							return false;
						$id .= chr($res['id']);
					}

				//Param Name
				$name = "";
				for ($j = 0; $j < $namelength; ++$j)
				{
					if (!$res = unpack('Cname',fread($handle,1)))
						return false;
					$name .= chr($res['name']);
				}

				//Param description
				$description = "";
				for ($j = 0; $j < $descriptionlength; ++$j)
				{
					if (!$res = unpack('Cdescription',fread($handle,1)))
						return false;
					$description .= chr($res['description']);
				}

				$this->addParameter($id, $name, $size, $type, $description, FALSE);
			}
			return true;
	}

	public function modifyIntervalFromId($cacheId, $data) {
		$result = parent::modifyIntervalFromId($cacheId, $data);

		foreach ($this->intervals as $interval)
		{
			if ($interval->getId() == $cacheId)
			{
				foreach($data as $key => $value) {
					$interval->setParamValue($key, $value);
					$interval->setIsModified(true);
					$this->isModified = true;
				}
				return $this->isModified;
			}
		}

		return false;
	}

	public function dump() {
		echo " => CatalogCacheObject : token = ".$this->token.", nb intervals = ".count($this->intervals).", last id = ".$this->lastId.", nb indexes = ".count($this->indexes).PHP_EOL;
		echo PHP_EOL;

		echo " => Parameters : ".PHP_EOL;
		foreach ($this->parameters as $parameter) {
			echo "    * id = ".$parameter['id'].", name = ".$parameter['name'].", size = ".$parameter['size'].", type = ".$parameter['type'].", description = ".$parameter['description'].PHP_EOL;
		}
		echo PHP_EOL;

		$this->filter->dump();
		echo PHP_EOL;

		$this->sort->dump();
		echo PHP_EOL;

		foreach ($this->intervals as $interval)
			$interval->dump();
		echo PHP_EOL;

		echo " => Indexes list : ";
		foreach ($this->indexes as $index)
		{
			echo $index.", ";
		}
		echo PHP_EOL;
	}
}

 ?>