q-list.php 7.52 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 most question listing pages, plus custom pages and plugin pages


	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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	exit;
}


/**
 * Returns the $qa_content structure for a question list page showing $questions retrieved from the
 * database. If $pagesize is not null, it sets the max number of questions to display. If $count is
 * not null, pagination is determined by $start and $count. The page title is $sometitle unless
 * there are no questions shown, in which case it's $nonetitle. $navcategories should contain the
 * categories retrived from the database using qa_db_category_nav_selectspec(...) for $categoryid,
 * which is the current category shown. If $categorypathprefix is set, category navigation will be
 * shown, with per-category question counts if $categoryqcount is true. The nav links will have the
 * prefix $categorypathprefix and possible extra $categoryparams. If $feedpathprefix is set, the
 * page has an RSS feed whose URL uses that prefix. If there are no links to other pages, $suggest
 * is used to suggest what the user should do. The $pagelinkparams are passed through to
 * qa_html_page_links(...) which creates links for page 2, 3, etc..
 * @param $questions
 * @param $pagesize
 * @param $start
 * @param $count
 * @param $sometitle
 * @param $nonetitle
 * @param $navcategories
 * @param $categoryid
 * @param $categoryqcount
 * @param $categorypathprefix
 * @param $feedpathprefix
 * @param $suggest
 * @param $pagelinkparams
 * @param $categoryparams
 * @param $dummy
 * @return array
 */
function qa_q_list_page_content($questions, $pagesize, $start, $count, $sometitle, $nonetitle,
	$navcategories, $categoryid, $categoryqcount, $categorypathprefix, $feedpathprefix, $suggest,
	$pagelinkparams = null, $categoryparams = null, $dummy = null)
{
	if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }

	require_once QA_INCLUDE_DIR . 'app/format.php';
	require_once QA_INCLUDE_DIR . 'app/updates.php';

	$userid = qa_get_logged_in_userid();


	// Chop down to size, get user information for display

Scott committed
71
	if (isset($pagesize)) {
Scott committed
72
		$questions = array_slice($questions, 0, $pagesize);
Scott committed
73
	}
Scott committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	$usershtml = qa_userids_handles_html(qa_any_get_userids_handles($questions));


	// Prepare content for theme

	$qa_content = qa_content_prepare(true, array_keys(qa_category_path($navcategories, $categoryid)));

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

		'hidden' => array(
			'code' => qa_get_form_security_code('vote'),
		),
	);

	$qa_content['q_list']['qs'] = array();

	if (count($questions)) {
		$qa_content['title'] = $sometitle;

		$defaults = qa_post_html_defaults('Q');
Scott committed
96
		if (isset($categorypathprefix)) {
Scott committed
97
			$defaults['categorypathprefix'] = $categorypathprefix;
Scott committed
98
		}
Scott committed
99 100 101 102 103 104 105 106

		foreach ($questions as $question) {
			$fields = qa_any_to_q_html_fields($question, $userid, qa_cookie_get(), $usershtml, null, qa_post_html_options($question, $defaults));

			if (!empty($fields['raw']['closedbyid'])) {
				$fields['closed'] = array(
					'state' => qa_lang_html('main/closed'),
				);
Scott committed
107 108
			}

Scott committed
109
			$qa_content['q_list']['qs'][] = $fields;
Scott committed
110
		}
Scott committed
111
	} else {
Scott committed
112
		$qa_content['title'] = $nonetitle;
Scott committed
113
	}
Scott committed
114

Scott committed
115 116 117
	if (isset($userid) && isset($categoryid)) {
		$favoritemap = qa_get_favorite_non_qs_map();
		$categoryisfavorite = @$favoritemap['category'][$navcategories[$categoryid]['backpath']];
Scott committed
118

Scott committed
119 120
		$qa_content['favorite'] = qa_favorite_form(QA_ENTITY_CATEGORY, $categoryid, $categoryisfavorite,
			qa_lang_sub($categoryisfavorite ? 'main/remove_x_favorites' : 'main/add_category_x_favorites', $navcategories[$categoryid]['title']));
Scott committed
121 122
	}

Scott committed
123
	if (isset($count) && isset($pagesize)) {
Scott committed
124
		$qa_content['page_links'] = qa_html_page_links(qa_request(), $start, $pagesize, $count, qa_opt('pages_prev_next'), $pagelinkparams);
Scott committed
125
	}
Scott committed
126

127 128
	$qa_content['canonical'] = qa_get_canonical();

Scott committed
129
	if (empty($qa_content['page_links'])) {
Scott committed
130
		$qa_content['suggest_next'] = $suggest;
Scott committed
131
	}
Scott committed
132

Scott committed
133
	if (qa_using_categories() && count($navcategories) && isset($categorypathprefix)) {
Scott committed
134
		$qa_content['navigation']['cat'] = qa_category_navigation($navcategories, $categoryid, $categorypathprefix, $categoryqcount, $categoryparams);
Scott committed
135
	}
Scott committed
136

137 138 139 140 141
	// set meta description on category pages
	if (!empty($navcategories[$categoryid]['content'])) {
		$qa_content['description'] = qa_html($navcategories[$categoryid]['content']);
	}

Scott committed
142
	if (isset($feedpathprefix) && (qa_opt('feed_per_category') || !isset($categoryid))) {
Scott committed
143 144 145
		$qa_content['feed'] = array(
			'url' => qa_path_html(qa_feed_request($feedpathprefix . (isset($categoryid) ? ('/' . qa_category_path_request($navcategories, $categoryid)) : ''))),
			'label' => strip_tags($sometitle),
Scott committed
146
		);
Scott committed
147
	}
Scott committed
148

Scott committed
149 150
	return $qa_content;
}
Scott committed
151 152


Scott committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166
/**
 * Return the sub navigation structure common to question listing pages
 * @param $sort
 * @param $categoryslugs
 * @return array
 */
function qa_qs_sub_navigation($sort, $categoryslugs)
{
	$request = 'questions';

	if (isset($categoryslugs)) {
		foreach ($categoryslugs as $slug) {
			$request .= '/' . $slug;
		}
Scott committed
167 168
	}

Scott committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	$navigation = array(
		'recent' => array(
			'label' => qa_lang('main/nav_most_recent'),
			'url' => qa_path_html($request),
		),

		'hot' => array(
			'label' => qa_lang('main/nav_hot'),
			'url' => qa_path_html($request, array('sort' => 'hot')),
		),

		'votes' => array(
			'label' => qa_lang('main/nav_most_votes'),
			'url' => qa_path_html($request, array('sort' => 'votes')),
		),

		'answers' => array(
			'label' => qa_lang('main/nav_most_answers'),
			'url' => qa_path_html($request, array('sort' => 'answers')),
		),

		'views' => array(
			'label' => qa_lang('main/nav_most_views'),
			'url' => qa_path_html($request, array('sort' => 'views')),
		),
	);

Scott committed
196
	if (isset($navigation[$sort])) {
Scott committed
197
		$navigation[$sort]['selected'] = true;
Scott committed
198
	} else {
Scott committed
199
		$navigation['recent']['selected'] = true;
Scott committed
200
	}
Scott committed
201

Scott committed
202
	if (!qa_opt('do_count_q_views')) {
Scott committed
203
		unset($navigation['views']);
Scott committed
204
	}
Scott committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

	return $navigation;
}


/**
 * Return the sub navigation structure common to unanswered pages
 * @param $by
 * @param $categoryslugs
 * @return array
 */
function qa_unanswered_sub_navigation($by, $categoryslugs)
{
	$request = 'unanswered';

	if (isset($categoryslugs)) {
		foreach ($categoryslugs as $slug) {
			$request .= '/' . $slug;
		}
	}
Scott committed
225

Scott committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	$navigation = array(
		'by-answers' => array(
			'label' => qa_lang('main/nav_no_answer'),
			'url' => qa_path_html($request),
		),

		'by-selected' => array(
			'label' => qa_lang('main/nav_no_selected_answer'),
			'url' => qa_path_html($request, array('by' => 'selected')),
		),

		'by-upvotes' => array(
			'label' => qa_lang('main/nav_no_upvoted_answer'),
			'url' => qa_path_html($request, array('by' => 'upvotes')),
		),
	);

Scott committed
243
	if (isset($navigation['by-' . $by])) {
Scott committed
244
		$navigation['by-' . $by]['selected'] = true;
Scott committed
245
	} else {
Scott committed
246
		$navigation['by-answers']['selected'] = true;
Scott committed
247
	}
Scott committed
248

Scott committed
249
	if (!qa_opt('voting_on_as')) {
Scott committed
250
		unset($navigation['by-upvotes']);
Scott committed
251
	}
Scott committed
252 253 254

	return $navigation;
}