]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearchModule.php
Merge pull request #7669 from tobiasd/20190927-en
[friendica.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\Core\Search;
11 use Friendica\Model;
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;
17
18 /**
19  * Base class for search modules
20  */
21 class BaseSearchModule extends BaseModule
22 {
23         /**
24          * Performs a search with an optional prefix
25          *
26          * @param string $prefix A optional prefix (e.g. @ or !) for searching
27          *
28          * @return string
29          * @throws HTTPException\InternalServerErrorException
30          * @throws \ImagickException
31          */
32         public static function performSearch($prefix = '')
33         {
34                 $a      = self::getApp();
35                 $config = $a->getConfig();
36
37                 $type = Search::TYPE_ALL;
38
39                 $localSearch = $config->get('system', 'poco_local_search');
40
41                 $search = $prefix . Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
42
43                 if (!$search) {
44                         return '';
45                 }
46
47                 $header = '';
48
49                 if (strpos($search, '@') === 0) {
50                         $search  = substr($search, 1);
51                         $type    = Search::TYPE_PEOPLE;
52                         $header  = L10n::t('People Search - %s', $search);
53
54                         if (strrpos($search, '@') > 0) {
55                                 $results = Search::getContactsFromProbe($search);
56                         }
57                 }
58
59                 if (strpos($search, '!') === 0) {
60                         $search = substr($search, 1);
61                         $type   = Search::TYPE_FORUM;
62                         $header = L10n::t('Forum Search - %s', $search);
63                 }
64
65                 $pager = new Pager($a->query_string);
66
67                 if ($localSearch && empty($results)) {
68                         $pager->setItemsPerPage(80);
69                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
70                 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
71                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
72                         $pager->setItemsPerPage($results->getItemsPage());
73                 }
74
75                 return self::printResult($results, $pager, $header);
76         }
77
78         /**
79          * Prints a human readable search result
80          *
81          * @param ResultList $results
82          * @param Pager      $pager
83          * @param string     $header
84          *
85          * @return string The result
86          * @throws HTTPException\InternalServerErrorException
87          * @throws \ImagickException
88          */
89         protected static function printResult(ResultList $results, Pager $pager, $header = '')
90         {
91                 if ($results->getTotal() == 0) {
92                         info(L10n::t('No matches'));
93                         return '';
94                 }
95
96                 $a = self::getApp();
97
98                 $id      = 0;
99                 $entries = [];
100                 foreach ($results->getResults() as $result) {
101
102                         // in case the result is a contact result, add a contact-specific entry
103                         if ($result instanceof ContactResult) {
104
105                                 $alt_text    = '';
106                                 $location    = '';
107                                 $about       = '';
108                                 $accountType = '';
109                                 $photo_menu  = [];
110
111                                 // If We already know this contact then don't show the "connect" button
112                                 if ($result->getCid() > 0 || $result->getPCid() > 0) {
113                                         $connLink = "";
114                                         $connTxt  = "";
115                                         $contact  = Model\Contact::getById(
116                                                 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
117                                         );
118
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);
126                                         } else {
127                                                 $photo_menu = [];
128                                         }
129                                 } else {
130                                         $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
131                                         $connTxt  = L10n::t('Connect');
132
133                                         $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
134                                         $photo_menu['follow']  = [L10n::t("Connect/Follow"), $connLink];
135                                 }
136
137                                 $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
138
139                                 $entry     = [
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(),
151                                         'about'        => $about,
152                                         'account_type' => $accountType,
153                                         'network'      => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
154                                         'id'           => ++$id,
155                                 ];
156                                 $entries[] = $entry;
157                         }
158                 }
159
160                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
161                 return Renderer::replaceMacros($tpl, [
162                         'title'     => $header,
163                         '$contacts' => $entries,
164                         '$paginate' => $pager->renderFull($results->getTotal()),
165                 ]);
166         }
167 }