3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Pager;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Object\Search\ResultList;
11 use Friendica\Util\Proxy as ProxyUtils;
13 use Friendica\Util\Strings;
16 * Base class for search modules
18 class BaseSearchModule extends BaseModule
21 * Performs a search with an optional prefix
23 * @param string $prefix A optional prefix (e.g. @ or !) for searching
26 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
27 * @throws \ImagickException
29 public static function performSearch($prefix = '')
32 $config = $a->getConfig();
36 $localSearch = $config->get('system', 'poco_local_search');
38 $search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
46 if (strpos($search, '@') === 0) {
47 $search = substr($search, 1);
48 $header = L10n::t('People Search - %s', $search);
49 $results = Model\Search::searchUser($search);
52 if (strpos($search, '!') === 0) {
53 $search = substr($search, 1);
55 $header = L10n::t('Forum Search - %s', $search);
58 $pager = new Pager($a->query_string);
60 if ($localSearch && empty($results)) {
61 $pager->setItemsPerPage(80);
62 $results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
64 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
65 $results = Model\Search::searchDirectory($search, $pager->getPage());
66 $pager->setItemsPerPage($results->getItemsPage());
69 return self::printResult($results, $pager, $header);
73 * Prints a human readable search result
75 * @param ResultList $results
77 * @param string $header
79 * @return string The result
80 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
81 * @throws \ImagickException
83 protected static function printResult(ResultList $results, Pager $pager, $header = '')
85 if (empty($results) || empty($results->getResults())) {
86 info(L10n::t('No matches') . EOL);
94 foreach ($results->getResults() as $result) {
102 // If We already know this contact then don't show the "connect" button
103 if ($result->getCid() > 0 || $result->getPcid() > 0) {
106 $contact = Model\Contact::getById(
107 ($result->getCid() > 0) ? $result->getCid() : $result->getPcid()
110 if (!empty($contact)) {
111 $photo_menu = Model\Contact::photoMenu($contact);
112 $details = Contact::getContactTemplateVars($contact);
113 $alt_text = $details['alt_text'];
114 $location = $contact['location'];
115 $about = $contact['about'];
116 $accountType = Model\Contact::getAccountType($contact);
121 $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
122 $connTxt = L10n::t('Connect');
124 $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
125 $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
128 $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
131 'alt_text' => $alt_text,
132 'url' => Model\Contact::magicLink($result->getUrl()),
133 'itemurl' => $result->getItem(),
134 'name' => $result->getName(),
135 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
136 'img_hover' => $result->getTags(),
137 'conntxt' => $connTxt,
138 'connlnk' => $connLink,
139 'photo_menu' => $photo_menu,
140 'details' => $location,
141 'tags' => $result->getTags(),
143 'account_type' => $accountType,
144 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
150 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
151 return Renderer::replaceMacros($tpl, [
153 '$contacts' => $entries,
154 '$paginate' => $pager->renderFull($results->getTotal()),