<?php
//define this module's name and path
$mn = basename(__FILE__ , ".php");
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
$widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];

if(empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)){
	$Mmargin = "0";
}else{
	$Mmargin = $marginTop." ".$marginRight." ".$marginBottom." ".$marginLeft." ";
}
if(empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)){
	$Mpadding = "0";
}else{
	$Mpadding = $paddingTop." ".$paddingRight." ".$paddingBottom." ".$paddingLeft." ";
}

global $id;
$productID = $id;
if(!$productID) return; // no product id, do nothing

$q = "select distinct relatedID as id from related_products where productID='$productID'";
$pIDs = $db->getRecords($q);
$IDs = array();
foreach($pIDs as $record){
	$hasAccess = $eshop->hasAccess($record->id);
	if($hasAccess) $IDs[] = $record->id;
}
if(!count($IDs)){
	// exit module in silent mode
	return;	
}
$IDs = implode(",", $IDs);
$q = "select p.id, cp.title from products as p, content_products as cp where p.id IN (".$IDs.") and cp.mainID=p.id and cp.langID='$langID'";
$products = $db->getRecords($q);
if(!count($products)){
	// exit module in silent mode
	return;
}

$cssContent = $moduleParameters["cssContent"];
$widthPercent = (int)$moduleParameters["widthPercent"];
$widthPercent = ($widthPercent) ? $widthPercent : 50;
$transitionSpeed = (int)$moduleParameters["transitionSpeed"];
$numberOfProducts = (int)$moduleParameters["numberOfProducts"];
$autoPlaySpeed = ($moduleParameters['autoPlaySpeed'] == "__EMPTY__" || empty($moduleParameters['autoPlaySpeed'])) ? "false" : (int)$moduleParameters['autoPlaySpeed'];
$showNavigation = ($moduleParameters['showNavigation'] == "__EMPTY__" || empty($moduleParameters['showNavigation'])) ? "false" : "true";
$goToFirstProduct = ($moduleParameters['goToFirstProduct'] == "__EMPTY__" || empty($moduleParameters['goToFirstProduct'])) ? "false" : "true";
$navigationPrevText = $moduleParameters["navigationPrevText"];
$navigationNextText = $moduleParameters["navigationNextText"];
$responsive = ($moduleParameters['responsive'] == "__EMPTY__" || empty($moduleParameters['responsive'])) ? "false" : "true";

if($useModuleContainer ){
?>
<div id="<?php echo $mn.$sufixID;?>" data-editable="editable">
<?php
}
?>

<style  type="text/css">
#<?php echo $mn.$sufixID;?> .item{
	position:relative;
	width: inherit;
	height: inherit;
	vertical-align: bottom;
	display: table-cell;	
	padding:15px;
}
#<?php echo "owl-".$mn.$sufixID;?> .item img{
    display: block;
	max-height: 150px;
	margin-top:5px !important;
}
#<?php echo "owl-".$mn.$sufixID;?> .item div{
	margin: 3px;
	height: .9em;
	text-align:center;
	overflow:hidden;
	font-size: 90%;
	padding:10px;	
}
.product_carousel h2{
	text-align: left;
	margin-bottom: 0;
}
.product_carousel .owl-wrapper{
	display: table;
	/*height: 200px;*/
}
.product_carousel .owl-item{
	height:inherit;
}
.owl-pagination{ display:none; }
.related_products_header{
	text-align: center;
	border-top: 1px solid silver;
	padding: 5px 0 30px 0;
	font-size: 130%;
}

<?php
if($useModuleContainer ){
	?>
#<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php 
}	

if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...

if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
}
?>
}
<?php
}
if($cssContent){ // include custom css only if cssContent has been found
	echo $variables->restoreModuleValue($cssContent);
}
?>
</style>


<script type="text/javascript">
jQuery(document).ready(function($) {
	$("#<?php echo "owl-".$mn.$sufixID;?>").owlCarousel({
	 
	    //Basic Speeds
	    slideSpeed : <?php echo $transitionSpeed; ?>,
	    paginationSpeed : 800,
	 
	    //Autoplay
	    autoPlay : <?php echo $autoPlaySpeed; ?>,
	    goToFirst : <?php echo $goToFirstProduct; ?>,
	    goToFirstSpeed : 1000,
	 
	    // Navigation
	    navigation : <?php echo $showNavigation; ?>,
	    navigationText : ["<?php echo $navigationPrevText; ?>","<?php echo $navigationNextText; ?>"],
		scrollPerPage : true, 
	 
	    // Responsive 
	    responsive : <?php echo $responsive; ?>,
	    items : <?php echo $numberOfProducts; ?>,
	    itemsDesktop : false,
	    itemsDesktopSmall : [999,3],
	    itemsTablet : [640,2],
	    itemsMobile : false,
	});
});
</script>

<div class="related_products_header rainbow_color"><?php echo t("Σχετικά προϊόντα");?></div>
<div class="product_carousel">
	<div id="<?php echo "owl-".$mn.$sufixID;?>" class="owl-carousel owl-theme">
	<?php
	foreach($products as $product){
		$title = $product->title;
		$productID = $product->id;
		$defaultImage = $content->getImage("products", $productID, $langID, 'default');
		if($defaultImage != "."){ // if a default image exists for this record
			$imageFile = "mediabank/medium/medium_".$defaultImage;
			$foundThumb = true;			
		}else{
			$imageFile = $p."images/no_image.png";
		}	
		$link = $sef->url("index.php?section=products&view=node&id=".$productID."&set=".$IDs, false);	
		?>
		<div class="item">
			<a class="cpt" href="<?php echo $link;?>">
				<img src="<?php echo $imageFile;?>" alt="<?php echo $title;?>">
				<div title="<?php echo htmlspecialchars($title);?>"><?php echo $title;?></div>
			</a>
			<span class="pcdisplay_rating"><?php $eshop->displayRating($productID); ?></span>
		</div>
		<?php
	}
	?>
	</div>
</div>	
<?php
if($useModuleContainer ){
?>
</div>
<?php 
}
?>
