- 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
include("path.php");
include(BASE."include/"."incl.php");
include(BASE."include/"."tableve.php");
require(BASE."include/incl.php");
require(BASE."include/category.php");
if(!$_SESSION['current']->hasPriv("admin"))
{
errorpage();
exit;
}
else
$oCat = new Category($_REQUEST['catId']);
if($_REQUEST['submit'])
{
global $admin_mode;
$admin_mode = 1;
}
apidb_header("Add Application Category");
$t = new TableVE("create");
if($_POST)
{
$t->update($_POST);
$oCat->update($_REQUEST['name'],$_REQUEST['description'],$_REQUEST['parentId']);
redirect(apidb_fullurl("appbrowse.php?catId=".$oCat->iCatId));
}
else
{
$table = "appCategory";
$query = "INSERT INTO $table VALUES(0, 'NONAME', null, 0)";
query_appdb("DELETE FROM $table WHERE catName = 'NONAME'");
if(debugging())
echo "$query <br /><br />\n";
$t->create($query, $table, "catId");
apidb_header("Add Category");
$sQuery = "SELECT catId, catName FROM appCategory WHERE catId!='".$_REQUEST['catId']."'";
$hResult = query_appdb($sQuery);
while($oRow = mysql_fetch_object($hResult))
{
$aCatsIds[]=$oRow->catId;
$aCatsNames[]=$oRow->catName;
}
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();
?>

View File

@@ -15,7 +15,7 @@ function admin_menu()
else $catId="";
$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";
$m->add("Delete this Category", "javascript:deleteURL(\"Are you sure?\", \"".$url."\")");
@@ -34,7 +34,7 @@ if( !is_numeric($catId) )
// list sub categories
$cat = new Category($catId);
$catFullPath = make_cat_path($cat->getCategoryPath());
$subs = $cat->getCategoryList();
$subs = $cat->aSubcatsIds;
//display admin box
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 "<tr class=color4>\n";
echo " <td><font color=white>Sub Category</font></td>\n";
echo " <td><font color=white>Description</font></td>\n";
echo " <td><font color=white>No. Apps</font></td>\n";
echo " <td>Sub Category</td>\n";
echo " <td>Description</td>\n";
echo " <td>No. Apps</td>\n";
echo "</tr>\n\n";
$c = 0;
while(list($id, list($name, $desc)) = each($subs))
while(list($i,$iSubcatId) = each($subs))
{
$oSubCat= new Category($iSubcatId);
//set row color
$bgcolor = ($c % 2) ? "color0" : "color1";
$bgcolor = ($i % 2) ? "color0" : "color1";
//get number of apps
$appcount = $cat->getAppCount($id);
$appcount = sizeof($oSubCat->aApplicationsIds);
//format desc
$desc = substr(stripslashes($desc),0,70);
$desc = substr(stripslashes($oSubCat->sDescription),0,70);
//display row
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>$appcount &nbsp;</td>\n";
echo "</tr>\n\n";
$c++;
}
echo "</table>\n\n";
@@ -87,7 +86,7 @@ if($subs)
// list applications in this category
$apps = $cat->getAppList($catId);
$apps = $cat->aApplicationsIds;
if($apps)
{
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 "<tr class=color4>\n";
echo " <td><font color=white>Application Name</font></td>\n";
echo " <td><font color=white>Description</font></td>\n";
echo " <td><font color=white>No. Versions</font></td>\n";
echo " <td>Application Name</td>\n";
echo " <td>Description</td>\n";
echo " <td>No. Versions</td>\n";
echo "</tr>\n\n";
$c = 0;
while(list($id, list($name, $desc)) = each($apps))
while(list($i, $iAppId) = each($apps))
{
//set row color
$bgcolor = ($c % 2) ? "color0" : "color1";
$oApp = new Application($iAppId);
//get number of versions
$query = query_appdb("SELECT count(*) as versions FROM appVersion WHERE appId = $id AND versionName != 'NONAME'");
$ob = mysql_fetch_object($query);
//set row color
$bgcolor = ($i % 2) ? "color0" : "color1";
//format desc
$desc = substr(stripslashes($desc),0,70);
$desc = trim_description($oApp->sDescription);
//display row
echo "<tr class=$bgcolor>\n";
echo " <td><a href='appview.php?appId=$id'>".stripslashes($name)."</a></td>\n";
echo " <td><a href='appview.php?appId=$iAppId'>".$oApp->sName."</a></td>\n";
echo " <td>$desc &nbsp;</td>\n";
echo " <td>$ob->versions &nbsp;</td>\n";
echo " <td>".sizeof($oApp->aVersionsIds)."</td>\n";
echo "</tr>\n\n";
$c++;
}
echo "</table>\n\n";

View File

@@ -3,115 +3,148 @@
/* this class represents a category + its children */
/***************************************************/
/**
* Category class for handling categories.
*/
class Category {
var $name;
var $id;
var $subcat;
var $iCatId;
var $iParentId;
var $sName;
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
/**
* the special name "ROOT" is the top category
* constructor, fetches the data.
*/
function Category($id = 0)
function Category($iCatId = null)
{
$this->id = $id;
$this->load($id);
// 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;
}
}
}
}
/**
* Creates a new category.
*/
function create($sName=null, $sDescription=null, $iParentId=null)
{
$aInsert = compile_insert_string(array( 'catName'=> $sName,
'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.
* 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($r)
if(sizeof($this->aApplicationsIds)>0)
{
while($ob = mysql_fetch_object($r))
{
$oApp = new Application($ob->appId);
$oApp->delete();
}
$r = query_appdb("DELETE FROM appCategory WHERE catId = $catId","Failed to delete category $catId");
if($r)
addmsg("Category $catId deleted.", "green");
}
}
/**
* load the category data into this class
*/
function load($id)
{
$this->id = $id;
if($id == 0)
{
$this->name = "ROOT";
addmsg("The category has not been deleted because there are still applications linked to it.", "red");
} 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);
$sQuery = "DELETE FROM appCategory
WHERE catId = ".$this->iCatId."
LIMIT 1";
query_appdb($sQuery);
addmsg("The category has been deleted.", "green");
}
}
/**
* 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:
*
@@ -120,67 +153,25 @@ class Category {
function getCategoryPath()
{
$path = array();
$id = $this->id;
while(1)
$iCatId = $this->iCatId;
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)
break;
$cat = mysql_fetch_object($result);
$path[] = array($cat->catId, $cat->catName);
$id = $cat->catParent;
$iCatId = $cat->catParent;
}
$path[] = array(0, "ROOT");
return array_reverse($path);
}
}
/**
* returns a list of applications in the specified category
/*
* Application functions that are not part of the class
*/
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
*/
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$
@@ -189,7 +180,7 @@ function make_cat_path($path, $appId = '', $versionId = '')
{
$str = "";
$catCount = 0;
while(list($idx, list($id, $name)) = each($path))
while(list($iCatIdx, list($iCatId, $name)) = each($path))
{
if($name == "ROOT")
$catname = "Main";
@@ -197,7 +188,7 @@ function make_cat_path($path, $appId = '', $versionId = '')
$catname = $name;
if ($catCount > 0) $str .= " &gt; ";
$str .= html_ahref($catname,"appbrowse.php?catId=$id");
$str .= html_ahref($catname,"appbrowse.php?catId=$iCatId");
$catCount++;
}
@@ -218,16 +209,4 @@ function make_cat_path($path, $appId = '', $versionId = '')
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;
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 (2, 'Games', 'Games', 0);
INSERT INTO appCategory VALUES (3, 'Graphics', 'Graphics viewers, editors, graphics demos, screensavers etc.', 29);