<?
namespace admin\view;

class Banners
{
	public static function showBanners()
	{
		global $bs;

		$out = \admin\view\PagePanel::show( 'Dodaj nowy', false, false, 'Banery' );

		$lpk = 25;

		if ( \System::formGetInt('r') )
			\System::setSessionVar( 'pp_banner_limit' , \System::formGetInt('r') );

		$limit 	= \System::getSessionVar( 'pp_banner_limit' );
		$limit 	= \System::checkBrowseLimit( $limit );
		$link   = './';

		$banners_count = \admin\factory\Banners::getCountBanners();

		$tpl = new \Savant3;

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

		$a	= \System::getPagingVar( 'a', $bs, $ls );
		$b	= \System::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\Banners::getBanners( $from, $lpk );

		$tpl -> _banners	= $banners;
		$tpl -> _bs       = $bs;
		$tpl -> _limit		= $lpk;
		$tpl -> _total		= $banners_count;
		$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( 'other/banner' );

		return $out;
	}

	public static function editBanner( $id = '' )
	{
    $id ? $value = 'Zapisz' : $value = 'Dodaj';		
    $order = \admin\factory\Banners::getMaxOrder();
    
		$out = \admin\view\PagePanel::show( false, $value, true, 'Banery' );

		$dbedit = new \DataEdit( 'pp_banner' , 'id' , $id );
		$dbedit -> addPosition( 'title',    'Tytuł',    'text' );
		$dbedit -> addPosition( 'link',     'Link',     'text' );
		$dbedit -> addPosition( 'enabled',  'Aktywny',  'radio' , '' , \System::getComboYesNo() );
		$dbedit -> addPosition( 'image',      'Obraz',    'file' );
		$dbedit -> setUploadFolder( 'image' , 'upload/banner/' );
    if ( !$id )
      $dbedit -> addPositionHidden( 'o' , $order );
		$out .= $dbedit -> draw();

		return $out;
	}
}
?>