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