<?php
function listProductsCompare( $sFile, $sBlock ){
	$oTpl = & TplParser::getInstance( );
	$oFile = & Files::getInstance( );
	$oDB = & DataBase::getInstance( );
	global $oPage;
	$aUrl = parse_url( 'http' . ( $_SERVER['HTTPS'] ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
	$content = null;
	$sBlock = strtoupper( $sBlock );
	$iStatus = throwStatus();
	$sql = 'SELECT `products`.*, `products_ext`.`sDescriptionFull`, `products_available`.`iDays`, `products_available`.`sText` `sAvailable`
					' . ( $sBlock == 'GOOGLE' ? ', `pv`.`sEan`, `pv`.`iProductVariant`, `pv`.`iFile`, `vpvn`.`sName` AS `sVariantName`' : null ) . '
					' . ( $sBlock == 'SKAPIEC' || $sBlock == 'NOKAUT' || $sBlock == 'CENEO' || $sBlock == 'GOOGLE' ? ', `producers`.`sName` AS `sProducerName`' : null ) . '
					' . ( $sBlock == 'SKAPIEC' || $sBlock == 'INTERIA' || $sBlock == 'HEUREKA' || $sBlock == 'ZBOZI' ? ', ( SELECT `iPage` FROM `' . $oDB->throwTableName( DB_PRODUCTS_PAGES ) . '` AS `pp` WHERE `pp`.`iProduct` = `products`.`iProduct` ORDER BY `iMainCategory` DESC, `iElement` ASC LIMIT 1 ) AS `iPage`' : null ) . '
						FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `products`
							LEFT JOIN `' . $oDB->throwTableName( DB_PRODUCTS_EXT ) . '` AS `products_ext` USING( `iProduct` )' . ' 
							LEFT JOIN `' . $oDB->throwTableName( DB_PRODUCTS_AVAILABLE ) . '` AS `products_available` ON( `products_available`.`iAvailable` = `products`.`TiAvailable` )
							' . ( $sBlock == 'SKAPIEC' || $sBlock == 'NOKAUT' || $sBlock == 'CENEO' || $sBlock == 'GOOGLE' ? ' LEFT JOIN `' . $oDB->throwTableName ( DB_PRODUCERS ) . '` AS `producers` ON (`producers`.`iProducer` = `products`.`iProducer`)' : null ) . '
							' . ( $sBlock == 'GOOGLE' ? ' LEFT JOIN `' . $oDB->throwTableName ( DB_PRODUCTS_VARIANTS ) . '` AS `pv` ON (`pv`.`iProduct` = `products`.`iProduct`) LEFT JOIN `' . $oDB->throwTableName ( DB_V_PRODUCTS_VARIANTS_NAMES ) . '` AS `vpvn` ON (`vpvn`.`iProductVariant` = `pv`.`iProductVariant`)' : null ) . '
						WHERE `products`.`iStatus` >= ' . $iStatus . ' AND `iCompare` > 0';
	if( $sBlock == 'NOKAUT' ){
		$sql .= ' AND `products`.`iProducer` IS NOT NULL AND `products`.`iProducer` > 0';
	}
	$aProductsInfo['aList'] = $oDB->executeSQL( $sql, true );
//	var_dump( $aProductsInfo );
	foreach( $aProductsInfo['aList'] as &$aData ){
		$aData['sLinkName'] = throwPageUrl( $aData['iProduct'] . ',' . change2Url( $aData['sName'] ) );
		if( isset( $aData['sNameCompare'] ) && $aData['sNameCompare'] != '' ) $aData['sName'] = $aData['sNameCompare'];
		$aData['aPagesTree'] = $GLOBALS['oProduct']->getPagesTree( $aData['iProduct'], $aData['sName']  );
		if( !empty( $aData['iFile'] ) ){
			$aData['aDataImage'] = $oFile->throwFile( 2, $aData['iFile'] );
		}
		elseif( isset( $oFile->aImagesDefault[2][$aData['iProduct']] ) ){
			$aData['aDataImage'] = $oFile->aImagesDefault[2][$aData['iProduct']];
		}
		if( !empty( $aData['sDescriptionShort'] ) ) $aData['sDescriptionShort'] = strip_tags( changeTxt( $aData['sDescriptionShort'], 'nlNds' ), '<br><p><ul><li>' );
		else $aData['sDescriptionShort'] = strip_tags( changeTxt( $aData['sDescriptionFull'], 'nlNds' ), '<br><p><ul><li>' );
		if( $sBlock == 'NOKAUT' || $sBlock == 'SKAPIEC' || $sBlock == 'GOOGLE' || $sBlock == 'CENEO'  ){
			$aData['aProducer'] = Array( 'sName' => $aData['sProducerName'] );
		}
		if( $sBlock == 'NOKAUT' ){
			if( $aData['iDays'] === NULL ){
				$aData['iDays'] = 4;
			} elseif( $aData['iDays'] == 0 ){
				$aData['iDays'] = 0;
			} elseif( $aData['iDays'] <= 7 ){
				$aData['iDays'] = 1;
			} elseif( $aData['iDays'] > 7 ){
				$aData['iDays'] = 2;
			}
		}
		if( $sBlock == 'CENEO' ){
			$aData['aAttributes'] = null;
			if( $aData['aProducer'] ){
				$aData['aAttributes'][] = Array( 'sAttributeName' => 'Producent', 'sAttributeValue' => $aData['aProducer']['sName'] );
			}
			if( $aData['sCatalogNumber'] ){
				$aData['aAttributes'][] = Array( 'sAttributeName' => 'Model', 'sAttributeValue' => $aData['sCatalogNumber'] );
			}
			if( $aData['sProductIndex'] ){
				$aData['aAttributes'][] = Array( 'sAttributeName' => 'EAN', 'sAttributeValue' => $aData['sProductIndex'] );
			}
			if( $aData['iDays'] === NULL ){
				$aData['iDays'] = 0;
			} elseif( $aData['iDays'] == 0 ){
				$aData['iDays'] = 1;
			} elseif( $aData['iDays'] <= 3 ){
				$aData['iDays'] = 3;
			} elseif( $aData['iDays'] <= 7 ){
				$aData['iDays'] = 7;
			} elseif( $aData['iDays'] > 7 ){
				$aData['iDays'] = 14;
			}
		}
		if( $sBlock == 'OKAZJE' ){
			if( $aData['iDays'] === NULL ){
				$aData['iDays'] = 0;
			} elseif( $aData['iDays'] == 0 ){
				$aData['iDays'] = 1;
			} elseif( $aData['iDays'] <= 3 ){
				$aData['iDays'] = 3;
			} elseif( $aData['iDays'] <= 7 ){
				$aData['iDays'] = 7;
			} elseif( $aData['iDays'] > 7 ){
				$aData['iDays'] = 14;
			}
		}
	}
	if( $sBlock == 'SKAPIEC' ){
		$aProductsInfo['sSkapiecDate'] = date( 'Y-m-d' );
		$aProductsInfo['aSkapiecCategories'] = $oPage->throwCompare( );
	}
	if( $sBlock == 'INTERIA' ){
		$aProductsInfo['sInteriaDate'] = date( 'Y-m-d' );
		$aProductsInfo['aInteriaCategories'] = $oPage->throwCompare( );
	}
	if( $sBlock == 'GOOGLE' ){
		$aProductsInfo['aAvailabilities'] = $oDB->throwFileArraySmall( DB_PRODUCTS_AVAILABLE, 'iAvailable', 'sText' );
		$aProductsInfo['aCheapestCarrier'] = $oDB->executeSQL( 'SELECT `sName`, `fPrice` FROM `' . $oDB->throwTableName( DB_CARRIERS_PRICES ) . '` INNER JOIN `' . $oDB->throwTableName( DB_CARRIERS ) . '` USING( `iCarrier` ) ORDER BY `fPrice` ASC LIMIT 1', true, DataBase::SINGLE_RECORD );
	}
	$aProductsInfo['sBlock'] = $sBlock;
	$oTpl->assign( 'aUrl', $aUrl );
	if( $sBlock == 'HEUREKA' || $sBlock == 'ZBOZI' ){
		$oPage->addZboziToCache();
		$oTpl->assign( 'aPagesInfo', $oPage->aPages );
	}
	$oTpl->assign( 'aProductsInfo', $aProductsInfo );
	return trim( $oTpl->parse( $sFile ) );
}
?>