<?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 extends sly_Controller_Backend implements sly_Controller_Interface {
	private $init;

	protected function init() {
		if ($this->init) return;
		$this->init = true;

		$this->buildMenu();

		// inject assets
		WV8_Settings::prepareHead();

		// print page title
		$layout = sly_Core::getLayout();
		$layout->pageHeader(t('wv8_title'));
	}

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

		$pages = WV8_Settings::getPages();

		if (empty($pages)) {
			$this->render('intro.phtml', array(), false);
		}
		else {
			$this->render('overview.phtml', array(), false);
		}
	}

	public function checkPermission($action) {
		$user = sly_Util_User::getCurrentUser();
		return $user && ($user->isAdmin() || $user->hasRight('pages', 'globalsettings'));
	}

	protected function getViewFolder() {
		return _WV8_PATH.'/templates/';
	}

	private function buildMenu() {
		// {settings_CRC32: page, settings_CRC32: page, ...}
		$nav      = sly_Core::getLayout()->getNavigation();
		$page     = $nav->find('globalsettings');
		$elements = WV8_Navigation::getNavElements();

		foreach ($elements as $el) {
			$subpage = $page->addSubpage($el['page'], $el['title']);
			if (isset($el['id'])) $subpage->setExtraParams(array('wv8page' => $el['id']));
		}
	}
}
