]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseSearch.php
Merge pull request #8227 from annando/daemon-checks
[friendica.git] / src / Module / BaseSearch.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\Renderer;
9 use Friendica\Core\Search;
10 use Friendica\DI;
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
17 /**
18  * Base class for search modules
19  */
20 class BaseSearch extends BaseModule
21 {
22         /**
23          * Performs a contact search with an optional prefix
24          *
25          * @param string $search Search query
26          * @param string $prefix A optional prefix (e.g. @ or !) for searching
27          *
28          * @return string
29          * @throws HTTPException\InternalServerErrorException
30          * @throws \ImagickException
31          */
32         public static function performContactSearch($search, $prefix = '')
33         {
34                 $a      = DI::app();
35                 $config = DI::config();
36
37                 $type = Search::TYPE_ALL;
38
39                 $localSearch = $config->get('system', 'poco_local_search');
40
41                 $search = $prefix . $search;
42
43                 if (!$search) {
44                         return '';
45                 }
46
47                 $header = '';
48
49                 if (strpos($search, '@') === 0) {
50                         $search  = substr($search, 1);
51                         $type    = Search::TYPE_PEOPLE;
52                         $header  = DI::l10n()->t('People Search - %s', $search);
53
54                         if (strrpos($search, '@') > 0) {
55                                 $results = Search::getContactsFromProbe($search);
56                         }
57                 }
58
59                 if (strpos($search, '!') === 0) {
60                         $search = substr($search, 1);
61                         $type   = Search::TYPE_FORUM;
62                         $header = DI::l10n()->t('Forum Search - %s', $search);
63                 }
64
65                 $args = DI::args();
66                 $pager = new Pager($args->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(DI::l10n()->t('No matches'));
94                         return '';
95                 }
96
97                 $id      = 0;
98                 $entries = [];
99                 foreach ($results->getResults() as $result) {
100
101                         // in case the result is a contact result, add a contact-specific entry
102                         if ($result instanceof ContactResult) {
103
104                                 $alt_text    = '';
105                                 $location    = '';
106                                 $about       = '';
107                                 $accountType = '';
108                                 $photo_menu  = [];
109
110                                 // If We already know this contact then don't show the "connect" button
111                                 if ($result->getCid() > 0 || $result->getPCid() > 0) {
112                                         $connLink = "";
113                                         $connTxt  = "";
114                                         $contact  = Model\Contact::getById(
115                                                 ($result->getCid() > 0) ? $result->getCid() : $result->getPCid()
116                                         );
117
118                                         if (!empty($contact)) {
119                                                 $photo_menu  = Model\Contact::photoMenu($contact);
120                                                 $details     = Contact::getContactTemplateVars($contact);
121                                                 $alt_text    = $details['alt_text'];
122                                                 $location    = $contact['location'];
123                                                 $about       = $contact['about'];
124                                                 $accountType = Model\Contact::getAccountType($contact);
125                                         } else {
126                                                 $photo_menu = [];
127                                         }
128                                 } else {
129                                         $connLink = DI::baseUrl()->get() . '/follow/?url=' . $result->getUrl();
130                                         $connTxt  = DI::l10n()->t('Connect');
131
132                                         $photo_menu['profile'] = [DI::l10n()->t("View Profile"), Model\Contact::magicLink($result->getUrl())];
133                                         $photo_menu['follow']  = [DI::l10n()->t("Connect/Follow"), $connLink];
134                                 }
135
136                                 $photo = str_replace("http:///photo/", Search::getGlobalDirectory() . "/photo/", $result->getPhoto());
137
138                                 $entry     = [
139                                         'alt_text'     => $alt_text,
140                                         'url'          => Model\Contact::magicLink($result->getUrl()),
141                                         'itemurl'      => $result->getItem(),
142                                         'name'         => $result->getName(),
143                                         'thumb'        => ProxyUtils::proxifyUrl($photo, false, ProxyUtils::SIZE_THUMB),
144                                         'img_hover'    => $result->getTags(),
145                                         'conntxt'      => $connTxt,
146                                         'connlnk'      => $connLink,
147                                         'photo_menu'   => $photo_menu,
148                                         'details'      => $location,
149                                         'tags'         => $result->getTags(),
150                                         'about'        => $about,
151                                         'account_type' => $accountType,
152                                         'network'      => ContactSelector::networkToName($result->getNetwork(), $result->getUrl()),
153                                         'id'           => ++$id,
154                                 ];
155                                 $entries[] = $entry;
156                         }
157                 }
158
159                 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
160                 return Renderer::replaceMacros($tpl, [
161                         'title'     => $header,
162                         '$contacts' => $entries,
163                         '$paginate' => $pager->renderFull($results->getTotal()),
164                 ]);
165         }
166 }