Blame view

php/classes/CatalogCacheSortObject.php 3.55 KB
ab7c3c63   Benjamin Renard   Sort parameter co...
1
2
3
4
<?php

class CatalogCacheSortPartObject extends TimeTableCacheSortPartObject
{
d93ac685   Hacene SI HADJ MOHAND   ok pour 7519
5
	public static $TYPE_PARAMETER = 7;
ab7c3c63   Benjamin Renard   Sort parameter co...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

	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);
	}
}

 ?>