7028208286bb6ba860798dcb751f65e073a721ff
[friendica.git/.git] / src / Module / BaseSearchModule.php
1 <?php
2
3 namespace Friendica\Module;
4
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;
12 use Friendica\Model;
13 use Friendica\Util\Strings;
14
15 /**
16  * Base class for search modules
17  */
18 class BaseSearchModule extends BaseModule
19 {
20         /**
21          * Performs a search with an optional prefix
22          *
23          * @param string $prefix A optional prefix (e.g. @ or !) for searching
24          *
25          * @return string
26          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
27          * @throws \ImagickException
28          */
29         public static function performSearch($prefix = '')
30         {
31                 $a      = self::getApp();
32                 $config = $a->getConfig();
33
34                 $community = false;
35
36                 $localSearch = $config->get('system', 'poco_local_search');
37
38                 $search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
39
40                 if (!$search) {
41                         return '';
42                 }
43
44                 $header = '';
45
46                 if (strpos($search, '@') === 0) {
47                         $search  = substr($search, 1);
48                         $header  = L10n::t('People Search - %s', $search);
49                         $results = Model\Search::searchUser($search);
50                 }
51
52                 if (strpos($search, '!') === 0) {
53                         $search    = substr($search, 1);
54                         $community = true;
55                         $header    = L10n::t('Forum Search - %s', $search);
56                 }
57
58                 $pager = new Pager($a->query_string);
59
60                 if ($localSearch && empty($results)) {
61                         $pager->setItemsPerPage(80);
62                         $results = Model\Search::searchLocal($search, $pager->getStart(), $pager->getItemsPerPage(), $community);
63
64                 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
65                         $results = Model\Search::searchDirectory($search, $pager->getPage());
66                         $pager->setItemsPerPage($results->getItemsPage());
67                 }
68
69                 return self::printResult($results, $pager, $header);
70         }
71
72         /**
73          * Prints a human readable search result
74          *
75          * @param ResultList $results
76          * @param Pager      $pager
77          * @param string     $header
78          *
79          * @return string The result
80          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
81          * @throws \ImagickException
82          */
83         protected static function printResult(ResultList $results, Pager $pager, $header = '')
84         {
85                 if (empty($results) || empty($results->getResults())) {
86                         info(L10n::t('No matches') . EOL);
87                         return '';
88                 }
89
90                 $a = self::getApp();
91
92                 $id = 0;
93                 $entries = [];
94                 foreach ($results->getResults() as $result) {
95
96                         $alt_text    = '';
97                         $location    = '';
98                         $about       = '';
99                         $accountType = '';
100                         $photo_menu  = [];
101
102                         // If We already know this contact then don't show the "connect" button
103                         if ($result->getCid() > 0 || $result->getPcid() > 0) {
104                                 $connLink = "";
105                                 $connTxt = "";
106                                 $contact = Model\Contact::getById(
107                                         ($result->getCid() > 0) ? $result->getCid() : $result->getPcid()
108                                 );
109
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);
117                                 } else {
118                                         $photo_menu = [];
119                                 }
120                         } else {
121                                 $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
122                                 $connTxt = L10n::t('Connect');
123
124                                 $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
125                                 $photo_menu['follow']  = [L10n::t("Connect/Follow"), $connLink];
126                         }
127
128                         $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
129
130                         $entry     = [
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(),
142                                 'about'        => $about,
143                                 'account_type' => $accountType,
144                                 'network'      => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
145                                 'id'           => ++$id,
146                         ];
147                         $entries[] = $entry;
148                 }
149
150                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
151                 return Renderer::replaceMacros($tpl, [
152                         'title'     => $header,
153                         '$contacts' => $entries,
154                         '$paginate' => $pager->renderFull($results->getTotal()),
155                 ]);
156         }
157 }