]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Update function calls
[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\System;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\FileTag;
18 use Friendica\Model\GContact;
19 use Friendica\Model\Profile;
20
21 require_once 'boot.php';
22 require_once 'include/dba.php';
23
24 class Widget
25 {
26         /**
27          * Return the follow widget
28          *
29          * @param string $value optional, default empty
30          */
31         public static function follow($value = "")
32         {
33                 return replace_macros(get_markup_template('follow.tpl'), array(
34                         '$connect' => L10n::t('Add New Contact'),
35                         '$desc' => L10n::t('Enter address or web location'),
36                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
37                         '$value' => $value,
38                         '$follow' => L10n::t('Connect')
39                 ));
40         }
41
42         /**
43          * Return Find People widget
44          */
45         public static function findPeople()
46         {
47                 $a = get_app();
48                 $global_dir = Config::get('system', 'directory');
49
50                 if (Config::get('system', 'invitation_only')) {
51                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
52                         if ($x || is_site_admin()) {
53                                 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
54                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
55                                         . '</div>';
56                         }
57                 }
58
59                 $nv = [];
60                 $nv['findpeople'] = L10n::t('Find People');
61                 $nv['desc'] = L10n::t('Enter name or interest');
62                 $nv['label'] = L10n::t('Connect/Follow');
63                 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
64                 $nv['findthem'] = L10n::t('Find');
65                 $nv['suggest'] = L10n::t('Friend Suggestions');
66                 $nv['similar'] = L10n::t('Similar Interests');
67                 $nv['random'] = L10n::t('Random Profile');
68                 $nv['inv'] = L10n::t('Invite Friends');
69                 $nv['directory'] = L10n::t('Global Directory');
70                 $nv['global_dir'] = $global_dir;
71                 $nv['local_directory'] = L10n::t('Local Directory');
72
73                 $aside = [];
74                 $aside['$nv'] = $nv;
75
76                 return replace_macros(get_markup_template('peoplefind.tpl'), $aside);
77         }
78
79         /**
80          * Return unavailable networks
81          */
82         public static function unavailableNetworks()
83         {
84                 // Always hide content from these networks
85                 $networks = ['face', 'apdn'];
86
87                 if (!Addon::isEnabled("statusnet")) {
88                         $networks[] = Protocol::STATUSNET;
89                 }
90
91                 if (!Addon::isEnabled("pumpio")) {
92                         $networks[] = Protocol::PUMPIO;
93                 }
94
95                 if (!Addon::isEnabled("twitter")) {
96                         $networks[] = Protocol::TWITTER;
97                 }
98
99                 if (Config::get("system", "ostatus_disabled")) {
100                         $networks[] = Protocol::OSTATUS;
101                 }
102
103                 if (!Config::get("system", "diaspora_enabled")) {
104                         $networks[] = Protocol::DIASPORA;
105                 }
106
107                 if (!Addon::isEnabled("pnut")) {
108                         $networks[] = Protocol::PNUT;
109                 }
110
111                 if (!sizeof($networks)) {
112                         return "";
113                 }
114
115                 $network_filter = implode("','", $networks);
116
117                 $network_filter = "AND `network` NOT IN ('$network_filter')";
118
119                 return $network_filter;
120         }
121
122         /**
123          * Return networks widget
124          *
125          * @param string $baseurl  baseurl
126          * @param string $selected optional, default empty
127          */
128         public static function networks($baseurl, $selected = '')
129         {
130                 if (!local_user()) {
131                         return '';
132                 }
133
134                 if (!Feature::isEnabled(local_user(), 'networks')) {
135                         return '';
136                 }
137
138                 $extra_sql = self::unavailableNetworks();
139
140                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
141                         local_user()
142                 );
143
144                 $nets = array();
145                 while ($rr = DBA::fetch($r)) {
146                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
147                 }
148                 DBA::close($r);
149
150                 if (count($nets) < 2) {
151                         return '';
152                 }
153
154                 return replace_macros(get_markup_template('nets.tpl'), array(
155                         '$title' => L10n::t('Networks'),
156                         '$desc' => '',
157                         '$sel_all' => (($selected == '') ? 'selected' : ''),
158                         '$all' => L10n::t('All Networks'),
159                         '$nets' => $nets,
160                         '$base' => $baseurl,
161                 ));
162         }
163
164         /**
165          * Return file as widget
166          *
167          * @param string $baseurl  baseurl
168          * @param string $selected optional, default empty
169          */
170         public static function fileAs($baseurl, $selected = '')
171         {
172                 if (!local_user()) {
173                         return '';
174                 }
175
176                 if (!Feature::isEnabled(local_user(), 'filing')) {
177                         return '';
178                 }
179
180                 $saved = PConfig::get(local_user(), 'system', 'filetags');
181                 if (!strlen($saved)) {
182                         return;
183                 }
184
185                 $matches = false;
186                 $terms = array();
187                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
188                 if ($cnt) {
189                         foreach ($matches as $mtch)
190                         {
191                                 $unescaped = xmlify(FileTag::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(FileTag::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 }