<?php 
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_STRICT); 
$rootPath = is_file("../../../../library/document.php") ? "../../../../" : "../../";
define("CMS", "korCMS");
include($rootPath."config/odyssey.ini.php");
include($rootPath."library/db.php");
include($rootPath."library/system.php");
include($rootPath."library/messages.php");
include($rootPath."library/variables.php");
include($rootPath."library/language.php");
include($rootPath."library/parameters.php");
include($rootPath."library/content.php");
include($rootPath."library/admin_pagination.php");
include($rootPath."library/fileSystem.php");
include($rootPath."library/sef.php");
$sef = new sef();
$db = new db();
$system = new system();
$messages = new messages();
$variables = new variables();
$language = new language();
$parameters = new parameters();
$content = new content();
$fileSystem = new fileSystem();
$db->query("SET NAMES 'utf8mb4'");

function getFPath(){
	$parts = explode("/", $_SERVER["PHP_SELF"]);
	array_shift($parts); // the first element will be empty to we get rid of it
	array_pop($parts); // get rid of script name
	$fPath =implode("/", $parts);
	if(substr($fPath, -1) != "/") $fPath.="/";
	$fPath = ($fPath == "/") ? null : $fPath;	
	return $fPath;	
}

$langID = ($_GET['langID']) ? (int)$_GET['langID'] : $language->getDefaultLanguage();
$Lsufix = ((int)$langID > 1) ? "_translated" : NULL;
$variables->getSafeVars(array_keys($_REQUEST));

$token1 = "38629649";
$token2 = "73972658";
list($stage1, $token2b) = explode($token2, $_REQUEST['u']);
list($token1b, $userID) = explode($token1, $stage1);
if(empty($userID)) die("Access denied");

// lets check if the section's record belongs to this user
$section = $system->getTableSectionName($sectionID);
$uid = $db->getRecord("select userID from $section where id='$linkID'")->userID;
if($uid <> $userID) die("Access denied");



$parameters->importParameters();

if($action=="delete" && $bankID && $linkID && $sectionID && $_REQUEST['id']){ // procceed to delete attachments
	$id = (int)$_REQUEST['id'];
	$db->query("DELETE FROM content_images, images
			   USING content_images INNER JOIN images
			   WHERE images.id='$id' and content_images.imgID=images.id 
			   AND images.bankID='$bankID' 
			   AND images.sectionID='$sectionID' 
			   AND images.linkID='$linkID'");
	$db->query("DELETE FROM content_files, files 
			   USING content_files INNER JOIN files 
			   WHERE files.id='$id'
			   AND content_files.fileID=files.id 
			   AND files.bankID='$bankID' 
			   AND files.sectionID='$sectionID' 
			   AND files.linkID='$linkID'");
	
}

function t($key, $langID=NULL, $context=NULL){ // shortcut of $language->getText($key, $langID=NULL, $context=NULL)
	global $language;
	global $langID;
	return $language->getText($key, $langID, $context);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="favicon.ico">
<title>Admin Panel</title>
<link href="<?php echo $rootPath;?>admin/css/admin.css" rel="stylesheet" type="text/css">
<link href="<?php echo $rootPath;?>admin/css/pagination.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?php echo $rootPath;?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo $rootPath;?>admin/js/admin.js"></script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	overflow: auto;
	max-width:700px;
	background: #ffffff;
	font-family: Verdana, sans-serif, Arial;
	font-size:11px;	
}
-->
</style>
</head>
<body>
<div style="text-align:center;">
<?php 
$query1 = "SELECT distinct bankID, id FROM images WHERE linkID=".$linkID." AND sectionID=".$sectionID." order by sort";
$query2 = "SELECT distinct bankID, id FROM files WHERE linkID=".$linkID." AND sectionID=".$sectionID." order by sort";
$records1 = $db->getRecords($query1);
$records2 = $db->getRecords($query2);
$records = (object) array_merge((array) $records1, (array) $records2);
if(!count($records1) && !count($records2)){
	?>
	<script>
	parent.$('#attachments_instructions').css('display','none');
	</script>	
	<div style="height:50px">&nbsp;</div>
	<?php
	$messages->addMessage(t("Δεν έχετε επισυνάψει ακόμα, κανένα αρχείο σε αυτή την εγγραφή")."!");
	$messages->printSystemMessages();
}

?>
<div id="attachments_results" style="display:inline-block; padding:0px;">
<?php	
foreach($records as $row){
	$ext = $fileSystem->getExtensionByFileTypeID($fileSystem->getFileTypeID($row->bankID));
	 ?>
	<table style="padding:0; float:left; height:230px; overflow:hidden;" cellspacing="5">
	<tr>		 
	<td style="vertical-align:top; height: 100px">
	<div class="delete_attachment_container" style="padding-top:15px; height: 100px; overflow:hidden">
	<div class="delete_icon" onclick="if(confirm('<?php echo t("Είστε βέβαιοι πως θέλετε να καταργήσετε την διασύνδεση με αυτό το αρχείο;");?>')){document.location.href='?u=<?php echo $u;?>&action=delete&id=<?php  echo $row->id;?>&sectionID=<?php  echo $sectionID;?>&linkID=<?php  echo $linkID;?>&bankID=<?php  echo $row->bankID;?>';}" title="<?php echo t("Κάντε κλικ για να καταργήσετε αυτή την διασύνδεση");?>!"></div>
	<?php 
	if($fileSystem->isImage($row->bankID)){
		$title = $fileSystem->getFileTitle("images", $row->id, $linkID, $row->bankID, $langID);
		$temp =$db->query("select * from images where id='$row->id'");
		$temprow = $db->getRow($temp);
		if($temprow->default) $title.="<br><span class='image_default'>Default</span>";
		if($temprow->slider) $title.="<br><span class='image_slider'>Slider</span>";
		if($temprow->logo) $title.="<br><span class='image_logo'>Logo</span>";
		?>
		<img src="<?php echo $rootPath;?>mediabank/thumbs/small_<?php  echo $row->bankID.'.'.$ext;?>?r=<?php  echo @time();?>" alt="<?php echo t("Κάντε κλικ για να καταργήσετε αυτή την διασύνδεση");?>!" border="0">
		<?php 
}else{
		$title = $fileSystem->getFileTitle("files", $row->id, $linkID, $row->bankID, $langID);
		?>
		<img src="<?php echo $rootPath;?>mediabank/icons/filetype_<?php  echo $ext;?>.gif" alt="<?php echo t("Κάντε κλικ για να καταργήσετε αυτή την διασύνδεση");?>!" border="0">
		<?php 
}
	?>
	</div>
	</td>
	</tr>
	<tr>
	<td style="text-align:center; width:170px; font-size:.8em; vertical-align:top"><?php  echo $title;?></td>
	</tr>		
	</table>
	<?php 
}
?>
</div>
</div>
</body>
</html>
