| 1 |
<?php
|
| 2 |
// $Id: author_facet.module,v 1.16 2009/01/04 19:36:25 davidlesieur Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides a facet for content authors.
|
| 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 author_facet_form_faceted_search_edit_form_alter(&$form, $form_state) {
|
| 15 |
$env = $form['env']['#value'];
|
| 16 |
|
| 17 |
$form['author_facet'] = array(
|
| 18 |
'#type' => 'fieldset',
|
| 19 |
'#title' => t('Author Facet options'),
|
| 20 |
'#collapsible' => TRUE,
|
| 21 |
'#collapsed' => TRUE,
|
| 22 |
'#weight' => 10,
|
| 23 |
);
|
| 24 |
|
| 25 |
$roles = array_filter(user_roles(TRUE), '_author_facet_filter_role');
|
| 26 |
if (count($roles)) {
|
| 27 |
$form['author_facet']['author_facet_excluded_roles'] = array( // TODO: Invert the logic, and ensure that all roles are included when none is checked.
|
| 28 |
'#type' => 'checkboxes',
|
| 29 |
'#title' => t('Roles to exclude from the Author facet'),
|
| 30 |
'#description' => t('Any user having one or more of the checked roles will not appear in the facet. Note that this setting is not a filter for search results; content from excluded users can still be searchable through other facets or from text searches.'),
|
| 31 |
'#options' => $roles,
|
| 32 |
'#default_value' => author_facet_excluded_roles($env),
|
| 33 |
);
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
$form['author_facet']['author_facet_help'] = array(
|
| 37 |
'#type' => 'markup',
|
| 38 |
'#value' => '<p>'. t('No roles are currently available.') .'</p>',
|
| 39 |
);
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_faceted_search_collect().
|
| 45 |
*/
|
| 46 |
function author_facet_faceted_search_collect(&$facets, $domain, $env, $selection, $arg = NULL) {
|
| 47 |
switch ($domain) {
|
| 48 |
case 'facets':
|
| 49 |
// If the author facet is allowed.
|
| 50 |
if (!isset($selection) || isset($selection['author'][1])) {
|
| 51 |
$excluded_roles = author_facet_excluded_roles($env);
|
| 52 |
$facets[] = new author_facet($excluded_roles);
|
| 53 |
}
|
| 54 |
break;
|
| 55 |
|
| 56 |
case 'text':
|
| 57 |
// If the author facet is allowed.
|
| 58 |
if (!isset($selection) || isset($selection['author'][1])) {
|
| 59 |
// Scan the search text for a 'author:uid' token, and extract a
|
| 60 |
// facet from it.
|
| 61 |
$uid = search_query_extract($arg, 'author');
|
| 62 |
if (is_numeric($uid)) {
|
| 63 |
$excluded_roles = author_facet_excluded_roles($env);
|
| 64 |
|
| 65 |
if ($name = author_facet_get_user_name($env, $uid, $excluded_roles)) {
|
| 66 |
// Create a facet with the user found in the search text as the
|
| 67 |
// active category.
|
| 68 |
$facets[] = new author_facet($excluded_roles, $uid, $name);
|
| 69 |
|
| 70 |
}
|
| 71 |
}
|
| 72 |
// Remove the parsed token from the search text, based
|
| 73 |
// upon search_query_insert().
|
| 74 |
if (!is_null(search_query_extract($arg, 'author'))) {
|
| 75 |
$arg = trim(preg_replace('/(^| )author:[^ ]*/i', '', $arg));
|
| 76 |
}
|
| 77 |
}
|
| 78 |
return $arg;
|
| 79 |
|
| 80 |
case 'node':
|
| 81 |
// If the author facet is allowed.
|
| 82 |
if (is_numeric($arg->uid) && (!isset($selection) || isset($selection['author'][1]))) {
|
| 83 |
$excluded_roles = author_facet_excluded_roles($env);
|
| 84 |
if ($name = author_facet_get_user_name($env, $arg->uid, $excluded_roles)) {
|
| 85 |
// Create a facet with the node's author as the active category.
|
| 86 |
$facets[] = new author_facet($excluded_roles, $arg->uid, $name);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
break;
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Implementation of hook_faceted_search_init().
|
| 95 |
*/
|
| 96 |
function author_facet_faceted_search_init(&$env) {
|
| 97 |
$env->settings['author_facet_excluded_roles'] = array();
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Return the name of a user.
|
| 102 |
*
|
| 103 |
* @return The user's name, or FALSE if not found or if the user's role is not
|
| 104 |
* allowed in the facet.
|
| 105 |
*/
|
| 106 |
function author_facet_get_user_name($env, $uid, $excluded_roles = array()) {
|
| 107 |
if ($uid == 0) {
|
| 108 |
return t('Anonymous');
|
| 109 |
}
|
| 110 |
elseif (count($excluded_roles)) {
|
| 111 |
return db_result(db_query('SELECT u.name FROM {users} u WHERE u.uid = %d AND NOT EXISTS (SELECT r.rid FROM {users_roles} r WHERE u.uid = r.uid AND r.rid IN ('. implode(', ', $excluded_roles) .'))', $uid));
|
| 112 |
}
|
| 113 |
else {
|
| 114 |
return db_result(db_query('SELECT u.name FROM {users} u WHERE u.uid = %d', $uid));
|
| 115 |
}
|
| 116 |
}
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Returns an array with the roles that should not be used in faceted searches.
|
| 120 |
*/
|
| 121 |
function author_facet_excluded_roles($env) {
|
| 122 |
$roles = array_filter(user_roles(TRUE), '_author_facet_filter_role');
|
| 123 |
return array_intersect($env->settings['author_facet_excluded_roles'], array_keys($roles));
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Callback for array_filter to remove unwanted roles.
|
| 128 |
*/
|
| 129 |
function _author_facet_filter_role($role) {
|
| 130 |
return $role != 'authenticated user';
|
| 131 |
}
|
| 132 |
|
| 133 |
/**
|
| 134 |
* A facet for node authors.
|
| 135 |
*/
|
| 136 |
class author_facet extends faceted_search_facet {
|
| 137 |
|
| 138 |
var $_excluded_roles;
|
| 139 |
|
| 140 |
/**
|
| 141 |
* Constructor. Optionally assigns the active user of the facet.
|
| 142 |
*/
|
| 143 |
function author_facet($excluded_roles = array(), $uid = 0, $name = '') {
|
| 144 |
$active_path = array();
|
| 145 |
if (is_numeric($uid) && $name) {
|
| 146 |
$active_path[] = new author_facet_category($uid, $name);
|
| 147 |
}
|
| 148 |
parent::faceted_search_facet('author', $active_path);
|
| 149 |
$this->_excluded_roles = $excluded_roles;
|
| 150 |
}
|
| 151 |
|
| 152 |
function get_id() {
|
| 153 |
return 1; // This module provides only one facet.
|
| 154 |
}
|
| 155 |
|
| 156 |
function get_label() {
|
| 157 |
return t('Author');
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Returns the available sort options for this facet.
|
| 162 |
*/
|
| 163 |
function get_sort_options() {
|
| 164 |
$options = parent::get_sort_options();
|
| 165 |
$options['name'] = t('Name');
|
| 166 |
return $options;
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Handler for the 'count' sort criteria.
|
| 171 |
*/
|
| 172 |
function build_sort_query_count(&$query) {
|
| 173 |
$query->add_orderby('count', 'DESC');
|
| 174 |
$query->add_orderby('users_name', 'ASC');
|
| 175 |
}
|
| 176 |
|
| 177 |
/**
|
| 178 |
* Handler for the 'name' sort criteria.
|
| 179 |
*/
|
| 180 |
function build_sort_query_name(&$query) {
|
| 181 |
$query->add_orderby('users_name', 'ASC');
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Returns the search text for this facet, taking into account this facet's
|
| 186 |
* active path.
|
| 187 |
*/
|
| 188 |
function get_text() {
|
| 189 |
if ($category = $this->get_active_category()) {
|
| 190 |
return $category->_uid;
|
| 191 |
}
|
| 192 |
return '';
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Updates a query for retrieving the root categories of this facet and their
|
| 197 |
* associated nodes within the current search results.
|
| 198 |
*
|
| 199 |
* @param $query
|
| 200 |
* The query object to update.
|
| 201 |
*
|
| 202 |
* @return
|
| 203 |
* FALSE if this facet can't have root categories.
|
| 204 |
*/
|
| 205 |
function build_root_categories_query(&$query) {
|
| 206 |
$query->add_table('users', 'uid', 'n', 'uid');
|
| 207 |
$query->add_field('users', 'uid');
|
| 208 |
$query->add_field('users', 'name');
|
| 209 |
$query->add_groupby('users_uid');
|
| 210 |
if (count($this->_excluded_roles)) {
|
| 211 |
$query->add_subquery('NOT EXISTS (SELECT users_roles.rid FROM {users_roles} users_roles WHERE users.uid = users_roles.uid AND users_roles.rid IN ('. implode(', ', $this->_excluded_roles) .'))');
|
| 212 |
}
|
| 213 |
return TRUE;
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* This factory method creates categories given query results that include the
|
| 218 |
* fields selected in get_root_categories_query() or get_subcategories_query().
|
| 219 |
*
|
| 220 |
* @param $results
|
| 221 |
* $results A database query result resource.
|
| 222 |
*
|
| 223 |
* @return
|
| 224 |
* Array of categories.
|
| 225 |
*/
|
| 226 |
function build_categories($results) {
|
| 227 |
$categories = array();
|
| 228 |
while ($result = db_fetch_object($results)) {
|
| 229 |
if ($result->users_uid == 0) {
|
| 230 |
$result->users_name = t('Anonymous');
|
| 231 |
}
|
| 232 |
$categories[] = new author_facet_category($result->users_uid, $result->users_name, $result->count);
|
| 233 |
}
|
| 234 |
return $categories;
|
| 235 |
}
|
| 236 |
}
|
| 237 |
|
| 238 |
/**
|
| 239 |
* A node-type based facet category.
|
| 240 |
*/
|
| 241 |
class author_facet_category extends faceted_search_category {
|
| 242 |
var $_uid = 0;
|
| 243 |
var $_name = '';
|
| 244 |
|
| 245 |
function author_facet_category($uid, $name, $count = NULL) {
|
| 246 |
parent::faceted_search_category($count);
|
| 247 |
$this->_uid = $uid;
|
| 248 |
$this->_name = $name;
|
| 249 |
}
|
| 250 |
|
| 251 |
/**
|
| 252 |
* Return the label of this category.
|
| 253 |
*
|
| 254 |
* @param $html
|
| 255 |
* TRUE when HTML is allowed in the label, FALSE otherwise. Checking this
|
| 256 |
* flag allows implementors to provide a rich-text label if desired, and an
|
| 257 |
* alternate plain text version for cases where HTML cannot be used. The
|
| 258 |
* implementor is responsible to ensure adequate security filtering.
|
| 259 |
*/
|
| 260 |
function get_label($html = FALSE) {
|
| 261 |
return check_plain($this->_name);
|
| 262 |
}
|
| 263 |
|
| 264 |
/**
|
| 265 |
* Updates a query for selecting nodes matching this category.
|
| 266 |
*
|
| 267 |
* @param $query
|
| 268 |
* The query object to update.
|
| 269 |
*/
|
| 270 |
function build_results_query(&$query) {
|
| 271 |
$query->add_where('n.uid = %d', $this->_uid);
|
| 272 |
}
|
| 273 |
}
|