]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
876fbc79e489eafa8f0bd91d7e1e9918c7e1bff0
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
24 use Friendica\Core\Addon;
25 use Friendica\Core\Cache\Enum\Duration;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Search;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Circle;
33 use Friendica\Model\Item;
34 use Friendica\Model\Post;
35 use Friendica\Model\Profile;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\Temporal;
38
39 class Widget
40 {
41         /**
42          * Return the follow widget
43          *
44          * @param string $value optional, default empty
45          * @return string
46          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
47          */
48         public static function follow(string $value = ''): string
49         {
50                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
51                         '$connect' => DI::l10n()->t('Add New Contact'),
52                         '$desc' => DI::l10n()->t('Enter address or web location'),
53                         '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
54                         '$value' => $value,
55                         '$follow' => DI::l10n()->t('Connect')
56                 ));
57         }
58
59         /**
60          * Return Find People widget
61          *
62          * @return string HTML code representing "People Widget"
63          */
64         public static function findPeople(): string
65         {
66                 $global_dir = Search::getGlobalDirectory();
67
68                 if (DI::config()->get('system', 'invitation_only')) {
69                         $x = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'invites_remaining'));
70                         if ($x || DI::app()->isSiteAdmin()) {
71                                 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
72                                         . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
73                                         . '</div>';
74                         }
75                 }
76
77                 $nv = [];
78                 $nv['findpeople'] = DI::l10n()->t('Find People');
79                 $nv['desc'] = DI::l10n()->t('Enter name or interest');
80                 $nv['label'] = DI::l10n()->t('Connect/Follow');
81                 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
82                 $nv['findthem'] = DI::l10n()->t('Find');
83                 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
84                 $nv['similar'] = DI::l10n()->t('Similar Interests');
85                 $nv['random'] = DI::l10n()->t('Random Profile');
86                 $nv['inv'] = DI::l10n()->t('Invite Friends');
87                 $nv['directory'] = DI::l10n()->t('Global Directory');
88                 $nv['global_dir'] = Profile::zrl($global_dir, true);
89                 $nv['local_directory'] = DI::l10n()->t('Local Directory');
90
91                 $aside = [];
92                 $aside['$nv'] = $nv;
93
94                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
95         }
96
97         /**
98          * Return unavailable networks as array
99          *
100          * @return array Unsupported networks
101          */
102         public static function unavailableNetworks(): array
103         {
104                 // Always hide content from these networks
105                 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::TWITTER, Protocol::ZOT];
106
107                 if (!Addon::isEnabled("discourse")) {
108                         $networks[] = Protocol::DISCOURSE;
109                 }
110
111                 if (!Addon::isEnabled("statusnet")) {
112                         $networks[] = Protocol::STATUSNET;
113                 }
114
115                 if (!Addon::isEnabled("pumpio")) {
116                         $networks[] = Protocol::PUMPIO;
117                 }
118
119                 if (!Addon::isEnabled("tumblr")) {
120                         $networks[] = Protocol::TUMBLR;
121                 }
122
123                 if (DI::config()->get("system", "ostatus_disabled")) {
124                         $networks[] = Protocol::OSTATUS;
125                 }
126
127                 if (!DI::config()->get("system", "diaspora_enabled")) {
128                         $networks[] = Protocol::DIASPORA;
129                 }
130
131                 if (!Addon::isEnabled("pnut")) {
132                         $networks[] = Protocol::PNUT;
133                 }
134                 return $networks;
135         }
136
137         /**
138          * Display a generic filter widget based on a list of options
139          *
140          * The options array must be the following format:
141          * [
142          *    [
143          *      'ref' => {filter value},
144          *      'name' => {option name}
145          *    ],
146          *    ...
147          * ]
148          *
149          * @param string $type The filter query string key
150          * @param string $title
151          * @param string $desc
152          * @param string $all The no filter label
153          * @param string $baseUrl The full page request URI
154          * @param array  $options
155          * @param string $selected The currently selected filter option value
156          * @return string
157          * @throws \Exception
158          */
159         private static function filter(string $type, string $title, string $desc, string $all, string $baseUrl, array $options, string $selected = null): string
160         {
161                 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
162                 $queryArray = [];
163
164                 if ($queryString) {
165                         parse_str($queryString, $queryArray);
166                         unset($queryArray[$type]);
167
168                         if (count($queryArray)) {
169                                 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
170                         } else {
171                                 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
172                         }
173                 } else {
174                         $baseUrl = trim($baseUrl, '?') . '?';
175                 }
176
177                 array_walk($options, function (&$value) {
178                         $value['ref'] = rawurlencode($value['ref']);
179                 });
180
181                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
182                         '$type'      => $type,
183                         '$title'     => $title,
184                         '$desc'      => $desc,
185                         '$selected'  => $selected,
186                         '$all_label' => $all,
187                         '$options'   => $options,
188                         '$base'      => $baseUrl,
189                 ]);
190         }
191
192         /**
193          * Return circle membership widget
194          *
195          * @param string $baseurl
196          * @param string $selected
197          * @return string
198          * @throws \Exception
199          */
200         public static function circles(string $baseurl, string $selected = ''): string
201         {
202                 if (!DI::userSession()->getLocalUserId()) {
203                         return '';
204                 }
205
206                 $options = array_map(function ($circle) {
207                         return [
208                                 'ref'  => $circle['id'],
209                                 'name' => $circle['name']
210                         ];
211                 }, Circle::getByUserId(DI::userSession()->getLocalUserId()));
212
213                 return self::filter(
214                         'circle',
215                         DI::l10n()->t('Circles'),
216                         '',
217                         DI::l10n()->t('Everyone'),
218                         $baseurl,
219                         $options,
220                         $selected
221                 );
222         }
223
224         /**
225          * Return contact relationship widget
226          *
227          * @param string $baseurl  baseurl
228          * @param string $selected optional, default empty
229          * @return string
230          * @throws \Exception
231          */
232         public static function contactRels(string $baseurl, string $selected = ''): string
233         {
234                 if (!DI::userSession()->getLocalUserId()) {
235                         return '';
236                 }
237
238                 $options = [
239                         ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
240                         ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
241                         ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
242                         ['ref' => 'nothing', 'name' => DI::l10n()->t('No relationship')],
243                 ];
244
245                 return self::filter(
246                         'rel',
247                         DI::l10n()->t('Relationships'),
248                         '',
249                         DI::l10n()->t('All Contacts'),
250                         $baseurl,
251                         $options,
252                         $selected
253                 );
254         }
255
256         /**
257          * Return networks widget
258          *
259          * @param string $baseurl  baseurl
260          * @param string $selected optional, default empty
261          * @return string
262          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
263          */
264         public static function networks(string $baseurl, string $selected = ''): string
265         {
266                 if (!DI::userSession()->getLocalUserId()) {
267                         return '';
268                 }
269
270                 $networks = self::unavailableNetworks();
271                 $query = "`uid` = ? AND NOT `deleted` AND `network` != '' AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")";
272                 $condition = array_merge([$query], array_merge([DI::userSession()->getLocalUserId()], $networks));
273
274                 $r = DBA::select('contact', ['network'], $condition, ['group_by' => ['network'], 'order' => ['network']]);
275
276                 $nets = [];
277                 while ($rr = DBA::fetch($r)) {
278                         $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
279                 }
280                 DBA::close($r);
281
282                 if (count($nets) < 2) {
283                         return '';
284                 }
285
286                 return self::filter(
287                         'nets',
288                         DI::l10n()->t('Protocols'),
289                         '',
290                         DI::l10n()->t('All Protocols'),
291                         $baseurl,
292                         $nets,
293                         $selected
294                 );
295         }
296
297         /**
298          * Return file as widget
299          *
300          * @param string $baseurl  baseurl
301          * @param string $selected optional, default empty
302          * @return string
303          * @throws \Exception
304          */
305         public static function fileAs(string $baseurl, string $selected = ''): string
306         {
307                 if (!DI::userSession()->getLocalUserId()) {
308                         return '';
309                 }
310
311                 $terms = [];
312                 foreach (Post\Category::getArray(DI::userSession()->getLocalUserId(), Post\Category::FILE) as $savedFolderName) {
313                         $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
314                 }
315
316                 return self::filter(
317                         'file',
318                         DI::l10n()->t('Saved Folders'),
319                         '',
320                         DI::l10n()->t('Everything'),
321                         $baseurl,
322                         $terms,
323                         $selected
324                 );
325         }
326
327         /**
328          * Return categories widget
329          *
330          * @param int    $uid      Id of the user owning the categories
331          * @param string $baseurl  Base page URL
332          * @param string $selected Selected category
333          * @return string
334          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
335          */
336         public static function categories(int $uid, string $baseurl, string $selected = ''): string
337         {
338                 if (!Feature::isEnabled($uid, 'categories')) {
339                         return '';
340                 }
341
342                 $terms = [];
343                 foreach (Post\Category::getArray($uid, Post\Category::CATEGORY) as $savedFolderName) {
344                         $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
345                 }
346
347                 return self::filter(
348                         'category',
349                         DI::l10n()->t('Categories'),
350                         '',
351                         DI::l10n()->t('Everything'),
352                         $baseurl,
353                         $terms,
354                         $selected
355                 );
356         }
357
358         /**
359          * Show a random selection of five common contacts between the visitor and the viewed profile user.
360          *
361          * @param int    $uid      Viewed profile user ID
362          * @param string $nickname Viewed profile user nickname
363          * @return string
364          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
365          * @throws \ImagickException
366          */
367         public static function commonFriendsVisitor(int $uid, string $nickname): string
368         {
369                 if (DI::userSession()->getLocalUserId() == $uid) {
370                         return '';
371                 }
372
373                 $visitorPCid = DI::userSession()->getPublicContactId() ?: DI::userSession()->getRemoteUserId();
374                 if (!$visitorPCid) {
375                         return '';
376                 }
377
378                 $localPCid = Contact::getPublicIdByUserId($uid);
379
380                 $condition = [
381                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
382                         $localPCid,
383                 ];
384
385                 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
386                 if (!$total) {
387                         return '';
388                 }
389
390                 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
391                 if (!DBA::isResult($commonContacts)) {
392                         return '';
393                 }
394
395                 $entries = [];
396                 foreach ($commonContacts as $contact) {
397                         $entries[] = [
398                                 'url'   => Contact::magicLinkByContact($contact),
399                                 'name'  => $contact['name'],
400                                 'photo' => Contact::getThumb($contact),
401                         ];
402                 }
403
404                 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
405                 return Renderer::replaceMacros($tpl, [
406                         '$desc'     => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
407                         '$base'     => DI::baseUrl(),
408                         '$nickname' => $nickname,
409                         '$linkmore' => $total > 5 ? 'true' : '',
410                         '$more'     => DI::l10n()->t('show more'),
411                         '$contacts' => $entries
412                 ]);
413         }
414
415         /**
416          * Insert a tag cloud widget for the present profile.
417          *
418          * @param int $uid   User ID
419          * @param int $limit Max number of displayed tags.
420          * @return string HTML formatted output.
421          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
422          * @throws \ImagickException
423          */
424         public static function tagCloud(int $uid, int $limit = 50): string
425         {
426                 if (empty($uid)) {
427                         return '';
428                 }
429
430                 if (Feature::isEnabled($uid, 'tagadelic')) {
431                         $owner_id = Contact::getPublicIdByUserId($uid);
432
433                         if (!$owner_id) {
434                                 return '';
435                         }
436                         return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
437                 }
438
439                 return '';
440         }
441
442         /**
443          * @param string $url Base page URL
444          * @param int    $uid User ID consulting/publishing posts
445          * @param bool   $wall True: Posted by User; False: Posted to User (network timeline)
446          * @return string
447          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
448          */
449         public static function postedByYear(string $url, int $uid, bool $wall): string
450         {
451                 $o = '';
452
453                 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
454
455                 /* arrange the list in years */
456                 $dnow = DateTimeFormat::localNow('Y-m-d');
457
458                 $ret = [];
459
460                 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
461                 $dthen = DI::cache()->get($cachekey);
462                 if (empty($dthen)) {
463                         $dthen = Item::firstPostDate($uid, $wall);
464                         DI::cache()->set($cachekey, $dthen, Duration::HOUR);
465                 }
466
467                 if ($dthen) {
468                         // Set the start and end date to the beginning of the month
469                         $cutoffday = $dthen;
470                         $thisday = substr($dnow, 4);
471                         $nextday = date('Y-m-d', strtotime($dnow . ' + 1 day'));
472                         $nextday = substr($nextday, 4);
473                         $dnow = substr($dnow, 0, 8) . '01';
474                         $dthen = substr($dthen, 0, 8) . '01';
475
476                         /*
477                          * Starting with the current month, get the first and last days of every
478                          * month down to and including the month of the first post
479                          */
480                         while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
481                                 $dyear = intval(substr($dnow, 0, 4));
482                                 $dstart = substr($dnow, 0, 8) . '01';
483                                 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
484                                 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
485                                 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
486                                 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
487
488                                 if (empty($ret[$dyear])) {
489                                         $ret[$dyear] = [];
490                                 }
491
492                                 $ret[$dyear][] = [$str, $end_month, $start_month];
493                                 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
494                         }
495                 }
496
497                 if (!DBA::isResult($ret)) {
498                         return $o;
499                 }
500
501
502                 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
503                 $cutoff = array_key_exists($cutoff_year, $ret);
504
505                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'), [
506                         '$title' => DI::l10n()->t('Archives'),
507                         '$size' => $visible_years,
508                         '$cutoff_year' => $cutoff_year,
509                         '$cutoff' => $cutoff,
510                         '$url' => $url,
511                         '$dates' => $ret,
512                         '$showless' => DI::l10n()->t('show less'),
513                         '$showmore' => DI::l10n()->t('show more'),
514                         '$onthisdate' => DI::l10n()->t('On this date'),
515                         '$thisday' => $thisday,
516                         '$nextday' => $nextday,
517                         '$cutoffday' => $cutoffday
518                 ]);
519
520                 return $o;
521         }
522
523         /**
524          * Display the account types sidebar
525          * The account type value is added as a parameter to the url
526          *
527          * @param string $base        Basepath
528          * @param string $accounttype Account type
529          * @return string
530          */
531         public static function accountTypes(string $base, string $accounttype): string
532         {
533                 $accounts = [
534                         ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
535                         ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
536                         ['ref' => 'news', 'name' => DI::l10n()->t('News')],
537                         ['ref' => 'community', 'name' => DI::l10n()->t('Groups')],
538                 ];
539
540                 return self::filter(
541                         'accounttype',
542                         DI::l10n()->t('Account Types'),
543                         '',
544                         DI::l10n()->t('All'),
545                         $base,
546                         $accounts,
547                         $accounttype
548                 );
549         }
550
551         /**
552          * Get a list of all channels
553          *
554          * @param string $base
555          * @param string $channelname
556          * @param integer $uid
557          * @return string
558          */
559         public static function channels(string $base, string $channelname, int $uid): string
560         {
561                 $channels = [];
562
563                 $enabled = DI::pConfig()->get($uid, 'system', 'enabled_timelines', []);
564
565                 foreach (DI::NetworkFactory()->getTimelines('') as $channel) {
566                         if (empty($enabled) || in_array($channel->code, $enabled)) {
567                                 $channels[] = ['ref' => $channel->code, 'name' => $channel->label];
568                         }
569                 }
570
571                 foreach (DI::ChannelFactory()->getTimelines($uid) as $channel) {
572                         if (empty($enabled) || in_array($channel->code, $enabled)) {
573                                 $channels[] = ['ref' => $channel->code, 'name' => $channel->label];
574                         }
575                 }
576
577                 foreach (DI::UserDefinedChannelFactory()->getForUser($uid) as $channel) {
578                         if (empty($enabled) || in_array($channel->code, $enabled)) {
579                                 $channels[] = ['ref' => $channel->code, 'name' => $channel->label];
580                         }
581                 }
582
583                 foreach (DI::CommunityFactory()->getTimelines(true) as $community) {
584                         if (empty($enabled) || in_array($community->code, $enabled)) {
585                                 $channels[] = ['ref' => $community->code, 'name' => $community->label];
586                         }
587                 }
588
589                 return self::filter(
590                         'channel',
591                         DI::l10n()->t('Channels'),
592                         '',
593                         '',
594                         $base,
595                         $channels,
596                         $channelname
597                 );
598         }
599 }