<?php include('inc/header.php'); ?>
<h1>Settings - Site: Rules</h1>
<div class="container">
<?php
	odbc_exec($odbc_connect, 'USE [' . $_CONFIG['db_databases']['web'] . ']');
	if(isset($_POST['addrule'])) {
		if($_POST['question'] == '' || $_POST['answer'] == '' || $_POST['list'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			$id = odbc_result(odbc_exec($odbc_connect, 'SELECT [id] FROM [web_rules] ORDER BY [id] DESC'), 'id')+1;
			odbc_exec($odbc_connect, 'INSERT INTO [web_rules](id, list, paragraph, [text], [datetime]) VALUES('.$id.', '.mssql_escape_string($_POST['list']).', \''.mssql_escape_string($_POST['question']).'\', \''.mssql_escape_string($_POST['answer']).'\', '.time().')');
			echo '<div class="success">Der Paragraph <b>§'.mssql_escape_string($_POST['id']).' - '.mssql_escape_string($_POST['question']).'</b> wurde erfolgreich hinzugefügt.</div>';
		}
	} elseif(isset($_POST['saverule'])) {
		if($_POST['edit_question'] == '' || $_POST['edit_answer'] == '' || $_POST['edit_list'] == '') {
			echo '<div class="fail">You must fill-in all fields!</div>';
		} else {
			odbc_exec($odbc_connect, '
				UPDATE 	[web_rules] 
				SET 	[list] = '.mssql_escape_string($_POST['edit_list']).',
						[paragraph] = \''.mssql_escape_string($_POST['edit_question']).'\',
						[text] = \''.mssql_escape_string($_POST['edit_answer']).'\',
						[datetime] = '.time().'
				WHERE 	[id] = '.mssql_escape_string($_POST['edit_faqid']).'
			');
			echo '<div class="success">Der Paragraph <b>§'.mssql_escape_string($_POST['edit_list']).' - '.mssql_escape_string($_POST['edit_question']).'</b> <i>(ID: '.mssql_escape_string($_POST['edit_faqid']).')</i> wurde erfolgreich aktualisiert.</div>';
		}
	}
	
	$select = odbc_exec($odbc_connect, 'SELECT * FROM [web_rules] ORDER BY [list] ASC');
	
	echo '<span id="text"></span>';
	echo '<div id="box"></div>';
	echo '<table><tr><td><a href="javascript:rule_addform();" title="Neuen Paragraphen hinzuf&uuml;gen" rel="tooltip"><img src="../images/add.png" /></a></td><td>Regel hinzuf&uuml;gen</td></tr></table>';
	
	echo '<form method="post" id="addruleform" style="display: none;">
		<table id="table">
			<tr>
				<td id="key">Paragraph:</td>
				<td id="value"><input type="text" name="list"/></td>
			</tr>
			<tr>
				<td id="key">Titel:</td>
				<td id="value"><input type="text" name="question" style="width:500px;" /></td>
			</tr>
			<tr>
				<td id="key" valign="top">Inhalt:</td>
				<td id="value"><textarea name="answer" cols="90" rows="5" style="width:504px; height:200px;"></textarea></td>
			</tr>
			<tr>
				<td id="key"></td>
				<td id="value"><input type="submit" name="addrule" value="Paragraphen hinzufügen" /></td>
			</tr>
		</table>
	</form><br/>';
	
	echo '<table id="table" style="width: 100%;">
		<tr>
			<td id="key">Paragraph</td>
			<td id="key">Titel</td>
			<td id="key">Inhalt des Paragraphen</td>
			<td id="key" style="width: 50px;"></td>
		</tr>';
	while($result = odbc_fetch_array($select)) {
		if(strlen($result['paragraph']) > 60) {$result['paragraph'] = substr($result['paragraph'], 0, 60).'...'; }
		if(strlen($result['text']) > 60) {$result['text'] = substr($result['text'], 0, 60).'...'; }
		echo '<tr id="id_'.$result['id'].'" class="row">
			<td id="value">'.$result['list'].'</td>
			<td id="value">'.$result['paragraph'].'</td>
			<td id="value">'.$result['text'].'</td>
			<td id="value"><a href="javascript:rule_edit('.$result['id'].');" title="Bearbeiten" rel="tooltip"><img src="../images/edit.png" /></a> <a href="javascript:rule_delete('.$result['id'].');" title="Löschen" rel="tooltip"><img src="../images/delete.png" /></a></td>
		</tr>';
	}
	echo '</table>';
?>
</div>
<?php include('inc/footer.php'); ?>