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

$groupnames = $groups; // untranslated!
$groups     = WV8_Settings::translateList($groups);

$firstGroup   = reset($groups);
$lastGroup    = end($groups);
$lastFieldset = '';
$form         = new sly_Form('index.php?page=globalsettings_settings&func=save', 'POST', $firstGroup, '', 'wv8');

$form->addHiddenValue('wv8page', WV8_Settings::hash($pagename));

foreach ($groups as $key => $group) {
	// get settings on this page and in this group
	$settings = WV8_Settings::getSettings(null, $pagename, $groupnames[$key]);
	$rendered = 0;

	foreach ($settings as $setting) {
		if ($setting->getData('hidden')) continue;

		if ($rendered === 0 && $group !== $lastFieldset) {
			$fieldset = $form->beginFieldset($group, $group == $lastGroup ? 'wv8-last-fieldset' : '');
			$fieldset->setAttribute('data-wv8id', $setting->getGroupID());

			$lastFieldset = $group;
			$rendered     = 1;
		}

		$setting->renderAsFormElement($form);
	}
}

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

$submit = new sly_Form_Input_Button('submit', 'submit', t('save'));
$submit->addClass('sly-form-submit');
$reset  = new sly_Form_Input_Button('reset', 'reset', t('reset'));
$bar    = new sly_Form_ButtonBar(compact('submit', 'reset'));

$bar->addFormRowClass('wv8-dont-hide');
$form->add($bar);

$form = sly_Core::dispatcher()->filter('WV8_FORM', $form, array(
	'groups'  => $groupnames,
	'partial' => false
));

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

?>
<script type="text/javascript">
jQuery(function($) {
	$('form#wv8').submit(function(evt) {
		return wv8.submitForm(evt, $(this), false);
	});
});
</script>
