From 59b2fb69e45256fbad0756b237c4a0abb8f8160f Mon Sep 17 00:00:00 2001
From: Graham Cole <chckens@sucs.org>
Date: Thu, 12 Jun 2008 22:05:07 +0000
Subject: [PATCH] rough attempt at cleaning up the junk interface. Needs a bit
 more work

---
 components/junk.php | 34 ++++++++++++-----
 templates/junk.tpl  | 93 ++++++++++++++++++++++-----------------------
 2 files changed, 71 insertions(+), 56 deletions(-)

diff --git a/components/junk.php b/components/junk.php
index 0bdf488..ac78033 100644
--- a/components/junk.php
+++ b/components/junk.php
@@ -8,7 +8,7 @@ $admin_group="staff";
 
 $admin = isset($session->groups[$admin_group]);
 
-// If your an admin and the path ends in Edit/ then a number put it into edit mode
+// If you're an admin and the path ends in Edit/ then a number put it into edit mode
 // create canedit rules
 $canedit=isset($pathlist[($component[depth]/2)+1]);
 $canedit=$canedit && isset($pathlist[($component[depth]/2)+2]);
@@ -26,7 +26,7 @@ if ($canedit){
 	}
 }
 
-// If your an admin and the path ends in Add put it into add mode
+// If you're an admin and the path ends in Add put it into add mode
 // create canadd rules
 $canadd=isset($pathlist[($component[depth]/2)+1]);
 $canadd=$canadd && $pathlist[($component[depth]/2)+1]=='Add';
@@ -35,7 +35,7 @@ $canadd=$canadd && $admin;
 if ($canadd){$mode = "add";}
 
 
-// Process actions before retriving the data
+// Process actions before retrieving the data
 // List request data
 if ($session->loggedin && isset($_REQUEST['action'])) {
 	// Junk Requests
@@ -47,7 +47,7 @@ if ($session->loggedin && isset($_REQUEST['action'])) {
 	elseif ($_REQUEST['action'] == "Un-Request") {
 		// Un-Request Item
 		if ($admin){
-			// if your admin just do it
+			// if you're admin just do it
 			$query = "UPDATE inventory SET requested_by=null, requested_on=null WHERE id=?";
                         $array = array($_REQUEST['item']);
 		}
@@ -59,22 +59,22 @@ if ($session->loggedin && isset($_REQUEST['action'])) {
 		$DB->Query($query, $array);
 	}
 	elseif ($_REQUEST['action'] == "Take" && $admin){
-		// Take item, if your admin
+		// Take item, if you're admin
 		$query = "UPDATE inventory SET taken_on=now() WHERE id=? AND requested_by IS NOT NULL";
 		$DB->Query($query, $_REQUEST['item']);
 	}
 	elseif ($_REQUEST['action'] == "Remove" && $admin){
-		// Remove item, if your admin and its been taken
+		// Remove item, if you're admin and its been taken
 		$query = "DELETE FROM inventory WHERE id=? AND taken_on IS NOT NULL";
 		$DB->Query($query, $_REQUEST['item']);
 	}
 	elseif ($_REQUEST['action'] == "Not Junk" && $admin){
-		// Mark item as not junk if its not been requested and your admin
+		// Mark item as not junk if it's not been requested and you're admin
 		$query = "UPDATE inventory SET status='unknown' WHERE id=? AND requested_by IS NULL";
 		$DB->Query($query, $_REQUEST['item']);
 	}
 	elseif ($_REQUEST['action'] =="Junk" && $admin){
-		// Mark item as junk, if your admin
+		// Mark item as junk, if you're admin
 		$query = "UPDATE inventory SET status='junk' WHERE id=?";
                 $DB->Query($query, $_REQUEST['item']);
 	}
@@ -125,7 +125,23 @@ if ($mode == 'list'){
 	else{
 		$junk = $DB->GetAll("SELECT * FROM inventory WHERE status = 'junk' ORDER BY category, title, id");
 	}
-	$smarty->assign("junk", $junk);
+
+	// group the junk by status then by category 
+	foreach($junk as $junkitem) {
+		if ($junkitem['status'] != 'junk') {
+			$status = "unknown";
+		} else if ($junkitem['requested_by'] == null) {
+			$status = "available"; 
+		} else if ($junkitem['taken_on'] == null) {
+			$status = "requested";
+		} else {
+			$status = "taken";
+		} 
+		
+		$sortedjunk[$status][$junkitem['category']][] = $junkitem; 
+	}
+
+	$smarty->assign("junk", $sortedjunk);
 	
 	// Check there is some junk
 	if (sizeof($junk) < 1) {
diff --git a/templates/junk.tpl b/templates/junk.tpl
index 16d5653..f3cedb9 100644
--- a/templates/junk.tpl
+++ b/templates/junk.tpl
@@ -1,61 +1,60 @@
 {if $mode == 'list'}
-{foreach name=junk from=$junk item=junk}
-<div class="box">
-	<div class="boxhead">
-		<h3>{$junk.title}</h3>
-	</div>
+{foreach name=junk from=$junk key=status item=statusitems}
+	<div class="box">
+	<div class="boxhead"> <h3>{$status}</h3></div>
 	<div class="boxcontent">
-		<p>
-			<h3>{$junk.category}</h3>
-			{$junk.description}
-			{if $junk.donated_by != null}
-			<br /><strong>Donated by:</strong>{$junk.donated_by}
+	{foreach from=$statusitems item=category key=categoryname} 
+		<h3>{$categoryname}s</h3>
+		<ul>
+	{foreach from=$category item=item}
+		<li>	
+			{$item.title}: {$item.description}
+		{if $status == 'requested'}
+			<br /><small>Requested by {$item.requested_by} on {$item.requested_on|date_format:"%e %b %Y"}</small>
+		{elseif $status  == "taken"}
+			<br /><small>Taken by {$item.requested_by} on {$item.taken_on|date_format:"%e %b %Y"}</small>
+		{/if}
+			{if $item.donated_by != null}
+			<br /><strong>Donated by:</strong>{$item.donated_by}
 			{/if}
 		{if $session->loggedin}
-		{if $admin || $junk.status=='junk'}
-		<form action="{$url}" method="POST">
-			<input type="hidden" name="item" value="{$junk.id}" />
-			{if $junk.status=='junk'}
-			{if $junk.requested_by==null}
-			<input type="submit" name="action" value="Request" />
-			{elseif ($junk.requested_by==$session->username || $admin) && $junk.taken_on==null}
-			<input type="submit" name="action" value="Un-Request" />
-			{/if}
-			{if $admin}
-			{if $junk.requested_by!=null && $junk.taken_on==null}
-			<input type="submit" name="action" value="Take" />
-			{elseif $junk.taken_on!=null}
-			<input type="submit" name="action" value="Remove" />
-			{elseif $junk.requested_by==null}
-			<input type="submit" name="action" value="Not Junk" />
-			{/if}
+			{if $admin || $item.status=='junk'}
+			<form action="{$url}" method="POST">
+				<input type="hidden" name="item" value="{$item.id}" />
+				{if $item.status=='junk'}
+				{if $item.requested_by==null}
+				<input type="submit" name="action" value="Request" />
+				{elseif ($item.requested_by==$session->username || $admin) && $item.taken_on==null}
+				<input type="submit" name="action" value="Un-Request" />
+				{/if}
+				{if $admin}
+				{if $item.requested_by!=null && $item.taken_on==null}
+				<input type="submit" name="action" value="Take" />
+				{elseif $item.taken_on!=null}
+				<input type="submit" name="action" value="Remove" />
+				{elseif $item.requested_by==null}
+				<input type="submit" name="action" value="Not Junk" />
+				{/if}
+				{/if}
+				{elseif $admin}
+				<input type="submit" name="action" value="Junk" />
+				{/if}
+			</form>
 			{/if}
-			{elseif $admin}
-			<input type="submit" name="action" value="Junk" />
-			{/if}
-		</form>
-		{/if}
 		{/if}
-		</p>
-		{if $admin && $junk.requested_by==null}
+		</li>
+		{if $admin && $item.requested_by==null}
 			<div class="edit">
-				<a href="{$componentpath}/Edit/{$junk.id}">Edit</a>
+				<a href="{$componentpath}/Edit/{$item.id}">Edit</a>
 				<div class="clear"></div>
 			</div>
 		{/if}
+	{/foreach}
+		</ul>
+{/foreach}
 	</div>
-	<div class="boxfoot"><p>
-	{if $junk.status!='junk'}
-		Not Junk
-	{elseif $junk.requested_by==null}
-		Available
-	{elseif $junk.taken_on==null}
-		Requested by {$junk.requested_by} on {$junk.requested_on|date_format:"%e %b %Y"}
-	{else}
-		Taken by {$junk.requested_by} on {$junk.taken_on|date_format:"%e %b %Y"}
-	{/if}
-	</p></div>
-</div>
+	 <div class="hollowfoot"><div><div></div></div></div>
+</div> 
 {/foreach}
 {if $admin}
 	<div class="edit">
-- 
GitLab