2019-05-18 20:50:14 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
2019-10-07 16:16:42 -04:00
|
|
|
use Friendica\App\Arguments;
|
2019-05-18 20:50:14 -04:00
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Content\ContactSelector;
|
|
|
|
use Friendica\Content\Pager;
|
|
|
|
use Friendica\Core\L10n;
|
|
|
|
use Friendica\Core\Renderer;
|
2019-05-20 12:42:27 -04:00
|
|
|
use Friendica\Core\Search;
|
2019-05-20 13:13:37 -04:00
|
|
|
use Friendica\Model;
|
|
|
|
use Friendica\Network\HTTPException;
|
2019-05-20 12:42:27 -04:00
|
|
|
use Friendica\Object\Search\ContactResult;
|
|
|
|
use Friendica\Object\Search\ResultList;
|
2019-05-18 20:50:14 -04:00
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
2019-05-18 23:06:02 -04:00
|
|
|
use Friendica\Util\Strings;
|
2019-05-18 20:50:14 -04:00
|
|
|
|
2019-05-18 22:43:09 -04:00
|
|
|
/**
|
2019-05-18 23:01:46 -04:00
|
|
|
* Base class for search modules
|
2019-05-18 22:43:09 -04:00
|
|
|
*/
|
2019-05-18 23:06:02 -04:00
|
|
|
class BaseSearchModule extends BaseModule
|
2019-05-18 20:50:14 -04:00
|
|
|
{
|
2019-05-18 23:06:02 -04:00
|
|
|
/**
|
|
|
|
* Performs a search with an optional prefix
|
|
|
|
*
|
2019-10-07 14:27:20 -04:00
|
|
|
* @param string $search Search query
|
2019-05-18 23:06:02 -04:00
|
|
|
* @param string $prefix A optional prefix (e.g. @ or !) for searching
|
|
|
|
*
|
|
|
|
* @return string
|
2019-05-20 12:42:27 -04:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-05-18 23:06:02 -04:00
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2019-10-07 14:27:20 -04:00
|
|
|
public static function performSearch($search, $prefix = '')
|
2019-05-18 23:06:02 -04:00
|
|
|
{
|
|
|
|
$a = self::getApp();
|
|
|
|
$config = $a->getConfig();
|
|
|
|
|
2019-05-20 13:13:37 -04:00
|
|
|
$type = Search::TYPE_ALL;
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
$localSearch = $config->get('system', 'poco_local_search');
|
|
|
|
|
2019-10-07 14:27:20 -04:00
|
|
|
$search = $prefix . $search;
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
if (!$search) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
|
|
|
|
if (strpos($search, '@') === 0) {
|
|
|
|
$search = substr($search, 1);
|
2019-05-20 13:19:57 -04:00
|
|
|
$type = Search::TYPE_PEOPLE;
|
2019-05-18 23:06:02 -04:00
|
|
|
$header = L10n::t('People Search - %s', $search);
|
2019-06-20 17:56:18 -04:00
|
|
|
|
|
|
|
if (strrpos($search, '@') > 0) {
|
|
|
|
$results = Search::getContactsFromProbe($search);
|
|
|
|
}
|
2019-05-18 23:06:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($search, '!') === 0) {
|
2019-05-20 13:19:57 -04:00
|
|
|
$search = substr($search, 1);
|
|
|
|
$type = Search::TYPE_FORUM;
|
|
|
|
$header = L10n::t('Forum Search - %s', $search);
|
2019-05-18 23:06:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-07 16:16:42 -04:00
|
|
|
/** @var Arguments $args */
|
|
|
|
$args = self::getClass(Arguments::class);
|
|
|
|
$pager = new Pager($args->getQueryString());
|
2019-05-18 23:06:02 -04:00
|
|
|
|
|
|
|
if ($localSearch && empty($results)) {
|
|
|
|
$pager->setItemsPerPage(80);
|
2019-05-20 13:15:47 -04:00
|
|
|
$results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
|
2019-05-18 23:06:02 -04:00
|
|
|
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
2019-05-20 13:19:57 -04:00
|
|
|
$results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
|
2019-05-18 23:06:02 -04:00
|
|
|
$pager->setItemsPerPage($results->getItemsPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::printResult($results, $pager, $header);
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:01:46 -04:00
|
|
|
/**
|
|
|
|
* Prints a human readable search result
|
|
|
|
*
|
2019-05-20 12:42:27 -04:00
|
|
|
* @param ResultList $results
|
2019-05-20 13:19:57 -04:00
|
|
|
* @param Pager $pager
|
|
|
|
* @param string $header
|
2019-05-18 23:01:46 -04:00
|
|
|
*
|
|
|
|
* @return string The result
|
2019-05-20 12:42:27 -04:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
2019-05-18 23:01:46 -04:00
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
2019-05-20 12:42:27 -04:00
|
|
|
protected static function printResult(ResultList $results, Pager $pager, $header = '')
|
2019-05-18 20:50:14 -04:00
|
|
|
{
|
2019-05-24 08:13:36 -04:00
|
|
|
if ($results->getTotal() == 0) {
|
|
|
|
info(L10n::t('No matches'));
|
2019-05-18 20:50:14 -04:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:01:46 -04:00
|
|
|
$a = self::getApp();
|
|
|
|
|
2019-05-20 13:19:57 -04:00
|
|
|
$id = 0;
|
2019-05-18 20:50:14 -04:00
|
|
|
$entries = [];
|
|
|
|
foreach ($results->getResults() as $result) {
|
|
|
|
|
2019-05-20 12:42:27 -04:00
|
|
|
// in case the result is a contact result, add a contact-specific entry
|
|
|
|
if ($result instanceof ContactResult) {
|
|
|
|
|
|
|
|
$alt_text = '';
|
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$accountType = '';
|
|
|
|
$photo_menu = [];
|
|
|
|
|
|
|
|
// If We already know this contact then don't show the "connect" button
|
|
|
|
if ($result->getCid() > 0 || $result->getPCid() > 0) {
|
|
|
|
$connLink = "";
|
|
|
|
$connTxt = "";
|
|
|
|
$contact = Model\Contact::getById(
|
|
|
|
($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($contact)) {
|
|
|
|
$photo_menu = Model\Contact::photoMenu($contact);
|
|
|
|
$details = Contact::getContactTemplateVars($contact);
|
|
|
|
$alt_text = $details['alt_text'];
|
|
|
|
$location = $contact['location'];
|
|
|
|
$about = $contact['about'];
|
|
|
|
$accountType = Model\Contact::getAccountType($contact);
|
|
|
|
} else {
|
|
|
|
$photo_menu = [];
|
|
|
|
}
|
2019-05-18 20:50:14 -04:00
|
|
|
} else {
|
2019-05-20 12:42:27 -04:00
|
|
|
$connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
|
|
|
|
$connTxt = L10n::t('Connect');
|
|
|
|
|
|
|
|
$photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
|
|
|
|
$photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
|
2019-05-18 20:50:14 -04:00
|
|
|
}
|
|
|
|
|
2019-05-20 12:42:27 -04:00
|
|
|
$photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
|
|
|
|
|
|
|
|
$entry = [
|
|
|
|
'alt_text' => $alt_text,
|
|
|
|
'url' => Model\Contact::magicLink($result->getUrl()),
|
|
|
|
'itemurl' => $result->getItem(),
|
|
|
|
'name' => $result->getName(),
|
|
|
|
'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
|
|
|
|
'img_hover' => $result->getTags(),
|
|
|
|
'conntxt' => $connTxt,
|
|
|
|
'connlnk' => $connLink,
|
|
|
|
'photo_menu' => $photo_menu,
|
|
|
|
'details' => $location,
|
|
|
|
'tags' => $result->getTags(),
|
|
|
|
'about' => $about,
|
|
|
|
'account_type' => $accountType,
|
|
|
|
'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
|
|
|
|
'id' => ++$id,
|
|
|
|
];
|
|
|
|
$entries[] = $entry;
|
2019-05-18 20:50:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
|
|
|
|
return Renderer::replaceMacros($tpl, [
|
|
|
|
'title' => $header,
|
|
|
|
'$contacts' => $entries,
|
|
|
|
'$paginate' => $pager->renderFull($results->getTotal()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|