CatalogCacheSortObject.php 3.55 KB
<?php

class CatalogCacheSortPartObject extends TimeTableCacheSortPartObject
{
	public static $TYPE_PARAMETER = 7;

	private $paramId = "";

	public function compare($interval_a, $interval_b) {
		if ($this->type != self::$TYPE_PARAMETER) {
			return parent::compare($interval_a, $interval_b) ;
		}

		$params_a = $interval_a->getParams();
		$params_b = $interval_b->getParams();

		// Retrieve data type
		$data_type = -1;
		foreach ($this->cacheObject->getParametersInfo() as $parameter) {
			if ($parameter['id'] == $this->paramId) {
				$data_type = $parameter['type'];
				break;
			}
		}

		//If something is not defined
		if ($data_type < 0) {
			return 0;
		}
		else if (!isset($params_a[$this->paramId]) && !isset($params_b[$this->paramId])) {
			return 0;
		}
		else if(!isset($params_a[$this->paramId])) {
			switch ($this->dir) {
				case self::$DIRECTION_ASC :
					return 1;
				default :
					return -1;
			}
		}
		else if (!isset($params_b[$this->paramId])) {
			switch ($this->dir) {
				case self::$DIRECTION_ASC :
					return -1;
				default :
					return 1;
			}
		}

			// Apply comparison on intervals parameter data
		switch ($parameter['type']) {
			case 0: //double
				$val_a_minus_b = floatval($params_a[$this->paramId]) - floatval($params_b[$this->paramId]);
			  break;
			case 1: //date (timestamp)
				$val_a_minus_b = intval($params_a[$this->paramId]) - intval($params_b[$this->paramId]);
				break;
			case 2: //string
				$val_a_minus_b = strcmp($params_a[$this->paramId], $params_b[$this->paramId]);
				break;
			case 3: //int
				$val_a_minus_b = intval($params_a[$this->paramId]) - intval($params_b[$this->paramId]);
				break;
			default: // not defined => string
				$val_a_minus_b = strcmp($params_a[$this->paramId], $params_b[$this->paramId]);
		}

		if ($this->dir == self::$DIRECTION_ASC) {
			return -$val_a_minus_b;
		}
		return $val_a_minus_b;
	}

	public function loadFromObject($part_obj) {
		parent::loadFromObject($part_obj);
		if ($this->type == self::$TYPE_UNKNOWN) {
			//Check if it's a catalog parameter
			foreach ($this->cacheObject->getParametersInfo() as $parameter) {
				if ($parameter['id'] == $part_obj->property) {
					$this->type = self::$TYPE_PARAMETER;
					$this->paramId = $parameter['id'];
				}
			}
		}
	}

	public function writeBin($handle) {
		parent::writeBin($handle);
		if ($this->type == self::$TYPE_PARAMETER) {
				//Param Id length
			fwrite($handle,pack('L',strlen($this->paramId)));

				//Param Id
			for ($i = 0; $i < strlen($this->paramId); ++$i)
				fwrite($handle,pack('C',ord($this->paramId[$i])));
		}
	}

	public function loadBin($handle) {
		parent::loadBin($handle);
		if ($this->type == self::$TYPE_PARAMETER) {
			  //Param Id length
			if (!$res = unpack('Lidlength',fread($handle,4)))
				return false;
			$idlength = $res['idlength'];

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

	public function dump() {
		if ($this->type == self::$TYPE_PARAMETER) {
			echo "   => ".get_class($this)." : type = parameter, id = ".$this->paramId;
			echo ", direction = ";
			switch ($this->dir)
			{
				case self::$DIRECTION_ASC :
					echo "ASC";
					break;
				case self::$DIRECTION_DES :
					echo "DESC";
					break;
				default:
					echo "unknown";
			}
			echo PHP_EOL;
			return;
		}
		parent::dump();
	}
}

class CatalogCacheSortObject extends TimeTableCacheSortObject
{
	protected function createNewPart() {
		return new CatalogCacheSortPartObject($this->cacheObject);
	}
}

 ?>