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