sbp_users.module in Search by Page 7
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
*/
/**
* Implements 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')
->fetchAll();
}
else {
$rids = array();
foreach ($rolelist as $key => $item) {
if ($item) {
$rids[] = $key;
}
}
if (!count($rids)) {
return array();
}
$query = db_select('users', 'u')
->fields('u', array(
'uid',
'language',
));
$query
->join('users_roles', 'ur', 'u.uid = ur.uid');
$query
->condition('u.status', 1)
->condition('ur.rid', $rids, 'IN');
$res = $query
->execute()
->fetchAll();
}
// 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();
foreach ($res as $item) {
$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;
}
/**
* Implements Search by Page hook_sbp_query_modify().
*
* Adds an access permission check to the search query.
*/
function sbp_users_sbp_query_modify($environment, $query) {
// User profile access is simple yes/no wholesale permission.
$ok = user_access('access user profiles');
$cond = db_and();
if ($ok) {
// If they can access users in general, join to user table to make
// sure user still exists and isn't blocked.
$query
->leftJoin('users', 'sbpu_u', 'sbpu_u.uid = sp.modid');
$cond
->condition('sbpu_u.status', 1);
}
else {
$cond
->where('0=1');
}
return $cond;
}
/**
* Implements 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, TRUE);
if (!$user->name) {
return NULL;
}
// Get basic user info
$ret = array(
'type' => t('User'),
'title' => check_plain($user->name),
'object' => $user,
);
// Get snippet from stored page data.
if ($keys) {
$content = search_by_page_stored_page_content('sbp_users', $id, $environment);
if ($content) {
$ret['snippet'] = search_by_page_excerpt($keys, $content);
}
}
return $ret;
}
/**
* Implements 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_update().
*
* Mark the user path as "time to reindex".
*/
function sbp_users_user_update(&$edit, $account, $category) {
search_by_page_force_reindex('sbp_users', $account->uid);
}
/**
* Implements hook_user_delete().
*
* Remove from index.
*/
function sbp_users_user_delete($account) {
search_by_page_force_remove('sbp_users', $account->uid);
}
Functions
Name | Description |
---|---|
sbp_users_sbp_details | Implements Search by Page hook_sbp_details(). |
sbp_users_sbp_paths | Implements Search by Page hook_sbp_paths(). |
sbp_users_sbp_query_modify | Implements Search by Page hook_sbp_query_modify(). |
sbp_users_sbp_settings | Implements Search by Page hook_sbp_settings(). |
sbp_users_user_delete | Implements hook_user_delete(). |
sbp_users_user_update | Implements hook_user_update(). |