<?php
if ( defined("class_groups_file") ){
  return false;
}
define("class_groups_file", 1);

include_once(MAIN_SOURCE_PATH.'/inc/class.query.php');
include_once(MAIN_SOURCE_PATH.'/modules/users/inc/url.php');
include_once(MAIN_SOURCE_PATH.'/modules/users/lang/ru/lang.php');

	//	класс запросов к таблице модулей
class groups_q extends query{
	
	
	
	public function __construct($alias){
	global $MAIN_DB;
		$this -> set_table($MAIN_DB -> prefix("system_groups") );
		$this -> set_alias($alias);
	}
	
	/*public function include_modules($alias,$cols=''){
	global $MAIN_DB;
		if ( empty($alias) ){
			$this -> make_error(ERROR_500);
		}
		$this -> include_table($MAIN_DB -> prefix('system_modules'),$alias,'m_id',$cols );
		return true;
	}
	*/
	public function where_id($v){
		$v = intval($v);
		if ( empty($v)  ){
			return FALSE;
		}
		return $this -> set_where( $this -> alias.'.g_id',$v);
	}
	
	
	
	
	public function orderby_id($v){
		return 	$this -> set_orderby($this -> alias.'.g_id',$v);
	}
	
	
	
	public function orderby_title($v){
		return 	$this -> set_orderby($this -> alias.'.g_name',$v);
	}
	
	
	/***
		Функции - обработчики столбцов таблицы
	***/
	
		
	

}



class groups{
	
	private $debug = false;
	private $info = array();
	private $last_error = false;
	private $main_key = false;
	private $set_info = array();
	
	private function __construct( $array=array() ){
		$this -> info = $array;
		$this -> main_key = $array['g_id'];
	}
	
	public function error(){
  		return $this -> last_error;
	}
	
	
	public function get($k){
		if ( isset($this -> info[$k]) ){
			return $this -> info[$k];
		} 
		sys_error(500, sprintf(E_NOT_VAR,$k));
	}
	
	public function set($k,$v){
		if ( method_exists($this,'set_'.$k) ){
			$f = 'set_'.$k;
			return $this -> $f($v);
		}
		$this -> set_info[$k] = $v;
		return true;
	}
	
	/***
		SET-функции
	**/



	private function set_g_name($v){
		
		if ( empty($v) ){
			$this -> make_error( M_GROUPS_ERROR_EMPTY_NAME );
		}
		$this -> set_info['g_name']=$v;
		return true;
	}
	
	/*private function set_g_type($v){
		if ( empty($v) ){
			$this -> make_error( sprintf(M_FAQ_ERROR_SHORT_DESC,$MAIN_PAGE-> config('faq_min_size_desc')) );
		}
		$this -> set_info['g_type']=sys_short_text($v);
		return true;
	}*/


	
	
	/***
	/***
		GET
	***/
	
	public function id(){
		return $this -> info['g_id'];
	}	
	
	
	public function set_debug($flag){
		$this -> debug = $flag;
	}
	
	public function delete(){
	global $MAIN_DB;
		$id = $this -> id();
		if ( empty($id) ){
			sys_error(500,E_EMPTY_ID);
		}
		$sql = "DELETE FROM ".MAIN_GROUP_TBL." WHERE g_id='$id'";
		$MAIN_DB -> query($sql);
		$sql = "DELETE FROM ".MAIN_GRP_USERS_LINK_TBL." WHERE g_id='$id'";
		$MAIN_DB -> query($sql);
		$sql = "DELETE FROM ".MAIN_GRP_BLK_LINK_TBL." WHERE g_id='$id'";
		$MAIN_DB -> query($sql);
		$sql = "DELETE FROM ".MAIN_GRP_PAGES_LINK_TBL." WHERE g_id='$id'";
		$MAIN_DB -> query($sql);
		
		return TRUE;
	}
	
	
	public function update(){
	global $MAIN_DB;

		if ( false===$this -> main_key ){
			sys_error(500,E_EMPTY_ID);
		}
		if ( !empty($this -> last_error ) ){
			 return false; 
		}
		if ( sizeof($this -> set_info)==0 ){
			sys_error(500,E_EMPTY_ARRAY);
		}
		$sql = array();
		foreach( $this -> set_info as $k=>$v ){
			$sql[]="`$k`='".sys_in_sql($v)."'";
		}
		$sql = "
						UPDATE ".MAIN_GROUP_TBL." 
						SET ".implode(',',$sql)." 
						WHERE g_id='".$this -> main_key."'";
		if ( !empty($this -> debug) ){
			die($sql);
		}
		$MAIN_DB -> query($sql);
		
	
		return true;
	}
	
	public function insert(){
	global $MAIN_DB;
			//	при инициализации значений произошла ошибка
		if ( !empty($this -> last_error ) ){
			 return false; 
		}
		if ( sizeof($this -> set_info)==0 ){
			sys_error(500,E_EMPTY_ARRAY);
		}
		
		
		
		
		$sql = array();
		foreach( $this -> set_info as $k=>$v ){
			$sql[]="`$k`='".sys_in_sql($v)."'";
		}
		$sql = "INSERT INTO ".MAIN_GROUP_TBL." SET ".implode(',',$sql)."";
		if ( !empty($this -> debug) ){
			die($sql);
		}
		$MAIN_DB -> query($sql);
		return $MAIN_DB -> insert_id();
	}
	
	
	public function get_error(){
		return $this -> last_error;
	}
	
	private function make_error($msg){
 		$this -> last_error = $msg;	
	}
	
 	
		
		

		
	
		//STATIC

	
	static function load_id($id){
		$m = new groups_q('m');
		if ( !$m -> where_id($id) ){
			return false;
		}
		$m -> get('*');
		if ( !$m -> get_count_rows() ){
			return false;
		}
		return new groups($m -> row());
	}
	
	static function create(){
		return new groups( array('g_id'=>0) );
	}
		
 	
 	
 	
	
 	
 	
 	
  	
  	
	
  	
  	
	
	
}


	
?>
