From d743cdef78ac50433ac8714769e2a100b841c780 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 13 Apr 2010 02:15:23 +0000 Subject: [PATCH] BUGFIX: Make TableListField sort checking use SQLQuery::canSortBy() to let SSReprot_FakeQuery work. ENHANCEMENT: Allow sort descending as well as ascending. (from r96054) (from r98132) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102604 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/TableListField.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/forms/TableListField.php b/forms/TableListField.php index 1887d990a..1e379aa80 100755 --- a/forms/TableListField.php +++ b/forms/TableListField.php @@ -324,10 +324,13 @@ JS if($this->form) { $sortLink = $this->Link(); $sortLink = HTTP::setGetVar("ctf[{$this->Name()}][sort]", $fieldName, $sortLink); - if(isset($_REQUEST['ctf'][$this->Name()]['dir'])) { - $XML_sort = (isset($_REQUEST['ctf'][$this->Name()]['dir'])) ? Convert::raw2xml($_REQUEST['ctf'][$this->Name()]['dir']) : null; - $sortLink = HTTP::setGetVar("ctf[{$this->Name()}][dir]", $XML_sort, $sortLink); - } + + // Apply sort direction + $dir = isset($_REQUEST['ctf'][$this->Name()]['dir']) ? $_REQUEST['ctf'][$this->Name()]['dir'] : null; + $dir = trim(strtolower($dir)); + $newDir = ($dir == 'desc') ? null : 'desc'; + $sortLink = HTTP::setGetVar("ctf[{$this->Name()}][dir]", Convert::raw2xml($newDir), $sortLink); + if(isset($_REQUEST['ctf'][$this->Name()]['search']) && is_array($_REQUEST['ctf'][$this->Name()]['search'])) { foreach($_REQUEST['ctf'][$this->Name()]['search'] as $parameter => $value) { $XML_search = Convert::raw2xml($value); @@ -484,13 +487,13 @@ JS $query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter(), $this->sourceSort, null, $this->sourceJoin); } - if(isset($_REQUEST['ctf'][$this->Name()]['sort'])) { - $SQL_sort = Convert::raw2sql($_REQUEST['ctf'][$this->Name()]['sort']); - $sql = $query->sql(); - // see {isFieldSortable} - if(in_array($SQL_sort,$query->select) || stripos($sql,"AS {$SQL_sort}")) { - $query->orderby = $SQL_sort; + if(!empty($_REQUEST['ctf'][$this->Name()]['sort'])) { + $sort = $_REQUEST['ctf'][$this->Name()]['sort']; + if(!empty($_REQUEST['ctf'][$this->Name()]['dir'])) { + $dir = $_REQUEST['ctf'][$this->Name()]['dir']; + if(strtoupper(trim($dir)) == 'DESC') $sort .= ' DESC'; } + if($query->canSortBy($sort)) $query->orderby = $sort; } return $query;