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

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

$tmpl = new Koobi("templates/" . ADMINTHEME . "/manufacturer/");
$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 "overview" :
	
	switch($_REQUEST['sort']==""){
		case "" : 
		case "asc" : $sort = "asc";
		default : $sort = $_REQUEST['sort'];
	}
	
	
	// <<-- CHARAKTER -->>
	switch($_REQUEST['charakter']){
		case "1" :
		for($i=1;$i<10;$i++) { $loop .= " OR name  LIKE '".$i."%'"; }
		$dbquery = " WHERE(name LIKE '0%' $loop)";
		$tmpl->assign('charakter', '0-9');
		break;
		
		case "" :
		case "all" :
		$dbquery = "";
		$tmpl->assign('charakter', $lang['all']);
		break;
		
		default :
		$dbquery = " WHERE(name LIKE '".$_REQUEST['charakter']."%')";
		$tmpl->assign('charakter', strtoupper($_REQUEST['charakter']));
		break;
	}
	
	if($_REQUEST['search']==1){
		$dbquery = "WHERE name like '%".$_REQUEST['mf']."%'";
	}
	
	$andarea = ($dbquery) ? " AND area = '$area'" : " WHERE area = '$area'" ;
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_manufacturer $dbquery $andarea order by name asc");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT ctime,uid,id,name,url,hits,ctime FROM ".PREFIX."_manufacturer $dbquery $andarea order by name $sort limit $a,$limit");
	$items = array();
	while( $row=$sql->fetchrow()){
		$row->website = $row->url;
		$pos = strpos ($row->website, "http://");
		if ($pos === false) { $row->website = "http://".$row->website; }
		$row->user = fetchusername($row->uid);
		$row->mftime = mygmt($row->ctime);
		array_push($items,$row);
	}
	
	$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"manufacturer.php?area=".$area."&sort=".$_REQUEST['sort']."&pp=".$limit."&charakter=".$_REQUEST['charakter']."&action=".$_REQUEST['action']."&pp=".$limit."&page={s}\">{t}</a> ");
	$tmpl->assign('navi', $navi);
	$tmpl->assign('pp', $limit);
	$tmpl->assign('sort', $sort);
	$tmpl->assign('items', $items);
	$tmpl->display('mf_overview.tpl');
	break;
	
	
	
	// <<-- NEU -->>
	case "new" :
	if(!permission('manufacturer_new')) { echo error($langadmin['noperms']); exit; }
	
	// <<-- NEU SPEICHERN -->>
	if($_REQUEST['save']==1){
		if($_FILES['file']['size']!=0) {
			if(	$_FILES['file']['type']=="image/pjpeg" || 
			$_FILES['file']['type']=="image/jpeg" || 
			$_FILES['file']['type']=="image/x-png" || 
			$_FILES['file']['type']=="image/png" || 
			$_FILES['file']['type']=="image/gif")
			{
				
				$fupload_name = ktrim($_FILES['file']['name'],1);
				$fupload_name = rand_tostring($target ,$fupload_name);
				
				move_uploaded_file($_FILES['file']['tmp_name'], $target . $fupload_name);
				@chmod($target . $fupload_name,0777);
				$icon= "$fupload_name";
			}
		}
		$text = ($_REQUEST['autobr1']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2'];
		
		$sql = new dbc("INSERT INTO ".PREFIX."_manufacturer (
		area,
		name,
		full_name,
		s_year,
		people,
		s_country,
		url,
		email,
		adress,
		descr,
		icon,
		ctime
		) VALUES (
		'$_REQUEST[area]',
		'".strip_tags($_REQUEST['name'])."',
		'".strip_tags($_REQUEST['full_name'])."',
		'".$_REQUEST['s_year']."',
		'".$_REQUEST['people']."',
		'".$_REQUEST['s_country']."',
		'".$_REQUEST['url']."',
		'".$_REQUEST['email']."',
		'".$_REQUEST['adress']."',
		'".$text."',
		'".$icon."',
		'".time()."'
		)
		");
		alog("alog_manufacturerad", ": ".$_REQUEST['name']."");
		header("Location:manufacturer.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = "";
	include("forms/forminit.php");
	
	$tmpl->assign('new', 1);
	$tmpl->display('mfform.tpl');
	break;
	
	// <<-- BEARBEITEN -->>
	case "edit" :
	if(!permission('manufactureredit')) { echo error($langadmin['noperms']); exit; }
	
	
	// <<-- AKTUALISIEREN -->>
	if($_REQUEST['save']==1){
		if($_REQUEST['imagedel']==1) {
			@unlink($target . $_REQUEST['oldimage']);
			$sql_in = " icon='', ";
		} else {
			$sql_in = " icon='$_REQUEST[oldimage]', ";
		}
		if($_FILES['file']['size']!=0) {
			if(	$_FILES['file']['type']=="image/pjpeg" || 
			$_FILES['file']['type']=="image/jpeg" || 
			$_FILES['file']['type']=="image/x-png" || 
			$_FILES['file']['type']=="image/png" || 
			$_FILES['file']['type']=="image/gif")
			{
				
				$fupload_name = ktrim($_FILES['file']['name'],1);
				$fupload_name = rand_tostring($target ,$fupload_name);
				
				move_uploaded_file($_FILES['file']['tmp_name'], $target . $fupload_name);
				@chmod($target . $fupload_name,0777);
				$sql_in = " icon='$fupload_name', ";
				@unlink($target . $_REQUEST['oldimage']);
			}
		}
		
		
		$text = ($_REQUEST['autobr1']==1) ? nl2br($_REQUEST['text2']) : $_REQUEST['text2'];
		
		$sql = new dbc("UPDATE ".PREFIX."_manufacturer set 
		$sql_in 
		name = '".strip_tags($_REQUEST['name'])."',
		full_name = '".strip_tags($_REQUEST['full_name'])."',
		s_year = '".$_REQUEST['s_year']."',
		people = '".$_REQUEST['people']."',
		s_country = '".$_REQUEST['s_country']."',
		url = '".$_REQUEST['url']."',
		email = '".$_REQUEST['email']."',
		adress = '".$_REQUEST['adress']."',
		descr = '".$text."'
		WHERE id='".$_REQUEST['id']."'");
		alog("alog_manufactureredit", ": ".$_REQUEST['name']."");
		header("Location:manufacturer.php?action=overview&area=".$_REQUEST['area']."&page=".$_REQUEST['page']."&pp=".$_REQUEST['pp']."&charakter=".$_REQUEST['charakter']."");
	}
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_manufacturer where id='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	
	
	if(is_file($target . $row->icon)) { $img = $row->icon; }
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->descr;
	include("forms/forminit.php");
	
	$tmpl->assign('img', $img);
	$tmpl->assign('row', $row);
	$tmpl->assign('edit', 1);
	$tmpl->display('mfform.tpl');
	break;
	
	// <<-- LOESCHEN -->>
	case "del":
	if(!permission('manufactureredit')) { echo error($langadmin['noperms']); exit; }
	$sql = new dbc("DELETE FROM ".PREFIX."_manufacturer where id='".$_REQUEST['id']."'");
	alog("alog_manufacturerdelete", ": ".$_REQUEST['name']."");
	header("Location:manufacturer.php?action=overview&area=".$_REQUEST['area']."");
	break;
}
?>