categories.php 3.53 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 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
*/

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
	exit;
}
Scott committed
26

Scott committed
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 32
$categoryslugs = qa_request_parts(1);
$countslugs = count($categoryslugs);
Scott committed
33 34


Scott committed
35
// Get information about appropriate categories and redirect to questions page if category has no sub-categories
Scott committed
36

Scott committed
37 38 39 40 41 42
$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
);
Scott committed
43

Scott committed
44
if ($countslugs && !isset($categoryid)) {
Scott committed
45
	return include QA_INCLUDE_DIR . 'qa-page-not-found.php';
Scott committed
46
}
Scott committed
47 48


Scott committed
49
// Function for recursive display of categories
Scott committed
50

Scott committed
51 52 53 54
function qa_category_nav_to_browse(&$navigation, $categories, $categoryid, $favoritemap)
{
	foreach ($navigation as $key => $navlink) {
		$category = $categories[$navlink['categoryid']];
Scott committed
55

Scott committed
56
		if (!$category['childcount']) {
Scott committed
57
			unset($navigation[$key]['url']);
Scott committed
58
		} elseif ($navlink['selected']) {
Scott committed
59 60 61 62
			$navigation[$key]['state'] = 'open';
			$navigation[$key]['url'] = qa_path_html('categories/' . qa_category_path_request($categories, $category['parentid']));
		} else
			$navigation[$key]['state'] = 'closed';
Scott committed
63

Scott committed
64
		if (@$favoritemap[$navlink['categoryid']]) {
Scott committed
65
			$navigation[$key]['favorited'] = true;
Scott committed
66
		}
Scott committed
67

Scott committed
68 69
		$navigation[$key]['note'] =
			' - <a href="'.qa_path_html('questions/'.implode('/', array_reverse(explode('/', $category['backpath'])))).'">'.( ($category['qcount']==1)
Scott committed
70
				? qa_lang_html_sub('main/1_question', '1', '1')
Scott committed
71 72
				: qa_lang_html_sub('main/x_questions', number_format($category['qcount']))
			).'</a>';
Scott committed
73

Scott committed
74 75
		if (strlen($category['content']))
			$navigation[$key]['note'] .= qa_html(' - ' . $category['content']);
Scott committed
76

Scott committed
77 78
		if (isset($navlink['subnav']))
			qa_category_nav_to_browse($navigation[$key]['subnav'], $categories, $categoryid, $favoritemap);
Scott committed
79
	}
Scott committed
80
}
Scott committed
81 82


Scott committed
83
// Prepare content for theme
Scott committed
84

Scott committed
85
$qa_content = qa_content_prepare(false, array_keys(qa_category_path($categories, $categoryid)));
Scott committed
86

Scott committed
87
$qa_content['title'] = qa_lang_html('misc/browse_categories');
Scott committed
88

Scott committed
89 90
if (count($categories)) {
	$navigation = qa_category_navigation($categories, $categoryid, 'categories/', false);
Scott committed
91

Scott committed
92
	unset($navigation['all']);
Scott committed
93

Scott committed
94
	$favoritemap = array();
Scott committed
95 96
	if (isset($favoritecats)) {
		foreach ($favoritecats as $category) {
Scott committed
97
			$favoritemap[$category['categoryid']] = true;
Scott committed
98 99
		}
	}
Scott committed
100

Scott committed
101
	qa_category_nav_to_browse($navigation, $categories, $categoryid, $favoritemap);
Scott committed
102

Scott committed
103 104 105 106
	$qa_content['nav_list'] = array(
		'nav' => $navigation,
		'type' => 'browse-cat',
	);
Scott committed
107

Scott committed
108 109 110 111
} else {
	$qa_content['title'] = qa_lang_html('main/no_categories_found');
	$qa_content['suggest_next'] = qa_html_suggest_qs_tags(qa_using_tags());
}
Scott committed
112 113


Scott committed
114
return $qa_content;