2014-01-15 20:55:05 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
|
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-01-15 20:55:05 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC\Share;
|
|
|
|
|
2015-04-30 12:52:30 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
|
2014-01-15 20:55:05 +04:00
|
|
|
class SearchResultSorter {
|
|
|
|
private $search;
|
|
|
|
private $encoding;
|
|
|
|
private $key;
|
2014-01-16 14:00:22 +04:00
|
|
|
private $log;
|
2014-01-15 20:55:05 +04:00
|
|
|
|
|
|
|
/**
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $search the search term as was given by the user
|
|
|
|
* @param string $key the array key containing the value that should be compared
|
2014-01-15 20:55:05 +04:00
|
|
|
* against
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $encoding optional, encoding to use, defaults to UTF-8
|
2015-04-30 12:52:30 +03:00
|
|
|
* @param ILogger $log optional
|
2014-01-15 20:55:05 +04:00
|
|
|
*/
|
2015-04-30 12:52:30 +03:00
|
|
|
public function __construct($search, $key, ILogger $log = null, $encoding = 'UTF-8') {
|
2014-01-15 20:55:05 +04:00
|
|
|
$this->encoding = $encoding;
|
|
|
|
$this->key = $key;
|
2014-02-05 20:05:56 +04:00
|
|
|
$this->log = $log;
|
2014-01-15 20:55:05 +04:00
|
|
|
$this->search = mb_strtolower($search, $this->encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* User and Group names matching the search term at the beginning shall appear
|
2014-01-15 21:19:20 +04:00
|
|
|
* on top of the share dialog. Following entries in alphabetical order.
|
2014-01-15 20:55:05 +04:00
|
|
|
* Callback function for usort. http://php.net/usort
|
|
|
|
*/
|
|
|
|
public function sort($a, $b) {
|
|
|
|
if(!isset($a[$this->key]) || !isset($b[$this->key])) {
|
2014-02-05 20:05:56 +04:00
|
|
|
if(!is_null($this->log)) {
|
|
|
|
$this->log->error('Sharing dialogue: cannot sort due to ' .
|
|
|
|
'missing array key', array('app' => 'core'));
|
|
|
|
}
|
2014-01-15 20:55:05 +04:00
|
|
|
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);
|
|
|
|
|
2014-01-15 21:19:20 +04:00
|
|
|
if($i === $j || $i > 0 && $j > 0) {
|
|
|
|
return strcmp(mb_strtolower($nameA, $this->encoding),
|
|
|
|
mb_strtolower($nameB, $this->encoding));
|
2014-01-15 20:55:05 +04:00
|
|
|
} elseif ($i === 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|