]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearch.php
Merge pull request #8909 from MrPetovan/task/ex_auth
[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\Pager;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Search;
28 use Friendica\DI;
29 use Friendica\Model;
30 use Friendica\Network\HTTPException;
31 use Friendica\Object\Search\ContactResult;
32 use Friendica\Object\Search\ResultList;
33
34 /**
35  * Base class for search modules
36  */
37 class BaseSearch extends BaseModule
38 {
39         /**
40          * Performs a contact search with an optional prefix
41          *
42          * @param string $search Search query
43          * @param string $prefix A optional prefix (e.g. @ or !) for searching
44          *
45          * @return string
46          * @throws HTTPException\InternalServerErrorException
47          * @throws \ImagickException
48          */
49         public static function performContactSearch($search, $prefix = '')
50         {
51                 $a      = DI::app();
52                 $config = DI::config();
53
54                 $type = Search::TYPE_ALL;
55
56                 $localSearch = $config->get('system', 'poco_local_search');
57
58                 $search = $prefix . $search;
59
60                 if (!$search) {
61                         return '';
62                 }
63
64                 $header = '';
65
66                 if (strpos($search, '@') === 0) {
67                         $search  = substr($search, 1);
68                         $type    = Search::TYPE_PEOPLE;
69                         $header  = DI::l10n()->t('People Search - %s', $search);
70
71                         if (strrpos($search, '@') > 0) {
72                                 $results = Search::getContactsFromProbe($search);
73                         }
74                 }
75
76                 if (strpos($search, '!') === 0) {
77                         $search = substr($search, 1);
78                         $type   = Search::TYPE_FORUM;
79                         $header = DI::l10n()->t('Forum Search - %s', $search);
80                 }
81
82                 if (DI::mode()->isMobile()) {
83                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
84                                 DI::config()->get('system', 'itemspage_network_mobile'));
85                 } else {
86                         $itemsPerPage = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
87                                 DI::config()->get('system', 'itemspage_network'));
88                 }
89
90                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), $itemsPerPage);
91
92                 if ($localSearch && empty($results)) {
93                         $pager->setItemsPerPage(80);
94                         $results = Search::getContactsFromLocalDirectory($search, $type, $pager->getStart(), $pager->getItemsPerPage());
95                 } elseif (strlen($config->get('system', 'directory')) && empty($results)) {
96                         $results = Search::getContactsFromGlobalDirectory($search, $type, $pager->getPage());
97                         $pager->setItemsPerPage($results->getItemsPage());
98                 }
99
100                 return self::printResult($results, $pager, $header);
101         }
102
103         /**
104          * Prints a human readable search result
105          *
106          * @param ResultList $results
107          * @param Pager      $pager
108          * @param string     $header
109          *
110          * @return string The result
111          * @throws HTTPException\InternalServerErrorException
112          * @throws \ImagickException
113          */
114         protected static function printResult(ResultList $results, Pager $pager, $header = '')
115         {
116                 if ($results->getTotal() == 0) {
117                         notice(DI::l10n()->t('No matches'));
118                         return '';
119                 }
120
121                 $entries = [];
122                 foreach ($results->getResults() as $result) {
123
124                         // in case the result is a contact result, add a contact-specific entry
125                         if ($result instanceof ContactResult) {
126                                 $contact = Model\Contact::getByURLForUser($result->getUrl(), local_user());
127                                 if (!empty($contact)) {
128                                         $entries[] = Contact::getContactTemplateVars($contact);
129                                 }
130                         }
131                 }
132
133                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
134                 return Renderer::replaceMacros($tpl, [
135                         'title'     => $header,
136                         '$contacts' => $entries,
137                         '$paginate' => $pager->renderFull($results->getTotal()),
138                 ]);
139         }
140 }