<?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);
$lastFieldset = $firstGroup;
$form         = new sly_Form('index.php?page=globalsettings_partial&func=save', 'POST', $firstGroup, '', 'wv8partial_'.uniqid());
$list         = array();

$form->beginFieldset($firstGroup);
$fieldset = $form->getCurrentFieldset();

foreach ($groups as $key => $group) {
	if ($group != $lastFieldset) {
		$fieldset     = $form->beginFieldset($group);
		$lastFieldset = $group;
	}

	$settings = WV8_Settings::getSettings($namespace, null, $groupnames[$key]);

	foreach ($settings as $setting) {
		$fieldset->setAttribute('data-wv8id', $setting->getGroupID());
		$setting->renderAsFormElement($form);

		$list[] = $setting->getNamespace().'/'.$setting->getName();
	}
}

$form->addHiddenValue('wv8_settings_list', implode('$', $list));

$form = sly_Core::dispatcher()->filter('WV8_FORM', $form, array(
	'settings' => $list,
	'partial'  => true
));

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

$registry     = sly_Core::getTempRegistry();
$formRendered = $registry->get('globalsettings.partial', false);

if (!$formRendered) {
	$registry->set('globalsettings.partial', true);

	?>
	<script type="text/javascript">
	jQuery(function($) {
		$('form[id^=wv8partial_]').submit(function(evt) {
			return wv8.submitForm(evt, $(this), true);
		});
	});
	</script>
	<?
}
