]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Bugfix - use magicLinks also for common/all friends and the directory
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @file src/Content/Widget.php
4  */
5 namespace Friendica\Content;
6
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\FileTag;
19 use Friendica\Model\GContact;
20 use Friendica\Model\Profile;
21 use Friendica\Util\Proxy as ProxyUtils;
22 use Friendica\Util\Strings;
23 use Friendica\Util\XML;
24
25 class Widget
26 {
27         /**
28          * Return the follow widget
29          *
30          * @param string $value optional, default empty
31          */
32         public static function follow($value = "")
33         {
34                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('follow.tpl'), array(
35                         '$connect' => L10n::t('Add New Contact'),
36                         '$desc' => L10n::t('Enter address or web location'),
37                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
38                         '$value' => $value,
39                         '$follow' => L10n::t('Connect')
40                 ));
41         }
42
43         /**
44          * Return Find People widget
45          */
46         public static function findPeople()
47         {
48                 $a = \get_app();
49                 $global_dir = Config::get('system', 'directory');
50
51                 if (Config::get('system', 'invitation_only')) {
52                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
53                         if ($x || is_site_admin()) {
54                                 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
55                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
56                                         . '</div>';
57                         }
58                 }
59
60                 $nv = [];
61                 $nv['findpeople'] = L10n::t('Find People');
62                 $nv['desc'] = L10n::t('Enter name or interest');
63                 $nv['label'] = L10n::t('Connect/Follow');
64                 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
65                 $nv['findthem'] = L10n::t('Find');
66                 $nv['suggest'] = L10n::t('Friend Suggestions');
67                 $nv['similar'] = L10n::t('Similar Interests');
68                 $nv['random'] = L10n::t('Random Profile');
69                 $nv['inv'] = L10n::t('Invite Friends');
70                 $nv['directory'] = L10n::t('Global Directory');
71                 $nv['global_dir'] = $global_dir;
72                 $nv['local_directory'] = L10n::t('Local Directory');
73
74                 $aside = [];
75                 $aside['$nv'] = $nv;
76
77                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('peoplefind.tpl'), $aside);
78         }
79
80         /**
81          * Return unavailable networks
82          */
83         public static function unavailableNetworks()
84         {
85                 // Always hide content from these networks
86                 $networks = ['face', 'apdn'];
87
88                 if (!Addon::isEnabled("statusnet")) {
89                         $networks[] = Protocol::STATUSNET;
90                 }
91
92                 if (!Addon::isEnabled("pumpio")) {
93                         $networks[] = Protocol::PUMPIO;
94                 }
95
96                 if (!Addon::isEnabled("twitter")) {
97                         $networks[] = Protocol::TWITTER;
98                 }
99
100                 if (Config::get("system", "ostatus_disabled")) {
101                         $networks[] = Protocol::OSTATUS;
102                 }
103
104                 if (!Config::get("system", "diaspora_enabled")) {
105                         $networks[] = Protocol::DIASPORA;
106                 }
107
108                 if (!Addon::isEnabled("pnut")) {
109                         $networks[] = Protocol::PNUT;
110                 }
111
112                 if (!sizeof($networks)) {
113                         return "";
114                 }
115
116                 $network_filter = implode("','", $networks);
117
118                 $network_filter = "AND `network` NOT IN ('$network_filter')";
119
120                 return $network_filter;
121         }
122
123         /**
124          * Return networks widget
125          *
126          * @param string $baseurl  baseurl
127          * @param string $selected optional, default empty
128          */
129         public static function networks($baseurl, $selected = '')
130         {
131                 if (!local_user()) {
132                         return '';
133                 }
134
135                 if (!Feature::isEnabled(local_user(), 'networks')) {
136                         return '';
137                 }
138
139                 $extra_sql = self::unavailableNetworks();
140
141                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
142                         local_user()
143                 );
144
145                 $nets = array();
146                 while ($rr = DBA::fetch($r)) {
147                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
148                 }
149                 DBA::close($r);
150
151                 if (count($nets) < 2) {
152                         return '';
153                 }
154
155                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
156                         '$title' => L10n::t('Protocols'),
157                         '$desc' => '',
158                         '$sel_all' => (($selected == '') ? 'selected' : ''),
159                         '$all' => L10n::t('All Protocols'),
160                         '$nets' => $nets,
161                         '$base' => $baseurl,
162                 ));
163         }
164
165         /**
166          * Return file as widget
167          *
168          * @param string $baseurl  baseurl
169          * @param string $selected optional, default empty
170          */
171         public static function fileAs($baseurl, $selected = '')
172         {
173                 if (!local_user()) {
174                         return '';
175                 }
176
177                 $saved = PConfig::get(local_user(), 'system', 'filetags');
178                 if (!strlen($saved)) {
179                         return;
180                 }
181
182                 $matches = false;
183                 $terms = array();
184                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
185                 if ($cnt) {
186                         foreach ($matches as $mtch)
187                         {
188                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
189                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
190                         }
191                 }
192
193                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
194                         '$title' => L10n::t('Saved Folders'),
195                         '$desc' => '',
196                         '$sel_all' => (($selected == '') ? 'selected' : ''),
197                         '$all' => L10n::t('Everything'),
198                         '$terms' => $terms,
199                         '$base' => $baseurl,
200                 ));
201         }
202
203         /**
204          * Return categories widget
205          *
206          * @param string $baseurl  baseurl
207          * @param string $selected optional, default empty
208          */
209         public static function categories($baseurl, $selected = '')
210         {
211                 $a = \get_app();
212
213                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
214                         return '';
215                 }
216
217                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
218                 if (!strlen($saved)) {
219                         return;
220                 }
221
222                 $matches = false;
223                 $terms = array();
224                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
225
226                 if ($cnt) {
227                         foreach ($matches as $mtch) {
228                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
229                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
230                         }
231                 }
232
233                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
234                         '$title' => L10n::t('Categories'),
235                         '$desc' => '',
236                         '$sel_all' => (($selected == '') ? 'selected' : ''),
237                         '$all' => L10n::t('Everything'),
238                         '$terms' => $terms,
239                         '$base' => $baseurl,
240                 ));
241         }
242
243         /**
244          * Return common friends visitor widget
245          *
246          * @param string $profile_uid uid
247          */
248         public static function commonFriendsVisitor($profile_uid)
249         {
250                 if (local_user() == $profile_uid) {
251                         return;
252                 }
253
254                 $cid = $zcid = 0;
255
256                 if (!empty($_SESSION['remote'])) {
257                         foreach ($_SESSION['remote'] as $visitor) {
258                                 if ($visitor['uid'] == $profile_uid) {
259                                         $cid = $visitor['cid'];
260                                         break;
261                                 }
262                         }
263                 }
264
265                 if (!$cid) {
266                         if (Profile::getMyURL()) {
267                                 $contact = DBA::selectFirst('contact', ['id'],
268                                                 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
269                                 if (DBA::isResult($contact)) {
270                                         $cid = $contact['id'];
271                                 } else {
272                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
273                                         if (DBA::isResult($gcontact)) {
274                                                 $zcid = $gcontact['id'];
275                                         }
276                                 }
277                         }
278                 }
279
280                 if ($cid == 0 && $zcid == 0) {
281                         return;
282                 }
283
284                 if ($cid) {
285                         $t = GContact::countCommonFriends($profile_uid, $cid);
286                 } else {
287                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
288                 }
289
290                 if (!$t) {
291                         return;
292                 }
293
294                 if ($cid) {
295                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
296                 } else {
297                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
298                 }
299
300                 if (!DBA::isResult($r)) {
301                         return;
302                 }
303
304                 $entries = [];
305                 foreach ($r as $rr) {
306                         $entry = [
307                                 'url'   => Contact::magicLink($rr['url']),
308                                 'name'  => $rr['name'],
309                                 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
310                         ];
311                         $entries[] = $entry;
312                 }
313
314                 $tpl = Renderer::getMarkupTemplate('remote_friends_common.tpl');
315                 return Renderer::replaceMacros($tpl, [
316                         '$desc'     => L10n::tt("%d contact in common", "%d contacts in common", $t),
317                         '$base'     => System::baseUrl(),
318                         '$uid'      => $profile_uid,
319                         '$cid'      => (($cid) ? $cid : '0'),
320                         '$linkmore' => (($t > 5) ? 'true' : ''),
321                         '$more'     => L10n::t('show more'),
322                         '$items'    => $entries
323                 ]);
324         }
325
326         /**
327          * Insert a tag cloud widget for the present profile.
328          *
329          * @brief Insert a tag cloud widget for the present profile.
330          * @param int     $limit Max number of displayed tags.
331          * @return string HTML formatted output.
332          */
333         public static function tagCloud($limit = 50)
334         {
335                 $a = \get_app();
336
337                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
338                         return '';
339                 }
340
341                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
342                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
343
344                         if (!$owner_id) {
345                                 return '';
346                         }
347                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
348                 }
349
350                 return '';
351         }
352 }