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