<?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 menu_control_menu{
	static $error = false;
	static $act = 'index';
	//private $JsHttpRequest = false;
	static $table = 'menu';
	static $tmpl_path = false;
	static $m_path = false;
	static $m_url = false;

	 function __construct(){
		/*
			define('A_CHARSET', $MAIN_PAGE -> charset() );
		include(MAIN_SOURCE_PATH.'/ext/ajax/JsHttpRequest.php');
		$this -> JsHttpRequest = new JsHttpRequest( A_CHARSET );
		
		$GLOBALS['_RESULT']['id_request'] = !empty($_REQUEST['id_request']) ? $_REQUEST['id_request'] : 0;
		
		preg_match('#^'.$MAIN_PAGE -> dirname().'_(.*)$#iUs',$_REQUEST['p_name'],$res);
		$this -> path =$res[1];
		$this -> path_tmpl = M_PATH.'/tmpl/'.$this -> path;*/
	}
	
	static function get(){
		global $MAIN_PAGE;
		//self::$tmpl_path = MAIN_PATH.'/tmp/view/project/controls/'.$MAIN_PAGE -> dirname();
		self::$m_path = MAIN_SOURCE_PATH.'/modules/'.$MAIN_PAGE -> dirname();
		self::$m_url = MAIN_SOURCE_URL.'/modules/'.$MAIN_PAGE -> dirname();
	

	
		include_once(self::$m_path.'/inc/url.php');
		include_once(self::$m_path.'/inc/class.menu.php');

		include_once(MAIN_SOURCE_PATH.'/inc/class.html.php');
		
		self::$act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : self::$act;
		

		if ( false===is_callable('self::'.self::$act, false)){
			sys_error(ERROR_404);
		}
		$func = self::$act;
		return self::$func();
	}
	
	

	
	/**
	* Формируем список различных меню системы
	*
	*/
	static function index(){
		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/main.html' );
		$p =  menu::query('f');
		//$p -> set_debug(1);
		$p -> where_level(1);
		$p -> order_left('ASC');
		$p -> get('c_id,c_name,c_var,c_id as url_item',false);
		$t -> rows = $p -> rows();
		return $t -> execute();
	}
	
	
	static function reload(){
		sys_block_disable();
		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/inc/tmpl_row.html' );
		$t -> execute = true;
		$p = menu::query('f');
		$p -> where_level(1);
	  $p -> order_left('ASC');
	  $p -> get('c_id,c_name,c_var,c_id as url_item',false);
	  $t -> rows = $p -> rows();
		HL::ajaxContent( $t -> execute() );
		exit();
	}
	

	static function delete(){
		sys_block_disable();
		try{
			$m = menu::load_id($_POST['id']);
			if ( false===$m ){
				throw new Exception('Ошибка: запис "'.$_POST['id'].'" не найдена!');
			}
			$m -> deleteAll();
		}catch( Exception $e){
			HL::ajaxError( $e -> getMessage() );
		}
		self::reload();
		exit();
	}
	
	static function edit(){

		sys_block_disable();
		$p = menu::query('p');
		//$p -> set_debug(1);		
		if ( false===$p -> where_id($_POST['id']) ){
			$row = array( );
		}else{
			$p -> get('
			  c_id,c_name,url,c_var,tmpl
			');
			$row = $p ->  row();
		}
	
		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/edit_menu.html' );
		$t -> row = $row ;
		
		HL::ajaxWindow( 'Добавить/Редактировать',$t -> execute() );
		exit();
	
	}
	


	static function save(){
		sys_block_disable();

		$p = menu::load_id($_POST['id']);
		if ( false===$p ){
		  $create = true;
			$p =  menu::create();
		}
		//$p -> set_debug(1);
		try{
		  foreach( $_POST['form'] as $k=>$v ){
		    $p -> set($k,$v);
		  } 

		  $id = ( $create===true ) ? $p -> insert(1) : $p -> update();
		}catch( Exception $e ){
			HL::ajaxError( $e -> getMessage() );
		  exit();
		}
		self::reload();
		exit();
	}
	
	
	////////////  Итемы меню  //////////////////
		
  /**
	* Формируем выбранное меню
	*
	*/
	static function items(){
		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/items.html' );
		$p = menu::query('f');
		//$p -> set_debug(1);
		$p -> where_id($_GET['menu_id']);
		$p -> get('c_id,c_name');
		list($t -> c_id,$t -> c_name) = $p -> row();
		
		  //  список подпунктов меню 
		$p = menu::query('f');
		//$p -> set_debug(1);
		$p -> where_more('c_level',1);
		$p -> where_down($_GET['menu_id']);
		$p -> where_main_rows();
		$p -> order_left('ASC');
		$p -> get('c_id, url, 1 as `down` , 1 as `up`,c_name as name, c_id as lang_links,c_level',false);
		$t -> rows = $p -> rows();
		
		
		$t -> menu_id = $_GET['menu_id'];
		return $t -> execute();
	}
	
		
	static function edit_item(){
	
		sys_block_disable();
		$p = menu::query('p');
		//$p -> set_debug(1);		
		if ( !$p -> where_id($_POST['id']) ){
			$row = array( 
			  'lang_list'=>filedsGetMenu::lang_list(),
			  'menu_select'=>filedsGetMenu::menu_select(),
			);
		}else{
			$p -> get('
			  c_id,c_name,url,lang,link_id,area,c_level,
			  c_id as menu_select,  
			  c_id as lang_list,
			  c_name as name

			');
			$row = $p ->  row();
		}
		
	
		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/edit_item.html' );
		$t -> row = $row ;
	  $t -> lang_edit = false;  // редактируем ключевую запись
		$t -> link_id =$_POST['id'];

		HL::ajaxWindow( 'Добавить/Редактировать',$t -> execute() );
		exit();
	}
	
	static function reload_item(){
		sys_block_disable();

		$t = new PHPTAL( self::$m_path.'/tmpl/control_menu/inc/tmpl_row_items.html' );
		$t -> execute = true;
		$p = new menuQuery('f');
	  $p -> order_left('ASC');
		$p -> where_more('c_level',1);
	  $p -> where_down($_GET['menu_id']);
		$p -> where_main_rows();
		$p -> get('c_id, url, 1 as `down` , 1 as `up`, c_name as name, c_id as lang_links,c_level',false);
		
	  $t -> rows = $p -> rows();
	
		HL::ajaxContent( $t -> execute() );
		exit();
	}

	static function save_item(){
		sys_block_disable();
		
		
		$p = menu::load_id($_POST['id']);
		if ( false===$p ){
		  $create = true;
			$p =  menu::create();
		}
		//$p -> set_debug(1);
		try{
		  foreach( $_POST['form'] as $k=>$v )
		  {
		    $p -> set($k,$v);
		  } 
		 
		  $id = ( $create===true ) ? $p -> insert($_POST['form']['c_parent']) : $p -> update($_POST['form']['c_parent']);
		  
		  if ( $create===true && empty($_POST['form']['link_id']) ){
		    //$p -> set_debug(1);
		    $p -> set('link_id',$id);
        $p -> update();
		  }
		}catch( Exception $e ){
		  echo $e -> getMessage();
		  exit();
		}

		self::reload_item();
		exit();
	}
	
	static function delete_item(){
		sys_block_disable();
		try{
			$m = menu::load_id($_POST['id']);
			if ( false===$m ){
				throw new Exception('Ошибка: запис "'.$_POST['id'].'" не найдена!');
			}
			$m -> deleteAll();
		}catch( Exception $e){
			HL::ajaxError( $e -> getMessage() );
		}
		self::reload_item();
		exit();
	}

	
	
		
		
	static function ajax_lang_edit(){
		/*sys_block_disable();
		$t = new PHPTAL( $this -> path_tmpl.'/edit_item.html' );
		$t -> lang_edit = true;  // редактируем ключевую запись
		$p = new menuQuery('p');
		//$p -> set_debug(1);
		$p -> where_link($_GET['id']);
		$p -> where_lang($_GET['lang']);
		$p -> get('c_id,c_name,url,lang,link_id,area');
			
		if ( $p-> get_count_rows()!=0 ){
		  $row = $p ->  row();
		}else{
		  $row = array('link_id'=>$_GET['id'],);
			
		}
		$t -> row = $row;
		$t -> lang = !empty($_GET['lang']) ? $_GET['lang'] : MAIN_LANG;
		$t -> link_id = $_GET['id'];
		$t -> menu_id = menu::parent($_GET['id']);
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = M_ADMIN_SP_ADD_EDIT_PAGE;
		exit();*/
	}
	
	
	static function up(){
		global $MAIN_DB ;
		sys_block_disable();
		  //  получим данные id на который кликнули
		list($c_left,$c_right,$c_level) = menu::getServiceInfo($_POST['id']);
		  //  получим данные id который находится выше
		$m =  menu::query('n');
	//	$m -> set_debug(1);
		$m -> where_less('c_left',$c_left);
		$m -> where_less('c_right',$c_right);
		$m -> where_level($c_level);
		$m -> set_page(1);
		$m -> set_count_rows(1);
		$m -> order_left('DESC');
		$m -> get('c_id');
		list($c_id) = $m -> row();
		
		  //  поменяем местами
    menu::order($c_id,$_POST['id']);
 		self::reload_item();
		exit();
	}
	
	static function down(){

		list($c_left,$c_right,$c_level) = menu::getServiceInfo($_POST['id']);
		  //  получим данные id который находится ниже
		$m =  menu::query('n');
		//$m -> set_debug(1);
		$m -> where_more('c_left',$c_left);
		$m -> where_more('c_right',$c_right);
		$m -> where_level($c_level);
		$m -> set_page(1);
		$m -> set_count_rows(1);
		$m -> order_left('ASC');
		$m -> get('c_id');
		list($c_id) = $m -> row();
		
		  //  поменяем местами
		menu::order($_POST['id'],$c_id);
  	self::reload_item();
		exit();
	}
	
	
	
	
	


}

