Skip to content

Commit eaa5565

Browse files
committed
Fix file and directory sort functions to one function
1 parent 1b3ccf7 commit eaa5565

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

src/controllers/ItemsController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ public function getItems()
1818
$path = $this->getCurrentPath();
1919
$sort_type = request('sort_type');
2020

21+
$files = $this->sortFilesAndDirectories($this->getFilesWithInfo($path), $sort_type);
22+
$directories = $this->sortFilesAndDirectories($this->getDirectories($path), $sort_type);
23+
2124
return [
2225
'html' => (string)view($this->getView())->with([
23-
'files' => $this->getFilesWithInfo($path, $sort_type),
24-
'directories' => $this->getDirectories($path, $sort_type),
25-
'items' => array_merge($this->getDirectories($path), $this->getFilesWithInfo($path))
26+
'files' => $files,
27+
'directories' => $directories,
28+
'items' => array_merge($directories, $files)
2629
]),
2730
'working_dir' => $this->getInternalPath($path)
2831
];

src/traits/LfmHelpers.php

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function enabledShareFolder()
236236
*** File System ***
237237
****************************/
238238

239-
public function getDirectories($path, $sort_type = 'alpha')
239+
public function getDirectories($path)
240240
{
241241
$thumb_folder_name = config('lfm.thumb_folder_name');
242242
$all_directories = File::directories($path);
@@ -260,16 +260,10 @@ public function getDirectories($path, $sort_type = 'alpha')
260260
}
261261
}
262262

263-
if ($sort_type == 'alpha') {
264-
uasort($arr_dir, array($this, 'compareDirAlpha'));
265-
} elseif ($sort_type == 'time') {
266-
uasort($arr_dir, array($this, 'compareDirTime'));
267-
}
268-
269263
return $arr_dir;
270264
}
271265

272-
public function getFilesWithInfo($path, $sort_type = 'alpha')
266+
public function getFilesWithInfo($path)
273267
{
274268
$arr_files = [];
275269

@@ -306,12 +300,6 @@ public function getFilesWithInfo($path, $sort_type = 'alpha')
306300
];
307301
}
308302

309-
if ($sort_type == 'alpha') {
310-
uasort($arr_files, array($this, 'compareFileAlpha'));
311-
} elseif ($sort_type == 'time') {
312-
uasort($arr_files, array($this, 'compareFileTime'));
313-
}
314-
315303
return $arr_files;
316304
}
317305

@@ -338,44 +326,27 @@ public function fileIsImage($file)
338326
return starts_with($mime_type, 'image');
339327
}
340328

341-
private static function compareDirAlpha($a, $b)
342-
{
343-
$cmp = strcmp($a->name, $b->name);
344-
345-
if ($cmp == 0) {
346-
return 0;
347-
}
348-
349-
return ($cmp > 0) ? 1 : -1;
350-
}
351-
352-
private static function compareDirTime($a, $b)
329+
public function sortFilesAndDirectories($arr_items, $sort_type)
353330
{
354-
if ($a->updated == $b->updated) {
355-
return 0;
331+
if ($sort_type == 'time') {
332+
$key_to_sort = 'updated';
333+
} elseif ($sort_type == 'alpha') {
334+
$key_to_sort = 'name';
335+
} else {
336+
$key_to_sort = 'updated';
356337
}
357338

358-
return ($a->updated > $b->updated) ? 1 : -1;
359-
}
360-
361-
private static function compareFileAlpha($a, $b)
362-
{
363-
$cmp = strcmp($a['name'], $b['name']);
364-
365-
if ($cmp == 0) {
366-
return 0;
367-
}
339+
uasort($arr_items, function ($a, $b) use ($key_to_sort) {
340+
$cmp = strcmp($a->{$key_to_sort}, $b->{$key_to_sort});
368341

369-
return ($cmp > 0) ? 1 : -1;
370-
}
342+
if ($cmp == 0) {
343+
return 0;
344+
}
371345

372-
private static function compareFileTime($a, $b)
373-
{
374-
if ($a['updated'] == $b['updated']) {
375-
return 0;
376-
}
346+
return ($cmp > 0) ? 1 : -1;
347+
});
377348

378-
return ($a['updated'] > $b['updated']) ? 1 : -1;
349+
return $arr_items;
379350
}
380351

381352

src/views/grid-view.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<img src="{{ $thumb_src }}">
2020
@else
2121
<div class="icon-container">
22-
<i class="fa {{ $file->icon }} fa-5x"></i>
22+
<i class="fa {{ $item->icon }} fa-5x"></i>
2323
</div>
2424
@endif
2525
</div>

0 commit comments

Comments
 (0)