<?
class tools
{
	function tools() {}


	function menu_top() {
		global $X;

		$row='<li class="main_page"><a href="/"'.($X->T->MAIN_PAGE==true ? ' class="sel"':'').'><i class="fa fa-plus"></i>Strona główna</a></li>';

		$sql=$X->DB->q(' SELECT * FROM strony WHERE active="1" AND parent_id="0" AND top_menu="1" ORDER BY pozycja,id ');
		while($dane=$X->DB->fa($sql)) {
			$row.='<li><a href="'.(empty($dane['link_href']) ? $X->STR->out($dane['x_key']) : $X->STR->out($dane['link_href'])).'"'.($dane_p['target_blank']==1 ? ' target="_blank"':'').($X->STR->out($dane['x_key'])==$_GET['page_key'] ? ' class="sel"':'').'><i class="fa fa-plus"></i>'.$X->STR->out($dane['title']).'</a></li>';
		}

		return '<ul>'.$row.'</ul>';
	}


	function menu_bottom() {
		global $X;

		$row='<li class="main_page"><a href="/"'.($X->T->MAIN_PAGE==true ? ' class="sel"':'').'><i class="fa fa-plus"></i>Strona główna</a></li>';

		$sql=$X->DB->q(' SELECT * FROM strony WHERE active="1" AND parent_id="0" AND bottom_menu="1" ORDER BY pozycja,id ');
		while($dane=$X->DB->fa($sql)) {
			$row.='<li><a href="'.(empty($dane['link_href']) ? $X->STR->out($dane['x_key']) : $X->STR->out($dane['link_href'])).'"'.($dane_p['target_blank']==1 ? ' target="_blank"':'').($X->STR->out($dane['x_key'])==$_GET['page_key'] ? ' class="sel"':'').'><i class="fa fa-plus"></i>'.$X->STR->out($dane['title']).'</a></li>';
		}

		return '<ul>'.$row.'</ul>';
	}


	function error_404() {
		header('HTTP/1.0 404 Not Found');
		$module['title'] = 'Error 404: Not Found';
		$module['html'] = '<div class="container"><div class="page" style="padding:30px 0px 250px 0px;"><h1>Error 404 / Not Found</h1><br /><b>Szukana strona nie została znaleziona.</b><br /><br /><a href="/" class="button">Przejdź do strony głównej</a></div></div>';
		return $module;
	}

	function navi($text, $link=false) {
		if(!empty($link)) {
			$this->NAVI[]='<a href="'.$link.'">'.$text.'</a>';
		} else {
			$this->NAVI[]='<span>'.$text.'</span>';
		}
	}

	function get_navi() {
		if(count($this->NAVI)>0) {
			foreach($this->NAVI as $nav){
				$items.='<li>'.$nav.'</li>';
			}
			if(count($this->NAVI)>0) {
				return '<ol class="breadcrumb">'.$items.'</ol><div class="breadcrumb_after"></div>';
			}
		}


	}


	function get_page_by_key($key, $as_array=false) {
		global $X;

		$page=$X->DB->fast(' SELECT id, title, content, seo_t, seo_k, seo_d FROM strony WHERE active="1" AND x_key="'.$key.'" ');
		if($page['id']==0) return false;

		if($as_array==true) {
			return array(
				'title'	=> $X->STR->outt((string)$page['title']),
				'html'	=> $X->STR->out ((string)$page['content']),
				'seo_t'	=> $X->STR->outt((string)$page['seo_t']),
				'seo_k'	=> $X->STR->outt((string)$page['seo_k']),
				'seo_d'	=> $X->STR->outt((string)$page['seo_d']),
			);
		} else {
			return $X->STR->out($page['content']);
		}
	}



	//xTemplates USAGE:////////////
	//		$TOOLS->add_template('FILM','classes/filmy.php','filmy','init');
	//		$TOOLS->parse_templates($this->HTML);
	function add_template($pattern, $class_file, $class_name, $class_cons, $class_init) {
		$this->TEMPLATES[$pattern] = array('pattern'=>$pattern, 'class_file'=>$class_file, 'class_name'=>$class_name, 'class_cons'=>$class_cons, 'class_init'=>$class_init);
	}

	function parse_templates(&$html) {
		if(count($this->TEMPLATES)==0) return $html;
		preg_match_all('/{{([^}]+)}}/e', $html, $found);
		if(count($found[1])>0) foreach($found[1] as $tmpl) $html=str_replace('{{'.$tmpl.'}}', $this->get_tmpl_data($tmpl), $html);
		return $html;
	}

	function get_tmpl_data($t) {
		$data=@explode('_',$t);
		$T=$this->TEMPLATES[trim($data[0])];
		if(!is_numeric($data[1]) || empty($T)) return false;
		if(!file_exists($T['class_file'])) return false;

		require_once($T['class_file']);
		$class_name = $T['class_name'];
		$class_cons = $T['class_cons'];
		$class_init = $T['class_init'];
		global $$class_name;
		if(!is_object($$class_name)) $$class_name = new $class_cons();
		return $$class_name->$class_init($data[1]);
	}
}
$T=new tools();
$X->T=$T;
?>