<?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
 */

$addURI   = $canAdd ? $_router->getPlainUrl(null, 'addArticle', array('category_id' => $categoryId, 'clang' => $clangId)) : '';
$table    = new sly_Table('sly-structure-articles');
$isActive = $renderAddArticle || $renderEditArticle;

$table->addColumn(sly_Table_Column::sprite('article-add', $addURI, t('add_article'), 'sly-col-icon'));
$table->addColumn(sly_Table_Column::factory(t('article_name'), 'sly-col-name'));
$table->addColumn(sly_Table_Column::factory(t('articletype'), 'sly-col-arttype'));

if ($isActive) {
	$table->addClass('sly-active');
	$table->addColumn(sly_Table_Column::factory(t('position_short'), 'sly-col-pos'));
}

$table->addColumn(sly_Table_Column::factory(t('status'), 'sly-col-status'));
$table->setIsEmpty(empty($articles) && !$renderAddArticle);
$table->setEmptyNotice(t('no_articles_yet'));

if ($isActive) {
	?>
	<form action="<?php print $_router->getUrl(null, $action) ?>" method="post">
		<fieldset>
			<input type="hidden" name="category_id" value="<?php echo $categoryId ?>" />
			<input type="hidden" name="clang" value="<?php echo $clangId ?>" />
			<?php print sly_Util_Csrf::renderInputTag() ?>
		</fieldset>
		<?php
}

$table->openBuffer();

if ($renderAddArticle) {
	$this->render(
		sly_Controller_Structure::$viewPath.'article_table_row_add.phtml',
		array('maxPosition' => $maxPosition),
		false
	);
}

foreach ($articles as $article) {
	if ($renderEditArticle && $renderEditArticle == $article->getId()) {
		$this->render(
			sly_Controller_Structure::$viewPath.'article_table_row_edit.phtml',
			array('article' => $article, 'maxPosition' => $maxPosition),
			false
		);
	}
	else {
		$this->render(
			sly_Controller_Structure::$viewPath.'article_table_row.phtml',
			array(
				'article' => $article, 'statusTypes' => $statusTypes,
				'canEdit' => $canEdit, 'isActive' => $isActive
			),
			false
		);
	}
}

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

if ($isActive) {
	?>
	</form>
	<?php
}
