]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
2f78d0fd3d387dd52f6e4830f2ed2f41c9c8b240
[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
22 require_once 'boot.php';
23 require_once 'include/dba.php';
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 `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('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                         {
192                                 $unescaped = xmlify(FileTag::decode($mtch[1]));
193                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
194                         }
195                 }
196
197                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
198                         '$title' => L10n::t('Saved Folders'),
199                         '$desc' => '',
200                         '$sel_all' => (($selected == '') ? 'selected' : ''),
201                         '$all' => L10n::t('Everything'),
202                         '$terms' => $terms,
203                         '$base' => $baseurl,
204                 ));
205         }
206
207         /**
208          * Return categories widget
209          *
210          * @param string $baseurl  baseurl
211          * @param string $selected optional, default empty
212          */
213         public static function categories($baseurl, $selected = '')
214         {
215                 $a = get_app();
216
217                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
218                         return '';
219                 }
220
221                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
222                 if (!strlen($saved)) {
223                         return;
224                 }
225
226                 $matches = false;
227                 $terms = array();
228                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
229
230                 if ($cnt) {
231                         foreach ($matches as $mtch) {
232                                 $unescaped = xmlify(FileTag::decode($mtch[1]));
233                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
234                         }
235                 }
236
237                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
238                         '$title' => L10n::t('Categories'),
239                         '$desc' => '',
240                         '$sel_all' => (($selected == '') ? 'selected' : ''),
241                         '$all' => L10n::t('Everything'),
242                         '$terms' => $terms,
243                         '$base' => $baseurl,
244                 ));
245         }
246
247         /**
248          * Return common friends visitor widget
249          *
250          * @param string $profile_uid uid
251          */
252         public static function commonFriendsVisitor($profile_uid)
253         {
254                 if (local_user() == $profile_uid) {
255                         return;
256                 }
257
258                 $cid = $zcid = 0;
259
260                 if (!empty($_SESSION['remote'])) {
261                         foreach ($_SESSION['remote'] as $visitor) {
262                                 if ($visitor['uid'] == $profile_uid) {
263                                         $cid = $visitor['cid'];
264                                         break;
265                                 }
266                         }
267                 }
268
269                 if (!$cid) {
270                         if (Profile::getMyURL()) {
271                                 $contact = DBA::selectFirst('contact', ['id'],
272                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
273                                 if (DBA::isResult($contact)) {
274                                         $cid = $contact['id'];
275                                 } else {
276                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
277                                         if (DBA::isResult($gcontact)) {
278                                                 $zcid = $gcontact['id'];
279                                         }
280                                 }
281                         }
282                 }
283
284                 if ($cid == 0 && $zcid == 0) {
285                         return;
286                 }
287
288                 if ($cid) {
289                         $t = GContact::countCommonFriends($profile_uid, $cid);
290                 } else {
291                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
292                 }
293
294                 if (!$t) {
295                         return;
296                 }
297
298                 if ($cid) {
299                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
300                 } else {
301                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
302                 }
303
304                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('remote_friends_common.tpl'), array(
305                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
306                         '$base' => System::baseUrl(),
307                         '$uid' => $profile_uid,
308                         '$cid' => (($cid) ? $cid : '0'),
309                         '$linkmore' => (($t > 5) ? 'true' : ''),
310                         '$more' => L10n::t('show more'),
311                         '$items' => $r)
312                 );
313         }
314
315         /**
316          * Insert a tag cloud widget for the present profile.
317          *
318          * @brief Insert a tag cloud widget for the present profile.
319          * @param int     $limit Max number of displayed tags.
320          * @return string HTML formatted output.
321          */
322         public static function tagCloud($limit = 50)
323         {
324                 $a = get_app();
325
326                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
327                         return '';
328                 }
329
330                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
331                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
332
333                         if (!$owner_id) {
334                                 return '';
335                         }
336                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
337                 }
338
339                 return '';
340         }
341 }