]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearchModule.php
Move mod/search to src/Module/Search/Index
[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 $search Search query
27          * @param string $prefix A optional prefix (e.g. @ or !) for searching
28          *
29          * @return string
30          * @throws HTTPException\InternalServerErrorException
31          * @throws \ImagickException
32          */
33         public static function performSearch($search, $prefix = '')
34         {
35                 $a      = self::getApp();
36                 $config = $a->getConfig();
37
38                 $type = Search::TYPE_ALL;
39
40                 $localSearch = $config->get('system', 'poco_local_search');
41
42                 $search = $prefix . $search;
43
44                 if (!$search) {
45                         return '';
46                 }
47
48                 $header = '';
49
50                 if (strpos($search, '@') === 0) {
51                         $search  = substr($search, 1);
52                         $type    = Search::TYPE_PEOPLE;
53                         $header  = L10n::t('People Search - %s', $search);
54
55                         if (strrpos($search, '@') > 0) {
56                                 $results = Search::getContactsFromProbe($search);
57                         }
58                 }
59
60                 if (strpos($search, '!') === 0) {
61                         $search = substr($search, 1);
62                         $type   = Search::TYPE_FORUM;
63                         $header = L10n::t('Forum Search - %s', $search);
64                 }
65
66                 $pager = new Pager(self::getArgs()->getQueryString());
67
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());
74                 }
75
76                 return self::printResult($results, $pager, $header);
77         }
78
79         /**
80          * Prints a human readable search result
81          *
82          * @param ResultList $results
83          * @param Pager      $pager
84          * @param string     $header
85          *
86          * @return string The result
87          * @throws HTTPException\InternalServerErrorException
88          * @throws \ImagickException
89          */
90         protected static function printResult(ResultList $results, Pager $pager, $header = '')
91         {
92                 if ($results->getTotal() == 0) {
93                         info(L10n::t('No matches'));
94                         return '';
95                 }
96
97                 $a = self::getApp();
98
99                 $id      = 0;
100                 $entries = [];
101                 foreach ($results->getResults() as $result) {
102
103                         // in case the result is a contact result, add a contact-specific entry
104                         if ($result instanceof ContactResult) {
105
106                                 $alt_text    = '';
107                                 $location    = '';
108                                 $about       = '';
109                                 $accountType = '';
110                                 $photo_menu  = [];
111
112                                 // If We already know this contact then don't show the "connect" button
113                                 if ($result->getCid() > 0 || $result->getPCid() > 0) {
114                                         $connLink = "";
115                                         $connTxt  = "";
116                                         $contact  = Model\Contact::getById(
117                                                 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
118                                         );
119
120                                         if (!empty($contact)) {
121                                                 $photo_menu  = Model\Contact::photoMenu($contact);
122                                                 $details     = Contact::getContactTemplateVars($contact);
123                                                 $alt_text    = $details['alt_text'];
124                                                 $location    = $contact['location'];
125                                                 $about       = $contact['about'];
126                                                 $accountType = Model\Contact::getAccountType($contact);
127                                         } else {
128                                                 $photo_menu = [];
129                                         }
130                                 } else {
131                                         $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
132                                         $connTxt  = L10n::t('Connect');
133
134                                         $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
135                                         $photo_menu['follow']  = [L10n::t("Connect/Follow"), $connLink];
136                                 }
137
138                                 $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
139
140                                 $entry     = [
141                                         'alt_text'     => $alt_text,
142                                         'url'          => Model\Contact::magicLink($result->getUrl()),
143                                         'itemurl'      => $result->getItem(),
144                                         'name'         => $result->getName(),
145                                         'thumb'        => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
146                                         'img_hover'    => $result->getTags(),
147                                         'conntxt'      => $connTxt,
148                                         'connlnk'      => $connLink,
149                                         'photo_menu'   => $photo_menu,
150                                         'details'      => $location,
151                                         'tags'         => $result->getTags(),
152                                         'about'        => $about,
153                                         'account_type' => $accountType,
154                                         'network'      => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
155                                         'id'           => ++$id,
156                                 ];
157                                 $entries[] = $entry;
158                         }
159                 }
160
161                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
162                 return Renderer::replaceMacros($tpl, [
163                         'title'     => $header,
164                         '$contacts' => $entries,
165                         '$paginate' => $pager->renderFull($results->getTotal()),
166                 ]);
167         }
168 }