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