<?php
/**
* @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @package HiLo CMS
*/

class admin_control_langs{
	private $error = false;
	private $act = 'index';
	private $JsHttpRequest = false;
	private $table = 'system_langs';
	  
	public function __construct(){
	global $MAIN_PAGE;
		define('M_PATH',MAIN_SOURCE_PATH.'/modules/'.$MAIN_PAGE -> dirname() );
		define('M_URL',MAIN_SOURCE_URL.'/modules/'.$MAIN_PAGE -> dirname() );
		define('A_CHARSET', $MAIN_PAGE -> charset() );
		include_once(M_PATH.'/inc/url.php');
		include_once(M_PATH.'/inc/class.langs.php');
		include_once(MAIN_SOURCE_PATH.'/inc/class.html.php');
		include(MAIN_SOURCE_PATH.'/ext/ajax/JsHttpRequest.php');
		$this -> JsHttpRequest = new JsHttpRequest( A_CHARSET );
		$this -> act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : $this -> act;
		$GLOBALS['_RESULT']['id_request'] = !empty($_REQUEST['id_request']) ? $_REQUEST['id_request'] : 0;
		
		preg_match('#^'.$MAIN_PAGE -> dirname().'_(.*)$#iUs',$_REQUEST['p_name'],$res);
		$this -> path =$res[1];
		$this -> path_tmpl = M_PATH.'/tmpl/'.$this -> path;
	}
	
	public function get(){
		$func = $this -> act;
		if ( !method_exists($this ,$func) ){
			sys_error(ERROR_404);
		}
		return $this -> $func();
	}
	

	private function index(){
		$t = new PHPTAL( $this -> path_tmpl.'/main.html' );
		$p = new langsQuery('f');
		//$p -> set_debug(1);
		$p -> include_module('m','m.m_name');
		$p -> orderby_id('DESC');
		$p -> get('f.* ',false);
		$t -> rows = $p -> rows();
		return $t -> execute();
	}
	

	private function ajax_reload(){
		sys_block_disable();
		$t = new PHPTAL(  $this -> path_tmpl.'/inc/tmpl_row.html' );
		$t -> execute = true;
		$p = new langsQuery('f');;
		$p -> include_module('m','m.m_name');
		$p -> orderby_id('DESC');
		$p -> get('f.*',false);
		$t -> rows = $p -> rows();
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}


	private function ajax_delete(){
		sys_block_disable();
		$m = langs::load_id($_REQUEST['id']);
		if ( false===$m ){
			die( A_EMPTY_ID );
		}
		$m -> delete();
		exit();
	}
	

	private function ajax_edit(){
		sys_block_disable();
		$p = new langsQuery('p');
		if ( !$p -> where_id($_REQUEST['id']) ){
			$row = array(
			  'module_list'=>filedsGetLangs::module_list(),
			  'lang_list'=>filedsGetLangs::lang_list(),
			  );
		}else{
			$p -> get('f.*,f.m_id as module_list, f.lang as lang_list');
			$row = $p ->  row();
		}
		$t = new PHPTAL( $this -> path_tmpl.'/edit.html' );
		$t -> row = $row ;
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = 'редактировать';
		exit();
	}
	
	
	
	
	
	private function ajax_save(){
		sys_block_disable();
		$p = langs::load_id($_REQUEST['id']);
		if ( false===$p ){
			$create=1;
			$p =  langs::create();
		}
		foreach( $_POST['form'] as $k=>$v ){
		  $p -> set($k,$v);
		}
    $id = ($create==1) ? $p -> insert() : $p -> update();
		exit();
	}

}

?>
