From f04773a76b2e3a02019bd87cb30767c29ff53d2d Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 25 Oct 2015 11:26:35 +0000
Subject: [PATCH] Add support for editing tags

---
 build/index.php       | 16 +++++++++++++---
 module_index.json     |  2 +-
 modules/page-edit.php | 15 ++++++++++++---
 settings.fragment.php |  1 +
 4 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/build/index.php b/build/index.php
index 5a5195d..6483eda 100644
--- a/build/index.php
+++ b/build/index.php
@@ -259,6 +259,7 @@ input[type=text]:not(.link-display), input[type=password], textarea { margin: 0.
 input[type=text], input[type=password], textarea { padding: 0.5rem 0.8rem; background: #d5cbf9; border: 0; border-radius: 0.3rem; font-size: 1rem; color: #442772; }
 textarea { width: calc(100% - 2rem); min-height: 35rem; font-size: 1.25rem; }
 textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8rem; padding: 0.5rem; font-weight: bolder; }
+.editform input[type=text] { width: calc(100% - 0.3rem); box-sizing: border-box; }
 
 footer { padding: 2rem; }
 /* #ffdb6d #36962c */";
@@ -1752,12 +1753,14 @@ register_module([
 			}
 			
 			$content = "<h1>$title</h1>";
+			$page_tags = implode(", ", (!empty($pageindex->{$env->page}->tags)) ? $pageindex->{$env->page}->tags : "");
 			if(!$env->is_logged_in and $settings->anonedits)
 			{
 				$content .= "<p><strong>Warning: You are not logged in! Your IP address <em>may</em> be recorded.</strong></p>";
 			}
-			$content .= "<form method='post' action='index.php?action=save&page=" . rawurlencode($page) . "&action=save'>
+			$content .= "<form method='post' action='index.php?action=save&page=" . rawurlencode($page) . "&action=save' class='editform'>
 			<textarea name='content'>$pagetext</textarea>
+			<input type='text' name='tags' value='$page_tags' placeholder='Enter some tags for the page here. Separate them with commas.' title='Enter some tags for the page here. Separate them with commas.' />
 			<p>$settings->editing_message</p>
 			<input type='submit' value='Save Page' />
 		</form>";
@@ -1810,12 +1813,18 @@ register_module([
 				mkdir(dirname("$env->page.md"), null, true);
 			}
 			
-			
+			// Read in the new page content
 			$pagedata = $_POST["content"];
-			
+			// Santise it if necessary
 			if($settings->clean_raw_html)
 				$pagedata = htmlentities($pagedata, ENT_QUOTES);
 			
+			// Read in the new page tags
+			$page_tags = explode(",", $_POST["tags"]);
+			// Trim off all the whitespace
+			foreach($page_tags as &$tag)
+				$tag = trim($tag);
+			
 			if(file_put_contents("$env->page.md", $pagedata) !== false)
 			{
 				$page = $env->page;
@@ -1834,6 +1843,7 @@ register_module([
 					$pageindex->$page->lasteditor = utf8_encode($env->user);
 				else
 					$pageindex->$page->lasteditor = utf8_encode("anonymous");
+				$pageindex->$page->tags = $page_tags;
 				
 				// A hack to resave the pagedata if the preprocessors have
 				// changed it. We need this because the preprocessors *must*
diff --git a/module_index.json b/module_index.json
index 2591893..79990cd 100644
--- a/module_index.json
+++ b/module_index.json
@@ -77,7 +77,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
         "id": "page-edit",
-        "lastupdate": 1445170746,
+        "lastupdate": 1445772370,
         "optional": false
     },
     {
diff --git a/modules/page-edit.php b/modules/page-edit.php
index 6badf5e..8135b2d 100644
--- a/modules/page-edit.php
+++ b/modules/page-edit.php
@@ -60,12 +60,14 @@ register_module([
 			}
 			
 			$content = "<h1>$title</h1>";
+			$page_tags = implode(", ", (!empty($pageindex->{$env->page}->tags)) ? $pageindex->{$env->page}->tags : "");
 			if(!$env->is_logged_in and $settings->anonedits)
 			{
 				$content .= "<p><strong>Warning: You are not logged in! Your IP address <em>may</em> be recorded.</strong></p>";
 			}
-			$content .= "<form method='post' action='index.php?action=save&page=" . rawurlencode($page) . "&action=save'>
+			$content .= "<form method='post' action='index.php?action=save&page=" . rawurlencode($page) . "&action=save' class='editform'>
 			<textarea name='content'>$pagetext</textarea>
+			<input type='text' name='tags' value='$page_tags' placeholder='Enter some tags for the page here. Separate them with commas.' title='Enter some tags for the page here. Separate them with commas.' />
 			<p>$settings->editing_message</p>
 			<input type='submit' value='Save Page' />
 		</form>";
@@ -118,12 +120,18 @@ register_module([
 				mkdir(dirname("$env->page.md"), null, true);
 			}
 			
-			
+			// Read in the new page content
 			$pagedata = $_POST["content"];
-			
+			// Santise it if necessary
 			if($settings->clean_raw_html)
 				$pagedata = htmlentities($pagedata, ENT_QUOTES);
 			
+			// Read in the new page tags
+			$page_tags = explode(",", $_POST["tags"]);
+			// Trim off all the whitespace
+			foreach($page_tags as &$tag)
+				$tag = trim($tag);
+			
 			if(file_put_contents("$env->page.md", $pagedata) !== false)
 			{
 				$page = $env->page;
@@ -142,6 +150,7 @@ register_module([
 					$pageindex->$page->lasteditor = utf8_encode($env->user);
 				else
 					$pageindex->$page->lasteditor = utf8_encode("anonymous");
+				$pageindex->$page->tags = $page_tags;
 				
 				// A hack to resave the pagedata if the preprocessors have
 				// changed it. We need this because the preprocessors *must*
diff --git a/settings.fragment.php b/settings.fragment.php
index f149876..f583c6e 100644
--- a/settings.fragment.php
+++ b/settings.fragment.php
@@ -256,6 +256,7 @@ input[type=text]:not(.link-display), input[type=password], textarea { margin: 0.
 input[type=text], input[type=password], textarea { padding: 0.5rem 0.8rem; background: #d5cbf9; border: 0; border-radius: 0.3rem; font-size: 1rem; color: #442772; }
 textarea { width: calc(100% - 2rem); min-height: 35rem; font-size: 1.25rem; }
 textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8rem; padding: 0.5rem; font-weight: bolder; }
+.editform input[type=text] { width: calc(100% - 0.3rem); box-sizing: border-box; }
 
 footer { padding: 2rem; }
 /* #ffdb6d #36962c */";
