<?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 users_control_members{
	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(MAIN_PATH.'/tmp/config/users.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(){
		$func = $this -> act;
		if ( !method_exists($this ,$func) ){
			sys_error(ERROR_404);
		}
		return  $this -> $func();
	}
	
	
	
	private function index(){
	global $MAIN_PAGE;
		$t = new PHPTAL( M_PATH.'/tmpl/control_members/main.html' );
		$p = new usersQuery('u');
		$p -> orderby_id('DESC');
		$p -> set_page(1);
		$p -> set_count_rows( $MAIN_PAGE -> config('users_pub_on_page') );
		//$p -> set_debug(1);
		$p -> get('
			u.u_active AS row_class,
			u.u_type AS user_type,
			u.u_login AS url_user_comments,
			u.*',true);
		$t -> rows =$p -> rows() ;
		$count = $p -> get_count_rows();
		if ( !empty($count) ){
			include_once(MAIN_SOURCE_PATH.'/inc/class.pagenav.php');
			$pn = new page_nav( $count);
			$pn -> set_limit( $MAIN_PAGE -> config('users_pub_on_page') );
			$pn -> set_url_tmpl(URL_PAGE_NAV);
			$t -> page_nav = $pn -> get(1);
		}
			//	подгружаем библиотеки 
		upload_img::include_files();
		return $t -> execute();
	}
	
	
	
		/*		AJAX	*/
		
		
		
	private function ajax_reload(){
	global $MAIN_PAGE;
		sys_block_disable();
		$t = new PHPTAL( M_PATH.'/tmpl/control_members/inc/row.html' );
		$t -> execute = true;

		$p = new usersQuery('u');
		if ( !empty($_REQUEST['orderby_key']) ){
			$p -> $_REQUEST['orderby_key']( $_REQUEST['orderby_value']);
		}else{
			$p -> orderby_id('DESC');
		}
		if ( !empty($_REQUEST['filter_active']) ){
			if (  $_REQUEST['filter_active']==5){
				 $_REQUEST['filter_active']=0;
			}
			$p -> where_active( $_REQUEST['filter_active'] );
		}
		if ( !empty($_REQUEST['filter_group']) ){
			$p -> where_group( $_REQUEST['filter_group'] );
		}
		
		$p -> where_id( $_REQUEST['filter_id'] );
		$p -> where_like_login( $_REQUEST['filter_login'] );
		
		$p -> set_page($_REQUEST['page']);
		$p -> set_count_rows( $MAIN_PAGE -> config('users_pub_on_page') );
		//$p -> set_debug(1);
		$p -> get('
			u.u_active AS row_class,
			u.u_login AS profile_url,
			u.*',true);
		$t -> rows = $p -> rows() ;
		
		$count = $p -> get_count_rows();
		if ( !empty($count) ){
			include_once(MAIN_SOURCE_PATH.'/inc/class.pagenav.php');
			$pn = new page_nav( $count);
			$pn -> set_limit( $MAIN_PAGE -> config('users_pub_on_page') );
			$pn -> set_url_tmpl(URL_PAGE_NAV);
			$t -> page_nav = $pn -> get($_REQUEST['page']);
		}
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}
	
		

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

	
	/**
	* форма редактирования пользователя
	*
	*/
	private function ajax_edit(){
	global $MAIN_PAGE;
		sys_block_disable();
		
		$b = new usersQuery('u');
		//$b -> set_debug(1);
		if ( !$b -> where_id($_GET['id']) )
		{
			$row = array('u_id'=>0,'groups_list'=> filedsGetUsers::groups_list(), 'upl_img'=> filedsGetUsers::upl_img(),);
		}
		else
		{
			$b -> get(
			'u.*,
			u.u_active as u_active_checked,
			u.u_avatar as upl_img,
			u.u_active AS `unconfirm`, 
			u.g_id as groups_list');
			$row = $b ->  row();
		}
   

 
		$t = new PHPTAL( false);
		$t -> setSnippet('users','users_control_members_ajax_edit');
		$t -> row = $row;
		
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = EDIT;
		exit();
	}
	

	/**
	* Сохраняем пользователя
	*
	*/
	private function ajax_save(){
	
		sys_block_disable();
	
		$p = users::load_id($_REQUEST['id']);
		if ( false===$p )
		{
			$p = users::create();
			$create = true;
			$p -> set('u_time_reg',MAIN_TIME);
		}
		//$p -> set_debug(1);
		
		try
		{
		  foreach( $_POST['form'] as $k=>$v )
		  {
		    $p -> set($k,$v);
		  }
		  $id = ( true===$create) ? $p -> insert() :  $p -> update();
		}
		catch( Exception $e )
		{
      sys_error(ERROR_501,$e -> getMessage() );  
    }
			
		
		
		$GLOBALS['_RESULT']['msg'] = MOD_SAVE;
		exit();
	}
	
	
	
		

	

}

?>
