You are here

function log_name_autocomplete in Log entity 7

Page callback for the log name autocomplete callback.

Parameters

string $log_type: The log type to filter to. If this is set to 'all' then no filtering will be performed. Multiple log types can be specified as a single string, separated by plus characters (+). ie: "mytype1+mytype2"

string $string: The string to search for.

1 string reference to 'log_name_autocomplete'
log_menu in ./log.module
Implements hook_menu().

File

./log.module, line 927
Log - A general purpose record keeping system.

Code

function log_name_autocomplete($log_type, $string) {

  // Search the database for logs with matching names.
  $query = db_select('log', 'l');
  $query
    ->fields('l', array(
    'name',
  ));
  $query
    ->addExpression('count(name)', 'name_count');
  $query
    ->condition('name', '%' . db_like($string) . '%', 'LIKE');
  $query
    ->groupBy('name');
  $query
    ->orderBy('name_count', 'DESC');
  $query
    ->range(0, 10);

  // If the log type is not "all", filter by log type.
  if ($log_type != 'all') {
    $log_types = array();
    if (strpos($log_type, '+') !== FALSE) {
      $log_types = explode('+', $log_type);
    }
    else {
      $log_types[] = $log_type;
    }
    $query
      ->condition('type', $log_types, 'IN');
  }

  // Execute the query.
  $result = $query
    ->execute();

  // Save matches to an array.
  $matches = array();
  foreach ($result as $row) {
    $matches[] = t('@log_name', array(
      '@log_name' => $row->name,
    ));
  }

  // Map to an associative array.
  $matches = drupal_map_assoc($matches);

  // Return the matches as JSON.
  drupal_json_output($matches);
}