-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublishing_options_facets.module
executable file
·210 lines (185 loc) · 5.93 KB
/
publishing_options_facets.module
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
202
203
204
205
206
207
208
209
<?php
/**
* @file
* Provides facets for publishing options.
*/
require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search.inc');
/**
* Implementation of hook_faceted_search_collect().
*/
function publishing_options_facets_faceted_search_collect(&$facets, $domain, $env, $selection, $arg = NULL) {
$publishing_options = array(
'sticky' => t('Sticky'),
'status' => t('Published'),
'promote' => t('Front page'),
'moderate' => t('Moderated'),
);
switch ($domain) {
case 'facets':
// If the Publishing Options facet is allowed.
foreach ($publishing_options as $publishing_option => $label) {
if (!isset($selection) || isset($selection[$publishing_option][1])) {
$facets[] = new publishing_options_facet($publishing_option, $label);
}
}
break;
case 'text':
// If the Publishing Options facet is allowed.
foreach ($publishing_options as $publishing_option => $label) {
if (!isset($selection) || isset($selection[$publishing_option][1])) {
// Scan the search text for a 'type:state' token, and extract a
// facet from it.
if ($state = search_query_extract($arg, $publishing_option)) {
// Create a facet with the state found in search text as the active
// category.
if (($state == 'y') || ($state == 'n')) {
$facets[] = new publishing_options_facet($publishing_option, $label, $state);
}
// Remove the parsed token from the search text.
$arg = search_query_insert($arg, $publishing_option);
}
}
}
return $arg;
case 'node':
// If the Publishing Options facet is allowed.
foreach ($publishing_options as $publishing_option => $label) {
if (!isset($selection) || isset($selection[$publishing_option][1])) {
// Create a facet with the node's state as the active category.
$facets[] = new publishing_options_facet($publishing_option, $label, $arg->$publishing_option ? 'y' : 'n');
}
}
break;
}
}
/**
* A publishing option based facet.
*/
class publishing_options_facet extends faceted_search_facet {
var $_option_name;
var $_label;
/**
* Constructor. Optionally assigns the active state of the facet.
*
* @param $state
* Optional. State to set as this facet's active category.
*/
function publishing_options_facet($option_name, $label, $state = NULL) {
$this->_option_name = $option_name;
$this->_label = $label;
$active_path = array();
if ($state) {
$active_path[] = new publishing_options_facet_category($this->_option_name, $state);
}
parent::faceted_search_facet($this->_option_name, $active_path);
}
function get_id() {
return 1; // There is only one instance of this facet
}
function get_label() {
return $this->_label;
}
/**
* Returns the available sort options for this facet.
*/
function get_sort_options() {
$options = parent::get_sort_options();
$options['value'] = t('Value');
return $options;
}
/**
* Handler for the 'count' sort criteria.
*/
function build_sort_query_count(&$query) {
$query->add_orderby('count', 'DESC');
$query->add_orderby("n_{$this->_option_name}", 'ASC');
}
/**
* Handler for the 'value' sort criteria.
*/
function build_sort_query_value(&$query) {
$query->add_orderby("n_{$this->_option_name}", 'DESC');
}
/**
* Return the search text for this facet, taking into account this facet's
* active path.
*/
function get_text() {
if ($category = $this->get_active_category()) {
return $category->_status; // This call breaks encapsulation...
}
return '';
}
/**
* Updates a query for retrieving the root categories of this facet and their
* associated nodes within the current search results.
*
* @param $query
* The query object to update.
*
* @return
* FALSE if this facet can't have root categories.
*/
function build_root_categories_query(&$query) {
$query->add_field('n', $this->_option_name);
$query->add_groupby("n_{$this->_option_name}");
return TRUE;
}
/**
* This factory method creates categories given query results that include the
* fields selected in get_root_categories_query() or get_subcategories_query().
*
* @param $results
* $results A database query result resource.
*
* @return
* Array of categories.
*/
function build_categories($results) {
$categories = array();
while ($result = db_fetch_object($results)) {
$column = "n_{$this->_option_name}";
$categories[] = new publishing_options_facet_category($this->_option_name, $result->$column ? 'y' : 'n', $result->count);
}
return $categories;
}
}
/**
* A publishing option based facet category.
*/
class publishing_options_facet_category extends faceted_search_category {
var $_option_name;
var $_status = '';
/**
* Constructor.
*/
function publishing_options_facet_category($option_name, $status, $count = NULL) {
parent::faceted_search_category($count);
$this->_option_name = $option_name;
$this->_status = $status;
}
/**
* Return the label of this category.
*
* @param $html
* TRUE when HTML is allowed in the label, FALSE otherwise. Checking this
* flag allows implementors to provide a rich-text label if desired, and an
* alternate plain text version for cases where HTML cannot be used. The
* implementor is responsible to ensure adequate security filtering.
*/
function get_label($html = FALSE) {
if ($this->_status == 'y') {
return t('Yes');
}
return t('No');
}
/**
* Updates a query for selecting nodes matching this category.
*
* @param $query
* The query object to update.
*/
function build_results_query(&$query) {
$query->add_where("n.{$this->_option_name} = '%s'", $this->_status == 'y' ? 1 : 0);
}
}