<?php
ob_start();
require_once("init.php");
if(!permission("autowords")){
	echo error($langadmin['noperms']);
	exit();
}
if($_REQUEST['action']=="edit"){
	$t = $langadmin['autowordedit'];
	$act = "edit";
	} else {
	$t = $langadmin['autowordnew'];
	$act = "new";
}

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

$limit = ($_REQUEST['pp']!="") ? $_REQUEST['pp'] : 15; 

if(!isset($page)){
	$page = 1;
}

if(!isset($_REQUEST['action'])){
	
	switch($_REQUEST['sort']){
		case "":
		case "name":
		$ord = " ORDER BY words ASC";
		break;
		
		case "hits":
		$ord = "ORDER BY hits DESC";
		break;
	}
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_autowords");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_autowords $ord limit $a,$limit");
	$erg = $sql->numrows();
	
	if((!$erg) && ($num)){
		$p = $_REQUEST['page']-1;
		header("Location:enzyclop.php?page=".$p."");
	}
	
	$autowords = array();
	while ($row=$sql->fetchrow()){
		array_push($autowords,$row);
	}
	
	$tmpl->assign("num", $num);
	$tmpl->assign("limit", $limit);
	$tmpl->assign("autowords", $autowords);
	$tmpl->assign("navi", pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"enzyclop.php?sort=$_REQUEST[sort]&amp;pp=$limit&amp;page={s}\">{t}</a> "));
	$tmpl->display("enzyclop.tpl");
}

if($_REQUEST['action']=="del"){
	$sql = new dbc("DELETE FROM ".PREFIX."_autowords WHERE id='".$_REQUEST['id']."'");
	header("Location:enzyclop.php?page=".$_REQUEST['page']."");
}


if(isset($_REQUEST['action']) && $_REQUEST['action']=="edit") {
	if($_REQUEST['send']==1){
		
		
		$text = ($_REQUEST['autobr2']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2']; 
		if(!empty($text)){
			$sql = new dbc("UPDATE ".PREFIX."_autowords 
			SET 
			words='".strip_tags($_REQUEST['words'])."', 
			text='".$text."' 
			WHERE id='".$_REQUEST['id']."'");
			header("Location:enzyclop.php?page=".$_REQUEST['page']."");
		}
		
	}
	$sql = new dbc("SELECT * FROM ".PREFIX."_autowords WHERE id='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->text;
	include("forms/forminit.php");
	//$tmpl->assign('ed1', $ed2);
	
	$tmpl->assign("words", htmlspecialchars($row->words));
	$tmpl->display("enzyclop_form.tpl");
}

if(isset($_REQUEST['action']) && $_REQUEST['action']=="new") {
	if($_REQUEST['send']==1 && $_REQUEST['words']!=""){
		
		$text = ($_REQUEST['autobr2']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2']; 
		
		$sql = new dbc("INSERT INTO ".PREFIX."_autowords(id,words,text) VALUES ('','".strip_tags($_REQUEST['words'])."','".$text."')");
		header("Location:enzyclop.php");
	}
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->text;
	include("forms/forminit.php");
	$tmpl->assign('ed1', $ed);
	
	$tmpl->assign("words", htmlspecialchars($row->words));
	$tmpl->display("enzyclop_form.tpl");
	
}
?>