<?php
class Products
{
	function showGenres()
	{
		if(perm('genres'))
		{
			//=================================================================
			// Neu
			//=================================================================
			if(isset($_POST['new']) && $_POST['new']==1)
			{
				foreach($_POST['Name'] as $nn => $nid)
				{
					if(!empty($_POST['Name'][$nn])) $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_genre (Id,Name,Sektion) VALUES ('','" . $_POST['Name'][$nn] . "','" . AREA . "')");
				}
			}
			
			//=================================================================
			// Speichern
			//=================================================================
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach($_POST['Name'] as $cat => $nid)
				{
					 if(!empty($_POST['Name'][$cat])) $GLOBALS['db']->Query("UPDATE " . PREFIX . "_genre SET Name='" . $_POST['Name'][$cat] . "' WHERE Id='$cat'");
					// Loeschen?
					if(isset($_POST['del'][$cat]) && $_POST['del'][$cat]==1 && perm('genres'))
					{
						$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_genre WHERE Id='$cat'");
						Logger('User ' . $_SESSION['user_name'] . ' delete Genre(' . $_POST['Name'][$cat]  . ')', '3', $_SESSION['benutzer_id']);
					}
				}
			}
			
			
			$pf = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_genre WHERE Sektion='" . AREA . "' ORDER BY Name ASC");
			while($row=$sql->fetchrow())
			{
				array_push($pf,$row);
			}
			$GLOBALS['tmpl']->assign('genres',$pf);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Products_genres']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/genres.tpl'));
		} else {
			no_perm();
		}
	}
	
	function DelRating($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_wertung WHERE Bereich='products' AND Objekt_Id='{$id}'");
		echo "<script>window.opener.location.reload(); window.close();</script>";
	}
	
	//=================================================================
	// Einstellungen
	//=================================================================
	function Settings()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_produkte_einstellungen
			SET 
				Kommentare = '" . $_POST['Kommentare'] . "',
				Wertung = '" . $_POST['Wertung'] . "'
			");
		}
		
		$GLOBALS['tmpl']->assign('res', $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_produkte_einstellungen")); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Products_Settings']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/settings.tpl'));
	}
	
	
	//=================================================================
	// Seite loeschen
	//=================================================================
	function deleteProduct($id)
	{
		echo '<script>window.opener.location.reload(); window.close();</script>';
	}
	
	function getGenre($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT Name,Id FROM " . PREFIX . "_genre  WHERE Id='{$id}'");
		if(!is_object($res)) return;
		$name = koobispecialchars($res->Name);
		return $name;
	}
	
	function getManufaturer()
	{
		$mf = array();
		$res = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_hersteller ORDER BY Name ASC");
		while($row = $res->fetchrow())
		{
			array_push($mf,$row);
		}
		return $mf;
	}
	
	function Genres()
	{
		$genres = array();
		$query = $GLOBALS['db']->Query("SELECT Name,Id FROM " . PREFIX . "_genre WHERE Sektion='" . AREA . "' ORDER BY Name ASC");
		while($row = $query->fetchrow())
		{
			array_push($genres,$row);
		}
		return $genres;
	}
	
	//=================================================================
	// Uebersicht
	//=================================================================
	function showProducts()
	{
		$def_order    = 'ORDER BY Id DESC';
		$def_order_n  = '';
		$def_search   = '';
		$def_status   = '';
		$def_status_n = '';
		$def_genre    = '';
		$def_genre_n  = '';
		$def_search_n = '';
		$def_datetill = '';
		$def_datetill_n = '';
		$def_hits     = '';
		$def_hits_n   = '';
		$def_istop    = '';
		$def_istop_n  = '';
		$products         = array();
		$area         = $_SESSION['a_area'];
		$limit        = GetLimit();
		
		// Schnellspeichern
		if(isset($_POST['quicksave']) && $_POST['quicksave']==1)
		{
			foreach($_POST['nid'] as $cat => $nid)
			{
				 $GLOBALS['db']->Query("UPDATE 
				 	" . PREFIX . "_produkte 
				SET 
					TopProduct = '" . $_POST['TopProduct'][$nid] . "', 
					Genre ='" . $_POST['Genre'][$nid] . "',
					Aktiv ='" . $_POST['Aktiv'][$nid] . "',
					Hits ='" . $_POST['Hits'][$nid] . "' 
				WHERE Id='$nid'");
				
				// Loeschen?
				if(isset($_POST['del'][$nid]) && $_POST['del'][$nid]==1 && perm('products_del'))
				{
					 $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_produkte WHERE Id='$nid'");
				}
			}
		}
		
		// Genre
		if(isset($_REQUEST['genre']) && $_REQUEST['genre']!='0')
		{
			$def_genre = "AND Genre = '" . abs((int)$_REQUEST['genre']) . "'";
			$def_genre_n = "&amp;genre=" . abs((int)$_REQUEST['genre']);
		}
		
		// Topprodukt
		if(isset($_REQUEST['istop']) && $_REQUEST['istop']!='')
		{
			$def_istop = "AND TopProduct = '" . abs((int)$_REQUEST['istop']) . "'";
			$def_istop_n = "&amp;istop=" . abs((int)$_REQUEST['istop']);
		}
		
		// Hits
		if(isset($_REQUEST['hits_from']) && isset($_REQUEST['hits_to']) && $_REQUEST['hits_from']<$_REQUEST['hits_to'])
		{
			$def_hits = "AND (Hits BETWEEN '" . abs((int)$_REQUEST['hits_from']) . "' AND '" . abs((int)$_REQUEST['hits_to']) . "')";
			$def_hits_n = "&amp;hits_from=" . abs((int)$_REQUEST['hits_from']) . "&amp;hits_to=" . abs((int)$_REQUEST['hits_to']);
		}
		
		// Status
		if(isset($_REQUEST['aktiv']) && $_REQUEST['aktiv']!='')
		{
			$def_status = "AND Aktiv = '" . abs((int)$_REQUEST['aktiv']) . "'";
			$def_status_n = "&amp;aktiv=" . abs((int)$_REQUEST['aktiv']);
		}
		
		// Datum erstellt bis
		if(!empty($_REQUEST['date_till']) && !empty($_REQUEST['date_till']))
		{
			$lafrom = @explode('.',$_REQUEST['date_till']);
			$lafrom = @mktime(23,59,59,$lafrom[1],$lafrom[0],$lafrom[2]);
			
			if($lafrom)
			{
				$def_datetill = " AND (Datum <= $lafrom) ";
				$def_datetill_n = "&amp;date_till=" . $_REQUEST['date_till'];
			}
		}
		
		
		// Suche wurde gestartet...
		if(isset($_REQUEST['q']) && !empty($_REQUEST['q']) && strlen($_REQUEST['q'])>=1)
		{
			$q = trim(preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9.@ ]*)/', '', $_REQUEST['q']));
			$_REQUEST['q'] = $q;
			
			$def_search = " 
			AND (
				Name1 LIKE '%$q%' OR 
				Name2 LIKE '%$q%' OR 
				Name3 LIKE '%$q%' 
			)";
			$def_search_n = "&amp;q={$q}";
		}
		
		// Sortierungen
		if(isset($_REQUEST['sort']))
		{
			$curr_page = ((isset($_REQUEST['page'])) ? '&page=' . abs((int)($_REQUEST['page'])) : '&amp;page=1');
			
			switch($_REQUEST['sort'])
			{
				case 'name_asc' : 
					$def_order    = ' ORDER BY Name1 ASC';
					$def_order_n  = '&sort=name_asc' . $curr_page;
					$def_order_ns = '&sort=name_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('name_s',$def_order_ns);
				break;
				
				case 'name_desc': 
					$def_order    = ' ORDER BY Name1 DESC'; 
					$def_order_n  = '&sort=name_desc' . $curr_page;
					$def_order_ns = '&sort=name_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('name_s',$def_order_ns);
				break;
				
				case 'username_asc' : 
					$def_order    = ' ORDER BY Benutzer ASC';
					$def_order_n  = '&sort=username_asc' . $curr_page;
					$def_order_ns = '&sort=username_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('username_s',$def_order_ns);
				break;
				
				case 'username_desc' : 
					$def_order    = ' ORDER BY Benutzer DESC';
					$def_order_n  = '&sort=username_desc' . $curr_page;
					$def_order_ns = '&sort=username_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('username_s',$def_order_ns);
				break;
				
				
				case 'date_asc' : 
					$def_order    = ' ORDER BY Datum ASC';
					$def_order_n  = '&sort=date_asc' . $curr_page;
					$def_order_ns = '&sort=date_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('date_s',$def_order_ns);
				break;
				
				case 'date_desc' : 
					$def_order    = ' ORDER BY Datum DESC';
					$def_order_n  = '&sort=date_desc' . $curr_page;
					$def_order_ns = '&sort=date_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('date_s',$def_order_ns);
				break;
				
				case 'hits_asc' : 
					$def_order    = ' ORDER BY Hits ASC';
					$def_order_n  = '&sort=hits_asc' . $curr_page;
					$def_order_ns = '&sort=hits_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('hits_s',$def_order_ns);
				break;
				
				case 'hits_desc' : 
					$def_order    = ' ORDER BY Hits DESC';
					$def_order_n  = '&sort=hits_desc' . $curr_page;
					$def_order_ns = '&sort=hits_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('hits_s',$def_order_ns);
				break;
				
				case 'genre_desc' : 
					$def_order    = ' ORDER BY Genre DESC';
					$def_order_n  = '&sort=genre_desc' . $curr_page;
					$def_order_ns = '&sort=genre_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('genre_s',$def_order_ns);
				break;
				
				case 'genre_asc' : 
					$def_order    = ' ORDER BY Genre ASC';
					$def_order_n  = '&sort=genre_asc' . $curr_page;
					$def_order_ns = '&sort=genre_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('genre_s',$def_order_ns);
				break;
				
				case 'top_desc' : 
					$def_order    = ' ORDER BY TopProduct DESC';
					$def_order_n  = '&sort=top_desc' . $curr_page;
					$def_order_ns = '&sort=top_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('top_s',$def_order_ns);
				break;
				
				case 'top_asc' : 
					$def_order    = ' ORDER BY TopProduct ASC';
					$def_order_n  = '&sort=top_asc' . $curr_page;
					$def_order_ns = '&sort=top_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('top_s',$def_order_ns);
				break;
				
				
				case 'active_desc' : 
					$def_order    = ' ORDER BY Aktiv DESC';
					$def_order_n  = '&sort=active_desc' . $curr_page;
					$def_order_ns = '&sort=active_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('active_s',$def_order_ns);
				break;
				
				case 'active_asc' : 
					$def_order    = ' ORDER BY Aktiv ASC';
					$def_order_n  = '&sort=active_asc' . $curr_page;
					$def_order_ns = '&sort=active_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('active_s',$def_order_ns);
				break;
				
			}
		}
		
		$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_produkte WHERE Sektion='" . AREA . "' {$def_search} {$def_genre} {$def_status} {$def_istop} {$def_hits} {$def_datetill}");
		
		$num = $sql->numrows();
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$q = "SELECT * FROM " . PREFIX . "_produkte WHERE Sektion='" . AREA . "'  {$def_search} {$def_status} {$def_istop} {$def_genre} {$def_hits} {$def_datetill} {$def_order} LIMIT $a,$limit";
		
		$sql = $GLOBALS['db']->Query($q);
		while($row=$sql->fetchrow())
		{
			$row->Comments = getCommentCount('products', $row->Id);
			$row->User = getUsername($row->Benutzer);
			$row->Wertung = getRating($row->Id,'products');
			#$row->Genre = $this->getGenre($row->Genre);
			array_push($products,$row);
		}
		
		$ordstr = "index.php?do=products&amp;sub=overview{$def_search_n}{$def_status_n}{$def_genre_n}{$def_istop_n}&amp;pp={$limit}{$def_hits_n}{$def_datetill_n}";
		$nastr = "{$def_order_n}{$def_search_n}{$def_status_n}{$def_genre_n}{$def_istop_n}&amp;pp={$limit}{$def_hits_n}{$def_datetill_n}";
		
		
		$GLOBALS['tmpl']->assign('ordstr',$ordstr);
		$GLOBALS['tmpl']->assign('products', $products); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Products']); 
		$GLOBALS['tmpl']->assign('limit',$limit);
		$GLOBALS['tmpl']->assign('genres',$this->Genres());
		if($num>$limit)$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', "<a class=\"page_navigation\" href=\"index.php?do=products&amp;sub=overview{$nastr}&pp={$limit}&page={s}\">{t}</a> "));
		
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/products.tpl'));
	}
	
	//=================================================================
	// Neu erstellen
	//=================================================================
	function newProduct()
	{
		if(perm('products_newedit'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$dp = explode('.', $_POST['Datum_Veroffentlichung']);
				$date_published = mktime(0,0,1,$dp[1],$dp[0],$dp[2]);
				$content = imgToPrefix($_POST['Content']);
				
				$query = "INSERT INTO " . PREFIX . "_produkte 
				(
					Id, 	
					Benutzer, 	
					Datum, 	
					Datum_Veroffentlichung, 	
					Name1, 	
					Name2, 	
					Name3, 	
					Beschreibung1, 	
					Beschreibung2, 	
					Beschreibung3, 	
					Textbilder1, 		
					Genre, 	
					Vertrieb, 	
					Hersteller, 	
					Asin, 	
					Plattform, 	
					Preis, 	
					Shopurl, 	
					Shop, 	
					Bild, 	
					Links, 	
					Galerien, 	
					Hits,	
					Sektion, 	
					TopProduct, 	
					Aktiv
				) VALUES (
					'', 	
					'" . $_SESSION['benutzer_id'] . "', 	
					'" . stime() . "', 	
					'" . $date_published . "',  	
					'" . $_POST['Titel'] . "', 	
					'" . $_POST['Titel'] . "',  	
					'" . $_POST['Titel'] . "',   	
					'" . $content . "', 	
					'" . $content . "',  	
					'" . $content . "',  	
					'" . @addslashes(base64_decode($_POST['inline_screens'])) . "',  	
					'" . $_POST['Genre'] . "',  	
					'" . @$_POST['Vertrieb'] . "',  	
					'" . @$_POST['Hersteller'] . "', 	
					'" . @$_POST['Asin'] . "', 	
					'" . @$_POST['Plattform'] . "', 	
					'" . @$_POST['Preis'] . "',  	
					'" . @$_POST['Shopurl'] . "',  	
					'" . @$_POST['Shop'] . "',  	
					'" . @$_POST['newImg_1'] . "',  	
					'" . @$_POST['Links'] . "',  	
					'" . @implode(',',$_POST['Galerien']) . "', 	
					'0', 	
					'" . AREA . "', 	
					'" . @$_POST['TopProduct'] . "',
					'" . @$_POST['Aktiv'] . "'
					)";
				
				$GLOBALS['db']->Query($query);
				$new_id = $GLOBALS['db']->InsertId();
				
				// Navi-Eigenschaften
				
				$doc_s = "index.php?p=products&area=" . AREA . "&action=showproduct&id=" . $new_id;
				$doc = "index.php?p=products&area=" . AREA . "&action=showproduct&id=" . $new_id . "&name=" . seotitle($_POST['Titel']);
				
				// Navi-Eintrag?
				if(isset($_POST['ToNavi']) && $_POST['ToNavi']==1)
				{
					$na = explode('|', $_POST['NaviCat2']);
					$ParentId = $na[1];
					$NaCat = $na[0];
					
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_navi 
					(
						Id,
						ParentId,
						NaviCat,
						Sektion,
						Titel_1,
						Titel_2,
						Titel_3,
						Dokument,
						group_id,
						Ziel,
						Position
					) VALUES (
						'',
						'"  . $ParentId . "',
						'" . $NaCat . "',
						'" . AREA . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $doc . "',
						'" . implode(',', $_POST['Groups']) . " ',
						'_self',
						'" . abs((int)$_POST['PosN']) . "'
					)");
				}
				
				// QuickNavi-Eintrag?
				if(isset($_POST['ToQuickNavi']) && $_POST['ToQuickNavi']==1)
				{
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_quicknavi 
					(
						Id,
						Sektion,
						Name_1,
						Name_2,
						Name_3,
						Dokument,
						Aktiv,
						Ziel,
						Position
					) VALUES (
						'',
						'" . AREA . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $doc . "',
						'1',
						'_self',
						'" . abs((int)$_POST['PosQN']) . "'
					)");
				}
				Logger('User ' . $_SESSION['user_name'] . ' create Product ('.$_POST['Titel'].')', '3', $_SESSION['benutzer_id']);
				echo '<script>window.opener.location.reload(); window.close()</script>';
			}
			
			$GLOBALS['tmpl']->assign('UserGroups',UserGroups());
			$GLOBALS['tmpl']->assign('Navis',getNavisWithEntries(AREA));
			$GLOBALS['tmpl']->assign('Gallery', fetchGalleryCategs($_SESSION['a_area'])); 
			$GLOBALS['tmpl']->assign('Content', fckInit(' ', 'Content', 350, 'Content'));
			$GLOBALS['tmpl']->assign('genres',$this->Genres());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Products_new']); 
			$GLOBALS['tmpl']->assign('genres',$this->Genres());
			$GLOBALS['tmpl']->assign('mf',$this->getManufaturer());
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/new.tpl'));
		} else {
			no_perm();
		}
	}
	
	
	//=================================================================
	// Content bearbeiten
	//=================================================================
	function editProduct($id)
	{
		if(perm('products_newedit'))
		{
			$LC = (isset($_REQUEST['langcode']) && !empty($_REQUEST['langcode'])) ? abs((int)$_REQUEST['langcode']) : 1;
			$isdb = '';
			$start = '';
			$end = '';
			$categ = '';
			$Bild = '';
			$TopContentBild = '';
			$Gallery = '';
			$BildAusr = '';
			$Sonstiges = '';
			$SetAll = '';
			
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{	
				
				if(perm('gallery_inlineshots') && isset($_POST['inline_screens']))
				{
					$isdb = ",Textbilder{$LC} = '" . addslashes(base64_decode($_POST['inline_screens'])) . "' ";
				}
				
				$Bild        = (isset($_POST['NoImg']) && $_POST['NoImg']==1) ? ",Bild = ''" : (isset($_POST['newImg_1']) && @$_POST['newImg_1']!='') ? ",Bild = '" . @$_POST['newImg_1'] . "'" : "";
				
				// Nur speichern, wenn 1. Sprache gewaehlt
				if(isset($_POST['langcode']) && $_POST['langcode']==1)
				{
					$dp = explode('.', $_POST['Datum_Veroffentlichung']);
					$date_published = mktime(0,0,1,$dp[1],$dp[0],$dp[2]);
					
					if(isset($_POST['saveAllLang']) && $_POST['saveAllLang']==1)
					{
						$SetAll = "
							,Name2 = '" . $_POST['Titel'] . "'
							,Beschreibung2 = '" . imgToPrefix($_POST['Content']) . "'
							,Name3 = '" . $_POST['Titel'] . "'
							,Beschreibung3 = '" . imgToPrefix($_POST['Content']) . "'
							";
					}
					
					$Gallery   = ",Galerien='" . @implode(',',@$_POST['Gallery']) . "'"; 
					$Sonstiges = "
						{$SetAll}
						,Genre='" . $_POST['Genre'] . "'
						,Vertrieb='" . $_POST['Vertrieb'] . "'
						,Hersteller='" . $_POST['Hersteller'] . "'
						,Preis='" . $_POST['Preis'] . "'
						,Shopurl='" . $_POST['Shopurl'] . "'
						,Shop='" . $_POST['Shop'] . "'
						,TopProduct='" . $_POST['TopProduct'] . "'
						,Links='" . $_POST['Links'] . "'
						,Datum_Veroffentlichung='" . $date_published . "'
						,Aktiv = '" . $_POST['Aktiv'] . "'";
				}
				
				$q = "UPDATE " . PREFIX . "_produkte 
				SET
					Name{$LC} = '" . $_POST['Titel'] . "',
					Beschreibung{$LC} = '" . imgToPrefix($_POST['Content']) . "'
					{$categ}
					{$Bild}
					{$Sonstiges}
					{$Gallery}
					{$isdb} 
				WHERE Id='$id'";
				
				$GLOBALS['db']->RowQuery($q);
				
				// Navi-Eigenschaften
				$doc_s = "index.php?p=products&area=" . AREA . "&action=showproduct&id=$id";
				$doc = "index.php?p=products&area=" . AREA . "&action=showproduct&id=$id&name=" . seotitle($_POST['Titel']);
				
					
				// Navi-Eintrag?
				if(isset($_POST['ToNavi']) && $_POST['ToNavi']==1)
				{
					$na = explode('|', $_POST['NaviCat2']);
					$ParentId = $na[1];
					$NaCat = $na[0];
				
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_navi 
					(
						Id,
						ParentId,
						NaviCat,
						Sektion,
						Titel_1,
						Titel_2,
						Titel_3,
						Dokument,
						group_id,
						Ziel,
						Position
					) VALUES (
						'',
						'"  . $ParentId . "',
						'" . $NaCat . "',
						'" . AREA . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $doc . "',
						'" . implode(',', $_POST['Groups']) . " ',
						'_self',
						'" . abs((int)$_POST['PosN']) . "'
					)");
				}
				
				
				
				// Neu
				if(isset($_POST['ToQuickNavi']) && $_POST['ToQuickNavi']==1)
				{
					$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_quicknavi 
					(
						Id,
						Sektion,
						Name_1,
						Name_2,
						Name_3,
						Dokument,
						Aktiv,
						Ziel,
						Position
					) VALUES (
						'',
						'" . AREA . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $_POST['Titel'] . "',
						'" . $doc . "',
						'1',
						'_self',
						'" . abs((int)$_POST['PosQN']) . "'
					)");
				}
				
				Logger('User ' . $_SESSION['user_name'] . ' update product ('.$_POST['Titel'].')', '3', $_SESSION['benutzer_id']);
				header("Location:index.php?do=products&sub=edit&id=$id&noframes=1&langcode=" . $_POST['langcode']);
				exit;
			}
			
			$LC = (isset($_REQUEST['langcode']) && !empty($_REQUEST['langcode'])) ? abs((int)$_REQUEST['langcode']) : 1;
			$res =  $GLOBALS['db']->RowQuery("SELECT 
				*, 
				Textbilder{$LC} as Textbilder,
				Name{$LC} as Titel,
				Beschreibung{$LC} as Inhalt 
			FROM " . PREFIX . "_produkte  WHERE Id='$id'");
			
			// Ist Dokument in QuickNavi vorhanden?
			$doc_s  = "index.php?p=products&area=" . AREA . "&action=showproduct&id=$id";
			$res_qn = $GLOBALS['db']->RowQuery("SELECT Id FROM " . PREFIX . "_quicknavi WHERE Dokument LIKE '" . $doc_s . "%' AND Sektion='" . AREA . "'");
			$res_n  = $GLOBALS['db']->RowQuery("SELECT Id FROM " . PREFIX . "_navi WHERE Dokument LIKE '" . $doc_s . "%' AND Sektion='" . AREA . "'");
			
			$res->inQuicknavi = @$res_qn->Id;
			$res->inNavi = @$res_n->Id;
			$res->Galerien = explode(',', $res->Galerien);
			$res->Bild = imgPrefix($res->Bild);
			
			$GLOBALS['tmpl']->assign('UserGroups',UserGroups());
			$GLOBALS['tmpl']->assign('Navis',getNavisWithEntries(AREA));
			$GLOBALS['tmpl']->assign('Gallery', fetchGalleryCategs($_SESSION['a_area'])); 
			$GLOBALS['tmpl']->assign('Content', fckInit(imgPrefix($res->Inhalt) . ' ', 'Content', 350, 'Content'));
			$GLOBALS['tmpl']->assign('InlineShots', unserialize($res->Textbilder)); 
			$GLOBALS['tmpl']->assign('content', $res); 
			$GLOBALS['tmpl']->assign('field_inline', "Textbilder{$LC}"); 
			$GLOBALS['tmpl']->assign('genres',$this->Genres());
			$GLOBALS['tmpl']->assign('mf',$this->getManufaturer());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Content_edit']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/edit.tpl'));
		
		} else {
			no_perm();
		}
	}
	
	// Kopieren
	function copyProduct($id)
	{
		if(perm('products_newedit'))
		{
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_produkte WHERE Id='{$id}'");
			if(is_object($res))
			{
				$GLOBALS['db']->RowQuery("INSERT INTO " . PREFIX . "_produkte 
				(
					Id, 	
					Benutzer, 	
					Datum, 	
					Datum_Veroffentlichung, 	
					Name1, 	
					Name2, 	
					Name3, 	
					Beschreibung1, 	
					Beschreibung2, 	
					Beschreibung3, 	
					Textbilder1, 	
					Textbilder2, 	
					Textbilder3, 	
					Genre, 	
					Vertrieb, 	
					Hersteller, 	
					Wertung, 	
					Asin, 	
					Plattform, 	
					Preis, 	
					Shopurl, 	
					Shop, 	
					Bild, 	
					Links, 	
					Galerien, 	
					Hits,	
					Sektion, 	
					TopProduct, 	
					Aktiv
				) VALUES (
					'', 	
					'" . $_SESSION['benutzer_id'] . "', 	
					'" . stime() . "', 	
					'" . $res->Datum_Veroffentlichung . "',  	
					'" . addslashes($res->Name1 . $GLOBALS['config_vars']['DbCopy']) . "', 	
					'" . addslashes($res->Name2 . $GLOBALS['config_vars']['DbCopy']) . "',  	
					'" . addslashes($res->Name3 . $GLOBALS['config_vars']['DbCopy']) . "',   	
					'" . addslashes($res->Beschreibung1) . "', 	
					'"  .addslashes($res->Beschreibung2) . "',  	
					'" . addslashes($res->Beschreibung3) . "',  	
					'" . $res->Textbilder1 . "',  	
					'" . $res->Textbilder2 . "',  	
					'" . $res->Textbilder3 . "', 	
					'" . $res->Genre . "',  	
					'" . $res->Vertrieb . "',  	
					'" . $res->Hersteller . "', 	
					'" . $res->Wertung . "',  	
					'" . $res->Asin . "', 	
					'" . $res->Plattform . "', 	
					'" . $res->Preis . "',  	
					'" . $res->Shopurl . "',  	
					'" . $res->Shop . "',  	
					'" . $res->Bild . "',  	
					'" . $res->Links . "',  	
					'" . $res->Galerien . "', 	
					'0', 	
					'" . $res->Sektion . "', 	
					'" . $res->TopProduct . "',
					'0'
				)");
				Logger('User ' . $_SESSION['user_name'] . ' copy Product ('.$res->Name1.')', '3', $_SESSION['benutzer_id']);
				echo '<script>window.opener.location.reload(); window.close();</script>';
			}
		} else {
			no_perm();
		}
	}
	
}
?>