<?php
class Partner
{
	//=================================================================
	// Uebersicht
	//=================================================================
	function ListPartners()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1 && isset($_POST['PartnerUrl']))
		{
			foreach($_POST['PartnerUrl'] as $pid => $em)
			{
				if(!empty($_POST['PartnerUrl'][$pid]) && !empty($_POST['PartnerName'][$pid]))
				{
					if(!starts_with('http://', $_POST['PartnerUrl'][$pid]))  $_POST['PartnerUrl'][$pid] = 'http://' .  $_POST['PartnerUrl'][$pid];
					
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_partner 
					Set 
						PartnerUrl='" . $_POST['PartnerUrl'][$pid] . "',
						PartnerName='" . $_POST['PartnerName'][$pid] . "',
						Hits='" . $_POST['Hits'][$pid] . "',
						Position='" . $_POST['Position'][$pid] . "',
						Aktiv='" . $_POST['Aktiv'][$pid] . "' 
					WHERE Id='" . $pid . "'");
				}
			}
			Logger('User ' . $_SESSION['user_name'] . ' update Affiliate ('.$_POST['PartnerName'][$pid].')', '3', $_SESSION['benutzer_id']);
		}
		
		// Neu anlegen
		if(isset($_POST['new']) && $_POST['new']==1)
		{
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_partner 
			VALUES (
				'',
				'" . $_POST['PartnerUrl'] . "',
				'" . $_POST['Position'] . "',
				'0',
				'" . $_POST['PartnerName'] . "',
				'" . $_POST['newImg_1'] . "',
				'" . AREA . "',
				'1' 
			)");
			Logger('User ' . $_SESSION['user_name'] . ' create new Affiliate ('.$_POST['PartnerName'].')', '3', $_SESSION['benutzer_id']);
			header('Location:index.php?do=partners');
			exit;
		}
		
		
		$partners = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_partner WHERE Sektion='" . AREA . "' ORDER BY Position ASC");
		while($row = $sql->fetchrow())
		{
			$row->Bild = (is_file(BASE_DIR . 'uploads/partner/' . $row->Bild)) ? 'uploads/partner/' . $row->Bild : '';
			array_push($partners, $row);
		}
		
		$GLOBALS['tmpl']->assign('partners', $partners);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Partners']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/partner/partner.tpl'));
	}
	
	//=================================================================
	// Bild bearbeiten /ersetzen
	//=================================================================
	function imgEdit($id)
	{
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_partner Set Bild='" . @$_POST['newImg_1'] . "' WHERE Id='" . $id . "'");
			$row = $GLOBALS['db']->RowQuery("SELECT  PartnerName FROM " . PREFIX . "_partner WHERE Id='{$id}'");
			Logger('User ' . $_SESSION['user_name'] . ' update Affiliate-Image ('.$row->PartnerName.')', '3', $_SESSION['benutzer_id']);
			echo '<script>window.opener.location.reload(); window.close();</script>';
		}
		
		$row = $GLOBALS['db']->RowQuery("SELECT  * FROM " . PREFIX . "_partner WHERE Id='{$id}'");
		$row->Bild = (is_file(BASE_DIR . 'uploads/partner/' . $row->Bild)) ? 'uploads/partner/' . $row->Bild : '';
		
		$GLOBALS['tmpl']->assign('res', $row); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Partners_imgEdit']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/partner/img_edit.tpl'));
	}
	
	//=================================================================
	// Partner loeschen
	//=================================================================
	function Delete($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT  Bild,PartnerName FROM " . PREFIX . "_partner WHERE Id='{$id}'");
		@unlink(BASE_DIR . 'uploads/partner/' . $res->Bild);
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_partner WHERE Id='{$id}'");
		Logger('User ' . $_SESSION['user_name'] . ' delete Affiliate ('.$res->PartnerName.')', '3', $_SESSION['benutzer_id']);
		header('Location:index.php?do=partners');
		exit;
	}
}
?>