2018-11-12 03:08:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Directory\Controllers\Api;
|
|
|
|
|
2018-12-24 03:15:50 +01:00
|
|
|
use Friendica\Directory\Content\Pager;
|
2018-11-12 03:08:33 +01:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
|
|
/**
|
2020-06-13 19:15:53 +02:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-11-12 03:08:33 +01:00
|
|
|
*/
|
|
|
|
class Search
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Friendica\Directory\Models\Profile
|
|
|
|
*/
|
|
|
|
private $profileModel;
|
|
|
|
/**
|
2018-11-16 05:59:00 +01:00
|
|
|
* @var \Gettext\TranslatorInterface
|
2018-11-12 03:08:33 +01:00
|
|
|
*/
|
|
|
|
private $l10n;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Friendica\Directory\Models\Profile $profileModel,
|
2018-11-16 05:59:00 +01:00
|
|
|
\Gettext\TranslatorInterface $l10n
|
2018-11-12 03:08:33 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
$this->profileModel = $profileModel;
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
|
|
|
|
{
|
2020-09-27 19:41:28 +02:00
|
|
|
$limit = min(100, filter_input(INPUT_GET, 'limit', FILTER_SANITIZE_NUMBER_INT) ?: 20);
|
|
|
|
|
|
|
|
$pager = new Pager($this->l10n, $request, $limit);
|
2018-11-12 03:08:33 +01:00
|
|
|
|
|
|
|
$originalQuery = $query = filter_input(INPUT_GET, 'q');
|
|
|
|
|
|
|
|
$field = filter_input(INPUT_GET, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW & FILTER_FLAG_STRIP_HIGH);
|
|
|
|
|
|
|
|
if ($field) {
|
|
|
|
$query .= '%';
|
|
|
|
$sql_where = '`' . $field . '` LIKE :query';
|
|
|
|
} else {
|
2020-09-27 14:39:31 +02:00
|
|
|
$sql_where = "MATCH (p.`name`, p.`pdesc`, p.`username`, p.`locality`, p.`region`, p.`country`, p.`tags` )
|
2018-11-12 03:08:33 +01:00
|
|
|
AGAINST (:query IN BOOLEAN MODE)";
|
2021-06-21 17:15:57 +02:00
|
|
|
// At sign (@) is a reserved symbol in InnoDB full-text search, it can't be escaped
|
|
|
|
$query = str_replace('@', ' ', $query);
|
2018-11-12 03:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$values = ['query' => $query];
|
|
|
|
|
|
|
|
$account_type = $args['account_type'] ?? 'All';
|
|
|
|
if ($account_type != 'All') {
|
|
|
|
$sql_where .= '
|
|
|
|
AND `account_type` = :account_type';
|
|
|
|
$values['account_type'] = $account_type;
|
|
|
|
}
|
|
|
|
|
2022-05-07 21:52:55 +02:00
|
|
|
$profiles = $this->profileModel->getListForDisplay(
|
|
|
|
null,
|
|
|
|
$pager->getItemsPerPage(),
|
|
|
|
$pager->getStart(),
|
|
|
|
$sql_where,
|
|
|
|
$values,
|
|
|
|
);
|
2018-11-12 03:08:33 +01:00
|
|
|
|
|
|
|
$count = $this->profileModel->getCountForDisplay($sql_where, $values);
|
|
|
|
|
|
|
|
$vars = [
|
2018-11-16 05:59:00 +01:00
|
|
|
'query' => $originalQuery,
|
|
|
|
'field' => $field,
|
|
|
|
'page' => $pager->getPage(),
|
2018-11-12 03:08:33 +01:00
|
|
|
'itemsperpage' => $pager->getItemsPerPage(),
|
2018-11-16 05:59:00 +01:00
|
|
|
'count' => $count,
|
|
|
|
'profiles' => $profiles
|
2018-11-12 03:08:33 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return $response->withJson($vars);
|
|
|
|
}
|
|
|
|
}
|