You are here

advuser.module in Advanced User 7.4

File

advuser.module
View source
<?php

/**
 * @file
 * advuser.module
 */

/**
 * Returns a list of filter form elements for the user entity.
 */
function advuser_filters() {
  $filters =& drupal_static(__FUNCTION__);
  if (empty($filters)) {
    $filters = array();

    // Get property and field info for user entity.
    $user_entity_info = entity_get_property_info('user');

    // Set the entity properties to expose.
    $user_properties = array_intersect(array_keys($user_entity_info['properties']), array(
      'uid',
      'name',
      'mail',
      'last_access',
      'last_login',
      'created',
    ));

    // Set property filters.
    foreach ($user_properties as $key) {
      $filters['propertyCondition'][$key] =& $user_entity_info['properties'][$key];
      $filters['propertyCondition'][$key] = array_merge($filters['propertyCondition'][$key], array(
        '#title' => $filters['propertyCondition'][$key]['label'],
      ));
    }

    // Set field filters.
    $form = array(
      '#parents' => array(
        'advuser',
      ),
    );
    $form_state = array();
    $filters['fieldCondition'] =& $user_entity_info['bundles']['user']['properties'];
    foreach ($filters['fieldCondition'] as $key => &$meta) {
      $field = field_info_field($key);
      $instance = field_info_instance('user', $key, 'user');
      $meta = field_default_form('user', NULL, $field, $instance, LANGUAGE_NONE, array(), $form, $form_state);
      if (!empty($meta[$key][$meta[$key]['#language']][0]['value'])) {
        $meta = $meta[$key][$meta[$key]['#language']][0]['value'];
      }
      else {
        $meta = $meta[$key][$meta[$key]['#language']];
      }
      $filters['fieldCondition'][$key] = $meta;
    }

    // Do some clean up on form elements.
    foreach ($filters as &$group) {
      foreach ($group as $key => &$meta) {
        foreach ($meta as $property => &$value) {
          if (!in_array($property, array(
            '#type',
            '#title',
            '#options',
            '#multiple',
            '#maxlength',
            '#weight',
            '#columns',
          ))) {
            unset($group[$key][$property]);
          }
        }
        $meta['#required'] = FALSE;
        $meta['#title'] = drupal_strtolower($meta['#title']);
        if (empty($meta['#type'])) {
          $meta['#type'] = 'textfield';
        }
        if ($meta['#type'] == 'textarea') {
          $meta['#type'] = 'textfield';
        }
        if (!empty($meta['#options']['_none'])) {
          $label = $meta['#options']['_none'];
          unset($meta['#options']['_none']);
          array_unshift($meta['#options'], $label);
        }
      }
    }
  }
  return $filters;
}

/**
 * TODO.
 */
function advuser_filter_set($filter_key, $part, $value, $json = FALSE) {
  if (!empty($filter_key)) {
    if (empty($_SESSION['advuser_overview_filter'][$filter_key])) {
      $_SESSION['advuser_overview_filter'][$filter_key] = array(
        'field' => NULL,
        'operator' => NULL,
        'value' => NULL,
      );
    }
    if (empty($part) && empty($value)) {
      unset($_SESSION['advuser_overview_filter'][$filter_key]);
    }
    else {
      $_SESSION['advuser_overview_filter'][$filter_key][$part] = $value;
    }
  }
  if ($json !== FALSE) {
    advuser_filter_get_all(TRUE);
  }
}

/**
 * TODO.
 */
function advuser_filter_get($filter_key, $part, $value = NULL) {
  if (empty($_SESSION['advuser_overview_filter'][$filter_key][$part])) {
    advuser_filter_set($filter_key, $part, $value);
  }
  return $_SESSION['advuser_overview_filter'][$filter_key][$part];
}

/**
 * TODO.
 */
function advuser_filter_get_all($json = FALSE) {
  $output = $_SESSION['advuser_overview_filter'];
  if ($json !== FALSE) {
    drupal_json_output($output);
  }
  else {
    return $output;
  }
}

/**
 * TODO.
 */
function advuser_filter_reset() {
  $_SESSION['advuser_overview_filter'] = array();
  $_SESSION['advuser_overview_filter_count'] = 0;
}

/**
 * Implements hook_menu().
 */
function advuser_menu() {
  $items = array();
  $items['admin/people/advuser/session'] = array(
    'title' => 'advuser session getter',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer users',
    ),
    'page callback' => 'advuser_filter_get_all',
    'page arguments' => array(
      TRUE,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/people/advuser/%/%/%/%'] = array(
    'title' => 'advuser session setter',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer users',
    ),
    'page callback' => 'advuser_filter_set',
    'page arguments' => array(
      3,
      4,
      5,
      6,
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function advuser_form_user_filter_form_alter(&$form, &$form_state) {
  $form['#submit'][] = 'advuser_form_user_filter_form_submit';
  $form['filters']['advuser_add'] = array(
    '#type' => 'submit',
    '#value' => t('Add Filter'),
    '#executes_submit_callback' => TRUE,
    '#ajax' => array(
      'callback' => 'advuser_form_user_filter_form_add',
      'wrapper' => 'advuser',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#attributes' => array(
      'class' => array(
        'advuser-add',
      ),
    ),
  );
  $form['filters']['advuser_reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#executes_submit_callback' => TRUE,
    '#ajax' => array(
      'callback' => 'advuser_form_user_filter_form_reset',
      'wrapper' => 'advuser',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['filters']['advuser'] = advuser_form_user_filter_form_add($form, $form_state, FALSE);
}

/**
 * Custom submit handler for user_filter_form.
 */
function advuser_form_user_filter_form_submit(&$form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  if ($form_state['values']['op'] == t('Reset')) {
    return advuser_form_user_filter_form_reset($form, $form_state);
  }
  if (!empty($form_state['values']['advuser'])) {
    foreach ($form_state['values']['advuser'] as $filter_key => &$meta) {
      if (!empty($meta['field'])) {
        if ($meta['operator'] == '') {
          $meta['operator'] = '=';
        }
        if (empty($meta['value'])) {
          $meta['value'] = NULL;
        }
        advuser_filter_set($filter_key, 'field', $meta['field']);
        advuser_filter_set($filter_key, 'operator', $meta['operator']);
        advuser_filter_set($filter_key, 'value', $meta['value']);
      }
      else {
        advuser_filter_set($filter_key, NULL, NULL);
      }
    }
  }
}

/**
 * AJAX callback for "Add Filter".
 */
function advuser_form_user_filter_form_add(&$form, &$form_state, $increment = TRUE) {

  // Get user entity filters.
  $filters = advuser_filters();

  // Set filter operators.
  $options_operator = array(
    '',
    '=',
    '<>',
    '>',
    '>=',
    '<',
    '<=',
    'STARTS_WITH',
    'CONTAINS',
  );
  $options_operator = drupal_map_assoc($options_operator);
  $default_operator = array_keys(array_slice($options_operator, 1, 1, TRUE));
  $default_operator = $default_operator[0];

  // Set filter fields.
  $options_field = array();
  foreach ($filters as &$group) {
    foreach ($group as $key => &$meta) {
      $options_field[$key] = $meta['#title'];
    }
  }
  $default_field = array_keys(array_slice($options_field, 0, 1, TRUE));
  $default_field = $default_field[0];
  $form['filters']['advuser'] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#attributes' => array(
      'id' => 'advuser',
      'class' => array(
        'filters',
      ),
    ),
  );
  $count = (!empty($_SESSION['advuser_overview_filter_count']) ? $_SESSION['advuser_overview_filter_count'] : 0) + ($increment ? 1 : 0);
  $_SESSION['advuser_overview_filter_count'] = $count;
  for ($i = 0; $i < $count; $i++) {
    $filter_key = 'filter_' . $i;

    // Get session filters.
    $field = advuser_filter_get($filter_key, 'field', $default_field);
    $operator = advuser_filter_get($filter_key, 'operator', $default_operator);
    $value = advuser_filter_get($filter_key, 'value');
    $form['filters']['advuser'][$filter_key] = array(
      '#type' => 'container',
      '#attributes' => array(
        'id' => $filter_key,
        'class' => array(
          'container-inline',
          'filter',
        ),
      ),
    );
    $form['filters']['advuser'][$filter_key]['field'] = array(
      '#type' => 'select',
      '#title' => t('Field'),
      '#title_display' => 'invisible',
      '#options' => $options_field,
      '#default_value' => $field,
      '#weight' => 0,
      '#attributes' => array(
        'data' => 'field',
        'style' => 'width: 127px;',
      ),
    );
    $form['filters']['advuser'][$filter_key]['operator'] = array(
      '#type' => 'select',
      '#options' => $options_operator,
      '#default_value' => $operator,
      '#weight' => 1,
      '#attributes' => array(
        'data' => 'operator',
        'style' => 'width: 80px;',
      ),
    );
    foreach ($filters as &$group) {
      foreach ($group as $key => &$meta) {
        if ($key == $field) {
          $meta = array_merge($meta, array(
            '#default_value' => $value,
            '#title_display' => 'invisible',
            '#weight' => 2,
            '#attributes' => array(
              'data' => 'value',
              'style' => 'width: ' . ($meta['#type'] == 'select' ? '102' : '96') . 'px;',
            ),
          ));
          $form['filters']['advuser'][$filter_key]['value'] = $meta;
        }
      }
    }
    $form['filters']['advuser'][$filter_key]['field']['#ajax']['callback'] = $form['filters']['advuser'][$filter_key]['operator']['#ajax']['callback'] = $form['filters']['advuser'][$filter_key]['value']['#ajax']['callback'] = 'advuser_form_user_filter_form_submit';
    $form['filters']['advuser'][$filter_key]['field']['#attached']['js'][] = $form['filters']['advuser'][$filter_key]['operator']['#attached']['js'][] = $form['filters']['advuser'][$filter_key]['value']['#attached']['js'][] = drupal_get_path('module', 'advuser') . '/advuser.js';
    $form['filters']['advuser'][$filter_key]['field']['#attributes']['class'][] = $form['filters']['advuser'][$filter_key]['operator']['#attributes']['class'][] = $form['filters']['advuser'][$filter_key]['value']['#attributes']['class'][] = 'advuser-filter';
  }
  return $form['filters']['advuser'];
}

/**
 * AJAX callback for "Clear Filters".
 */
function advuser_form_user_filter_form_reset(&$form, &$form_state) {
  advuser_filter_reset();
  return advuser_form_user_filter_form_add($form, $form_state, FALSE);
}

/**
 * Implements hook_query_TAG_alter().
 */
function advuser_query_advuser_FILTER_QUERY_alter(QueryAlterableInterface $query) {
  $session = advuser_filter_get_all();
  if (!empty($session)) {
    $filters = advuser_filters();
    $subquery = new EntityFieldQuery();
    $subquery
      ->entityCondition('entity_type', 'user');
    foreach ($filters as $group_key => &$group) {
      foreach ($group as $key => &$meta) {
        foreach ($session as $filter_key => &$filter) {
          $field = $filter['field'];
          $operator = $filter['operator'];
          $value = $filter['value'];
          if ($key == $field && !empty($value)) {
            if (!empty($meta['#columns'])) {
              $subquery
                ->{$group_key}($field, $meta['#columns'][0], $value, $operator);
            }
            else {
              $subquery
                ->{$group_key}($field, $value, $operator);
            }
          }
        }
      }
    }
    $users = array(
      0,
    );
    $results = $subquery
      ->execute();
    if (!empty($results)) {
      $users = array_keys($results['user']);
    }
    $query
      ->condition('u.uid', $users, 'IN');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function advuser_form_user_admin_account_alter(&$form, &$form_state) {
}

Functions

Namesort descending Description
advuser_filters Returns a list of filter form elements for the user entity.
advuser_filter_get TODO.
advuser_filter_get_all TODO.
advuser_filter_reset TODO.
advuser_filter_set TODO.
advuser_form_user_admin_account_alter Implements hook_form_FORM_ID_alter().
advuser_form_user_filter_form_add AJAX callback for "Add Filter".
advuser_form_user_filter_form_alter Implements hook_form_FORM_ID_alter().
advuser_form_user_filter_form_reset AJAX callback for "Clear Filters".
advuser_form_user_filter_form_submit Custom submit handler for user_filter_form.
advuser_menu Implements hook_menu().
advuser_query_advuser_FILTER_QUERY_alter Implements hook_query_TAG_alter().