<?php
ob_start();
require_once("init.php");
if(!permission("sectionedit")){ echo error($langadmin['noperms']); exit();
}

$tmpl = new Koobi("templates/".ADMINTHEME."/sections/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('pref', $pref);
$tmpl->register_function("permission", "permission");

if(!isset($_REQUEST['action'])){ 
	if($_REQUEST['save']!=""){
		if(!permission("sectionedit")) { echo error($langadmin['nopermsectionedit']); exit();}
		
		foreach ($_POST['id'] as $id => $area_name) {
			$sql = new dbc("UPDATE ".PREFIX."_areas SET  pass='".$_REQUEST['pass'][$id]."', tmpl='".$_REQUEST['tmpl'][$id]."', active='".$_REQUEST['active'][$id]."', area_name='".strip_tags($_POST['area_name'][$id])."', area_descr='".$_REQUEST['area_descr'][$id]."', area_template='".$_REQUEST['area_template'][$id]."' WHERE area_id=$id ");
		}
		alog("alog_refreshsektion", "");
		
		
		// <<-- SEKTION LOESCHEN -->>
		if(count($_POST['del']) > 0){
			if(!permission("sectiondel")) { echo error($langadmin['nopermsectiondel']); exit();}
			foreach ($_POST['del'] as $id => $area) {
				if($_REQUEST['del'][$id]!=1){
					$sql = new dbc("DELETE FROM ".PREFIX."_areas WHERE area_id='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("ALTER TABLE ".PREFIX."_areas PACK_KEYS =0 CHECKSUM =0 DELAY_KEY_WRITE =0 AUTO_INCREMENT =1");
					$sql = new dbc("DELETE FROM ".PREFIX."_container WHERE area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_faq WHERE area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_news WHERE area_id='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_newscat WHERE area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_navi WHERE area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_permissions WHERE  section_id='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_speedbar WHERE  section='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_static WHERE  area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_gallery WHERE  area='".$_REQUEST['del'][$id]."'");
					$sql = new dbc("DELETE FROM ".PREFIX."_poll WHERE  area_id='".$_REQUEST['del'][$id]."'");
					alog("alog_deletesektion", ": ".$_REQUEST['del'][$id]);
				}
			}
		}
		header("Location:sections.php");
	}
	
	
	
	// ===============================================
	// <<-- SEKTION HOLEN -->>
	// ===============================================
	$sql = new dbc("SELECT * FROM ".PREFIX."_areas ORDER BY area_id ASC");
	$sections = array();
	while ($row = $sql->fetchrow()){
		
		unset($sel);
		
		$verzname = "../templates";
		$dh = opendir( $verzname );
		$sel = "";
		
		while ( gettype( $datei = readdir ( $dh )) != boolean )
		{
			
			if ( is_dir( "$verzname/$datei" ))
			if ($datei != "." && $datei != "..") {
				
				$sel .= "<option value=\"$datei\"";
				if($row->area_template == $datei){
					$sel .= " selected";
				}
				$sel .= ">" . strtoupper($datei) . "</option>";
				$datei = "";
				
			} 
		}
		$row->sel = $sel;
		$row->password = $row->pass;
		$row->tmpl = htmlspecialchars($row->tmpl); 
		
		array_push($sections, $row);
		
		closedir( $dh );
	}
	
	$tmpl->assign("sections", $sections);
	$tmpl->display("section_show.tpl");
	
	
}





if(isset($_REQUEST['action']) && $_REQUEST['action']=="new") {
	if(!permission("sectionedit")) { echo error($langadmin['nopermsectionedit']); exit();}
	if($_REQUEST['send']==1 && $_REQUEST['area_name']!=""){
		$sql = new dbc("INSERT INTO ".PREFIX."_areas (
		area_id,
		area_name,
		area_descr,
		area_template,
		active,
		pass,
		tmpl
		) VALUES (
		'',
		'".addslashes($_REQUEST['area_name'])."',
		'".addslashes($_REQUEST['area_descr'])."',
		'".$_REQUEST['area_template']."',
		'".$_REQUEST['active']."',
		'".$_REQUEST['pass']."',
		'".$_REQUEST['tmpl']."'
		)
		");
		$inid = $sql->getid();
		
		$sql = new dbc("SELECT * FROM ".PREFIX."_container WHERE area='1'");
		while($row = $sql->fetchrow()){
			$sql2 = new dbc("INSERT INTO ".PREFIX."_container (
			id,
			name,
			tpl,
			typ,
			area
			) VALUES (
			'$row->id',
			'$row->name',
			'$row->tpl',
			'$row->typ',
			'$inid'
			)
			");
		}
		
		alog("alog_adsektion", ": ".$inid."");
		header("Location:sections.php");
	} // 
	
	
	$verzname = "../templates";
	$dh = opendir( $verzname );
	$sel = "";
	
	while ( gettype( $datei = readdir ( $dh )) != boolean )
	{
		
		if ( is_dir( "$verzname/$datei" ))
		if ($datei != "." && $datei != "..") {
			
			$sel .= "<option value=\"$datei\">";
			
			$sel .= strtoupper($datei) . "</option>";
			$datei = "";
			
		} 
	}
	
	closedir( $dh );
	
	
	$tmpl->assign("sel", $sel);	
	$tmpl->assign("sections", $sections);
	$tmpl->display("section_new.tpl");
}
?>