<?php
/*
	
*/

class users_control_groups{
	private $error = false;
	private $act = 'index';
	private $JsHttpRequest = false;
	
	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.groups.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'] = !empty($_REQUEST['id_request']) ? $_REQUEST['id_request'] : 0;
	}
	
	public function get(){
		//sleep(2);
		$func = $this -> act;
		if ( !method_exists($this ,$func) ){
			sys_error(ERROR_404);
		}

		return $this -> $func();
		
		
	}
	
	
	
	private function index(){
		$t = new PHPTAL( M_PATH.'/tmpl/control_groups/main.html' );
		$p = new groups_q('a');
		$p -> orderby_id('DESC');
		//$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_GROUPS = sys_url(URL_USERS_CONTROL_GROUPS);
		$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_groups/inc/row.html' );
		$t -> execute = true;
		$p = new groups_q('a');
		if ( !empty($_REQUEST['orderby_key']) ){
			$p -> $_REQUEST['orderby_key']( $_REQUEST['orderby_value']);
		}else{		
			$p -> orderby_id('DESC');
		}
		//$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 = groups::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 groups_q('b');
		$b -> where_id($_REQUEST['id']);
		if ( !$b -> get('*') ){
			die( $b -> get_error() );
		}
		$row = $b ->  row();
		
		$time_update_limit = array
		(
			1243752188=>M_USERS_GROUP_NO_TIME_LIMIT,//	ограничения наступят через 30 лет ))
			604800=>M_USERS_GROUP_1_WEEK_LIMIT,	
			86400=>M_USERS_GROUP_1_DAY_LIMIT,
			3600=>M_USERS_GROUP_1_HOUR_LIMIT,
			0=>M_USERS_GROUP_FULL_LIMIT	//	сразу
		);
		/*$s = new select_box();
  		$s -> empty_record(false);
  		$s -> selected_id( $row['g_time_update_limit'] );
		foreach( $time_update_limit as $time =>$value ){
			$s -> set($time,$value );
		}
   	 	$row['g_time_update_limit_list'] = $s -> get(); */
   	 
   	 	$groups = array
		(
			0=>M_USERS_GROUP_NONE,
			'user'=>M_USERS_GROUP_USER,	
			'admin'=>M_USERS_GROUP_ADMIN,
			'org'=>M_USERS_GROUP_ORG,
		);
		$s = new select_box();
  		$s -> empty_record(false);
  		$s -> selected_id( $row['g_type'] );
		foreach( $groups as $k =>$v ){
			$s -> set($k,$v);
		}
   	 	$row['g_type_list'] = $s -> get(); 
   	 	
			//	выводим
		$t = new PHPTAL( M_PATH.'/tmpl/control_groups/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 = groups::load_id($_REQUEST['id']);
		
		if ( false===$p ){
			die( A_EMPTY_ID );
		}
			//	устанавливаем обновлённые переменные
		$p -> set('g_name',$_REQUEST['g_name']);
		$p -> set('g_type',$_REQUEST['g_type']);
		//$p -> set('g_time_update_limit',$_REQUEST['g_time_update_limit']);
		//$p -> set_debug(1);
		$id = $p -> update();
		if ( false===$id ){
			echo $p -> get_error();
			exit();
		}
		$GLOBALS['_RESULT']['msg'] = MOD_SAVE;
		exit();
	}
	
	

	


}

?>
