]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Move $pager and $page_offset out of App
[friendica.git] / mod / match.php
1 <?php
2 /**
3  * @file mod/match.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Pager;
8 use Friendica\Content\Widget;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Util\Network;
15 use Friendica\Util\Proxy as ProxyUtils;
16
17 require_once 'include/text.php';
18
19 /**
20  * @brief Controller for /match.
21  *
22  * It takes keywords from your profile and queries the directory server for
23  * matching keywords from other profiles.
24  *
25  * @param App $a App
26  *
27  * @return void|string
28  */
29 function match_content(App $a)
30 {
31         $o = '';
32         if (! local_user()) {
33                 return;
34         }
35
36         $a->page['aside'] .= Widget::findPeople();
37         $a->page['aside'] .= Widget::follow();
38
39         $_SESSION['return_path'] = $a->cmd;
40
41         $r = q(
42                 "SELECT `pub_keywords`, `prv_keywords` FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
43                 intval(local_user())
44         );
45         if (! DBA::isResult($r)) {
46                 return;
47         }
48         if (! $r[0]['pub_keywords'] && (! $r[0]['prv_keywords'])) {
49                 notice(L10n::t('No keywords to match. Please add keywords to your default profile.') . EOL);
50                 return;
51         }
52
53         $params = [];
54         $tags = trim($r[0]['pub_keywords'] . ' ' . $r[0]['prv_keywords']);
55
56         if ($tags) {
57                 $params['s'] = $tags;
58                 if ($pager->getPage() != 1) {
59                         $params['p'] = $pager->getPage();
60                 }
61
62                 if (strlen(Config::get('system', 'directory'))) {
63                         $x = Network::post(get_server().'/msearch', $params)->getBody();
64                 } else {
65                         $x = Network::post(System::baseUrl() . '/msearch', $params)->getBody();
66                 }
67
68                 $j = json_decode($x);
69
70                 if (count($j->results)) {
71                         $pager = new Pager($a->query_string, $j->total, $j->items_page);
72
73                         $id = 0;
74
75                         foreach ($j->results as $jj) {
76                                 $match_nurl = normalise_link($jj->url);
77                                 $match = q(
78                                         "SELECT `nurl` FROM `contact` WHERE `uid` = '%d' AND nurl='%s' LIMIT 1",
79                                         intval(local_user()),
80                                         DBA::escape($match_nurl)
81                                 );
82
83                                 if (!count($match)) {
84                                         $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
85                                         $connlnk = System::baseUrl() . '/follow/?url=' . $jj->url;
86                                         $photo_menu = [
87                                                 'profile' => [L10n::t("View Profile"), Contact::magicLink($jj->url)],
88                                                 'follow' => [L10n::t("Connect/Follow"), $connlnk]
89                                         ];
90
91                                         $contact_details = Contact::getDetailsByURL($jj->url, local_user());
92
93                                         $entry = [
94                                                 'url' => Contact::magicLink($jj->url),
95                                                 'itemurl' => defaults($contact_details, 'addr', $jj->url),
96                                                 'name' => $jj->name,
97                                                 'details'       => defaults($contact_details, 'location', ''),
98                                                 'tags'          => defaults($contact_details, 'keywords', ''),
99                                                 'about'         => defaults($contact_details, 'about', ''),
100                                                 'account_type'  => Contact::getAccountType($contact_details),
101                                                 'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
102                                                 'inttxt' => ' ' . L10n::t('is interested in:'),
103                                                 'conntxt' => L10n::t('Connect'),
104                                                 'connlnk' => $connlnk,
105                                                 'img_hover' => $jj->tags,
106                                                 'photo_menu' => $photo_menu,
107                                                 'id' => ++$id,
108                                         ];
109                                         $entries[] = $entry;
110                                 }
111                         }
112
113                         $tpl = get_markup_template('viewcontact_template.tpl');
114
115                         $o .= replace_macros(
116                                 $tpl,
117                                 [
118                                 '$title' => L10n::t('Profile Match'),
119                                 '$contacts' => $entries,
120                                 '$paginate' => $pager->renderFull()]
121                         );
122                 } else {
123                         info(L10n::t('No matches') . EOL);
124                 }
125         }
126
127         return $o;
128 }