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

if ( (!permission('productnew"')) && (!permission('productedit') )) { echo error($langadmin['noperms']); exit; }

$tmpl = new Koobi("templates/" . ADMINTHEME . "/products/");
$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'];
	}
	
	// <<-- PLATFORM -->>
	switch($_REQUEST['platform']){
		case "" : 
		case "all" : 
		$db_platform = ""; 
		$pf_once = "all";
		break;
		
		default : 
		$db_platform = " WHERE platform like '%".escs($_REQUEST['platform'])."%'"; 
		$db_pf = " AND (platform like '%".$_REQUEST['platform']."%')"; 
		$pf_once = $_REQUEST['platform'];
		break;
	} 
	// <<-- CHARAKTER -->>
	switch($_REQUEST['charakter']){
		case "1" :
		for($i=1;$i<10;$i++) { $loop .= " OR title  LIKE '".$i."%' $db_pf"; }
		$dbquery = " WHERE(title LIKE '0%' $loop)";
		$tmpl->assign('charakter', '0-9');
		break;
		
		case "" :
		case "all" :
		$dbquery = "";
		$tmpl->assign('charakter', $lang['all']);
		break;
		
		default :
		$dbquery = " WHERE(title LIKE '".$_REQUEST['charakter']."%') $db_pf ";
		$tmpl->assign('charakter', strtoupper($_REQUEST['charakter']));
		break;
	}
	
	if($_REQUEST['search']==1){
		$dbquery = "WHERE title like '%".$_REQUEST['product_q']."%' $db_pf ";
		$tmpl->assign('product_q', stripslashes($_REQUEST['product_q']));
	}
	
	
	$andarea = ($dbquery) ? " AND area = '$area'" : " WHERE area = '$area'" ;
	
	$sql = new dbc("SELECT id FROM ".PREFIX."_products $dbquery $andarea order by title asc");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_products $dbquery $andarea order by title $sort limit $a,$limit");
	$items = array();
	while( $row=$sql->fetchrow()){
		$row->user = fetchusername($row->uid);
		$row->mftime = mygmt($row->ctime);
		$row->platform = fetch_platform($row->platform,1);
		
		array_push($items,$row);
	}
	
	$navi = pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"product.php?platform=".$_REQUEST['platform']."&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->assign('getall_platforms', getall_platforms());
	$tmpl->display('product_overview.tpl');
	break;
	
	
	
	// <<-- NEU -->>
	case "new" :
	if(!permission('productnew')) { 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";
			}
		}
		$descr = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1){
			$descr = nl2br($_REQUEST['text2']);
		}
		$sql = new dbc("INSERT INTO ".PREFIX."_products (
		id,
		area,
		boxshot,
		title,
		subtitle,
		asin,
		release,
		genre,
		publisher,
		manufacturer,
		voting,
		shopurl,
		shop,
		price,
		inline_screens,
		platform,
		gallery,
		links,
		descr
		) VALUES (
		'',
		'$_REQUEST[area]',
		'".$icon."',
		'".strip_tags($_REQUEST['title'])."',
		'".strip_tags($_REQUEST['subtitle'])."',
		'".strip_tags($_REQUEST['asin'])."',
		'".strip_tags($_REQUEST['release'])."',
		'".$_REQUEST['genre']."',
		'".$_REQUEST['publisher']."',
		'".$_REQUEST['manufacturer']."',
		'".$_REQUEST['voting']."',
		'".strip_tags($_REQUEST['shopurl'])."',
		'".strip_tags($_REQUEST['shop'])."',
		'".strip_tags($_REQUEST['price'])."',
		'".addslashes(base64_decode($_REQUEST['inline_screens']))."',
		'".@implode(";", $_REQUEST['platforms'])."',
		'".@implode(";", $_REQUEST['galids'])."',
		'".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."',
		'".$descr."'
		)
		");
		alog("alog_productnew", ": ".$_REQUEST['title']."");
		#exit;
		header("Location:product.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	include("forms/dropdown.php");
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = "";
	include("forms/forminit.php");
	include("forms/dropdown.php");
	
	$tmpl->assign('new', 1);
	$tmpl->display('product_form.tpl');
	break;
	
	// <<-- BEARBEITEN -->>
	case "edit" :
	if(!permission('productedit')) { echo error($langadmin['noperms']); exit; }
	
	
	// <<-- AKTUALISIEREN -->>
	if($_REQUEST['save']==1){
		if($_REQUEST['imagedel']==1) {
			@unlink($target . $_REQUEST['oldimage']);
			$sql_in = " boxshot='', ";
		} else {
			$sql_in = " boxshot='$_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 = " boxshot='$fupload_name', ";
				@unlink($target . $_REQUEST['oldimage']);
			}
		}
		
		$descr = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1){
			$descr = nl2br($_REQUEST['text2']);
		}
		
		$sql = new dbc("UPDATE ".PREFIX."_products set 
		$sql_in 
		title = '".strip_tags($_REQUEST['title'])."',
		subtitle = '".strip_tags($_REQUEST['subtitle'])."',
		asin = '".strip_tags($_REQUEST['asin'])."',
		release = '".strip_tags($_REQUEST['release'])."',
		genre = '".$_REQUEST['genre']."',
		publisher = '".$_REQUEST['publisher']."',
		manufacturer = '".$_REQUEST['manufacturer']."',
		voting = '".$_REQUEST['voting']."',
		shopurl = '".strip_tags($_REQUEST['shopurl'])."',
		shop = '".strip_tags($_REQUEST['shop'])."',
		price = '".strip_tags($_REQUEST['price'])."',
		inline_screens = '".addslashes(base64_decode($_REQUEST['inline_screens']))."',
		platform = '".implode(";", $_REQUEST['platforms'])."',
		gallery = '".implode(";", $_REQUEST['galids'])."',
		links = '".str_replace(array("\r\n","\n"), ";", $_REQUEST['links'])."',
		descr = '".$descr."'
		WHERE id='".$_REQUEST['id']."'");
		alog("alog_productedit", ": ".$_REQUEST['title']."");
		header("Location:product.php?action=overview&area=".$_REQUEST['area']."&page=".$_REQUEST['page']."&pp=".$_REQUEST['pp']."&charakter=".$_REQUEST['charakter']."");
	}
	
	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_products  where id='".$_REQUEST['id']."'");
	$row = $sql->fetchrow();
	if(is_file($target . $row->boxshot)) { $img = $row->boxshot; }
	
	
	// <<-- PLATFORM -->>
	$sql = new dbc("SELECT id,platform FROM " . PREFIX . "_products WHERE id = '". $_REQUEST['id'] . "'" );
	$linked_platforms = $sql->fetchrow();
	$linked_platforms->id = explode(";", $linked_platforms->platform);
	$tmpl->assign("pforms", $linked_platforms);
	
	
	// <<-- VERKNÜPFTE GALERIEN -->>
	$r_navi = new dbc("SELECT id,gallery,inline_screens FROM " . PREFIX . "_products WHERE id = '". $_REQUEST['id'] . "'" );
	$row_linked_gals = $r_navi->fetchrow();
	$row_linked_gals->id = explode(";", $row_linked_gals->gallery);
	$tmpl->assign("linkedgals", $row_linked_gals);
	
	// <<-- INLINESHOTS -->>
	if($row_linked_gals->inline_screens){
		$tmpl->assign("inlineshots", base64_encode(serialize($row_linked_gals->inline_screens)));
	}
	
	// <<-- LINKS -->>
	$tmpl->assign("links", str_replace(";", "\n", $row->links));
	
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->descr;
	include("forms/forminit.php");
	include("forms/dropdown.php");
	
	
	
	$tmpl->assign('img', $img);
	$tmpl->assign('row', $row);
	$tmpl->assign('edit', 1);
	$tmpl->display('product_form.tpl');
	break;
	
	// <<-- LOESCHEN -->>
	case "del":
	if(!permission('productedit')) { echo error($langadmin['noperms']); exit; }
	$sql = new dbc("DELETE FROM ".PREFIX."_products where id='".$_REQUEST['id']."'");
	alog("alog_productdelete", ": ".$_REQUEST['name']."");
	header("Location:product.php?action=overview&area=".$_REQUEST['area']."");
	break;
}
?>