/[drupal]/contributions/modules/faceted_search/content_type_facet.module
ViewVC logotype

Contents of /contributions/modules/faceted_search/content_type_facet.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.21 - (show annotations) (download) (as text)
Tue Mar 3 05:13:12 2009 UTC (8 months, 3 weeks ago) by davidlesieur
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA2, HEAD
Changes since 1.20: +6 -2 lines
File MIME type: text/x-php
Localization support for content type names.
1 <?php
2 // $Id: content_type_facet.module,v 1.20 2009/01/04 19:36:25 davidlesieur Exp $
3
4 /**
5 * @file
6 * Provides a facet for content types.
7 */
8
9 require_once('./'. drupal_get_path('module', 'faceted_search') .'/faceted_search.inc');
10
11 /**
12 * Implementation of hook_form_alter().
13 */
14 function content_type_facet_form_faceted_search_edit_form_alter(&$form, &$form_state) {
15 $env = $form['env']['#value'];
16
17 $form['content_type_facet'] = array(
18 '#type' => 'fieldset',
19 '#title' => t('Content Type Facet options'),
20 '#collapsible' => TRUE,
21 '#collapsed' => TRUE,
22 '#weight' => 10,
23 );
24
25 $types = array();
26 foreach (array_keys(node_get_types('names')) as $type) {
27 $types[$type] = $type;
28 }
29 if (count($types)) {
30 $names = node_get_types('names');
31 faceted_search_localize_types($names);
32 $form['content_type_facet']['content_type_facet_types'] = array(
33 '#type' => 'checkboxes',
34 '#title' => t('Content types to display in the Content type facet'),
35 '#description' => t('Only the checked types will appear in the Content type facet. If none is checked, all types allowed by the search environment may appear in the facet. Note that this setting is not a filter for search results; all content types allowed by the search environment will still be searchable through other facets or from text searches.'),
36 '#options' => $names,
37 '#default_value' => array_intersect($types, array_filter($env->settings['content_type_facet_types'])),
38 );
39 }
40 else {
41 $form['content_type_facet']['content_type_facet_help'] = array(
42 '#type' => 'markup',
43 '#value' => '<p>'. t('No content types are currently defined.') .'</p>',
44 );
45 }
46 }
47
48 /**
49 * Implementation of hook_faceted_search_collect().
50 */
51 function content_type_facet_faceted_search_collect(&$facets, $domain, $env, $selection, $arg = NULL) {
52 switch ($domain) {
53 case 'facets':
54 // If the content type facet is allowed.
55 if (!isset($selection) || isset($selection['content_type'][1])) {
56 $types = content_type_facet_get_types($env);
57 $facets[] = new content_type_facet($types);
58 }
59 break;
60
61 case 'text':
62 // If the content type facet is allowed.
63 if (!isset($selection) || isset($selection['content_type'][1])) {
64 // Find the allowed types.
65 $all_types = array();
66 foreach (array_keys(node_get_types('names')) as $type) {
67 $all_types[$type] = $type;
68 }
69 $types = content_type_facet_get_types($env);
70 if (empty($types)) {
71 $allowed_types = $all_types;
72 }
73 else {
74 $allowed_types = array_intersect($types, $all_types);
75 }
76
77 // Scan the search text for a 'content_type:type' token, and extract a
78 // facet from it.
79 if ($type = search_query_extract($arg, 'content_type')) {
80 // Create a facet with the type found in search text as the active
81 // category.
82 if (isset($allowed_types[$type])) {
83 $facets[] = new content_type_facet($types, $type);
84 }
85 // Remove the parsed token from the search text.
86 $arg = search_query_insert($arg, 'content_type');
87 }
88 }
89 return $arg;
90
91 case 'node':
92 // If the content type facet is allowed.
93 if (!isset($selection) || isset($selection['content_type'][1])) {
94 $types = content_type_facet_get_types($env);
95 if (empty($types) || isset($types[$arg->type])) {
96 // Create a facet with the node's type as the active category.
97 $facets[] = new content_type_facet($types, $arg->type);
98 }
99 }
100 break;
101 }
102 }
103
104 /**
105 * Implementation of hook_faceted_search_init().
106 */
107 function content_type_facet_faceted_search_init(&$env) {
108 $env->settings['content_type_facet_types'] = array();
109 }
110
111 /**
112 * Return an array of all node types that are allowed to be shown in the content
113 * type facet.
114 *
115 * @return
116 * Array of type names, or an empty array if all types are allowed.
117 */
118 function content_type_facet_get_types($env) {
119 $allowed_types = faceted_search_types($env);
120 $types = array_filter($env->settings['content_type_facet_types']);
121 if (empty($allowed_types) || empty($types)) {
122 // All types are allowed (if there are restrictions in the environment,
123 // they're going to be already built into the query).
124 return $types;
125 }
126 else {
127 // Only return types that are allowed by the environment.
128 $types = array_intersect($types, $allowed_types);
129 if (count($types) == count($allowed_types)) {
130 // All types are selected; do the same as if none was.
131 return array();
132 }
133 return $types;
134 }
135 }
136
137 /**
138 * A node-type based facet.
139 */
140 class content_type_facet extends faceted_search_facet {
141
142 var $_types = array();
143
144 /**
145 * Constructor. Optionally assigns the active type of the facet.
146 *
147 * @param $types
148 * Array of possible type names for this facet, or empty array if all types
149 * are allowed.
150 * @param $type
151 * Optional. Type to set as this facet's active category.
152 */
153 function content_type_facet($types, $type = NULL) {
154 $active_path = array();
155 if ($type) {
156 $active_path[] = new content_type_facet_category($type, node_get_types('name', $type));
157 }
158 parent::faceted_search_facet('content_type', $active_path);
159 $this->_types = $types;
160 }
161
162 function get_id() {
163 return 1; // This module provides only one facet
164 }
165
166 function get_label() {
167 return t('Content type');
168 }
169
170 /**
171 * Returns the available sort options for this facet.
172 */
173 function get_sort_options() {
174 $options = parent::get_sort_options();
175 $options['type'] = t('Type');
176 return $options;
177 }
178
179 /**
180 * Handler for the 'count' sort criteria.
181 */
182 function build_sort_query_count(&$query) {
183 $query->add_orderby('count', 'DESC');
184 $query->add_orderby('node_type_name', 'ASC');
185 }
186
187 /**
188 * Handler for the 'type' sort criteria.
189 */
190 function build_sort_query_type(&$query) {
191 $query->add_orderby('node_type_name', 'ASC');
192 }
193
194 /**
195 * Return the search text for this facet, taking into account this facet's
196 * active path.
197 */
198 function get_text() {
199 if ($category = $this->get_active_category()) {
200 return $category->_type;
201 }
202 return '';
203 }
204
205 /**
206 * Updates a query for retrieving the root categories of this facet and their
207 * associated nodes within the current search results.
208 *
209 * @param $query
210 * The query object to update.
211 *
212 * @return
213 * FALSE if this facet can't have root categories.
214 */
215 function build_root_categories_query(&$query) {
216 $query->add_table('node_type', 'type', 'n', 'type');
217 $query->add_field('node_type', 'type');
218 $query->add_field('node_type', 'name');
219 if (!empty($this->_types)) {
220 $query->add_where("node_type.type IN ('". implode("', '", $this->_types) ."')");
221 }
222 $query->add_groupby('node_type_type');
223 return TRUE;
224 }
225
226 /**
227 * This factory method creates categories given query results that include the
228 * fields selected in get_root_categories_query() or get_subcategories_query().
229 *
230 * @param $results
231 * $results A database query result resource.
232 *
233 * @return
234 * Array of categories.
235 */
236 function build_categories($results) {
237 $categories = array();
238 while ($result = db_fetch_object($results)) {
239 $categories[] = new content_type_facet_category($result->node_type_type, $result->node_type_name, $result->count);
240 }
241 return $categories;
242 }
243 }
244
245 /**
246 * A node-type based facet category.
247 */
248 class content_type_facet_category extends faceted_search_category {
249 var $_type = NULL;
250 var $_name = '';
251
252 /**
253 * Constructor.
254 */
255 function content_type_facet_category($type, $name, $count = NULL) {
256 parent::faceted_search_category($count);
257 $this->_type = $type;
258 $this->_name = $name;
259 faceted_search_localize_type($this->_type, $this->_name);
260 }
261
262 /**
263 * Return the label of this category.
264 *
265 * @param $html
266 * TRUE when HTML is allowed in the label, FALSE otherwise. Checking this
267 * flag allows implementors to provide a rich-text label if desired, and an
268 * alternate plain text version for cases where HTML cannot be used. The
269 * implementor is responsible to ensure adequate security filtering.
270 */
271 function get_label($html = FALSE) {
272 return check_plain($this->_name);
273 }
274
275 /**
276 * Updates a query for selecting nodes matching this category.
277 *
278 * @param $query
279 * The query object to update.
280 */
281 function build_results_query(&$query) {
282 $query->add_where("n.type = '%s'", $this->_type);
283 }
284 }
285

  ViewVC Help
Powered by ViewVC 1.1.2