You are here

function acquia_purge_autocomplete in Acquia Purge 7

Menu callback to fulfill autocompletes on manual purging forms.

See also

_acquia_purge_manualpurge_base()

1 string reference to 'acquia_purge_autocomplete'
acquia_purge_menu in ./acquia_purge.module
Implements hook_menu().

File

./acquia_purge.admin.inc, line 13
Admin page callbacks and theme functions for the Acquia Purge module.

Code

function acquia_purge_autocomplete($string) {
  $query_limit = 10;
  $matches = array();

  // Overwrite $string and work around Drupal buggingly ignoring slashes.
  $string = implode('/', array_slice(explode('/', $_GET['q']), 1));

  // Scan for node/ lookups as they're quite common.
  if (drupal_substr($string, 0, 1) == 'n') {
    $query = db_select('node', 'n')
      ->fields('n', array(
      'nid',
    ))
      ->range(0, $query_limit);

    // Lookup node IDs if the user entered node/, else limit on 15.
    if (drupal_strlen($string) > 5) {
      $string = (int) str_replace('node/', '', $string);
      $query
        ->condition('n.nid', '%' . db_like($string) . '%', 'LIKE');
    }
    $results = $query
      ->execute();
    foreach ($results as $nid) {
      $path = 'node/' . $nid->nid;
      $matches[] = $path;
    }
  }

  // Scan for URL aliases when the path module is enabled.
  if (module_exists('path')) {
    $query = db_select('url_alias', 'u')
      ->fields('u', array(
      'alias',
    ))
      ->condition('alias', db_like($string) . '%', 'LIKE')
      ->range(0, $query_limit)
      ->execute();
    foreach ($query as $alias) {
      $matches[] = $alias->alias;
    }
  }

  // Scan for menu_router paths, as long as they don't contain % characters.
  if (module_exists('menu')) {
    $query = db_select('menu_router', 'm')
      ->fields('m', array(
      'path',
    ))
      ->condition('path', db_like($string) . '%', 'LIKE')
      ->condition('path', 'acquia_purge_ajax_autocomplete', '!=')
      ->condition('path', 'acquia_purge_ajax_processor', '!=')
      ->condition('path', 'admin%', 'NOT LIKE')
      ->range(0, $query_limit)
      ->execute();
    foreach ($query as $path) {
      if (strpos($path->path, '%') !== FALSE) {
        continue;
      }
      $matches[] = $path->path;
    }
  }

  // Sort the values alphabetically.
  sort($matches);

  // Rewrite the matches array by copying the values into keys.
  $clean_url = variable_get('clean_url', 0);
  foreach ($matches as $key => $path) {
    if (!$clean_url) {
      $path = "index.php?q={$path}";
    }
    unset($matches[$key]);
    $matches[$path] = $path;
  }

  // Return the output JSON'ed.
  drupal_json_output($matches);
}