<?php include('inc/header.php'); ?>
<h1>Settings - Site: News</h1>
<div class="container">
<?php
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
if(isset($_GET['newsletter'])) {
	if(isset($_POST['sendRundmail'])) {
		if(empty($_POST['rundmail_title']) OR empty($_POST['rundmail_content'])) {
			echo '<div class="fail">Bitte alle beiden Felder ausfüllen.</div>';
		} elseif (!isset($_POST['rundmail_checkbox'])) {
			echo '<div class="fail">Du hast vergessen, den Haken zu setzen.</div>';
		} else {
		
			$title = $_POST['rundmail_title'];
			$message = $_POST['rundmail_content'];
			
			$mails = odbc_exec($odbc_connect, '
				SELECT [email] 
				FROM [' . $_CONFIG['db_databases']['acc'] . '].[dbo].[ACCOUNT_TBL_DETAIL]
				GROUP BY [email]
				ORDER BY [email] ASC
			');
			$id = 1;
			while($empfaenger = odbc_fetch_array($mails)) {
				$id = $id+1;
				mail_Rundmail($empfaenger['email'], $title, nl2br($message));
			}
			echo '<div class="success">E-Mail erfolgreich an '.$id.' registrierte E-Mails gesendet.</div>';
		}
	}



	echo '
	<table width="100%" cellpadding="3" cellspacing="0">
		<tr><td><a href="site_news.php" title="Zurück" rel="tooltip">Zurück auf die Newsseite wechseln</a></tr>
		<tr><td><h2>Rundmail schreiben</h2></td></tr>
		<tr><td width="100%">
			<form method="post" action="">
			
				<table id="table" width="100%">
					<tr>
						<td valign="top" width="5%">Betreff:</td><td id="value" width="95%"><input type="text" name="rundmail_title" style="width:90%;"/></td>
					</tr>
					<tr>
						<td valign="top">Nachricht:</td><td id="value"><textarea name="rundmail_content" style="width:90%; height:500px;"></textarea></td>
					</tr>
					<tr>
						<td valign="top"></td><td id="value"><input style="float:left;" type="checkbox" name="rundmail_checkbox" />Ich möchte diese Rundmail wirklich an alle Nutzer von '.$_CONFIG['allg_svr_name'].' senden.</td>
					</tr>
					<tr>
						<td valign="top"></td><td id="value"><br /><input type="submit" name="sendRundmail" value="Rundmail abschicken" /></td>
					</tr>
				</table>
				
			</form>
		</td></tr>
	</table>';



}
else {

echo ' 
<table><tr><td><a href="site_news.php?newsletter" title="Eine Rundmail verfassen" rel="tooltip">Eine Rundmail an alle User verfassen</a></tr></table>
<h2>News</h2>';

	if(isset($_POST['newssbm'])) {
		if($_POST['title'] == '' || $_POST['category'] == '' || $_POST['content'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		}
        else {
			odbc_exec($odbc_connect, 'INSERT INTO [web_news](title,text,author,category,datetime,forenlink) VALUES(\''.mssql_escape_string($_POST['title']).'\', \''.mssql_escape_string($_POST['content']).'\', \''.mssql_escape_string($_POST['author']).'\', \''.mssql_escape_string($_POST['category']).'\', \''.date('d.m.Y H:i:s').'\',\''.mssql_escape_string($_POST['forenlink']).'\')');
			echo '<div class="success" onclick="updateSiteContent(\'news\', \'' . $_CONFIG['website_cronjob_filename'] . '\', \'' . $_CONFIG['website_cronjob_site'] . '\');">Die Neuigkeit wurde erfolgreich eingetragen. Hier nun noch einmal klicken, dann ist sie live auf der Website.</div>';
		}
	} elseif(isset($_POST['savenewssbm'])) {
		if($_POST['edit_title'] == '' || $_POST['edit_category'] == '' || $_POST['edit_content'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			odbc_exec($odbc_connect, 'UPDATE [web_news] SET title=\''.mssql_escape_string($_POST['edit_title']).'\', category=\''.mssql_escape_string($_POST['edit_category']).'\', text=\''.mssql_escape_string($_POST['edit_content']).'\', forenlink=\''.mssql_escape_string($_POST['edit_forenlink']).'\' WHERE nid=\''.mssql_escape_string($_POST['edit_nid']).'\'');
			echo '<div class="success">The news has successfully been changed!</div>';
		}
	}
	$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_news] ORDER BY datetime DESC');
	$categories = odbc_exec($odbc_connect, 'SELECT * FROM [web_newscategories]');
	echo '<span id="text"></span>';
	echo '<div id="box"></div>';
	echo '<table><tr><td><a href="javascript:news_addform();" title="Add" rel="tooltip"><img src="../images/add.png" /></a></td><td>Add News</td></tr></table>';
	echo '<form method="post" id="newsaddform" style="display: none;">
		<input type="hidden" name="author" value="'.$_SESSION['user'].'" />
		<table id="table">
			<tr>
				<td id="key">Title:</td>
				<td id="value"><input type="text" name="title" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key">Category:</td>
				<td id="value">
					<select name="category" style="width: 200px;">';
					while($category = odbc_fetch_array($categories)) {
						echo '<option value="'.$category['ncatid'].'">'.$category['title'].'</option>';
					}
					echo '</select>
				</td>
			</tr>
			<tr>
				<td id="key">Forenlink:</td>
				<td id="value"><input type="text" name="forenlink" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key" style="vertical-align: top;">Content:</td>
				<td id="value"><textarea name="content" style="width: 500px; height: 150px;"></textarea></td>
			</tr>
			<tr>
				<td id="key"></td>
				<td id="value"><input type="submit" name="newssbm" value="Submit News" /></td>
			</tr>
		</table>
	</form><br/>';
	echo '<table id="table" width="100%">
		<tr>
			<td id="key">Category</td>
			<td id="key">Title</td>
			<td id="key">Author</td>
			<td id="key">Forum</td>
			<td id="key"><table><tr><td><img src="../images/date.png" /></td><td>Date/Time</td></tr></table></td>
			<td width="15%"></td>
		</tr>';
	while($result = odbc_fetch_array($select)) {
		$category = odbc_exec($odbc_connect, 'SELECT title FROM [web_newscategories] WHERE ncatid=\''.mssql_escape_string($result['category']).'\'');
		$content = preg_replace('(<.*/>)', '', $result['text']);
		if(strlen($content) > 30) {
			$content = substr($content, 0, 30).'...';
		}
		if(!empty($result['forenlink'])) {$forum = '<a href="'.$result['forenlink'].'" target="_blank">Link</a>';} else {$forum = '';}
		echo '<tr id="nid_'.$result['nid'].'" class="row">
			<td id="value">'.odbc_result($category, 'title').'</td>
			<td id="value">'.$result['title'].'</td>
			<td id="value">'.$result['author'].'</td>
			<td id="value">'.$forum.'</td>
			<td id="value">'.date('d.m.Y, H:i', strtotime($result['datetime'])).'</td>
			<td id="value"><a href="javascript:news_edit('.$result['nid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png"></a> <a href="javascript:news_delete('.$result['nid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png"></a></td>
		</tr>';
	}
	echo '</table><br/><br/>';
?>
<h2>Categories</h2>
<?php
	if(isset($_POST['categorysbm'])) {
		if($_POST['cattitle'] == '' || $_POST['catimage'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			odbc_exec($odbc_connect, 'INSERT INTO [web_newscategories](title,icon) VALUES(\''.mssql_escape_string($_POST['cattitle']).'\', \''.mssql_escape_string($_POST['catimage']).'\')');
			echo '<div class="success">The category has successfully been added!</div>';
		}
	} elseif(isset($_POST['savecatsbm'])) {
		if($_POST['edit_cattitle'] == '' || $_POST['edit_catimage'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			odbc_exec($odbc_connect, 'UPDATE [web_newscategories] SET title=\''.mssql_escape_string($_POST['edit_cattitle']).'\', icon=\''.mssql_escape_string($_POST['edit_catimage']).'\' WHERE ncatid=\''.mssql_escape_string($_POST['edit_catid']).'\'');
			echo '<div class="success">The category has successfully been changed!</div>';
		}
	}
	
	$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_newscategories]');
	echo '<span id="text2"></span>';
	echo '<div id="box2"></div>';
	echo '<table><tr><td><a href="javascript:category_addform();" title="Add" rel="tooltip"><img src="../images/add.png" /></a></td><td>Add Category</td></tr></table>';
	echo '<form method="post" id="categoryaddform" style="display: none;">
		<table id="table">
			<tr>
				<td id="key">Title:</td>
				<td id="value"><input type="text" name="cattitle" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key">Image:</td>
				<td id="value"><input type="text" name="catimage" style="width: 200px;" /></td>
			</tr>
			<tr>
				<td id="key"></td>
				<td id="value"><input type="submit" name="categorysbm" value="Add Category" /></td>
			</tr>
		</table>
	</form><br/>';
	echo '<table id="table" style="width: 100%;">
		<tr>
			<td id="key">Title</td>
			<td id="key">Image</td>
			<td style="width: 50px;"></td>
		</tr>';
	while($result = odbc_fetch_array($select)) {
		echo '<tr id="catid_'.$result['ncatid'].'" class="row">
			<td id="value">'.$result['title'].'</td>
			<td id="value"><a href="../img/'.$result['icon'].'">'.$result['icon'].'</a></td>
			<td id="value"><a href="javascript:category_edit('.$result['ncatid'].');" title="Edit" rel="tooltip"><img src="../images/edit.png"></a> <a href="javascript:category_delete('.$result['ncatid'].');" title="Delete" rel="tooltip"><img src="../images/delete.png"></a></td>
		</tr>';
	}
	echo '</table>';
}
?>
</div>
<?php include('inc/footer.php'); ?>