diff --git a/include/application.php b/include/application.php index 99b1e22..8449e6a 100644 --- a/include/application.php +++ b/include/application.php @@ -883,9 +883,10 @@ class Application { 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 = ""; + $sOrdering = $bAscending ? "ASC" : "DESC"; /* Should we add a limit clause to the query? */ if($iRows || $iStart) @@ -909,27 +910,28 @@ class Application { if(!$bRejected) return FALSE; - $sQuery .= " AND appFamily.submitterId = '?' ORDER BY ?$sLimit"; + $sQuery .= " AND appFamily.submitterId = '?' ORDER BY ? ?$sLimit"; if($sLimit) { $hResult = query_parameters($sQuery, $sQueued, $_SESSION['current']->iUserId, $sOrderBy, - $iStart, $iRows); + $sOrdering, $iStart, $iRows); } else { $hResult = query_parameters($sQuery, $sQueued, - $_SESSION['current']->iUserId, $sOrderBy); + $_SESSION['current']->iUserId, $sOrderBy, + $sOrdering); } } else { - $sQuery .= " ORDER BY ?$sLimit"; + $sQuery .= " ORDER BY ? ?$sLimit"; if($sLimit) { - $hResult = query_parameters($sQuery, $sQueued, $sOrderBy, + $hResult = query_parameters($sQuery, $sQueued, $sOrderBy, $sOrdering, $iStart, $iRows); } else { - $hResult = query_parameters($sQuery, $sQueued, $sOrderBy); + $hResult = query_parameters($sQuery, $sQueued, $sOrderBy, $sOrdering); } } @@ -939,13 +941,18 @@ class Application { return $hResult; } + public static function objectGetSortableFields() + { + return array("submitTime", "appName"); + } + public static function objectGetHeader() { - $oTableRow = new TableRow(); - $oTableRow->AddTextCell("Submission Date"); + $oTableRow = new TableRowSortable(); + $oTableRow->AddSortableTextCell("Submission Date", "submitTime"); $oTableRow->AddTextCell("Submitter"); $oTableRow->AddTextCell("Vendor"); - $oTableRow->AddTextCell("Application"); + $oTableRow->AddSortableTextCell("Application", "appName"); return $oTableRow; } diff --git a/include/application_queue.php b/include/application_queue.php index 6fa7123..3c98455 100644 --- a/include/application_queue.php +++ b/include/application_queue.php @@ -364,12 +364,17 @@ class application_queue 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, - $sOrderBy); + $sOrderBy, $bAscending); } - + + public static function objectGetSortableFields() + { + return application::objectGetSortableFields(); + } + function objectGetHeader() { return $this->oApp->objectGetHeader(); diff --git a/include/objectManager.php b/include/objectManager.php index 6a282f8..fd5a24e 100644 --- a/include/objectManager.php +++ b/include/objectManager.php @@ -16,6 +16,7 @@ class ObjectManager private $sReturnToTitle; /* Used to preserve the title when processing entries from a queue list, for instance */ private $oMultiPage; private $oTableRow; + private $oSortInfo; /* Contains sort info used when displaying tables */ private $oObject; /* Store an instance of the object of the class we are working with. This is useful if we are calling object functions which modify @@ -76,6 +77,19 @@ class ObjectManager $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() { return $this->iId; @@ -180,18 +194,41 @@ class ObjectManager // current page, if applicable. $this->handleMultiPageControls($aClean, TRUE); + /* Set the sort info */ + $this->setSortInfo($aClean); + /* query the class for its entries */ /* We pass in $this->bIsQueue to tell the object */ /* if we are requesting a list of its queued objects or */ /* all of its objects */ if($this->oMultiPage->bEnabled) { - $hResult = $oObject->objectGetEntries($this->bIsQueue, $this->bIsRejected, - $this->oMultiPage->iItemsPerPage, - $this->oMultiPage->iLowerLimit); + /* 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->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 { - $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? */ @@ -1171,6 +1208,12 @@ class ObjectManager $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; } @@ -1199,6 +1242,12 @@ class ObjectManager if($this->sReturnToTitle) $sReturn .= "sReturnToTitle."\" />\n"; + if($this->oSortInfo && $this->oSortInfo->sCurrentSort) + { + $sReturn .= "oSortInfo->sCurrentSort}\" />"; + $sReturn .= "oSortInfo->bAscending ? 'true' : 'false')."\" />"; + } + return $sReturn; } @@ -1226,6 +1275,10 @@ class ObjectManager $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(); } diff --git a/include/table.php b/include/table.php index 07c42bd..a2d413b 100644 --- a/include/table.php +++ b/include/table.php @@ -200,7 +200,7 @@ class TableCell 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 $sClass; // CSS class to be used 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 // when outputting a table row //TODO: php5 consider inheriting from HtmlTableRow since this class is really an diff --git a/objectManager.php b/objectManager.php index 526f291..d50fe6f 100644 --- a/objectManager.php +++ b/objectManager.php @@ -66,6 +66,7 @@ if($aClean['bIsRejected'] == 'true') } $oObject->getMultiPageDataFromInput($aClean); +$oObject->setSortInfo($aClean); $sClass = $oObject->getClass(); $oOtherObject = new $sClass($oObject->getId());