-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
on filtering the share box users and groups whose name begins with the s... #6778
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8227bc4
on filtering the share box users and groups whose name begins with th…
blizzz c6687e1
respect coding guidelines
blizzz f862070
move sorter into a class
blizzz 7e451a2
sort following entries in alphabetical order
blizzz 35b316f
test for share dialoge sorter
blizzz 8a4dbde
wrong tld
blizzz 1cf599d
Inject logger
blizzz 0213928
fix DI
blizzz 3be5022
intendation
blizzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2014 Arthur Schiwon <blizzz@owncloud.com> | ||
* 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; | ||
private $log; | ||
|
||
/** | ||
* @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 | ||
* @param $log optional, an \OC\Log instance | ||
*/ | ||
public function __construct($search, $key, \OC\Log $log = null, $encoding = 'UTF-8') { | ||
$this->encoding = $encoding; | ||
$this->key = $key; | ||
$this->log = $log; | ||
$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. Following entries in alphabetical order. | ||
* Callback function for usort. http://php.net/usort | ||
*/ | ||
public function sort($a, $b) { | ||
if(!isset($a[$this->key]) || !isset($b[$this->key])) { | ||
if(!is_null($this->log)) { | ||
$this->log->error('Sharing dialogue: cannot sort due to ' . | ||
'missing array key', array('app' => 'core')); | ||
} | ||
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 || $i > 0 && $j > 0) { | ||
return strcmp(mb_strtolower($nameA, $this->encoding), | ||
mb_strtolower($nameB, $this->encoding)); | ||
} elseif ($i === 0) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* ownCloud | ||
* | ||
* @author Arthur Schiwon | ||
* @copyright 2014 Arthur Schiwon <blizzz@owncloud.com> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
class Test_Share_Search extends \PHPUnit_Framework_TestCase { | ||
public function testSort() { | ||
$search = 'lin'; | ||
$sorter = new \OC\Share\SearchResultSorter($search, 'foobar'); | ||
|
||
$result = array( | ||
array('foobar' => 'woot'), | ||
array('foobar' => 'linux'), | ||
array('foobar' => 'Linus'), | ||
array('foobar' => 'Bicyclerepairwoman'), | ||
); | ||
|
||
usort($result, array($sorter, 'sort')); | ||
$this->assertTrue($result[0]['foobar'] === 'Linus'); | ||
$this->assertTrue($result[1]['foobar'] === 'linux'); | ||
$this->assertTrue($result[2]['foobar'] === 'Bicyclerepairwoman'); | ||
$this->assertTrue($result[3]['foobar'] === 'woot'); | ||
} | ||
|
||
/** | ||
* @expectedException PHPUnit_Framework_Error | ||
*/ | ||
public function testSortWrongLog() { | ||
$sorter = new \OC\Share\SearchResultSorter('foo', 'bar', 'UTF-8', 'foobar'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something wrong with the spacing here? Not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's just Github.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this line has 5 spaces. the previous one has 1 tab. please fix that