search.php 4.21 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-page-search.php
	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
23 24 25 26
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
Scott committed
27

Scott committed
28 29 30
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
31 32 33 34


//	Perform the search if appropriate

Scott committed
35
if (strlen(qa_get('q'))) {
Scott committed
36 37 38

	//	Pull in input parameters

Scott committed
39 40 41
	$inquery = trim(qa_get('q'));
	$userid = qa_get_logged_in_userid();
	$start = qa_get_start();
Scott committed
42

Scott committed
43 44 45
	$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
46 47 48

	//	Perform the search using appropriate module

Scott committed
49
	$results = qa_get_search_results($inquery, $start, $count, $userid, false, false);
Scott committed
50 51 52

	//	Count and truncate results

Scott committed
53 54 55
	$pagesize = qa_opt('page_size_search');
	$gotcount = count($results);
	$results = array_slice($results, 0, $pagesize);
Scott committed
56 57 58

	//	Retrieve extra information on users

Scott committed
59
	$fullquestions = array();
Scott committed
60

Scott committed
61 62 63 64
	foreach ($results as $result) {
		if (isset($result['question']))
			$fullquestions[] = $result['question'];
	}
Scott committed
65

Scott committed
66
	$usershtml = qa_userids_handles_html($fullquestions);
Scott committed
67 68 69

	//	Report the search event

Scott committed
70 71 72 73 74
	qa_report_event('search', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
		'query' => $inquery,
		'start' => $start,
	));
}
Scott committed
75 76 77 78


//	Prepare content for theme

Scott committed
79
$qa_content = qa_content_prepare(true);
Scott committed
80

Scott committed
81 82
if (strlen(qa_get('q'))) {
	$qa_content['search']['value'] = qa_html($inquery);
Scott committed
83

Scott committed
84 85 86 87
	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
88

Scott committed
89 90
	$qa_content['q_list']['form'] = array(
		'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
91

Scott committed
92 93 94 95
		'hidden' => array(
			'code' => qa_get_form_security_code('vote'),
		),
	);
Scott committed
96

Scott committed
97
	$qa_content['q_list']['qs'] = array();
Scott committed
98

Scott committed
99
	$qdefaults = qa_post_html_defaults('Q');
Scott committed
100

Scott committed
101 102 103 104 105 106 107 108
	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
109

Scott committed
110 111 112 113 114 115 116 117 118 119 120
	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
121 122
		}

Scott committed
123 124
		if (isset($qdefaults['blockwordspreg']))
			$result['title'] = qa_block_words_replace($result['title'], $qdefaults['blockwordspreg']);
Scott committed
125

Scott committed
126 127
		$fields['title'] = qa_html($result['title']);
		$fields['url'] = qa_html($result['url']);
Scott committed
128

Scott committed
129 130
		$qa_content['q_list']['qs'][] = $fields;
	}
Scott committed
131

Scott committed
132 133
	$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
134

Scott committed
135 136 137 138 139 140
	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
141

Scott committed
142 143
	if (empty($qa_content['page_links']))
		$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
Scott committed
144

Scott committed
145 146
} else
	$qa_content['error'] = qa_lang_html('main/search_explanation');
Scott committed
147 148


Scott committed
149
return $qa_content;