<?php
	class BlogAdminComponent extends Component
	{
		public function GetUUIDs()
		{
			$uuids[] = 'admin\/blog$';
			$uuids[] = 'admin\/blog\/(.*){0,1}$';
			return $uuids;
		}
		
		private function edit_entry( $id, $page)
		{
			if( !isset( $_REQUEST['title'] ) )
			{
				$res = get_db_rows( 'SELECT title, post_html FROM blog_posts WHERE id=' . intval( $id ) );
				if( !db_num_rows( $res ) )
				{
					list_entries( $page );
					return 1;
				}
				$tuple = get_db_tuple( $res );
?>
<form method="post" action="/<?= $page ?>">
	<input type="hidden" name="r" value="<?= $id ?>" />
	<table>
		<tfoot>
			<tr>
				<td colspan="2" align="right"><input type="submit" value="Update Blog Entry" /></td>
			</tr>
		</tfoot>
		<tbody>
			<tr>
				<th>Title</th>
				<td><input type="text" name="title" size="50" value="<?= $tuple['title'] ?>" /></td>
			</tr>
			<tr>
				<th>Entry text</th>
				<td><textarea cols="50" rows="15" name="text"><?= $tuple['post_html'] ?></textarea></td>
			</tr>
		</tbody>
	</table>
</form>
<?php
			}
			else
			{
				$title = db_escape( $_REQUEST['title'] );
				$text = db_escape( $_REQUEST['text'] );
				$id = intval( $id );

				$res = get_db_rows( "UPDATE blog_posts SET title='" . $title . "', post_html='" . $text . "' WHERE id= " . $id );
?>
<p>The blog entry: <strong><?= $title ?></strong> was successfully updated.</p>
<p><a href="/admin/blog">Return to blog admin</a></p>
<?php
			}
		}

		private function list_entries( $page )
		{
?>
<p>This is a list of the blog entries. You might consider <a href="/admin/blog/add">adding an entry</a>.</p>
<table>
	<thead>
		<tr>
			<th>Title</th>
			<th>Author</th>
			<th>Options</th>
		</tr>
	</thead>
	<tbody>
<?php
			$res = get_db_rows( 'SELECT id,title,author FROM blog_posts ORDER BY time DESC' );
			while( $tuple = get_db_tuple( $res ) )
			{
?>
		<tr>
			<td><a href="/blog/<?= $tuple['id'] ?>"><?= $tuple['title'] ?></a></td>
			<td><?= $tuple['author'] ?></td>
			<td><a href="/<?= $page ?>/delete?r=<?= $tuple['id'] ?>">delete</a> <a href="/<?= $page ?>/edit?r=<?= $tuple['id'] ?>">edit</a></td>
		</tr>
<?php
			}
?>
</table>
<?php
		}

		private function delete_entry( $id, $page )
		{
			if( isset( $_REQUEST['confirm'] ) )
			{
				$res = get_db_rows('DELETE FROM blog_posts WHERE id=' . intval( $id ) );
?>
<p>The blog entry with the id of <code><?= $id ?></code> has been deleted.</p>
<p><a href="/admin/blog">Return to blog manager.</a></p>
<?php
			}
			else
			{
?>
<p>Are you sure you want to revoke the blog entry with id: <code><?= $id ?></code>?
Blog entries which have been deleted cannot be un-deleted later.</p>
<p><a href="/<?= $page ?>?r=<?= $id ?>&amp;confirm=true">Yes, delete it!</a></p>
<p><a href="/admin/blog">No, don't do that!</a></p>
<?php
			}
		}

		private function add_entry( $page )
		{
			if( !isset( $_REQUEST['title'] ) )
			{
?>
<form method="post" action="/<?= $page ?>">
	<table>
		<tfoot>
			<tr>
				<td colspan="2" align="right"><input type="submit" value="Add Blog Entry" /></td>
			</tr>
		</tfoot>
		<tbody>
			<tr>
				<th>Title</th>
				<td><input type="text" name="title" size="50" /></td>
			</tr>
			<tr>
				<th>Entry text</th>
				<td>
					<textarea cols="50" rows="15" name="text"></textarea>
				</td>
			</tr>
		</tbody>
	</table>
</form>
<?php
			}
			else
			{
				$title = db_escape( $_REQUEST['title'] );
				$text = db_escape( $_REQUEST['text'] );
				$time = time();
				$author = db_escape( $_SESSION['username'] );

				$res = get_db_rows( "INSERT INTO blog_posts (author, time, title, post_html) VALUES (E'" . $author . "', " . $time . ", E'" . $title . "', E'" . $text . "')" );
?>
<p>The blog entry: <strong><?= $title ?></strong> was successfully added.</p>
<p><a href="/admin/blog">Return to blog admin</a></p>
<?php
			}
		}

		private function default_message()
		{
			echo '<p>An unsupported action was requested: <code>' . $action . '</code></p>' . PHP_EOL;
		}

		public function Run($page, $matches)
		{
			if( !_hasflag( FLAG_BLOG ) )
			{
				header( 'Location: /admin' );
				die();
			}

			_page_header( SITE_NAME . ' :: Blog Manager' );

			$action = isset( $matches[1] ) ? $matches[1] : 'list';
?>
<a href="/">home</a> / <a href="/admin">administrate</a> / blog manager 
<h2>Blog Manager</h2>
<script type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
			// General options
			mode : "textareas",
			theme : "advanced",
			plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Skin options
			skin : "o2k7",
			skin_variant : "silver",

			// Example content CSS (should be your site CSS)
			content_css : "/css/site.css",

			// Drop lists for link/image/media dialogs
			external_link_list_url : "js/link_list.js",
			external_image_list_url : "js/image_list.js",
			media_external_list_url : "js/media_list.js",
	});
</script>
<?php
			switch( $action )
			{
				case 'edit':
					if( isset( $_REQUEST['r'] ) )
						$this->edit_entry( $_REQUEST['r'], $page );
					else
						$this->list_entries( $page );
					break;
				case 'list':
					$this->list_entries( $page );
					break;
				case 'delete':
					if( isset( $_REQUEST['r'] ) )
						$this->delete_entry( $_REQUEST['r'], $page );
					else
						$this->list_entries( $page );
					break;
				case 'add':
					$this->add_entry( $page );
					break;
				default:
					$this->default_message();
			}
			_page_footer();
		}
	};
	new BlogAdminComponent;
?>
