<?php
ob_start(); 
require_once("init.php"); 
$limit = limit();
$incdir = dirname(__FILE__);
define("INC", $incdir);

if ( !permission('banner') ) { echo error($langadmin['noperms']); exit; }

$tmpl = new Koobi("templates/" . ADMINTHEME . "/banner/");
$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");

// <<-- UEBERSICHT -->>
switch($_REQUEST['action']){
	case "" :
	case "random" :
		switch($_REQUEST['do']){
		
			// ERWEITERETS BEARBEITEN
			case "showcode":
				if($_REQUEST['sub']=='save'){
				$pcode = ($_POST['autobr']==1) ? nl2br($_POST['text2']) : $_POST['text2'];
				$weight = ($_POST['weight']=='') ? 1 : $_POST['weight'] ;
				$sql = new dbc("UPDATE " . PREFIX . "_banner SET views='".$_POST['views']."', weight='$weight', name='".$_POST['name']."', code='".$pcode."' WHERE id = $id");
				 header("Location:banner.php?area=$_REQUEST[area]");
				}
				
				$sql = new dbc("SELECT * FROM ".PREFIX."_banner WHERE id = '$_REQUEST[id]'");
				$row = $sql->fetchrow();
				$fck_value = $row->code;
				include("forms/forminit.php");
				
				$tmpl->assign('name', kspecialchars($row->name));
				$tmpl->assign('row', $row);
				$tmpl->display('edit_ext.tpl');
				exit;
			break;
			
			// NEUEN ZUFALLSCODE SPEICHERN
			case "new" :
			$text = ($_REQUEST['autobr']==1) ? nl2br($_REQUEST['code']) : $_REQUEST['code'];
			$name = kspecialchars($_REQUEST['name']);
			$sql = new dbc("INSERT INTO " . PREFIX . "_banner (area,active,id,name,code,weight) VALUES ('$_REQUEST[area]','1','','$name','$text','$_REQUEST[weight]') ");
			break;
			
			// LÖSCHEN
			case "del" :
			$sql = new dbc("DELETE FROM ".PREFIX."_banner WHERE id='$_REQUEST[id]'");
			break;
			
			// SPEICHERN ... AKTUALISIEREN
			case "save":
			 if($_POST['name']>=1){
			 foreach ($_POST['name'] as $id => $name) {
			 $pcode = ($_POST['autobr'][$id]==1) ? nl2br($_POST['code'][$id]) : $_POST['code'][$id];
			 $active = ($_POST['active'][$id]==1) ? "active='1', " : "active='', ";
			 $sql = new dbc("UPDATE " . PREFIX . "_banner SET $active views='".$_POST['hits'][$id]."', weight='".$_POST['weight'][$id]."', name='".$_POST['name'][$id]."', code='".$pcode."' WHERE id = $id");
			 }
			}
			break;
		}
			$items = array();
			$sql = new dbc("SELECT * FROM ".PREFIX."_banner WHERE area='$_REQUEST[area]'");
			while ( $row = $sql->fetchrow() ){
				$row->code = kspecialchars($row->code);
				array_push($items,$row);
			}
			
			$tmpl->assign('items', $items);
			$tmpl->display('banner_overview.tpl');
	break;

	

}
?>