1
0
Fork 0
friendica_2020-09-1_sharedH.../src/Module/BaseSearch.php

143 lines
4.1 KiB
PHP
Raw Normal View History

2019-05-19 02:50:14 +02:00
<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-05-19 02:50:14 +02:00
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Content\ContactSelector;
use Friendica\Content\Pager;
use Friendica\Core\Renderer;
use Friendica\Core\Search;
use Friendica\DI;
2019-05-20 19:13:37 +02:00
use Friendica\Model;
use Friendica\Network\HTTPException;
use Friendica\Object\Search\ContactResult;
use Friendica\Object\Search\ResultList;
2019-05-19 02:50:14 +02:00
2019-05-19 04:43:09 +02:00
/**
* Base class for search modules
2019-05-19 04:43:09 +02:00
*/
2020-01-23 05:14:14 +01:00
class BaseSearch extends BaseModule
2019-05-19 02:50:14 +02:00
{
/**
* Performs a contact search with an optional prefix
*
* @param string $search Search query
* @param string $prefix A optional prefix (e.g. @ or !) for searching
*
* @return string
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public static function performContactSearch($search, $prefix = '')
{
$a = DI::app();
$config = DI::config();
2019-05-20 19:13:37 +02:00
$type = Search::TYPE_ALL;
$localSearch = $config->get('system', 'poco_local_search');
$search = $prefix . $search;
if (!$search) {
return '';
}
$header = '';
if (strpos($search, '@') === 0) {
$search = substr($search, 1);
2019-05-20 19:19:57 +02:00
$type = Search::TYPE_PEOPLE;
$header = DI::l10n()->t('People Search - %s', $search);
2019-06-20 23:56:18 +02:00
if (strrpos($search, '@') > 0) {
$results = Search::getContactsFromProbe($search);
}
}
if (strpos($search, '!') === 0) {
2019-05-20 19:19:57 +02:00
$search = substr($search, 1);
$type = Search::TYPE_FORUM;
$header = DI::l10n()->t('Forum Search - %s', $search);
}
if (DI::mode()->isMobile()) {
$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
DI::config()->get('system', 'itemspage_network_mobile'));
} else {
$itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
DI::config()->get('system', 'itemspage_network'));
}
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
if ($localSearch && empty($results)) {
$pager->setItemsPerPage(80);
2019-05-20 19:15:47 +02:00
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
2019-05-20 19:19:57 +02:00
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
$pager->setItemsPerPage($results->getItemsPage());
}
return self::printResult($results, $pager, $header);
}
/**
* Prints a human readable search result
*
* @param ResultList $results
2019-05-20 19:19:57 +02:00
* @param Pager $pager
* @param string $header
*
* @return string The result
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
protected static function printResult(ResultList $results, Pager $pager, $header = '')
2019-05-19 02:50:14 +02:00
{
if ($results->getTotal() == 0) {
2020-07-23 08:11:21 +02:00
notice(DI::l10n()->t('No matches'));
2019-05-19 02:50:14 +02:00
return '';
}
2019-05-20 19:19:57 +02:00
$id = 0;
2019-05-19 02:50:14 +02:00
$entries = [];
foreach ($results->getResults() as $result) {
// in case the result is a contact result, add a contact-specific entry
if ($result instanceof ContactResult) {
2020-07-28 21:30:55 +02:00
$contact = Model\Contact::getByURL($result->getUrl());
if (!empty($contact)) {
$entries[] = Model\Contact::getTemplateData($contact, ++$id);
}
2019-05-19 02:50:14 +02:00
}
}
$tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
return Renderer::replaceMacros($tpl, [
'title' => $header,
'$contacts' => $entries,
'$paginate' => $pager->renderFull($results->getTotal()),
]);
}
}