From 03c84de2b0a76e00dbd67cd59a1862f2b1fb19c8 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Fri, 2 May 2014 13:03:14 +0200 Subject: [PATCH 1/3] numeric sorting of table values and start of allowing more sorting criteria see #78 and #108 --- helper.php | 10 ++--- syntax/table.php | 99 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/helper.php b/helper.php index 43939ad..8b89635 100644 --- a/helper.php +++ b/helper.php @@ -200,7 +200,7 @@ function _formatData($column, $value, &$R){ case 'pageid': list($id,$title) = explode('|',$val,2); $id = $this->_addPrePostFixes($column['type'], $id); - $outs[] = $R->internallink($id,$title,null,true); + $outs[] = $R->internallink($id,$title,null,true); break; case 'nspage': // no prefix/postfix here @@ -271,16 +271,16 @@ function _formatData($column, $value, &$R){ * @returns array with key, type, ismulti, title, opt */ function _column($col){ - preg_match('/^([^_]*)(?:_(.*))?((? $col, - 'multi' => ($matches[3] === 's'), + 'multi' => ($matches[4] === 's'), 'key' => utf8_strtolower($matches[1]), 'origkey' => $matches[1], //similar to key, but stores upper case 'title' => $matches[1], - 'type' => utf8_strtolower($matches[2]) + 'type' => utf8_strtolower($matches[3]), + 'datatype'=> $matches[2] == '(num)' ? 'numeric' : '' ); - // fix title for special columns static $specials = array('%title%' => array('page', 'title'), '%pageid%' => array('title', 'page'), diff --git a/syntax/table.php b/syntax/table.php index 9bb1f80..2ebe1c6 100644 --- a/syntax/table.php +++ b/syntax/table.php @@ -158,13 +158,11 @@ function handle($match, $state, $pos, Doku_Handler &$handler){ break; case 'order': case 'sort': - $column = $this->dthlp->_column($line[1]); - $sort = $column['key']; - if(substr($sort,0,1) == '^'){ - $data['sort'] = array(substr($sort,1),'DESC'); - }else{ - $data['sort'] = array($sort,'ASC'); - } + $cols = explode(',', $line[1]); + foreach($cols as $col) { + $param = $this->parseSortParam($col); + $data['sort'][$param[0]] = $param; + } break; case 'where': case 'filter': @@ -389,14 +387,16 @@ function preList($clist, $data) { $text .= ''; // add sort arrow - if(isset($data['sort']) && $ckey == $data['sort'][0]){ - if($data['sort'][1] == 'ASC'){ + if(isset($data['sort']) && isset($data['sort'][$ckey])) { + $sortterm = $data['sort'][$ckey]; + if($sortterm[1] == 'ASC'){ $text .= ' '; $ckey = '^'.$ckey; }else{ $text .= ' '; } - } + if($sortterm[2] == 'numeric') $ckey = $ckey . '(num)'; + }; // Clickable header for dynamic sorting $text .= ' array( - 'volume' => array('volume', 'DESC', '') + 'volume' => array('volume', 'DESC', 'numeric'), + '%pageid%' => array('%pageid%', 'ASC', '') ), 'align' => array( '0' => 'center' @@ -130,7 +131,7 @@ function testHandle() { LEFT JOIN data AS T1 ON T1.pid = W1.pid AND T1.key = 'employee' LEFT JOIN data AS T2 ON T2.pid = W1.pid AND T2.key = 'deadline' LEFT JOIN data AS T3 ON T3.pid = W1.pid AND T3.key = 'website' LEFT JOIN data AS T4 ON T4.pid = W1.pid AND T4.key = 'volume' LEFT JOIN pages ON W1.pid=pages.pid GROUP BY W1.pid - ORDER BY T4.value DESC LIMIT 11", + ORDER BY CAST(T4.value AS NUMERIC) DESC, pages.page ASC LIMIT 11", 'cur_param' => array() );