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