<?
namespace admin\view;

class BannerManager
{
	public function show()
	{
		global $sys , $db , $lang , $bs;

		$out = \admin\view\PagePanel::show( true , false , false , $lang -> getTrans( 'T_BANERY' ) );

		$lpk = 25;

		if ( $sys -> formGetInt('r') )
			$sys -> setSessionVar( 'pcms_banner_limit' , $sys -> formGetInt('r') );

		$limit 	= $sys -> getSessionVar( 'pcms_banner_limit' );
		$limit 	= $sys -> checkBrowseLimit( $limit );
		$link	= 'index.php';

		$banners_count = \System::getCountBanners();

		$tpl = new \Savant3;

		$ls = ceil( $banners_count / $lpk );
		if ( $bs < 1 )
			$bs = 1;
		else if ( $bs > $ls )
			$bs = $ls;

		$a	= $sys -> getPagingVar( 'a' , $bs , $ls );
		$b	= $sys -> getPagingVar( 'b' , $bs , $ls );
		$tpl -> _a		= $bs - $b;
		$tpl -> _b		= $bs + $a;
		$tpl -> _ls		= $ls;
		$tpl -> _link	= $link;
		$tpl -> _bs	= $bs;

		$from = $lpk * ( $bs - 1 );
		if ( $from < 0 )
			$from = 0;
		$lp = $from+1;

		$banners = \admin\factory\BannerManager::getBanners( $from , $lpk );

		$tpl -> _banners	= $banners;
		$tpl -> _bs			= $bs;
		$tpl -> _limit		= $lpk;
		$tpl -> _total		= count( $banners );
		$tpl -> _start		= $lpk * ( $bs - 1 ) + 1 > 0 ? $lpk * ( $bs - 1 ) + 1 : 0;
		$tpl -> _end		= $lpk * $bs > $banners_count ? $banners_count : $lpk * $bs;

		$out .= $tpl -> fetch( 'templates/banner.php' );

		return $out;
	}

	public function edit( $id = '' )
	{
		global $lang;

		$out = \admin\view\PagePanel::show( false , true , true , $lang -> getTrans( 'T_BANERY' ) );

		$dbedit = new \DataEdit( 'pcms_banner' , 'id' , $id );
		$dbedit -> addPosition( 'title'		, $lang -> getTrans( 'T_TYTUL' )	, 'text' );
		$dbedit -> addPosition( 'link'		, $lang -> getTrans( 'T_LINK' )		, 'text' );
		$dbedit -> addPosition( 'enabled'	, $lang -> getTrans( 'T_AKTYWNY' )	, 'radio' , '' , \System::getComboYesNo() );
		$dbedit -> addPosition( 'image'		, $lang -> getTrans( 'T_ZDJECIE' )	, 'file' );
		$dbedit -> setUploadFolder( 'image' , 'upload/banner/' );
		$out .= $dbedit -> draw();

		return $out;
	}

	public function add()
	{
		global $sys , $lang;

		$out = \admin\view\PagePanel::show( false , true , true , $lang -> getTrans( 'T_BANERY' ) );

		$order = \admin\factory\BannerManager::getMaxOrder();

		$dbedit = new \DataEdit( 'pcms_banner' , 'id' );
		$dbedit -> addPosition( 'title'		, $lang -> getTrans( 'T_TYTUL' )	, 'text' );
		$dbedit -> addPosition( 'link'		, $lang -> getTrans( 'T_LINK' )		, 'text' );
		$dbedit -> addPosition( 'enabled'	, $lang -> getTrans( 'T_AKTYWNY' )	, 'radio' , '' , $sys -> getComboYesNo() );
		$dbedit -> addPosition( 'image'		, $lang -> getTrans( 'T_ZDJECIE' )	, 'file' );
		$dbedit -> setUploadFolder( 'image' , 'upload/banner/' );
		$dbedit -> addPositionHidden( 'o' , $order );
		$out .= $dbedit -> draw();

		return $out;
	}
}
?>