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