<?php	
if (!isset($file)) $file = NULL;
if (!isset($filesize)) $filesize = NULL;

$file_dir = "../".$module."/original/";

$extension_tmp = explode(".", $file);
$extension = end($extension_tmp);

print "
<input type='hidden' class='field' id='file_old' name='file_old' value='".$file."'>
<input type='hidden' class='field' id='filesize' name='filesize' value='".$filesize."'>
<div class='form-row'>
	<label for='upload' class='form-label'>".LABEL_FILE."</label>
	<div class='form-cols upload'>
		<div class='form-col form-full upload'>
			<div class='form-controls'>
				<input type='file' class='field image-upload' id='upload' name='upload' value=''>
				<label for='upload' class='label-upload' data-id='upload'";
if ($file) print " style='background-image: url(".$file_dir.$file.")'";
print ">".LABEL_CHOOSE_FILE."</label>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->";
	
$tables_data = NULL;
$sql_tables = "SHOW TABLES LIKE '%\\_images'";
$result_tables = $conn->prepare($sql_tables);
$result_tables->execute();
if ($result_tables->rowCount()){
	while ($arr_tables = $result_tables->fetch(PDO::FETCH_COLUMN)){
		$tables_data[] = $arr_tables;
	}
}

$media_used = false;
foreach ($tables_data AS $key => $value){
	$sql_check = "SELECT id FROM ".$value." WHERE id_media = :id_media ";
	$result_check = $conn->prepare($sql_check);
	$result_check->bindValue(':id_media', $arr['id'], PDO::PARAM_INT);
	$result_check->execute();
	if ($result_check->rowCount()){
		$media_used = true;
		break;
	}
}

if ($media_used){
	print "
	<div class='form-cols'>";
	foreach ($tables_data AS $key => $value){
		$media_module = str_replace('_images', '', $value);
		$sql_media = "SELECT * FROM ".$value." WHERE id_media = :id_media ";
		$result_media = $conn->prepare($sql_media);
		$result_media->bindValue(':id_media', $id, PDO::PARAM_INT);
		$result_media->execute();
		if ($result_media->rowCount()){
			$arr_media = $result_media->fetch();
			
			$media_module_module = NULL;
			$media_module_title = NULL;
			
			$sql_tmp = "SELECT text FROM backend_modules WHERE title = :title ";
			$result_tmp = $conn->prepare($sql_tmp);
			$result_tmp->bindValue(':title', $media_module, PDO::PARAM_STR);
			$result_tmp->execute();
			if ($result_tmp->rowCount()){
				$arr_tmp = $result_tmp->fetch();
				$media_module_module = $arr_tmp['text'];
			}
			if ($media_module != 'products') {
				$sql_tmp = "SELECT title FROM ".$media_module." WHERE id = :id ";
				$result_tmp = $conn->prepare($sql_tmp);
				$result_tmp->bindValue(':id', $arr_media['id_module'], PDO::PARAM_INT);
				$result_tmp->execute();
				if ($result_tmp->rowCount()){
					$arr_tmp = $result_tmp->fetch();
					$media_module_title = $arr_tmp['title'];
				}
			} else {
				$sql_tmp = "SELECT title FROM ".$media_module."_content WHERE id_module = :id ";
				if (isset($c_languages) && $c_languages){
					$sql_tmp .= "AND language = :language ";
				}
				$result_tmp = $conn->prepare($sql_tmp);
				$result_tmp->bindValue(':id', $arr_media['id_module'], PDO::PARAM_INT);
				if (isset($c_languages) && $c_languages){
					if (isset($language) && $language){
						$result_tmp->bindValue(':language', $language, PDO::PARAM_STR);
					} else {
						$result_tmp->bindValue(':language', $_SESSION['backend']['language'], PDO::PARAM_STR);
					}
				}
				$result_tmp->execute();
				if ($result_tmp->rowCount()){
					$arr_tmp = $result_tmp->fetch();
					$media_module_title = $arr_tmp['title'];
				}
			}
			print "
		<label for='".$arr_media['id_module']."' class='form-label right'>
			<a href='#' class='edit' data-id='".$arr_media['id_module']."' data-tab='images' data-module='".$media_module."' data-backend-module='".$media_module."' title='".LABEL_EDIT."'>
				".$media_module_module." - ".$media_module_title."
				<i class='material-icons md-22'>edit</i>
			</a>
		</label>";
		}
	}
	print "
	</div><!-- /.form-cols -->";
}

print "
</div><!-- /.form-row -->

<div class='form-row'>
	<label for='file' class='form-label'>".LABEL_FILE."</label>
	<div class='form-cols'>
		<div class='form-col form-full'>
			<div class='form-controls'>
				<input type='text' class='field' id='file' name='file' value='".str_replace('.'.$extension, '', $file)."'>
			</div><!-- /.form-controls -->
		</div><!-- /.form-col -->
	</div><!-- /.form-cols -->
</div><!-- /.form-row -->";

