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\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Group;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Util\DateTimeFormat;
35 use Friendica\Util\Temporal;
40 * Return the follow widget
42 * @param string $value optional, default empty
44 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46 public static function follow($value = "")
48 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
49 '$connect' => DI::l10n()->t('Add New Contact'),
50 '$desc' => DI::l10n()->t('Enter address or web location'),
51 '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
53 '$follow' => DI::l10n()->t('Connect')
58 * Return Find People widget
60 public static function findPeople()
62 $global_dir = DI::config()->get('system', 'directory');
64 if (DI::config()->get('system', 'invitation_only')) {
65 $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
66 if ($x || DI::app()->isSiteAdmin()) {
67 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
68 . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
74 $nv['findpeople'] = DI::l10n()->t('Find People');
75 $nv['desc'] = DI::l10n()->t('Enter name or interest');
76 $nv['label'] = DI::l10n()->t('Connect/Follow');
77 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
78 $nv['findthem'] = DI::l10n()->t('Find');
79 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
80 $nv['similar'] = DI::l10n()->t('Similar Interests');
81 $nv['random'] = DI::l10n()->t('Random Profile');
82 $nv['inv'] = DI::l10n()->t('Invite Friends');
83 $nv['directory'] = DI::l10n()->t('Global Directory');
84 $nv['global_dir'] = $global_dir;
85 $nv['local_directory'] = DI::l10n()->t('Local Directory');
90 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
94 * Return unavailable networks as array
96 * @return array Unsupported networks
98 public static function unavailableNetworks()
100 // Always hide content from these networks
101 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::ZOT];
103 if (!Addon::isEnabled("discourse")) {
104 $networks[] = Protocol::DISCOURSE;
107 if (!Addon::isEnabled("statusnet")) {
108 $networks[] = Protocol::STATUSNET;
111 if (!Addon::isEnabled("pumpio")) {
112 $networks[] = Protocol::PUMPIO;
115 if (!Addon::isEnabled("twitter")) {
116 $networks[] = Protocol::TWITTER;
119 if (DI::config()->get("system", "ostatus_disabled")) {
120 $networks[] = Protocol::OSTATUS;
123 if (!DI::config()->get("system", "diaspora_enabled")) {
124 $networks[] = Protocol::DIASPORA;
127 if (!Addon::isEnabled("pnut")) {
128 $networks[] = Protocol::PNUT;
134 * Display a generic filter widget based on a list of options
136 * The options array must be the following format:
139 * 'ref' => {filter value},
140 * 'name' => {option name}
145 * @param string $type The filter query string key
146 * @param string $title
147 * @param string $desc
148 * @param string $all The no filter label
149 * @param string $baseUrl The full page request URI
150 * @param array $options
151 * @param string $selected The currently selected filter option value
155 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
157 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
161 parse_str($queryString, $queryArray);
162 unset($queryArray[$type]);
164 if (count($queryArray)) {
165 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
167 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
170 $baseUrl = trim($baseUrl, '?') . '?';
173 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
177 '$selected' => $selected,
178 '$all_label' => $all,
179 '$options' => $options,
185 * Return group membership widget
187 * @param string $baseurl
188 * @param string $selected
192 public static function groups($baseurl, $selected = '')
198 $options = array_map(function ($group) {
200 'ref' => $group['id'],
201 'name' => $group['name']
203 }, Group::getByUserId(local_user()));
207 DI::l10n()->t('Groups'),
209 DI::l10n()->t('Everyone'),
217 * Return contact relationship widget
219 * @param string $baseurl baseurl
220 * @param string $selected optional, default empty
224 public static function contactRels($baseurl, $selected = '')
231 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
232 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
233 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
238 DI::l10n()->t('Relationships'),
240 DI::l10n()->t('All Contacts'),
248 * Return networks widget
250 * @param string $baseurl baseurl
251 * @param string $selected optional, default empty
253 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255 public static function networks($baseurl, $selected = '')
261 $networks = self::unavailableNetworks();
262 $query = "`uid` = ? AND NOT `deleted` AND `network` != '' AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")";
263 $condition = array_merge([$query], array_merge([local_user()], $networks));
265 $r = DBA::select('contact', ['network'], $condition, ['group_by' => ['network'], 'order' => ['network']]);
268 while ($rr = DBA::fetch($r)) {
269 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
273 if (count($nets) < 2) {
279 DI::l10n()->t('Protocols'),
281 DI::l10n()->t('All Protocols'),
289 * Return file as widget
291 * @param string $baseurl baseurl
292 * @param string $selected optional, default empty
293 * @return string|void
296 public static function fileAs($baseurl, $selected = '')
303 foreach (Post\Category::getArray(local_user(), Post\Category::FILE) as $savedFolderName) {
304 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
309 DI::l10n()->t('Saved Folders'),
311 DI::l10n()->t('Everything'),
319 * Return categories widget
321 * @param int $uid Id of the user owning the categories
322 * @param string $baseurl Base page URL
323 * @param string $selected Selected category
324 * @return string|void
325 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
327 public static function categories(int $uid, string $baseurl, string $selected = '')
329 if (!Feature::isEnabled($uid, 'categories')) {
334 foreach (Post\Category::getArray($uid, Post\Category::CATEGORY) as $savedFolderName) {
335 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
340 DI::l10n()->t('Categories'),
342 DI::l10n()->t('Everything'),
350 * Show a random selection of five common contacts between the visitor and the viewed profile user.
352 * @param int $uid Viewed profile user ID
353 * @param string $nickname Viewed profile user nickname
354 * @return string|void
355 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
356 * @throws \ImagickException
358 public static function commonFriendsVisitor(int $uid, string $nickname)
360 if (local_user() == $uid) {
364 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
369 $localPCid = Contact::getPublicIdByUserId($uid);
372 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
376 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
381 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
382 if (!DBA::isResult($commonContacts)) {
387 foreach ($commonContacts as $contact) {
389 'url' => Contact::magicLinkByContact($contact),
390 'name' => $contact['name'],
391 'photo' => Contact::getThumb($contact),
395 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
396 return Renderer::replaceMacros($tpl, [
397 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
398 '$base' => DI::baseUrl(),
399 '$nickname' => $nickname,
400 '$linkmore' => $total > 5 ? 'true' : '',
401 '$more' => DI::l10n()->t('show more'),
402 '$contacts' => $entries
407 * Insert a tag cloud widget for the present profile.
409 * @param int $uid User ID
410 * @param int $limit Max number of displayed tags.
411 * @return string HTML formatted output.
412 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
413 * @throws \ImagickException
415 public static function tagCloud(int $uid, int $limit = 50)
421 if (Feature::isEnabled($uid, 'tagadelic')) {
422 $owner_id = Contact::getPublicIdByUserId($uid);
427 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
434 * @param string $url Base page URL
435 * @param int $uid User ID consulting/publishing posts
436 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
438 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
440 public static function postedByYear(string $url, int $uid, bool $wall)
444 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
446 /* arrange the list in years */
447 $dnow = DateTimeFormat::localNow('Y-m-d');
451 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
452 $dthen = DI::cache()->get($cachekey);
454 $dthen = Item::firstPostDate($uid, $wall);
455 DI::cache()->set($cachekey, $dthen, Duration::HOUR);
459 // Set the start and end date to the beginning of the month
460 $dnow = substr($dnow, 0, 8) . '01';
461 $dthen = substr($dthen, 0, 8) . '01';
464 * Starting with the current month, get the first and last days of every
465 * month down to and including the month of the first post
467 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
468 $dyear = intval(substr($dnow, 0, 4));
469 $dstart = substr($dnow, 0, 8) . '01';
470 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
471 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
472 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
473 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
475 if (empty($ret[$dyear])) {
479 $ret[$dyear][] = [$str, $end_month, $start_month];
480 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
484 if (!DBA::isResult($ret)) {
489 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
490 $cutoff = array_key_exists($cutoff_year, $ret);
492 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
493 '$title' => DI::l10n()->t('Archives'),
494 '$size' => $visible_years,
495 '$cutoff_year' => $cutoff_year,
496 '$cutoff' => $cutoff,
499 '$showless' => DI::l10n()->t('show less'),
500 '$showmore' => DI::l10n()->t('show more')
507 * Display the account types sidebar
508 * The account type value is added as a parameter to the url
510 * @param string $base Basepath
511 * @param int $accounttype Acount type
514 public static function accounttypes(string $base, $accounttype)
517 ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
518 ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
519 ['ref' => 'news', 'name' => DI::l10n()->t('News')],
520 ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
523 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
524 DI::l10n()->t('All'), $base, $accounts, $accounttype);