You are here

function imce_search_callback in IMCE Tools 6

Same name and namespace in other branches
  1. 7 imce_search/imce_search.module \imce_search_callback()
1 string reference to 'imce_search_callback'
imce_search_menu in imce_search/imce_search.module
hook_menu implementation, defines imce_swfupload path for handling file uploads

File

imce_search/imce_search.module, line 17

Code

function imce_search_callback() {
  $path_parts = explode('/', $_GET['q']);
  array_shift($path_parts);

  // get rid of callback dir
  $case_insensitive = array_shift($path_parts);
  $file_string = array_pop($path_parts);
  $dir_base = join('/', $path_parts);
  $query_args = array();
  if ($file_string) {
    if ($case_insensitive) {
      $file_column = 'LOWER(filename)';
      $query_args[] = drupal_strtolower($file_string);
    }
    else {
      $file_column = 'filename';
      $query_args[] = $file_string;
    }
    $sql = sprintf('SELECT * FROM {files} WHERE %s', $file_column) . " like('%%%s%%')";
    if ($dir_base) {
      $sql .= ' AND filepath like(\'%s%%\')';
      $query_args[] = file_directory_path() . '/' . $dir_base;
    }
    $suggestions['search'] = 'Searched ' . ($dir_base ? check_plain($dir_base) . ' and sub' : ' all ') . 'directories for ' . check_plain($file_string);
    $suggestions['files'] = array();
    $res = db_query($sql, $query_args);
    while ($row = db_fetch_object($res)) {
      $suggestions['files'][] = $row->filepath;
    }
  }
  else {
    $suggestions['search'] = 'Invalid search criteria, please specify a search';
    $suggestions['files'] = array();
  }
  drupal_json($suggestions);
  exit;
}