<?php
function saveBox( $aForm, $bExist = null ){
	$oDB = DataBase::getInstance( );
	$oFile = FilesAdmin::getInstance( );
	if( !isset( $aForm['iBox'] ) || !is_numeric( $aForm['iBox'] ) ){
		$sParam = null;
	} else{
		$sParam = 'iBox';
	}
	$aForm = changeMassTxt( $aForm, '', Array( 'sContent', 'nds' ) );
	$aForm['iBox'] = $oDB->save( DB_BOXES, $aForm, $sParam );
	$oDB->deleteInFile( DB_BOXES_PAGES, $aForm['iBox'], 'iBox' );
	foreach( $aForm['aPages'] as $iPage ) if( is_numeric( $iPage ) && !empty( $iPage ) ){
		$oDB->save( DB_BOXES_PAGES, Array( 'iBox' => $aForm['iBox'], 'iPage' => $iPage ) );
	}
	if( isset( $aForm['aFilesDelete'] ) ) $oFile->deleteSelectedFiles( $aForm['aFilesDelete'], 3 );
	if( isset( $aForm['aFilesDescription'] ) ) $oFile->saveFiles( $aForm, $aForm['iBox'], 3 );
	if( isset( $_FILES['aNewFiles'] ) ) $oFile->addFilesUploadedMulti( $aForm['iBox'], 3, 'iBox' );
	if( isset( $aForm['aDirFiles'] ) ) $oFile->addFilesFromServer( $aForm, $aForm['iBox'], 3, 'iBox' );
	return $aForm['iBox'];
}
function deleteBox( $iBox ){
	$oDB = DataBase::getInstance( );
	$oDB->deleteInFile( DB_BOXES, $iBox, 'iBox' );
}
function throwBox( $iBox ){
	$oDB = DataBase::getInstance( );
	$aData = $oDB->throwData( DB_BOXES, $iBox, 'iBox' );
	if( !isset( $aData ) || !is_array( $aData ) ){
		return null;
	} else{
		$aData['sContent'] = changeTxt( $aData['sContent'], 'Ndsnl' );
		return $aData;
	}
}
function listBoxes( ){
	$oDB = DataBase::getInstance( );
	return $oDB->throwFileArray( DB_BOXES );
}
?>