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 \Atlas\Pdo\Connection
|
|
|
|
*/
|
|
|
|
private $atlas;
|
|
|
|
/**
|
|
|
|
* @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(
|
|
|
|
\Atlas\Pdo\Connection $atlas,
|
|
|
|
\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->atlas = $atlas;
|
|
|
|
$this->profileModel = $profileModel;
|
|
|
|
$this->l10n = $l10n;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
|
|
|
|
{
|
|
|
|
$pager = new Pager($this->l10n, $request, 20);
|
|
|
|
|
|
|
|
$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 {
|
|
|
|
$sql_where = "MATCH (p.`name`, p.`pdesc`, p.`profile_url`, p.`locality`, p.`region`, p.`country`, p.`tags` )
|
|
|
|
AGAINST (:query IN BOOLEAN MODE)";
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|