<?php
//=================================================================
// Funktionen fuer den Newsletter-Versand
//
// Letzte News
//=================================================================
function getLastNews($count,$f='html',$news='')
{
	$q = $GLOBALS['db']->Query("SELECT Id,Sektion,Titel1,News1,Bild1 FROM " . PREFIX . "_news WHERE Sektion='" . AREA . "' AND Aktiv='1' AND ZeitStart<='" . stime() . "' ORDER BY Id DESC LIMIT {$count}");
	
	$news = ($f=='html') ? "<h3>" . $GLOBALS['config_vars']['Newsletter_NewsTitle'] . "</h3><br />" :  $GLOBALS['config_vars']['Newsletter_NewsTitle'] . "\r\n===================================\r\n";
	
	while($row = $q->fetchrow())
	{
		$link = getHomeLNewsletter() . 'index.php?p=news&area=' . AREA . '&newsid=' . $row->Id . '&name=' . seotitle($row->Titel1);
		
		if($f=='html')
		{
			$news .= (($row->Bild1!='') ? '<img src="' . getHomeLNewsletter() . str_replace('%%ECRUOS_GMI_IBOOK%%','',$row->Bild1) . '" alt="" border="0" align="right" />' : '') . '<h3><a href="' . $link . '">' . $row->Titel1 . '</a></h3>';
			$news .= substr(strip_tags($row->News1),0,300) . '...<br />' . $GLOBALS['config_vars']['Newsletter_ReadMore'] . ' <a href="' . $link . '">' . $link . '</a><hr style="clear:both" noshade=noshade size=1 />';
		} else {
			$news .= "\r\n" . $row->Titel1 . "\r\n-----------------------------------\r\n";
			$news .= substr($row->News1,0,300) . '...'  . "\r\n" . $GLOBALS['config_vars']['Newsletter_ReadMore'] . $link . "\r\n===================================\r\n";
		}
	}
	return cleanUnwanted($news);
}

//=================================================================
// Letzte Shop-Artikel
//=================================================================
function getLastShopArticles($count,$f='html')
{
	$q = $GLOBALS['db']->Query("SELECT Id,Kategorie,Artikelnummer,Titel_1,Beschreibung_1,Bild FROM " . PREFIX . "_shop_produkte WHERE Aktiv='1' ORDER BY Id DESC LIMIT {$count}");
	$articles = ($f=='html') ? "<h3>" . $GLOBALS['config_vars']['Newsletter_ShopTitle'] . "</h3><br />" :  $GLOBALS['config_vars']['Newsletter_ShopTitle'] . "\r\n===================================\r\n";
	while($row = $q->fetchrow())
	{
		$link = getHomeLNewsletter() . 'index.php?p=shop&action=showproduct&id=' . $row->Id . '&cid=' . $row->Kategorie . '&name' . seotitle($row->Titel_1);
		
		if($f=='html')
		{
			$articles .= (($row->Bild!='') ? '<img src="' . getHomeLNewsletter() . 'system/shopthumb.php?smode=4&img=' . $row->Bild . '" alt="" border="0" align="right" />' : '') . '<h3><a href="' . $link . '">' . $row->Titel_1 . '</a></h3>';
			$articles .= substr(strip_tags($row->Beschreibung_1),0,300) . '...<br />' . $GLOBALS['config_vars']['Newsletter_ReadMore'] . ' <a href="' . $link . '">' . $link . '</a><hr style="clear:both" noshade=noshade size=1 />';
		} else {
			$articles .= "\r\n" . $row->Titel_1 . "\r\n-----------------------------------\r\n";
			$articles .= substr($row->Beschreibung_1,0,300) . '...'  . "\r\n" . $GLOBALS['config_vars']['Newsletter_ReadMore'] . $link . "\r\n===================================\r\n";
		}
	}
	return $articles;
}


//=================================================================
// Letzte Galerien
//=================================================================
function getLastGalleries($count,$f='html')
{
	$q = $GLOBALS['db']->Query("SELECT *  FROM " . PREFIX . "_galerie WHERE Sektion='" . AREA . "' AND Aktiv='1' ORDER BY Id DESC LIMIT {$count}");
	$gals = ($f=='html') ? "<h3>" . $GLOBALS['config_vars']['Newsletter_GalTitle'] . "</h3><br />" :  $GLOBALS['config_vars']['Newsletter_GalTitle'] . "\r\n===================================\r\n";
	while($row = $q->fetchrow())
	{
		$link = getHomeLNewsletter() . 'index.php?p=gallery&action=showgallery&id=' . $row->Id . '&categ=' . $row->Kategorie . '&name' . seotitle($row->Name_1) . '&area=' . AREA;
		
		if($f=='html')
		{
			$res = $GLOBALS['db']->RowQuery("SELECT Id,Bildname  FROM " . PREFIX . "_galerie_bilder WHERE Galerie_Id='{$row->Id}' ORDER BY Id DESC LIMIT 1");
			$gals .= ((is_object($res) && file_exists(BASE_DIR . 'cache/' . md5('medium_'.$res->Bildname) )) ? '<img src="' . getHomeLNewsletter() . 'cache/' . md5('medium_'.$res->Bildname) . '" alt="" border="0" align="right" />' : '') . '<h3><a href="' . $link . '">' . $row->Name_1 . '</a></h3>';
			$gals .= substr(strip_tags($row->Beschreibung_1),0,300) . '...<br />' . $GLOBALS['config_vars']['Newsletter_ShowGal'] . ' <a href="' . $link . '">' . $link . '</a><hr style="clear:both" noshade=noshade size=1 />';
		} else {
			$gals .= "\r\n" . $row->Name_1 . "\r\n-----------------------------------\r\n";
			$gals .= substr($row->Beschreibung_1,0,300) . '...'  . "\r\n" . $GLOBALS['config_vars']['Newsletter_ShowGal'] . $link . "\r\n===================================\r\n";
		}
	}
	return $gals;
}


//=================================================================
// Stoerende Elemente entfernen
//=================================================================
function cleanUnwanted($text)
{
	$text = eregi_replace("\[SCREEN:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $text);
	$text = eregi_replace("\[CONTACT:([a-zA-Z0-9\\\/\_\.\]*)\]", "", $text);
	return $text;
}

function getHomeLNewsletter()
{
	$p = 'http://' . $_SERVER['HTTP_HOST'] . str_replace('admin/index.php','',$_SERVER['PHP_SELF']);
	return $p;
}
?>