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

$messages    = '<div id="messages">'.sly_Helper_Message::renderFlashMessage().'</div>';
$params      = array('id' => $article->getId(), 'clang' => $article->getClang(), 'article' => $article);
$form        = new sly_Form($_router->getPlainUrl(null, 'setarticletype'), 'POST', t('general'), '', 'content_article_form');
$hasType     = $article->hasType();
$hasTemplate = $article->hasTemplate();

/////////////////////////////////////////////////////////////////
// init form

$form->setEncType('multipart/form-data');
$form->addHiddenValue('article_id', $article->getId());
$form->addHiddenValue('clang', $article->getClang());

/////////////////////////////////////////////////////////////////
// article type

$type = new sly_Form_Select_DropDown('article_type', t('articletype'), $article->getType(), $articletypes, 'article_type');
$form->add($type);

// buttons
$button = new sly_Form_Input_Button('submit', 'save_article', t('submit'));
$button->addClass('sly-form-submit');
$form->setSubmitButton($button);

// additional form elements
$form = sly_Core::dispatcher()->filter('SLY_ART_META_FORM', $form, $params);

/////////////////////////////////////////////////////////////////
// header

?>
<div class="sly-content-header">
	<?php
	// navigation
	$this->render('content/_slotmenu.phtml',    array('article' => $article, 'slot' => $slot), false);
	$this->render('content/_subpagemenu.phtml', array('article' => $article), false);
	?>
</div>
<?php
if (!$localmsg) {
	print $messages;
}

if ($form instanceof sly_Viewable) {
	print $form->render();
}

if (!$hasType) {
	print sly_Helper_Message::warn(t('select_type_first'));
}
elseif (!$hasTemplate) {
	print sly_Helper_Message::info(t('no_template_defined'));
}
elseif ($slot === null) {
	print sly_Helper_Message::info(t('template_has_no_slots'));
}
elseif (empty($modules)) {
	print sly_Helper_Message::info(t('no_modules_allowed'));
}

$noContentAllowed = !$hasType || !$hasTemplate || $slot === null || empty($modules);
$articleSlices    = $article->getSlices($slot);

if ($noContentAllowed && !empty($articleSlices)) {
	print sly_Helper_Message::warn(t('slices_left'));
}

if (!$noContentAllowed) {
	print '<div class="sly-content-editmode">';

	for ($i = 0; $i <= count($articleSlices); ++$i) {
		try {
			if ($function === 'add' && $pos === $i) {
				if (!$this->checkPermission('addarticleslice', $module)) {
					print sly_Helper_Message::warn(t('no_rights_to_this_module'));
				}
				else {
					sly_Helper_Content::printAddSliceForm($module, $pos, $article->getId(), $article->getClang(), $slot, isset($slicevalues) ? $slicevalues : array());
				}
			}
			elseif (!$noContentAllowed) {
				$this->render('content/_add_slice_form.phtml', array('article' => $article, 'pos' => $i, 'slot' => $slot, 'modules' => $modules), false);
			}

			if ($pos === $i && $localmsg) {
				print $messages;
			}

			if (isset($articleSlices[$i])) {
				$artslice = $articleSlices[$i];

				if ($function === 'edit' && $slice_id === $artslice->getId()) {
					if (!$this->checkPermission('editarticleslice', $artslice->getModule())) {
						print sly_Helper_Message::warn(t('no_rights_to_this_module'));
					}
					else {
						if (!isset($slicevalues)) {
							$slicevalues = $artslice->getSlice()->getValues();
						}

						sly_Helper_Content::printEditSliceForm($artslice, $slicevalues);
					}
				}
				else {
					$this->render('content/_slice_toolbar.phtml', array('slice' => $artslice, 'noContentAllowed' => $noContentAllowed), false);
					$this->render('content/_slice_content.phtml', array('slice' => $artslice), false);
				}
			}
		}
		catch (Exception $e) {
			print sly_Helper_Message::warn(t('error_rendering_slice', $e->getMessage()));
		}
	}

	print '</div>';
}
