<?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)
*/

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'];

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

$relatedFilesHeader = $moduleParameters['relatedFilesHeader'];

if($useModuleContainer ){	
?>
<div id="<?php echo $mn.$sufixID;?>" data-editable="editable">
<?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;?>;
<?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	
}
?>
}
.fileLinkDiv{
	width: 140px;
	min-height: 120px;
	margin:15px;
	float:left;
	text-align:center;
	font-style:italic;
}
</style>
<?php
}

	global $langID;
	global $protocol;
	$dataSource = ($moduleParameters['dataSource'] == "auto") ? $section : $moduleParameters['dataSource'];
	$sectionID = $system->getTableSectionID($dataSource);
	$id = (int)$_REQUEST['id']; // id of current record
	$hasFiles = $content->getNumOfFiles($sectionID, $id, $langID);
	if($hasFiles){
		if($relatedFilesHeader){
			echo "<div class='related_files'>".t($relatedFilesHeader)."</div>";
		}
		$hasPDF = false;
		$files = $content->getFiles($sectionID, $id);	
		?>
		<div class="related_content">
		<?php
		foreach($files as $file){
			$filetitle = $fileSystem->getFileTitle("files", $file->id, $id, $file->bankID);
			$ext = $fileSystem->getExtensionByFileTypeID($fileSystem->getFileTypeID($file->bankID));
			if($ext == "pdf") $hasPDF = true;
			?>
			<div class="fileLinkDiv" style="margin:7px">
			<a href="mediabank/<?php echo $file->bankID.'.'.$ext;?>" target="_blank"><img src="images/icons/filetype_<?php echo $ext;?>.gif" alt="Download file" style="border: 0"/></a><br />
			<?php echo $filetitle;?>
			</div>
		<?php 
		}
		?>
		</div>
		<?php
		if($hasPDF){
			?>
	<div style="border-top: 1px solid silver; text-align:right; padding:5px 0; clear:left;">
		<a href="<?php echo $protocol;?>://get.adobe.com/reader/" data-rel="external"><img src="images/get_adobe_reader.png" width="149" height="36" alt="Get Adobe Reader"></a>
	</div>
	<?php 
	}
}

if($useModuleContainer ){
?>
</div>
<?php
}
}
?>
<div style='clear:both'></div>