<?php
/*
	
*/

class users_control_rang{
	private $error = false;
	private $act = 'index';
	private $JsHttpRequest = false;
	
	public function __construct(){
	global $MAIN_PAGE;
			
		global $MAIN_DB;
		$MAIN_DB -> set_prefix('cn');
			//	--	--	--
			
		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.rang.php');
	
		include_once(M_PATH.'/cache/config.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'] = $_REQUEST['id_request'];
	}
	
	public function get(){
		//sleep(2);
		$func = $this -> act;
		if ( !method_exists($this ,$func) ){
			sys_error(ERROR_404);
		}

		$res =  $this -> $func();
		global $MAIN_DB;
		$MAIN_DB -> set_prefix(MAIN_DBPREFIX);
		
		return $res;
		//	return $this -> $func();
			//	--	--	--
		
		
	}
	
	
	
	private function index(){
		$t = new PHPTAL( M_PATH.'/tmpl/control_rang/main.html' );
		$p = new rang_q('a');
		//$p -> set_debug(1);
		$p -> get('*',false);
		$rows= array();
		while( $row = $p -> row() ){
			$rows[]=arr_to_obj($row);
		}		
		$t -> rows = $rows;
		$t -> URL_USERS_CONTROL_RANG = sys_url(URL_USERS_CONTROL_RANG);
		$t -> URL_USERS_CONTROL_MEMBERS = sys_url(URL_USERS_CONTROL_MEMBERS);
		$t -> URL_ADMIN = sys_url(URL_ADMIN);
		return $t -> execute();
	}
	
	
	
		/*		AJAX	*/
		
		
	private function ajax_reload(){
		sys_block_disable();
		$t = new PHPTAL( M_PATH.'/tmpl/control_rang/inc/row.html' );
		$t -> execute = true;
		$p = new rang_q('a');
		//$p -> set_debug(1);
		$p -> get('*',false);
		$rows= array();
		while( $row = $p -> row() ){
			$rows[]=arr_to_obj($row);
		}		
		$t -> rows = $rows;
	
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}
	
		

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

	
	
		//	редактируем id
	private function ajax_edit(){
		sys_block_disable();

			//	получаем id  из базы
		$b = new rang_q('b');
		if ( !$b -> where_id($_REQUEST['id']) ){
			$row = array('r_id'=>0);
		}
		else{
			$b -> get('*');
			$row = $b ->  row();
		}
			
		
   	 	
			//	выводим
		$t = new PHPTAL( M_PATH.'/tmpl/control_rang/edit.html' );
		$t -> row = arr_to_obj($row );
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = EDIT;
		exit();
	}
	

		//	сохраняем форму 
	private function ajax_save(){
		sys_block_disable();
			//	загружаем объект
		$p = rang::load_id($_REQUEST['id']);
		
		if ( false===$p ){
			$p =rang::create();
		}
			//	устанавливаем обновлённые переменные
		$p -> set('r_name',$_REQUEST['r_name']);
		$p -> set('r_points',$_REQUEST['r_points']);
		$p -> set('r_image',$_REQUEST['r_image']);
		//$p -> set_debug(1);
		if ( empty($_REQUEST['id']) ){
			$id = $p -> insert();
		}else{
			$id = $p -> update();
		}
		if ( false===$id ){
			echo $p -> get_error();
			exit();
		}
		$GLOBALS['_RESULT']['msg'] = MOD_SAVE;
		exit();
	}
	
	

	


}

?>
