2007-11-13 12:28:36 +00:00
< ? php
// $Id$
/**
* @ file
* User page callbacks for the profile module .
*/
/**
* Menu callback ; display a list of user information .
*/
function profile_browse () {
// Ensure that the path is converted to 3 levels always.
list (, $name , $value ) = array_pad ( explode ( '/' , $_GET [ 'q' ], 3 ), 3 , '' );
2009-05-26 10:41:06 +00:00
$field = db_query ( " SELECT DISTINCT(fid), type, title, page, visibility FROM { profile_field} WHERE name = :name " , array ( ':name' => $name )) -> fetchObject ();
2007-11-13 12:28:36 +00:00
if ( $name && $field -> fid ) {
// Only allow browsing of fields that have a page title set.
if ( empty ( $field -> page )) {
drupal_not_found ();
return ;
}
// Do not allow browsing of private and hidden fields by non-admins.
if ( ! user_access ( 'administer users' ) && ( $field -> visibility == PROFILE_PRIVATE || $field -> visibility == PROFILE_HIDDEN )) {
2007-12-08 14:06:23 +00:00
drupal_access_denied ();
return ;
2007-11-13 12:28:36 +00:00
}
// Compile a list of fields to show.
2009-05-26 10:41:06 +00:00
$fields = db_query ( 'SELECT name, title, type, weight, page FROM {profile_field} WHERE fid <> :fid AND visibility = :visibility ORDER BY weight' , array (
':fid' => $field -> fid ,
':visibility' => PROFILE_PUBLIC_LISTINGS ,
)) -> fetchAll ();
$query = db_select ( 'users' ) -> extend ( 'PagerDefault' );
$query -> join ( 'profile_value' , 'v' , 'u.uid = v.uid' );
$query
-> fields ( 'u' , array ( 'uid' , 'access' ))
-> condition ( 'v.fid' , $field -> fid )
-> condition ( 'u.access' , 0 , '<>' )
-> condition ( 'u.status' , 0 , '<>' )
-> orderBy ( 'u.access' , 'DESC' );
2007-11-13 12:28:36 +00:00
// Determine what query to use:
$arguments = array ( $field -> fid );
switch ( $field -> type ) {
case 'checkbox' :
2009-05-26 10:41:06 +00:00
$query -> condition ( 'v.value' , 1 );
2007-11-13 12:28:36 +00:00
break ;
case 'textfield' :
case 'selection' :
2009-05-26 10:41:06 +00:00
$query -> condition ( 'v.value' , $value );
2007-11-13 12:28:36 +00:00
break ;
case 'list' :
2009-05-26 10:41:06 +00:00
$query -> condition ( 'v.value' , '%' . $value . '%' , 'LIKE' );
2007-11-13 12:28:36 +00:00
break ;
default :
drupal_not_found ();
return ;
}
2009-05-26 10:41:06 +00:00
$uids = $query
-> limit ( 20 )
-> execute ()
-> fetchCol ();
2009-03-14 23:01:38 +00:00
// Load the users.
2009-05-26 10:41:06 +00:00
$users = user_load_multiple ( $uids );
2007-11-13 12:28:36 +00:00
$content = '' ;
2009-03-14 23:01:38 +00:00
foreach ( $users as $account ) {
2007-11-13 12:28:36 +00:00
$profile = _profile_update_user_fields ( $fields , $account );
$content .= theme ( 'profile_listing' , $account , $profile );
}
$output = theme ( 'profile_wrapper' , $content );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-11-13 12:28:36 +00:00
if ( $field -> type == 'selection' || $field -> type == 'list' || $field -> type == 'textfield' ) {
$title = strtr ( check_plain ( $field -> page ), array ( '%value' => theme ( 'placeholder' , $value )));
}
else {
$title = check_plain ( $field -> page );
}
2008-10-13 00:33:05 +00:00
drupal_set_title ( $title , PASS_THROUGH );
2007-11-13 12:28:36 +00:00
return $output ;
}
2008-10-12 04:30:09 +00:00
elseif ( $name && ! $field -> fid ) {
2007-11-13 12:28:36 +00:00
drupal_not_found ();
}
else {
// Compile a list of fields to show.
2009-05-26 10:41:06 +00:00
$fields = db_query ( 'SELECT name, title, type, weight, page, visibility FROM {profile_field} WHERE visibility = :visibility ORDER BY category, weight' , array ( ':visibility' => PROFILE_PUBLIC_LISTINGS )) -> fetchAll ();
2007-11-13 12:28:36 +00:00
// Extract the affected users:
2009-05-26 10:41:06 +00:00
$query = db_select ( 'users' , 'u' ) -> extend ( 'PagerDefault' );
$uids = $query
-> fields ( 'u' , array ( 'uid' , 'access' ))
-> condition ( 'u.uid' , 0 , '>' )
-> condition ( 'u.status' , 0 , '>' )
-> condition ( 'u.access' , 0 , '>' )
-> orderBy ( 'u.access' , 'DESC' )
-> limit ( 20 )
-> execute ()
-> fetchCol ();
$users = user_load_multiple ( $uids );
2007-11-13 12:28:36 +00:00
$content = '' ;
2009-03-14 23:01:38 +00:00
foreach ( $users as $account ) {
2007-11-13 12:28:36 +00:00
$profile = _profile_update_user_fields ( $fields , $account );
$content .= theme ( 'profile_listing' , $account , $profile );
}
$output = theme ( 'profile_wrapper' , $content );
2009-04-26 19:44:40 +00:00
$output .= theme ( 'pager' , NULL );
2007-11-13 12:28:36 +00:00
2009-05-26 10:41:06 +00:00
drupal_set_title ( t ( 'User list' ));
2007-11-13 12:28:36 +00:00
return $output ;
}
}
/**
* Callback to allow autocomplete of profile text fields .
*/
function profile_autocomplete ( $field , $string ) {
$matches = array ();
2009-05-16 15:23:16 +00:00
$autocomplete_field = ( bool ) db_query_range ( " SELECT 1 FROM { profile_field} WHERE fid = :fid AND autocomplete = 1 " , array ( ':fid' => $field ), 0 , 1 ) -> fetchField ();
if ( $autocomplete_field ) {
2009-05-26 10:41:06 +00:00
$values = db_query_range ( " SELECT value FROM { profile_value} WHERE fid = :fid AND LOWER(value) LIKE LOWER(:value) GROUP BY value ORDER BY value ASC " , array (
2008-09-15 20:48:10 +00:00
':fid' => $field ,
2009-05-24 17:39:35 +00:00
':value' => $string . '%' ,
2009-05-26 10:41:06 +00:00
), 0 , 10 ) -> fetchCol ();
foreach ( $values as $value ) {
$matches [ $value ] = check_plain ( $value );
2007-11-13 12:28:36 +00:00
}
}
drupal_json ( $matches );
}