| 1 |
<?php
|
| 2 |
// $Id: profilesearch.module,v 1.1 2008/02/23 10:43:42 aalamaki Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* ProfileSearch lets users search the user profiles.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_menu().
|
| 11 |
*/
|
| 12 |
function profilesearch_menu($may_cache) {
|
| 13 |
|
| 14 |
if ($may_cache) {
|
| 15 |
$items[] = array(
|
| 16 |
'title' => t('Profile search results'),
|
| 17 |
'path' => 'profilesearch',
|
| 18 |
'callback' => 'profilesearch_dosearch',
|
| 19 |
'access' => user_access('access user profiles'),
|
| 20 |
'type' => MENU_CALLBACK,
|
| 21 |
);
|
| 22 |
}
|
| 23 |
|
| 24 |
return $items;
|
| 25 |
}
|
| 26 |
|
| 27 |
/**
|
| 28 |
* Implementation of hook_block().
|
| 29 |
*/
|
| 30 |
function profilesearch_block($op = 'list', $delta = 0, $edit = array()) {
|
| 31 |
if ($op == 'list') {
|
| 32 |
$blocks = array();
|
| 33 |
$blocks['search'] = array('info' => t('Profile search block'));
|
| 34 |
return $blocks;
|
| 35 |
}
|
| 36 |
elseif ($op == 'view' && $delta = 'search') {
|
| 37 |
return array(
|
| 38 |
'title' => t('Search users'),
|
| 39 |
'content' => drupal_get_form('profilesearch_search_form'),
|
| 40 |
);
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
function profilesearch_search_form() {
|
| 45 |
$form['search'] = array(
|
| 46 |
'#type' => 'textfield',
|
| 47 |
'#size' => 20,
|
| 48 |
'#title' => 'Search',
|
| 49 |
);
|
| 50 |
$form['submit'] = array(
|
| 51 |
'#type' => 'submit',
|
| 52 |
'#value' => 'Search',
|
| 53 |
);
|
| 54 |
|
| 55 |
return $form;
|
| 56 |
}
|
| 57 |
|
| 58 |
function profilesearch_search_form_submit($form_id, $form_values) {
|
| 59 |
|
| 60 |
$searchstring = str_replace(" ", "+", $form_values['search']);
|
| 61 |
|
| 62 |
return 'profilesearch/'. $searchstring;
|
| 63 |
}
|
| 64 |
|
| 65 |
function profilesearch_dosearch($searchstring) {
|
| 66 |
|
| 67 |
$result = db_query("SELECT DISTINCT u.*, pf.title AS title, pv.value AS value
|
| 68 |
FROM {profile_values} pv
|
| 69 |
LEFT JOIN {users} u ON pv.uid = u.uid
|
| 70 |
LEFT JOIN {profile_fields} pf ON pf.fid=pv.fid
|
| 71 |
WHERE pv.value LIKE '%s'
|
| 72 |
OR u.name LIKE '%s'
|
| 73 |
OR u.mail LIKE '%s'
|
| 74 |
ORDER BY u.name", array('%'. $searchstring .'%', '%'. $searchstring .'%', '%'. $searchstring .'%'));
|
| 75 |
|
| 76 |
$records = array();
|
| 77 |
while ($record = db_fetch_object($result)) {
|
| 78 |
if (empty($records[$record->uid])) {
|
| 79 |
$records[$record->uid] = $record;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
return theme('profilesearch_results', $records);
|
| 84 |
}
|
| 85 |
|
| 86 |
function theme_profilesearch_results($records) {
|
| 87 |
|
| 88 |
$header = array(t('User'), t('E-mail'), t('Field'), t('Value'));
|
| 89 |
|
| 90 |
foreach ($records as $user) {
|
| 91 |
$row = array();
|
| 92 |
|
| 93 |
$row[] = theme('username', $user);
|
| 94 |
$row[] = $user->mail;
|
| 95 |
// We could be matching on the username or email address, in
|
| 96 |
// which case skip the field.
|
| 97 |
if (empty($user->value)) {
|
| 98 |
$row[] = t('n/a');
|
| 99 |
$row[] = t('n/a');
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
$row[] = $user->title;
|
| 103 |
$row[] = $user->value;
|
| 104 |
}
|
| 105 |
|
| 106 |
$rows[] = $row;
|
| 107 |
}
|
| 108 |
|
| 109 |
return theme('table', $header, $rows);
|
| 110 |
}
|