Browse Source

Implement sorting

Closes #4
master
Chimo 2 years ago
parent
commit
9cd41cb6e9
4 changed files with 82 additions and 16 deletions
  1. 71
    16
      actions/filesadminpanel.php
  2. 11
    0
      css/file-manager.css
  3. BIN
      images/sort-asc.png
  4. BIN
      images/sort-desc.png

+ 71
- 16
actions/filesadminpanel.php View File

@@ -12,25 +12,47 @@ class FilesadminpanelAction extends AdminPanelAction
12 12
     function prepare(array $args=array()) {
13 13
         parent::prepare($args);
14 14
 
15
-        $page = isset($args['page']) ? intval($args['page']) : 1;
15
+        $this->pluginDir = common_root_url() .
16
+            str_replace(array(INSTALLDIR . '/', '/actions'), '', dirname(__FILE__));
16 17
 
17
-        if ($page <= 0) {
18
-            $page = 1;
18
+        $this->page = $this->int('page', 1, null, 1);
19
+        $this->sortBy = $this->trimmed('sort-by', 'id');
20
+        $this->sortDir = $this->trimmed('sort-dir', 'asc');
21
+
22
+        $sortable_columns = ['id', 'size'];
23
+
24
+        // URL param sanitation: make sure 'sort-by' is one of the
25
+        // columns we're expecting. If not, default sorting by 'id'
26
+        if (!in_array($this->sortBy, $sortable_columns)) {
27
+            $this->sortBy = 'id';
19 28
         }
20 29
 
21
-        $this->page = $page;
22
-        $this->args = $args;
30
+        // URL param sanitation: make sure 'sort-dir' is either 'asc'
31
+        // or 'desc'. Default to 'asc' if neither.
32
+        if ($this->sortDir !== 'desc') {
33
+            $this->sortDir = 'asc';
34
+        }
23 35
 
24 36
         $offset = ($this->page - 1) * 10;
25 37
         $limit = 10;
26 38
 
39
+        $this->files = $this->getFiles($offset, $limit);
40
+        $this->overview = $this->getOverview();
41
+
42
+        return true;
43
+    }
44
+
45
+    function getFiles($offset, $limit) {
27 46
         $file = new File();
28 47
 
29 48
         $file->whereAdd('filename IS NOT NULL'); // Only local files
49
+        $file->orderBy($this->sortBy . ' ' . $this->sortDir);
30 50
         $file->limit($offset, $limit);
31 51
 
32
-        $this->files = $file->fetchAll();
52
+        return $file->fetchAll();
53
+    }
33 54
 
55
+    function getOverview() {
34 56
         $overview = new File();
35 57
         $overview->selectAdd();
36 58
         $overview->selectAdd('count(*) as total_files');
@@ -39,17 +61,17 @@ class FilesadminpanelAction extends AdminPanelAction
39 61
         // TODO: handle cases where this fails for wtv reason
40 62
         if ($overview->find()) {
41 63
             $overview->fetch();
42
-
43
-            $this->total_files = $overview->total_files;
44
-            $this->total_size = $overview->total_size;
45 64
         }
46 65
 
47
-        return true;
66
+        return array(
67
+            'total_files' => $overview->total_files,
68
+            'total_size' => $overview->total_size
69
+        );
48 70
     }
49 71
 
50 72
     function showOverview() {
51
-        $bytes = $this->formatBytes($this->total_size);
52
-        $nb_files = number_format($this->total_files);
73
+        $bytes = $this->formatBytes($this->overview['total_size']);
74
+        $nb_files = number_format($this->overview['total_files']);
53 75
 
54 76
         $this->element('p', null, "There are $nb_files files saved on your instance, using $bytes of disk space.");
55 77
     }
@@ -62,6 +84,20 @@ class FilesadminpanelAction extends AdminPanelAction
62 84
         return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
63 85
     }
64 86
 
87
+    function showSort($by, $direction) {
88
+        $klass = 'sort-icon';
89
+        $href = '?sort-by=' . $by . '&sort-dir=' . $direction . '&page=' . $this->page;
90
+        $img_src = $this->pluginDir . '/images/sort-' . $direction . '.png';
91
+
92
+        if ($this->sortBy === $by && $this->sortDir === $direction) {
93
+            $klass .= ' active';
94
+        }
95
+
96
+        $this->elementStart('a', array('class' => $klass, 'href' => $href));
97
+        $this->element('img', array('src' => $img_src));
98
+        $this->elementEnd('a');
99
+    }
100
+
65 101
     function showContent() {
66 102
         if ($this->page === 1) {
67 103
             $this->showOverview();
@@ -76,8 +112,19 @@ class FilesadminpanelAction extends AdminPanelAction
76 112
         $this->elementStart('table', array('class' => 'chr-files'));
77 113
         $this->elementStart('thead');
78 114
         $this->elementStart('tr');
79
-        $this->element('th', null, 'File');
80
-        $this->element('th', null, 'Size');
115
+
116
+        $this->elementStart('th');
117
+        $this->text('File');
118
+        $this->showSort('id', 'asc');
119
+        $this->showSort('id', 'desc');
120
+        $this->elementEnd('th');
121
+
122
+        $this->elementStart('th');
123
+        $this->text('Size');
124
+        $this->showSort('size', 'asc');
125
+        $this->showSort('size', 'desc');
126
+        $this->elementEnd('th');
127
+
81 128
         $this->element('th', null, 'Referred by');
82 129
         $this->element('th', null, 'Delete');
83 130
         $this->elementEnd('tr');
@@ -175,7 +222,11 @@ class FilesadminpanelAction extends AdminPanelAction
175 222
                                             'id' => 'pagination'));
176 223
         }
177 224
         if ($have_before) {
178
-            $pargs   = array('page' => $page-1);
225
+            $pargs = array(
226
+                'page' => $page - 1,
227
+                'sort-by' => $this->sortBy,
228
+                'sort-dir' => $this->sortDir
229
+            );
179 230
             $this->elementStart('li', array('class' => 'nav_prev'));
180 231
             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
181 232
                                       'rel' => 'prev'),
@@ -185,7 +236,11 @@ class FilesadminpanelAction extends AdminPanelAction
185 236
             $this->elementEnd('li');
186 237
         }
187 238
         if ($have_after) {
188
-            $pargs   = array('page' => $page + 1);
239
+            $pargs   = array(
240
+                'page' => $page + 1,
241
+                'sort-by' => $this->sortBy,
242
+                'sort-dir' => $this->sortDir
243
+            );
189 244
             $this->elementStart('li', array('class' => 'nav_next'));
190 245
             $this->element('a', array('href' => common_local_url($action, $args, $pargs),
191 246
                                       'rel' => 'next'),

+ 11
- 0
css/file-manager.css View File

@@ -34,3 +34,14 @@
34 34
     width: auto;
35 35
 }
36 36
 
37
+.sort-icon {
38
+    opacity: 0.3;
39
+}
40
+
41
+.sort-icon:hover {
42
+    text-decoration: none;
43
+}
44
+
45
+.sort-icon.active {
46
+    opacity: 1;
47
+}

BIN
images/sort-asc.png View File


BIN
images/sort-desc.png View File


Loading…
Cancel
Save