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

	 function __construct(){

	}
	
	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.autocatalog.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_posts/main.html' );
		$p =  autoCatalog::query('f');
		//$p -> set_debug(1);
		$p -> orderby_sort('ASC');
		$p -> get('*,category_id categoryName',false);
		$t -> rows = $p -> rows();

		$e = html_control::editor('CKEditor');
	  $e -> load();
	  upload_img::include_files();

		return $t -> execute();
	}
	
	
	static function reload(){
		sys_block_disable();
		$t = new PHPTAL( self::$m_path.'/tmpl/control_posts/inc/tmpl_row.html' );
		$t -> execute = true;
		$p = autoCatalog::query('f');
		$p -> orderby_sort('ASC');
	  $p -> get('*,category_id categoryName',false);
	  $t -> rows = $p -> rows();
		HL::ajaxContent( $t -> execute() );
		exit();
	}
	

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

		sys_block_disable();
		$p = autoCatalog::query('p');
		//$p -> set_debug(1);		
		if ( false===$p -> where_id($_POST['id']) ){
			$row = array( 'upl_img'=>filedsGetAutoCatalog::upl_img(),'upl_img2'=>filedsGetAutoCatalog::upl_img2(),'categorySelect'=>filedsGetAutoCatalog::categorySelect() );
		}else{
			$p -> get('*,preview_image as upl_img, images as upl_img2, category_id as categorySelect');
			$row = $p ->  row();
		}
	
		$t = new PHPTAL( self::$m_path.'/tmpl/control_posts/form.html' );
		$t -> row = $row ;
		
		HL::ajaxWindow( 'Добавить/Редактировать',$t -> execute() );
		exit();
	
	}
	


	static function save(){
		sys_block_disable();

		$p = autoCatalog::searchById($_POST['id']);
		if ( false===$p ){
		  $create = true;
			$p =  autoCatalog::create();
		}
	//$p -> set_debug(1);
		try{
		  foreach( $_POST['form'] as $k=>$v ){
		    $p -> set($k,$v);
		  } 
		  $p -> set('desc',$_POST['desc']);
		  $id = ( $create===true ) ? $p -> insert(1) : $p -> update();
		}catch( Exception $e ){
			HL::ajaxError( $e -> getMessage() );
		  exit();
		}
		self::reload();
		exit();
	}
	
	
	private function up(){
		sys_block_disable();
		try{
			$f = review::searchById($_POST['id']);
			$down_sort = $f -> sort;

			$m = review::query('c');
			//$m -> set_debug(1);
			$m -> where_more('sort',$down_sort );
			$m -> set_limit(0,1);
			$m -> orderby_sort('ASC');
			$m -> get('review_id');
			list($up_id) = $m -> row();

			$up = review::searchById($up_id);
			if ( false===$up ){
				throw new Exception('Выше некуда!');
			}
			$f -> set('sort',$up -> sort );
			$f -> update();
			$up -> set('sort',$down_sort);
			$up -> update();

		}catch( Exception $e ){
			HL::ajaxError( $e -> getMessage() );			
		}

    self::reload();
		exit();
	}
	
	private function down(){
		sys_block_disable();
		try{
			$f = review::searchById($_POST['id']);
			$up_sort = $f -> sort;

			$m = review::query('c');
			//$m -> set_debug(1);
			$m -> where_less('sort',$up_sort );
			$m -> set_limit(0,1);
			$m -> orderby_sort('DESC');
			$m -> get('review_id');
			list($down_id) = $m -> row();

			$down = review::searchById($down_id);
			if ( false===$down ){
				throw new Exception('Ниже некуда!');
			}
			$f -> set('sort',$down -> sort );
			$f -> update();
			$down -> set('sort',$up_sort);
			$down -> update();


		}catch( Exception $e ){
			HL::ajaxError( $e -> getMessage() );			
		}

    self::reload();
		exit();
	}
	
	
	
	


}

