- improve existing category class

- use category class in category admin and appbrowse
 => fix adding a category which was broken
 => fix deleting a category which was broken
 => fix editing a category which was broken
This commit is contained in:
Jonathan Ernst
2005-02-09 02:20:21 +00:00
committed by WineHQ
parent 5e7cceeb4a
commit faf301fed1
4 changed files with 208 additions and 217 deletions

View File

@@ -1,41 +1,57 @@
<?php <?php
include("path.php"); include("path.php");
include(BASE."include/"."incl.php"); require(BASE."include/incl.php");
include(BASE."include/"."tableve.php"); require(BASE."include/category.php");
if(!$_SESSION['current']->hasPriv("admin")) if(!$_SESSION['current']->hasPriv("admin"))
{ {
errorpage(); errorpage();
exit; exit;
} }
else $oCat = new Category($_REQUEST['catId']);
if($_REQUEST['submit'])
{ {
global $admin_mode; $oCat->update($_REQUEST['name'],$_REQUEST['description'],$_REQUEST['parentId']);
$admin_mode = 1; redirect(apidb_fullurl("appbrowse.php?catId=".$oCat->iCatId));
}
apidb_header("Add Application Category");
$t = new TableVE("create");
if($_POST)
{
$t->update($_POST);
} }
else else
{ {
$table = "appCategory"; apidb_header("Add Category");
$query = "INSERT INTO $table VALUES(0, 'NONAME', null, 0)"; $sQuery = "SELECT catId, catName FROM appCategory WHERE catId!='".$_REQUEST['catId']."'";
$hResult = query_appdb($sQuery);
query_appdb("DELETE FROM $table WHERE catName = 'NONAME'"); while($oRow = mysql_fetch_object($hResult))
{
if(debugging()) $aCatsIds[]=$oRow->catId;
echo "$query <br /><br />\n"; $aCatsNames[]=$oRow->catName;
}
$t->create($query, $table, "catId"); echo "<form method=\"post\" action=\"addCategory.php\">
<input type=\"hidden\" name=\"catId\" value=\"".$oCat->iCatId."\" />
<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">
<tr>
<td width=\"15%\" class=\"box-label\"><b>Category name</b></td>
<td class=\"box-body\">
<input type=\"text\" size=\"50\" name=\"name\" value=\"".$oCat->sName."\" />
</td>
</tr>
<tr>
<td width=\"15%\" class=\"box-label\"><b>Description</b></td>
<td class=\"box-body\">
<input type=\"text\" size=\"50\" name=\"description\" value=\"".$oCat->sDescription."\" />
</td>
</tr>
<tr>
<td width=\"15%\" class=\"box-label\"><b>Parent</b></td>
<td class=\"box-body\">
".html_select("parentId",$aCatsIds,$oCat->iParentId,$aCatsNames)."
</td>
</tr>
<tr>
<td colspan=\"2\" class=\"box-body\">
<input type=\"submit\" name=\"submit\" value=\"Submit\" />
</td>
</tr>
</table>
</form>";
} }
apidb_footer(); apidb_footer();
?> ?>

View File

@@ -15,7 +15,7 @@ function admin_menu()
else $catId=""; else $catId="";
$m = new htmlmenu("Admin"); $m = new htmlmenu("Admin");
$m->add("Edit this Category", BASE."admin/editCategory.php?catId=$catId"); $m->add("Edit this Category", BASE."admin/addCategory.php?catId=$catId");
$url = BASE."admin/deleteAny.php?what=category&catId=$catId&confirmed=yes"; $url = BASE."admin/deleteAny.php?what=category&catId=$catId&confirmed=yes";
$m->add("Delete this Category", "javascript:deleteURL(\"Are you sure?\", \"".$url."\")"); $m->add("Delete this Category", "javascript:deleteURL(\"Are you sure?\", \"".$url."\")");
@@ -34,7 +34,7 @@ if( !is_numeric($catId) )
// list sub categories // list sub categories
$cat = new Category($catId); $cat = new Category($catId);
$catFullPath = make_cat_path($cat->getCategoryPath()); $catFullPath = make_cat_path($cat->getCategoryPath());
$subs = $cat->getCategoryList(); $subs = $cat->aSubcatsIds;
//display admin box //display admin box
if($_SESSION['current']->hasPriv("admin") && $catId != 0) if($_SESSION['current']->hasPriv("admin") && $catId != 0)
@@ -53,31 +53,30 @@ if($subs)
echo "<table width='100%' border=0 cellpadding=3 cellspacing=1>\n\n"; echo "<table width='100%' border=0 cellpadding=3 cellspacing=1>\n\n";
echo "<tr class=color4>\n"; echo "<tr class=color4>\n";
echo " <td><font color=white>Sub Category</font></td>\n"; echo " <td>Sub Category</td>\n";
echo " <td><font color=white>Description</font></td>\n"; echo " <td>Description</td>\n";
echo " <td><font color=white>No. Apps</font></td>\n"; echo " <td>No. Apps</td>\n";
echo "</tr>\n\n"; echo "</tr>\n\n";
$c = 0; while(list($i,$iSubcatId) = each($subs))
while(list($id, list($name, $desc)) = each($subs))
{ {
$oSubCat= new Category($iSubcatId);
//set row color //set row color
$bgcolor = ($c % 2) ? "color0" : "color1"; $bgcolor = ($i % 2) ? "color0" : "color1";
//get number of apps //get number of apps
$appcount = $cat->getAppCount($id); $appcount = sizeof($oSubCat->aApplicationsIds);
//format desc //format desc
$desc = substr(stripslashes($desc),0,70); $desc = substr(stripslashes($oSubCat->sDescription),0,70);
//display row //display row
echo "<tr class=$bgcolor>\n"; echo "<tr class=$bgcolor>\n";
echo " <td><a href='appbrowse.php?catId=$id'>".stripslashes($name)."</a></td>\n"; echo " <td><a href='appbrowse.php?catId=$iSubcatId'>".$oSubCat->sName."</a></td>\n";
echo " <td>$desc &nbsp;</td>\n"; echo " <td>$desc &nbsp;</td>\n";
echo " <td>$appcount &nbsp;</td>\n"; echo " <td>$appcount &nbsp;</td>\n";
echo "</tr>\n\n"; echo "</tr>\n\n";
$c++;
} }
echo "</table>\n\n"; echo "</table>\n\n";
@@ -87,7 +86,7 @@ if($subs)
// list applications in this category // list applications in this category
$apps = $cat->getAppList($catId); $apps = $cat->aApplicationsIds;
if($apps) if($apps)
{ {
echo html_frame_start("",'98%','',2); echo html_frame_start("",'98%','',2);
@@ -98,32 +97,27 @@ if($apps)
echo "<table width='100%' border=0 cellpadding=3 cellspacing=1>\n\n"; echo "<table width='100%' border=0 cellpadding=3 cellspacing=1>\n\n";
echo "<tr class=color4>\n"; echo "<tr class=color4>\n";
echo " <td><font color=white>Application Name</font></td>\n"; echo " <td>Application Name</td>\n";
echo " <td><font color=white>Description</font></td>\n"; echo " <td>Description</td>\n";
echo " <td><font color=white>No. Versions</font></td>\n"; echo " <td>No. Versions</td>\n";
echo "</tr>\n\n"; echo "</tr>\n\n";
$c = 0; while(list($i, $iAppId) = each($apps))
while(list($id, list($name, $desc)) = each($apps))
{ {
//set row color $oApp = new Application($iAppId);
$bgcolor = ($c % 2) ? "color0" : "color1";
//set row color
$bgcolor = ($i % 2) ? "color0" : "color1";
//get number of versions //format desc
$query = query_appdb("SELECT count(*) as versions FROM appVersion WHERE appId = $id AND versionName != 'NONAME'"); $desc = trim_description($oApp->sDescription);
$ob = mysql_fetch_object($query);
//format desc //display row
$desc = substr(stripslashes($desc),0,70); echo "<tr class=$bgcolor>\n";
echo " <td><a href='appview.php?appId=$iAppId'>".$oApp->sName."</a></td>\n";
//display row echo " <td>$desc &nbsp;</td>\n";
echo "<tr class=$bgcolor>\n"; echo " <td>".sizeof($oApp->aVersionsIds)."</td>\n";
echo " <td><a href='appview.php?appId=$id'>".stripslashes($name)."</a></td>\n"; echo "</tr>\n\n";
echo " <td>$desc &nbsp;</td>\n";
echo " <td>$ob->versions &nbsp;</td>\n";
echo "</tr>\n\n";
$c++;
} }
echo "</table>\n\n"; echo "</table>\n\n";

View File

@@ -3,114 +3,147 @@
/* this class represents a category + its children */ /* this class represents a category + its children */
/***************************************************/ /***************************************************/
/**
* Category class for handling categories.
*/
class Category { class Category {
var $iCatId;
var $name; var $iParentId;
var $id; var $sName;
var $subcat; var $sDescription;
var $aApplicationsIds; // an array that contains the appId of every application linked to this category
var $aSubcatsIds; // an array that contains the appId of every application linked to this category
/**
* constructor, fetches the data.
*/
function Category($iCatId = null)
{
// we are working on an existing vendor
if($iCatId=="0" || $iCatId)
{
/*
* We fetch the data related to this vendor.
*/
$sQuery = "SELECT *
FROM appCategory
WHERE catId = ".$iCatId;
if($hResult = query_appdb($sQuery))
{
$oRow = mysql_fetch_object($hResult);
$this->iCatId = $iCatId;
$this->iParentId = $oRow->catParent;
$this->sName = $oRow->catName;
$this->sDescription = $oRow->catDescription;
}
/*
* We fetch applicationsIds.
*/
$sQuery = "SELECT appId
FROM appFamily
WHERE catId = ".$iCatId;
if($hResult = query_appdb($sQuery))
{
while($oRow = mysql_fetch_object($hResult))
{
$this->aApplicationsIds[] = $oRow->appId;
}
}
/*
* We fetch subcatIds.
*/
$sQuery = "SELECT catId
FROM appCategory
WHERE catParent = ".$iCatId;
if($hResult = query_appdb($sQuery))
{
while($oRow = mysql_fetch_object($hResult))
{
$this->aSubcatsIds[] = $oRow->catId;
}
}
}
}
/** /**
* the special name "ROOT" is the top category * Creates a new category.
*/ */
function Category($id = 0) function create($sName=null, $sDescription=null, $iParentId=null)
{ {
$this->id = $id; $aInsert = compile_insert_string(array( 'catName'=> $sName,
$this->load($id); 'catDescription' => $sDescription,
'catParent' => $iParentId ));
$sFields = "({$aInsert['FIELDS']})";
$sValues = "({$aInsert['VALUES']})";
if(query_appdb("INSERT INTO appCategory $sFields VALUES $sValues", "Error while creating a new vendor."))
{
$this->iCatId = mysql_insert_id();
$this->category($this->iCatId);
return true;
}
else
return false;
} }
/**
* Update category.
* Returns true on success and false on failure.
*/
function update($sName=null, $sDescription=null, $iParentId=null)
{
if(!$this->iCatId)
return $this->create($sName, $sDescription, $iParentId);
if($sName)
{
if (!query_appdb("UPDATE appCategory SET catName = '".$sName."' WHERE catId = ".$this->iCatId))
return false;
$this->sName = $sName;
}
if($sDescription)
{
if (!query_appdb("UPDATE appCategory SET catDescription = '".$sDescription."' WHERE catId = ".$this->iCatId))
return false;
$this->sDescription = $sDescription;
}
if($iParentId)
{
if (!query_appdb("UPDATE appCategory SET catParent = '".$iParentId."' WHERE catId = ".$this->iCatId))
return false;
$this->iParentId = $iParentId;
}
return true;
}
/** /**
* Deletes the category from the database. * Deletes the category from the database.
* and request the deletion of linked elements.
*/ */
function delete() function delete($bSilent=false)
{ {
$r = query_appdb("SELECT appId FROM appFamily WHERE catId = ".$this->id,"Failed to delete category ".$this->id); if(sizeof($this->aApplicationsIds)>0)
if($r)
{ {
while($ob = mysql_fetch_object($r)) addmsg("The category has not been deleted because there are still applications linked to it.", "red");
{ } else
$oApp = new Application($ob->appId); {
$oApp->delete(); $sQuery = "DELETE FROM appCategory
} WHERE catId = ".$this->iCatId."
$r = query_appdb("DELETE FROM appCategory WHERE catId = $catId","Failed to delete category $catId"); LIMIT 1";
if($r) query_appdb($sQuery);
addmsg("Category $catId deleted.", "green"); addmsg("The category has been deleted.", "green");
} }
} }
/**
* load the category data into this class
*/
function load($id)
{
$this->id = $id;
if($id == 0)
{
$this->name = "ROOT";
} else
{
$result = query_appdb("SELECT * FROM appCategory WHERE catId = $id");
if(!$result)
{
// category not found!
errorpage("Internal Error: Category not found!");
return;
}
$ob = mysql_fetch_object($result);
$this->name = $ob->catName;
}
$result = query_appdb("SELECT catId, catName, catDescription FROM ".
"appCategory WHERE catParent = $this->id " .
"ORDER BY catName");
if(mysql_num_rows($result) == 0)
return; // no sub categories
$this->subcat = array();
while($row = mysql_fetch_object($result))
{
// ignore NONAME categories
if($row->catName == "NONAME")
continue;
$this->subcat[$row->catId] = array($row->catName, $row->catDescription);
}
}
/**
* resolve the category id by name
*/
function getCategoryId($name)
{
if($name == "ROOT")
return 0;
$result = query_appdb("SELECT catId FROM appCategory WHERE ".
"catName = '$name'");
if(!$result)
return -1;
if(mysql_num_rows($result) != 1)
return -1;
$row = mysql_fetch_object($result);
return $row->catId;
}
/**
* returns the list of sub categories
*
* category list has the following format:
*
* { { catId => { catName, catDescription } }, ... }
*/
function getCategoryList()
{
return $this->subcat;
}
/** /**
* returns a path like: * returns a path like:
@@ -120,67 +153,25 @@ class Category {
function getCategoryPath() function getCategoryPath()
{ {
$path = array(); $path = array();
$id = $this->id; $iCatId = $this->iCatId;
while(1) while($iCatId != 0)
{ {
$result = query_appdb("SELECT catName, catId, catParent FROM appCategory WHERE catId = $id"); $result = query_appdb("SELECT catName, catId, catParent FROM appCategory WHERE catId = $iCatId");
if(!$result || mysql_num_rows($result) != 1) if(!$result || mysql_num_rows($result) != 1)
break; break;
$cat = mysql_fetch_object($result); $cat = mysql_fetch_object($result);
$path[] = array($cat->catId, $cat->catName); $path[] = array($cat->catId, $cat->catName);
$id = $cat->catParent; $iCatId = $cat->catParent;
} }
$path[] = array(0, "ROOT"); $path[] = array(0, "ROOT");
return array_reverse($path); return array_reverse($path);
} }
}
/**
* returns a list of applications in the specified category
*/
function getAppList($id)
{
$result = query_appdb("SELECT appId, appName, description FROM ".
"appFamily WHERE catId = $id ".
"ORDER BY appName");
if(!$result || mysql_num_rows($result) == 0)
return array();
$list = array();
while($row = mysql_fetch_object($result))
{
if($row->appName == "NONAME")
continue;
$list[$row->appId] = array($row->appName, $row->description);
}
return $list;
}
/** /*
* returns the number of apps in the specified category * Application functions that are not part of the class
*/ */
function getAppCount($id, $recurse = 1)
{
$total = 0;
$result = query_appdb("SELECT appId FROM appFamily WHERE catId = $id");
if($result)
$total += mysql_num_rows($result);
if($recurse)
{
$result = query_appdb("SELECT catId FROM appCategory WHERE catParent = $id");
if($result)
{
while($ob = mysql_fetch_object($result))
$total += $this->getAppCount($ob->catId, 1);
}
}
return $total;
}
};
/** /**
* create the Category: line at the top of appdb pages$ * create the Category: line at the top of appdb pages$
@@ -189,7 +180,7 @@ function make_cat_path($path, $appId = '', $versionId = '')
{ {
$str = ""; $str = "";
$catCount = 0; $catCount = 0;
while(list($idx, list($id, $name)) = each($path)) while(list($iCatIdx, list($iCatId, $name)) = each($path))
{ {
if($name == "ROOT") if($name == "ROOT")
$catname = "Main"; $catname = "Main";
@@ -197,7 +188,7 @@ function make_cat_path($path, $appId = '', $versionId = '')
$catname = $name; $catname = $name;
if ($catCount > 0) $str .= " &gt; "; if ($catCount > 0) $str .= " &gt; ";
$str .= html_ahref($catname,"appbrowse.php?catId=$id"); $str .= html_ahref($catname,"appbrowse.php?catId=$iCatId");
$catCount++; $catCount++;
} }
@@ -218,16 +209,4 @@ function make_cat_path($path, $appId = '', $versionId = '')
return $str; return $str;
} }
function lookupCategoryName($catId)
{
$sResult = query_appdb("SELECT * FROM appCategory ".
"WHERE catId = ".$catId);
if(!$sResult || mysql_num_rows($sResult) != 1)
return "Unknown category";
$ob = mysql_fetch_object($sResult);
return $ob->catName;
}
?> ?>

View File

@@ -1,5 +1,7 @@
use apidb; use apidb;
INSERT INTO appCategory VALUES ('0', 'MAIN', 'Root category', NULL);
UPDATE appCategory SET catId = '0' WHERE catName = 'MAIN' LIMIT 1;
INSERT INTO appCategory VALUES (1, 'Audio', 'Audio related applications', 29); INSERT INTO appCategory VALUES (1, 'Audio', 'Audio related applications', 29);
INSERT INTO appCategory VALUES (2, 'Games', 'Games', 0); INSERT INTO appCategory VALUES (2, 'Games', 'Games', 0);
INSERT INTO appCategory VALUES (3, 'Graphics', 'Graphics viewers, editors, graphics demos, screensavers etc.', 29); INSERT INTO appCategory VALUES (3, 'Graphics', 'Graphics viewers, editors, graphics demos, screensavers etc.', 29);