[art-web] Show background items in the top level backgrounds directory



commit 2ff87933f5a6b315cfa3651c57ee385290c85c73
Author: Thomas Wood <thos gnome org>
Date:   Wed Dec 30 11:15:13 2009 +0000

    Show background items in the top level backgrounds directory
    
    Rather than show a category list and search box, show a list of all
    backgrounds, with options to filter this list.
    
    [models]
     * Remove category parameter from filtered models
     * Add get_category_list function
    
    [views/backgrounds]
     * Add category list to filter box
    
    [controllers/backgrounds]
     * Always get a list of themes, even if no category is set

 controllers/backgrounds.php |   64 +++++++++++++++++++-----------------------
 models/art.php              |   10 +++---
 models/backgrounds.php      |   37 +++++++++++++++++-------
 views/backgrounds.php       |   26 ++++++++++++++---
 4 files changed, 81 insertions(+), 56 deletions(-)
---
diff --git a/controllers/backgrounds.php b/controllers/backgrounds.php
index 7ec948e..a56d2f9 100644
--- a/controllers/backgrounds.php
+++ b/controllers/backgrounds.php
@@ -79,47 +79,42 @@ if ($sort)
 else
   $sortby = 'name';
 
-$filter = GET_COOKIE ('filter', null);
-if ($filter == 'none')
-  $filter = null;
+$filter_sql = 'TRUE';
+$resolution = GET_COOKIE ('resolution', null);
 
-$start = ($page - 1) * $limit;
+if ($resolution && $resolution != 'none')
+  $filter_sql .= ' AND resolution="' . mysql_real_escape_string ($resolution) . '"';
 
 if ($category)
-  if ($category == "search")
-  {
-    $search = mysql_escape_string (GET ('text'));
-    $search = "background.name LIKE '%".$search."%'";
-    $search_text = htmlspecialchars (GET ('text'));
+  $filter_sql .= " AND category='$category'";
 
-    $view_data = $bg->search_items ($search, $start, $limit, $sortby);
-    $total_backgrounds = $bg->search_total ($search);
+$start = ($page - 1) * $limit;
+
+if ($category == 'search')
+{
+  $search = mysql_escape_string (GET ('text'));
+  $search = "background.name LIKE '%".$search."%'";
+  $search_text = htmlspecialchars (GET ('text'));
+
+  $view_data = $bg->search_items ($search, $start, $limit, $sortby);
+  $total_backgrounds = $bg->search_total ($search);
+}
+else
+{
+  if ($background_id)
+  {
+    $view_data = $bg->get_single_item ($category, $background_id);
+    $total_backgrounds = 1;
   }
   else
   {
-    if ($background_id)
-    {
-      $view_data = $bg->get_single_item ($category, $background_id);
-      $total_backgrounds = 1;
-    }
-    else
+    if ($filter_sql)
     {
-      if ($filter)
-      {
-        $filter_sql = 'resolution="' . mysql_real_escape_string ($filter) . '"';
-        $view_data = $bg->get_filtered_items ($category, $start, $limit,
-                                              $sortby, $filter_sql);
-        $total_backgrounds = $bg->get_filtered_total ($category, $filter_sql);
-      }
-      else
-      {
-        $view_data = $bg->get_items ($category, $start, $limit, $sortby);
-        $total_backgrounds = $bg->get_total ($category);
-      }
+      $view_data = $bg->get_filtered_items ($start, $limit, $sortby, $filter_sql);
+      $total_backgrounds = $bg->get_filtered_total ($filter_sql);
     }
   }
-else
-  $view_data = null;
+}
 
 $bg_res = array ();
 if ($view_data)
@@ -130,10 +125,9 @@ if ($view_data)
   }
 }
 
-if ($category)
-  $resolution_filter = $bg->get_resolution_list ($category);
-else
-  $resolution_filter = array ();
+$resolution_filter = $bg->get_resolution_list ($category);
+
+$category_filter = $bg->get_category_list ();
 
 /* load view */
 require ("views/backgrounds.php");
diff --git a/models/art.php b/models/art.php
index ecff2ef..2b4d11d 100644
--- a/models/art.php
+++ b/models/art.php
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright (C) 2008 Thomas Wood <thos gnome org>
+ * Copyright (C) 2008, 2009 Thomas Wood <thos gnome org>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -122,13 +122,13 @@ abstract class ArtModel
     return $total[0];
   }
 
-  function get_filtered_items ($category, $start, $length, $order, $filter)
+  function get_filtered_items ($start, $length, $order, $filter)
   {
     /* check that start and length values are numeric */
     if (!is_numeric ($start) || !is_numeric ($length))
       return;
 
-    $sql = sprintf ($this->get_filtered_items_sql, $category, $filter, $order,
+    $sql = sprintf ($this->get_filtered_items_sql, $filter, $order,
                     $start, $length);
 
     if ($sql === '')
@@ -147,9 +147,9 @@ abstract class ArtModel
     return $table;
   }
 
-  function get_filtered_total ($category, $filter)
+  function get_filtered_total ($filter)
   {
-    $sql = sprintf ($this->get_filtered_total_sql, $category, $filter);
+    $sql = sprintf ($this->get_filtered_total_sql, $filter);
     $r = mysql_query ($sql);
     if (!$r)
       printf ("Database error: %s", mysql_error());
diff --git a/models/backgrounds.php b/models/backgrounds.php
index 9e1edd2..07afb19 100644
--- a/models/backgrounds.php
+++ b/models/backgrounds.php
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright (C) 2008 Thomas Wood <thos gnome org>
+ * Copyright (C) 2008, 2009 Thomas Wood <thos gnome org>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -36,18 +36,22 @@ class BackgroundsModel extends ArtModel
             INNER JOIN user ON background.userID = user.userID
             RIGHT JOIN background_resolution
             ON background_resolution.backgroundID = background.backgroundID
-            WHERE status='active' AND category = '%s'
+            WHERE status='active'
             AND background.backgroundID > 1000
             AND (%s)
+            GROUP BY background.backgroundID
             ORDER BY %s LIMIT %s,%s";
 
-  var $get_filtered_total_sql = "SELECT COUNT(name) FROM background
+  var $get_filtered_total_sql = "
+        SELECT COUNT(*) FROM (
+            SELECT background.backgroundID FROM background
             INNER JOIN user ON background.userID = user.userID
             RIGHT JOIN background_resolution
             ON background_resolution.backgroundID = background.backgroundID
-            WHERE status='active' AND category = '%s'
+            WHERE status='active'
             AND background.backgroundID > 1000
-            AND (%s)";
+            AND (%s)
+            GROUP BY background.backgroundID) AS a";
 
   var $get_total_sql = "SELECT COUNT(name) FROM background
             WHERE category = '%s' AND status='active'
@@ -85,12 +89,14 @@ class BackgroundsModel extends ArtModel
   {
     $sql = "SELECT resolution FROM background_resolution
             INNER JOIN background
-            ON background.backgroundID = background_resolution.backgroundID
-            WHERE background.category = '$category'
-            AND background.status = 'active'
-            AND background.backgroundID > 1000
-            GROUP BY resolution
-            ORDER BY resolution";
+            ON background.backgroundID = background_resolution.backgroundID WHERE ";
+    if ($category)
+      $sql .= "background.category = '$category' AND ";
+    $sql .= "background.status = 'active'
+             AND background.backgroundID > 1000
+             GROUP BY resolution
+             ORDER BY resolution";
+
     $r = mysql_query ($sql);
     $res = array ();
     $res['none'] = 'All';
@@ -100,6 +106,15 @@ class BackgroundsModel extends ArtModel
 
     return $res;
   }
+
+  function get_category_list ()
+  {
+    return array (''         => 'All',
+                  'gnome'    => 'GNOME',
+                  'nature'   => 'Nature',
+                  'abstract' => 'Abstract',
+                  'other'    => 'Other');
+  }
 }
 
 ?>
diff --git a/views/backgrounds.php b/views/backgrounds.php
index 262e6db..eb0da8a 100644
--- a/views/backgrounds.php
+++ b/views/backgrounds.php
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright (C) 2008 Thomas Wood <thos gnome org>
+ * Copyright (C) 2008, 2009 Thomas Wood <thos gnome org>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -71,10 +71,24 @@ function selected ($a, $b)
 }
 
 ?>
-<h2><a href="/backgrounds">Backgrounds</a> / <?php echo $d_category?></a></h2>
+<h2><a href="/backgrounds">Backgrounds</a>
+<?php if ($d_category) echo '/ '.$d_category ?></h2>
 
 <div style="text-align:center"><?php $p->print_pagination (); ?></div>
 <form method="get" style="text-align:center; font-size: small;">
+
+  <label>Category:
+  <select name="category"
+    onchange="this.form.action='/backgrounds/'+this.form.category.value;
+    this.form.submit()"
+    style="font-size: small">
+  <?php foreach ($category_filter as $value => $name): ?>
+    <option value=<?php echo "'$value'"; selected ($category, $value) ?>>
+    <?php echo $name ?></option>
+  <?php endforeach ?>
+  </select>
+  </label>
+
   <label>Sort By:
   <select name="sort" onchange="this.form.submit()" style="font-size: small">
     <option value="name"<?php selected ($sort, 'name')?>>Name</option>
@@ -83,9 +97,9 @@ function selected ($a, $b)
   </label>
 
   <label>Size:
-  <select name="filter" onchange="this.form.submit()" style="font-size: small">
+  <select name="resolution" onchange="this.form.submit()" style="font-size: small">
   <?php foreach ($resolution_filter as $value => $name): ?>
-    <option value=<?php echo $value; selected ($filter, $value) ?>>
+    <option value=<?php echo "'$value'"; selected ($resolution, $value) ?>>
     <?php echo $name ?></option>
   <?php endforeach ?>
   </select>
@@ -113,7 +127,8 @@ String.prototype.rot13 = rot13 = function(s)
 <div align="center">
 <i>No results, try adjusting the size filter.</i>
 </div>
-<?php endif ?>
+
+<?php else: ?>
 
 <?php foreach ($view_data as $row): ?>
 <div class="list-item">
@@ -140,6 +155,7 @@ String.prototype.rot13 = rot13 = function(s)
 </div>
 <?php endforeach ?>
 
+<?php endif /* (!$view_data) */ ?>
 
 <br clear="both">
 <br>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]