<?php
require_once('include/admin.inc.php');

auth_redirect(); // Require logged in user to access this page.

$page = new Page();
$page->url_name = $_REQUEST['page_name'];
$page = getpage($page->url_name);

if( $_POST ) {
	$page->url_name = $_POST['page_name'] ? $_POST['page_name'] : preg_replace('/\s+/', '_', strtolower(trim($_POST['title'])));
	$page->status = $_POST['page_status'] == 'published' ? 'published' : 'draft';
	$page->title = $_POST['title'];
	$page->body = $_POST['content'];
	$page->style = $_POST['style'];

	if( USING_TIDY ) {
		$tidy = new tidy;
		$config = $tidy->getConfig();
		$tidy->parseString( $page->body, $config, 'UTF8' );
		$tidy->cleanRepair();
		$page->body = tidy_get_output($tidy);
	}

	$action = isset($_POST['publish']) ? 'post' : 'edit';

	if( isset( $_POST['publish'] ) ) $page->status = 'published';	// If [publish] button is used, ignore radio button


	switch( $_POST['action'] ) {
		case 'new_page':
			check_nonce('new-page');
			if( !insertpage($page) )
			{
				adminlog("Error inserting page.", MTS_PAGE, MTA_INSERT, E_ERROR);
				mtdie('There was an error inserting the page into the database.', 'SQL Error');
			}
			break;

		case 'savepage':
			check_nonce('save-page-' . $page->url_name);
			updatepage($page);
			break;
	}
	if( $upload_info ) $info.=$upload_info;
	if( $upload_error ) $error.=$upload_error;
	if( $error ) $action='edit';

	if( 'post' == $action ) _redirect( ADMIN_PATH . '/manage-pages.php?saved=success' );

	$info.= '<p>' . ( $page->status === 'draft' ? 'Page draft saved.' : sprintf('Page published. <a href="%s%s/static.php?name=%s">View on site</a>.', SITE_HOST, SITE_PATH, $page->url_name) ) . '</p>';

} elseif( !$page->url_name ) {
	mtdie('Attempted to edit page with no name supplied.', 'Bad Request');
}

/////////////////////// Display Edit Form ///////////////////////


adminhead('Edit Static Page');
adminmenu('manage-pages.php');


?>

<h2>Editing Page "<?php echo htmlentities($page->title, ENT_COMPAT, 'UTF-8') ; ?>"</h2>

<form enctype="multipart/form-data" action="edit-page.php" method="post" name="post" id="post">
<?php nonce_field('save-page-'.$page->url_name); ?>
<input type="hidden" name="action" value="savepage" />

<div id="poststuff">

<div id="moremeta">
<div id="grabit" class="dbx-group">

<fieldset id="pagenamediv" class="dbx-box">
<h3 class="dbx-handle">Page URL Name</h3>
<div class="dbx_content">
	<input type="text" name="page_name" value="<?php echo $page->url_name ?>" />
</div>
</fieldset>

<fieldset id="statusdiv" class="dbx-box">
<h3 class="dbx-handle">Published Status</h3>
<div class="dbx-content">
	<label><input type="radio" name="page_status" value="draft" <?php echo $page->status == 'draft' ? 'checked="checked"' : ''; ?>/>Draft</label>
	<label><input type="radio" name="page_status" value="published" <?php echo $page->status == 'published' ? 'checked="checked"' : ''; ?>/>Published</label>
</div>
</fieldset>


</div>
</div>

<fieldset id="titlediv">
	<legend>Title</legend>
	<div><input type="text" name="title" size="40" tabindex="1" value="<?php echo htmlentities($page->title, ENT_COMPAT, 'UTF-8'); ?>" id="title" /></div>
</fieldset>

<fieldset id="postdivrich">
	<legend>Page</legend>
	<style type="text/css">
		#postdivrich table, #postdivrich #quicktags {border-top: none;}
		#quicktags {border-bottom: none; padding-bottom: 2px; margin-bottom: -1px;}
		#edButtons {border-bottom: 1px solid #ccc;}
	</style>
	<div><textarea class="mceEditor" rows="13" cols="40" name="content" tabindex="3" id="content"><?php echo htmlentities($page->body, ENT_COMPAT, 'UTF-8'); ?></textarea></div>
</fieldset>



<p class="submit">
 <input name="save" type="submit" id="save" tabindex="3" value="Save and Continue Editing" style="font-weight: bold;" />
 <input name="publish" type="submit" id="publish" tabindex="5" accesskey="p" value="Publish" />
</p>



<div class="dbx-b-ox-wrapper">

<fieldset id="cssdivrich">
	<legend>Optional CSS</legend>

	<style type="text/css">
		#postdivrich table, #postdivrich #quicktags {border-top: none;}
		#quicktags {border-bottom: none; padding-bottom: 2px; margin-bottom: -1px;}
		#edButtons {border-bottom: 1px solid #ccc;}
	</style>
	<div><textarea rows="13" cols="40" name="style" tabindex="6" id="style"><?php echo htmlentities($page->style, ENT_COMPAT, 'UTF-8'); ?></textarea></div>

</fieldset>

</div>


</div>
</form>

<script src="include/tinymce/tinymce.min.js"></script>
<script src="include/mce_config.js"></script>
<script type="text/javascript">
mce_config.selector = '#content';
mce_config.content_css = 'styles/megatokyo.css';

tinymce.init(mce_config);
</script>

<?php adminfooter() ?>
