From 00e3e3940de140e6fe6ce3c558c71a47f676b456 Mon Sep 17 00:00:00 2001
From: Christopher Smith <chris@jalakai.co.uk>
Date: Sat, 3 Aug 2013 17:17:58 +0200
Subject: [PATCH] fix the default ordering of media files to be "natural".

also allow the order to be specified in more places.
---
 inc/media.php    | 13 ++++---------
 inc/template.php |  4 ++--
 lib/exe/ajax.php |  3 ++-
 3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/inc/media.php b/inc/media.php
index a182ecb33..a9f6be771 100644
--- a/inc/media.php
+++ b/inc/media.php
@@ -1296,7 +1296,7 @@ function media_restore($image, $rev, $auth){
  * @author Kate Arzamastseva <pshns@ukr.net>
  * @triggers MEDIA_SEARCH
  */
-function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort=''){
+function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort='natural'){
     global $conf;
     global $lang;
 
@@ -1316,15 +1316,10 @@ function media_searchlist($query,$ns,$auth=null,$fullscreen=false,$sort=''){
                     $conf['mediadir'],
                     'search_media',
                     array('showmsg'=>false,'pattern'=>$pattern),
-                    $dir);
+                    $dir,
+                    1,
+                    $sort);
         }
-
-        $data = array();
-        foreach ($evdata['data'] as $k => $v) {
-            $data[$k] = ($sort == 'date') ? $v['mtime'] : $v['id'];
-        }
-        array_multisort($data, SORT_DESC, SORT_NUMERIC, $evdata['data']);
-
         $evt->advise_after();
         unset($evt);
     }
diff --git a/inc/template.php b/inc/template.php
index ca1c8d9d5..c8520dc26 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -1229,7 +1229,7 @@ function tpl_localeFN($id) {
  * @param bool $fromajax - set true when calling this function via ajax
  * @author Andreas Gohr <andi@splitbrain.org>
  */
-function tpl_mediaContent($fromajax = false) {
+function tpl_mediaContent($fromajax = false, $sort='natural') {
     global $IMG;
     global $AUTH;
     global $INUSE;
@@ -1259,7 +1259,7 @@ function tpl_mediaContent($fromajax = false) {
         if($do == 'filesinuse') {
             media_filesinuse($INUSE, $IMG);
         } elseif($do == 'filelist') {
-            media_filelist($NS, $AUTH, $JUMPTO);
+            media_filelist($NS, $AUTH, $JUMPTO,false,$sort);
         } elseif($do == 'searchlist') {
             media_searchlist($INPUT->str('q'), $NS, $AUTH);
         } else {
diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php
index 9769503a7..6e2011cd9 100644
--- a/lib/exe/ajax.php
+++ b/lib/exe/ajax.php
@@ -219,10 +219,11 @@ function ajax_medialist(){
     global $INPUT;
 
     $NS = cleanID($INPUT->post->str('ns'));
+    $sort = $INPUT->post->bool('recent') ? 'date' : 'natural';
     if ($INPUT->post->str('do') == 'media') {
         tpl_mediaFileList();
     } else {
-        tpl_mediaContent(true);
+        tpl_mediaContent(true, $sort);
     }
 }
 
-- 
GitLab