qa-page-search.php 4.44 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-page-search.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-app-format.php';
	require_once QA_INCLUDE_DIR.'qa-app-options.php';
	require_once QA_INCLUDE_DIR.'qa-app-search.php';


//	Perform the search if appropriate

	if (strlen(qa_get('q'))) {
Scott Vivian committed
40

Gideon Greenspan committed
41
	//	Pull in input parameters
Scott Vivian committed
42

Gideon Greenspan committed
43 44 45
		$inquery=trim(qa_get('q'));
		$userid=qa_get_logged_in_userid();
		$start=qa_get_start();
Scott Vivian committed
46

Gideon Greenspan committed
47 48 49
		$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 Vivian committed
50

Gideon Greenspan committed
51 52 53
	//	Perform the search using appropriate module

		$results=qa_get_search_results($inquery, $start, $count, $userid, false, false);
Scott Vivian committed
54

Gideon Greenspan committed
55
	//	Count and truncate results
Scott Vivian committed
56

Gideon Greenspan committed
57 58 59
		$pagesize=qa_opt('page_size_search');
		$gotcount=count($results);
		$results=array_slice($results, 0, $pagesize);
Scott Vivian committed
60 61 62

	//	Retrieve extra information on users

Gideon Greenspan committed
63
		$fullquestions=array();
Scott Vivian committed
64

Gideon Greenspan committed
65 66 67
		foreach ($results as $result)
			if (isset($result['question']))
				$fullquestions[]=$result['question'];
Scott Vivian committed
68

Gideon Greenspan committed
69
		$usershtml=qa_userids_handles_html($fullquestions);
Scott Vivian committed
70

Gideon Greenspan committed
71
	//	Report the search event
Scott Vivian committed
72

Gideon Greenspan committed
73 74 75 76 77 78 79 80 81 82 83 84 85
		qa_report_event('search', $userid, qa_get_logged_in_handle(), qa_cookie_get(), array(
			'query' => $inquery,
			'start' => $start,
		));
	}


//	Prepare content for theme

	$qa_content=qa_content_prepare(true);

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

Gideon Greenspan committed
87 88 89 90
		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 Vivian committed
91

Gideon Greenspan committed
92
		$qa_content['q_list']['form']=array(
Gideon Greenspan committed
93
			'tags' => 'method="post" action="'.qa_self_html().'"',
Gideon Greenspan committed
94 95 96 97

			'hidden' => array(
				'code' => qa_get_form_security_code('vote'),
			),
Gideon Greenspan committed
98
		);
Scott Vivian committed
99

Gideon Greenspan committed
100
		$qa_content['q_list']['qs']=array();
Scott Vivian committed
101

Gideon Greenspan committed
102
		$qdefaults=qa_post_html_defaults('Q');
Scott Vivian committed
103

Gideon Greenspan committed
104 105
		foreach ($results as $result)
			if (!isset($result['question'])) { // if we have any non-question results, display with less statistics
Gideon Greenspan committed
106 107 108
				$qdefaults['voteview']=false;
				$qdefaults['answersview']=false;
				$qdefaults['viewsview']=false;
Gideon Greenspan committed
109 110
				break;
			}
Scott Vivian committed
111

Gideon Greenspan committed
112 113
		foreach ($results as $result) {
			if (isset($result['question']))
Gideon Greenspan committed
114 115
				$fields=qa_post_html_fields($result['question'], $userid, qa_cookie_get(),
					$usershtml, null, qa_post_html_options($result['question'], $qdefaults));
Scott Vivian committed
116

Gideon Greenspan committed
117 118 119 120 121 122 123 124
			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 Vivian committed
125

Gideon Greenspan committed
126 127
			if (isset($qdefaults['blockwordspreg']))
				$result['title']=qa_block_words_replace($result['title'], $qdefaults['blockwordspreg']);
Scott Vivian committed
128

Gideon Greenspan committed
129 130
			$fields['title']=qa_html($result['title']);
			$fields['url']=qa_html($result['url']);
Scott Vivian committed
131

Gideon Greenspan committed
132 133 134 135 136
			$qa_content['q_list']['qs'][]=$fields;
		}

		$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 Vivian committed
137

Gideon Greenspan committed
138 139 140 141 142 143 144 145 146 147 148 149
		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)),
			);

		if (empty($qa_content['page_links']))
			$qa_content['suggest_next']=qa_html_suggest_qs_tags(qa_using_tags());

	} else
		$qa_content['error']=qa_lang_html('main/search_explanation');

Scott Vivian committed
150 151


Gideon Greenspan committed
152 153 154 155 156 157
	return $qa_content;


/*
	Omit PHP closing tag to help avoid accidental output
*/