art-web r542 - in branches/art-hub: controllers models



Author: thos
Date: Tue Jun 17 21:15:21 2008
New Revision: 542
URL: http://svn.gnome.org/viewvc/art-web?rev=542&view=rev

Log:
* Create a generic "art" model and use it for the backgrounds and themes models


Added:
   branches/art-hub/models/art.php
Modified:
   branches/art-hub/controllers/backgrounds.php
   branches/art-hub/controllers/themes.php
   branches/art-hub/models/backgrounds.php
   branches/art-hub/models/themes.php

Modified: branches/art-hub/controllers/backgrounds.php
==============================================================================
--- branches/art-hub/controllers/backgrounds.php	(original)
+++ branches/art-hub/controllers/backgrounds.php	Tue Jun 17 21:15:21 2008
@@ -5,7 +5,7 @@
 /* load model */
 require ("models/backgrounds.php");
 
-$bg = new Backgrounds();
+$bg = new BackgroundsModel();
 
 preg_match ('/^\/backgrounds\/(abstract|gnome|nature|other)\/?$/', $_SERVER['PHP_SELF'], $params);
 $category = $params[1];
@@ -21,14 +21,17 @@
 $start = ($page - 1) * $limit;
 
 if ($category)
-  $view_data = $bg->get_backgrounds ($category, $start, $limit, "name");
+  $view_data = $bg->get_items ($category, $start, $limit, "name");
 else
   $view_data = null;
 
 $bg_res = array ();
-foreach ($view_data as $b)
+if ($view_data)
 {
-  $bg_res[$b['backgroundID']] = $bg->get_resolutions ($b['backgroundID']);
+  foreach ($view_data as $b)
+  {
+    $bg_res[$b['backgroundID']] = $bg->get_resolutions ($b['backgroundID']);
+  }
 }
 
 $total_backgrounds = $bg->get_total ($category);

Modified: branches/art-hub/controllers/themes.php
==============================================================================
--- branches/art-hub/controllers/themes.php	(original)
+++ branches/art-hub/controllers/themes.php	Tue Jun 17 21:15:21 2008
@@ -5,7 +5,7 @@
 /* load model */
 require ("models/themes.php");
 
-$themes = new Themes();
+$themes = new ThemesModel();
 
 preg_match ('/^\/themes\/(gtk2|metacity|icon|gdm_greeter|splash_screens|gtk_engines)\/?$/',
             $_SERVER['PHP_SELF'], $params);
@@ -22,7 +22,7 @@
 $start = ($page - 1) * $limit;
 
 if ($category)
-  $view_data = $themes->get_themes ($category, $start, $limit, "name");
+  $view_data = $themes->get_items ($category, $start, $limit, "name");
 else
   $view_data = null;
 

Added: branches/art-hub/models/art.php
==============================================================================
--- (empty file)
+++ branches/art-hub/models/art.php	Tue Jun 17 21:15:21 2008
@@ -0,0 +1,48 @@
+<?php
+require ("mysql.inc.php");
+
+abstract class ArtModel
+{
+  var $get_items_sql = '';
+  var $get_total_sql = '';
+
+  function get_items ($category, $start, $length, $order)
+  {
+    /* check that start and length values are numeric */
+    if (!is_numeric ($start) || !is_numeric ($length))
+      return;
+
+    $sql = sprintf ($this->get_items_sql, $category, $order, $start, $length);
+
+    if ($sql === '')
+      return;
+
+    $bg_select_result = mysql_query ($sql);
+    if (!$bg_select_result)
+      printf ("Database error: %s", mysql_error());
+
+    $table = Array ();
+    while ($row = mysql_fetch_assoc ($bg_select_result))
+    {
+      $table[] = $row;
+    }
+
+    return $table;
+  }
+
+  function get_total ($category)
+  {
+    $sql = sprintf ($this->get_total_sql, $category);
+    $r = mysql_query ($sql);
+    if (!$r)
+      printf ("Database error: %s", mysql_error());
+
+
+    $total = mysql_fetch_row ($r);
+
+    return $total[0];
+  }
+
+}
+
+?>

Modified: branches/art-hub/models/backgrounds.php
==============================================================================
--- branches/art-hub/models/backgrounds.php	(original)
+++ branches/art-hub/models/backgrounds.php	Tue Jun 17 21:15:21 2008
@@ -1,40 +1,15 @@
 <?php
-require ("mysql.inc.php");
+require ('models/art.php');
 
-class Backgrounds
+class BackgroundsModel extends ArtModel
 {
-  function get_backgrounds ($category, $start, $length, $order)
-  {
-    $sql = "SELECT * FROM background,user
-            WHERE status='active' AND category = '$category'
+  var $get_items_sql = "SELECT * FROM background,user
+            WHERE status='active' AND category = '%s'
             AND background.userID = user.userID
-            ORDER BY $order LIMIT $start,$length ";
-
-    $bg_select_result = mysql_query ($sql);
-    if (!$bg_select_result)
-      printf ("Database error: %s", mysql_error());
-    $table = Array ();
-    while ($row = mysql_fetch_assoc ($bg_select_result))
-    {
-      $table[] = $row;
-    }
-
-    return $table;
-  }
-
-  function get_total ($category)
-  {
-    $sql = "SELECT COUNT(name) FROM background
-            WHERE category = '$category'";
-    $r = mysql_query ($sql);
-    if (!$r)
-      printf ("Database error: %s", mysql_error());
-
+            ORDER BY %s LIMIT %s,%s";
 
-    $total = mysql_fetch_row ($r);
-
-    return $total[0];
-  }
+  var $get_total_sql = "SELECT COUNT(name) FROM background
+            WHERE category = '%s'";
 
   function get_resolutions ($backgroundID)
   {

Modified: branches/art-hub/models/themes.php
==============================================================================
--- branches/art-hub/models/themes.php	(original)
+++ branches/art-hub/models/themes.php	Tue Jun 17 21:15:21 2008
@@ -1,40 +1,15 @@
 <?php
-require ("mysql.inc.php");
+require ('models/art.php');
 
-class Themes
+class ThemesModel extends ArtModel
 {
-  function get_themes ($category, $start, $length, $order)
-  {
-    $sql = "SELECT * FROM theme,user
-            WHERE status='active' AND category = '$category'
+  var $get_items_sql = "SELECT * FROM theme,user
+            WHERE status='active' AND category = '%s'
             AND theme.userID = user.userID
-            ORDER BY $order LIMIT $start,$length ";
+            ORDER BY %s LIMIT %s,%s";
 
-    $r = mysql_query ($sql);
-    if (!$r)
-      printf ("Database error: %s", mysql_error());
-    $table = Array ();
-    while ($row = mysql_fetch_assoc ($r))
-    {
-      $table[] = $row;
-    }
-
-    return $table;
-  }
-
-  function get_total ($category)
-  {
-    $sql = "SELECT COUNT(name) FROM theme
-            WHERE category = '$category'";
-    $r = mysql_query ($sql);
-    if (!$r)
-      printf ("Database error: %s", mysql_error());
-
-
-    $total = mysql_fetch_row ($r);
-
-    return $total[0];
-  }
+  var $get_total_sql = "SELECT COUNT(name) FROM theme
+            WHERE category = '%s'";
 }
 
 ?>



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