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