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\Core\Search;
12 use Friendica\Network\HTTPException;
13 use Friendica\Object\Search\ContactResult;
14 use Friendica\Object\Search\ResultList;
15 use Friendica\Util\Proxy as ProxyUtils;
16 use Friendica\Util\Strings;
19 * Base class for search modules
21 class BaseSearchModule extends BaseModule
24 * Performs a search with an optional prefix
26 * @param string $prefix A optional prefix (e.g. @ or !) for searching
29 * @throws HTTPException\InternalServerErrorException
30 * @throws \ImagickException
32 public static function performSearch($prefix = '')
35 $config = $a->getConfig();
37 $type = Search::TYPE_ALL;
39 $localSearch = $config->get('system', 'poco_local_search');
41 $search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
49 if (strpos($search, '@') === 0) {
50 $search = substr($search, 1);
51 $type = Search::TYPE_PEOPLE;
52 $header = L10n::t('People Search - %s', $search);
53 $results = Search::getContactsFromProbe($search);
56 if (strpos($search, '!') === 0) {
57 $search = substr($search, 1);
58 $type = Search::TYPE_FORUM;
59 $header = L10n::t('Forum Search - %s', $search);
62 $pager = new Pager($a->query_string);
64 if ($localSearch && empty($results)) {
65 $pager->setItemsPerPage(80);
66 $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
68 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
69 $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
70 $pager->setItemsPerPage($results->getItemsPage());
73 return self::printResult($results, $pager, $header);
77 * Prints a human readable search result
79 * @param ResultList $results
81 * @param string $header
83 * @return string The result
84 * @throws HTTPException\InternalServerErrorException
85 * @throws \ImagickException
87 protected static function printResult(ResultList $results, Pager $pager, $header = '')
89 if (empty($results) || empty($results->getResults())) {
90 info(L10n::t('No matches') . EOL);
98 foreach ($results->getResults() as $result) {
100 // in case the result is a contact result, add a contact-specific entry
101 if ($result instanceof ContactResult) {
109 // If We already know this contact then don't show the "connect" button
110 if ($result->getCid() > 0 || $result->getPCid() > 0) {
113 $contact = Model\Contact::getById(
114 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
117 if (!empty($contact)) {
118 $photo_menu = Model\Contact::photoMenu($contact);
119 $details = Contact::getContactTemplateVars($contact);
120 $alt_text = $details['alt_text'];
121 $location = $contact['location'];
122 $about = $contact['about'];
123 $accountType = Model\Contact::getAccountType($contact);
128 $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
129 $connTxt = L10n::t('Connect');
131 $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
132 $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
135 $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
138 'alt_text' => $alt_text,
139 'url' => Model\Contact::magicLink($result->getUrl()),
140 'itemurl' => $result->getItem(),
141 'name' => $result->getName(),
142 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
143 'img_hover' => $result->getTags(),
144 'conntxt' => $connTxt,
145 'connlnk' => $connLink,
146 'photo_menu' => $photo_menu,
147 'details' => $location,
148 'tags' => $result->getTags(),
150 'account_type' => $accountType,
151 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
158 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
159 return Renderer::replaceMacros($tpl, [
161 '$contacts' => $entries,
162 '$paginate' => $pager->renderFull($results->getTotal()),