--- profileplus.module.ori	Tue Apr 25 12:56:42 2006
+++ profileplus.module	Wed Mar 21 15:17:54 2007
@@ -16,6 +16,7 @@ function profileplus_help($section) {
    }
 }
 
+
 /**
  * Hide the default 'users' tab from the search page as it is useless
  */
@@ -28,6 +29,14 @@ function profileplus_menu() {
 }
 
 
+/**
+ * Implementation of hook_perm().
+ */
+function profileplus_perm() {
+  return array('search user profiles');
+}
+
+
 /*
  * Implementation of hook_search()
  */
@@ -40,42 +49,70 @@ function profile_search($op = 'search', 
     case 'search':
       if (user_access('access user profiles')) {
         $find = array();
-        // Replace wildcards with MySQL/PostgreSQL wildcards.
+        $keywords = explode(' ', check_plain(strip_tags($keys)));
+        $i = count($keywords);
         $keys = preg_replace('!\*+!', '%', $keys);
-        if (user_access('administer users')) {
-          // administrators don't have restrictions
-          $sql = "FROM {users} u INNER JOIN {profile_values} pv ON u.uid = pv.uid INNER JOIN {profile_fields} pf ON pv.fid = pf.fid WHERE LOWER(pv.value) LIKE LOWER('%%%s%%')";
-          $result = pager_query('SELECT DISTINCT u.* '.$sql, 15, 0, 'SELECT COUNT(DISTINCT u.uid) '.$sql, $keys);
-        }
-        else {
-          // non-administrators can only search public fields and active users
-          $sql = "FROM {users} u INNER JOIN {profile_values} pv ON u.uid = pv.uid INNER JOIN {profile_fields} pf ON pv.fid = pf.fid WHERE LOWER(pv.value) LIKE LOWER('%%%s%%') AND pf.visibility IN (%d, %d) AND u.status = 1";
-          $result = pager_query('SELECT DISTINCT u.* '.$sql, 15, 0, 'SELECT COUNT(DISTINCT u.uid) '.$sql, $keys, PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
-        }
 
-        while ($account = db_fetch_object($result)) {
-          $user = user_load(array('uid'=>$account->uid));
-          $profile_fields = profile_view_profile($user);
-
-          $entry = array();
-          foreach($profile_fields as $category => $fields) {
-            foreach($fields as $field) {
-              $entry[] = $field['value'];
+        if ($i > 0){
+          $users = array();
+          while ($i > 0) {
+            $j = $i - 1;
+            if (user_access('administer users')) {
+            // administrators don't have restrictions
+              $sql = "FROM {users} u INNER JOIN {profile_values} pv ON u.uid = pv.uid INNER JOIN {profile_fields} pf ON pv.fid = pf.fid WHERE pv.value LIKE '%%%s%%' OR u.name LIKE '%%%s%%'";
+              $result = db_query('SELECT DISTINCT u.* '.$sql, $keywords[$j], $keywords[$j]);
+            }
+            elseif (user_access('search user profiles')) {
+            // non-administrators can only search public fields and active users
+              $sql = "FROM {users} u INNER JOIN {profile_values} pv ON u.uid = pv.uid INNER JOIN {profile_fields} pf ON pv.fid = pf.fid WHERE pv.value LIKE '%%%s%%' OR u.name LIKE '%%%s%%' AND pf.visibility IN (%d, %d) AND u.status = 1";
+              $result = db_query('SELECT DISTINCT u.* '.$sql, $keywords[$j], $keywords[$j], PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
+            }
+
+            while ($auser = db_fetch_object($result)) {
+              if (in_array($auser->uid,$users) && !in_array($auser->uid,$match)){
+                $match[] = $auser->uid;
+              }
+              else {
+                $users[] = $auser->uid;
+              }
             }
+            $i = $i - 1;
           }
 
-          $view = implode(' | ', $entry);
-          $find[] = array('title' => $account->name, 'link' => url("user/$account->uid/view"), 'snippet' => search_excerpt($keys, $view));
-        }
+          if ($match) {
+            $items = $match;
+          }
+          else {
+            $items = $users;
+          }
 
-        return $find;
-      }
+          // reload all the retrieved uids through a pager_query to get paged results
+          $finalsql = pager_query('SELECT * FROM {users} WHERE uid IN ('.implode(', ', $items).')');
+            while ($pageditems = db_fetch_object($finalsql)) {
+              $user = user_load(array('uid'=>$pageditems->uid));
+              $profile_fields = profile_view_profile($user);
+
+              $entry = array();
+              foreach($profile_fields as $category => $fields) {
+                foreach($fields as $field) {
+                  $entry[] = $field['value'];
+                }
+                $view = implode(' | ', $entry);
+                $find[] = array('title' => $user->name, 'link' => url("user/$user->uid/view"), 'snippet' => search_excerpt($keys, $view));
+              }
+            }
+
+          return $find;
+        }
+      } // end search case
   }
 }
 
+/**
+ * Implementation of hook_install().
+ */
 function profileplus_install() {
   // new module weights in core: put profileplus after user.module
   $ret[] = db_query("UPDATE {system} SET weight = 2 WHERE name = 'profileplus'");
   return $ret;
 }
-?>
\ No newline at end of file
