objectManager: Support user-selected sorting in tables

This commit is contained in:
Alexander Nicolaysen Sørnes
2007-11-08 17:32:00 +01:00
committed by Chris Morgan
parent 62e3a98fca
commit e7625791a0
5 changed files with 173 additions and 18 deletions

View File

@@ -883,9 +883,10 @@ class Application {
return $sLink; return $sLink;
} }
public static function objectGetEntries($bQueued, $bRejected, $iRows = 0, $iStart = 0, $sOrderBy = "appId") public static function objectGetEntries($bQueued, $bRejected, $iRows = 0, $iStart = 0, $sOrderBy = "appId", $bAscending = TRUE)
{ {
$sLimit = ""; $sLimit = "";
$sOrdering = $bAscending ? "ASC" : "DESC";
/* Should we add a limit clause to the query? */ /* Should we add a limit clause to the query? */
if($iRows || $iStart) if($iRows || $iStart)
@@ -909,27 +910,28 @@ class Application {
if(!$bRejected) if(!$bRejected)
return FALSE; return FALSE;
$sQuery .= " AND appFamily.submitterId = '?' ORDER BY ?$sLimit"; $sQuery .= " AND appFamily.submitterId = '?' ORDER BY ? ?$sLimit";
if($sLimit) if($sLimit)
{ {
$hResult = query_parameters($sQuery, $sQueued, $hResult = query_parameters($sQuery, $sQueued,
$_SESSION['current']->iUserId, $sOrderBy, $_SESSION['current']->iUserId, $sOrderBy,
$iStart, $iRows); $sOrdering, $iStart, $iRows);
} else } else
{ {
$hResult = query_parameters($sQuery, $sQueued, $hResult = query_parameters($sQuery, $sQueued,
$_SESSION['current']->iUserId, $sOrderBy); $_SESSION['current']->iUserId, $sOrderBy,
$sOrdering);
} }
} else } else
{ {
$sQuery .= " ORDER BY ?$sLimit"; $sQuery .= " ORDER BY ? ?$sLimit";
if($sLimit) if($sLimit)
{ {
$hResult = query_parameters($sQuery, $sQueued, $sOrderBy, $hResult = query_parameters($sQuery, $sQueued, $sOrderBy, $sOrdering,
$iStart, $iRows); $iStart, $iRows);
} else } else
{ {
$hResult = query_parameters($sQuery, $sQueued, $sOrderBy); $hResult = query_parameters($sQuery, $sQueued, $sOrderBy, $sOrdering);
} }
} }
@@ -939,13 +941,18 @@ class Application {
return $hResult; return $hResult;
} }
public static function objectGetSortableFields()
{
return array("submitTime", "appName");
}
public static function objectGetHeader() public static function objectGetHeader()
{ {
$oTableRow = new TableRow(); $oTableRow = new TableRowSortable();
$oTableRow->AddTextCell("Submission Date"); $oTableRow->AddSortableTextCell("Submission Date", "submitTime");
$oTableRow->AddTextCell("Submitter"); $oTableRow->AddTextCell("Submitter");
$oTableRow->AddTextCell("Vendor"); $oTableRow->AddTextCell("Vendor");
$oTableRow->AddTextCell("Application"); $oTableRow->AddSortableTextCell("Application", "appName");
return $oTableRow; return $oTableRow;
} }

View File

@@ -364,12 +364,17 @@ class application_queue
return $this->oApp->objectGetEntriesCount($bQueued, $bRejected); return $this->oApp->objectGetEntriesCount($bQueued, $bRejected);
} }
function objectGetEntries($bQueued, $bRejected, $iRows = 0, $iStart = 0, $sOrderBy = "appId") function objectGetEntries($bQueued, $bRejected, $iRows = 0, $iStart = 0, $sOrderBy = "appId", $bAscending = TRUE)
{ {
return $this->oApp->objectGetEntries($bQueued, $bRejected, $iRows, $iStart, return $this->oApp->objectGetEntries($bQueued, $bRejected, $iRows, $iStart,
$sOrderBy); $sOrderBy, $bAscending);
} }
public static function objectGetSortableFields()
{
return application::objectGetSortableFields();
}
function objectGetHeader() function objectGetHeader()
{ {
return $this->oApp->objectGetHeader(); return $this->oApp->objectGetHeader();

View File

@@ -16,6 +16,7 @@ class ObjectManager
private $sReturnToTitle; /* Used to preserve the title when processing entries from a queue list, for instance */ private $sReturnToTitle; /* Used to preserve the title when processing entries from a queue list, for instance */
private $oMultiPage; private $oMultiPage;
private $oTableRow; private $oTableRow;
private $oSortInfo; /* Contains sort info used when displaying tables */
private $oObject; /* Store an instance of the object of the class private $oObject; /* Store an instance of the object of the class
we are working with. This is useful if we are working with. This is useful if
we are calling object functions which modify we are calling object functions which modify
@@ -76,6 +77,19 @@ class ObjectManager
$this->bIsRejected = $bIsRejected; $this->bIsRejected = $bIsRejected;
} }
public function setSortInfo($aClean = null)
{
/* No use to continue if there are no sortable fields */
if(!$this->getOptionalSetting("objectGetSortableFields", FALSE))
return;
$this->oSortInfo = null;
$this->oSortInfo = new TableSortInfo($this->makeUrl().'&');
if($aClean)
$this->oSortInfo->ParseArray($aClean, $this->getObject()->objectGetSortableFields());
}
public function getId() public function getId()
{ {
return $this->iId; return $this->iId;
@@ -180,18 +194,41 @@ class ObjectManager
// current page, if applicable. // current page, if applicable.
$this->handleMultiPageControls($aClean, TRUE); $this->handleMultiPageControls($aClean, TRUE);
/* Set the sort info */
$this->setSortInfo($aClean);
/* query the class for its entries */ /* query the class for its entries */
/* We pass in $this->bIsQueue to tell the object */ /* We pass in $this->bIsQueue to tell the object */
/* if we are requesting a list of its queued objects or */ /* if we are requesting a list of its queued objects or */
/* all of its objects */ /* all of its objects */
if($this->oMultiPage->bEnabled) if($this->oMultiPage->bEnabled)
{ {
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected, /* Has the user chosen a particular field to sort by? If not we want to use the default */
$this->oMultiPage->iItemsPerPage, if($this->oSortInfo->sCurrentSort)
$this->oMultiPage->iLowerLimit); {
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected,
$this->oMultiPage->iItemsPerPage,
$this->oMultiPage->iLowerLimit,
$this->oSortInfo->sCurrentSort,
$this->oSortInfo->bAscending);
} else
{
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected,
$this->oMultiPage->iItemsPerPage,
$this->oMultiPage->iLowerLimit);
}
} else } else
{ {
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected); /* Has the user chosen a particular field to sort by? If not we want to use the default */
if($this->oSortInfo->sCurrentSort)
{
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected,
$this->oSortInfo->sCurrentSort,
$this->oSortInfo->bAscending);
} else
{
$hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected);
}
} }
/* did we get any entries? */ /* did we get any entries? */
@@ -1171,6 +1208,12 @@ class ObjectManager
$sUrl .= "&iPage=".$this->oMultiPage->iPage; $sUrl .= "&iPage=".$this->oMultiPage->iPage;
} }
if($this->oSortInfo && $this->oSortInfo->sCurrentSort)
{
$sUrl .= "&sOrderBy={$this->oSortInfo->sCurrentSort}";
$sUrl .= '&bAscending='.($this->oSortInfo->bAscending ? 'true' : 'false');
}
return $sUrl; return $sUrl;
} }
@@ -1199,6 +1242,12 @@ class ObjectManager
if($this->sReturnToTitle) if($this->sReturnToTitle)
$sReturn .= "<input type=\"hidden\" name=\"sReturnToTitle\" value=\"".$this->sReturnToTitle."\" />\n"; $sReturn .= "<input type=\"hidden\" name=\"sReturnToTitle\" value=\"".$this->sReturnToTitle."\" />\n";
if($this->oSortInfo && $this->oSortInfo->sCurrentSort)
{
$sReturn .= "<input type=\"hidden\" name=\"sOrderBy\" value=\"{$this->oSortInfo->sCurrentSort}\" />";
$sReturn .= "<input type=\"hidden\" name=\"bAscending\" value=\"".($this->oSortInfo->bAscending ? 'true' : 'false')."\" />";
}
return $sReturn; return $sReturn;
} }
@@ -1226,6 +1275,10 @@ class ObjectManager
$oTableRow->SetClass($sClass); $oTableRow->SetClass($sClass);
/* Set the current sorting info if the header is sortable */
if(get_class($oTableRow) == "TableRowSortable")
$oTableRow->SetSortInfo($this->oSortInfo);
echo $oTableRow->GetString(); echo $oTableRow->GetString();
} }

View File

@@ -200,7 +200,7 @@ class TableCell
class TableRow class TableRow
{ {
private $aTableCells; // array that contains the cells for the table row protected $aTableCells; // array that contains the cells for the table row
private $sStyle; // CSS style to be used private $sStyle; // CSS style to be used
private $sClass; // CSS class to be used private $sClass; // CSS class to be used
private $sValign; // valign="$sValign" - if this variable is set private $sValign; // valign="$sValign" - if this variable is set
@@ -298,6 +298,95 @@ class TableRow
} }
} }
/* Class for a sortable table row. The user can click on the header for a sortable field, and it
will alternate between sorting that by ascending/descending order and the default sorting */
class TableRowSortable extends TableRow
{
private $aSortVars; /* Array of sort variables. Not all fields have to be sortable.
This is paired with the aTableCells array from TableRow */
function TableRowSortable()
{
$this->aSortVars = array();
$this->TableRow();
}
/* Adds a table cell without sorting */
function AddTableCell(TableCell $oCell)
{
$this->aTableCells[] = $oCell;
$this->aSortVars[] = '';
}
/* Adds a text cell without sorting */
function AddTextCell($shText)
{
$this->AddTableCell(new TableCell($shText));
}
/* Adds a text cell with a sorting var */
function AddSortableTextCell($shText, $sSortVar)
{
$this->aTableCells[] = new TableCell($shText);
$this->aSortVars[] = $sSortVar;
}
/* Sets sorting info on all cells that are sortable */
function SetSortInfo(TableSortInfo $oSortInfo)
{
for($i = 0; $i < sizeof($this->aTableCells); $i++)
{
$sSortVar = $this->aSortVars[$i];
if($sSortVar)
{
$bAscending = TRUE;
if($this->aSortVars[$i] == $oSortInfo->sCurrentSort)
{
if($oSortInfo->bAscending)
$bAscending = FALSE;
else
$sSortVar = '';
}
$sAscending = $bAscending == TRUE ? 'true': 'false';
$this->aTableCells[$i]->SetCellLink($oSortInfo->shUrl."sOrderBy=$sSortVar&bAscending=$sAscending");
}
}
}
}
/* Container for table sorting info, used to hold the current sort order */
class TableSortInfo
{
var $sCurrentSort;
var $bAscending;
var $shUrl;
function TableSortInfo($shUrl, $sCurrentSort = '', $bAscending = TRUE)
{
$this->sCurrentSort = $sCurrentSort;
$this->shUrl = $shUrl;
$this->bAscending = $bAscending;
}
/* Parses an array of HTTP vars to determine current sort settings.
Optionally checks the sort var against an array of legal values */
function ParseArray($aClean, $aLegalValues = null)
{
$sCurrentSort = key_exists('sOrderBy', $aClean) ? $aClean['sOrderBy'] : '';
if($aLegalValues && array_search($sCurrentSort, $aLegalValues) === FALSE)
return;
$this->sCurrentSort = $sCurrentSort;
$this->bAscending = key_exists('bAscending', $aClean) ?
($aClean['bAscending'] == 'false') ? false : true : true;
}
}
// object manager table row, has additional parameters used by the object manager // object manager table row, has additional parameters used by the object manager
// when outputting a table row // when outputting a table row
//TODO: php5 consider inheriting from HtmlTableRow since this class is really an //TODO: php5 consider inheriting from HtmlTableRow since this class is really an

View File

@@ -66,6 +66,7 @@ if($aClean['bIsRejected'] == 'true')
} }
$oObject->getMultiPageDataFromInput($aClean); $oObject->getMultiPageDataFromInput($aClean);
$oObject->setSortInfo($aClean);
$sClass = $oObject->getClass(); $sClass = $oObject->getClass();
$oOtherObject = new $sClass($oObject->getId()); $oOtherObject = new $sClass($oObject->getId());