<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Products
{
	var $_pageLimit = 15;
	var $_allowed_html = '<br><br />';
	
	function newProducts($limit=5)
	{
		$LC = $_SESSION['Langcode'];
		
		$items = array();
		$query= $GLOBALS['db']->Query("SELECT Id,Bild,Name{$LC} AS Name, Beschreibung{$LC} AS Beschreibung 
		FROM 
			" . PREFIX . "_produkte  WHERE Aktiv='1' AND Sektion='" . AREA . "' ORDER BY Id DESC LIMIT $limit");
		
		while($row = $query->fetchrow())
		{
			// Wertung
			$row->Beschreibung = cleanEmptyTags($row->Beschreibung);
			$row->Beschreibung = strip_tags($row->Beschreibung, $this->_allowed_html);
			array_push($items, $row);
		}
		
		return $items;
	}
	
	function GetSettings()
	{
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_produkte_einstellungen LIMIT 1" );
		$gal_settings = $sql->fetchrow();
		return $gal_settings;
	}
	
	function ShowProduct($id)
	{
		$LC = $_SESSION['Langcode'];
		
		// Hitcount erhoehen
		if(!@$_SESSION['ProdClick_' . $id])
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_produkte SET Hits=Hits+1 WHERE Id='{$id}'");
			$_SESSION['ProdClick_' . $id] = 'seen';
		}
		
		$row = $GLOBALS['db']->ArrQuery("SELECT 
			*,
			Textbilder{$LC},
			Name{$LC} AS Name,
			Beschreibung{$LC} AS Inhalt 
		FROM " . PREFIX . "_produkte  WHERE Aktiv='1' AND  Id='{$id}'");
		
		if(!is_array($row)) refer('index.php?p=products&area=' . AREA);
		
		$row['Inhalt']  = (!empty($row['Textbilder' . $LC])) ? inlinescreens($row['Textbilder' . $LC], $row['Inhalt']) : $row['Inhalt'];
		$row['Inhalt'] = cleanEmptyTags($row['Inhalt']);
		$row['Inhalt'] = autowords($row['Inhalt']);
		
		
		
		//=================================================================
		// Interne Seiten
		//=================================================================
		$_REQUEST['artpage'] = (isset($_REQUEST['artpage']) && $_REQUEST['artpage'] != '' && $_REQUEST['artpage'] >= 1) ? cleanInt($_REQUEST['artpage']) : 1;
		$seite_anzeigen = explode("[--NEU--]", $row['Inhalt']);
		$anzahl_seiten = count($seite_anzeigen);
		
		if($_REQUEST['artpage'] > $anzahl_seiten)
		{
			$_REQUEST['artpage'] = $anzahl_seiten;
			$row['Inhalt'] = $seite_anzeigen[$anzahl_seiten-1];
		} else {
			$row['Inhalt'] = $seite_anzeigen[@$_REQUEST['artpage']-1];
		}
		//=================================================================
		// Wenn mehrere Seiten vorhanden sind, Navi erzeugen
		//=================================================================
		if($anzahl_seiten>1)
		{
			
			$article_pages = artpage($anzahl_seiten, $_REQUEST['artpage'], " <a class=\"page_navigation\" href=\"index.php?p=products&amp;area=" . AREA . "&amp;action=showproduct&amp;id="  . seotitle($row['Id']) . "&amp;name="  . seotitle($row['Name']) . "&amp;artpage={s}\">{t}</a> ");
			$GLOBALS['tmpl']->assign('article_pages', $article_pages);
		}
		
		// Hersteller + Vertriebs-Link
		$row['ManLink'] = $this->GetManufacturer($row['Hersteller']);
		$row['PubLink'] = $this->GetManufacturer($row['Vertrieb']);
		
		// Links 
		if($row['Links']!='')
		{
			$alternatives = array();
			$mirrors = explode("\r\n", $row['Links']);
			$i=1;
			foreach($mirrors as $m)
			{
				$mi = '';
				$det = explode(";",$m);
				$mi->Id = $i++;
				$mi->Link = $det[0];
				$mi->Name = $det[1];
				array_push($alternatives, $mi);
			}
			$GLOBALS['tmpl']->assign('ProductLinks', $alternatives);
		}
		
		// Galerien enthalten?
		if($row['Galerien']!='')
		{
			if(file_exists(BASE_DIR . '/class/Class.Gallery.php') && get_active('gallery'))
			{
				include_once(BASE_DIR . '/class/Class.Gallery.php');
				$Gallery = new Gallery;
				$GLOBALS['tmpl']->assign('IncludedGalleries', $Gallery->getExtenGallery($row['Galerien']));
			}
		}
		
		//=================================================================
		// Kommentare
		//=================================================================
		$Settings = $this->GetSettings();
		
		if($Settings->Kommentare == 1)
		{
			$comment = new Comments;
			// Kommentar bearbeiten
			if(isset($_POST['comment_action']) && $_POST['comment_action'] == 'edit' && permission('edit_comments')) $comment->EditComment('products',$_POST['comment_id']);
			
			// Neuer Kommentar
			if(permission('comments'))
			{
				$comment->NewComment('products', @$_POST['Redir'],$id);
			}
			
			// Kommentare holen
			$Comments = $comment->GetComments('products',$id,'index.php?p=products&amp;action=showdproduct&amp;id=' . $id . '&amp;name=' . seotitle($row['Name']));
			$GLOBALS['tmpl']->assign('GetComments',$GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/comments/comments.tpl'));
		}
		
		//=================================================================
		// Wertung
		//=================================================================
		if($Settings->Wertung==1)
		{
			$row['Wertung'] = getRating($row['Id'],'products');
			$GLOBALS['tmpl']->assign('RatingUrl', 'index.php?p=rating&action=rate&id=' . $id. '&where=products');
			$GLOBALS['tmpl']->assign('RatingForm', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/other/rating.tpl')); 
		}
		
		$row['Genre'] = $this->getGenre($row['Genre']);
		
		// Meta
		setMeta($row['Name'],$row['Inhalt']);
		
		$GLOBALS['tmpl']->assign('res', $row);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Products'] . $GLOBALS['config_vars']['PageSep'] .$row['Name']);
		$GLOBALS['tmpl']->assign('headernav', '<a href="index.php?p=products&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Products'] . '</a>');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/details.tpl'));
	}
	
	//=================================================================
	// Uebersicht
	//=================================================================
	function Overview()
	{
		$Settings = $this->GetSettings();
		$LC = $_SESSION['Langcode'];
		$Search = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9_)( ]*)/', '', @$_REQUEST['q']);
		
		if((strlen($Search) >= 2))
		{
			$Serach_db = " AND (Name{$LC} LIKE '%{$Search}%') ";
			$Serach_nav = "&amp;q=" . urlencode($Search);
		} else {
			$Serach_db = "";
			$Serach_nav = "";
		}
		
		
		switch(@$_REQUEST['sort'])
		{
			case '':
			default:
			case 'datedesc':
				$req = 'datedesc';
				$defsort = "ORDER BY  TopProduct DESC, Datum DESC";
				$defsort_n = "&amp;sort=datedesc";
				$GLOBALS['tmpl']->assign('img_date', 'sorter_down');
				$GLOBALS['tmpl']->assign('datesort', 'dateasc');
			break;
			
			case 'dateasc':
				$req = 'dateasc';
				$defsort = "ORDER BY  TopProduct DESC, Datum ASC";
				$defsort_n = "&amp;sort=dateasc";
				$GLOBALS['tmpl']->assign('img_date', 'sorter_up');
				$GLOBALS['tmpl']->assign('datesort', 'datedesc');
			break;
			
			case 'namedesc':
				$req = 'namedesc';
				$defsort = "ORDER BY  TopProduct DESC, Name{$LC} DESC";
				$defsort_n = "&amp;sort=namedesc";
				$GLOBALS['tmpl']->assign('img_name', 'sorter_down');
				$GLOBALS['tmpl']->assign('namesort', 'nameasc');
			break;
			
			case 'nameasc':
				$req = 'nameasc';
				$defsort = "ORDER BY  TopProduct DESC, Name{$LC} ASC";
				$defsort_n = "&amp;sort=nameasc";
				$GLOBALS['tmpl']->assign('img_name', 'sorter_up');
				$GLOBALS['tmpl']->assign('namesort', 'namedesc');
			break;
			
			
			case 'genredesc':
				$req = 'genredesc';
				$defsort = "ORDER BY  TopProduct DESC, Genre DESC";
				$defsort_n = "&amp;sort=genredesc";
				$GLOBALS['tmpl']->assign('img_genre', 'sorter_down');
				$GLOBALS['tmpl']->assign('genresort', 'genreasc');
			break;
			
			case 'genreasc':
				$req = 'genreasc';
				$defsort = "ORDER BY  TopProduct DESC, Genre ASC";
				$defsort_n = "&amp;sort=genreasc";
				$GLOBALS['tmpl']->assign('img_genre', 'sorter_down');
				$GLOBALS['tmpl']->assign('genresort', 'genredesc');
			break;
			
			case 'hitsdesc':
				$req = 'hitsdesc';
				$defsort = "ORDER BY  TopProduct DESC, Hits DESC";
				$defsort_n = "&amp;sort=hitsdesc";
				$GLOBALS['tmpl']->assign('img_hits', 'sorter_down');
				$GLOBALS['tmpl']->assign('hitssort', 'hitsasc');
			break;
			
			case 'hitsasc':
				$req = 'hitsasc';
				$defsort = "ORDER BY  TopProduct DESC, Hits ASC";
				$defsort_n = "&amp;sort=hitsasc";
				$GLOBALS['tmpl']->assign('img_hits', 'sorter_up');
				$GLOBALS['tmpl']->assign('hitssort', 'hitsdesc');
			break;
		}
		
		$_REQUEST['q'] = $Search;
		$_REQUEST['sort'] = $req;
		
		$prefix = '';
		$area = AREA;
		$items = array();
		
		$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS Gesamt FROM " . PREFIX . "_produkte WHERE Aktiv='1' AND Sektion='" . AREA . "' {$Serach_db} {$defsort}");
		$num = $res->Gesamt;
		$limit = GetLim($this->_pageLimit);
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$query= $GLOBALS['db']->Query("SELECT 
			*,
			Name{$LC} AS Name, 
			Beschreibung{$LC} AS Beschreibung 
		FROM 
			" . PREFIX . "_produkte  WHERE Aktiv='1' AND Sektion='" . AREA . "' {$Serach_db} {$defsort} LIMIT $a,$limit");
		
		while($row = $query->fetchrow())
		{
			// Wertung
			if($Settings->Wertung==1) $row->Wertung = getRating($row->Id,'products');
			if($Settings->Kommentare==1) $row->CCount = getPageCommentCount($row->Id,'products');
			$row->Genre = $this->getGenre($row->Genre);
			$row->Beschreibung = cleanEmptyTags($row->Beschreibung);
			$row->Beschreibung = strip_tags($row->Beschreibung,$this->_allowed_html);
			array_push($items, $row);
		}
		
		
		
		// Navi
		if($num>$limit)
		{
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, "", "<a class=\"page_navigation\" href=\"index.php?p=products&amp;area=".AREA."&amp;page={s}{$defsort_n}{$Serach_nav}\">{t}</a> "));
		}
		
		if($Settings->Wertung==1)
		{
			$GLOBALS['tmpl']->assign('product_rate',1);
		}
		
		$GLOBALS['tmpl']->assign('items', $items);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Products']);
		$GLOBALS['tmpl']->assign('headernav', '<a href="index.php?p=products&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Products'] . '</a>');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/products/products.tpl'));
	}
	
	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 GetManufacturer($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT Name,Id FROM " . PREFIX . "_hersteller  WHERE Id='{$id}'");
		if(!is_object($res)) return;
		$link = "<a href=\"index.php?p=manufacturer&amp;area=" . AREA . "&amp;action=showdetails&amp;id={$id}&amp;name=" . seotitle($res->Name) . "\">" . koobispecialchars($res->Name) ."</a>";
		return $link;
		
	}
}
?>