<?php
if( $a == 'list' ){
	if( isset( $_GET['sPhrase'] ) ){
		$aFieldsSearch = Array( 'sName', 'sEmail', 'sStreet', 'sZipCode', 'sCity', 'sPhone', 'sOrder' );
		$aWords = preg_split( '/\\s+/', $_GET['sPhrase'] );
		foreach( $aWords as $iWord => $sWord ){
			if( empty( $sWord ) ) unset( $aWords[$iWord] );
		}
	} else{
		$aWords = null;
	}
	if( !empty( $aWords ) ){
		foreach( $aWords as &$sWord ){
			$sWord = '( `' . implode( '` LIKE \'%' . $oDB->escape( $sWord ) . '%\' OR `', $aFieldsSearch ) . '` LIKE \'%' . $oDB->escape( $sWord ) . '%\' )';
		}
		$sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM `' . $oDB->throwTableName( DB_FAST_ORDERS ) . '` WHERE ' . implode( ' AND ', $aWords ) . ' ORDER BY `iOrder` DESC';
	} else{
		$sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM `' . $oDB->throwTableName( DB_FAST_ORDERS ) . '` ORDER BY `iOrder` DESC';
	}

	$iList = $config['admin_list'];
	if( $iList != 'inf' ){
		$iPageNumber = $iPage;
		if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 ) $iPageNumber = 1;
		if( isset( $_POST['sort'] ) || isset( $_POST['productsPerPage'] ) ){
			$iPageNumber = 1;
			$aActions['o2'] = 1;
		}
		$iEnd = $iPageNumber * $iList;
		$iStart = $iEnd - $iList;
		$sql.= ' LIMIT ' . $iStart . ',' . $iList;
		$aFastOrders = $oDB->executeSQL( $sql, true );
		$sql = 'SELECT FOUND_ROWS()';
		$iCount = $oDB->executeSQL( $sql, true, DataBase::SINGLE_VALUE );
		$aReturn['aPagesCountData'] = Array( 'iCount' => ceil( $iCount / $iList ), 'iPageNumber' => $iPageNumber );
		if( is_array( $aFastOrders ) ) foreach( $aFastOrders as &$aFastOrder ){
			$aFastOrder = changeMassTxt( $aFastOrder, 'br' );
		}
		$aReturn['aFastOrders'] = $aFastOrders;
	}

	if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
	$oTpl->assign( 'aFastOrders', $aReturn );
	$oTpl->assign( 'a', $a );
	$oTpl->assign( 'sUrlExt', '&amp;sPhrase=' . $_GET['sPhrase'] );
	$content.= $oTpl->parse( 'fastorders_list.tpl' );
} elseif( $a == 'delete' && isset( $iOrder ) ){
	$oDB->deleteInFile( DB_FAST_ORDERS, $iOrder, 'iOrder' );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=fastorders-list&sOption=del' );
	exit;
} elseif( $a == 'form' && isset( $iOrder ) ){
	if( isset( $_POST['sOption'] ) ){
		$_POST['iOrder'] = $iOrder;
		$oDB->save( DB_FAST_ORDERS, $_POST, 'iOrder' );
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	$aFastOrder = $oDB->throwData( DB_FAST_ORDERS, $iOrder, 'iOrder' );
	$oTpl->assign( 'aFastOrder', $aFastOrder );
	$content .= $oTpl->parse( 'fastorders_form.tpl' );
}