<?php
/*
* Документы на сайте
*	
*/

class admin_menu{
	private $error = false;
	private $act = 'main';
	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_SOURCE_PATH.'/modules/admin/lang/'.MAIN_USER_LANG.'/lang.php');
		include_once(MAIN_SOURCE_PATH.'/inc/class.html.php');
		include_once(M_PATH.'/inc/class.menu.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);
		}
		return $this -> $func();
	}
	
	
	private function main(){
//	global $MAIN_PAGE;
	  //$MAIN_PAGE -> set_tmpl_id(3);
	//  $MAIN_PAGE -> set_tmpl('tmpl_admin.html');
	  $t = new PHPTAL( M_PATH.'/tmpl/menu/main.html' );
		$p = new menu_q('a');
		$p -> where_lang(MAIN_LANG);
		$p -> orderby_order('ASC');
		$p -> get('*, 1 as `down` , 1 as `up`',false);
			//$row['up'] = $row['down'] = true;
		$rows =  $p -> rows();
		$rows[0]['up'] = false; 
	  $rows[ (sizeof($rows)-1) ]['down'] = false;
		$t -> rows =$rows;
		return $t -> execute();
	}
	
	
			//	редактируем id
	private function ajax_edit(){
	global $MAIN_DB,$MAIN_USER, $MAIN_PAGE  ;
		sys_block_disable();
			//	получаем id  из базы
		$b = new menu_q('n');
		if ( false === $b -> where_key($_REQUEST['key']) ){
		  $row = array(
		    'm_key' => false,
		    'm_path' => false
		    );
		}else{
		  $b -> where_lang($_GET['lang']);
		  //$b -> set_debug(1);
		  $b -> get('*');
		  $row = $b ->  row();
		}
		
	 	  //  языки
	/*	$l = new lang_select_box(); 
	  $row['lang_list']  = $l -> get($_GET['lang']);*/
	  
	  
	 
	  
			//	выводим
		$t = new PHPTAL( M_PATH.'/tmpl/menu/edit.html' );
		$t -> row = $row;
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = EDIT;
		exit();
	}
	
		//	сохраняем форму 
	private function ajax_save(){
	global $MAIN_USER ;
		sys_block_disable();
			//	загружаем объект
			
		$p = menu::load_id($_REQUEST['id']);
		if ( false===$p ){
			$p= menu::create();
			define('create_menu',1);
		  $p -> set('m_key', sys_str_rand(10) );
		}
	  $p -> set('m_name', $_REQUEST['m_name'] );
    $p -> set('m_lang','ru');
    $p -> set('m_src',$_REQUEST['m_src']);
	  //$p -> set_debug(1);
		if ( create_menu==1 ){
			$id = $p -> insert();	
		}else{
			$id = $p -> update();
		}
		if ( false===$id ){
			echo $p -> get_error();
			exit();
		}
		
		if ( true===create_menu ){
		  //  мультиязычность
		  $p = menu::load_id($id);
		  $GLOBALS['_RESULT']['key'] = $p -> key();
		  $GLOBALS['_RESULT']['lang'] = MAIN_LANG;
		}
		$GLOBALS['_RESULT']['msg'] = MOD_SAVE;
		exit();
	}
	
	private function ajax_reload(){
		sys_block_disable();
	  $t = new PHPTAL( M_PATH.'/tmpl/menu/inc/row.html' );
		$t -> execute = true;
		$p = new menu_q('a');
		$p -> where_lang(MAIN_LANG);
		$p -> orderby_order('ASC');
		$p -> get('*, 1 as `down` , 1 as `up`',false);
		$rows = $p -> rows();
		$rows[0]['up'] = false; 
	  $rows[ (sizeof($rows)-1) ]['down'] = false;
		$t -> rows = 	$rows;
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}
	
	
	private function ajax_delete(){
		sys_block_disable();
		$m = new menu_q('n');
		$m -> where_key($_REQUEST['key']);
	//$m -> set_debug(1);
		$m -> get('m_id');
	  while( list($id) = $m -> row() ){
		  $sp = menu::load_id($id);
		  if ( false===$sp ){
			  die( A_EMPTY_ID );
		  }
		  $sp -> delete();
		}
		exit();
	}
	
	private function ajax_up(){
	global $MAIN_DB;
		sys_block_disable();
		$m = new menu_q('n');
		$m -> where_key($_REQUEST['key']);
	//$m -> set_debug(1);
		$m -> get('m_order');
		list($order) = $m -> row();
		
		$m = new menu_q('n');
		$m -> where_order_less($order);
		$m -> set_page(1);
		$m -> set_count_rows(1);
		$m -> orderby_order('DESC');
    //$m -> set_debug(1);
		$m -> get('m_key,m_order');
		
		list($r_key,$r_order) = $m -> row();
		
		$sql = "
		  UPDATE ".$MAIN_DB -> prefix("system_menu")." 
		  SET m_order = '".$r_order."'
		  WHERE m_key = '".$_REQUEST['key']."'";
		$MAIN_DB -> query($sql);
		
		$sql = "
		  UPDATE ".$MAIN_DB -> prefix("system_menu")." 
		  SET m_order = '".$order."'
		  WHERE m_key = '".$r_key."'";
		$MAIN_DB -> query($sql);

		exit();
	}
	
		private function ajax_down(){
			global $MAIN_DB;
			sys_block_disable();
		$m = new menu_q('n');
		$m -> where_key($_REQUEST['key']);
	//$m -> set_debug(1);
		$m -> get('m_order');
		list($order) = $m -> row();
		
		$m = new menu_q('n');
		$m -> where_order_more($order);
		$m -> set_page(1);
		$m -> set_count_rows(1);
		$m -> orderby_order('ASC');
    //$m -> set_debug(1);
		$m -> get('m_key,m_order');
		
		list($r_key,$r_order) = $m -> row();
		
		$sql = "
		  UPDATE ".$MAIN_DB -> prefix("system_menu")." 
		  SET m_order = '".$r_order."'
		  WHERE m_key = '".$_REQUEST['key']."'";
		$MAIN_DB -> query($sql);
		
		$sql = "
		  UPDATE ".$MAIN_DB -> prefix("system_menu")." 
		  SET m_order = '".$order."'
		  WHERE m_key = '".$r_key."'";
		$MAIN_DB -> query($sql);
		exit();
	}
}

?>
