<?php
function throwBanners( $aDisplayTypes = null, $iPositionMin = null, $iPositionMax = null ){
	global $aBannersDisplay;
	if( !isset( $aDisplayTypes ) ){
		$aDisplayTypes = $GLOBALS['aBannersTypes'];
	}
	$oDB = DataBase::getInstance( );
	$oTpl = TplParser::getInstance( );
	if( !isset( $aUrl ) ){
		$aUrl = parse_url( $_SERVER['REQUEST_URI'] );
		if( !isset( $aUrl['host'] ) ) $aUrl['host'] = null;
		if( !isset( $aUrl['path'] ) ) $aUrl['path'] = null;
		if( isset( $aUrl['query'] ) ) $aUrl['query'] = '?' . $aUrl['query'];
		else $aUrl['query'] = null;
	}
	$aReturn = Array( );
	$iStatus = throwStatus( );
	foreach( $aDisplayTypes as $iType => $sName ){
		$iPage = intval( $GLOBALS['iContent'] );
		$iProducer = intval( $GLOBALS['aData']['iProducer'] );
		$aBanners = null;
		do{
			$sql = 'SELECT `banners`.`iBanner`, `banners`.`sFile`, `banners`.`sLink`, `banners`.`iType`, `banners`.`iWidth`, `banners`.`iHeight`, `banners`.`sColor`, `banners`.`iMax`, `banners`.`iStatus`, `banners`.`sText`, `banners`.`iTargetBlank`
							FROM `' . $oDB->throwTableName( DB_BANNERS ) . '` AS `banners`
							LEFT JOIN `' . $oDB->throwTableName( DB_BANNERS_STATS ) . '` AS `banners_stats` ON (`banners`.`iBanner` = `banners_stats`.`iBanner` )';
			if( isset( $iProducer ) && $iProducer > 0 ){
				$sql .= ' LEFT JOIN `' . $oDB->throwTableName( DB_BANNERS_PRODUCERS ) . '` AS `banners_producers` ON( `banners`.`iBanner` = `banners_producers`.`iBanner` )';
			} else {
				$sql .= ' LEFT JOIN `' . $oDB->throwTableName( DB_BANNERS_PAGES ) . '` AS `banners_pages` ON( `banners`.`iBanner` = `banners_pages`.`iBanner` )';
			}
			$sql .= '		WHERE
								( `iStatus` >= ' . intval( $iStatus ) . ' )
								AND `iType` = ' . $iType . '
								AND ( ( `banners`.`iMax` = \'0\' ) OR ( `banners_stats`.`iViews` < `banners`.`iMax` ) )
								AND ( ( `banners`.`iMaxClicks` = \'0\' ) OR ( `banners_stats`.`iClicks` < `banners`.`iMaxClicks` ) )
								AND ( ( `banners`.`iTimeFrom` IS NULL ) OR ( `banners`.`iTimeFrom` = \'0\' ) OR ( `banners`.`iTimeFrom` < UNIX_TIMESTAMP() ) )
								AND ( ( `banners`.`iTimeTo` IS NULL ) OR ( `banners`.`iTimeTo` = \'0\' ) OR ( `banners`.`iTimeTo` > UNIX_TIMESTAMP() ) )';
			if( isset( $iProducer ) && $iProducer > 0 ){
				$sql.= ' AND ( `banners_producers`.`iProducer` = ' . intval( $iProducer ) . ' )';
			} elseif( $iPage > 0 ){
				$sql.= ' AND ( `banners_pages`.`iPage` = ' . intval( $iPage ) . ' )
						 AND NOT EXISTS ( SELECT 1 FROM ' . $oDB->throwTableName( DB_BANNERS_PRODUCERS ) . ' WHERE `iBanner` = `banners`.`iBanner` )';
			} else{
				$sql.= ' AND ( `banners_pages`.`iPage` IS NULL )
						 AND NOT EXISTS ( SELECT 1 FROM ' . $oDB->throwTableName( DB_BANNERS_PRODUCERS ) . ' WHERE `iBanner` = `banners`.`iBanner` )';
			}
			
			if( isset( $iPositionMin ) ){
				$sql .= ' AND ( `banners`.`iPosition` >= ' . intval( $iPositionMin ) . ' )';
			}
			if( isset( $iPositionMax ) ){
				$sql .= ' AND ( `banners`.`iPosition` <= ' . intval( $iPositionMax ) . ' )';
			}
			$oTpl->assign( 'iType', $iType );
			if( $aBannersDisplay[$iType] ){
				$sql.= ' ORDER BY `iPosition`, `iBanner`';
			} else{
				//losowy
				$sql.= ' ORDER BY RAND() LIMIT 1';
			}
			$aBanners = $oDB->executeSQL( $sql, true );
			if( isset( $iProducer ) ){
				unset( $iProducer );
			} elseif( $iPage > 0 ){
				$iPage = intval( $GLOBALS['oPage']->aPages[$iPage]['iPageParent'] );
			} else{
				break;
			}
		} while( empty( $aBanners ) );
		$aBannersIds = Array( );
		foreach( $aBanners as &$aData ){
			$aBannersIds[] = $aData['iBanner'];
			$aData['sExt'] = $oDB->throwExtOfFile( $aData['sFile'] );
			$aData['sBannerLink'] = $aData['iBanner'] . ',banners.html';
			$aData['sBannerLinkEncoded'] = urlencode( $aData['sBannerLink'] );
		}
		changeBannersStats( $aBannersIds, 'iViews' );
		$oTpl->assign( 'aBanners', $aBanners );
		$aReturn[$iType] = $oTpl->parse( 'banners.tpl' );
	}
	return $aReturn;
}
function throwBanner( $iBanner ){
	$oDB = DataBase::getInstance( );
	$aBanner = $oDB->throwDataFromFiles( Array( DB_BANNERS, DB_BANNERS_STATS ), $iBanner, 'iBanner' );
	if( $aBanner['iTimeFrom'] > 0 ) $aBanner['sTimeFrom'] = date( 'Y-m-d H:i:s', $aBanner['iTimeFrom'] );
	if( $aBanner['iTimeTo'] > 0 ) $aBanner['sTimeTo'] = date( 'Y-m-d H:i:s', $aBanner['iTimeTo'] );
	return $aBanner;
}
function goToBannerLink( $iBanner ){
	$oDB = DataBase::getInstance( );
	if( is_numeric( $iBanner ) ){
		$sql = 'SELECT `sLink` FROM `' . $oDB->throwTableName( DB_BANNERS ) . '` WHERE `iBanner` = \'' . $iBanner . '\';';
		$sLink = $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
		if( !empty( $sLink ) ){
			changeBannersStats( Array( $iBanner ), 'iClicks' );
			header( 'Location: ' . $sLink );
			exit;
		}
	}
}
function changeBannersStats( $aId, $sIndex ){
	$oDB = DataBase::getInstance( );
	foreach( $aId as $iKey => $iBanner ){
		if( is_numeric( $iBanner ) ) $aId[$iKey] = '( `iBanner` = \'' . $iBanner . '\' )';
		else unset( $aId[$iKey] );
	}
	if( !empty( $aId ) ){
		$sql = 'UPDATE `' . $oDB->throwTableName( DB_BANNERS_STATS ) . '` SET `' . $sIndex . '` = ( `' . $sIndex . '`+1 ) WHERE ' . implode( ' OR ', $aId ) . ';';
		$oDB->executeSQL( $sql );
	}
}
?>