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