@@ -16,14 +16,24 @@ class FormFilterAPI extends SelectQueryExtender {
16
16
*/
17
17
protected $ filterName ;
18
18
19
+ /**
20
+ * Joins params.
21
+ *
22
+ * @var array
23
+ */
24
+ protected $ joinParam = array ();
25
+
19
26
/**
20
27
* Sets up the filter name.
21
28
*
22
29
* @param string $filter_name
23
30
* A filter name.
24
31
*
25
- * @return object
32
+ * @return object The SearchQuery object.
26
33
* The SearchQuery object.
34
+ *
35
+ * @throws FormFilterAPIException
36
+ * Error if join params count < 3.
27
37
*/
28
38
public function processFilters ($ filter_name ) {
29
39
$ this ->filterName = $ filter_name ;
@@ -40,8 +50,8 @@ class FormFilterAPI extends SelectQueryExtender {
40
50
break ;
41
51
42
52
case 'operation ' :
43
- list ($ operation ,$ opval ) = explode (' ' , $ value ['value ' ]);
44
- $ this ->condition ($ field , $ opval , $ operation );
53
+ list ($ operation , $ operation_value ) = explode (' ' , $ value ['value ' ]);
54
+ $ this ->condition ($ field , $ operation_value , $ operation );
45
55
break ;
46
56
47
57
case 'expression ' :
@@ -56,6 +66,136 @@ class FormFilterAPI extends SelectQueryExtender {
56
66
}
57
67
}
58
68
69
+ /**
70
+ * Add to query left join:
71
+ * @code
72
+ * '#join' => array('left', array('users' => 'u'), array('%alias.uid', '=', 'pgt.uid')),
73
+ * @endcode
74
+ *
75
+ * Inner join, table name = alias:
76
+ * @code
77
+ * '#join' => array('inner', 'users', array('%alias.uid', '=', 'pgt.uid')),
78
+ * @endcode
79
+ *
80
+ *
81
+ * Many joins:
82
+ * @code
83
+ * '#join' => array(
84
+ * array('inner', array('users' => 'u'), array('%alias.uid', '=', 'pgt.uid')),
85
+ * array('left', 'users', array('%alias.uid', '=', 'pgt.uid')),
86
+ * ),
87
+ * @endcode
88
+ */
89
+ if (is_array ($ filters ['join ' ]) && !empty ($ filters ['join ' ])) {
90
+ foreach ($ filters ['join ' ] as $ joins ) {
91
+ if (is_string (reset ($ joins ))) {
92
+ $ joins = array ($ joins );
93
+ }
94
+
95
+ foreach ($ joins as $ join ) {
96
+ if (count ($ join ) == 3 ) {
97
+ $ this ->joinParam = $ join ;
98
+
99
+ $ type = $ this ->getJoinType ();
100
+ $ table = $ this ->getJoinTable ();
101
+ $ alias = $ this ->getJoinTableAlias ();
102
+ $ condition = $ this ->getJoinCondition ();
103
+
104
+ call_user_func_array (array ($ this , $ type ), array ($ table , $ alias , $ condition ));
105
+ }
106
+ else {
107
+ throw new FormFilterAPIException (t ('Join parameters should have 3 elements in the array. ' ));
108
+ }
109
+ }
110
+ }
111
+ }
112
+
59
113
return $ this ;
60
114
}
115
+
116
+ /**
117
+ * Get join type.
118
+ *
119
+ * Avaiable type:
120
+ * - leftJoin
121
+ * - innerJoin
122
+ *
123
+ * @return string
124
+ * Join type.
125
+ *
126
+ * @see FormFilterAPI::processFilters()
127
+ */
128
+ protected function getJoinType () {
129
+ $ type = strtolower ($ this ->joinParam [0 ]);
130
+
131
+ if (in_array ($ type , array ('left ' , 'leftjoin ' ))) {
132
+ return 'leftJoin ' ;
133
+ }
134
+
135
+ return 'innerJoin ' ;
136
+ }
137
+
138
+ /**
139
+ * Get name table for join.
140
+ *
141
+ * @return string
142
+ * Join table name.
143
+ *
144
+ * @see FormFilterAPI::processFilters()
145
+ */
146
+ protected function getJoinTable () {
147
+ $ table_name = is_array ($ this ->joinParam [1 ]) ? key ($ this ->joinParam [1 ]) : $ this ->joinParam [1 ];
148
+
149
+ if (!$ table_name ) {
150
+ $ table_name = $ this ->getJoinTableAlias ();
151
+ }
152
+
153
+ return $ table_name ;
154
+ }
155
+
156
+ /**
157
+ * Get alias table.
158
+ *
159
+ * If alias is not specified, alias = table name.
160
+ *
161
+ * @return string
162
+ * Alias table name.
163
+ *
164
+ * @see FormFilterAPI::processFilters()
165
+ */
166
+ protected function getJoinTableAlias () {
167
+ return is_array ($ this ->joinParam [1 ]) ? reset ($ this ->joinParam [1 ]) : $ this ->joinParam [1 ];
168
+ }
169
+
170
+ /**
171
+ * Get join condition.
172
+ *
173
+ * @return string
174
+ * Condition string. Example: u.uid=t.uid.
175
+ *
176
+ * @throws FormFilterAPIException
177
+ * If join condition not have 3 elements if the array throw exception.
178
+ *
179
+ * @see FormFilterAPI::processFilters()
180
+ */
181
+ protected function getJoinCondition () {
182
+ $ condition = $ this ->joinParam [2 ];
183
+
184
+ if (is_string ($ condition )) {
185
+ return $ condition ;
186
+ }
187
+
188
+ if (count ($ condition ) < 3 ) {
189
+ throw new FormFilterAPIException (t ('Join conditions must contain 3 elements of the array. ' ));
190
+ }
191
+
192
+ return implode (' ' , $ condition );
193
+ }
61
194
}
195
+
196
+ /**
197
+ * Exception thrown by FormFilterAPI().
198
+ *
199
+ * Error caused when checking parameters join.
200
+ */
201
+ class FormFilterAPIException extends Exception {}
0 commit comments