summaryrefslogtreecommitdiffstats
path: root/apps/files/lib
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-04-03 20:57:06 +0200
committerVincent Petry <pvince81@owncloud.com>2014-04-28 17:42:04 +0200
commit688f6162daeec724c537f9c283092f45b29b05f2 (patch)
tree27fd75f137b773b886a5d9a0d4511ff136008133 /apps/files/lib
parente055a411ea4b2a32dcf20c910d332867dc91f516 (diff)
downloadnextcloud-server-688f6162daeec724c537f9c283092f45b29b05f2.tar.gz
nextcloud-server-688f6162daeec724c537f9c283092f45b29b05f2.zip
Add sorting to files list, trashbin and public files
Diffstat (limited to 'apps/files/lib')
-rw-r--r--apps/files/lib/helper.php55
1 files changed, 52 insertions, 3 deletions
diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php
index 0ae87d12fbf..94c7658c9cf 100644
--- a/apps/files/lib/helper.php
+++ b/apps/files/lib/helper.php
@@ -70,6 +70,32 @@ class Helper
}
/**
+ * Comparator function to sort files by date
+ *
+ * @param \OCP\Files\FileInfo $a file
+ * @param \OCP\Files\FileInfo $b file
+ * @return int -1 if $a must come before $b, 1 otherwise
+ */
+ public static function mtimeCmp($a, $b) {
+ $aTime = $a->getMTime();
+ $bTime = $b->getMTime();
+ return $aTime - $bTime;
+ }
+
+ /**
+ * Comparator function to sort files by size
+ *
+ * @param \OCP\Files\FileInfo $a file
+ * @param \OCP\Files\FileInfo $b file
+ * @return int -1 if $a must come before $b, 1 otherwise
+ */
+ public static function sizeCmp($a, $b) {
+ $aSize = $a->getSize();
+ $bSize = $b->getSize();
+ return $aSize - $bSize;
+ }
+
+ /**
* Formats the file info to be returned as JSON to the client.
*
* @param \OCP\Files\FileInfo $i
@@ -120,12 +146,35 @@ class Helper
* returns it as a sorted array of FileInfo.
*
* @param string $dir path to the directory
+ * @param string $sortAttribute attribute to sort on
+ * @param bool $sortDescending true for descending sort, false otherwise
* @return \OCP\Files\FileInfo[] files
*/
- public static function getFiles($dir) {
+ public static function getFiles($dir, $sortAttribute = 'name', $sortDescending = false) {
$content = \OC\Files\Filesystem::getDirectoryContent($dir);
- usort($content, array('\OCA\Files\Helper', 'fileCmp'));
- return $content;
+ return self::sortFiles($content, $sortAttribute, $sortDescending);
+ }
+
+ /**
+ * Sort the given file info array
+ *
+ * @param \OCP\Files\FileInfo[] files to sort
+ * @param string $sortAttribute attribute to sort on
+ * @param bool $sortDescending true for descending sort, false otherwise
+ * @return \OCP\Files\FileInfo[] sorted files
+ */
+ public static function sortFiles($files, $sortAttribute = 'name', $sortDescending = false) {
+ $sortFunc = 'fileCmp';
+ if ($sortAttribute === 'mtime') {
+ $sortFunc = 'mtimeCmp';
+ } else if ($sortAttribute === 'size') {
+ $sortFunc = 'sizeCmp';
+ }
+ usort($files, array('\OCA\Files\Helper', $sortFunc));
+ if ($sortDescending) {
+ $files = array_reverse($files);
+ }
+ return $files;
}
}