]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Merge pull request #8978 from annando/contact-update
[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          * Show a random selection of five common contacts between the visitor and the viewed profile user.
378          *
379          * @param int    $uid      Viewed profile user ID
380          * @param string $nickname Viewed profile user nickname
381          * @return string|void
382          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
383          * @throws \ImagickException
384          */
385         public static function commonFriendsVisitor(int $uid, string $nickname)
386         {
387                 if (local_user() == $uid) {
388                         return '';
389                 }
390
391                 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
392                 if (!$visitorPCid) {
393                         return '';
394                 }
395
396                 $localPCid = Contact::getPublicIdByUserId($uid);
397
398                 $condition = [
399                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
400                         $localPCid,
401                 ];
402
403                 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
404                 if (!$total) {
405                         return '';
406                 }
407
408                 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
409                 if (!DBA::isResult($commonContacts)) {
410                         return '';
411                 }
412
413                 $entries = [];
414                 foreach ($commonContacts as $contact) {
415                         $entries[] = [
416                                 'url'   => Contact::magicLink($contact['url']),
417                                 'name'  => $contact['name'],
418                                 'photo' => Contact::getThumb($contact),
419                         ];
420                 }
421
422                 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
423                 return Renderer::replaceMacros($tpl, [
424                         '$desc'     => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
425                         '$base'     => DI::baseUrl(),
426                         '$nickname' => $nickname,
427                         '$linkmore' => $total > 5 ? 'true' : '',
428                         '$more'     => DI::l10n()->t('show more'),
429                         '$contacts' => $entries
430                 ]);
431         }
432
433         /**
434          * Insert a tag cloud widget for the present profile.
435          *
436          * @param int $limit Max number of displayed tags.
437          * @return string HTML formatted output.
438          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
439          * @throws \ImagickException
440          */
441         public static function tagCloud($limit = 50)
442         {
443                 $a = DI::app();
444
445                 $uid = intval($a->profile['uid']);
446
447                 if (!$uid || !$a->profile['url']) {
448                         return '';
449                 }
450
451                 if (Feature::isEnabled($uid, 'tagadelic')) {
452                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, false);
453
454                         if (!$owner_id) {
455                                 return '';
456                         }
457                         return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
458                 }
459
460                 return '';
461         }
462
463         /**
464          * @param string $url Base page URL
465          * @param int    $uid User ID consulting/publishing posts
466          * @param bool   $wall True: Posted by User; False: Posted to User (network timeline)
467          * @return string
468          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
469          */
470         public static function postedByYear(string $url, int $uid, bool $wall)
471         {
472                 $o = '';
473
474                 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
475
476                 /* arrange the list in years */
477                 $dnow = DateTimeFormat::localNow('Y-m-d');
478
479                 $ret = [];
480
481                 $dthen = Item::firstPostDate($uid, $wall);
482                 if ($dthen) {
483                         // Set the start and end date to the beginning of the month
484                         $dnow = substr($dnow, 0, 8) . '01';
485                         $dthen = substr($dthen, 0, 8) . '01';
486
487                         /*
488                          * Starting with the current month, get the first and last days of every
489                          * month down to and including the month of the first post
490                          */
491                         while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
492                                 $dyear = intval(substr($dnow, 0, 4));
493                                 $dstart = substr($dnow, 0, 8) . '01';
494                                 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
495                                 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
496                                 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
497                                 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
498
499                                 if (empty($ret[$dyear])) {
500                                         $ret[$dyear] = [];
501                                 }
502
503                                 $ret[$dyear][] = [$str, $end_month, $start_month];
504                                 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
505                         }
506                 }
507
508                 if (!DBA::isResult($ret)) {
509                         return $o;
510                 }
511
512
513                 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
514                 $cutoff = array_key_exists($cutoff_year, $ret);
515
516                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
517                         '$title' => DI::l10n()->t('Archives'),
518                         '$size' => $visible_years,
519                         '$cutoff_year' => $cutoff_year,
520                         '$cutoff' => $cutoff,
521                         '$url' => $url,
522                         '$dates' => $ret,
523                         '$showmore' => DI::l10n()->t('show more')
524                 ]);
525
526                 return $o;
527         }
528 }