<?php if(!defined('master_script')) die();

/**
 * Settings
 *
 * @category   Extension (imagelist)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-12
 */
	
	if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
		$_GET['dir'] = trim($_GET['dir'], '/') . '/';
		
		$page_storage_path = $_GET['dir'] . 'storage/';
		$catalogue_file = $page_storage_path . 'imagelist.cat';
		
		if(!file_exists($page_storage_path)) mkdir($page_storage_path);
	}

/**
 * Save imagelist
 *
 * @category   Extension (imagelist)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-12
 */

	if(isset($_POST['filelist'])) {
		$images = explode('][', trim($_POST['filelist'], ']['));
		file_put_contents($catalogue_file, implode("\n", $images));		
	}

/**
 * Get imagelist
 *
 * @category   Extension (imagelist)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-06-12
 */

	if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
		$images_files = array();

		if(is_file($catalogue_file)) {
			$images_array = file($catalogue_file);
			
			foreach($images_array as $image_entry) {
				$image_entry = $_GET['dir'] . trim($image_entry);
				if(is_file($image_entry)) $images_files[] = $image_entry;
			}
		}
			
		$images_files_new = filelist($_GET['dir'], 'image', array(), array(), true);
    	
    	foreach($images_files_new as $image_entry) {
    		if(!in_array($image_entry, $images_files)) array_unshift($images_files, $image_entry);
    	}
	}

/**
 * Backend template
 *
 * @category   Extension (imagelist)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-11-21
 */

?>

<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<meta name="language" content="<?php echo $user_data['backend language']; ?>">
	<meta name="generator" content="http://getpagemap.com/">

	<title><?php echo txt('global', 'Bilderliste'); ?></title>

	<!-- MoreCSS -->
	<link rel="MoreCSS" href="<?php echo $system_data['folder']['plugins']; ?>actions/sources/imagelist.css">
	<script type="text/javascript" src="<?php echo $system_data['folder']['plugins']; ?>morecss/MoreCSS.js"></script>
	<script type="text/javascript" src="<?php echo $system_data['folder']['plugins']; ?>morecss/MoreCSS.effects.js"></script>
	<script type="text/javascript" src="<?php echo $system_data['folder']['plugins']; ?>morecss/MoreCSS.sort.js"></script>
	
	<!-- BASICS -->	
	<link rel="stylesheet" type="text/css" media="all" href="<?php echo $system_data['folder']['backend']; ?>index.css">
	
	<style type="text/css">
		body {
			width: 300px;
			overflow-x: hidden;
			margin: 15px;
			background: white;
		}
		
		#file-options img {
			vertical-align: middle;
		}
		
		ul, li {
			display: block;
			overflow: hidden;
			width: 100%;
			margin: 0;
			padding: 0;
			list-style-type: none;
		}
		
		li {
			height: 50px;
			background-position: left top;
			background-repeat: no-repeat;
			background-color: transparent;
		}
		
		li b {
			display: block;
			margin-top: 1px;
			padding-left: 75px;
			background-color: #f6f6f6;
			background-position: 0px 0px;
			background-repeat: no-repeat;
			font-weight: normal;
			font-size: 12px;
			line-height: 45px;
			color: #666;
		}
		
		.placeholder {
			background-image: url(<?php echo $system_data['folder']['backend']; ?>mask/imagelist/placeholder-background.png);
		}
		
		.drag {
			width: 300px;
			background-image: url(<?php echo $system_data['folder']['backend']; ?>mask/imagelist/drag-background.png);
		}

		.drag b {
			color: #333;
		}
	</style>
</head>

<body>

	<div id="file-options">
		<span class="separator"><?php echo txt('global', 'Sortieren'); ?> ▸</span>
		<a class="normal" href="#normal"><img src="<?php echo $system_data['folder']['plugins']; ?>icons/arrow_down.png" title="<?php echo txt('global', 'Aufsteigend'); ?>"></a>
		<a class="reverse" href="#reverse"><img src="<?php echo $system_data['folder']['plugins']; ?>icons/arrow_up.png" title="<?php echo txt('global', 'Absteigend'); ?>"></a>
		<a class="shuffle" href="#shuffle"><img src="<?php echo $system_data['folder']['plugins']; ?>icons/arrow_switch.png" title="<?php echo txt('global', 'Zufällig'); ?>"></a>
		<span class="separator"><?php echo txt('global', 'Speichern'); ?> ▸</span>
		<a href="#save" onclick="saveImagelist(); return false"><img src="<?php echo $system_data['folder']['plugins']; ?>icons/disk.png" title="<?php echo txt('global', 'Speichern'); ?>"></a>
	</div>
	
	<hr>
	
	<?php if(isset($images_files) && count($images_files) > 0) { ?>
		<ul id="imagelist">
			<?php
				foreach($images_files as $image) {
			   		$imagepath = $system_data['folder']['plugins'] . 'thumbnail/thumbnail.php?image=' . $image;
					$imagepath .= '&amp;thumbnail_size=50';
					$imagepath .= '&amp;thumbnail_crop_width=50';
					$imagepath .= '&amp;thumbnail_crop_height=50';
					$imagepath .= '&amp;thumbnail_crop=1';
					$imagepath .= '&amp;thumbnail_save=0';
					
					echo '<li><b style="background-image: url(\'' . $imagepath . '\')">' . limit_text(basename($image), 25, '…') . '</b></li>';
				}
			?>
		</ul>
	<?php } else { ?>
		<p><?php echo txt('global', 'Es konnten keine Bilder gefunden werden.'); ?></p>
	<?php } ?>
	
	<form id="filelistForm" accept-charset="utf-8" method="post" action="index.php?backend&amp;actions=imagelist.php&amp;dir=<?php echo $_GET['dir']; ?>">
		<input id="filelist" type="hidden" name="filelist" value="">
	</form>

	<script type="text/javascript">
    	// Save image list
    	var saveImagelist = function() {
    		var filelistField = document.getElementById('filelist');
    		filelistField.value = '';
    		
    		var imagelistContainer = document.getElementById('imagelist');
    		var imagelistItems = (imagelistContainer) ? imagelistContainer.getElementsByTagName('li') : [];
    		
    		for(var i = 0; i < imagelistItems.length; i++)
    			filelistField.value += '[' + MoreCSS.trim(imagelistItems[i].getElementsByTagName('b')[0].innerHTML) + ']';
    		
    		var filelistFormElement = document.getElementById('filelistForm');
    		filelistFormElement.submit();
    	};
    </script>
</body>
</html>
