<?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($seo[2]))
	$page = new page($seo[2]);
else
{
	$pagesQuery = $_sql->query("SELECT pageSlug, pageTitle FROM pages");
	if ($pagesQuery->num_rows)
	{
		echo "<h1>{locale:pages}</h1>";
		echo "<table class='designed pages'><tr><td>{locale:page_title}</td></tr>";
		while ($data = $pagesQuery->fetch_assoc())
			echo "<tr><td><a href='/admin/page/$data[pageSlug]'>$data[pageTitle]</a></td></tr>";
		echo "</table>";
	}
}
?>
<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>