1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 12 13 14 15 16
17 class FOFModelBehaviorFilters extends FOFModelBehavior
18 {
19 20 21 22 23 24 25 26 27
28 public function onAfterBuildQuery(&$model, &$query)
29 {
30 $table = $model->getTable();
31 $tableName = $table->getTableName();
32 $tableKey = $table->getKeyName();
33 $db = $model->getDBO();
34
35 $filterzero = $model->getState('_emptynonzero', null);
36
37 $fields = $model->getTableFields();
38 $backlist = $model->blacklistFilters();
39
40 foreach ($fields as $fieldname => $fieldtype)
41 {
42 if (in_array($fieldname, $backlist)) {
43 continue;
44 }
45 $field = new stdClass;
46 $field->name = $fieldname;
47 $field->type = $fieldtype;
48 $field->filterzero = $filterzero;
49
50 $filterName = ($field->name == $tableKey) ? 'id' : $field->name;
51 $filterState = $model->getState($filterName, null);
52
53 $field = FOFModelField::getField($field, array('dbo' => $db, 'table_alias' => $model->getTableAlias()));
54
55 if ((is_array($filterState) && (
56 array_key_exists('value', $filterState) ||
57 array_key_exists('from', $filterState) ||
58 array_key_exists('to', $filterState)
59 )) || is_object($filterState))
60 {
61 $options = new JRegistry($filterState);
62 }
63 else
64 {
65 $options = new JRegistry;
66 $options->set('value', $filterState);
67 }
68
69 $methods = $field->getSearchMethods();
70 $method = $options->get('method', $field->getDefaultSearchMethod());
71
72 if (!in_array($method, $methods))
73 {
74 $method = 'exact';
75 }
76
77 switch ($method)
78 {
79 case 'between':
80 case 'outside':
81 case 'range' :
82 $sql = $field->$method($options->get('from', null), $options->get('to'));
83 break;
84
85 case 'interval':
86 case 'modulo':
87 $sql = $field->$method($options->get('value', null), $options->get('interval'));
88 break;
89
90 case 'exact':
91 case 'partial':
92 case 'search':
93 default:
94 $sql = $field->$method($options->get('value', null));
95 break;
96 }
97
98 if ($sql)
99 {
100 $query->where($sql);
101 }
102 }
103 }
104 }
105