3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\ContactSelector;
7 use Friendica\Content\Pager;
8 use Friendica\Core\Renderer;
9 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;
18 * Base class for search modules
20 class BaseSearchModule extends BaseModule
23 * Performs a contact search with an optional prefix
25 * @param string $search Search query
26 * @param string $prefix A optional prefix (e.g. @ or !) for searching
29 * @throws HTTPException\InternalServerErrorException
30 * @throws \ImagickException
32 public static function performContactSearch($search, $prefix = '')
35 $config = DI::config();
37 $type = Search::TYPE_ALL;
39 $localSearch = $config->get('system', 'poco_local_search');
41 $search = $prefix . $search;
49 if (strpos($search, '@') === 0) {
50 $search = substr($search, 1);
51 $type = Search::TYPE_PEOPLE;
52 $header = DI::l10n()->t('People Search - %s', $search);
54 if (strrpos($search, '@') > 0) {
55 $results = Search::getContactsFromProbe($search);
59 if (strpos($search, '!') === 0) {
60 $search = substr($search, 1);
61 $type = Search::TYPE_FORUM;
62 $header = DI::l10n()->t('Forum Search - %s', $search);
66 $pager = new Pager($args->getQueryString());
68 if ($localSearch && empty($results)) {
69 $pager->setItemsPerPage(80);
70 $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
71 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
72 $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
73 $pager->setItemsPerPage($results->getItemsPage());
76 return self::printResult($results, $pager, $header);
80 * Prints a human readable search result
82 * @param ResultList $results
84 * @param string $header
86 * @return string The result
87 * @throws HTTPException\InternalServerErrorException
88 * @throws \ImagickException
90 protected static function printResult(ResultList $results, Pager $pager, $header = '')
92 if ($results->getTotal() == 0) {
93 info(DI::l10n()->t('No matches'));
99 foreach ($results->getResults() as $result) {
101 // in case the result is a contact result, add a contact-specific entry
102 if ($result instanceof ContactResult) {
110 // If We already know this contact then don't show the "connect" button
111 if ($result->getCid() > 0 || $result->getPCid() > 0) {
114 $contact = Model\Contact::getById(
115 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
118 if (!empty($contact)) {
119 $photo_menu = Model\Contact::photoMenu($contact);
120 $details = Contact::getContactTemplateVars($contact);
121 $alt_text = $details['alt_text'];
122 $location = $contact['location'];
123 $about = $contact['about'];
124 $accountType = Model\Contact::getAccountType($contact);
129 $connLink = DI::baseUrl()->get() . '/follow/?url=' . $result->getUrl();
130 $connTxt = DI::l10n()->t('Connect');
132 $photo_menu['profile'] = [DI::l10n()->t("View Profile"), Model\Contact::magicLink($result->getUrl())];
133 $photo_menu['follow'] = [DI::l10n()->t("Connect/Follow"), $connLink];
136 $photo = str_replace("http:///photo/", Search::getGlobalDirectory() . "/photo/", $result->getPhoto());
139 'alt_text' => $alt_text,
140 'url' => Model\Contact::magicLink($result->getUrl()),
141 'itemurl' => $result->getItem(),
142 'name' => $result->getName(),
143 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
144 'img_hover' => $result->getTags(),
145 'conntxt' => $connTxt,
146 'connlnk' => $connLink,
147 'photo_menu' => $photo_menu,
148 'details' => $location,
149 'tags' => $result->getTags(),
151 'account_type' => $accountType,
152 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
159 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
160 return Renderer::replaceMacros($tpl, [
162 '$contacts' => $entries,
163 '$paginate' => $pager->renderFull($results->getTotal()),