123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- //
- // Copyright 2009 Clemson University
- //
- /*******************************************************************************
- * Tripal Search initiation
- ******************************************************************************/
- function tripal_search_init(){
- // Add javascript
- drupal_add_js(drupal_get_path('theme', 'tripal').
- '/js/tripal_search.js');
- }
- /*******************************************************************************
- * Tripal Search adds advanced search functions to Drupal's Search module
- */
- function tripal_search_menu() {
- $items = array();
- foreach (module_list() as $name) {
- if (module_hook($name, 'search') && $title = module_invoke($name, 'search', 'name')) {
- $items['tripal_search/'. $name] = array(
- 'title' => $title,
- 'page callback' => 'tripal_search_view',
- 'access arguments' => array('search content'),
- 'type' => MENU_LOCAL_TASK
- );
- }
- }
- return $items;
- }
- /*******************************************************************************
- * Implementation of hook_form_alter
- */
- function tripal_search_form_alter(&$form, $form_state, $form_id) {
- if ($form_id == 'search_form' && arg(2)) {
- // for pagination etc.
- $get = drupal_query_string_encode($_GET, array('q'));
- if(preg_match("/node/",arg(1))){
- $form['basic']['inline']['fasta'] = array('#type' => 'markup',
- '#value' => "<br><br><a id=\"tripal_search_link\" href=\"".
- url('tripal_search/'. arg(1) .'/'. urlencode(search_get_keys()),
- array('query' => trim($get)?$get:NULL)).
- "\">Download features (multi-FASTA format)</a><br><br>");
- }
- }
- }
- /*******************************************************************************
- * Menu callback; presents an tripal_search results page.
- */
- function tripal_search_view() {
- $type = arg(1);
- $keys = search_get_keys();
- // Only perform search if there is non-whitespace search term:
- if (!module_hook($type, 'search')) {
- return drupal_not_found();
- }
- if (trim($keys)) {
- $results = tripal_do_search($keys,$type); //var_dump($pager_total['0']);
- return tripal_search_file($type, $keys, $results);
- }
- }
- /*******************************************************************************
- * Return an open search results file.
- */
- function tripal_search_file($type, $keys, $results) {
- drupal_set_header('Content-Type: text');
- drupal_set_header('Content-Disposition: attachment; filename="searchresults.fasta"');
-
- foreach ($results as $result) {
- // Get feature id from drupal database
- $sqld = "SELECT * FROM {chado_feature} CF INNER JOIN {node} N on N.nid = CF.nid WHERE CF.nid = %d";
- if($f_objd = db_fetch_object(db_query($sqld, $result->sid))){
- // Get sequence from chado database
- $previous_db = tripal_db_set_active('chado'); // use chado database
- $sqlc = "SELECT * FROM {feature} WHERE feature_id = '%s'";
- $f_objc = db_fetch_object(db_query($sqlc,$f_objd->feature_id));
- tripal_db_set_active($previous_db); // now use drupal database
- print tripal_feature_return_fasta($f_objc, $desc);
- }
- }
- }
- /*******************************************************************************
- * This code is a duplicate of the do_search function but with the paging
- * call replaced by a simle db_query call.
- */
- function tripal_do_search($keywords, $type, $join1 = '', $where1 = '1 = 1', $arguments1 = array(), $columns2 = 'i.relevance AS score', $join2 = '', $arguments2 = array(), $sort_parameters = 'ORDER BY score DESC') {
- $query = search_parse_query($keywords);
- if ($query[2] == '') {
- form_set_error('keys', t('You must include at least one positive keyword with @count characters or more.', array('@count' => variable_get('minimum_word_size', 3))));
- }
- if ($query[6]) {
- if ($query[6] == 'or') {
- drupal_set_message(t('Search for either of the two terms with uppercase <strong>OR</strong>. For example, <strong>cats OR dogs</strong>.'));
- }
- }
- if ($query === NULL || $query[0] == '' || $query[2] == '') {
- return array();
- }
- // Build query for keyword normalization.
- $conditions = "$where1 AND ($query[2]) AND i.type = '%s'";
- $arguments1 = array_merge($arguments1, $query[3], array($type));
- $join = "INNER JOIN {search_total} t ON i.word = t.word $join1";
- if (!$query[5]) {
- $conditions .= " AND ($query[0])";
- $arguments1 = array_merge($arguments1, $query[1]);
- $join .= " INNER JOIN {search_dataset} d ON i.sid = d.sid AND i.type = d.type";
- }
- // Calculate maximum keyword relevance, to normalize it.
- $select = "SELECT SUM(i.score * t.count) AS score FROM {search_index} i $join WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d ORDER BY score DESC";
- $arguments = array_merge($arguments1, array($query[4]));
- $normalize = db_result(db_query_range($select, $arguments, 0, 1));
- if (!$normalize) {
- return array();
- }
- $columns2 = str_replace('i.relevance', '('. (1.0 / $normalize) .' * SUM(i.score * t.count))', $columns2);
- // Build query to retrieve results.
- $select = "SELECT i.type, i.sid, $columns2 FROM {search_index} i $join $join2 WHERE $conditions GROUP BY i.type, i.sid HAVING COUNT(*) >= %d";
- $count_select = "SELECT COUNT(*) FROM ($select) n1";
- $arguments = array_merge($arguments2, $arguments1, array($query[4]));
-
- // Do actual search query
- // $result = pager_query("$select $sort_parameters", 10, 0, $count_select, $arguments);
- $result = db_query("$select $sort_parameters",$arguments);
- $results = array();
- while ($item = db_fetch_object($result)) {
- $results[] = $item;
- }
- return $results;
- }
|