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