You are here

function filedepot_displaySearchListing in filedepot 7

Same name and namespace in other branches
  1. 6 lib-ajaxserver.php \filedepot_displaySearchListing()
1 call to filedepot_displaySearchListing()
filedepot_dispatcher in ./ajaxserver.php

File

./lib-ajaxserver.php, line 462
lib-ajaxserver.php Library functions for the ajax_server

Code

function filedepot_displaySearchListing($query_terms) {
  $output = '';
  if (!empty($query_terms)) {
    $filedepot = filedepot_filedepot();
    $args = array();
    $fields = array(
      'a.title',
      'a.description',
      'b.name',
      'b.description',
    );
    $keywords = explode(' ', $query_terms);
    $sql = '';
    $select = "SELECT a.fid, a.cid, a.title, a.fname, a.date, a.version, a.submitter, " . "a.status, a.description, a.size, b.pid, b.nid, b.name as foldername, b.description as folderdesc " . "FROM filedepot_files a JOIN filedepot_categories b ON b.cid=a.cid ";
    if (!empty($filedepot->allowableViewFoldersSql)) {
      $where = 'WHERE a.cid IN (' . $filedepot->allowableViewFoldersSql . ') AND ';
    }
    else {
      $where = 'WHERE ';
    }

    /* Build the AND clause of the SQL Query
     * Find files that match all the search terms in any of the defined fields
     * So for each field name, description folder, that field text must have
     * all the keywords
     */
    $field_id = 1;
    $where_and = '';
    foreach ($fields as $field) {
      $term_id = 1;
      $where_and .= $field_id == 1 ? '(' : '1=1) OR (';
      foreach ($keywords as $keyword) {
        $where_and .= "{$field} like :afield{$field_id}_{$term_id} AND ";
        $args = array_merge($args, array(
          ":afield{$field_id}_{$term_id}" => "%{$keyword}%",
        ));
        $term_id++;
      }
      $field_id++;
    }
    $where_and .= empty($where_and) ? '' : '1=1) ';
    $sql .= "{$select} {$where} {$where_and} UNION {$select} ";

    /* Build the second query - UNION used so that we return first the
     * files that match all the keywords
     * Second query will find files that match ANY of the search terms in any of the defined fields
     */
    if (!empty($filedepot->allowableViewFoldersSql)) {
      $sql .= ' WHERE a.cid IN (' . $filedepot->allowableViewFoldersSql . ') AND (';
    }
    else {
      $sql .= ' WHERE (';
    }

    /* Build the second query OR clause - loop over the fields and terms */
    $field_id = 1;
    $where_or = '';
    foreach ($fields as $field) {
      $term_id = 1;
      $where_or .= $field_id > 1 ? '  OR ' : '';
      foreach ($keywords as $keyword) {
        $where_or .= $term_id > 1 ? '  OR ' : '';
        $where_or .= "{$field} like :bfield{$field_id}_{$term_id}";
        $args = array_merge($args, array(
          ":bfield{$field_id}_{$term_id}" => "%{$keyword}%",
        ));
        $term_id++;
      }
      $field_id++;
    }
    $where_or .= !empty($where_or) ? ') ' : ' ';
    $sql .= $where_or;
    $result = db_query($sql, $args);
    $time = timer_read('get_time');
    drupal_set_message($time);
    $filedepot->recordCount = $result
      ->rowCount();
    while ($A = $result
      ->fetchAssoc()) {
      $output .= theme('filedepot_filelisting', array(
        'listingrec' => $A,
      ));
    }
  }
  return $output;
}