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
96 public static function unavailableNetworks()
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;
129 if (!sizeof($networks)) {
133 $network_filter = implode("','", $networks);
135 $network_filter = "AND `network` NOT IN ('$network_filter')";
137 return $network_filter;
141 * Display a generic filter widget based on a list of options
143 * The options array must be the following format:
146 * 'ref' => {filter value},
147 * 'name' => {option name}
152 * @param string $type The filter query string key
153 * @param string $title
154 * @param string $desc
155 * @param string $all The no filter label
156 * @param string $baseUrl The full page request URI
157 * @param array $options
158 * @param string $selected The currently selected filter option value
162 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
164 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
168 parse_str($queryString, $queryArray);
169 unset($queryArray[$type]);
171 if (count($queryArray)) {
172 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
174 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
177 $baseUrl = trim($baseUrl, '?') . '?';
180 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
184 '$selected' => $selected,
185 '$all_label' => $all,
186 '$options' => $options,
192 * Return group membership widget
194 * @param string $baseurl
195 * @param string $selected
199 public static function groups($baseurl, $selected = '')
205 $options = array_map(function ($group) {
207 'ref' => $group['id'],
208 'name' => $group['name']
210 }, Group::getByUserId(local_user()));
214 DI::l10n()->t('Groups'),
216 DI::l10n()->t('Everyone'),
224 * Return contact relationship widget
226 * @param string $baseurl baseurl
227 * @param string $selected optional, default empty
231 public static function contactRels($baseurl, $selected = '')
238 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
239 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
240 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
245 DI::l10n()->t('Relationships'),
247 DI::l10n()->t('All Contacts'),
255 * Return networks widget
257 * @param string $baseurl baseurl
258 * @param string $selected optional, default empty
260 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
262 public static function networks($baseurl, $selected = '')
268 $extra_sql = self::unavailableNetworks();
270 $r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`",
275 while ($rr = DBA::fetch($r)) {
276 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
280 if (count($nets) < 2) {
286 DI::l10n()->t('Protocols'),
288 DI::l10n()->t('All Protocols'),
296 * Return file as widget
298 * @param string $baseurl baseurl
299 * @param string $selected optional, default empty
300 * @return string|void
303 public static function fileAs($baseurl, $selected = '')
310 foreach (Post\Category::getArray(local_user(), Post\Category::FILE) as $savedFolderName) {
311 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
316 DI::l10n()->t('Saved Folders'),
318 DI::l10n()->t('Everything'),
326 * Return categories widget
328 * @param string $baseurl baseurl
329 * @param string $selected optional, default empty
330 * @return string|void
331 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
333 public static function categories($baseurl, $selected = '')
337 $uid = intval($a->getProfileOwner());
339 if (!Feature::isEnabled($uid, 'categories')) {
344 foreach (Post\Category::getArray(local_user(), Post\Category::CATEGORY) as $savedFolderName) {
345 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
350 DI::l10n()->t('Categories'),
352 DI::l10n()->t('Everything'),
360 * Show a random selection of five common contacts between the visitor and the viewed profile user.
362 * @param int $uid Viewed profile user ID
363 * @param string $nickname Viewed profile user nickname
364 * @return string|void
365 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
366 * @throws \ImagickException
368 public static function commonFriendsVisitor(int $uid, string $nickname)
370 if (local_user() == $uid) {
374 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
379 $localPCid = Contact::getPublicIdByUserId($uid);
382 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
386 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
391 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
392 if (!DBA::isResult($commonContacts)) {
397 foreach ($commonContacts as $contact) {
399 'url' => Contact::magicLinkByContact($contact),
400 'name' => $contact['name'],
401 'photo' => Contact::getThumb($contact),
405 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
406 return Renderer::replaceMacros($tpl, [
407 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
408 '$base' => DI::baseUrl(),
409 '$nickname' => $nickname,
410 '$linkmore' => $total > 5 ? 'true' : '',
411 '$more' => DI::l10n()->t('show more'),
412 '$contacts' => $entries
417 * Insert a tag cloud widget for the present profile.
419 * @param int $uid User ID
420 * @param int $limit Max number of displayed tags.
421 * @return string HTML formatted output.
422 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
423 * @throws \ImagickException
425 public static function tagCloud(int $uid, int $limit = 50)
431 if (Feature::isEnabled($uid, 'tagadelic')) {
432 $owner_id = Contact::getPublicIdByUserId($uid);
437 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
444 * @param string $url Base page URL
445 * @param int $uid User ID consulting/publishing posts
446 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
448 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
450 public static function postedByYear(string $url, int $uid, bool $wall)
454 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
456 /* arrange the list in years */
457 $dnow = DateTimeFormat::localNow('Y-m-d');
461 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
462 $dthen = DI::cache()->get($cachekey);
464 $dthen = Item::firstPostDate($uid, $wall);
465 DI::cache()->set($cachekey, $dthen, Duration::HOUR);
469 // Set the start and end date to the beginning of the month
470 $dnow = substr($dnow, 0, 8) . '01';
471 $dthen = substr($dthen, 0, 8) . '01';
474 * Starting with the current month, get the first and last days of every
475 * month down to and including the month of the first post
477 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
478 $dyear = intval(substr($dnow, 0, 4));
479 $dstart = substr($dnow, 0, 8) . '01';
480 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
481 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
482 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
483 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
485 if (empty($ret[$dyear])) {
489 $ret[$dyear][] = [$str, $end_month, $start_month];
490 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
494 if (!DBA::isResult($ret)) {
499 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
500 $cutoff = array_key_exists($cutoff_year, $ret);
502 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
503 '$title' => DI::l10n()->t('Archives'),
504 '$size' => $visible_years,
505 '$cutoff_year' => $cutoff_year,
506 '$cutoff' => $cutoff,
509 '$showless' => DI::l10n()->t('show less'),
510 '$showmore' => DI::l10n()->t('show more')
517 * Display the account types sidebar
518 * The account type value is added as a parameter to the url
520 * @param string $base Basepath
521 * @param int $accounttype Acount type
524 public static function accounttypes(string $base, $accounttype)
527 ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
528 ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
529 ['ref' => 'news', 'name' => DI::l10n()->t('News')],
530 ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
533 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
534 DI::l10n()->t('All'), $base, $accounts, $accounttype);