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

$config    = sly_Core::config();
$locales   = $this->getBackendLocales();
$yesNo     = array('true' => t('yes'), 'false' => t('no'));
$types     = array('' => t('no_articletype'));
$languages = sly_Util_Language::findAll();

foreach ($languages as $id => $language) {
	$languages[$id] = $language->getName();
}

print sly_Helper_Message::renderFlashMessage();

// build form

$form = new sly_Form($_router->getPlainUrl(null, 'update'), 'POST', '', '', 'sly-form-system-setup');
$form->setSubmitButton(new sly_Form_Input_Button('submit', 'sendit', t('update')));
$form->setApplyButton();
$form->setResetButton();

// build form elements

try {
	$typeService = sly_Service_Factory::getArticleTypeService();
	$types       = array_merge($types, $typeService->getArticleTypes());
}
catch (Exception $e) {
	// pass...
}

foreach ($types as $key => $name) {
	$types[$key] = sly_translate($name);
}

$timezoneSelect  = sly_Form_Helper::getTimezoneSelect();
$projectName     = new sly_Form_Input_Text('projectname', t('project_name'), sly_Core::getProjectName());
$startArticle    = new sly_Form_Widget_Link('start_article', t('start_article'), sly_Core::getSiteStartArticleId());
$notFound        = new sly_Form_Widget_Link('notfound_article', t('not_found_article'), sly_Core::getNotFoundArticleId());
$backendLocale   = new sly_Form_Select_DropDown('backend_locale', t('backend_locale'), sly_Core::getDefaultLocale(), $locales);
$defaultType     = new sly_Form_Select_DropDown('default_type', t('default_articletype'), sly_Core::getDefaultArticleType(), $types);
$cachingStrategy = new sly_Form_Select_DropDown('caching_strategy', t('caching_strategy'), sly_Core::getCachingStrategy(), sly_Cache::getAvailableCacheImpls());
$developerMode   = new sly_Form_Input_Checkbox('developer_mode', t('developer_mode'), '1', t('enabled'));

if (count($languages) === 1) {
	$form->addHiddenValue('default_clang', sly_Core::getDefaultClangId());
	$defaultClang = new sly_Form_Text(t('default_language'), sly_Core::getCurrentLanguage()->getName());
}
else {
	$defaultClang = new sly_Form_Select_DropDown('default_clang', t('default_language'), sly_Core::getDefaultClangId(), $languages);
}

$buttonBar = new sly_Form_ButtonBar(array(
	'submit' => new sly_Form_Input_Button('submit', 'sendit', t('update'))
));

// configure elements

$projectName->setRequired();
$startArticle->setRequired();
$notFound->setRequired();
$developerMode->setChecked(sly_Core::isDeveloperMode());

// assemble form

$form->addElements(array($projectName, $startArticle, $notFound, $defaultType, $defaultClang, $developerMode, $backendLocale, $cachingStrategy, $timezoneSelect));

// caching form

$caches = array(
	'sly_core'          => t('sally_core_cache'),
	'sly_asset'         => t('sally_assetcache'),
	'sly_develop'       => t('sally_develop_content'),
	'sly_reinit_addons' => t('reinit_all_addon_assets')
);

$selected = array_keys($caches);
unset($selected[3]); // do not re-init all addOns by default

$caches = new sly_Form_Select_Checkbox('caches', '', $selected, $caches);
$caches = sly_Core::dispatcher()->filter('SLY_SYSTEM_CACHES', $caches);

?>

<div class="sly-area-col-2">
	<div class="sly-area col-a">
		<h2><?php print t('features') ?></h2>

		<div class="sly-area-content">
			<h4><?php print t('delete_cache') ?></h4>
			<p><?php print t('delete_cache_description') ?></p>

			<form method="post" action="<?php print $_router->getUrl(null, 'clearcache') ?>">
				<p>
					<?php print sly_Util_Csrf::renderInputTag() ?>
					<input type="submit" class="sly-button" value="<?php print t('delete_cache') ?>" />
					<a href="#" id="sly-system-toggle-cache">(&hellip;)</a>
				</p>

				<div class="sly-form" id="sly-form-system-caches">
					<p class="sly-form-col-a sly-form-select-row"><?php print $caches->render(); ?></p>
				</div>
			</form>

			<?php if ($hasSetupApp): ?>
			<h4><?php print t('setup') ?></h4>
			<p><?php print t('setup_text') ?></p>
			<p class="button"><a class="sly-button sly-postlink sly-confirm-me" href="<?php print $_router->getUrl(null, 'setup') ?>"><?php print t('setup') ?></a></p>
			<?php endif ?>

			<h4><?php print t('version') ?></h4>
			<p>SallyCMS <?php print sly_Core::getVersion() ?> &ndash; PHP <?php print PHP_VERSION ?></p>

			<h4><?php print t('database_connection') ?></h4>
			<p><?php
			$database = $config->get('DATABASE');
			$pdo      = sly_DB_Persistence::getInstance()->getPDO();
			$version  = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);

			if (strtolower($database['DRIVER']) === 'sqlite') {
				print sly_html(sprintf('%s.sq3 (v%s)', $database['NAME'], $version));
			}
			else {
				print sly_html(sprintf('%s @ %s/%s (v%s)', $database['LOGIN'], $database['HOST'], $database['NAME'], $version));
			}

			if (strtolower($database['DRIVER']) !== 'mysql') {
				printf(' (%s)', strtolower($database['DRIVER']));
			}
			?>
		</div>
	</div>

	<div class="sly-area col-b">
		<h2><?php print t('settings') ?></h2>

		<div class="sly-area-content">
			<?php print $form->render() ?>
		</div>
	</div>
</div>
