<?php

if (isset($_GET['status']))
	switch ($_GET['status'])
	{
		case 'added':
			array_push($info, $_locale['entry_added']);
		break;

		case 'updated':
			array_push($info, $_locale['entry_updated']);
		break;
	}

if (isset($_POST['entryContent']) && $user['userLevel'] > 2)
{

	if (isset($_POST['entryAdd']))
	{
		if (blog::add($_POST['entryHeader'], $_POST['entryTitle'], $_POST['entrySlug'], $_POST['entryContent'], $_POST['entryPublished'], (isset($_POST['entryHidden']) ? true : false), $_POST['entryPIN']))
			redirect(get_entry_link($_POST['entrySlug']), 'added');
		else
			array_push($error, $_locale['entry_not_added']);
	} elseif (isset($_POST['entryUpdate']))
	{
		if (blog::update($_POST['entryUpdate'], $_POST['entryHeader'], $_POST['entryTitle'], $_POST['entryContent'], $_POST['entryPublished'], (isset($_POST['entryHidden']) ? true : false), $_POST['entryPIN']))
			redirect(get_current_link(), 'updated');
		else
			array_push($error, $_locale['entry_not_updated']);
	}

}

if (isset($_POST['entryTag']) && $user['userLevel'] > 2)
{
	if (isset($_POST['tagIdRemove']))
	{
		if (blog::tagRemove($_POST['tagIdRemove'], $_POST['entryId']))
			array_push($info, $_locale['entry_tag_removed']);
		else
			array_push($error, $_locale['entry_tag_not_removed']);
	} elseif (blog::tag($_POST['tagId'], $_POST['entryId']))
		array_push($info, $_locale['entry_tag_added']);
	else
		array_push($error, $_locale['entry_tag_not_added']);
}

if (isset($seo[2]) && $user['userLevel'] > 2)
{
	$entry = new blog($seo[2]);
	if ($entry->entries)
	{
		$entryData = $entry->entry();
		if (isset($seo[3]) && $user['userLevel'] > 3)
			switch ($seo[3])
			{
				case 'delete':
					if ($entry->delete($entryData['entryId']))
						redirect('/admin/entry');
					else
						array_push($error, $_locale['entry_not_deleted']);
				break;

				case 'pin':
					if ($entry->pin($entryData['entryId']))
						redirect();
					else
						array_push($error, $_locale['entry_not_pinned']);
				break;

				case 'unpin':
					if ($entry->unpin($entryData['entryId']))
						redirect();
					else
						array_push($error, $_locale['entry_not_unpinned']);
				break;
			}
	}
} else
{
	$timedQuery = $_sql->query("SELECT entrySlug, entryTitle, entryCreated, entryPublished FROM entries WHERE entryPublished > ".time()."");
	if ($timedQuery->num_rows)
	{
		echo "<h1>{locale:timed_entries}</h1>";
		echo "<table class='designed timed'><tr><td>{locale:entry_title}</td><td>{locale:created_on}</td><td>{locale:timed_pub_date}</td></tr>";
		while ($data = $timedQuery->fetch_assoc())
			echo "<tr><td><a href='".get_entry_link($data['entrySlug'])."'>$data[entryTitle]</a></td><td>".show_date($data['entryCreated'])."</td><td>".show_date($data['entryPublished'])."</td></tr>";
		echo "</table>";
	}

	$hiddenQuery = $_sql->query("SELECT entrySlug, entryTitle, entryCreated, entryPublished FROM entries WHERE entryHidden IS NOT NULL");
	if ($hiddenQuery->num_rows)
	{
		echo "<h1>{locale:hidden_entries}</h1>";
		echo "<table class='designed timed'><tr><td>{locale:entry_title}</td><td>{locale:created_on}</td><td>{locale:timed_pub_date}</td></tr>";
		while ($data = $hiddenQuery->fetch_assoc())
			echo "<tr><td><a href='".get_entry_link($data['entrySlug'], null, true)."'>$data[entryTitle]</a></td><td>".show_date($data['entryCreated'])."</td><td>".show_date($data['entryPublished'])."</td></tr>";
		echo "</table>";
	}
}
?>
<h1>{locale:entry_editor}</h1>
<form action="<?=get_current_link()?>" method="post" name="entry-edit">

<?php if (isset($entryData)) : addTitle($entryData['entryTitle']); headerImage($entryData['entryHeader']); ?>
	<input type="text" name="entryHeader" value="<?=htmlentities($entryData['entryHeader'])?>" placeholder="{locale:entry_header}" maxlength="255" />
	<input type="text" name="entryTitle" value="<?=htmlentities($entryData['entryTitle'])?>" placeholder="{locale:entry_title}" maxlength="250" />
	<textarea id="entry-textarea" name="entryContent" rows="30"><?=htmlspecialchars($entryData['entryContent'])?></textarea>
	<input id="entry-date" type="text" name="entryPublished" value="<?=date(DATE_FORMAT_DEFAULT, $entryData['entryPublished'])?>" placeholder="{locale:entry_pub_date}" maxlength="50" />
	<label><input type="checkbox" name="entryHidden" value="1" <?=($entryData['entryHidden'] ? 'checked ' : '')?> /> {locale:entry_hide}</label>
	<input type="text" name="entryPIN" value="<?=htmlentities($entryData['entryPIN'])?>" placeholder="{locale:entry_pin}" maxlength="6" />
	<input type="hidden" name="entryUpdate" value="<?=$entryData['entryId']?>" />
<?php else: ?>
	<input type="text" name="entryHeader" id="entryHeader" placeholder="{locale:entry_header}" maxlength="255" />
	<input type="text" name="entryTitle" id="entryTitle" placeholder="{locale:entry_title}" maxlength="250" />
	<textarea id="entry-textarea" name="entryContent" rows="30"></textarea>
	<input id="entry-date" type="text" name="entryPublished" placeholder="{locale:entry_pub_date}" maxlength="50" />
	<input type="text" name="entrySlug" id="entrySlug" placeholder="{locale:entry_slug}" maxlength="100" />
	<label><input type="checkbox" name="entryHidden" value="1" /> {locale:entry_hide}</label>
	<input type="text" name="entryPIN" placeholder="{locale:entry_pin}" maxlength="6" />
	<input type="hidden" name="entryAdd" value="true" />
<?php endif ?>

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

</form>

<?php
if (isset($entryData))
{
	$tags = $_sql->query("SELECT * FROM tags");
	if ($tags->num_rows)
	{
		echo "<h3>{locale:tags}</h3>";
		echo "<table class='designed'><tr><td>{locale:tag_name}</td><td>{locale:add}</td></tr>"
				."<form action='".get_current_link()."' method='post' name='tagentry'>"
				."<input type='hidden' name='entryId' value='$entryData[entryId]' />"
				."<input type='hidden' name='entryTag' value='true' />";

		while ($tag = $tags->fetch_assoc())
			echo "<tr><td>$tag[tagName]</td><td>".($_sql->query("SELECT taggedId FROM tagged WHERE taggedEntry = $entryData[entryId] AND taggedTag = $tag[tagId]")->num_rows ? "<button type='submit' name='tagIdRemove' value='$tag[tagId]'>{locale:remove}</button>":"<button type='submit' name='tagId' value='$tag[tagId]'>{locale:add}</button>")."</td></tr>";

		echo "</form>"
			."</table>";
	}
}
?>

<script>$("#entry-date").datepicker({ minDate: 0, maxDate: "+48M" });</script>
