<?php

if( $a == 'list' ){
	$sql = 'SELECT SQL_CALC_FOUND_ROWS `an`.`iProduct`, `an`.`iTime`, `an`.`iTimeSent`, `sEmail`, `sPhone`, `iTimeSent`, IFNULL( CONCAT( `p`.`sName`, \', \', `pvn`.`sName` ), `p`.`sName` ) AS `sName` FROM `' . $oDB->throwTableName( DB_PRODUCTS_AVAIL_NEWSLETTER ) . '` `an` INNER JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) . '` `p` USING( `iProduct` ) LEFT JOIN `' . $oDB->throwTableName( DB_V_PRODUCTS_VARIANTS_NAMES ) . '` `pvn` USING( `iProductVariant` ) WHERE 1';
	if( isset( $_GET['iProduct'] ) ){
		$sql .= ' AND `iProduct` = ' . intval( $_GET['iProduct'] );
		$sUrlExt .= '&amp;iProduct=' . $_GET['iProduct'];
	}
	if( isset( $_GET['iProductVariant'] ) ){
		$sql .= ' AND `iProductVariant` = ' . intval( $_GET['iProduct'] );
		$sUrlExt .= '&amp;iProductVariant=' . $_GET['iProductVariant'];
	}
	if( isset( $_GET['iSent'] ) && $_GET['iSent'] != -1 ){
		$sql .= ' AND `iTimeSent` IS ' . ( $_GET['iSent'] == 1 ? 'NOT NULL' : 'NULL' );
		$sUrlExt .= '&amp;iSent=' . $_GET['iSent'];
	}
	$iPageNumber = isset( $_GET['iPage'] ) ? $_GET['iPage'] : 1;
	if( !isset( $iPageNumber ) || !is_numeric( $iPageNumber ) || $iPageNumber < 1 ) $iPageNumber = 1;
	$iList = $config['admin_list'];
	$iEnd = $iPageNumber * $iList;
	$iStart = $iEnd - $iList;
	$sql.= ' LIMIT ' . $iStart . ',' . $iList;
	$aProducts = $oDB->executeSQL( $sql, true );
	$iProducts = $oDB->executeSQL( 'SELECT FOUND_ROWS() AS `count`;', true, DataBase::SINGLE_VALUE );
	$aPagesCountData = Array( 'iCount' => ceil( $iProducts / $iList ), 'iPageNumber' => $iPageNumber );
	$iCurrentTime = time();
	foreach( $aProducts as &$aProduct ){
		$aProduct['sTimeDifference'] = time_difference( $iCurrentTime - $aProduct['iTime'] );
		if( !empty( $aProduct['iTimeSent'] ) ){
			$aProduct['sTimeDifferenceSent'] = time_difference( $iCurrentTime - $aProduct['iTimeSent'] );
		}
		$aProduct['aPageTree'] = $oProduct->getPagesTree( $aProduct['iProduct'], $aProduct['sName'] );
		if( isset( $oFile->aImagesDefault[2][$aProduct['iProduct']] ) ){
			$aProduct['aDataImage'] = $oFile->aImagesDefault[2][$aProduct['iProduct']];
		}
	}
	$oTpl->assign( 'aProducts', $aProducts );
	$oTpl->assign( 'aPagesCountData', $aPagesCountData );
	$oTpl->assign( 'sUrlExt', $sUrlExt );
	$content .= $oTpl->parse( 'avail_newsletter_list.tpl' );
}