<?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 system_class extends ASK_MODEL
{
	public function fetch_category_data($type, $parent_id = 0, $order = 'sort ASC,id ASC')
	{
		static $category_list_all;
		
		if (!$category_list_all[$type])
		{
			if ($type)
			{
				$category_list_all_query = $this->fetch_all('category', '`type` = \'' . $this->quote($type) . '\'', $order);
			}
			else
			{
				$category_list_all_query = $this->fetch_all('category', '', $order);
			}
			
			if ($category_list_all_query)
			{
				foreach ($category_list_all_query AS $key => $val)
				{
					$category_list_all[$type][$val['parent_id']][] = $val;
				}
			}
		}
		
		if (!$category_all = $category_list_all[$type][$parent_id])
		{
			return array();
		}
		
		return $category_all;
	}
	
	/* Get classification array */
	public function fetch_category($type, $parent_id = 0)
	{
		$category_list = array();
		
		if (!$category_all = $this->fetch_category_data($type, $parent_id))
		{
			return $category_list;
		}
		
		foreach ($category_all AS $key => $val)
		{
			if (!$val['icon'])
			{
				$val['icon'] = GET_STATIC_URL . '/css/default/img/default_class_imgs.png';
			}
			else
			{
				$val['icon'] = get_setting('upload_url') . '/category/' . $val['icon'];
			}
			
			$category_list[$val['id']] = array(
				'id' => $val['id'],
				'title' => $val['title'],
				'icon' => $val['icon'],
				'description' => $val['description'],
				'parent_id' => $val['parent_id']
			);
			
			if ($child_list = $this->fetch_category($type, $val['id']))
			{
				$category_list[$val['id']]['child'] = $child_list;
			}
		}
		
		return $category_list;
	}
	
	/* Get HTML data classification */
	public function build_category_html($type, $parent_id = 0, $selected_id = 0, $prefix = '', $child = true)
	{
		$category_list = $this->fetch_category($type, $parent_id);
		
		if (!$category_list)
		{
			return false;
		}
		
		if ($prefix)
		{
			$_prefix = $prefix . ' ';
		}
		
		foreach ($category_list AS $category_id => $val)
		{
			if ($selected_id == $val['id'])
			{
				$html .= '<option value="' . $category_id . '" selected="selected">' . $_prefix . $val['title'] . '</option>';
			}
			else
			{
				$html .= '<option value="' . $category_id . '">' . $_prefix . $val['title'] . '</option>';
			}
			
			if ($child && $val['child'])
			{
				$prefix .= '-';
				
				$html .= $this->build_category_html($type, $val['id'], $selected_id, $prefix);
			}
		}
			
		return $html;
	}
	
	/* Get JSON data classification */
	public function build_category_json($type, $parent_id = 0, $prefix = '')
	{
		$category_list = $this->fetch_category($type, $parent_id);
		
		if (empty($category_list))
		{
			return false;
		}
		
		if ($prefix)
		{
			$_prefix = $prefix . ' ';
		}
		
		foreach ($category_list AS $category_id => $val)
		{
			$data[] = array(
				'id' => $category_id,
				'title' => $_prefix . $val['title'],
				'description' => $val['description']
			);
			
			if ($val['child'])
			{
				$prefix .= '-';
				
				$data = array_merge($data, json_decode($this->build_category_json($type, $val['id'], $prefix), true));
			}
		}
			
		return json_encode($data);
	}
	
	/* Gets an array of information */
	public function get_category_info($category_id)
	{
		static $all_category;

		if (!$all_category)
		{
			if ($all_category_query = $this->fetch_all('category'))
			{
				foreach ($all_category_query AS $key => $val)
				{
					if (!$val['url_token'])
					{
						$val['url_token'] = $val['id'];
					}
					
					$all_category[$val['id']] = $val;
				}
			}
		}
		
		return $all_category[$category_id];
	}
	
	/* Gets an array of information */
	public function get_category_info_by_url_token($url_token)
	{
		static $all_category;

		if (!$all_category)
		{
			if ($all_category_query = $this->fetch_all('category'))
			{
				foreach ($all_category_query AS $key => $val)
				{
					if (!$val['url_token'])
					{
						$val['url_token'] = $val['id'];
					}
					
					$all_category[$val['url_token']] = $val;
				}
			}
		}
		
		return $all_category[$url_token];
	}
	
	public function get_category_list($type)
	{
		$category_list = array();
		
		$category_all = $this->fetch_all('category', '`type` = \'' . $this->quote($type) . '\'', 'id ASC');
		
		foreach($category_all as $key => $val)
		{
			if (!$val['url_token'])
			{
				$val['url_token'] = $val['id'];
			}
		
			$category_list[$val['id']] = $val;
		}
		
		return $category_list;
	}
	
	public function get_category_with_child_ids($type, $category_id)
	{
		$category_ids[] = intval($category_id);
		
		if ($child_ids = $this->fetch_category_data($type, $category_id))
		{
			$category_ids = array_merge($category_ids, fetch_array_value($child_ids, 'id'));
		}
		
		return $category_ids;
	}
	
	public function clean_break_attach()
	{
		if ($attachs = $this->query_all("SELECT `id`, `access_key` FROM " . get_table('attach') . " WHERE (item_id IS NULL OR item_id < 1) AND wait_approval = 0 AND add_time < " . (time() - 3600 * 24)))
		{
			foreach ($attachs AS $key => $val)
			{
				$this->model('publish')->remove_attach($val['id'], $val['access_key']);
			}
		}
		
		return true;
	}
	
	public function check_stop_keyword($keyword)
	{
		if (trim($keyword) == '')
		{
			return false;
		}
		
		if (utf8_strlen($keyword) == 1)
		{
			return false;
		}
		
		if (strstr($keyword, 'The') OR strstr($keyword, 'the') OR strstr($keyword, 'Yes'))
		{
			return false;
		}
		
		static $stop_words_list;
		
		if (!$stop_words_list)
		{
			$stop_words_list = array(
				'?', '、', '。', '“', '”', '《', '》', '！', ',', '：', '；', '？', '末', '啊', '阿', '哎', '哎呀', '哎哟', '唉', '俺', '俺们', '按', '按照', '吧', '吧哒', '把', '被', '本', '本着', 
				'aber', 'alle', 'als', 'also', 'am', 'an', 'andern', 'auch', 'auf', 'aus', 'bei', 'bey',
				'bis', 'da', 'daher', 'das', 'dass', 'de', 'dem', 'den', 'der', 'des', 'die', 'diese',
				'dieser', 'dieses', 'doch', 'durch', 'eben', 'ein', 'eine', 'einem', 'einen', 'einer', 'eines',
				'er', 'es', 'fur', 'gegen', 'haben', 'hat', 'ihre', 'im', 'in', 'ist', 'kan', 'man', 'mehr', 'mit',
				'nach', 'nicht', 'noch', 'nur', 'oder', 'ohne', 'sehr', 'sei', 'selbst', 'sey', 'sich', 'sie', 'sind',
				'so', 'uber', 'um', 'und', 'unter', 'vgl', 'vom', 'von', 'weil', 'welche', 'wenn', 'werden', 'wie', 'wird', 'zu', 'zur',
				'a\'s', 'able', 'about', 'above', 'according', 'accordingly', 'across', 'actually',
				'after', 'afterwards', 'again', 'against', 'ain\'t', 'all', 'allow', 'allows',
				'almost', 'alone', 'along', 'already', 'also', 'although', 'always', 'am',
				'among', 'amongst', 'an', 'and', 'another', 'any', 'anybody', 'anyhow',
				'anyone', 'anything', 'anyway', 'anyways', 'anywhere', 'apart', 'appear', 'appreciate',
				'appropriate', 'are', 'aren\'t', 'around', 'as', 'aside', 'ask', 'asking',
				'associated', 'at', 'available', 'away', 'awfully', 'be', 'became', 'because',
				'become', 'becomes', 'becoming', 'been', 'before', 'beforehand', 'behind', 'being',
				'believe', 'below', 'beside', 'besides', 'best', 'better', 'between', 'beyond',
				'both', 'brief', 'but', 'by', 'c\'mon', 'c\'s', 'came', 'can',
				'can\'t', 'cannot', 'cant', 'cause', 'causes', 'certain', 'certainly', 'changes',
				'clearly', 'co', 'com', 'come', 'comes', 'concerning', 'consequently', 'consider',
				'considering', 'contain', 'containing', 'contains', 'corresponding', 'could', 'couldn\'t', 'course',
				'currently', 'definitely', 'described', 'despite', 'did', 'didn\'t', 'different', 'do',
				'does', 'doesn\'t', 'doing', 'don\'t', 'done', 'down', 'downwards', 'during',
				'each', 'edu', 'eg', 'eight', 'either', 'else', 'elsewhere', 'enough',
				'entirely', 'especially', 'et', 'etc', 'even', 'ever', 'every', 'everybody',
				'everyone', 'everything', 'everywhere', 'ex', 'exactly', 'example', 'except', 'far',
				'few', 'fifth', 'first', 'five', 'followed', 'following', 'follows', 'for',
				'former', 'formerly', 'forth', 'four', 'from', 'further', 'furthermore', 'get',
				'gets', 'getting', 'given', 'gives', 'go', 'goes', 'going', 'gone',
				'got', 'gotten', 'greetings', 'had', 'hadn\'t', 'happens', 'hardly', 'has',
				'hasn\'t', 'have', 'haven\'t', 'having', 'he', 'he\'s', 'hello', 'help',
				'hence', 'her', 'here', 'here\'s', 'hereafter', 'hereby', 'herein', 'hereupon',
				'hers', 'herself', 'hi', 'him', 'himself', 'his', 'hither', 'hopefully',
				'how', 'howbeit', 'however', 'i\'d', 'i\'ll', 'i\'m', 'i\'ve', 'ie',
				'if', 'ignored', 'immediate', 'in', 'inasmuch', 'inc', 'indeed', 'indicate',
				'indicated', 'indicates', 'inner', 'insofar', 'instead', 'into', 'inward', 'is',
				'isn\'t', 'it', 'it\'d', 'it\'ll', 'it\'s', 'its', 'itself', 'just',
				'keep', 'keeps', 'kept', 'know', 'known', 'knows', 'last', 'lately',
				'later', 'latter', 'latterly', 'least', 'less', 'lest', 'let', 'let\'s',
				'like', 'liked', 'likely', 'little', 'look', 'looking', 'looks', 'ltd',
				'mainly', 'many', 'may', 'maybe', 'me', 'mean', 'meanwhile', 'merely',
				'might', 'more', 'moreover', 'most', 'mostly', 'much', 'must', 'my',
				'myself', 'name', 'namely', 'nd', 'near', 'nearly', 'necessary', 'need',
				'needs', 'neither', 'never', 'nevertheless', 'new', 'next', 'nine', 'no',
				'nobody', 'non', 'none', 'noone', 'nor', 'normally', 'not', 'nothing',
				'novel', 'now', 'nowhere', 'obviously', 'of', 'off', 'often', 'oh',
				'ok', 'okay', 'old', 'on', 'once', 'one', 'ones', 'only',
				'onto', 'or', 'other', 'others', 'otherwise', 'ought', 'our', 'ours',
				'ourselves', 'out', 'outside', 'over', 'overall', 'own', 'particular', 'particularly',
				'per', 'perhaps', 'placed', 'please', 'plus', 'possible', 'presumably', 'probably',
				'provides', 'que', 'quite', 'qv', 'rather', 'rd', 're', 'really',
				'reasonably', 'regarding', 'regardless', 'regards', 'relatively', 'respectively', 'right', 'said',
				'same', 'saw', 'say', 'saying', 'says', 'second', 'secondly', 'see',
				'seeing', 'seem', 'seemed', 'seeming', 'seems', 'seen', 'self', 'selves',
				'sensible', 'sent', 'serious', 'seriously', 'seven', 'several', 'shall', 'she',
				'should', 'shouldn\'t', 'since', 'six', 'so', 'some', 'somebody', 'somehow',
				'someone', 'something', 'sometime', 'sometimes', 'somewhat', 'somewhere', 'soon', 'sorry',
				'specified', 'specify', 'specifying', 'still', 'sub', 'such', 'sup', 'sure',
				't\'s', 'take', 'taken', 'tell', 'tends', 'th', 'than', 'thank',
				'thanks', 'thanx', 'that', 'that\'s', 'thats', 'the', 'their', 'theirs',
				'them', 'themselves', 'then', 'thence', 'there', 'there\'s', 'thereafter', 'thereby',
				'therefore', 'therein', 'theres', 'thereupon', 'these', 'they', 'they\'d', 'they\'ll',
				'they\'re', 'they\'ve', 'think', 'third', 'this', 'thorough', 'thoroughly', 'those',
				'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together',
				'too', 'took', 'toward', 'towards', 'tried', 'tries', 'truly', 'try',
				'trying', 'twice', 'two', 'un', 'under', 'unfortunately', 'unless', 'unlikely',
				'until', 'unto', 'up', 'upon', 'us', 'use', 'used', 'useful',
				'uses', 'using', 'usually', 'value', 'various', 'very', 'via', 'viz',
				'vs', 'want', 'wants', 'was', 'wasn\'t', 'way', 'we', 'we\'d',
				'we\'ll', 'we\'re', 'we\'ve', 'welcome', 'well', 'went', 'were', 'weren\'t',
				'what', 'what\'s', 'whatever', 'when', 'whence', 'whenever', 'where', 'where\'s',
				'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', 'wherever', 'whether', 'which',
				'while', 'whither', 'who', 'who\'s', 'whoever', 'whole', 'whom', 'whose',
				'why', 'will', 'willing', 'wish', 'with', 'within', 'without', 'won\'t',
				'wonder', 'would', 'wouldn\'t', 'yes', 'yet', 'you', 'you\'d', 'you\'ll',
				'you\'re', 'you\'ve', 'your', 'yours', 'yourself', 'yourselves', 'zero'
			);
		}
		
		if (in_array(trim($keyword), $stop_words_list))
		{
			return false;
		}
		
		return true;
	}
	
	public function analysis_keyword($string)
	{
		$string = str_replace(array(
			"~", 
			"!", 
			"@", 
			"#", 
			"$", 
			"%", 
			"^", 
			"&", 
			"*", 
			",", 
			".", 
			"?", 
			";", 
			":", 
			"'", 
			'"', 
			"[", 
			"]", 
			"{", 
			"}", 
			"！", 
			" ￥", 
			"……", 
			"…", 
			"、", 
			"，", 
			"。", 
			"？", 
			"；", 
			":", 
			"‘", 
			"“", 
			"”", 
			"’", 
			" 【", 
			"】", 
			"～", 
			"！", 
			"＠", 
			"＃", 
			"＄", 
			"％", 
			"＾", 
			"＆", 
			"＊", 
			"，", 
			"．", 
			" ＜", 
			"＞", 
			"；", 
			":", 
			"＇", 
			"＂", 
			"［", 
			"］", 
			"｛", 
			"｝", 
			"／", 
			"＼"
		), '', $string);

		$analysis = new Services_Analisa_Analisa();
		
		$analysis->SetSource(strtolower($string));
		$analysis->StartAnalysis();
		
		$final_result = $analysis->GetFinallyResult(',');
		
		$final_result = explode(',', $final_result);
		
		if (is_array($final_result))
		{			
			$final_result = array_unique($final_result);
			
			foreach ($final_result as $key => $keyword)
			{			
				if (!$this->check_stop_keyword($keyword))
				{
					unset($final_result[$key]);
				}
				else
				{
					$final_result[$key] = trim($keyword);
				}
			}
		}
		
		return $final_result;
	}
}