From b79a042bdcb68524a872038d440c15bfda7c5856 Mon Sep 17 00:00:00 2001
From: Daku <dakutree@codeanimu.net>
Date: Mon, 30 Dec 2013 01:48:07 +0000
Subject: [PATCH] added option to order pool list by created date/last
 updated/title/count

---
 ext/pools/main.php  | 17 ++++++++++++++++-
 ext/pools/script.js | 10 ++++++++++
 ext/pools/theme.php | 10 ++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 ext/pools/script.js

diff --git a/ext/pools/main.php b/ext/pools/main.php
index e449ab1f..7cdc0374 100644
--- a/ext/pools/main.php
+++ b/ext/pools/main.php
@@ -74,6 +74,8 @@ class Pools extends Extension {
 
 		if ($config->get_int("ext_pools_version") < 2){
 			$database->Execute("ALTER TABLE `pools`	ADD UNIQUE INDEX (`title`);");
+			$database->Execute("ALTER TABLE `pools`	ADD `lastupdated` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;");
+
 			$config->set_int("ext_pools_version", 2);
 		}
 	}
@@ -314,13 +316,26 @@ class Pools extends Extension {
 
 		$poolsPerPage = $config->get_int("poolsListsPerPage");
 
+
+		$order_by = "";
+		$order = get_prefixed_cookie("ui-order-pool");
+		if($order == "created" || is_null($order)){
+			$order_by = "ORDER BY p.date DESC";
+		}elseif($order == "updated"){
+			$order_by = "ORDER BY p.lastupdated DESC";
+		}elseif($order == "name"){
+			$order_by = "ORDER BY p.title ASC";
+		}elseif($order == "count"){
+			$order_by = "ORDER BY p.posts DESC";
+		}
+
 		$pools = $database->get_all("
 				SELECT p.id, p.user_id, p.public, p.title, p.description,
 				       p.posts, u.name as user_name
 				FROM pools AS p
 				INNER JOIN users AS u
 				ON p.user_id = u.id
-				ORDER BY p.date DESC
+				$order_by
 				LIMIT :l OFFSET :o
 				", array("l"=>$poolsPerPage, "o"=>$pageNumber * $poolsPerPage)
 				);
diff --git a/ext/pools/script.js b/ext/pools/script.js
new file mode 100644
index 00000000..72ea80e6
--- /dev/null
+++ b/ext/pools/script.js
@@ -0,0 +1,10 @@
+$(function() {
+	var order_pool = $.cookie("shm_ui-order-pool");
+	$("#order_pool option[value="+order_pool+"]").attr("selected", true);
+
+	$('#order_pool').change(function(){
+		var val = $("#order_pool option:selected").val();
+		$.cookie("shm_ui-order-pool", val, {path: '/', expires: 365}); //FIXME: This won't play nice if COOKIE_PREFIX is not "shm_".
+		window.location.href = '';
+	});
+});
diff --git a/ext/pools/theme.php b/ext/pools/theme.php
index 39c4318e..cc924045 100644
--- a/ext/pools/theme.php
+++ b/ext/pools/theme.php
@@ -67,11 +67,21 @@ class PoolsTheme extends Themelet {
 			<br><a href="'.make_link("pool/updated").'">Pool Changes</a>
 		';
 
+		$order_html = '
+			<select id="order_pool">
+			  <option value="created">Recently created</option>
+			  <option value="updated">Last updated</option>
+			  <option value="name">Name</option>
+			  <option value="count">Post count</option>
+			</select>
+		';
+
 		$blockTitle = "Pools";
 		$page->set_title(html_escape($blockTitle));
 		$page->set_heading(html_escape($blockTitle));
 		$page->add_block(new Block($blockTitle, $html, "main", 10));
 		$page->add_block(new Block("Navigation", $nav_html, "left", 10));
+		$page->add_block(new Block("Order By", $order_html, "left", 15));
 
 		$this->display_paginator($page, "pool/list", null, $pageNumber, $totalPages);
 	}