<?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
+--------------------------------------------------------------------------
*/

define('IN_AJAX', TRUE);


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

class ajax extends ASK_CONTROLLER
{
	var $per_page = 20;
	
	public function get_access_rule()
	{
		$rule_action['rule_type'] = "white";
		
		if ($this->user_info['permission']['search_avail'])
		{
			$rule_action['rule_type']="black"; //'black'Blacklist, blacklist check  'white' white list, outside the whitelist check
		}
		
		$rule_action['actions'] = array();
		
		return $rule_action;
	}
	
	public function setup()
	{
		HTTP::no_cache_header();
	}
	
	public function search_result_action()
	{
		$limit = intval($_GET['page']) * $this->per_page . ', ' . $this->per_page;
		
		switch ($_GET['search_type'])
		{	
			case 'all':
				$search_result = $this->model('search')->search($_GET['q'], 'all', $limit);
			break;
				
			case 'questions':
				$search_result = $this->model('search')->search($_GET['q'], 'question', $limit);
			break;
			
			case 'topics':
				$search_result = $this->model('search')->search($_GET['q'], 'topic', $limit);
			break;
			
			case 'users':
				$search_result = $this->model('search')->search($_GET['q'], 'user', $limit);
			break;
		}
		
		foreach ($search_result AS $key => $val)
		{
			switch ($val['type'])
			{
				case 1:
					$search_result[$key]['focus'] = $this->model("question")->has_focus_question($val['sno'], $this->user_id);
				break;
				
				case 2:
					$search_result[$key]['focus'] = $this->model('topic')->has_focus_topic($this->user_id, $val['sno']);
				break;
				
				case 3:
					$search_result[$key]['focus'] = $this->model('follow')->user_follow_check($this->user_id, $val['sno']);
				break;
			}
		}
		
		TPL::assign('search_result', $search_result);
		
		TPL::output('search/ajax/search_result');
	}
	
	public function search_action()
	{
		H::ajax_json_output($this->model('search')->search($_GET['q'], $_GET['type'], intval($_GET['limit'])));
	}
}