You are here

function privatemsg_realname_query_privatemsg_autocomplete_alter in Privatemsg 7.2

Same name and namespace in other branches
  1. 7 privatemsg_realname/privatemsg_realname.module \privatemsg_realname_query_privatemsg_autocomplete_alter()

Implements hook_query_privatemsg_autocomplete_alter().

File

privatemsg_realname/privatemsg_realname.module, line 46

Code

function privatemsg_realname_query_privatemsg_autocomplete_alter(SelectQueryInterface $query) {
  $search = $query
    ->getMetaData('arg_1');
  $names = $query
    ->getMetaData('arg_2');

  // LEFT JOIN realname table, some users might not have a realname stored in
  // there.
  $alias = $query
    ->leftJoin('realname', 'r', '%alias.uid = u.uid');

  // Either select users where the profile field name and value matches or the username.
  // This does replace the default where.
  $conditions =& $query
    ->conditions();
  foreach ($conditions as $key => $condition) {

    // Figure out which is the condition that checks the username.
    if (isset($condition['field']) && is_string($condition['field']) && $condition['field'] == 'u.name') {

      // Update existing condition.
      if (variable_get('privatemsg_realname_search_username', TRUE)) {
        $condition['field'] = db_or()
          ->condition('r.realname', $search . '%', 'LIKE')
          ->condition('u.name', $search . '%', 'LIKE');

        //$condition['operator'] = NULL;
        $condition['value'] = array();
      }
      else {
        $condition['field'] = 'r.realname';
      }
      $conditions[$key] = $condition;
    }
  }
  if (!empty($names)) {

    // Exclude already existing realnames, but explicitly allow NULL.
    // r.realname is left joined and can be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
    $query
      ->condition(db_or()
      ->condition($alias . '.realname', $names, 'NOT IN')
      ->isNull($alias . '.realname'));
  }
}