qa-page-categories.php 3.87 KB
Newer Older
Gideon Greenspan committed
1 2 3
<?php

/*
Gideon Greenspan committed
4
	Question2Answer by Gideon Greenspan and contributors
Gideon Greenspan committed
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

	http://www.question2answer.org/

	
	File: qa-include/qa-page-categories.php
	Version: See define()s at top of qa-include/qa-base.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.'qa-db-selects.php';
	require_once QA_INCLUDE_DIR.'qa-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
	
Gideon Greenspan committed
42 43
	$userid=qa_get_logged_in_userid();
	list($categories, $categoryid, $favoritecats)=qa_db_select_with_pending(
Gideon Greenspan committed
44
		qa_db_category_nav_selectspec($categoryslugs, false, false, true),
Gideon Greenspan committed
45 46
		$countslugs ? qa_db_slugs_to_category_id_selectspec($categoryslugs) : null,
		isset($userid) ? qa_db_user_favorite_categories_selectspec($userid) : null
Gideon Greenspan committed
47 48 49 50 51 52 53 54
	);
	
	if ($countslugs && !isset($categoryid))
		return include QA_INCLUDE_DIR.'qa-page-not-found.php';
	

//	Function for recursive display of categories

Gideon Greenspan committed
55
	function qa_category_nav_to_browse(&$navigation, $categories, $categoryid, $favoritemap)
Gideon Greenspan committed
56 57 58 59 60 61 62 63 64 65 66 67
	{
		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';
				
Gideon Greenspan committed
68 69 70
			if (@$favoritemap[$navlink['categoryid']])
				$navigation[$key]['favorited']=true;
				
Gideon Greenspan committed
71 72 73
			$navigation[$key]['note']='';
			
			$navigation[$key]['note'].=
Gideon Greenspan committed
74
				' - <a href="'.qa_path_html('questions/'.implode('/', array_reverse(explode('/', $category['backpath'])))).'">'.( ($category['qcount']==1)
Gideon Greenspan committed
75 76
					? qa_lang_html_sub('main/1_question', '1', '1')
					: qa_lang_html_sub('main/x_questions', number_format($category['qcount']))
Gideon Greenspan committed
77
				).'</a>';
Gideon Greenspan committed
78 79 80 81 82
				
			if (strlen($category['content']))
				$navigation[$key]['note'].=qa_html(' - '.$category['content']);
			
			if (isset($navlink['subnav']))
Gideon Greenspan committed
83
				qa_category_nav_to_browse($navigation[$key]['subnav'], $categories, $categoryid, $favoritemap);
Gideon Greenspan committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		}
	}
		

//	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']);
		
Gideon Greenspan committed
99 100 101 102 103 104
		$favoritemap=array();
		if (isset($favoritecats))
			foreach ($favoritecats as $category)
				$favoritemap[$category['categoryid']]=true;

		qa_category_nav_to_browse($navigation, $categories, $categoryid, $favoritemap);
Gideon Greenspan committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		
		$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;


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