PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/request/domain/Filter.php

http://zoop.googlecode.com/
PHP | 84 lines | 77 code | 6 blank | 1 comment | 27 complexity | 65aaac54af1e51733b738c9e04a6c1c6 MD5 | raw file
 1<?php
 2class Filter extends DbObject
 3{
 4	static public function jsonToSql($json)
 5	{
 6		return self::sqlify(json_decode($json)->subs[0]);
 7	}
 8	
 9	static function sqlify($data)
10	{
11		$parts = array();
12		foreach($data->fields as $field)
13			$parts[] = self::sqlifyField($field);
14		foreach($data->subs as $sub)
15			$parts[] = '(' . self::sqlify($sub) . ')';
16		$boolOp = $data->anyall == 'any' ? ' OR ' : ' AND ';
17		return implode($boolOp, $parts);
18	}
19
20	static function sqlifyField($field)
21	{
22		$config = Config::get('app.filter');
23		$name = $config['fields'][$field->field]['field'];
24		$type = $config['fields'][$field->field]['type'];
25		$operator = $field->operator;
26		$widgit = $config['type_operator_map'][$type][$field->operator]['widgit'];
27
28		// echo_r(array($type, $operator, $widgit));
29
30		$left = $name;
31
32		if($operator == 'is')
33		{
34			$op = '=';
35			$right = $field->operandValues->menu;
36		}
37		else if($operator == 'is_not')
38		{
39			$op = '<>';
40			$right = $field->operandValues->menu;
41		}
42		else if($operator == 'in')
43		{
44			$op = 'IN';
45			$right = '(' . implode(', ', $field->operandValues->multi) . ')';
46		}
47		else if($operator == 'not_in')
48		{
49			$op = 'NOT IN';
50			$right = '(' . implode(', ', $field->operandValues->multi) . ')';
51		}
52		else if($operator == 'less_than')
53		{
54			$op = ' < ';
55			$right = $field->operandValues->menu;
56		}
57		else if($operator == 'greater_than')
58		{
59			$op = ' > ';
60			$right = $field->operandValues->menu;
61		}
62		else if($operator == 'less_than_equal')
63		{
64			$op = ' <= ';
65			$right = $field->operandValues->menu;
66		}
67		else if($operator == 'greater_than_equal')
68		{
69			$op = ' >= ';
70			$right = $field->operandValues->menu;
71		}
72		else if($operator == 'in_the_range')
73		{
74			$all = "($left >= {$field->operandValues->menu1} AND $left <= {$field->operandValues->menu2})";
75			return $all;
76		}
77		else
78		{
79			trigger_error("unrecognized operator '$operator'");
80		}
81
82		return "($left $op $right)";
83	}
84}