<?php
if (!defined('QA_VERSION'))
{
    header('Location: ../');
    exit;
}
require_once QA_INCLUDE_DIR . 'king-db/selects.php';
require_once QA_INCLUDE_DIR . 'king-app/format.php';
$requestparts = explode('/', qa_request());
$explicitqa = (strtolower($requestparts[0]) == 'qa');

if ($explicitqa)
{
    $slugs = array_slice($requestparts, 1);
}
else
if (strlen($requestparts[0]))
{
    $slugs = $requestparts;
}
else
{
    $slugs = array();
}
$countslugs = count($slugs);
$start = qa_get_start();
$userid = qa_get_logged_in_userid();
list($questions1, $questions2, $categories, $categoryid, $custompage) = qa_db_select_with_pending(
    qa_db_qs_selectspec($userid, 'created', $start, $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,
	(($countslugs == 1) && !$explicitqa) ? qa_db_page_full_selectspec($slugs[0], false) : null
);
//for cat desc in meta
 if (isset($categoryid) && !( $categories[$categoryid]['content'] && !qa_has_custom_home()))
  {
define('CURRENT_CATEGORY_CONTENT', $categories[$categoryid]['content']);
$CURRENT_CATEGORY_CONTENT = $categories[$categoryid]['content'];
// var_dump('This variable should have changed value: ', $CURRENT_CATEGORY_CONTENT);
}
//endforcatdesc in meta
if (isset($custompage) && !($custompage['flags'] & QA_PAGE_FLAGS_EXTERNAL))
{
    qa_set_template('custom-' . $custompage['pageid']);
    $qa_content = qa_content_prepare();
    $level = qa_get_logged_in_level();
    
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['custompage'] = $custompage['content'];
        
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']
                    )) ,
                ) ,
            );
        }
    }
    else
    {
        $qa_content['error'] = qa_lang_html('users/no_permission');
    }
    $qa_content['sside'] = true;
    $qa_content['class'] = ' post-page';
    return $qa_content;
}

if ($explicitqa && (!qa_is_http_post()) && !qa_has_custom_home())
{
    qa_redirect(qa_category_path_request($categories, $categoryid) , $_GET);
}

if ((!$explicitqa) && $countslugs && !isset($categoryid))
{
    $pagemodules = qa_load_modules_with('page', 'match_request');
    $request = qa_request();
    foreach ($pagemodules as $pagemodule)
    {
        
if ($pagemodule->match_request($request))
        {
            qa_set_template('plugin');
            return $pagemodule->process_request($request);
        }
    }
}

if ((!$explicitqa) && (!$countslugs) && qa_opt('show_custom_home'))
{
    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;
}
require_once QA_INCLUDE_DIR . 'king-app/q-list.php';
qa_set_template('qa');
$questions = qa_any_sort_and_dedupe(array_merge($questions1, $questions2));
$pagesize = qa_opt('page_size_home');

if (!isset($categoryid))
{
    return include QA_INCLUDE_DIR . 'king-page-not-found.php';
}
$output = '';

if ($countslugs)
{
    $output .= '';
    $output .= '<H1 style="font-size: 25px; padding: 4px;"> ';
    $output .= $categories[$categoryid]['icon'] . ' ';
    $output .= qa_html($categories[$categoryid]['title']);
    $output .= '</H1><br/>';
    
if (!$categories[$categoryid]['qcount'])
    {
        $output .= '<H2><p style="font-size: 18px;padding: 10px;margin-top: -21px;border-top: #d8d6d6 solid 1px;">' . $categories[$categoryid]['content'] . ' - <u>Download free ' . $categories[$categoryid]['title'] . ' in ' . date("Y") . '</u> [NEW] </p></H2>';
        $output .= '<br/><p>' . qa_lang_html('main/no_questions_found') . '</p>';
    }
    else
    {
        $output .= '<H2><p style="font-size: 18px;padding: 10px;margin-top: -21px;border-top: #d8d6d6 solid 1px;">' . $categories[$categoryid]['content'] . ' - <u>Download free ' . $categories[$categoryid]['title'] . ' in ' . date("Y") . '</u> [NEW] </p></H2>';
    }
    $output .= king_follow_tc($categoryid, 'cat');
    $output .= '';
    $categorytitlehtml = qa_html($categories[$categoryid]['title']);
    $sometitle = qa_lang_html_sub('main/recent_qs_as_in_x', $categorytitlehtml);
    $nonetitle = $output;
}
else
{
    $sometitle = qa_lang_html('main/recent_qs_as_title');
    $nonetitle = qa_lang_html('main/no_questions_found');
}
$qa_content = qa_q_list_page_content(
	$questions, $pagesize, $start, $countslugs ? $categories[$categoryid]['qcount'] : qa_opt('cache_qcount'), 
	$sometitle, $nonetitle, $categories, $categoryid, true,
	$explicitqa ? 'qa/' : '', qa_opt('feed_for_qa') ? 'qa' : null, null, null, null
);

if (QA_ALLOW_UNINDEXED_QUERIES || !$countslugs)
{
    $qa_content['navigation']['sub'] = qa_qs_sub_navigation($sort, $categoryslugs);
}
$qa_content['class'] = ' full-page';
$qa_content['header'] = $output;
return $qa_content;