<?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 Manufacturer
{
	var $_pageLimit = 15;
	var $_allowed_html = '<br /><br>';
	
	function Overview()
	{
		$LC = $_SESSION['Langcode'];
		$Search = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9_)( ]*)/', '', @$_REQUEST['q']);
		
		if((strlen($Search) >= 2))
		{
			$Serach_db = " AND (Name 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  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  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  Name 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  Name ASC";
				$defsort_n = "&amp;sort=nameasc";
				$GLOBALS['tmpl']->assign('img_name', 'sorter_up');
				$GLOBALS['tmpl']->assign('namesort', 'namedesc');
			break;
			
			
			case 'hitsdesc':
				$req = 'hitsdesc';
				$defsort = "ORDER BY  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  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 . "_hersteller WHERE 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 *,Beschreibung_{$LC} AS Beschreibung FROM " . PREFIX . "_hersteller  WHERE Sektion='" . AREA . "' {$Serach_db} {$defsort} LIMIT $a,$limit");
		
		while($row = $query->fetchrow())
		{
			$row->Beschreibung =  strip_tags($row->Beschreibung,$this->_allowed_html);
			$row->ProdCount = $this->getProductCount($row->Id);
			array_push($items, $row);
		}
		
		// Navi
		if($num>$limit)
		{
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, "", "<a class=\"page_navigation\" href=\"index.php?p=manufacturer&amp;area=".AREA."&amp;page={s}{$defsort_n}{$Serach_nav}\">{t}</a> "));
		}
		
		$GLOBALS['tmpl']->assign('items', $items);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Manufacturer']);
		$GLOBALS['tmpl']->assign('headernav', '<a href="index.php?p=manufacturer&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Manufacturer'] . '</a>');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/manufacturer/overview.tpl'));
	}
	
	
	function ShowManufacturer($id)
	{
		$LC = $_SESSION['Langcode'];
		$res = $GLOBALS['db']->RowQuery("SELECT *,Beschreibung_{$LC} AS Beschreibung FROM " . PREFIX . "_hersteller  WHERE Id='{$id}'");
		
		if(!is_object($res)) refer('index.php?p=manufacturer&area=' . AREA);
		
		$res->Adresse = nl2br($res->Adresse);
		$res->Prdukte = $this->getProducts($id);
		
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('Products', $res->Prdukte);
		$GLOBALS['tmpl']->assign('pagetitle_html', $GLOBALS['config_vars']['Manufacturer'] . $GLOBALS['config_vars']['PageSep'] . $res->Name);
		$GLOBALS['tmpl']->assign('headernav', '<a href="index.php?p=manufacturer&amp;area=' . AREA . '">' . $GLOBALS['config_vars']['Manufacturer'] . '</a>');
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/manufacturer/details.tpl'));
	}
	
	
	function getProducts($id)
	{
		$LC = $_SESSION['Langcode'];
		$products = array();
		$query = $GLOBALS['db']->Query("SELECT 
			*,
			Name{$LC} AS Name,
			Beschreibung{$LC} AS Beschreibung,
			Textbilder{$LC} AS Textbilder 
		FROM " . PREFIX . "_produkte WHERE Hersteller='$id' AND Sektion='" . AREA . "' ORDER BY Id DESC");
		
		while($row = $query->fetchrow())
		{	
			$row->Beschreibung = strip_tags($row->Beschreibung,'<br><br />');
			array_push($products,$row);
		}
		return $products;
	}
	
	
	function getProductCount($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS PCount FROM " . PREFIX . "_produkte WHERE Aktiv='1' AND Hersteller='$id'");
		return $res->PCount;
	}
	
	function AjQuickSearch($name)
	{
		$LC = @$_SESSION['Langcode'];
		$q = preg_replace('/[^-A-Za-zA-ÖO-öo-y0-9_. ]/', '', $name) ;
		$q = strtolower($q);
		if (!$q) return;
		
		$res = array();
		$result = $GLOBALS['db']->Query("SELECT Name FROM " . PREFIX . "_hersteller WHERE Name LIKE '%" . $q . "%' AND Sektion='1' ");
		while($row = $result->fetchrow())
		{
			array_push($res, $row->Name);
		}
		
		foreach ($res as $key=>$value) 
		{
			if (strpos(strtolower($value), $q) !== false) echo stripslashes(koobispecialchars($value)) . "\n"; // $key;
		}
	}
	
	//=================================================================
	// Hitcount erhoehen
	//=================================================================
	function Updatehitcount($id)
	{
		// Nur erhoehen, wenn Click von gleicher Seite
		if(strpos(@$_SERVER['HTTP_REFERER'], HOME_PATH)!==false)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_hersteller Set Hits=Hits+1 WHERE Id='" . abs((int)$id) . "'");
		}
	}
}
?>