Browse apps: Include sub-categories when filtering by category

This commit is contained in:
Alexander Nicolaysen Sørnes
2009-04-05 16:01:11 +02:00
parent 460af69ae5
commit 7cbc67f1f5
4 changed files with 88 additions and 16 deletions

View File

@@ -993,6 +993,18 @@ class Application {
$sExtraTables .= ',appData'; $sExtraTables .= ',appData';
$sWhereFilter .= " AND appData.type = 'downloadurl' AND appData.versionId = appVersion.versionId AND appData.state = 'accepted'"; $sWhereFilter .= " AND appData.type = 'downloadurl' AND appData.versionId = appVersion.versionId AND appData.state = 'accepted'";
} }
if($aOptions['appCategory'])
{
$oCategory = new Category($aOptions['appCategory']);
$aSubCats = $oCategory->getSubCatList();
$sWhereFilter .= " AND ( catId = '{$aOptions['appCategory']}' ";
foreach($aSubCats as $oCat)
{
$iCatId = $oCat->objectGetId();
$sWhereFilter .= " OR catId = '$iCatId' ";
}
$sWhereFilter .= ") ";
}
/* Should we add a limit clause to the query? */ /* Should we add a limit clause to the query? */
if($iRows || $iStart) if($iRows || $iStart)
{ {
@@ -1065,7 +1077,7 @@ class Application {
$oFilter->AddFilterInfo('appVersion.rating', 'Rating', array(FILTER_EQUALS), FILTER_VALUES_ENUM, array('Platinum', 'Gold', 'Silver', 'Bronze', 'Garbage')); $oFilter->AddFilterInfo('appVersion.rating', 'Rating', array(FILTER_EQUALS), FILTER_VALUES_ENUM, array('Platinum', 'Gold', 'Silver', 'Bronze', 'Garbage'));
$oFilter->AddFilterInfo('versions.id', 'Wine version', array(FILTER_EQUALS,FILTER_LESS_THAN,FILTER_GREATER_THAN), FILTER_VALUES_ENUM, $aWineVersionIds, $aWineVersions); $oFilter->AddFilterInfo('versions.id', 'Wine version', array(FILTER_EQUALS,FILTER_LESS_THAN,FILTER_GREATER_THAN), FILTER_VALUES_ENUM, $aWineVersionIds, $aWineVersions);
$oFilter->AddFilterInfo('appFamily.catId', 'Category', array(FILTER_EQUALS), FILTER_VALUES_ENUM, $aCatIds, $aCatNames); $oFilter->AddFilterInfo('appCategory', 'Category', array(FILTER_OPTION_ENUM), FILTER_VALUES_OPTION_ENUM, $aCatIds, $aCatNames);
$oFilter->AddFilterInfo('appVersion.license', 'License', array(FILTER_EQUALS), FILTER_VALUES_ENUM, $aLicenses); $oFilter->AddFilterInfo('appVersion.license', 'License', array(FILTER_EQUALS), FILTER_VALUES_ENUM, $aLicenses);
$oFilter->AddFilterInfo('appFamily.appName', 'Name', array(FILTER_CONTAINS, FILTER_STARTS_WITH, FILTER_ENDS_WITH), FILTER_VALUES_NORMAL); $oFilter->AddFilterInfo('appFamily.appName', 'Name', array(FILTER_CONTAINS, FILTER_STARTS_WITH, FILTER_ENDS_WITH), FILTER_VALUES_NORMAL);
$oFilter->AddFilterInfo('onlyDownloadable', 'Only show downloadable apps', array(FILTER_OPTION_BOOL), FILTER_VALUES_OPTION_BOOL, array('false','true')); $oFilter->AddFilterInfo('onlyDownloadable', 'Only show downloadable apps', array(FILTER_OPTION_BOOL), FILTER_VALUES_OPTION_BOOL, array('false','true'));
@@ -1237,6 +1249,18 @@ class Application {
$sExtraTables .= ',appData'; $sExtraTables .= ',appData';
$sWhereFilter .= " AND appData.type = 'downloadurl' AND appData.versionId = appVersion.versionId"; $sWhereFilter .= " AND appData.type = 'downloadurl' AND appData.versionId = appVersion.versionId";
} }
if($aOptions['appCategory'])
{
$oCategory = new Category($aOptions['appCategory']);
$aSubCats = $oCategory->getSubCatList();
$sWhereFilter .= " AND ( catId = '{$aOptions['appCategory']}' ";
foreach($aSubCats as $oCat)
{
$iCatId = $oCat->objectGetId();
$sWhereFilter .= " OR catId = '$iCatId' ";
}
$sWhereFilter .= ") ";
}
if($sState != 'accepted' && !application::canEdit()) if($sState != 'accepted' && !application::canEdit())
{ {

View File

@@ -137,7 +137,7 @@ class Category {
/* Get a category's subcategory objects. Names are indented according /* Get a category's subcategory objects. Names are indented according
to subcategory level */ to subcategory level */
function getSubCatList($iLevel) function getSubCatList($iLevel = 0)
{ {
$aOut = array(); $aOut = array();
$iId = $this->iCatId ? $this->iCatId : 0; $iId = $this->iCatId ? $this->iCatId : 0;

View File

@@ -18,6 +18,7 @@ define('FILTER_LESS_THAN', 7);
define('FILTER_NOT_EQUALS', 8); define('FILTER_NOT_EQUALS', 8);
define('FILTER_NOT_LIKE', 9); define('FILTER_NOT_LIKE', 9);
define('FILTER_OPTION_BOOL', 10); define('FILTER_OPTION_BOOL', 10);
define('FILTER_OPTION_ENUM', 11);
/* A filter as part of an SQL query, such as something = 'somevalue' */ /* A filter as part of an SQL query, such as something = 'somevalue' */
class Filter class Filter
@@ -82,7 +83,7 @@ class Filter
public function getExpression() public function getExpression()
{ {
/* We let callers handle options themselves, so don't include them in the WHERE clause */ /* We let callers handle options themselves, so don't include them in the WHERE clause */
if($this->iType == FILTER_OPTION_BOOL) if($this->isOption())
return ''; return '';
$sData = $this->sData; $sData = $this->sData;
@@ -105,6 +106,19 @@ class Filter
return "{$this->sColumn} $sOp '$sData'"; return "{$this->sColumn} $sOp '$sData'";
} }
public function IsOption()
{
switch($this->iType)
{
case FILTER_OPTION_BOOL:
case FILTER_OPTION_ENUM:
return true;
default:
return false;
}
}
} }
/* Class handling tables where the user can filter contents */ /* Class handling tables where the user can filter contents */

View File

@@ -13,6 +13,7 @@ require_once('db_filter.php');
define('FILTER_VALUES_NORMAL', 1); define('FILTER_VALUES_NORMAL', 1);
define('FILTER_VALUES_ENUM', 2); define('FILTER_VALUES_ENUM', 2);
define('FILTER_VALUES_OPTION_BOOL', 3); define('FILTER_VALUES_OPTION_BOOL', 3);
define('FILTER_VALUES_OPTION_ENUM', 4);
define('MAX_FILTERS', 50); define('MAX_FILTERS', 50);
@@ -71,6 +72,19 @@ class FilterInfo
return $this->aTypes; return $this->aTypes;
} }
public function isOption()
{
switch($this->iValueType)
{
case FILTER_VALUES_OPTION_BOOL:
case FILTER_VALUES_OPTION_ENUM:
return true;
default:
return false;
}
}
public static function getOpName($iOpId) public static function getOpName($iOpId)
{ {
switch($iOpId) switch($iOpId)
@@ -219,7 +233,7 @@ class FilterInterface
/* Printing 'equal to' sounds weird if it is the only choice */ /* Printing 'equal to' sounds weird if it is the only choice */
if($aTypes[0] != FILTER_EQUALS) if($aTypes[0] != FILTER_EQUALS)
$shEditor .= $oColumn->getOpName($aTypes[0]); $shEditor .= $oColumn->getOpName($aTypes[0]);
} else } else if ($aTypes[0] != FILTER_OPTION_ENUM)
{ {
$shEditor .= "<select name='i{$sColumn}Op$sId'>"; $shEditor .= "<select name='i{$sColumn}Op$sId'>";
@@ -240,6 +254,9 @@ class FilterInterface
$shEditor .= "<option value='$iType'$sSel>".$oColumn->getOpName($iType).'</option><br />'; $shEditor .= "<option value='$iType'$sSel>".$oColumn->getOpName($iType).'</option><br />';
} }
$shEditor .= '</select> '; $shEditor .= '</select> ';
} else
{
echo "<input type=\"hidden\" name=\"i{$sColumn}Op$sId\" value=\"{$aTypes[0]}\" />";
} }
switch($oColumn->getValueType()) switch($oColumn->getValueType())
@@ -248,6 +265,7 @@ class FilterInterface
$shEditor .= "<input type='text' value=\"{$oFilter->getData()}\" name='s{$sColumn}Data$sId' size='30' />"; $shEditor .= "<input type='text' value=\"{$oFilter->getData()}\" name='s{$sColumn}Data$sId' size='30' />";
break; break;
case FILTER_VALUES_ENUM: case FILTER_VALUES_ENUM:
case FILTER_VALUES_OPTION_ENUM:
$shEditor .= $this->getEnumEditor($oColumn, $oFilter, $sId); $shEditor .= $this->getEnumEditor($oColumn, $oFilter, $sId);
break; break;
} }
@@ -369,12 +387,17 @@ class FilterInterface
{ {
if(!array_key_exists($oOption->getColumn(), $aCounts)) if(!array_key_exists($oOption->getColumn(), $aCounts))
$shNewItemsEditor .= $this->getOptionBoolEditor(-1, $oDummyFilter); $shNewItemsEditor .= $this->getOptionBoolEditor(-1, $oDummyFilter);
$shNewItemsEditor .= '<br />';
} else } else
{
/* Make necessary checks for filters that are only supposed to be shown once */
if($oOption->getValueType() != FILTER_VALUES_OPTION_ENUM || !array_key_exists($oOption->getColumn(), $aCounts))
{ {
$shNewItemsEditor .= $this->getItemEditor(-1, $oDummyFilter); $shNewItemsEditor .= $this->getItemEditor(-1, $oDummyFilter);
}
$shNewItemsEditor .= '<br />'; $shNewItemsEditor .= '<br />';
} }
}
}
return $shNewItemsEditor.$shCurrentItemsEditor; return $shNewItemsEditor.$shCurrentItemsEditor;
} }
@@ -403,6 +426,8 @@ class FilterInterface
/* Only show an option as an active filter if it has been changed /* Only show an option as an active filter if it has been changed
from the default */ from the default */
if($oOption->getValueType() == FILTER_VALUES_OPTION_BOOL || $oOption->getValueType() == FILTER_VALUES_OPTION_ENUM)
{
if($oOption->getValueType() == FILTER_VALUES_OPTION_BOOL) if($oOption->getValueType() == FILTER_VALUES_OPTION_BOOL)
{ {
/* The default option is the first entry in the list of choices */ /* The default option is the first entry in the list of choices */
@@ -410,8 +435,12 @@ class FilterInterface
$sDefault = $aChoices[0]; $sDefault = $aChoices[0];
if(!$sData) if(!$sData)
$sData = 'false'; $sData = 'false';
if($sData == $sDefault) if($sData == $sDefault)
continue; continue;
}
if($i > 0)
continue;
$bChangedOption = true; $bChangedOption = true;
} }
@@ -485,16 +514,21 @@ class FilterInterface
$aOptions = array(); $aOptions = array();
foreach($this->oFilterSet->getFilters() as $oFilter) foreach($this->oFilterSet->getFilters() as $oFilter)
{ {
if($oFilter->getOperatorId() == FILTER_OPTION_BOOL) if($oFilter->isOption())
$aOptions[$oFilter->getColumn()] = $oFilter->getData(); $aOptions[$oFilter->getColumn()] = $oFilter->getData();
} }
foreach($this->aFilterInfo as $oFilterInfo) foreach($this->aFilterInfo as $oFilterInfo)
{ {
if($oFilterInfo->getValueType() == FILTER_VALUES_OPTION_BOOL && if($oFilterInfo->isOption() &&
!array_key_exists($oFilterInfo->getColumn(), $aOptions)) !array_key_exists($oFilterInfo->getColumn(), $aOptions))
{ {
$aTypes = $oFilterInfo->getTypes(); $aTypes = $oFilterInfo->getTypes();
if($oFilterInfo->getValueType() == FILTER_VALUES_OPTION_BOOL)
$sDefault = $aTypes[0]; $sDefault = $aTypes[0];
else
$sDefault = '';
$aOptions[$oFilterInfo->getColumn()] = $sDefault; $aOptions[$oFilterInfo->getColumn()] = $sDefault;
} }
} }