<?php
ob_start();
require_once("init.php");
if((!isset($area)) || ($_REQUEST['area']=="")){
	$area = 1;
	}else{
	$area = $_REQUEST['area'];
}
$tmpl = new Koobi("templates/".ADMINTHEME."/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign('pref', $pref);


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


if($_REQUEST['action']=="showpiccomments"){

	// <<-- BEARBEITEN & SPEICHERN -->>
	if($_REQUEST['subaction']=="save") {
		foreach ($_POST["title"] as $id => $title) {
			$sql = new dbc("UPDATE " . PREFIX . "_gallerycomments SET title='".$_POST['title'][$id]."', comment='".$_POST['comment'][$id]."' WHERE id = $id");
		}
	// <<-- LOESCHEN -->>
	if($_POST['del']>0){
		foreach ($_POST["del"] as $id => $del) {
			$sql = new dbc("DELETE FROM " . PREFIX . "_gallerycomments WHERE id = $id");
		}
	  }
	}
		
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallerycomments  WHERE imgid='".escs($_REQUEST['img_id'])."' ORDER BY id DESC");
	$num = $sql->numrows();
	
	$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
	$tmpl->assign("theme", ADMINTHEME);
	
	$comments = array();
	while($row = $sql->fetchrow()){
		$row->ctime = mygmt($row->ctime);
		$row->author = fetchusername($row->autor);
		array_push($comments,$row);
	}
	$tmpl->assign('comments',$comments);
	$tmpl->display('comments.tpl');
	
}

// ======================================
// <<-- Galeriebilder anzeigen -->>
// ====================================== 
if($_REQUEST['action']=="showpics") {
	
	$limit = (!isset($_POST['pp'])) ? 10 : $limit = $_POST['pp'];
	
	if($_REQUEST['save']==1){
		
		foreach ($_POST["name"] as $id => $name) {
			$query = "UPDATE " . PREFIX . "_gallery_items SET name='$name',descr='".$_POST['descr'][$id]."' WHERE id = $id";
			$result = new dbc($query);
		}
		
		foreach ($_POST["del"] as $id => $del) {
			$sql = new dbc("SELECT path FROM " . PREFIX . "_gallery_items WHERE id = $id");
			$row = $sql->fetchrow();
			unlink("../uploads/galerie/" . $row->path);
			$sql = new dbc("DELETE FROM " . PREFIX . "_gallery_items WHERE id = $id");
		}
		header("Location:gallery.php?action=overview&area=".$_REQUEST['area']."&galid=".$_REQUEST['galid']."");
	}
	
	$gallery = array();
	$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
	$tmpl->assign("theme", ADMINTHEME);
	$sql = new dbc("SELECT id FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] ");
	
	$num = $sql->numrows();
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT descr,name,id,path,hits FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] order by id desc limit $a,$limit");
	
	$i = 0;
	while($row=$sql->fetchrow()){
		$sql2 = new dbc("SELECT id FROM ".PREFIX."_gallerycomments  WHERE imgid='$row->id' ");
		$num2 = $sql2->numrows();
		$gallery[$i]["cnum"] = $num2;
		$gallery[$i]["comments"] =  $num2 . ' ' . $lang['comments'];
		$gallery[$i]["image"] = "<img border=\"0\" vspace=\"0\" src=\"../system/thumb.php?img_id=".$row->id."\">";
		$gallery[$i]["descr"] = htmlspecialchars($row->descr);
		$gallery[$i]["name"] = htmlspecialchars($row->name);
		$gallery[$i]["id"] = $row->id;
		$gallery[$i]["hits"] = $row->hits;
		$i++;
	}
	if($i < 1){
		echo error($langadmin['error_nopics']);
		exit();
	}else{
		
		$tmpl->assign("galid", $_REQUEST['galid']);
		$tmpl->assign("area", $area);
		$tmpl->assign("lang", $langadmin);
		$tmpl->assign("gallery", $gallery);
		$tmpl->assign("pp", $_POST['pp']);
		$tmpl->assign("navi", pagenav($seiten, $lang['aktpos'], " <a class=\"pnav\" href=\"gallery.php?action=showpics&galid=".$_REQUEST['galid']."&area=".$area."&title=".$_REQUEST['title']."&pp=".$limit."&page={s}\">{t}</a> "));
		$tmpl->display("gallerypics.tpl"); 
	}
}


// ======================================
// <<-- Galeriebilder hinzufügen -->>
// ======================================
if($_REQUEST['action']=="newpics") {
	if(!permission("galleryaddpics"))  { echo error($langadmin['noperms']); exit(); }
	
	$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
	$tmpl->assign("theme", ADMINTHEME);
	if($_REQUEST['upload']==1){
		$tmpl->assign("status", upload($_FILES));
		$tmpl->assign("upload", 1);
		
	}
	$tmpl->assign("lang", $langadmin);
	$tmpl->assign("area", $area);
	$tmpl->assign("gallery", $gallery);
	$tmpl->assign("galdropdown", galdropdown(0,1,1));
	$tmpl->display("galleryupload.tpl");
}

// ======================================
// <<-- Galerieübersicht -->>
// ======================================
if($_REQUEST['action']=="overview") {
	if(!permission("gallery"))  { echo error($langadmin['noperms']); exit(); }
	$gallery = array();
	getGallery(0, "", $gallery, $area, $dummy, $dummy, $dummy);
	
	$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
	$tmpl->assign("theme", ADMINTHEME);
	$tmpl->assign("test", $gallery_array);
	$tmpl->assign("lang", $langadmin);
	$tmpl->assign("area", $area);
	$tmpl->assign("gallery", $gallery);
	$tmpl->assign("donow", $donow);
	
	if(permission("gallerynow")) { 
		$tmpl->assign("canactivate", 1);
	}
	
	$tmpl->display("galleryshow.tpl");
}



if ($_REQUEST["action"] == "save_posi") {
	foreach ($_POST["posi"] as $id => $posi) {
		if($posi != "") {
			$query = "UPDATE " . PREFIX . "_gallery SET posi=$posi WHERE id = $id";
			$result = new dbc($query);
		}
	}
	
	
	if(permission("gallerynow")) { 
		
		foreach ($_POST["active"] as $id => $active) {
			$sql = new dbc("SELECT uid FROM ".PREFIX."_gallery WHERE uid='".UID."' AND id=$id ");
			
			$num = $sql->numrows();
			$activate = 1;
			
			if( !permission("alles") ) {
				if( (!permission("galleryeditall")) && ($num<1) ){
					$activate = 0;
				}
			}
			
			if($activate == 1) {
				$query = "UPDATE " . PREFIX . "_gallery SET active=$active WHERE id = $id";
				$result = new dbc($query);
			}
			
		}
	}
	
	header("Location:gallery.php?action=overview&area=".$_REQUEST['area']."");
}


// ======================================
// <<-- Galerie anlegen -->>
// ======================================
if(!isset($_REQUEST['action'])){
	if(!permission("gallery")) { echo error($langadmin['noperms']); exit(); }
	if( ($_REQUEST['send']==1) && (!empty($_REQUEST['title'])) ){
		
		
		if($_REQUEST['autobr']==1) {
			$descr = nl2br($_REQUEST['descr']);
		} else {
			$descr = $_REQUEST['descr'];
		}
		
		
		if(!permission("gallerynow")) { 
			$active = 0;
		}else{
			$active = $_REQUEST['active'];
		}
		
		$sql = new dbc("INSERT INTO ".PREFIX."_gallery (
		ctime,
		maxwidth,
		cancomment,
		descr,
		id,
		parent_id,
		area,
		title,
		active,
		uid,
		matchword,
		product
		) VALUES (
		'".time()."',
		'$_REQUEST[maxwidth]',
		'$_REQUEST[cancomment]',
		'$descr',
		'',
		'$_REQUEST[thegal]',
		'$_REQUEST[area]',
		'".trim($_REQUEST['title'])."',
		'".$active."',
		'".UID."',
		'".strip_tags($_REQUEST['matchword'])."',
		'".$_REQUEST['product']."'
		) ");
		header("Location:gallery.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	
	// ========================================================
	// <<-- Galeriebilder löschen -->>
	// ========================================================
	if( ($_GET['del']==1) && ($_GET['galid']!=0) ){
		
		$sql = new dbc("SELECT uid FROM ".PREFIX."_gallery WHERE uid='".UID."' AND id='".$_GET['galid']."' ");
		$num = $sql->numrows();
		
		if( !permission("alles") ) {
			if( (permission("galleryedit")) && ($num<1) ) { echo error($langadmin['noperms']); exit(); }
			if( !permission("galleryedit")) { echo error($langadmin['noperms']); exit(); }
		}
		
		
		galdel($_REQUEST['galid']);
		
		header("Location:gallery.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	if($_REQUEST['edit']!=1) {
		if($_REQUEST['editsave']!=1){
			$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
			$tmpl->assign("theme", ADMINTHEME);
			$tmpl->assign("lang", $langadmin);
			$tmpl->assign("area", $area);
			$tmpl->assign("galdropdown", galdropdown(0,1,1));
			
			if(permission("gallerynow")) { 
				$tmpl->assign("active", 1);
			}
			// <<-- PRODUKTE -->>
			$sql = new dbc("SELECT title,id  FROM ".PREFIX."_products WHERE area='$area' order by title asc");
			$products = array();
			while( $row_pro=$sql->fetchrow()){
				array_push($products,$row_pro);
			}
			$tmpl->assign('products', $products);
			$tmpl->display("gallerynew.tpl");
		}
	}
	
	if($_REQUEST['editsave']==1){
		
		
		if($_REQUEST['autobr']==1) {
			$descr = nl2br($_REQUEST['descr']);
			$field_1_text = nl2br($_REQUEST['field_1_text']);
			$field_2_text = nl2br($_REQUEST['field_2_text']);
			$field_3_text = nl2br($_REQUEST['field_3_text']);
		} else {
			$descr = $_REQUEST['descr'];
			$descr = nl2br($_REQUEST['descr']);
			$field_1_text = $_REQUEST['field_1_text'];
			$field_2_text = $_REQUEST['field_2_text'];
			$field_3_text = $_REQUEST['field_3_text'];
		}
		
		// ======================================
		// <<-- Datensatz aktualisieren -->>
		// ======================================
		
		$sql = new dbc("UPDATE ".PREFIX."_gallery 
		SET 
		$sql_in title='".strip_tags($_REQUEST['title'])."', 
		descr='$descr', 
		maxwidth='$_REQUEST[maxwidth]',
		cancomment='$_REQUEST[cancomment]',
		active='$_REQUEST[active]',
		field_1_text = '$field_1_text',
		field_2_text = '$field_2_text',
		field_3_text = '$field_3_text',
		field_1 = '".strip_tags($_REQUEST['field_1'])."',
		field_2 = '".strip_tags($_REQUEST['field_2'])."',
		field_3 = '".strip_tags($_REQUEST['field_3'])."',
		product = '".$_REQUEST['product']."',
		matchword = '".strip_tags($_REQUEST['matchword'])."'
		WHERE id='$_REQUEST[id]' ");
		header("Location:gallery.php?action=overview&area=".$_REQUEST['area']."&galid=".$_REQUEST['id']."");
	}
	
	if($_REQUEST['edit']==1) {
		$sql = new dbc("SELECT * FROM ".PREFIX."_gallery WHERE id='$_GET[galid]' ");
		$row = $sql->fetchrow();
		
		$sql = new dbc("SELECT uid FROM ".PREFIX."_gallery WHERE id='$_GET[galid]' AND uid='".UID."' ");
		$num = $sql->numrows();
		
		
		
		if( !permission("alles") ) {
			if( (permission("galleryedit")) && ($num<1) ) { echo error($langadmin['noperms']); exit(); }
			if( !permission("galleryedit")) { echo error($langadmin['noperms']); exit(); }
		}
		
		
		if($_REQUEST['editsave']!=1){
			$tmpl = new Koobi("templates/".ADMINTHEME."/gallery/");
			$tmpl->assign("theme", ADMINTHEME);
			$tmpl->assign("lang", $langadmin);
			$tmpl->assign("area", $area);
			$tmpl->assign("row", $row);
			$tmpl->assign("active", $active);
			$tmpl->assign("img", $img);
			$tmpl->assign("descr", htmlspecialchars($row->descr));
			$tmpl->assign("title", stripslashes($row->title));
			$tmpl->assign("galid", $_GET['galid']);
			
			// <<-- PRODUKTE -->>
			$sql = new dbc("SELECT title,id  FROM ".PREFIX."_products WHERE area='$area'  order by title asc");
			$products = array();
			while( $row_pro=$sql->fetchrow()){
				array_push($products,$row_pro);
			}
			$tmpl->assign('products', $products);
			
			if($row->active==1) {
				$tmpl->assign("active", 1);
			} 
			if(permission("gallerynow")) { 
				$tmpl->assign("canactivate", 1);
			}
			
			
			$tmpl->display("galleryedit.tpl");
		}
	} 
} 
?>