]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearchModule.php
Merge remote-tracking branch 'upstream/develop' into http-417
[friendica.git] / src / Module / BaseSearchModule.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\App\Arguments;
6 use Friendica\BaseModule;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Pager;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\Search;
12 use Friendica\Model;
13 use Friendica\Network\HTTPException;
14 use Friendica\Object\Search\ContactResult;
15 use Friendica\Object\Search\ResultList;
16 use Friendica\Util\Proxy as ProxyUtils;
17 use Friendica\Util\Strings;
18
19 /**
20  * Base class for search modules
21  */
22 class BaseSearchModule extends BaseModule
23 {
24         /**
25          * Performs a search with an optional prefix
26          *
27          * @param string $search Search query
28          * @param string $prefix A optional prefix (e.g. @ or !) for searching
29          *
30          * @return string
31          * @throws HTTPException\InternalServerErrorException
32          * @throws \ImagickException
33          */
34         public static function performSearch($search, $prefix = '')
35         {
36                 $a      = self::getApp();
37                 $config = $a->getConfig();
38
39                 $type = Search::TYPE_ALL;
40
41                 $localSearch = $config->get('system', 'poco_local_search');
42
43                 $search = $prefix . $search;
44
45                 if (!$search) {
46                         return '';
47                 }
48
49                 $header = '';
50
51                 if (strpos($search, '@') === 0) {
52                         $search  = substr($search, 1);
53                         $type    = Search::TYPE_PEOPLE;
54                         $header  = L10n::t('People Search - %s', $search);
55
56                         if (strrpos($search, '@') > 0) {
57                                 $results = Search::getContactsFromProbe($search);
58                         }
59                 }
60
61                 if (strpos($search, '!') === 0) {
62                         $search = substr($search, 1);
63                         $type   = Search::TYPE_FORUM;
64                         $header = L10n::t('Forum Search - %s', $search);
65                 }
66
67                 /** @var Arguments $args */
68                 $args = self::getClass(Arguments::class);
69                 $pager = new Pager($args->getQueryString());
70
71                 if ($localSearch && empty($results)) {
72                         $pager->setItemsPerPage(80);
73                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
74                 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
75                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
76                         $pager->setItemsPerPage($results->getItemsPage());
77                 }
78
79                 return self::printResult($results, $pager, $header);
80         }
81
82         /**
83          * Prints a human readable search result
84          *
85          * @param ResultList $results
86          * @param Pager      $pager
87          * @param string     $header
88          *
89          * @return string The result
90          * @throws HTTPException\InternalServerErrorException
91          * @throws \ImagickException
92          */
93         protected static function printResult(ResultList $results, Pager $pager, $header = '')
94         {
95                 if ($results->getTotal() == 0) {
96                         info(L10n::t('No matches'));
97                         return '';
98                 }
99
100                 $a = self::getApp();
101
102                 $id      = 0;
103                 $entries = [];
104                 foreach ($results->getResults() as $result) {
105
106                         // in case the result is a contact result, add a contact-specific entry
107                         if ($result instanceof ContactResult) {
108
109                                 $alt_text    = '';
110                                 $location    = '';
111                                 $about       = '';
112                                 $accountType = '';
113                                 $photo_menu  = [];
114
115                                 // If We already know this contact then don't show the "connect" button
116                                 if ($result->getCid() > 0 || $result->getPCid() > 0) {
117                                         $connLink = "";
118                                         $connTxt  = "";
119                                         $contact  = Model\Contact::getById(
120                                                 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
121                                         );
122
123                                         if (!empty($contact)) {
124                                                 $photo_menu  = Model\Contact::photoMenu($contact);
125                                                 $details     = Contact::getContactTemplateVars($contact);
126                                                 $alt_text    = $details['alt_text'];
127                                                 $location    = $contact['location'];
128                                                 $about       = $contact['about'];
129                                                 $accountType = Model\Contact::getAccountType($contact);
130                                         } else {
131                                                 $photo_menu = [];
132                                         }
133                                 } else {
134                                         $connLink = $a->getBaseURL() . '/follow/?url=' . $result->getUrl();
135                                         $connTxt  = L10n::t('Connect');
136
137                                         $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($result->getUrl())];
138                                         $photo_menu['follow']  = [L10n::t("Connect/Follow"), $connLink];
139                                 }
140
141                                 $photo = str_replace("http:///photo/", get_server() . "/photo/", $result->getPhoto());
142
143                                 $entry     = [
144                                         'alt_text'     => $alt_text,
145                                         'url'          => Model\Contact::magicLink($result->getUrl()),
146                                         'itemurl'      => $result->getItem(),
147                                         'name'         => $result->getName(),
148                                         'thumb'        => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
149                                         'img_hover'    => $result->getTags(),
150                                         'conntxt'      => $connTxt,
151                                         'connlnk'      => $connLink,
152                                         'photo_menu'   => $photo_menu,
153                                         'details'      => $location,
154                                         'tags'         => $result->getTags(),
155                                         'about'        => $about,
156                                         'account_type' => $accountType,
157                                         'network'      => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
158                                         'id'           => ++$id,
159                                 ];
160                                 $entries[] = $entry;
161                         }
162                 }
163
164                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
165                 return Renderer::replaceMacros($tpl, [
166                         'title'     => $header,
167                         '$contacts' => $entries,
168                         '$paginate' => $pager->renderFull($results->getTotal()),
169                 ]);
170         }
171 }