From 394a60e849d20c04a1647705f992d3ee64900207 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sun, 25 Oct 2015 15:45:25 +0000
Subject: [PATCH] Display tag at bottom of page and link to list of page with
 that tag

---
 build/index.php       | 84 ++++++++++++++++++++++++++++++++-----------
 module_index.json     |  4 +--
 modules/page-list.php | 72 ++++++++++++++++++++++++++++---------
 modules/page-view.php |  9 +++--
 settings.fragment.php |  3 +-
 5 files changed, 130 insertions(+), 42 deletions(-)

diff --git a/build/index.php b/build/index.php
index de426c8..4a42b3a 100644
--- a/build/index.php
+++ b/build/index.php
@@ -262,7 +262,8 @@ textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8re
 .editform input[type=text] { width: calc(100% - 0.3rem); box-sizing: border-box; }
 
 .page-tags-display { margin: 0.5rem 0 0 0; padding: 0; list-style-type: none; }
-.page-tags-display li { display: inline-block; margin: 0.5rem; padding: 0.5rem; background: #D2C3DD; color: #FB701A; white-space: nowrap; }
+.page-tags-display li { display: inline-block; margin: 0.5rem; padding: 0.5rem; background: #D2C3DD; white-space: nowrap; }
+.page-tags-display a { color: #FB701A; text-decoration: none; }
 .page-tags-display li:before { content: \"\\A\"; position: relative; top: 0.03rem; left: -0.9rem; width: 0; height: 0; border-top: 0.6rem solid transparent; border-bottom: 0.6rem solid transparent; border-right: 0.5rem solid #D2C3DD; }
 
 footer { padding: 2rem; }
@@ -2011,32 +2012,72 @@ register_module([
 		add_action("list", function() {
 			global $pageindex, $settings;
 			
+			$title = "All Pages";
+			$content = "	<h1>$title on $settings->sitename</h1>";
+			
 			$sorted_pageindex = get_object_vars($pageindex);
 			ksort($sorted_pageindex, SORT_NATURAL);
-			$title = "All Pages";
-			$content = "	<h1>$title on $settings->sitename</h1>
-	<table>
+			
+			$content .= generate_page_list(array_keys($sorted_pageindex));
+			exit(page_renderer::render_main("$title - $settings->sitename", $content));
+		});
+		
+		add_action("list-tags", function() {
+			global $pageindex, $settings;
+			
+			if(empty($_GET["tag"]))
+			{
+				http_response_code(301);
+				header("location: ?action=list");
+			}
+			
+			$tag = $_GET["tag"];
+			
+			
+			$sorted_pageindex = get_object_vars($pageindex);
+			ksort($sorted_pageindex, SORT_NATURAL);
+			
+			$pagelist = [];
+			foreach($pageindex as $pagename => $pagedetails)
+			{
+				if(empty($pagedetails->tags)) continue;
+				if(in_array($tag, $pagedetails->tags))
+					$pagelist[] = $pagename;
+			}
+			
+			$content = "<h1>$tag</h1>\n";
+			$content .= generate_page_list($pagelist);
+			
+			exit(page_renderer::render("$tag - Page List - $settings->sitename", $content));
+		});
+	}
+]);
+
+function generate_page_list($pagelist)
+{
+	global $pageindex;
+	
+	$result = "<table>
 		<tr>
 			<th>Page Name</th>
 			<th>Size</th>
 			<th>Last Editor</th>
 			<th>Last Edit Time</th>
 		</tr>\n";
-		foreach($sorted_pageindex as $pagename => $pagedetails)
-		{
-			$content .= "\t\t<tr>
+	foreach($pagelist as $pagename)
+	{
+			$result .= "\t\t<tr>
 			<td><a href='index.php?page=$pagename'>$pagename</a></td>
-			<td>" . human_filesize($pagedetails->size) . "</td>
-			<td>$pagedetails->lasteditor</td>
-			<td>" . human_time_since($pagedetails->lastmodified) . " <small>(" . date("l jS \of F Y \a\\t h:ia T", $pagedetails->lastmodified) . ")</small></td>
-
-		</tr>\n";
-			}
-			$content .= "	</table>";
-			exit(page_renderer::render_main("$title - $settings->sitename", $content));
-		});
+			<td>" . human_filesize($pageindex->$pagename->size) . "</td>
+			<td>" . $pageindex->$pagename->lasteditor . "</td>
+			<td>" . human_time_since($pageindex->$pagename->lastmodified) . " <small>(" . date("l jS \of F Y \a\\t h:ia T", $pageindex->$pagename->lastmodified) . ")</small></td>
+	
+	</tr>\n";
 	}
-]);
+	$result .= "	</table>";
+	
+	return $result;
+}
 
 
 
@@ -2381,9 +2422,12 @@ register_module([
 			// todo display tags here
 			if(!empty($pageindex->$page->tags))
 			{
-				$content .= "<ul class='page-tags-display'><li>";
-				$content .= implode("</li><li>", $pageindex->$page->tags);
-				$content .= "</li></ul>\n";
+				$content .= "<ul class='page-tags-display'>\n";
+				foreach($pageindex->$page->tags as $tag)
+				{
+					$content .= "<li><a href='?action=list-tags&tag=$tag'>$tag</a></li>\n";
+				}
+				$content .= "\n</ul>\n";
 			}
 			else
 			{
diff --git a/module_index.json b/module_index.json
index bda4063..e521c91 100644
--- a/module_index.json
+++ b/module_index.json
@@ -104,7 +104,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds a page that lists all the pages in the index along with their metadata.",
         "id": "page-list",
-        "lastupdate": 1445170746,
+        "lastupdate": 1445787342,
         "optional": false
     },
     {
@@ -149,7 +149,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to view pages. You reallyshould include this one.",
         "id": "page-view",
-        "lastupdate": 1445775829,
+        "lastupdate": 1445787503,
         "optional": false
     },
     {
diff --git a/modules/page-list.php b/modules/page-list.php
index 09a96f6..5128423 100644
--- a/modules/page-list.php
+++ b/modules/page-list.php
@@ -9,31 +9,71 @@ register_module([
 		add_action("list", function() {
 			global $pageindex, $settings;
 			
+			$title = "All Pages";
+			$content = "	<h1>$title on $settings->sitename</h1>";
+			
 			$sorted_pageindex = get_object_vars($pageindex);
 			ksort($sorted_pageindex, SORT_NATURAL);
-			$title = "All Pages";
-			$content = "	<h1>$title on $settings->sitename</h1>
-	<table>
+			
+			$content .= generate_page_list(array_keys($sorted_pageindex));
+			exit(page_renderer::render_main("$title - $settings->sitename", $content));
+		});
+		
+		add_action("list-tags", function() {
+			global $pageindex, $settings;
+			
+			if(empty($_GET["tag"]))
+			{
+				http_response_code(301);
+				header("location: ?action=list");
+			}
+			
+			$tag = $_GET["tag"];
+			
+			
+			$sorted_pageindex = get_object_vars($pageindex);
+			ksort($sorted_pageindex, SORT_NATURAL);
+			
+			$pagelist = [];
+			foreach($pageindex as $pagename => $pagedetails)
+			{
+				if(empty($pagedetails->tags)) continue;
+				if(in_array($tag, $pagedetails->tags))
+					$pagelist[] = $pagename;
+			}
+			
+			$content = "<h1>$tag</h1>\n";
+			$content .= generate_page_list($pagelist);
+			
+			exit(page_renderer::render("$tag - Page List - $settings->sitename", $content));
+		});
+	}
+]);
+
+function generate_page_list($pagelist)
+{
+	global $pageindex;
+	
+	$result = "<table>
 		<tr>
 			<th>Page Name</th>
 			<th>Size</th>
 			<th>Last Editor</th>
 			<th>Last Edit Time</th>
 		</tr>\n";
-		foreach($sorted_pageindex as $pagename => $pagedetails)
-		{
-			$content .= "\t\t<tr>
+	foreach($pagelist as $pagename)
+	{
+			$result .= "\t\t<tr>
 			<td><a href='index.php?page=$pagename'>$pagename</a></td>
-			<td>" . human_filesize($pagedetails->size) . "</td>
-			<td>$pagedetails->lasteditor</td>
-			<td>" . human_time_since($pagedetails->lastmodified) . " <small>(" . date("l jS \of F Y \a\\t h:ia T", $pagedetails->lastmodified) . ")</small></td>
-
-		</tr>\n";
-			}
-			$content .= "	</table>";
-			exit(page_renderer::render_main("$title - $settings->sitename", $content));
-		});
+			<td>" . human_filesize($pageindex->$pagename->size) . "</td>
+			<td>" . $pageindex->$pagename->lasteditor . "</td>
+			<td>" . human_time_since($pageindex->$pagename->lastmodified) . " <small>(" . date("l jS \of F Y \a\\t h:ia T", $pageindex->$pagename->lastmodified) . ")</small></td>
+	
+	</tr>\n";
 	}
-]);
+	$result .= "	</table>";
+	
+	return $result;
+}
 
 ?>
diff --git a/modules/page-view.php b/modules/page-view.php
index a815c92..e52cdd3 100644
--- a/modules/page-view.php
+++ b/modules/page-view.php
@@ -62,9 +62,12 @@ register_module([
 			// todo display tags here
 			if(!empty($pageindex->$page->tags))
 			{
-				$content .= "<ul class='page-tags-display'><li>";
-				$content .= implode("</li><li>", $pageindex->$page->tags);
-				$content .= "</li></ul>\n";
+				$content .= "<ul class='page-tags-display'>\n";
+				foreach($pageindex->$page->tags as $tag)
+				{
+					$content .= "<li><a href='?action=list-tags&tag=$tag'>$tag</a></li>\n";
+				}
+				$content .= "\n</ul>\n";
 			}
 			else
 			{
diff --git a/settings.fragment.php b/settings.fragment.php
index f4bb981..cbcccb5 100644
--- a/settings.fragment.php
+++ b/settings.fragment.php
@@ -259,7 +259,8 @@ textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8re
 .editform input[type=text] { width: calc(100% - 0.3rem); box-sizing: border-box; }
 
 .page-tags-display { margin: 0.5rem 0 0 0; padding: 0; list-style-type: none; }
-.page-tags-display li { display: inline-block; margin: 0.5rem; padding: 0.5rem; background: #D2C3DD; color: #FB701A; white-space: nowrap; }
+.page-tags-display li { display: inline-block; margin: 0.5rem; padding: 0.5rem; background: #D2C3DD; white-space: nowrap; }
+.page-tags-display a { color: #FB701A; text-decoration: none; }
 .page-tags-display li:before { content: \"\\A\"; position: relative; top: 0.03rem; left: -0.9rem; width: 0; height: 0; border-top: 0.6rem solid transparent; border-bottom: 0.6rem solid transparent; border-right: 0.5rem solid #D2C3DD; }
 
 footer { padding: 2rem; }
