You are here

function biblio_db_search in Bibliography Module 5

Same name and namespace in other branches
  1. 6.2 includes/biblio.pages.inc \biblio_db_search()
  2. 6 biblio.pages.inc \biblio_db_search()
1 call to biblio_db_search()
_biblio_get_user_pubs in ./biblio.module
1 string reference to 'biblio_db_search'
biblio_menu in ./biblio.module
Implementation of hook_menu().

File

./biblio.module, line 2268

Code

function biblio_db_search() {
  global $user;
  $inline = false;
  $join = array();
  $join[] = "left join {biblio} b  on n.vid=b.vid ";
  $join[] = "left join {biblio_types} bt  on b.biblio_type=bt.tid ";
  if (variable_get('biblio_view_only_own', 0)) {
    $limit .= " AND n.uid={$user->uid} ";
  }
  $pager_attrib = array(
    "sort" => variable_get('biblio_sort', 'year'),
    'order' => variable_get('biblio_order', 'DESC'),
  );
  $limits = null;
  if (isset($_GET['order'])) {
    $pager_attrib['order'] = $_GET['order'] == 'desc' ? 'desc' : 'asc';
  }
  if (isset($_GET['sort'])) {
    $pager_attrib['sort'] = check_plain($_GET['sort']);
  }
  switch ($pager_attrib['sort']) {
    case 'type':
      $sortby = "ORDER BY b.biblio_type %s, b.biblio_year DESC, SUBSTRING(n.title,1,1) ASC ";
      break;
    case 'title':
      $sortby = "ORDER BY SUBSTRING(n.title,1,1) %s, b.biblio_year DESC ";
      break;
    case 'author':

      /*$join[] = "inner join {biblio_has_author} as ha on a.aid = ha.aid";
        $join[] = "inner join  {biblio_author_index} as a on b.nid= ha.nid";
        $sortby = " ORDER BY a.author %s ";
        $where[] = "ha.rank = 1 ";
        */
      $sortby = "ORDER BY SUBSTRING(LTRIM(b.biblio_authors),1,1) %s, b.biblio_year DESC ";
      break;
    case 'keyword':

      // added msh 070808
      $sortby = "ORDER BY SUBSTRING(LTRIM(b.biblio_keywords),1,4) %s, b.biblio_keywords ASC ";
      break;
    case 'year':
    default:
      $sortby = "ORDER BY b.biblio_year %s, b.biblio_date DESC, SUBSTRING(n.title,1,1) ASC, b.biblio_type ASC ";
  }

  //end switch
  if (!isset($_SESSION['biblio_filter']) || !is_array($_SESSION['biblio_filter'])) {
    $_SESSION['biblio_filter'] = array();
  }
  $numargs = func_num_args();
  $arg_list = func_get_args();
  if (!in_array('profile', $arg_list)) {

    //don't include any stored filters on the profile page
    $session =& $_SESSION['biblio_filter'];
    foreach ($session as $filter) {
      $arg_list = array_merge($arg_list, $filter);
    }
  }
  if (count($arg_list) >= 1) {
    $args = array();
    while ($arg_list) {
      $type = $arg_list[0];
      array_shift($arg_list);
      $operator = $operator ? $operator : " AND ";

      //defaults to AND
      switch ($type) {
        case 'and':
          $operator = " AND ";
          break;
        case 'or':
          $operator = " OR ";
          break;
        case 'inline':
          $inline = 'inline';
          break;
        case 'profile':
          $inline = "profile";
          break;
        case 'term':
        case 'term_id':
          $term = explode("?", $arg_list[0]);
          array_shift($arg_list);
          $join[] = "inner join {term_node} as tn on n.nid = tn.nid";
          if ($type == 'term') {
            $join[] = "inner join  {term_data} as td on tn.tid= td.tid";
            $where[] = "td.name = '%s' ";
          }
          elseif ($type == 'term_id') {
            $where[] = "tn.tid = '%d' ";
          }
          $terms[] = db_escape_string($term[0]);
          array_push($args, $type, $term[0]);
          break;
        case 'rss.xml':
          $rss = TRUE;
          break;
        case 'author':
          $term = explode("?", $arg_list[0]);
          array_shift($arg_list);
          $where[] = 'b.biblio_authors RLIKE "[[:<:]]%s[[:>:]]" ';
          $terms[] = db_escape_string($term[0]);
          array_push($args, $type, $term[0]);
          $title = 'Author - ' . $term[0];
          $operator = NULL;
          break;
        case 'publisher':
          $term = explode("?", $arg_list[0]);
          array_shift($arg_list);
          $where[] = "b.biblio_publisher RLIKE '%s' ";
          $terms[] = db_escape_string($term[0]);
          array_push($args, $type, $term[0]);
          $operator = NULL;
          break;
        case 'year':
          $term = db_escape_string($arg_list[0]);
          array_shift($arg_list);
          $where[] = "b.biblio_year=%d ";

          //$limit .= " AND b.biblio_year=%d ";
          $terms[] = (int) $term;
          array_push($args, $type, (int) $term);
          $operator = NULL;
          break;
        case 'uid':
          $term = db_escape_string($arg_list[0]);
          array_shift($arg_list);
          $where[] = "n.uid=%d ";

          //$limit .= " AND b.biblio_year=%d ";
          $terms[] = (int) $term;
          array_push($args, $type, (int) $term);
          $operator = NULL;
          break;
        case 'keyword':
          $term = explode("?", $arg_list[0]);
          array_shift($arg_list);
          $where[] = "b.biblio_keywords RLIKE '%s' ";
          $terms[] = db_escape_string($term[0]);
          array_push($args, $type, $term[0]);
          $operator = NULL;
          break;
        case 'type':
          $term = db_escape_string($arg_list[0]);
          array_shift($arg_list);
          $limit .= $operator . "b.biblio_type=%d ";
          $terms[] = (int) $term;
          array_push($args, $type, (int) $term);
          $operator = NULL;
          break;
        case 'order':
          $term = db_escape_string($arg_list[0]) == 'desc' ? 'desc' : 'asc';
          array_shift($arg_list);
          $pager_attrib['order'] = $term;
          array_push($args, $type, $term);
          break;
        case 'sort':
          $term = db_escape_string($arg_list[0]);
          array_shift($arg_list);
          $pager_attrib['sort'] = $term;
          array_push($args, $type, $term);
          switch ($term) {
            case 'type':
              $sortby = "ORDER BY b.biblio_type %s, b.biblio_year DESC, SUBSTRING(n.title,1,1) ASC ";
              break;
            case 'title':
              $sortby = "ORDER BY SUBSTRING(n.title,1,1) %s, b.biblio_year DESC ";
              break;
            case 'year':
            default:
              $sortby = "ORDER BY b.biblio_year %s, SUBSTRING(n.title,1,1) ASC, b.biblio_type ASC ";
          }

          //end switch
          break;
        default:
          $fields = biblio_get_db_fields();
          $term = explode("?", $arg_list[0]);
          array_shift($arg_list);
          if (in_array("biblio_{$type}", $fields)) {
            $where[] = "b.biblio_{$type} RLIKE '%s' ";
            $terms[] = db_escape_string($term[0]);
            array_push($args, $type, $term[0]);
            $operator = NULL;
          }
          break;
      }
    }
  }
  $where[] = "n.type='biblio' ";
  if ($user->uid != 1) {
    $where[] = 'n.status = 1 ';
  }

  //show only published entries to everyone except admin
  $joins = implode(' ', $join);
  $where_clause = count($where) > 1 ? '(' . implode(') AND (', $where) . ')' : $where[0];
  $query = db_rewrite_sql("SELECT DISTINCT n.*, b.*, bt.name as biblio_type_name FROM {node} n {$joins}  WHERE {$where_clause} {$published} {$limit} {$sortby} ");
  $count_query = db_rewrite_sql("SELECT DISTINCT COUNT(*) FROM {node} n {$joins}  WHERE {$where_clause} {$published} {$limit} ");

  //echo $query; die;
  $_SESSION['last_biblio_query'] = $query;
  $terms[] = $pager_attrib['order'];
  $_SESSION['last_biblio_query_terms'] = $terms;
  $result = pager_query($query, variable_get('biblio_rowsperpage', 25), 0, $count_query, $terms);
  if ($rss) {
    biblio_feed($result, $title);
  }
  else {
    return biblio_show_results($result, $pager_attrib, $args, $inline);
  }
}