<?php
/*
 * Copyright (c) 2013, 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
 */

////////////////////////////////////////////////////////
// print breadcrumb path

$path    = '<li> : <a href="'.$_router->getUrl(null, null, $this->appendQueryString(array())).'">'.ht('start').'</a></li>';
$isForm  = $action === 'add' || $action === 'edit';
$addMode = $action === 'add';
$catID   = $cat    ? $cat->getId()    : 0;
$editID  = $active ? $active->getId() : 0;

if ($cat) {
	$parents = $cat->getParentTree();

	foreach ($parents as $parent) {
		$path .= '<li> : <a href="'.$_router->getUrl(null, null, $this->appendQueryString(array('cat_id' => $parent->getId()))).'">'.sly_html($parent->getName()).'</a></li>';
	}

	$path .= '<li> : <a href="'.$_router->getUrl(null, null, $this->appendQueryString(array('cat_id' => $catID))).'">'.sly_html($cat->getName()).'</a></li>';
}

?>
<div class="sly-navi-path">
	<ul>
		<li><?php echo t('path') ?></li>
		<?php echo $path ?>
	</ul>
</div>
<?php

print sly_Helper_Message::renderFlashMessage();

////////////////////////////////////////////////////////
// start form if needed

if ($isForm) {
	$form = new sly_Form('index.php', 'POST', '');
	$form->addHiddenValue('cat_id', $catID);

	if ($active) {
		$form->addHiddenValue('edit_id', $active->getId());
	}

	$this->appendParamsToForm($form);
	$form->setSubmitButton(null);
	$form->setResetButton(null);

	?>
	<form action="<?php print $_router->getUrl(null, $action) ?>" method="post">
	<?php

	print $form->render(true);
}

$addLink = $_router->getPlainUrl(null, 'add', $this->appendQueryString(array('cat_id' => $catID)));
$table   = new sly_Table('sly-mediapool-structure');

$table->addColumn(sly_Table_Column::sprite('mediacat-add', $addLink, t('add_category'), 'sly-col-icon'));
$table->addColumn(sly_Table_Column::factory(t('category_name'), 'sly-col-name'));
$table->addColumn(sly_Table_Column::factory(t('function'), 'sly-col-func'));

$table->setIsEmpty(empty($children) && $action !== 'add');
$table->setEmptyNotice(t('no_categories_yet'));
$table->openBuffer();

if ($addMode) {
	?>
	<tr class="sly-active">
		<td class="sly-col-icon sly-icon"><?php echo sly_Util_HTML::getSpriteLink('', t('add_category'), 'mediacat') ?></td>
		<td class="sly-col-name sly-has-input"><div><input class="sly-form-text" type="text" size="10" id="sly-form-field-name" name="catname" value="" autofocus="autofocus" required="required" placeholder="<?php echo ht('category_name') ?>" /></div></td>
		<td class="sly-col-func sly-has-input"><div><input type="submit" class="sly-form-submit" value="<?php echo t('add') ?>" /></div></td>
	</tr>
	<?php
}

foreach ($children as $cat) {
	$id = $cat->getId();

	if ($action === 'edit' && $editID === $id) {
		?>
		<tr class="sly-active">
			<td class="sly-col-icon sly-icon"><?php echo sly_Util_HTML::getSpriteLink('', $cat->getName(), 'mediacat') ?></td>
			<td class="sly-col-name sly-has-input"><div><input class="sly-form-text" type="text" id="sly-form-field-name" name="catname" value="<?php echo sly_html($cat->getName()) ?>" autofocus="autofocus" required="required" placeholder="<?php echo ht('category_name') ?>" /></div></td>
			<td class="sly-col-func sly-has-input" colspan="2"><div><input type="submit" class="sly-form-submit" value="<?php echo t('save') ?>" /></div></td>
		</tr>
		<?php
	}
	else {
		$params  = $this->appendQueryString(array('cat_id' => $catID, 'edit_id' => $id));
		$rawEdit = $_router->getUrl(null, 'edit', $params, '&');
		$delete  = $_router->getUrl(null, 'delete', $params);
		$visit   = $_router->getUrl(null, null, $this->appendQueryString(array('cat_id' => $id)));
		?>
		<tr>
			<td class="sly-col-icon sly-icon"><?php echo sly_Util_HTML::getSpriteLink($rawEdit, $cat->getName(), 'mediacat') ?></td>
			<td class="sly-col-name"><a href="<?php echo $visit ?>"><?php echo sly_html($cat->getName()) ?></a></td>
			<td class="sly-col-func">
				<a href="<?php echo sly_html($rawEdit) ?>"><?php echo t('change')?></a>
				<a href="<?php echo $delete ?>" class="sly-action sly-delete sly-postlink"><?php echo t('delete') ?></a>
			</td>
		</tr>
		<?php
	}
}

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

if ($isForm) {
	print '</form>';
}
