Blame view

php/classes/FilterRes.php 1.65 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
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
<?php
  abstract class FilterRes
  {
  	abstract function getCondResult($cond);
  	
  	abstract function getAll();
  	
  	function andOp($cond_res1,$cond_res2)
  	{
  		$result = array();
  		for ($i = 0; $i < count($cond_res1); $i++)
  		  for ($j = 0; $j < count($cond_res2); $j++)
  		    if ($cond_res1[$i] == $cond_res2[$j])
  		    {
  		    	array_push($result,$cond_res1[$i]);
  		    	break;
  		    }
  		return $result;
  	}
  	
  	function orOp($cond_res1,$cond_res2)
  	{
  		$result = $cond_res1;		
  		for ($i = 0; $i < count($cond_res2); $i++)
  		{
  		  $found = false;
  		  for ($j = 0; $j < count($result); $j++)
  		    if ($cond_res2[$i] == $result[$j])
  		    {
  		       	$found = true;
  		    	break;
  		    }
  		  if (!$found)
  		  	array_push($result,$cond_res2[$i]);
  		}
  		return $result;
  	}
  	
  	function getResult($filter,$isEmptyFilter)
  	{  		
  		if ($isEmptyFilter)
  			return $this->getAll();
  		
  		$ops = array();
  		for ($i = 0 ; $i < count($filter); $i++)
  		{
  			$cond = $filter[$i];
  			$cond_res[$i] = $this->getCondResult($cond);
  			$ops[$i] = $cond['logical'];
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'and')
  				continue;
  			$cond_res[$i-1] = $this->andOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;		
  		}
  		
  		for ($i = 1; $i < count($cond_res); $i++)
  		{
  			if ($ops[$i] != 'or')
  				continue;
  			$cond_res[$i-1] = $this->orOp($cond_res[$i-1],$cond_res[$i]);
  			array_splice($cond_res, $i, 1);
  			array_splice($ops, $i, 1);
			$i--;			
  		}
  		
  		return $cond_res[0];
  	}
  }
?>