<?php
//define this module's name and path
$mn = "odyssey_latest_articles";
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)
*/

if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...
$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." ";
}

$dataSource = $moduleParameters['dataSource'];
$dataSource = ($dataSource == "auto" || empty($dataSource)) ? $section : $dataSource;
if($system->getTableSectionID($dataSource)) { // only if a valid section
	

$publishedFieldName = $moduleParameters['publishedFieldName'];
$featuredFieldName = $moduleParameters['featuredFieldName'];
$subtitleFieldName = $moduleParameters['subtitleFieldName'];
$minitextFieldName = $moduleParameters['minitextFieldName'];
$showPublished = (empty($moduleParameters['showPublished'])) ? false : true;
$showSubtitle = (empty($moduleParameters['showSubtitle'])) ? false : true;
$showMinitext = (empty($moduleParameters['showMinitext'])) ? false : true;
$viewName = $moduleParameters['viewName'];
$numberOfArticles = $moduleParameters['numberOfArticles'];
$displayFeatured = $moduleParameters['displayFeatured'];
$sortingType= ($moduleParameters['sortingType'] == "sort") ? "sort" : "id DESC";
$thumbType = (int)$moduleParameters['thumbType'];
$thumbWidth = (int)$moduleParameters['thumbWidth'] ? (int)$moduleParameters['thumbWidth'] : 158; // make sure there is a value for width
$thumbHeight = (int)$moduleParameters['thumbHeight'] ? (int)$moduleParameters['thumbHeight'] : 158; // make sure there is a value for height
$thumbAlign = $moduleParameters['thumbAlign'];
$cssContent = $moduleParameters["cssContent"];

if($useModuleContainer ){

?>

<div id="<?php echo $mn.$sufixID;?>" data-editable="editable" class="responsive_float">
<?php
}
?>
<style type="text/css">
#<?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;?>;
	max-width: 600px;
	margin: <?php echo $marginTop;?> auto <?php echo $marginBottom;?> auto !important; /* override for keeping centered*/
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php
}	
if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php	
}
?>
}
<?php
if($cssContent){ // load custom css a cssContent has been found
	echo $variables->restoreModuleValue($cssContent);
}
$tcw = ($thumbType < 2) ? '80%' : '50%';
?>
.textual-content{
	min-width: <?php echo $tcw;?>;
	<?php
	if($thumbAlign == "left"){
	?>
	margin-left: <?php echo $thumbWidth +40;?>px;
	min-width: auto;
	<?php	
	}
	?>
}
.thumb-container{
	 margin-bottom: 40px !important;
}

@media only screen and (max-width: 550px) {
	.thumb-container{
		float:none !important;
	}
	.textual-content{
		<?php
		if($thumbAlign == "left"){
		?>		
		width: 80%;
		<?php
		}
		?>
		margin-left: 22px;
	}	
}
</style>
<?php
}
   

if(!count(preg_grep( "/jOthumb\.js/", $this->loadedFiles))){ // αν δεν έχει φορτωθεί το jquery plugin jOthumb
	if($thumbType > 1) { // αν έχει επιλεγεί thumbnail με αυτό το plugin
		?>
		<script type="text/javascript" src="<?php echo getBase();?>js/jOplugins/jOthumb.js"></script>
		<script type="text/javascript">
		$().ready(function(){
			$('div[data-latest="jOthumb"]').each(function(){
				$(this).jOthumb(<?php if($thumbType != 3) { echo "{'static': 1}";}?>);
			});
		});
		</script>
		<?php
	}
}
	global $langID;
	$sectionID = $system->getTableSectionID($dataSource);
	$q = "select distinct $dataSource.id as id, ";
	if($system->isFieldNameExists($publishedFieldName, $sectionID) && $showPublished) $q .= "$dataSource.$publishedFieldName as $publishedFieldName, ";
	if($system->isFieldNameExists($minitextFieldName, $sectionID) && $showMinitext) $q .= "content_$dataSource.$minitextFieldName as $minitextFieldName, ";
	if($system->isFieldNameExists($subtitleFieldName, $sectionID) && $showSubtitle) $q .= "content_$dataSource.$subtitleFieldName as $subtitleFieldName, ";
	$q .= "content_$dataSource.title as title from $dataSource, content_$dataSource where $dataSource.active=1 "; 
	if($system->isFieldNameExists($featuredFieldName, $sectionID)){
		if($displayFeatured == "0") $q .= "and $dataSource.$featuredFieldName <> 1 ";
		if($displayFeatured == "1") $q .= "and $dataSource.$featuredFieldName = 1 ";
	}
	$q.="and content_$dataSource.langID='$langID' and $dataSource.id=content_$dataSource.mainID ";
	if($system->isFieldNameExists($publishedFieldName, $sectionID)){
		$q .= "order by $dataSource.$publishedFieldName DESC ";
	}else{
		$q .= "order by $dataSource.$sortingType ";
	}
	$q .= "LIMIT $numberOfArticles";
	$maxrows = $db->getNumRows($q);
	$latest_records=$db->getRecords($q);
	
	if($maxrows){
?>
		<ul class="latest_articles" style="width:100%">
	<?php
	foreach($latest_records as $latest_row){
		if($thumbType){
			$image = $content->getImage($dataSource, $latest_row->id, $langID, 'default');
			$thumbURL = "mediabank/medium/medium_".$image;
			if($image ==".") $thumbURL = "images/no_image.png";
			$w = $thumbWidth;
			$h = $thumbHeight;
		}
		if($latest_row->$publishedFieldName) $published = t("Δημοσιεύτηκε στις")." ".$dates->makeDatefromTimestamp($latest_row->$publishedFieldName)."&nbsp;&nbsp;&nbsp;&nbsp;"; 
		$latestArticleLink = $sef->url("index.php?section=".$dataSource."&view=".$viewName."&id=".$latest_row->id, false);
		?>
		
		<li >
		<?php
		if($thumbType){
			$expandImage = $thumbType == 3;
			?>
            <div class="thumb-container" style="margin:0 10px; width:<?php echo ($w+10);?>px; height: <?php echo ($h+10);?>px; float:<?php echo $thumbAlign;?>;">
            <?php
			if($thumbType > 1){	
				$content->displayThumbnail($thumbURL, $w, $h, $latestArticleLink, $expandImage);
			}else{
				?>
                <a href="<?php echo $latestArticleLink;?>"><img src="<?php echo $thumbURL;?>" width="<?php echo $w;?>" height="<?php echo $h;?>" ></a>
                <?php
			}
			?>
            </div>
            <?php
		}
		if($thumbType > 1){
		?>
		<div style="height:10px"></div>
		<?php
		}
		?>
        <div class="textual-content">
		<a class="latest_articles_index_title" href="<?php echo $latestArticleLink;?>"><?php echo $latest_row->title;?></a>
		<?php
		if($latest_row->$subtitleFieldName){
		?>
		<span class="latest_articles_index_subtitle"><?php echo $latest_row->$subtitleFieldName;?></span>
		<?php
		}
		if($published){
		?>
		<div class="latest_articles_published"><?php echo $published;?></div>
		<?php
		}
		if($latest_row->$minitextFieldName){
		?>
		<div class="latest_articles_minitext"><?php echo $variables->limitText($latest_row->$minitextFieldName);?></div>
		<?php
		}
		?>
        </div>
		</li>
		<?php
	}
	?>
	</ul>
	<div style="clear:both"></div>
	<?php
	}else{
		?>
		<div style="padding-bottom: 200px"><?php echo t("Δεν βρέθηκαν άρθρα");?>...</div>
		<?php
	}

if($useModuleContainer ){
?>
</div>
<?php
}
}
?>