<?php

if (isset($_POST['pageContent']) && isset($_POST['pageSlug']) && $user['userLevel'] > 3)
{
	$page = new page($_POST['pageSlug']);
	if (isset($_POST['pageAdd']))
	{
		if ($page->create($_POST['pageTitle'], $_POST['pageContent']))
			array_push($info, $_locale['page_added']);
		else
			array_push($error, $_locale['page_not_added']);
	} elseif (isset($_POST['pageUpdate']))
	{
		if ($page->update($_POST['pageTitle'], $_POST['pageContent']))
			array_push($info, $_locale['page_updated']);
		else
			array_push($error, $_locale['page_not_updated']);
	}

}

if (isset($_POST['page_delete']))
{
	$page = new page($_POST['page_delete']);
	if ($page -> delete())	array_push($info, $_locale['page_deleted']);
	else					array_push($error, $_locale['page_delete_failed']);
}

if (isset($seo[2]))
	$page = new page($seo[2]);
else
{
	$pagesQuery = $_sql->query("SELECT pageSlug, pageTitle, pageCreated, pageModified, pageDeleted FROM pages ORDER BY pageDeleted ASC, pageTitle ASC");
	if ($pagesQuery->num_rows)
	{
		echo "<h1>{locale:pages}</h1>";
		echo "<form action='".get_current_link()."' method='post' name='page-delete'>";
		echo "<table class='designed pages'><thead><tr><th>{locale:page_title}</th><th>{locale:page_modified}</th><th>{locale:page_delete}</th></tr></thead><tbody>";
		while ($data = $pagesQuery->fetch_assoc())
			echo "<tr><td><a href='/admin/page/$data[pageSlug]'>$data[pageTitle]</a></td><td>".date($_set['dateformat'], $data['pageModified'] > $data['pageCreated'] ? $data['pageModified'] : $data['pageCreated'])."</td><td>".($data['pageDeleted'] ? date($_set['dateformat'], $data['pageDeleted']) : "<button type='submit' name='page_delete' value='$data[pageSlug]'>{locale:delete}</button>")."</td></tr>";
		echo "</tbody></table>";
		echo "</form>";
	}
}
?>
<h1>{locale:page_editor}</h1>
<form action="<?=get_current_link()?>" method="post" name="entry-edit">

<?php if (isset($page->data)) : addTitle($page->data['pageTitle']); ?>
	<input type="text" name="pageTitle" value="<?=$page->data['pageTitle']?>" placeholder="{locale:page_title}" maxlength="250" />
	<textarea name="pageContent"><?=htmlspecialchars($page->data['pageContent'])?></textarea>
	<input type="hidden" name="pageSlug" value="<?=$page->data['pageSlug']?>" />
	<input type="hidden" name="pageUpdate" value="true" />
	<button type="button" onclick="window.location.href='/admin/page'">{locale:cancel}</button>
<?php else: ?>
	<input type="text" id="pageTitle" name="pageTitle" value="" placeholder="{locale:page_title}" maxlength="250" />
	<textarea name="pageContent"></textarea>
	<input type="text" id="pageSlug" name="pageSlug" value="" placeholder="{locale:page_slug}" />
	<input type="hidden" name="pageAdd" value="true" />
<?php endif ?>

<button type="submit">{locale:save}</button>

</form>