default.php 5.38 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 home page, Q&A listing page, 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
*/

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

27 28
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
29 30


Scott committed
31
// Determine whether path begins with qa or not (question and answer listing can be accessed either way)
Scott committed
32

33 34
$requestparts = explode('/', qa_request());
$explicitqa = (strtolower($requestparts[0]) == 'qa');
Scott committed
35

Scott committed
36
if ($explicitqa) {
37
	$slugs = array_slice($requestparts, 1);
Scott committed
38
} elseif (strlen($requestparts[0])) {
39
	$slugs = $requestparts;
Scott committed
40
} else {
41
	$slugs = array();
Scott committed
42
}
Scott committed
43

44
$countslugs = count($slugs);
Scott committed
45 46


Scott committed
47
// Get list of questions, other bits of information that might be useful
Scott committed
48

49
$userid = qa_get_logged_in_userid();
Scott committed
50

51 52 53 54 55
list($questions1, $questions2, $categories, $categoryid, $custompage) = qa_db_select_with_pending(
	qa_db_qs_selectspec($userid, 'created', 0, $slugs, null, false, false, qa_opt_if_loaded('page_size_activity')),
	qa_db_recent_a_qs_selectspec($userid, 0, $slugs),
	qa_db_category_nav_selectspec($slugs, false, false, true),
	$countslugs ? qa_db_slugs_to_category_id_selectspec($slugs) : null,
Scott committed
56
	($countslugs == 1 && !$explicitqa) ? qa_db_page_full_selectspec($slugs[0], false) : null
57
);
Scott committed
58 59


Scott committed
60
// First, if this matches a custom page, return immediately with that page's content
Scott committed
61

62 63
if (isset($custompage) && !($custompage['flags'] & QA_PAGE_FLAGS_EXTERNAL)) {
	qa_set_template('custom-' . $custompage['pageid']);
Scott committed
64

65
	$qa_content = qa_content_prepare();
Scott committed
66

67
	$level = qa_get_logged_in_level();
Scott committed
68

69 70 71
	if (!qa_permit_value_error($custompage['permit'], $userid, $level, qa_get_logged_in_flags()) || !isset($custompage['permit'])) {
		$qa_content['title'] = qa_html($custompage['heading']);
		$qa_content['custom'] = $custompage['content'];
Scott committed
72

73 74 75 76 77 78 79 80
		if ($level >= QA_USER_LEVEL_ADMIN) {
			$qa_content['navigation']['sub'] = array(
				'admin/pages' => array(
					'label' => qa_lang('admin/edit_custom_page'),
					'url' => qa_path_html('admin/pages', array('edit' => $custompage['pageid'])),
				),
			);
		}
Scott committed
81

Scott committed
82
	} else {
83
		$qa_content['error'] = qa_lang_html('users/no_permission');
Scott committed
84
	}
Scott committed
85

86 87
	return $qa_content;
}
Scott committed
88 89


Scott committed
90
// Then, see if we should redirect because the 'qa' page is the same as the home page
Scott committed
91

Scott committed
92
if ($explicitqa && !qa_is_http_post() && !qa_has_custom_home()) {
93
	qa_redirect(qa_category_path_request($categories, $categoryid), $_GET);
Scott committed
94
}
Scott committed
95 96


Scott committed
97
// Then, if there's a slug that matches no category, check page modules provided by plugins
Scott committed
98

Scott committed
99
if (!$explicitqa && $countslugs && !isset($categoryid)) {
100 101
	$pagemodules = qa_load_modules_with('page', 'match_request');
	$request = qa_request();
Scott committed
102

103 104 105 106 107 108
	foreach ($pagemodules as $pagemodule) {
		if ($pagemodule->match_request($request)) {
			$tmpl = isset($custompage['pageid']) ? 'custom-' . $custompage['pageid'] : 'custom';
			qa_set_template($tmpl);
			return $pagemodule->process_request($request);
		}
Scott committed
109
	}
110
}
Scott committed
111 112


Scott committed
113
// Then, check whether we are showing a custom home page
Scott committed
114

Scott committed
115
if (!$explicitqa && !$countslugs && qa_opt('show_custom_home')) {
116 117 118 119 120 121
	qa_set_template('custom');
	$qa_content = qa_content_prepare();
	$qa_content['title'] = qa_html(qa_opt('custom_home_heading'));
	$qa_content['custom'] = qa_opt('custom_home_content');
	return $qa_content;
}
Scott committed
122 123


Scott committed
124
// If we got this far, it's a good old-fashioned Q&A listing page
Scott committed
125

126
require_once QA_INCLUDE_DIR . 'app/q-list.php';
Scott committed
127

128 129 130
qa_set_template('qa');
$questions = qa_any_sort_and_dedupe(array_merge($questions1, $questions2));
$pagesize = qa_opt('page_size_home');
Scott committed
131

132
if ($countslugs) {
Scott committed
133
	if (!isset($categoryid)) {
134
		return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
Scott committed
135
	}
Scott committed
136

137 138 139
	$categorytitlehtml = qa_html($categories[$categoryid]['title']);
	$sometitle = qa_lang_html_sub('main/recent_qs_as_in_x', $categorytitlehtml);
	$nonetitle = qa_lang_html_sub('main/no_questions_in_x', $categorytitlehtml);
Scott committed
140

141 142 143 144
} else {
	$sometitle = qa_lang_html('main/recent_qs_as_title');
	$nonetitle = qa_lang_html('main/no_questions_found');
}
Scott committed
145 146


Scott committed
147
// Prepare and return content for theme for Q&A listing page
Scott committed
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
$qa_content = qa_q_list_page_content(
	$questions, // questions
	$pagesize, // questions per page
	0, // start offset
	null, // total count (null to hide page links)
	$sometitle, // title if some questions
	$nonetitle, // title if no questions
	$categories, // categories for navigation
	$categoryid, // selected category id
	true, // show question counts in category navigation
	$explicitqa ? 'qa/' : '', // prefix for links in category navigation
	qa_opt('feed_for_qa') ? 'qa' : null, // prefix for RSS feed paths (null to hide)
	(count($questions) < $pagesize) // suggest what to do next
		? qa_html_suggest_ask($categoryid)
		: qa_html_suggest_qs_tags(qa_using_tags(), qa_category_path_request($categories, $categoryid)),
	null, // page link params
	null // category nav params
);


return $qa_content;