<?php
function throwBannersStats( $iBanner = null ){
	$oDB = DataBase::getInstance( );
	if( isset( $iBanner ) && is_numeric( $iBanner ) ){
		$sql = 'SELECT `iBanner`, `iViews`, `iClicks` FROM `' . $oDB->throwTableName( DB_BANNERS_STATS ) . '` WHERE `iBanner` = \'' . $iBanner . '\';';
		return $oDB->executeSQL( $sql, true, DataBase::SINGLE_RECORD );
	} else{
		$sql = 'SELECT `iBanner`, `iViews`, `iClicks` FROM `' . $oDB->throwTableName( DB_BANNERS_STATS ) . '`;';
		$aStats = Array( );
		return $oDB->executeSQL( $sql, true, DataBase::ASSOCIATIVE, 'iBanner' );
	}
}
function deleteBanner( $iBanner ){
	global $oFile;
	$aData = throwBanner( $iBanner );
	if( isset( $aData ) && is_array( $aData ) ){
		$oDB = DataBase::getInstance( );
		$oFile->deleteFiles( Array( $iBanner ), 51, 'iBanner', 'sFile', false );
		$oDB->deleteInFile( DB_BANNERS, $iBanner, 'iBanner' );
		$oDB->deleteInFile( DB_BANNERS_STATS, $iBanner, 'iBanner' );
	}
}
function listBanners( ){
	$oDB = DataBase::getInstance( );
	$aBanners = $oDB->throwFileArray( DB_BANNERS );
	if( isset( $aBanners ) ){
		$content = null;
		$aStats = throwBannersStats( );
		$aTypes = $GLOBALS['aBannersTypes'];
		foreach( $aBanners as &$aBanner ){
			$aBanner['sExt'] = $oDB->throwExtOfFile( $aBanner['sFile'] );
			if( !isset( $aStats[$aBanner['iBanner']] ) ){
				$aBanner['iClicks'] = null;
				$aBanner['iViews'] = null;
			} else{
				$aBanner['iClicks'] = $aStats[$aBanner['iBanner']]['iClicks'];
				$aBanner['iViews'] = $aStats[$aBanner['iBanner']]['iViews'];
			}
			$aBanner['sType'] = isset( $aTypes[$aBanner['iType']] ) ? $aTypes[$aBanner['iType']] : null;
		}
		return $aBanners;
	}
}
function saveBanner( $aForm ){
	$oDB = DataBase::getInstance( );
	if( !isset( $aForm['iStatus'] ) ) $aForm['iStatus'] = 0;
	if( !isset( $aForm['iTargetBlank'] ) ) $aForm['iTargetBlank'] = 0;
	$aForm['iTimeFrom'] = dateToTime( $aForm['sTimeFrom'], 'ymd' );
	$aForm['iTimeTo'] = dateToTime( $aForm['sTimeTo'], 'ymd' );
	if( isset( $_FILES['sFile']['name'] ) && !empty( $_FILES['sFile']['name'] ) ){
		$aForm['sFile'] = $oDB->uploadFile( $_FILES['sFile'], DIR_FILES );
	}
	if( isset( $aForm['iBanner'] ) && is_numeric( $aForm['iBanner'] ) ){
		$sParam = 'iBanner';
	} else{
		$sParam = null;
	}
	if( empty( $aForm['iPosition'] ) ) $aForm['iPosition'] = 0;
	$aForm['iBanner'] = $oDB->save( DB_BANNERS, $aForm, $sParam );
	$oDB->save( DB_BANNERS_STATS, $aForm, $sParam );
	$oDB->deleteInFile( DB_BANNERS_PAGES, $aForm['iBanner'], 'iBanner' );
	foreach( $aForm['aPages'] as $iPage ) if( is_numeric( $iPage ) && !empty( $iPage ) ){
		$oDB->save( DB_BANNERS_PAGES, Array( 'iBanner' => $aForm['iBanner'], 'iPage' => $iPage ) );
	}
	
	$oDB->deleteInFile( DB_BANNERS_PRODUCERS, $aForm['iBanner'], 'iBanner' );
	foreach( $aForm['aProducers'] as $iProducer ) if( is_numeric( $iProducer ) && !empty( $iProducer ) ){
		$oDB->save( DB_BANNERS_PRODUCERS, Array( 'iBanner' => $aForm['iBanner'], 'iProducer' => $iProducer ) );
	}
	
	return $aForm['iBanner'];
}
function saveBanners( $aForm ){
	$oDB = DataBase::getInstance( );
	foreach( $aForm['aStatuses'] as $iBanner => $iStatus ) {
		$oDB->update( DB_BANNERS, Array( 'iStatus' => $iStatus ), Array( 'iBanner' => $iBanner ) );
	}
}
?>