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