<?php
class Mhelp extends Model
{
	var $base_url = '/public/downloads/';
	var $base_path = 'public/downloads/';
	var $full_url;
	
	var $keywords;
	
	var $search_xml = false;
	
	
	
	
	
	
	
	
	function __construct()
	{
		parent::__construct();
		
		$this->keywords = $this->config->item('help_keywords');
		$this->keywords = $this->keywords[$this->muser->user['lang']];
		
		$g = $this->get_folder();
		
		$this->full_url = $this->base_url.$g[0].'/'.$g[1];
		
	}
	
	function get_folder()
	{
		if(hun())
		{
			return array('2009Help','Hun');
		}
		else
		{
			return array('2009Help','Eng');
		}
	}
	
	function read_help_xml($folder, $lang)
	{
		return simplexml_load_file($this->base_path.$folder.'/'.$lang.'/Data/Tartalom.xml','SimpleXMLElement',LIBXML_NOCDATA);
	}
	
	function read_search_xml($folder, $lang)
	{
		return simplexml_load_file($this->base_path.$folder.'/'.$lang.'/Data/Search.xml','SimpleXMLElement');
	}
	
	function read_search_chunk_xml($folder, $lang, $filename)
	{
		return simplexml_load_file($this->base_path.$folder.'/'.$lang.'/Data/'.$filename,'SimpleXMLElement');
	}
	
	
	function get_stem($word,$xml=false)
	{
		if($xml !== false)
		{
			$this->search_xml = $xml;
		}
		else
		{
			if($xml !== false)
			{
				$this->search_xml = $xml;
			}
			
			$folder = $this->get_folder();
			$this->search_xml = $this->read_search_xml($folder[0],$folder[1]);
		}
	
		$result = array();
		$keys = array();	
			
		foreach($this->search_xml->ents->stem as $stem)
		{
				$attributes = $stem->attributes();
			//ad($attributes);
			if(isset($attributes['chunk']))
			{
				if(strlen($word) >= strlen($attributes['n']))
				{
					if(substr_count( $word,$attributes['n'],0,strlen($attributes['n']))!=0)
					{
						$result[strlen($attributes['n'])] = array( 'chunk'=> $attributes['chunk'],'n'=>$attributes['n']);
						$keys[] = strlen($attributes['n']);
					}
				}
			}
		}
		
		if(count($result)==0)
		{
			return false;
		}
		else
		{
			//return $result[max($keys)];
			return $result[max($keys)];
		}
	}
	
	
	function search($word,$fulltext=false)
	{
		
		$folder = $this->get_folder();
		
		$xml = $this->read_search_xml($folder[0],$folder[1]);
		
		// keresés a fejezet címekben
		$url_found = array();
		
		foreach($xml->urls->Url as $url)
		{
			if(strstr(strtolower($url->attributes()->Title),strtolower($word)))
			{
				$source = substr($url->attributes()->Source,3);
				
				$url_found[]=array('title'=>$url->attributes()->Title, 'url'=>$source);
			}
		}
		
		
		// keresés a szövegben
		if($fulltext)
		{
			$stem = $this->get_stem($word,$xml);
			if($stem)
			{
				
				$chunk = $this->read_search_chunk_xml($folder[0],$folder[1],$stem['chunk']);
				
				foreach($chunk->stem as $stem2)
				{
					$attributes = $stem2->attributes();
					
					$n = (string) $attributes['n'];
					
					if($n==$stem['n'])
					{
						if((string) $stem2->phr->attributes()->n == $word)
						{
							foreach($stem2->phr->ent as $ent)
							{
								$attributes2 = $ent->attributes();
								ad($attributes2);
							}								
						}
						
					}
				}
			}
		}
		
			
		
		if (count($url_found)==0)
		{
			return false;
		}
		else
		{
			return $url_found;
		}
	}
	
	function render_nodes()
	{
		$lang = $this->muser->user['lang'];
		
		
		$folder = $this->get_folder();
		
		$xml = $this->read_help_xml($folder[0], $folder[1]);
		
		$this->full_url = $this->base_url.$folder[0].'/'.$folder[1];
		
		$html = '<ul>';
		
		foreach($xml->TocEntry as $TE)
		{
			$html.= $this->render_node($TE,true);
		}
		
		$html.= '</ul>';
		
		return $html;
	}
	
	
	function render_node($node)
	{
		$out = '';
		
		$rel_url = base64_encode($this->full_url.$node->attributes()->Link);
		
		$out.= '<li><a href="/support/showarticle/'.$rel_url.'">'.$node->attributes()->Title.'</a></li>';
		if(isset($node->TocEntry))
		{
			$out.='<ul>';
			
			foreach($node->TocEntry as $Entry)
			{
				$out.=$this->render_node($Entry);
			}
			
			$out.='</ul>';
		}
		
		
		return $out;
		
		
	}
	
	
	
	
	
	
}