2019-05-18 20:50:14 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Content\ContactSelector;
|
|
|
|
use Friendica\Content\Pager;
|
|
|
|
use Friendica\Core\L10n;
|
|
|
|
use Friendica\Core\Renderer;
|
2019-05-18 23:01:46 -04:00
|
|
|
use Friendica\Object\Search\ResultList;
|
2019-05-18 20:50:14 -04:00
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
|
|
|
use Friendica\Model;
|
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
|
|
|
|
*
|
|
|
|
* @param string $prefix A optional prefix (e.g. @ or !) for searching
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
|
|
|
public static function performSearch($prefix = '')
|
|
|
|
{
|
|
|
|
$a = self::getApp();
|
|
|
|
$config = $a->getConfig();
|
|
|
|
|
|
|
|
$community = false;
|
|
|
|
|
|
|
|
$localSearch = $config->get('system', 'poco_local_search');
|
|
|
|
|
|
|
|
$search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
|
|
|
|
|
|
|
|
if (!$search) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$header = '';
|
|
|
|
|
|
|
|
if (strpos($search, '@') === 0) {
|
|
|
|
$search = substr($search, 1);
|
|
|
|
$header = L10n::t('People Search - %s', $search);
|
|
|
|
$results = Model\Search::searchUser($search);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($search, '!') === 0) {
|
|
|
|
$search = substr($search, 1);
|
|
|
|
$community = true;
|
|
|
|
$header = L10n::t('Forum Search - %s', $search);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pager = new Pager($a->query_string);
|
|
|
|
|
|
|
|
if ($localSearch && empty($results)) {
|
|
|
|
$pager->setItemsPerPage(80);
|
|
|
|
$results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
|
|
|
|
|
|
|
|
} elseif (strlen($config->get('system', 'directory')) && empty($results)) {
|
|
|
|
$results = Model\Search::searchDirectory($search, $pager->getPage());
|
|
|
|
$pager->setItemsPerPage($results->getItemsPage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::printResult($results, $pager, $header);
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:01:46 -04:00
|
|
|
/**
|
|
|
|
* Prints a human readable search result
|
|
|
|
*
|
|
|
|
* @param ResultList $results
|
|
|
|
* @param Pager $pager
|
|
|
|
* @param string $header
|
|
|
|
*
|
|
|
|
* @return string The result
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
|
|
|
protected static function printResult(ResultList $results, Pager $pager, $header = '')
|
2019-05-18 20:50:14 -04:00
|
|
|
{
|
|
|
|
if (empty($results) || empty($results->getResults())) {
|
|
|
|
info(L10n::t('No matches') . EOL);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-05-18 23:01:46 -04:00
|
|
|
$a = self::getApp();
|
|
|
|
|
|
|
|
$id = 0;
|
2019-05-18 20:50:14 -04:00
|
|
|
$entries = [];
|
|
|
|
foreach ($results->getResults() as $result) {
|
|
|
|
|
|
|
|
$alt_text = '';
|
|
|
|
$location = '';
|
|
|
|
$about = '';
|
|
|
|
$accountType = '';
|
2019-05-18 22:43:09 -04:00
|
|
|
$photo_menu = [];
|
2019-05-18 20:50:14 -04:00
|
|
|
|
|
|
|
// If We already know this contact then don't show the "connect" button
|
|
|
|
if ($result->getCid() > 0 || $result->getPcid() > 0) {
|
2019-05-18 22:43:09 -04:00
|
|
|
$connLink = "";
|
|
|
|
$connTxt = "";
|
2019-05-18 22:38:35 -04:00
|
|
|
$contact = Model\Contact::getById(
|
|
|
|
($result->getCid() > 0) ? $result->getCid() : $result->getPcid()
|
|
|
|
);
|
2019-05-18 20:50:14 -04:00
|
|
|
|
2019-05-18 22:38:35 -04:00
|
|
|
if (!empty($contact)) {
|
2019-05-18 20:50:14 -04:00
|
|
|
$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 = [];
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-18 22:43:09 -04:00
|
|
|
$connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
|
|
|
|
$connTxt = L10n::t('Connect');
|
2019-05-18 20:50:14 -04:00
|
|
|
|
|
|
|
$photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
|
2019-05-18 22:43:09 -04:00
|
|
|
$photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
|
2019-05-18 20:50:14 -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(),
|
2019-05-18 22:43:09 -04:00
|
|
|
'conntxt' => $connTxt,
|
|
|
|
'connlnk' => $connLink,
|
2019-05-18 20:50:14 -04:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
|
|
|
|
return Renderer::replaceMacros($tpl, [
|
|
|
|
'title' => $header,
|
|
|
|
'$contacts' => $entries,
|
|
|
|
'$paginate' => $pager->renderFull($results->getTotal()),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|