sbp_users.module in Search by Page 6
Same filename and directory in other branches
Module file for Search by Page Users, a sub-module for Search by Page.
Allows you to add user profiles, by role, to Search by Page.
File
sbp_users.moduleView source
<?php
/**
* @file
* Module file for Search by Page Users, a sub-module for Search by Page.
*
* Allows you to add user profiles, by role, to Search by Page.
* @ingroup search_by_page
*/
/**
* Implementation of Search by Page hook_sbp_paths().
*
* Returns a list of all the user pages that should be indexed.
*/
function sbp_users_sbp_paths($environment) {
// What user roles do they want to index?
$rolelist = search_by_page_setting_get('sbp_users_roles_indexed', $environment, array());
if (!is_array($rolelist) || !count($rolelist)) {
return array();
}
// This variable comes from a checkbox array form element. So it
// gives us an array like '3' => '3', '4' => 0
// meaning role ID 3 should be indexed, but not 4,
// so pick out the ones to actually index
// Special case: if they chose Authenticated User, they want all users,
// but only non-blocked and skip user 0 (non-logged-in user placeholder).
$doall = $rolelist[DRUPAL_AUTHENTICATED_RID];
if ($doall) {
$res = db_query('SELECT u.uid, u.language FROM {users} u WHERE u.status=1 AND u.uid > 0');
}
else {
$args = array();
foreach ($rolelist as $key => $item) {
if ($item) {
$args[] = $key;
}
}
if (!count($args)) {
return array();
}
$res = db_query('SELECT u.uid, u.language FROM {users} u JOIN {users_roles} ur ON u.uid = ur.uid WHERE u.status=1 AND ur.rid IN (' . db_placeholders($args, 'int') . ')', $args);
}
// Build an array of paths
$min_time = search_by_page_setting_get('sbp_users_min_time', $environment, 1);
$max_time = search_by_page_setting_get('sbp_users_max_time', $environment, 0);
$langs = language_list();
$langs = array_keys($langs);
$lang_opt = 'all';
if (count($langs) > 1) {
$lang_opt = search_by_page_setting_get('sbp_users_language', $environment, 'all');
}
$role = search_by_page_setting_get('sbp_users_role', $environment, DRUPAL_ANONYMOUS_RID);
$ret = array();
while ($item = db_fetch_object($res)) {
$stuff = array(
'id' => $item->uid,
'role' => $role,
'min_time' => $min_time,
'max_time' => $max_time,
);
if ($lang_opt == 'user') {
$stuff['languages'] = array(
$item->language,
);
}
else {
$stuff['languages'] = $langs;
}
$ret['user/' . $item->uid] = $stuff;
}
return $ret;
}
/**
* Implementation of Search by Page hook_sbp_query_modify().
*
* Adds an access permission check to the search query.
*/
function sbp_users_sbp_query_modify($environment) {
// User profile access is simple yes/no wholesale permission.
$ok = user_access('access user profiles');
$where = 0;
$join = '';
if ($ok) {
// If they can access users in general, join to user table to make
// sure user still exists and isn't blocked.
$join = 'LEFT JOIN {users} sbpu_u ON sbpu_u.uid = sp.modid';
$where = 'sbpu_u.status = 1';
}
return array(
'where' => $where,
'join' => $join,
'arguments' => array(),
);
}
/**
* Implementation of Search by Page hook_sbp_details().
*
* Returns details for a particular user ID, for particular search keys.
*/
function sbp_users_sbp_details($id, $environment, $keys = NULL) {
$id = intval($id);
if (!$id) {
return NULL;
}
$user = user_load($id);
if (!$user->name) {
return NULL;
}
// Get basic user info
$ret = array(
'type' => t('User'),
'title' => check_plain($user->name),
);
// Render this user, to get snippet
if ($keys) {
$content = menu_execute_active_handler('user/' . $id);
if (!is_int($content)) {
$ret['snippet'] = search_by_page_excerpt($keys, search_by_page_strip_tags($content, $environment));
}
}
return $ret;
}
/**
* Implementation of Search by Page hook_sbp_settings().
*
* Adds a user role selection form to the Search by Page settings page.
*/
function sbp_users_sbp_settings($environment) {
$form = array();
$form['sbp_users'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#weight' => -20,
'#title' => t('Users'),
);
$form['sbp_users']['sbp_users_roles_indexed'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles to index'),
'#default_value' => search_by_page_setting_get('sbp_users_roles_indexed', $environment, array()),
'#options' => user_roles(TRUE),
'#weight' => 0,
'#description' => t('Choose which user roles you would like to have indexed by Search by Page. If you select "authenticated user", you will get all users registered on your site.'),
);
// Give language options if there is more than one language on the site.
$langs = language_list();
if (count($langs) > 1) {
$form['sbp_users']['sbp_users_language'] = array(
'#type' => 'radios',
'#title' => t('Language(s) to use when indexing user pages'),
'#default_value' => search_by_page_setting_get('sbp_users_language', $environment, 'all'),
'#options' => array(
'all' => t('All available languages'),
'user' => t("Each profile in that user's preferred language only"),
),
'#weight' => 2,
'#description' => t("If you choose <em>All available languages</em>, each profile will be indexed several times, with the viewing language set to each available language in turn, which may result in either content, field labels, or both changing to the viewing language. If you choose to index the profile in the user's preferred language only, each page will be indexed only in one language. In either case, when a search is done, only content in the current language will be selected."),
);
}
$form['sbp_users']['sbp_users_role'] = array(
'#type' => 'radios',
'#title' => t('Role for indexing'),
'#options' => user_roles(),
'#default_value' => search_by_page_setting_get('sbp_users_role', $environment, DRUPAL_ANONYMOUS_RID),
'#weight' => 4,
'#description' => t("When Search by Page indexes pages for searching, the pages will be viewed from the perspective and permissions of a user with this role."),
);
$times = array(
'1' => t('1 second'),
'3600' => t('1 hour'),
'86400' => t('1 day'),
'604800' => t('1 week'),
'31449600' => t('1 year'),
'0' => t('Never'),
);
$form['sbp_users']['sbp_users_min_time'] = array(
'#type' => 'select',
'#title' => t('Minimum reindexing time'),
'#options' => $times,
'#default_value' => search_by_page_setting_get('sbp_users_min_time', $environment, 1),
'#weight' => 5,
'#description' => t("After indexing any new and updated user pages, Search by Page also cycles through previously-indexed user pages, in case the rendered view of the user page has changed (even though the user account information has not been edited, other information displayed on the page may have changed, such as points, ratings, a list of content the user has created, a Twitter feed, or a Content Profile). On some sites, you may want to limit the amount of reindexing, by setting a minimum time -- user pages will not be reindexed until this time has passed, unless the user account information has been updated."),
);
$form['sbp_users']['sbp_users_max_time'] = array(
'#type' => 'select',
'#title' => t('Maximum reindexing time'),
'#options' => $times,
'#default_value' => search_by_page_setting_get('sbp_users_max_time', $environment, 0),
'#weight' => 6,
'#description' => t("Conversely to the minimum reindexing time (see above), Search by Page can be set to prioritize reindexing each user page (by marking it as needing immediate reindexing) after this amount of time has passed. This has higher priority than the cycle-through reindexing of the setting above.") . ' ' . t('But be careful with this setting! If you set it too small, it can interfere with new content being indexed, because the reindexed content will have equal priority to content that has never been indexed. So make sure your settings allow for enough time for new content to be indexed before forcing reindexing.'),
);
return $form;
}
/**
* Implements hook_user().
*
* If the user account/profile is being updated, mark the user path as
* "time to reindex". If it's deleted, remove from index.
*/
function sbp_users_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'update':
search_by_page_force_reindex('sbp_users', $account->uid);
break;
case 'delete':
search_by_page_force_remove('sbp_users', $account->uid);
break;
}
}
Functions
Name | Description |
---|---|
sbp_users_sbp_details | Implementation of Search by Page hook_sbp_details(). |
sbp_users_sbp_paths | Implementation of Search by Page hook_sbp_paths(). |
sbp_users_sbp_query_modify | Implementation of Search by Page hook_sbp_query_modify(). |
sbp_users_sbp_settings | Implementation of Search by Page hook_sbp_settings(). |
sbp_users_user | Implements hook_user(). |