1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/
	File: qa-include/qa-page-categories.php
	Description: Controller for page listing categories
	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
*/
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
	header('Location: ../');
	exit;
}
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
$categoryslugs = qa_request_parts(1);
$countslugs = count($categoryslugs);
//	Get information about appropriate categories and redirect to questions page if category has no sub-categories
$userid = qa_get_logged_in_userid();
list($categories, $categoryid, $favoritecats) = qa_db_select_with_pending(
	qa_db_category_nav_selectspec($categoryslugs, false, false, true),
	$countslugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null,
	isset($userid) ? qa_db_user_favorite_categories_selectspec($userid) : null
);
if ($countslugs && !isset($categoryid))
	return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
//	Function for recursive display of categories
function qa_category_nav_to_browse(&$navigation, $categories, $categoryid, $favoritemap)
{
	foreach ($navigation as $key => $navlink) {
		$category = $categories[$navlink['categoryid']];
		if (!$category['childcount'])
			unset($navigation[$key]['url']);
		elseif ($navlink['selected']) {
			$navigation[$key]['state'] = 'open';
			$navigation[$key]['url'] = qa_path_html('categories/' . qa_category_path_request($categories, $category['parentid']));
		} else
			$navigation[$key]['state'] = 'closed';
		if (@$favoritemap[$navlink['categoryid']])
			$navigation[$key]['favorited'] = true;
		$navigation[$key]['note'] =
			' - <a href="'.qa_path_html('questions/'.implode('/', array_reverse(explode('/', $category['backpath'])))).'">'.( ($category['qcount']==1)
				? qa_lang_html_sub('main/1_question', '1', '1')
				: qa_lang_html_sub('main/x_questions', number_format($category['qcount']))
			).'</a>';
		if (strlen($category['content']))
			$navigation[$key]['note'] .= qa_html(' - ' . $category['content']);
		if (isset($navlink['subnav']))
			qa_category_nav_to_browse($navigation[$key]['subnav'], $categories, $categoryid, $favoritemap);
	}
}
//	Prepare content for theme
$qa_content = qa_content_prepare(false, array_keys(qa_category_path($categories, $categoryid)));
$qa_content['title'] = qa_lang_html('misc/browse_categories');
if (count($categories)) {
	$navigation = qa_category_navigation($categories, $categoryid, 'categories/', false);
	unset($navigation['all']);
	$favoritemap = array();
	if (isset($favoritecats))
		foreach ($favoritecats as $category)
			$favoritemap[$category['categoryid']] = true;
	qa_category_nav_to_browse($navigation, $categories, $categoryid, $favoritemap);
	$qa_content['nav_list'] = array(
		'nav' => $navigation,
		'type' => 'browse-cat',
	);
} else {
	$qa_content['title'] = qa_lang_html('main/no_categories_found');
	$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
}
return $qa_content;