<?php
/*
+--------------------------------------------------------------------------
|   TanyaCMS - Question and Answer Sofware
|   -----------------------------------------------------------------------
|   by fbschool dev team
|   (c) 2011 - 2012 FBSCHOOL
|   http://www.fbschool.com/tanyacms
|   -----------------------------------------------------------------------
|   This is NOT a freeware, use is subject to license terms
|   Support: 13455n@gmail.com
+--------------------------------------------------------------------------
*/


if (!defined('IN_TANYACMS'))
{
	die;
}

class main extends ASK_CONTROLLER
{

	public function get_access_rule()
	{
		$rule_action['rule_type'] = "white"; //Blacklist, blacklist check  'white' white list, outside the whitelist check
		

		if ($this->user_info['permission']['visit_feature'] AND $this->user_info['permission']['visit_site'])
		{
			$rule_action['actions'] = array(
				'index'
			);
		}
		
		return $rule_action;
	}

	public function setup()
	{
		$this->crumb('Feature', '/feature/');
	}

	public function index_action()
	{
		if (is_numeric($_GET['id']))
		{
			if (! $feature_info = $this->model('feature')->get_feature_by_id($_GET['id']))
			{
				H::redirect_msg('Feature does not exist', '/');
			}
		}
		else if (! $feature_info = $this->model('feature')->get_feature_by_url_token($_GET['id']))
		{
			H::redirect_msg('Feature does not exist', '/');
		}
		
		if ($feature_info['url_token'] != $_GET['id'])
		{
			HTTP::redirect('/feature/' . $feature_info['url_token']);
		}
		
		if (! $topic_list = $this->model('feature')->get_topics_by_feature_id($feature_info['id']))
		{
			H::redirect_msg('Must be included under the topic 1 more topics', '/');
		}
		
		foreach ($topic_list as $key => $val)
		{
			$topic_ids[] = $val['topic_id'];
		}
		
		$topic_focus = $this->model('topic')->has_focus_topics($this->user_id, $topic_ids);
	
		foreach ($topic_list as $key => $val)
		{
			$topic_list[$key]['has_focus'] = $topic_focus[$val['topic_id']];
		}
		
		$this->crumb($feature_info['title'], '/feature/' . $feature_info['url_token']);
		
		TPL::import_css('css/main.css');
		
		TPL::assign('sidebar_hot_topics', $topic_list);
		TPL::assign('feature_info', $feature_info);
		
		$nav_menu = $this->model('menu')->get_nav_menu_list(null, true);
		
		if (is_array($nav_menu['feature_ids']) && in_array($feature_info['id'], $nav_menu['feature_ids']))
		{
			// Navigation
			if (TPL::is_output('block/content_nav_menu.tpl.htm', 'home/explore'))
			{
				unset($nav_menu['feature_ids']);
				TPL::assign('content_nav_menu', $nav_menu);
			}
			
			// Sidebar Hot users
			if (TPL::is_output('block/sidebar_hot_users.tpl.htm', 'home/explore'))
			{
				$sidebar_hot_users = $this->model('module')->sidebar_hot_users($this->user_id, 5);
				
				TPL::assign('sidebar_hot_users', $sidebar_hot_users);
			}
			
			// Sidebar topics
			if (TPL::is_output('block/sidebar_feature.tpl.htm', 'home/explore'))
			{
				$feature_list = $this->model('module')->feature_list();
				TPL::assign('feature_list', $feature_list);
			}
			
			if (TPL::is_output('block/content_question.tpl.htm', 'home/explore'))
			{
				if ($feature_info['id'])
				{
					$_GET['topic_id'] = $this->model('feature')->get_topics_by_feature_id($feature_info['id'], false, false);
				}
				
				if (! $_GET['sort_type'])
				{
					$_GET['sort_type'] = 'new';
				}
			
				if ($_GET['sort_type'] == 'unresponsive')
				{
					$_GET['answer_count'] = '0';
				}
				
				$question_list = $this->model('question')->get_questions_list($_GET['page'], get_setting('contents_per_page'), $_GET['sort_type'], $_GET['topic_id'], $_GET['category'], $_GET['answer_count'], $_GET['day']);
				
				TPL::assign('question_list', $question_list);
				TPL::assign('question_list_bit', TPL::output('question/ajax/list', false));
			}
			
			TPL::import_js('js/explore.js');
			
			TPL::output('home/explore');
		}
		else
		{			
			TPL::import_js('js/feature.js');
			
			TPL::output('feature/detail');
		}
	
	}
}