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

class sly_Controller_Globalsettings_Settings extends sly_Controller_Globalsettings_Saver {
	protected function init() {
		parent::init();

		$pageHash = $this->getCurrentPage();

		if ($pageHash === null) {
			throw new WV8_Exception('Die angegebene Unterseite wurde nicht gefunden.');
		}
	}

	protected function getCurrentPage() {
		$pageHash = sly_request('wv8page', 'string'); // CRC32 hash
		$pages    = WV8_Settings::getPages();

		foreach ($pages as $page) {
			if (WV8_Settings::hash($page) === $pageHash) {
				return $page;
			}
		}

		return null;
	}

	public function indexAction() {
		$this->init();

		// get groups to show
		$pagename = $this->getCurrentPage();
		$groups   = WV8_Settings::getGroups($pagename);

		// show groups
		$this->render('settings.phtml', compact('groups', 'pagename'), false);
	}

	public function saveAction() {
		$this->init();

		$pagename = $this->getCurrentPage();
		$settings = WV8_Settings::getSettings(null, $pagename);

		$this->saveList($settings);
		$this->indexAction();
	}
}
