<?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_mail{
	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.mailTmpl.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'] = $_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 index(){
		$t = new PHPTAL();//
		$t -> setSnippet('admin','admin_mail_index');// M_PATH.'/tmpl/control_mail/main.html' );
		$t -> index = true; //  для шаблона
		$p = new mailTmplQuery('m');
		$p -> where_main_rows(); // у админки всегда один язык
		$p -> orderby_id('DESC');
		$p -> get('*, m.m_from_name AS `from`,m_id as lang_links',false);
		$t -> rows = $p -> rows();
		return $t -> execute();
	}
	
	
  /**	
  * AJAX функции
  *
  *
	*/
		
	
	
	/**
	* перезагружаем таблицу со списком шаблонов
	*
	*/
	private function ajax_reload(){
		sys_block_disable();
			$t = new PHPTAL();//
		$t -> setSnippet('admin','admin_mail_index');// M_PATH.'/tmpl/control_mail/main.html' );
		$t -> execute = true;
		$p = new mailTmplQuery('m');
		$p -> where_main_rows(); // у админки всегда один язык
		$p -> orderby_id('DESC');
		$p -> get('*, m.m_from_name AS `from`,m_id as lang_links',false);
		$t -> rows = $p -> rows();
		$GLOBALS['_RESULT']['content'] = $t -> execute();
		exit();
	}
	
			
	private function ajax_delete(){
		sys_block_disable();
		$m = mailTmpl::load_id($_REQUEST['id']);
		if ( false===$m ){
			die( A_EMPTY_ID );
		}
		$m -> delete();
		exit();
	}
		

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

	
		
	/**
	* Форма добавления/редактирования шаблона
	*
	*/
	private function ajax_edit(){
	global $MAIN_PAGE ;
		sys_block_disable();
			//	получаем из базы страницу $_REQUEST['id']
		$p = new mailTmplQuery('f');
		//$p -> set_debug(1);
		if ( false!==$p -> where_link($_GET['id']) ){
		  $p -> where_main_rows();
		  $p -> get('*, lang as lang_list,m_content_type as type_list  ');
		  $row = $p ->  row();
		}else{
		  $row = array( 'lang_list'=>filedsGetMailTmpl::lang_list(),'type_list'=>filedsGetMailTmpl::type_list() );
		}
	
	  
	  $row['site_name'] = $MAIN_PAGE -> config('sys_site_name');
	  $row['site_email'] = $MAIN_PAGE -> config('sys_mail');
  
		
			//	формируем страницу
		$t = new PHPTAL( );//M_PATH.'/tmpl/control_mail/form.html' );
		$t -> setSnippet('admin','admin_mail_edit');
		$t -> lang_edit = false;  // редактируем ключевую запись
    $t -> lang = !empty($_GET['lang']) ? $_GET['lang'] : MAIN_LANG;
		$t -> row = ($row );
		$GLOBALS['_RESULT']['content'] =  $t -> execute();
		$GLOBALS['_RESULT']['title'] = M_ADMIN_M_TMPL;
		exit();
	}
	
			  /**
	  * Редактируем языки
	  *
	  */
	private function ajax_lang_edit(){
		sys_block_disable();
		$t = new PHPTAL( false );
		$t -> setSnippet( 'admin','admin_mail_edit'); //$this -> path_tmpl.'/edit.html' );
		$t -> lang_edit = true;  // редактируем ключевую запись
		$p = new mailTmplQuery('p');
		//$p -> set_debug(1);
		$p -> where_link($_GET['id']);
		$p -> where_lang($_GET['lang']);
		$p -> get('*,m_content_type as type_list');
			
		if ( $p-> get_count_rows()!=0 ){
		  $row = $p ->  row();
			
		}else{
		  $row = array('link_id'=>$_GET['id'],'type_list'=>filedsGetMailTmpl::type_list() );
			
		}
		$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();
	}
	
	
	

	
	


}

?>
