<?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 admin_stat_pages{
	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.staticPages.php');
		//include_once(M_PATH.'/inc/class.modules.php');
		include_once(MAIN_SOURCE_PATH.'/modules/admin/lang/'.MAIN_LANG.'/lang.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;
		
		//preg_match('#^'.$MAIN_PAGE -> dirname().'_(.*)$#iUs',$_REQUEST['p_name'],$res);
		//$this -> path =$res[1];
		//$this -> path_tmpl = M_PATH.'/tmpl/'.$this -> path;
	  $this -> path_tmpl = M_PATH.'/tmpl/control_stat_pages';
	}
	
	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( $this -> path_tmpl.'/main.html' );
		
		$p = new staticPagesQuery('f');
		//$p -> set_debug(1);
		$p -> include_tmpl('t','t.t_name as tmpl_name');
		$p -> where_main_rows(); // у админки всегда один язык
		$p -> orderby_id('DESC');
		$p -> get('f.*,p_id as lang_links',false);
		
		$t -> rows = $p -> rows();
		
			//	редактор
		$e = html_control::editor('CKEditor');
	  $e -> load();
		
		$e = html_control::editor('codemirror');
	  $e -> load();
		/*$e = html_control::editor(MAIN_EDITOR);
		$e -> set_name('p_content');
		$e -> set_value('');
		$e -> set_attr('cols',10);
		$t -> editor = $e -> show();*/
			
		return $t -> execute();
	}
	
	
	private function ajax_reload(){

		sys_block_disable();
		$t = new PHPTAL( $this -> path_tmpl.'/inc/tmpl_row.html' );
		$t -> execute = true;
		
		$p = new staticPagesQuery('f');
		$p -> where_main_rows();
		//$p -> where_lang(MAIN_LANG); // у админки всегда один язык
		$p -> include_tmpl('t','t.t_name as tmpl_name');
		$p -> orderby_id('DESC');
		$p -> get('f.*,p_id as lang_links',false);
		$t -> rows = $p -> rows();
		
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}


	private function ajax_delete(){
		sys_block_disable();
		$p = new staticPagesQuery('a');
		$p -> where_link( $_POST['id'] );
		$p -> get('p_id');
		while( list($id) = $p -> row() )
		{
      $m = staticPages::load_id($id);
      $m -> delete();
      
    }
		staticPages::groups($_POST['id'],false);
		exit();
	}
	
		
	private function ajax_edit(){
		sys_block_disable();
		$t = new PHPTAL( $this -> path_tmpl.'/edit.html' );
		$t -> lang_edit = false;  // редактируем ключевую запись
		$p = new staticPagesQuery('p');
		if ( !$p -> where_link($_GET['id']) ){
			$row = array( 'lang_list'=>filedsGetStaticPages::lang_list(),'groups'=>filedsGetStaticPages::groups(), 'tmpl_list'=>filedsGetStaticPages::tmpl_list() );
			
		}else{
		  //$p -> where_lang($_GET['lang']);
			$p -> where_main_rows();
			$p -> get('*,
			  p_type_content AS type_content_tmpl,
			  p_type_content AS type_content_php,
			  p_type_content AS type_content_html,
			  p_id as groups,
			  t_id as tmpl_list,
			  lang as lang_list
			');
			
			$row = $p ->  row();
		}
	  
		$t -> row = $row ;
		$t -> lang = !empty($_GET['lang']) ? $_GET['lang'] : MAIN_LANG;
		 $GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = M_ADMIN_SP_ADD_EDIT_PAGE;
		exit();
	}
	
	
	
	private function ajax_save(){
		sys_block_disable();
		
		$p = staticPages::load_id($_POST['id']);
		if ( false===$p ){
		  $create = 1;
			$p =  staticPages::create();
		}
		//$p -> set_debug(1);
		try
		{
	    foreach( $_POST['form'] as $k=>$v )
	    {
		    $p -> set($k,$v);	
		  }
		  $p -> set('p_content',$_POST['p_content']);
		  $id = ( $create==1 ) ? $p -> insert() : $p -> update();
		  if ( $create==1 && empty($_POST['form']['link_id']) ){
		    $p -> set('link_id',$id);
        $p -> update();
		  }
		  
		}
		catch( Exception $e )
		{
		  echo $e -> getMessage();
		  exit();
		} 


   // $langs = explode(',','MAIN_LANGS');
			//	обновляем права доступа к страничке
		staticPages::groups($id,$_POST['access']);
		
			//	обновляем морду
		$GLOBALS['_RESULT']['msg'] = M_ADMIN_SP_SAVE;
		exit();
	}
	
	  /**
	  * Редактируем языки
	  *
	  */
	private function ajax_lang_edit(){
		sys_block_disable();
		$t = new PHPTAL( $this -> path_tmpl.'/edit.html' );
		$t -> lang_edit = true;  // редактируем ключевую запись
		$p = new staticPagesQuery('p');
		//$p -> set_debug(1);
		$p -> where_link($_GET['id']);
		$p -> where_lang($_GET['lang']);
		$p -> get('*,
			  p_type_content AS type_content_tmpl,
			  p_type_content AS type_content_php,
			  p_type_content AS type_content_html,
			  p_id as groups,
			  t_id as tmpl_list,
			  lang as lang_list
			');
			
		if ( $p-> get_count_rows()!=0 ){
		  $row = $p ->  row();
			
		}else{
		  $row = array( 'groups'=>filedsGetStaticPages::groups(), 'tmpl_list'=>filedsGetStaticPages::tmpl_list() ,'link_id'=>$_GET['id'],);
			
		}
		$t -> row = $row;
		$t -> lang = !empty($_GET['lang']) ? $_GET['lang'] : MAIN_LANG;
		
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = M_ADMIN_SP_ADD_EDIT_PAGE;
		exit();
	}
	
	
	
	
	
	


}

?>
