3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\ContactSelector;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Search;
31 use Friendica\Network\HTTPException;
32 use Friendica\Object\Search\ContactResult;
33 use Friendica\Object\Search\ResultList;
34 use Friendica\Util\Proxy as ProxyUtils;
37 * Base class for search modules
39 class BaseSearch extends BaseModule
42 * Performs a contact search with an optional prefix
44 * @param string $search Search query
45 * @param string $prefix A optional prefix (e.g. @ or !) for searching
48 * @throws HTTPException\InternalServerErrorException
49 * @throws \ImagickException
51 public static function performContactSearch($search, $prefix = '')
54 $config = DI::config();
56 $type = Search::TYPE_ALL;
58 $localSearch = $config->get('system', 'poco_local_search');
60 $search = $prefix . $search;
68 if (strpos($search, '@') === 0) {
69 $search = substr($search, 1);
70 $type = Search::TYPE_PEOPLE;
71 $header = DI::l10n()->t('People Search - %s', $search);
73 if (strrpos($search, '@') > 0) {
74 $results = Search::getContactsFromProbe($search);
78 if (strpos($search, '!') === 0) {
79 $search = substr($search, 1);
80 $type = Search::TYPE_FORUM;
81 $header = DI::l10n()->t('Forum Search - %s', $search);
84 if (DI::mode()->isMobile()) {
85 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
86 DI::config()->get('system', 'itemspage_network_mobile'));
88 $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
89 DI::config()->get('system', 'itemspage_network'));
92 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
94 if ($localSearch && empty($results)) {
95 $pager->setItemsPerPage(80);
96 $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
97 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
98 $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
99 $pager->setItemsPerPage($results->getItemsPage());
102 return self::printResult($results, $pager, $header);
106 * Prints a human readable search result
108 * @param ResultList $results
109 * @param Pager $pager
110 * @param string $header
112 * @return string The result
113 * @throws HTTPException\InternalServerErrorException
114 * @throws \ImagickException
116 protected static function printResult(ResultList $results, Pager $pager, $header = '')
118 if ($results->getTotal() == 0) {
119 notice(DI::l10n()->t('No matches'));
125 foreach ($results->getResults() as $result) {
127 // in case the result is a contact result, add a contact-specific entry
128 if ($result instanceof ContactResult) {
136 // If We already know this contact then don't show the "connect" button
137 if ($result->getCid() > 0 || $result->getPCid() > 0) {
140 $contact = Model\Contact::getById(
141 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
144 if (!empty($contact)) {
145 $photo_menu = Model\Contact::photoMenu($contact);
146 $details = Contact::getContactTemplateVars($contact);
147 $alt_text = $details['alt_text'];
148 $location = $contact['location'];
149 $about = $contact['about'];
150 $accountType = Model\Contact::getAccountType($contact);
155 $connLink = DI::baseUrl()->get() . '/follow/?url=' . $result->getUrl();
156 $connTxt = DI::l10n()->t('Connect');
158 $photo_menu['profile'] = [DI::l10n()->t("View Profile"), Model\Contact::magicLink($result->getUrl())];
159 $photo_menu['follow'] = [DI::l10n()->t("Connect/Follow"), $connLink];
162 $photo = str_replace("http:///photo/", Search::getGlobalDirectory() . "/photo/", $result->getPhoto());
165 'alt_text' => $alt_text,
166 'url' => Model\Contact::magicLink($result->getUrl()),
167 'itemurl' => $result->getItem(),
168 'name' => $result->getName(),
169 'thumb' => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
170 'img_hover' => $result->getTags(),
171 'conntxt' => $connTxt,
172 'connlnk' => $connLink,
173 'photo_menu' => $photo_menu,
174 'details' => $location,
175 'tags' => $result->getTags(),
177 'account_type' => $accountType,
178 'network' => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
185 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
186 return Renderer::replaceMacros($tpl, [
188 '$contacts' => $entries,
189 '$paginate' => $pager->renderFull($results->getTotal()),