imce_search.module 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php //$Id$
  2. /**
  3. * hook_menu implementation, defines imce_swfupload path for handling file
  4. * uploads
  5. */
  6. function imce_search_menu() {
  7. $menu['imce_search_callback'] = array(
  8. 'title' => 'IMCE Search Callback',
  9. 'type' => MENU_CALLBACK,
  10. 'page callback' => 'imce_search_callback',
  11. 'access callback' => 'imce_access',
  12. );
  13. return $menu;
  14. }
  15. function imce_search_callback() {
  16. $path_parts = explode('/', $_GET['q']);
  17. array_shift($path_parts); // get rid of callback dir
  18. $case_insensitive = array_shift($path_parts);
  19. $file_string = array_pop($path_parts);
  20. $dir_base = join('/', $path_parts);
  21. $query_args = array();
  22. if ($file_string) {
  23. $query = db_select('file_managed', 'f', array('target' => 'slave'));
  24. $query->fields('f', array('uri'));
  25. if ($case_insensitive) {
  26. $query->where('LOWER(f.filename) LIKE (:fname)', array(':fname' => '%' . drupal_strtolower($file_string) . '%'));
  27. }
  28. else {
  29. $query->condition('f.filename', '%' . $file_string . '%', 'LIKE');
  30. }
  31. if ($dir_base) {
  32. $query->condition('uri', '%//%' . $dir_base . '%' , 'LIKE');
  33. }
  34. $suggestions['search'] = 'Searched ' . ($dir_base ? check_plain($dir_base) . ' and sub' : ' all ') . 'directories for ' . check_plain($file_string);
  35. $suggestions['files'] = array();
  36. $res = $query->execute();
  37. foreach ($res as $row) {
  38. $suggestions['files'][] = file_uri_target($row->uri);
  39. }
  40. }
  41. else {
  42. $suggestions['search'] = 'Invalid search criteria, please specify a search';
  43. $suggestions['files'] = array();
  44. }
  45. echo drupal_json_encode($suggestions);
  46. exit;
  47. }
  48. function imce_search_form($form, &$form_state) {
  49. $form['imce-search-term'] = array(
  50. '#type' => 'textfield',
  51. '#title' => t('Search'),
  52. '#description' => t('Search for files in current directory and subdirectories'),
  53. '#size' => 25,
  54. );
  55. $form['imce-search-case'] = array(
  56. '#type' => 'checkbox',
  57. '#title' => t('Case insensitive'),
  58. '#description' => t('This option may have no effect depending on your database configuration'),
  59. '#default_value' => 0,
  60. );
  61. $form['imce-search-button'] = array(
  62. '#type' => 'button',
  63. '#value' => t('Search'),
  64. );
  65. $form['imce-search-results'] = array(
  66. '#markup' => '<div id="imce-search-results"><div></div><ul></ul></div>',
  67. );
  68. return ($form);
  69. }
  70. /**
  71. * provide form content to imce, registered function for callback
  72. */
  73. function imce_search_content(&$imce) {
  74. drupal_add_css(drupal_get_path('module', 'imce_search') . '/imce_search.css');
  75. drupal_add_js(drupal_get_path('module', 'imce_search') . '/imce_search.js');
  76. $form = drupal_get_form('imce_search_form');
  77. return drupal_render($form);
  78. }