<?php
class Manufacturer
{
	//=================================================================
	// Uebersicht
	//=================================================================
	function showManufacturer()
	{
		$def_order    = 'ORDER BY Name ASC';
		$def_order_n  = '';
		$def_search   = '';
		$def_status   = '';
		$def_status_n = '';
		$def_search_n = '';
		$def_hits     = '';
		$def_hits_n   = '';
		$manufacturer         = 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 . "_hersteller SET Hits ='" . $_POST['Hits'][$nid] . "' WHERE Id='$nid'");
				// Loeschen?
				if(isset($_POST['del'][$nid]) && $_POST['del'][$nid]==1 && perm('manufacturer_del'))
				{
					$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_hersteller WHERE Id='$nid'");
				}
			}
		}
		
		// 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']);
		}
		
		// 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 (Name 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 Name 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 Name 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 '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;
			}
		}
		
		$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_hersteller WHERE Sektion='" . AREA . "' {$def_search} {$def_hits}");
		
		$num = $sql->numrows();
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$q = "SELECT * FROM " . PREFIX . "_hersteller WHERE Sektion='" . AREA . "'  {$def_search} {$def_hits} {$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);
			array_push($manufacturer,$row);
		}
		
		$ordstr = "index.php?do=manufacturer&amp;sub=overview{$def_search_n}&amp;pp={$limit}{$def_hits_n}";
		$nastr = "{$def_order_n}{$def_search_n}&amp;pp={$limit}{$def_hits_n}";
		
		
		$GLOBALS['tmpl']->assign('ordstr',$ordstr);
		$GLOBALS['tmpl']->assign('manufacturer', $manufacturer); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Manufacturer']); 
		$GLOBALS['tmpl']->assign('limit',$limit);
		if($num>$limit)$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', "<a class=\"page_navigation\" href=\"index.php?do=manufacturer&amp;sub=overview{$nastr}&page={s}\">{t}</a> "));
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/manufacturer/manufacturer.tpl'));
	}
	
	//=================================================================
	// Bearbeiten
	//=================================================================
	function editManufacturer($id)
	{
		if(!perm('manufacturer_newedit'))
		{
			no_perm();
		} else {
			
			//=================================================================
			// Speichern
			//=================================================================
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$Bild = '';
				$Beschreibung_1 = $_POST['Beschreibung_1'];
				$Beschreibung_2 = (isset($_POST['Beschreibung_2']) && $_POST['Beschreibung_2']!='' && strlen($_POST['Beschreibung_2'])>6) ? $_POST['Beschreibung_2'] : $_POST['Beschreibung_1'];
				$Beschreibung_3 = (isset($_POST['Beschreibung_3']) && $_POST['Beschreibung_3']!='' && strlen($_POST['Beschreibung_3'])>6) ? $_POST['Beschreibung_3'] : $_POST['Beschreibung_1'];
			
				if(isset($_POST['NoImg']) && $_POST['NoImg']==1) $Bild = "Bild = '',";
				if(isset($_POST['newImg_1']) && !empty($_POST['newImg_1'])) $Bild = "Bild = '" . $_POST['newImg_1'] . "',";
				
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_hersteller 
				SET
					Name = '" . $_POST['Name'] . "',
					NameLang = '" . $_POST['NameLang'] . "',
					Beschreibung_1 = '" . imgToPrefix($Beschreibung_1) . "', 	
					Beschreibung_2 = '" . imgToPrefix($Beschreibung_2) . "', 	
					Beschreibung_3 = '" . imgToPrefix($Beschreibung_3) . "', 	
					{$Bild}
					Gruendung = '" . $_POST['Gruendung'] . "', 	
					GruendungLand = '" . $_POST['GruendungLand'] . "',	
					Personen = '" . $_POST['Personen'] . "', 	
					Homepage = '" . $_POST['Homepage'] . "', 	
					Adresse = '" . $_POST['Adresse'] . "', 	
					Telefonkontakt = '" . $_POST['Telefonkontakt'] . "'
				WHERE 
					Id='$id'");
				Logger('User ' . $_SESSION['user_name'] . ' update Manufacturer (' . $_POST['Name']  . ')', '3', $_SESSION['benutzer_id']);
				echo '<script>window.opener.location.reload(); window.close();</script>';
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_hersteller WHERE Sektion='" . AREA . "' AND Id='$id'");
			
			$GLOBALS['tmpl']->assign('res',$res);
			$GLOBALS['tmpl']->assign('Beschreibung_1', fckInit(imgPrefix($res->Beschreibung_1) . ' ', 'Beschreibung_1', 250, 'Settings'));
			$GLOBALS['tmpl']->assign('Beschreibung_2', fckInit(imgPrefix($res->Beschreibung_2) . ' ', 'Beschreibung_2', 250, 'Settings'));
			$GLOBALS['tmpl']->assign('Beschreibung_3', fckInit(imgPrefix($res->Beschreibung_3) . ' ', 'Beschreibung_3', 250, 'Settings'));
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Manufacturer_edit']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/manufacturer/edit_new.tpl'));
		}
	}
	
	//=================================================================
	// Neu
	//=================================================================
	function newManufacturer()
	{
		//=================================================================
		// Speichern
		//=================================================================
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$Beschreibung_1 = $_POST['Beschreibung_1'];
			$Beschreibung_2 = (isset($_POST['Beschreibung_2']) && $_POST['Beschreibung_2']!='' && strlen($_POST['Beschreibung_2'])>6) ? $_POST['Beschreibung_2'] : $_POST['Beschreibung_1'];
			$Beschreibung_3 = (isset($_POST['Beschreibung_3']) && $_POST['Beschreibung_3']!='' && strlen($_POST['Beschreibung_3'])>6) ? $_POST['Beschreibung_3'] : $_POST['Beschreibung_1'];
			
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_hersteller 
			(
				Id,
				Datum,
				Benutzer,
				Name,
				NameLang,
				Beschreibung_1,
				Beschreibung_2,
				Beschreibung_3,
				Bild,
				Gruendung,
				GruendungLand,
				Personen,
				Homepage,
				Adresse,
				Telefonkontakt
			) VALUES (
				'',
				'" .stime() . "',
				'" . $_SESSION['benutzer_id'] . "',
				'" . $_POST['Name'] . "',
				'" . $_POST['NameLang'] . "',
				'" . imgToPrefix($Beschreibung_1) . "', 	
				'" . imgToPrefix($Beschreibung_2) . "', 	
				'" . imgToPrefix($Beschreibung_3) . "', 	
				'" . @$_POST['newImg_1'] . "',
				'" . $_POST['Gruendung'] . "', 	
				'" . $_POST['GruendungLand'] . "',	
				'" . $_POST['Personen'] . "', 	
				'" . $_POST['Homepage'] . "', 	
				'" . $_POST['Adresse'] . "', 	
				'" . $_POST['Telefonkontakt'] . "'
			)");
			
			Logger('User ' . $_SESSION['user_name'] . ' create new Manufacturer (' . $_POST['Name']  . ')', '3', $_SESSION['benutzer_id']);
			echo '<script>window.opener.location.reload(); window.close();</script>';
		}
		
		$GLOBALS['tmpl']->assign('Beschreibung_1', fckInit(' ', 'Beschreibung_1', 250, 'Settings'));
		$GLOBALS['tmpl']->assign('Beschreibung_2', fckInit(' ', 'Beschreibung_2', 250, 'Settings'));
		$GLOBALS['tmpl']->assign('Beschreibung_3', fckInit(' ', 'Beschreibung_3', 250, 'Settings'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Manufacturer_new']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/manufacturer/edit_new.tpl'));
	}
}
?>