<?php
ob_start();
require_once("init.php");
if ( !permission('settings')  ) { echo error($langadmin['noperms']); exit; }
$tmpl = new Koobi("templates/" . ADMINTHEME . "/permissions/");

$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("lang", $lang);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign('pref', $pref);

// ======================================
//  <<-- ÜBERSICHT -->>
// ======================================
if ($_GET["a"] == "view") {	
	// ======================================
	//  <<-- SEKTIONEN -->>
	// ======================================
	$q_sections = new dbc("
	SELECT
	area_id,
	area_name
	FROM 
	" . PREFIX . "_areas
	");
	
	$sections = array();
	
	while ($section = $q_sections->fetchrow()) {
		array_push($sections, $section);
	}
	
	// ======================================
	//  <<-- BEREICHE -->>
	// ======================================
	
	// bereiche sind nicht dynamisch
	// werden also statisch im template ausgegeben
	
	// ======================================
	//  <<-- GRUPPEN -->>
	// ======================================
	
	$q_groups = new dbc("
	SELECT
	ugroup,
	groupname
	FROM
	" . PREFIX . "_usergroup
	");
	
	$groups = array();
	
	while ($group = $q_groups->fetchrow()) {
		array_push($groups, $group);
	}	
	
	// ======================================
	//  template zuweisungen
	// ======================================
	
	$tmpl->assign("sections", $sections);
	$tmpl->assign("groups", $groups);
	
	$tmpl->display("permissions.tpl");
}



// ======================================
//  <<-- BEARBEITEN -->>
// ======================================
if ($_GET["a"] == "edit") {
	$section_id = $_GET["sid"];
	$group_id = $_GET["gid"];
	
	if ($section_id != "" && $group_id != "") {
		
		$q_permission = new dbc("
		SELECT
		permissions
		FROM 
		" . PREFIX . "_permissions
		WHERE
		group_id = $group_id AND
		section_id = $section_id
		");
		
		if (!$q_permission->numrows()) {
			$insert = new dbc("
			INSERT INTO
			" . PREFIX . "_permissions (group_id, section_id)
			VALUES
			($group_id, $section_id)
			");
		}
		
		$permissions = $q_permission->fetchrow();
		$permissions = explode("|", $permissions->permissions);
		
		$tmpl->assign("permissions", $permissions);
		$tmpl->assign("group_id", $group_id);
		$tmpl->assign("section_id", $section_id);
		$tmpl->assign("lang", $langadmin);
		$tmpl->display("permissions_edit.tpl");
	} else {
		// FEHLER
	}
	
}


// ======================================
//  <<-- SPEICHERN -->>
// ======================================
if ("apply" == $_GET["a"]) {
	if (is_array($_POST["permissions"])) {
		$permissions = implode("|", $_POST["permissions"]);
	} else {
		$permissions = "";
	}
	
	// Administratoren
	if (1 == $_POST["gid"]) $permissions = "alles";
	
	$result = new dbc("
	UPDATE 
	" . PREFIX . "_permissions 
	SET 
	permissions = '$permissions' 
	WHERE 
	group_id = " . $_POST["gid"] . " AND
	section_id = " . $_POST["sid"]
	);
	header("Location:permissions.php?a=view");
}
?>
