<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

$is06  = sly_Core::getVersion('X.Y') === '0.6';
$base  = $is06 ? '../data/dyn/public/import_export/' : sly_Util_AddOn::assetBaseUri('sallycms/import-export');
$user  = sly_Util_User::getCurrentUser();
$canEx = $user->isAdmin() || $user->hasRight('import_export', 'export');
$canIm = $user->isAdmin() || $user->hasRight('import_export', 'import');
$canDL = $user->isAdmin() || $user->hasRight('import_export', 'download');

// prepare table
$table = new sly_Table('exports');
$table->addColumn(new sly_Table_Column('<img src="'.$base.'images/backup.png" alt="" />', '', '', array('class' => 'sly-col-icon sly-icon')));
$table->addColumn(new sly_Table_Column('Datei', '', '', array('class' => 'sly-col-file')));

if ($canIm || $canEx) {
	$table->addColumn(new sly_Table_Column('Optionen', '', '', array('class' => 'sly-col-funcs')));
}

$table->addColumn(new sly_Table_Column('Datum', '', '', array('class' => 'sly-col-date')));
$table->addColumn(new sly_Table_Column('Größe', '', '', array('class' => 'sly-col-size')));
$table->setIsEmpty(empty($files));
$table->setEmptyNotice(t('im_export_no_backups_available'));

?>
<div class="sly-content">
	<?php
	// print messages
	print sly_Helper_Message::renderFlashMessage();

	// intro text
	print '<p>'.t('im_export_intro_import').'</p>';

	// sort archives by date
	foreach ($files as $idx => $file) {
		$filename = $this->baseDir.$file;

		if (!file_exists($filename)) {
			unset($files[$idx]);
			continue;
		}

		$info = sly_A1_Util::getArchiveInfo($filename);
		$info['filename'] = $file;

		$files[$idx] = $info;
	}

	usort($files, array('sly_A1_Util', 'compareArchiveDate'));
	$files = array_reverse($files);

	// print table
	$table->openBuffer();

	foreach ($files as $info) {
		$file    = $info['filename'];
		$baseURL = 'index.php?page=a1imex_import&amp;file='.urlencode($file).'&amp;func=';

		?>
		<tr>
			<td class="sly-col-icon sly-icon"><img src="<?php print $base ?>images/backup.png" alt="" /></td>
			<td class="sly-col-file">
				<?php if ($canDL): ?>
				<a href="<?php print $baseURL ?>download" title="<?php print sly_html($info['comment']) ?>"><?php print sly_html($info['name']) ?></a>
				<?php else: ?>
				<span title="<?php print sly_html($info['comment']) ?>"><?php print sly_html($info['name']) ?></span>
				<?php endif ?>

				<?php if (!empty($info['description'])): ?>
				<span class="desc">(<?php print sly_html($info['description']) ?>)</span>
				<?php endif ?>
			</td>

			<?php if ($canIm || $canEx): ?>
			<td class="sly-col-funcs">
				<?php if ($canIm && !empty($info['missing'])): ?>
				<span class="sly-strike import" title="<?php print t('requires').' '.sly_Util_String::humanImplode($info['missing']) ?>"><?php print t('im_export_import_file') ?></span>
				<?php elseif ($canIm && !empty($info['missing']) || !$info['compatible']): ?>
				<span class="sly-strike import" title="<?php print t(($is06 ? 'component' : 'addon').'_incompatible_with_this_version') ?>"><?php print t('im_export_import_file') ?></span>
				<?php elseif ($canIm): ?>
				<a href="<?php print $baseURL ?>import" class="import sly-confirm-me sly-postlink"><?php print t('im_export_import_file') ?></a>
				<?php endif ?>

				<?php if ($canEx): ?>
				<a href="<?php print $baseURL ?>delete" class="delete sly-confirm-me sly-postlink"><?php print t('im_export_delete_file') ?></a>
				<?php endif ?>
			</td>
			<?php endif ?>

			<td class="sly-col-date"><abbr class="a1-timeago" title="<?php print date('Y-m-d\TG:i:sP', $info['date']) ?>"><?php print sly_Util_String::formatDatetime($info['date']) ?></abbr></td>
			<td class="sly-col-size"><?php print sly_Util_String::formatFilesize(filesize($this->baseDir.$file)) ?></td>
		</tr>
		<?php
	}

	$table->closeBuffer();
	print $table->render();

	sly_Core::getLayout()->addJavaScript('jQuery(function($){$(".a1-timeago").timeago();});')

	?>
</div>
