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