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 string $baseurl baseurl
322 * @param string $selected optional, default empty
323 * @return string|void
324 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
326 public static function categories($baseurl, $selected = '')
330 $uid = intval($a->getProfileOwner());
332 if (!Feature::isEnabled($uid, 'categories')) {
337 foreach (Post\Category::getArray(local_user(), Post\Category::CATEGORY) as $savedFolderName) {
338 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
343 DI::l10n()->t('Categories'),
345 DI::l10n()->t('Everything'),
353 * Show a random selection of five common contacts between the visitor and the viewed profile user.
355 * @param int $uid Viewed profile user ID
356 * @param string $nickname Viewed profile user nickname
357 * @return string|void
358 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
359 * @throws \ImagickException
361 public static function commonFriendsVisitor(int $uid, string $nickname)
363 if (local_user() == $uid) {
367 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
372 $localPCid = Contact::getPublicIdByUserId($uid);
375 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
379 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
384 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
385 if (!DBA::isResult($commonContacts)) {
390 foreach ($commonContacts as $contact) {
392 'url' => Contact::magicLinkByContact($contact),
393 'name' => $contact['name'],
394 'photo' => Contact::getThumb($contact),
398 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
399 return Renderer::replaceMacros($tpl, [
400 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
401 '$base' => DI::baseUrl(),
402 '$nickname' => $nickname,
403 '$linkmore' => $total > 5 ? 'true' : '',
404 '$more' => DI::l10n()->t('show more'),
405 '$contacts' => $entries
410 * Insert a tag cloud widget for the present profile.
412 * @param int $uid User ID
413 * @param int $limit Max number of displayed tags.
414 * @return string HTML formatted output.
415 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
416 * @throws \ImagickException
418 public static function tagCloud(int $uid, int $limit = 50)
424 if (Feature::isEnabled($uid, 'tagadelic')) {
425 $owner_id = Contact::getPublicIdByUserId($uid);
430 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
437 * @param string $url Base page URL
438 * @param int $uid User ID consulting/publishing posts
439 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
441 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
443 public static function postedByYear(string $url, int $uid, bool $wall)
447 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
449 /* arrange the list in years */
450 $dnow = DateTimeFormat::localNow('Y-m-d');
454 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
455 $dthen = DI::cache()->get($cachekey);
457 $dthen = Item::firstPostDate($uid, $wall);
458 DI::cache()->set($cachekey, $dthen, Duration::HOUR);
462 // Set the start and end date to the beginning of the month
463 $dnow = substr($dnow, 0, 8) . '01';
464 $dthen = substr($dthen, 0, 8) . '01';
467 * Starting with the current month, get the first and last days of every
468 * month down to and including the month of the first post
470 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
471 $dyear = intval(substr($dnow, 0, 4));
472 $dstart = substr($dnow, 0, 8) . '01';
473 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
474 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
475 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
476 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
478 if (empty($ret[$dyear])) {
482 $ret[$dyear][] = [$str, $end_month, $start_month];
483 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
487 if (!DBA::isResult($ret)) {
492 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
493 $cutoff = array_key_exists($cutoff_year, $ret);
495 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
496 '$title' => DI::l10n()->t('Archives'),
497 '$size' => $visible_years,
498 '$cutoff_year' => $cutoff_year,
499 '$cutoff' => $cutoff,
502 '$showless' => DI::l10n()->t('show less'),
503 '$showmore' => DI::l10n()->t('show more')
510 * Display the account types sidebar
511 * The account type value is added as a parameter to the url
513 * @param string $base Basepath
514 * @param int $accounttype Acount type
517 public static function accounttypes(string $base, $accounttype)
520 ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
521 ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
522 ['ref' => 'news', 'name' => DI::l10n()->t('News')],
523 ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
526 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
527 DI::l10n()->t('All'), $base, $accounts, $accounttype);