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