-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformfilterapi.extender.inc
201 lines (176 loc) · 4.64 KB
/
formfilterapi.extender.inc
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @file
* Filter query extender and helper functions.
*/
/**
* Query extender for form query condition.
*/
class FormFilterAPI extends SelectQueryExtender {
/**
* The filter name.
*
* @var string
*/
protected $filterName;
/**
* Joins params.
*
* @var array
*/
protected $joinParam = array();
/**
* Sets up the filter name.
*
* @param string $filter_name
* A filter name.
*
* @return object The SearchQuery object.
* The SearchQuery object.
*
* @throws FormFilterAPIException
* Error if join params count < 3.
*/
public function processFilters($filter_name) {
$this->filterName = $filter_name;
$filters = formfilterapi_build_filter_query($this->filterName);
if (is_array($filters['where'])) {
foreach ($filters['where'] as $cond) {
foreach ($cond as $field => $value) {
if (is_array($value)) {
switch ($value['op']) {
case 'like':
$this->condition($field, "%" . $value['value'] . "%", 'LIKE');
break;
case 'operation':
list($operation, $operation_value) = explode(' ', $value['value']);
$this->condition($field, $operation_value, $operation);
break;
case 'expression':
$this->where($field, array(':value' => $value['value']));
break;
}
}
else {
$this->condition($field, $value);
}
}
}
}
/**
* Add to query left join:
* @code
* '#join' => array('left', array('users' => 'u'), array('%alias.uid', '=', 'pgt.uid')),
* @endcode
*
* Inner join, table name = alias:
* @code
* '#join' => array('inner', 'users', array('%alias.uid', '=', 'pgt.uid')),
* @endcode
*
*
* Many joins:
* @code
* '#join' => array(
* array('inner', array('users' => 'u'), array('%alias.uid', '=', 'pgt.uid')),
* array('left', 'users', array('%alias.uid', '=', 'pgt.uid')),
* ),
* @endcode
*/
if (is_array($filters['join']) && !empty($filters['join'])) {
foreach ($filters['join'] as $joins) {
if (is_string(reset($joins))) {
$joins = array($joins);
}
foreach ($joins as $join) {
if (count($join) == 3) {
$this->joinParam = $join;
$type = $this->getJoinType();
$table = $this->getJoinTable();
$alias = $this->getJoinTableAlias();
$condition = $this->getJoinCondition();
call_user_func_array(array($this, $type), array($table, $alias, $condition));
}
else {
throw new FormFilterAPIException(t('Join parameters should have 3 elements in the array.'));
}
}
}
}
return $this;
}
/**
* Get join type.
*
* Avaiable type:
* - leftJoin
* - innerJoin.
*
* @return string
* Join type.
*
* @see FormFilterAPI::processFilters()
*/
protected function getJoinType() {
$type = strtolower($this->joinParam[0]);
if (in_array($type, array('left', 'leftjoin'))) {
return 'leftJoin';
}
return 'innerJoin';
}
/**
* Get name table for join.
*
* @return string
* Join table name.
*
* @see FormFilterAPI::processFilters()
*/
protected function getJoinTable() {
$table_name = is_array($this->joinParam[1]) ? key($this->joinParam[1]) : $this->joinParam[1];
if (!$table_name) {
$table_name = $this->getJoinTableAlias();
}
return $table_name;
}
/**
* Get alias table.
*
* If alias is not specified, alias = table name.
*
* @return string
* Alias table name.
*
* @see FormFilterAPI::processFilters()
*/
protected function getJoinTableAlias() {
return is_array($this->joinParam[1]) ? reset($this->joinParam[1]) : $this->joinParam[1];
}
/**
* Get join condition.
*
* @return string
* Condition string. Example: u.uid=t.uid.
*
* @throws FormFilterAPIException
* If join condition not have 3 elements if the array throw exception.
*
* @see FormFilterAPI::processFilters()
*/
protected function getJoinCondition() {
$condition = $this->joinParam[2];
if (is_string($condition)) {
return $condition;
}
if (count($condition) < 3) {
throw new FormFilterAPIException(t('Join conditions must contain 3 elements of the array.'));
}
return implode(' ', $condition);
}
}
/**
* Exception thrown by FormFilterAPI().
*
* Error caused when checking parameters join.
*/
class FormFilterAPIException extends Exception {}