[Pkg-owncloud-commits] [owncloud] 85/145: move sorter into a class

David Prévot taffit at moszumanska.debian.org
Wed Feb 26 16:27:44 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit f8620704d492287d5acb18bdc60b3a1303c01ad1
Author: Arthur Schiwon <blizzz at owncloud.com>
Date:   Wed Jan 15 17:55:05 2014 +0100

    move sorter into a class
---
 core/ajax/share.php                      | 27 ++--------------
 lib/private/share/searchresultsorter.php | 53 ++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 24 deletions(-)

diff --git a/core/ajax/share.php b/core/ajax/share.php
index 5c2dbc6..21e320a 100644
--- a/core/ajax/share.php
+++ b/core/ajax/share.php
@@ -354,32 +354,11 @@ if (isset($_POST['action']) && isset($_POST['itemType']) && isset($_POST['itemSo
 						break;
 					}
 				}
-				usort($shareWith, 'sortSearchFirst');
+				$sorter = new \OC\Share\SearchResultSorter($_GET['search'],
+														   'label');
+				usort($shareWith, array($sorter, 'sort'));
 				OC_JSON::success(array('data' => $shareWith));
 			}
 			break;
 	}
 }
-
-
-/**
- * User and Group names matching the search term at the beginning shall appear
- * on top of the share dialog.
- * Callback function for usort. http://php.net/usort
- */
-function sortSearchFirst($a, $b) {
-	$enc = 'UTF-8';
-	$nameA = mb_strtolower($a['label'], $enc);
-	$nameB = mb_strtolower($b['label'], $enc);
-	$term = mb_strtolower($_GET['search'], $enc);
-	$i = mb_strpos($nameA, $term, 0, 'UTF-8');
-	$j = mb_strpos($nameB, $term, 0, 'UTF-8');
-
-	if($i === $j) {
-		return 0;
-	} elseif ($i === 0) {
-		return -1;
-	} else {
-		return 1;
-	}
-}
diff --git a/lib/private/share/searchresultsorter.php b/lib/private/share/searchresultsorter.php
new file mode 100644
index 0000000..27f94a6
--- /dev/null
+++ b/lib/private/share/searchresultsorter.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ * Copyright (c) 2014 Arthur Schiwon <blizzz at owncloud.bzoc>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ *
+ */
+namespace OC\Share;
+
+class SearchResultSorter {
+	private $search;
+	private $encoding;
+	private $key;
+
+	/**
+	 * @param $search the search term as was given by the user
+	 * @param $key the array key containing the value that should be compared
+	 * against
+	 * @param $encoding optional, encoding to use, defaults to UTF-8
+	 */
+	public function __construct($search, $key, $encoding = 'UTF-8') {
+		$this->encoding = $encoding;
+		$this->key = $key;
+		$this->search = mb_strtolower($search, $this->encoding);
+	}
+
+	/**
+	 * User and Group names matching the search term at the beginning shall appear
+	 * on top of the share dialog.
+	 * Callback function for usort. http://php.net/usort
+	 */
+	public function sort($a, $b) {
+		if(!isset($a[$this->key]) || !isset($b[$this->key])) {
+			\OCP\Util::writeLog('core', 'Sharing: cannot sort due to missing'.
+										'array key', \OC_Log::ERROR);
+			return 0;
+		}
+		$nameA = mb_strtolower($a[$this->key], $this->encoding);
+		$nameB = mb_strtolower($b[$this->key], $this->encoding);
+		$i = mb_strpos($nameA, $this->search, 0, $this->encoding);
+		$j = mb_strpos($nameB, $this->search, 0, $this->encoding);
+
+		if($i === $j) {
+			return 0;
+		} elseif ($i === 0) {
+			return -1;
+		} else {
+			return 1;
+		}
+	}
+}
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list