Use the Item class instead of DBA calls when possible
[friendica.git/.git] / mod / suggest.php
1 <?php
2 /**
3  * @file mod/suggest.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Widget;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GContact;
14 use Friendica\Util\Proxy as ProxyUtils;
15
16 function suggest_init(App $a) {
17         if (! local_user()) {
18                 return;
19         }
20
21         if (x($_GET,'ignore') && intval($_GET['ignore'])) {
22                 // Check if we should do HTML-based delete confirmation
23                 if ($_REQUEST['confirm']) {
24                         // <form> can't take arguments in its "action" parameter
25                         // so add any arguments as hidden inputs
26                         $query = explode_querystring($a->query_string);
27                         $inputs = [];
28                         foreach ($query['args'] as $arg) {
29                                 if (strpos($arg, 'confirm=') === false) {
30                                         $arg_parts = explode('=', $arg);
31                                         $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
32                                 }
33                         }
34
35                         $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), [
36                                 '$method' => 'get',
37                                 '$message' => L10n::t('Do you really want to delete this suggestion?'),
38                                 '$extra_inputs' => $inputs,
39                                 '$confirm' => L10n::t('Yes'),
40                                 '$confirm_url' => $query['base'],
41                                 '$confirm_name' => 'confirmed',
42                                 '$cancel' => L10n::t('Cancel'),
43                         ]);
44                         $a->error = 1; // Set $a->error so the other module functions don't execute
45                         return;
46                 }
47                 // Now check how the user responded to the confirmation query
48                 if (!$_REQUEST['canceled']) {
49                         DBA::insert('gcign', ['uid' => local_user(), 'gcid' => $_GET['ignore']]);
50                 }
51         }
52
53 }
54
55 function suggest_content(App $a)
56 {
57         $o = '';
58
59         if (! local_user()) {
60                 notice(L10n::t('Permission denied.') . EOL);
61                 return;
62         }
63
64         $_SESSION['return_url'] = System::baseUrl() . '/' . $a->cmd;
65
66         $a->page['aside'] .= Widget::findPeople();
67         $a->page['aside'] .= Widget::follow();
68
69
70         $r = GContact::suggestionQuery(local_user());
71
72         if (! DBA::isResult($r)) {
73                 $o .= L10n::t('No suggestions available. If this is a new site, please try again in 24 hours.');
74                 return $o;
75         }
76
77         $id = 0;
78
79         foreach ($r as $rr) {
80
81                 $connlnk = System::baseUrl() . '/follow/?url=' . (($rr['connect']) ? $rr['connect'] : $rr['url']);
82                 $ignlnk = System::baseUrl() . '/suggest?ignore=' . $rr['id'];
83                 $photo_menu = [
84                         'profile' => [L10n::t("View Profile"), Contact::magicLink($rr["url"])],
85                         'follow' => [L10n::t("Connect/Follow"), $connlnk],
86                         'hide' => [L10n::t('Ignore/Hide'), $ignlnk]
87                 ];
88
89                 $contact_details = Contact::getDetailsByURL($rr["url"], local_user(), $rr);
90
91                 $entry = [
92                         'url' => Contact::magicLink($rr['url']),
93                         'itemurl' => (($contact_details['addr'] != "") ? $contact_details['addr'] : $rr['url']),
94                         'img_hover' => $rr['url'],
95                         'name' => $contact_details['name'],
96                         'thumb' => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
97                         'details'       => $contact_details['location'],
98                         'tags'          => $contact_details['keywords'],
99                         'about'         => $contact_details['about'],
100                         'account_type'  => Contact::getAccountType($contact_details),
101                         'ignlnk' => $ignlnk,
102                         'ignid' => $rr['id'],
103                         'conntxt' => L10n::t('Connect'),
104                         'connlnk' => $connlnk,
105                         'photo_menu' => $photo_menu,
106                         'ignore' => L10n::t('Ignore/Hide'),
107                         'network' => ContactSelector::networkToName($rr['network'], $rr['url']),
108                         'id' => ++$id,
109                 ];
110                 $entries[] = $entry;
111         }
112
113         $tpl = get_markup_template('viewcontact_template.tpl');
114
115         $o .= replace_macros($tpl,[
116                 '$title' => L10n::t('Friend Suggestions'),
117                 '$contacts' => $entries,
118         ]);
119
120         return $o;
121 }