You are here

function advuser_build_filter_query in Advanced User 7.3

Same name and namespace in other branches
  1. 5.2 advuser_filters.inc \advuser_build_filter_query()
  2. 6.3 advuser.module \advuser_build_filter_query()
  3. 6.2 advuser_filters.inc \advuser_build_filter_query()

Build query for advuser administration filters based on session.

Parameters

$reset Resets the static cached array.:

Return value

array containing 'join' clauses, 'where' clauses and replacement 'args' for the variable sections of the clauses.

5 calls to advuser_build_filter_query()
advuser_admin_account in forms/advuser_admin_account.inc
Provide the list of filtered users.
advuser_build_query in ./advuser.module
Get the sql query string.
advuser_multiple_delete_confirm in forms/advuser_multiple_delete_confirm.inc
Implement the menu callback function for admin/people/advuser/confirm/delete.
advuser_multiple_email_confirm in forms/advuser_multiple_email_confirm.inc
The menu callback function for admin/people/advuser/confirm/email
advuser_multiple_role_edit in ./advuser.module
Callback function for admin mass adding/deleting a user role.

File

./advuser.module, line 544
Advanced user module allows you to select users based on an advanced set of filtering and apply actions to block, unblock, delete or email the selected users.

Code

function advuser_build_filter_query($reset = FALSE) {
  $advuser =& $_SESSION['advuser'];
  static $filter_array = array();
  if (empty($filter_array) || $reset) {
    $filters =& $advuser['filters'];
    $advuser_filters = advuser_filters();

    // Build query
    $where = $args = $join = array();
    foreach ($filters as $filter) {
      list($key, $op, $qop, $value) = array_values($filter);

      // This checks to see if this permission filter is an enabled permission
      // for the authenticated role.  If so, then all users would be listed, and
      // we can skip adding it to the filter query.
      switch ($key) {
        case 'permission':
          $account = new stdClass();
          $account->uid = 'advuser_filter';
          $account->roles = array(
            DRUPAL_AUTHENTICATED_RID => 1,
          );
          if (user_access($value, $account)) {
            continue;
          }
          break;
        case 'created':
        case 'last_access':

          // Convert the string date to a unix time value.
          $value = strtotime($value);
          break;
        case 'user_roles':

          // Allow for filtering for users where no role is set.
          if (ord($value) === 0) {
            $qop = 'IS NULL';
            $value = NULL;
          }
          break;
        default:
          if (isset($advuser_filters[$key]['join'])) {
            $join[] = $advuser_filters[$key]['join'];
          }
      }

      // Provide for '%%%s%%', '%%%s' and '%s%%' as needed.
      $arg_prefix = $arg_suffix = NULL;
      switch ($qop) {
        case 'NOT LIKE':
        case 'LIKE':
          $arg_prefix = $arg_suffix = '%';
          break;
        case 'BEGINS WITH':
          $qop = 'LIKE';
          $arg_suffix = '%';
          break;
        case 'ENDS WITH':
          $qop = 'LIKE';
          $arg_prefix = '%';
          break;
      }

      // Provide operator symbol to SQL translation.
      switch ($qop) {
        case '!=':
        case 'NOT LIKE':
          $in = 'NOT IN';
          $eq = '!=';
          $andor = 'AND';
          break;
        default:
          $in = 'IN';
          $eq = '=';
          $andor = 'OR';
      }
      $pkey = ":{$key}";
      $pkey_replace = "%{$key}_pkey";
      $ph[$pkey] = isset($ph[$pkey]) ? $ph[$pkey] + 1 : 0;
      $placeholder = "{$pkey}_{$ph[$pkey]}";

      // Build the 'where' clauses.
      $_where = $op . ' ' . str_ireplace("%op", $qop, $advuser_filters[$key]['where']);
      $_where = str_ireplace("%eq", $eq, $_where);
      $_where = str_ireplace("%andor", $andor, $_where);
      $_where = str_ireplace("%in", $in, $_where);
      $_where = str_ireplace($pkey_replace, $placeholder, $_where);
      if ($qop == 'IS NULL') {
        $_where = str_ireplace($placeholder, '', $_where);
      }
      if (isset($advuser_filters[$key]['join'])) {
        $j = $advuser_filters[$key]['join'];
        if (isset($j[2]) && strpos($j[2], '%alias.') !== FALSE) {
          $_where = str_ireplace('%alias.', '%alias_' . (count($join) - 1) . '.', $_where);
        }
      }
      $where[] = $_where;

      // Build the argument values.
      if ($qop != 'IS NULL') {
        $args[$placeholder] = $arg_prefix . $value . $arg_suffix;
      }
    }
    $where = count($where) ? '(' . implode(' ', $where) . ')' : '';
    $filter_array = array(
      'where' => $where,
      'join' => $join,
      'args' => $args,
    );
  }
  return $filter_array;
}