Skip to content

Commit 6da9917

Browse files
Merge branch "master" with "issue15". Conflicts:
formfilterapi.test formfilterapi.api.php
2 parents d2f0792 + 2d718de commit 6da9917

6 files changed

+291
-66
lines changed

formfilterapi.api.php

+35-10
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@
1111
* Via this hook possible to change variables before processing their module.
1212
* And add your form elements, for filtering.
1313
*
14-
* @param string $op
14+
* @param string $operation
1515
* Name of the operation is executed.
16-
*
1716
* @param string $session_name
18-
* The session name form.
17+
* The unique session name.
1918
*
20-
* @param array $sessvals
21-
* Field before filtration.
19+
* @return array
20+
* Form with filter elements.
2221
*/
23-
function hook_formfilterapi($op, $session_name, &$sessvals = array()) {
24-
$example_session_name = 'test';
25-
26-
switch ($op) {
22+
function hook_formfilterapi($operation, $session_name) {
23+
switch ($operation) {
2724
case 'filters':
28-
if ($session_name == $example_session_name) {
25+
if ($session_name == 'test') {
2926
$form['title'] = array(
3027
'#type' => 'textfield',
3128
'#title' => t('Title'),
@@ -43,3 +40,31 @@ function hook_formfilterapi($op, $session_name, &$sessvals = array()) {
4340
break;
4441
}
4542
}
43+
44+
/**
45+
* Alter filters for hook_formfilterapi().
46+
*
47+
* @param array $filters
48+
* Filters from hook_formfilterapi().
49+
* @param string $session_name
50+
* The unique session name.
51+
* @param string $operation
52+
* Name of the operation is executed.
53+
*/
54+
function hook_formfilterapi_alter(&$filters, $session_name, $operation) {
55+
switch ($operation) {
56+
case 'filters':
57+
if ($session_name == 'test') {
58+
$filters['sticky'] = array(
59+
'#type' => 'select',
60+
'#title' => t('Sticky at top of lists'),
61+
'#options' => array(
62+
'1' => t('Yes'),
63+
'0' => t('No'),
64+
),
65+
'#where' => 'n.sticky',
66+
);
67+
}
68+
break;
69+
}
70+
}

formfilterapi.css

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
form.formfilterapiform {
1+
form.formfilterapi {
22
background: url(search.png) no-repeat;
33
background-position-x: 15px;
44
background-position-y: 15px;
55
padding: 2px 2px 2px 160px;
66
}
77

8-
form.formfilterapiform div.active {
8+
form.formfilterapi div.active {
99
background-color: #c1fdbf;
1010
border: 1px solid #acec88;
1111
padding: 2px;
1212
margin: 2px;
1313
}
1414

15-
form.formfilterapiform div.inactive {
15+
form.formfilterapi div.inactive {
1616
color: gray;
1717
padding: 2px;
1818
margin: 2px;
1919
}
2020

21-
form.formfilterapiform .form-item label {
21+
form.formfilterapi .form-item label {
2222
float: left;
2323
margin: 2px 10px 2px 0px;
2424
padding: 1px;
@@ -28,18 +28,18 @@ form.formfilterapiform .form-item label {
2828
vertical-align: bottom;
2929
}
3030

31-
form.formfilterapiform .form-item input,
32-
form.formfilterapiform .form-item select {
31+
form.formfilterapi .form-item input,
32+
form.formfilterapi .form-item select {
3333
width: 45%;
3434
}
3535

36-
form.formfilterapiform .buttons {
36+
form.formfilterapi .buttons {
3737
height: 30px;
3838
padding-right: 20px;
3939
padding-top: 5px;
4040
}
4141

42-
form.formfilterapiform .buttons input {
42+
form.formfilterapi .buttons input {
4343
float: right;
4444
margin-left: 5px;
4545
}

formfilterapi.extender.inc

+143-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ class FormFilterAPI extends SelectQueryExtender {
1616
*/
1717
protected $filterName;
1818

19+
/**
20+
* Joins params.
21+
*
22+
* @var array
23+
*/
24+
protected $joinParam = array();
25+
1926
/**
2027
* Sets up the filter name.
2128
*
2229
* @param string $filter_name
2330
* A filter name.
2431
*
25-
* @return object
32+
* @return object The SearchQuery object.
2633
* The SearchQuery object.
34+
*
35+
* @throws FormFilterAPIException
36+
* Error if join params count < 3.
2737
*/
2838
public function processFilters($filter_name) {
2939
$this->filterName = $filter_name;
@@ -40,8 +50,8 @@ class FormFilterAPI extends SelectQueryExtender {
4050
break;
4151

4252
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);
4555
break;
4656

4757
case 'expression':
@@ -56,6 +66,136 @@ class FormFilterAPI extends SelectQueryExtender {
5666
}
5767
}
5868

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+
59113
return $this;
60114
}
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+
}
61194
}
195+
196+
/**
197+
* Exception thrown by FormFilterAPI().
198+
*
199+
* Error caused when checking parameters join.
200+
*/
201+
class FormFilterAPIException extends Exception {}

0 commit comments

Comments
 (0)