search.php 4.17 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for search page


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28 29
require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR . 'app/options.php';
require_once QA_INCLUDE_DIR . 'app/search.php';
Scott committed
30 31


Scott committed
32
// Perform the search if appropriate
Scott committed
33

Scott committed
34
if (strlen(qa_get('q'))) {
Scott committed
35
	// Pull in input parameters
Scott committed
36 37 38
	$inquery = trim(qa_get('q'));
	$userid = qa_get_logged_in_userid();
	$start = qa_get_start();
Scott committed
39

Scott committed
40 41 42
	$display = qa_opt_if_loaded('page_size_search');
	$count = 2 * (isset($display) ? $display : QA_DB_RETRIEVE_QS_AS) + 1;
	// get enough results to be able to give some idea of how many pages of search results there are
Scott committed
43

Scott committed
44
	// Perform the search using appropriate module
Scott committed
45

Scott committed
46
	$results = qa_get_search_results($inquery, $start, $count, $userid, false, false);
Scott committed
47

Scott committed
48
	// Count and truncate results
Scott committed
49

Scott committed
50 51 52
	$pagesize = qa_opt('page_size_search');
	$gotcount = count($results);
	$results = array_slice($results, 0, $pagesize);
Scott committed
53

Scott committed
54
	// Retrieve extra information on users
Scott committed
55

Scott committed
56
	$fullquestions = array();
Scott committed
57

Scott committed
58 59 60 61
	foreach ($results as $result) {
		if (isset($result['question']))
			$fullquestions[] = $result['question'];
	}
Scott committed
62

Scott committed
63
	$usershtml = qa_userids_handles_html($fullquestions);
Scott committed
64

Scott committed
65
	// Report the search event
Scott committed
66

Scott committed
67 68 69 70 71
	qa_report_event('search', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
		'query' => $inquery,
		'start' => $start,
	));
}
Scott committed
72 73


Scott committed
74
// Prepare content for theme
Scott committed
75

Scott committed
76
$qa_content = qa_content_prepare(true);
Scott committed
77

Scott committed
78 79
if (strlen(qa_get('q'))) {
	$qa_content['search']['value'] = qa_html($inquery);
Scott committed
80

Scott committed
81 82 83 84
	if (count($results))
		$qa_content['title'] = qa_lang_html_sub('main/results_for_x', qa_html($inquery));
	else
		$qa_content['title'] = qa_lang_html_sub('main/no_results_for_x', qa_html($inquery));
Scott committed
85

Scott committed
86 87
	$qa_content['q_list']['form'] = array(
		'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
88

Scott committed
89 90 91 92
		'hidden' => array(
			'code' => qa_get_form_security_code('vote'),
		),
	);
Scott committed
93

Scott committed
94
	$qa_content['q_list']['qs'] = array();
Scott committed
95

Scott committed
96
	$qdefaults = qa_post_html_defaults('Q');
Scott committed
97

Scott committed
98 99 100 101 102 103 104 105
	foreach ($results as $result) {
		if (!isset($result['question'])) { // if we have any non-question results, display with less statistics
			$qdefaults['voteview'] = false;
			$qdefaults['answersview'] = false;
			$qdefaults['viewsview'] = false;
			break;
		}
	}
Scott committed
106

Scott committed
107 108 109 110 111 112 113 114 115 116 117
	foreach ($results as $result) {
		if (isset($result['question'])) {
			$fields = qa_post_html_fields($result['question'], $userid, qa_cookie_get(),
				$usershtml, null, qa_post_html_options($result['question'], $qdefaults));
		} elseif (isset($result['url'])) {
			$fields = array(
				'what' => qa_html($result['url']),
				'meta_order' => qa_lang_html('main/meta_order'),
			);
		} else {
			continue; // nothing to show here
Scott committed
118 119
		}

Scott committed
120 121
		if (isset($qdefaults['blockwordspreg']))
			$result['title'] = qa_block_words_replace($result['title'], $qdefaults['blockwordspreg']);
Scott committed
122

Scott committed
123 124
		$fields['title'] = qa_html($result['title']);
		$fields['url'] = qa_html($result['url']);
Scott committed
125

Scott committed
126 127
		$qa_content['q_list']['qs'][] = $fields;
	}
Scott committed
128

Scott committed
129 130
	$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $start + $gotcount,
		qa_opt('pages_prev_next'), array('q' => $inquery), $gotcount >= $count);
Scott committed
131

Scott committed
132 133 134 135 136 137
	if (qa_opt('feed_for_search')) {
		$qa_content['feed'] = array(
			'url' => qa_path_html(qa_feed_request('search/' . $inquery)),
			'label' => qa_lang_html_sub('main/results_for_x', qa_html($inquery)),
		);
	}
Scott committed
138

Scott committed
139 140
	if (empty($qa_content['page_links']))
		$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
Scott committed
141

Scott committed
142 143
} else
	$qa_content['error'] = qa_lang_html('main/search_explanation');
Scott committed
144 145


Scott committed
146
return $qa_content;