3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Content;
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;
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;
42 * Return the follow widget
44 * @param string $value optional, default empty
46 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
48 public static function follow($value = "")
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'),
55 '$follow' => DI::l10n()->t('Connect')
60 * Return Find People widget
62 public static function findPeople()
64 $global_dir = Search::getGlobalDirectory();
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)
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');
92 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
96 * Return unavailable networks as array
98 * @return array Unsupported networks
100 public static function unavailableNetworks()
102 // Always hide content from these networks
103 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::ZOT];
105 if (!Addon::isEnabled("discourse")) {
106 $networks[] = Protocol::DISCOURSE;
109 if (!Addon::isEnabled("statusnet")) {
110 $networks[] = Protocol::STATUSNET;
113 if (!Addon::isEnabled("pumpio")) {
114 $networks[] = Protocol::PUMPIO;
117 if (!Addon::isEnabled("twitter")) {
118 $networks[] = Protocol::TWITTER;
121 if (DI::config()->get("system", "ostatus_disabled")) {
122 $networks[] = Protocol::OSTATUS;
125 if (!DI::config()->get("system", "diaspora_enabled")) {
126 $networks[] = Protocol::DIASPORA;
129 if (!Addon::isEnabled("pnut")) {
130 $networks[] = Protocol::PNUT;
136 * Display a generic filter widget based on a list of options
138 * The options array must be the following format:
141 * 'ref' => {filter value},
142 * 'name' => {option name}
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
157 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
159 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
163 parse_str($queryString, $queryArray);
164 unset($queryArray[$type]);
166 if (count($queryArray)) {
167 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
169 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
172 $baseUrl = trim($baseUrl, '?') . '?';
175 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
179 '$selected' => $selected,
180 '$all_label' => $all,
181 '$options' => $options,
187 * Return group membership widget
189 * @param string $baseurl
190 * @param string $selected
194 public static function groups($baseurl, $selected = '')
200 $options = array_map(function ($group) {
202 'ref' => $group['id'],
203 'name' => $group['name']
205 }, Group::getByUserId(local_user()));
209 DI::l10n()->t('Groups'),
211 DI::l10n()->t('Everyone'),
219 * Return contact relationship widget
221 * @param string $baseurl baseurl
222 * @param string $selected optional, default empty
226 public static function contactRels($baseurl, $selected = '')
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')],
240 DI::l10n()->t('Relationships'),
242 DI::l10n()->t('All Contacts'),
250 * Return networks widget
252 * @param string $baseurl baseurl
253 * @param string $selected optional, default empty
255 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
257 public static function networks($baseurl, $selected = '')
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));
267 $r = DBA::select('contact', ['network'], $condition, ['group_by' => ['network'], 'order' => ['network']]);
270 while ($rr = DBA::fetch($r)) {
271 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
275 if (count($nets) < 2) {
281 DI::l10n()->t('Protocols'),
283 DI::l10n()->t('All Protocols'),
291 * Return file as widget
293 * @param string $baseurl baseurl
294 * @param string $selected optional, default empty
295 * @return string|void
298 public static function fileAs($baseurl, $selected = '')
305 foreach (Post\Category::getArray(local_user(), Post\Category::FILE) as $savedFolderName) {
306 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
311 DI::l10n()->t('Saved Folders'),
313 DI::l10n()->t('Everything'),
321 * Return categories widget
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
329 public static function categories(int $uid, string $baseurl, string $selected = '')
331 if (!Feature::isEnabled($uid, 'categories')) {
336 foreach (Post\Category::getArray($uid, Post\Category::CATEGORY) as $savedFolderName) {
337 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
342 DI::l10n()->t('Categories'),
344 DI::l10n()->t('Everything'),
352 * Show a random selection of five common contacts between the visitor and the viewed profile user.
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
360 public static function commonFriendsVisitor(int $uid, string $nickname)
362 if (local_user() == $uid) {
366 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
371 $localPCid = Contact::getPublicIdByUserId($uid);
374 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
378 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
383 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
384 if (!DBA::isResult($commonContacts)) {
389 foreach ($commonContacts as $contact) {
391 'url' => Contact::magicLinkByContact($contact),
392 'name' => $contact['name'],
393 'photo' => Contact::getThumb($contact),
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
409 * Insert a tag cloud widget for the present profile.
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
417 public static function tagCloud(int $uid, int $limit = 50)
423 if (Feature::isEnabled($uid, 'tagadelic')) {
424 $owner_id = Contact::getPublicIdByUserId($uid);
429 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
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)
440 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
442 public static function postedByYear(string $url, int $uid, bool $wall)
446 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
448 /* arrange the list in years */
449 $dnow = DateTimeFormat::localNow('Y-m-d');
453 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
454 $dthen = DI::cache()->get($cachekey);
456 $dthen = Item::firstPostDate($uid, $wall);
457 DI::cache()->set($cachekey, $dthen, Duration::HOUR);
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';
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
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'));
477 if (empty($ret[$dyear])) {
481 $ret[$dyear][] = [$str, $end_month, $start_month];
482 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
486 if (!DBA::isResult($ret)) {
491 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
492 $cutoff = array_key_exists($cutoff_year, $ret);
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,
501 '$showless' => DI::l10n()->t('show less'),
502 '$showmore' => DI::l10n()->t('show more')
509 * Display the account types sidebar
510 * The account type value is added as a parameter to the url
512 * @param string $base Basepath
513 * @param int $accounttype Acount type
516 public static function accounttypes(string $base, $accounttype)
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')],
525 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
526 DI::l10n()->t('All'), $base, $accounts, $accounttype);