]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
3c3af4f6ac37f225422fae4067bf7d57184f102b
[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\Proxy as ProxyUtils;
22 use Friendica\Util\Strings;
23 use Friendica\Util\XML;
24
25 class Widget
26 {
27         /**
28          * Return the follow widget
29          *
30          * @param string $value optional, default empty
31          * @return string
32          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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          * @return string
131          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
132          */
133         public static function networks($baseurl, $selected = '')
134         {
135                 if (!local_user()) {
136                         return '';
137                 }
138
139                 if (!Feature::isEnabled(local_user(), 'networks')) {
140                         return '';
141                 }
142
143                 $extra_sql = self::unavailableNetworks();
144
145                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
146                         local_user()
147                 );
148
149                 $nets = array();
150                 while ($rr = DBA::fetch($r)) {
151                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
152                 }
153                 DBA::close($r);
154
155                 if (count($nets) < 2) {
156                         return '';
157                 }
158
159                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
160                         '$title' => L10n::t('Protocols'),
161                         '$desc' => '',
162                         '$sel_all' => (($selected == '') ? 'selected' : ''),
163                         '$all' => L10n::t('All Protocols'),
164                         '$nets' => $nets,
165                         '$base' => $baseurl,
166                 ));
167         }
168
169         /**
170          * Return file as widget
171          *
172          * @param string $baseurl  baseurl
173          * @param string $selected optional, default empty
174          * @return string|void
175          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
176          */
177         public static function fileAs($baseurl, $selected = '')
178         {
179                 if (!local_user()) {
180                         return '';
181                 }
182
183                 $saved = PConfig::get(local_user(), 'system', 'filetags');
184                 if (!strlen($saved)) {
185                         return;
186                 }
187
188                 $matches = false;
189                 $terms = array();
190                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
191                 if ($cnt) {
192                         foreach ($matches as $mtch)
193                         {
194                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
195                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
196                         }
197                 }
198
199                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
200                         '$title' => L10n::t('Saved Folders'),
201                         '$desc' => '',
202                         '$sel_all' => (($selected == '') ? 'selected' : ''),
203                         '$all' => L10n::t('Everything'),
204                         '$terms' => $terms,
205                         '$base' => $baseurl,
206                 ));
207         }
208
209         /**
210          * Return categories widget
211          *
212          * @param string $baseurl  baseurl
213          * @param string $selected optional, default empty
214          * @return string|void
215          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
216          */
217         public static function categories($baseurl, $selected = '')
218         {
219                 $a = \get_app();
220
221                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
222                         return '';
223                 }
224
225                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
226                 if (!strlen($saved)) {
227                         return;
228                 }
229
230                 $matches = false;
231                 $terms = array();
232                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
233
234                 if ($cnt) {
235                         foreach ($matches as $mtch) {
236                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
237                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
238                         }
239                 }
240
241                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
242                         '$title' => L10n::t('Categories'),
243                         '$desc' => '',
244                         '$sel_all' => (($selected == '') ? 'selected' : ''),
245                         '$all' => L10n::t('Everything'),
246                         '$terms' => $terms,
247                         '$base' => $baseurl,
248                 ));
249         }
250
251         /**
252          * Return common friends visitor widget
253          *
254          * @param string $profile_uid uid
255          * @return string|void
256          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
257          */
258         public static function commonFriendsVisitor($profile_uid)
259         {
260                 if (local_user() == $profile_uid) {
261                         return;
262                 }
263
264                 $cid = $zcid = 0;
265
266                 if (!empty($_SESSION['remote'])) {
267                         foreach ($_SESSION['remote'] as $visitor) {
268                                 if ($visitor['uid'] == $profile_uid) {
269                                         $cid = $visitor['cid'];
270                                         break;
271                                 }
272                         }
273                 }
274
275                 if (!$cid) {
276                         if (Profile::getMyURL()) {
277                                 $contact = DBA::selectFirst('contact', ['id'],
278                                                 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
279                                 if (DBA::isResult($contact)) {
280                                         $cid = $contact['id'];
281                                 } else {
282                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
283                                         if (DBA::isResult($gcontact)) {
284                                                 $zcid = $gcontact['id'];
285                                         }
286                                 }
287                         }
288                 }
289
290                 if ($cid == 0 && $zcid == 0) {
291                         return;
292                 }
293
294                 if ($cid) {
295                         $t = GContact::countCommonFriends($profile_uid, $cid);
296                 } else {
297                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
298                 }
299
300                 if (!$t) {
301                         return;
302                 }
303
304                 if ($cid) {
305                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
306                 } else {
307                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
308                 }
309
310                 if (!DBA::isResult($r)) {
311                         return;
312                 }
313
314                 $entries = [];
315                 foreach ($r as $rr) {
316                         $entry = [
317                                 'url'   => Contact::magicLink($rr['url']),
318                                 'name'  => $rr['name'],
319                                 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
320                         ];
321                         $entries[] = $entry;
322                 }
323
324                 $tpl = Renderer::getMarkupTemplate('remote_friends_common.tpl');
325                 return Renderer::replaceMacros($tpl, [
326                         '$desc'     => L10n::tt("%d contact in common", "%d contacts in common", $t),
327                         '$base'     => System::baseUrl(),
328                         '$uid'      => $profile_uid,
329                         '$cid'      => (($cid) ? $cid : '0'),
330                         '$linkmore' => (($t > 5) ? 'true' : ''),
331                         '$more'     => L10n::t('show more'),
332                         '$items'    => $entries
333                 ]);
334         }
335
336         /**
337          * Insert a tag cloud widget for the present profile.
338          *
339          * @brief Insert a tag cloud widget for the present profile.
340          * @param int $limit Max number of displayed tags.
341          * @return string HTML formatted output.
342          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
343          * @throws \ImagickException
344          */
345         public static function tagCloud($limit = 50)
346         {
347                 $a = \get_app();
348
349                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
350                         return '';
351                 }
352
353                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
354                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
355
356                         if (!$owner_id) {
357                                 return '';
358                         }
359                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
360                 }
361
362                 return '';
363         }
364 }