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;
13 use Friendica\Network\HTTPException;
14 use Friendica\Object\Search\ContactResult;
15 use Friendica\Object\Search\ResultList;
16 use Friendica\Util\Proxy as ProxyUtils;
19 * Base class for search modules
21 class BaseSearchModule extends BaseModule
24 * Performs a contact search with an optional prefix
26 * @param string $search Search query
27 * @param string $prefix A optional prefix (e.g. @ or !) for searching
30 * @throws HTTPException\InternalServerErrorException
31 * @throws \ImagickException
33 public static function performContactSearch($search, $prefix = '')
36 $config = DI::config();
38 $type = Search::TYPE_ALL;
40 $localSearch = $config->get('system', 'poco_local_search');
42 $search = $prefix . $search;
50 if (strpos($search, '@') === 0) {
51 $search = substr($search, 1);
52 $type = Search::TYPE_PEOPLE;
53 $header = L10n::t('People Search - %s', $search);
55 if (strrpos($search, '@') > 0) {
56 $results = Search::getContactsFromProbe($search);
60 if (strpos($search, '!') === 0) {
61 $search = substr($search, 1);
62 $type = Search::TYPE_FORUM;
63 $header = L10n::t('Forum Search - %s', $search);
67 $pager = new Pager($args->getQueryString());
69 if ($localSearch && empty($results)) {
70 $pager->setItemsPerPage(80);
71 $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
72 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
73 $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
74 $pager->setItemsPerPage($results->getItemsPage());
77 return self::printResult($results, $pager, $header);
81 * Prints a human readable search result
83 * @param ResultList $results
85 * @param string $header
87 * @return string The result
88 * @throws HTTPException\InternalServerErrorException
89 * @throws \ImagickException
91 protected static function printResult(ResultList $results, Pager $pager, $header = '')
93 if ($results->getTotal() == 0) {
94 info(L10n::t('No matches'));
100 foreach ($results->getResults() as $result) {
102 // in case the result is a contact result, add a contact-specific entry
103 if ($result instanceof ContactResult) {
111 // If We already know this contact then don't show the "connect" button
112 if ($result->getCid() > 0 || $result->getPCid() > 0) {
115 $contact = Model\Contact::getById(
116 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
119 if (!empty($contact)) {
120 $photo_menu = Model\Contact::photoMenu($contact);
121 $details = Contact::getContactTemplateVars($contact);
122 $alt_text = $details['alt_text'];
123 $location = $contact['location'];
124 $about = $contact['about'];
125 $accountType = Model\Contact::getAccountType($contact);
130 $connLink = DI::baseUrl()->get() . '/follow/?url=' . $result->getUrl();
131 $connTxt = L10n::t('Connect');
133 $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
134 $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connLink];
137 $photo = str_replace("http:///photo/", Search::getGlobalDirectory() . "/photo/", $result->getPhoto());
140 'alt_text' => $alt_text,
141 'url' => Model\Contact::magicLink($result->getUrl()),
142 'itemurl' => $result->getItem(),
143 'name' => $result->getName(),
144 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
145 'img_hover' => $result->getTags(),
146 'conntxt' => $connTxt,
147 'connlnk' => $connLink,
148 'photo_menu' => $photo_menu,
149 'details' => $location,
150 'tags' => $result->getTags(),
152 'account_type' => $accountType,
153 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
160 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
161 return Renderer::replaceMacros($tpl, [
163 '$contacts' => $entries,
164 '$paginate' => $pager->renderFull($results->getTotal()),