3 * @copyright Copyright (C) 2010-2021, 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\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 || is_site_admin()) {
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 public static function unavailableNetworksAsArray()
98 // Always hide content from these networks
99 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
101 if (!Addon::isEnabled("discourse")) {
102 $networks[] = Protocol::DISCOURSE;
105 if (!Addon::isEnabled("statusnet")) {
106 $networks[] = Protocol::STATUSNET;
109 if (!Addon::isEnabled("pumpio")) {
110 $networks[] = Protocol::PUMPIO;
113 if (!Addon::isEnabled("twitter")) {
114 $networks[] = Protocol::TWITTER;
117 if (DI::config()->get("system", "ostatus_disabled")) {
118 $networks[] = Protocol::OSTATUS;
121 if (!DI::config()->get("system", "diaspora_enabled")) {
122 $networks[] = Protocol::DIASPORA;
125 if (!Addon::isEnabled("pnut")) {
126 $networks[] = Protocol::PNUT;
132 * Return unavailable networks
134 public static function unavailableNetworks()
136 $networks = self::unavailableNetworksAsArray();
138 if (!sizeof($networks)) {
142 $network_filter = implode("','", $networks);
144 $network_filter = "AND `network` NOT IN ('$network_filter')";
146 return $network_filter;
150 * Display a generic filter widget based on a list of options
152 * The options array must be the following format:
155 * 'ref' => {filter value},
156 * 'name' => {option name}
161 * @param string $type The filter query string key
162 * @param string $title
163 * @param string $desc
164 * @param string $all The no filter label
165 * @param string $baseUrl The full page request URI
166 * @param array $options
167 * @param string $selected The currently selected filter option value
171 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
173 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
177 parse_str($queryString, $queryArray);
178 unset($queryArray[$type]);
180 if (count($queryArray)) {
181 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
183 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
186 $baseUrl = trim($baseUrl, '?') . '?';
189 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
193 '$selected' => $selected,
194 '$all_label' => $all,
195 '$options' => $options,
201 * Return group membership widget
203 * @param string $baseurl
204 * @param string $selected
208 public static function groups($baseurl, $selected = '')
214 $options = array_map(function ($group) {
216 'ref' => $group['id'],
217 'name' => $group['name']
219 }, Group::getByUserId(local_user()));
223 DI::l10n()->t('Groups'),
225 DI::l10n()->t('Everyone'),
233 * Return contact relationship widget
235 * @param string $baseurl baseurl
236 * @param string $selected optional, default empty
240 public static function contactRels($baseurl, $selected = '')
247 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
248 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
249 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
254 DI::l10n()->t('Relationships'),
256 DI::l10n()->t('All Contacts'),
264 * Return networks widget
266 * @param string $baseurl baseurl
267 * @param string $selected optional, default empty
269 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
271 public static function networks($baseurl, $selected = '')
277 $extra_sql = self::unavailableNetworks();
279 $r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`",
284 while ($rr = DBA::fetch($r)) {
285 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
289 if (count($nets) < 2) {
295 DI::l10n()->t('Protocols'),
297 DI::l10n()->t('All Protocols'),
305 * Return file as widget
307 * @param string $baseurl baseurl
308 * @param string $selected optional, default empty
309 * @return string|void
312 public static function fileAs($baseurl, $selected = '')
319 foreach (Post\Category::getArray(local_user(), Post\Category::FILE) as $savedFolderName) {
320 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
325 DI::l10n()->t('Saved Folders'),
327 DI::l10n()->t('Everything'),
335 * Return categories widget
337 * @param string $baseurl baseurl
338 * @param string $selected optional, default empty
339 * @return string|void
340 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
342 public static function categories($baseurl, $selected = '')
346 $uid = intval($a->getProfileOwner());
348 if (!Feature::isEnabled($uid, 'categories')) {
353 foreach (Post\Category::getArray(local_user(), Post\Category::CATEGORY) as $savedFolderName) {
354 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
359 DI::l10n()->t('Categories'),
361 DI::l10n()->t('Everything'),
369 * Show a random selection of five common contacts between the visitor and the viewed profile user.
371 * @param int $uid Viewed profile user ID
372 * @param string $nickname Viewed profile user nickname
373 * @return string|void
374 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
375 * @throws \ImagickException
377 public static function commonFriendsVisitor(int $uid, string $nickname)
379 if (local_user() == $uid) {
383 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
388 $localPCid = Contact::getPublicIdByUserId($uid);
391 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
395 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
400 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
401 if (!DBA::isResult($commonContacts)) {
406 foreach ($commonContacts as $contact) {
408 'url' => Contact::magicLinkByContact($contact),
409 'name' => $contact['name'],
410 'photo' => Contact::getThumb($contact),
414 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
415 return Renderer::replaceMacros($tpl, [
416 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
417 '$base' => DI::baseUrl(),
418 '$nickname' => $nickname,
419 '$linkmore' => $total > 5 ? 'true' : '',
420 '$more' => DI::l10n()->t('show more'),
421 '$contacts' => $entries
426 * Insert a tag cloud widget for the present profile.
428 * @param int $uid User ID
429 * @param int $limit Max number of displayed tags.
430 * @return string HTML formatted output.
431 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
432 * @throws \ImagickException
434 public static function tagCloud(int $uid, int $limit = 50)
440 if (Feature::isEnabled($uid, 'tagadelic')) {
441 $owner_id = Contact::getPublicIdByUserId($uid);
446 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
453 * @param string $url Base page URL
454 * @param int $uid User ID consulting/publishing posts
455 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
457 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
459 public static function postedByYear(string $url, int $uid, bool $wall)
463 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
465 /* arrange the list in years */
466 $dnow = DateTimeFormat::localNow('Y-m-d');
470 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
471 $dthen = DI::cache()->get($cachekey);
473 $dthen = Item::firstPostDate($uid, $wall);
474 DI::cache()->set($cachekey, $dthen, Duration::HOUR);
478 // Set the start and end date to the beginning of the month
479 $dnow = substr($dnow, 0, 8) . '01';
480 $dthen = substr($dthen, 0, 8) . '01';
483 * Starting with the current month, get the first and last days of every
484 * month down to and including the month of the first post
486 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
487 $dyear = intval(substr($dnow, 0, 4));
488 $dstart = substr($dnow, 0, 8) . '01';
489 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
490 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
491 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
492 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
494 if (empty($ret[$dyear])) {
498 $ret[$dyear][] = [$str, $end_month, $start_month];
499 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
503 if (!DBA::isResult($ret)) {
508 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
509 $cutoff = array_key_exists($cutoff_year, $ret);
511 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
512 '$title' => DI::l10n()->t('Archives'),
513 '$size' => $visible_years,
514 '$cutoff_year' => $cutoff_year,
515 '$cutoff' => $cutoff,
518 '$showless' => DI::l10n()->t('show less'),
519 '$showmore' => DI::l10n()->t('show more')
526 * Display the account types sidebar
527 * The account type value is added as a parameter to the url
529 * @param string $base Basepath
530 * @param int $accounttype Acount type
533 public static function accounttypes(string $base, $accounttype)
536 ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
537 ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
538 ['ref' => 'news', 'name' => DI::l10n()->t('News')],
539 ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
542 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
543 DI::l10n()->t('All'), $base, $accounts, $accounttype);