3 * @copyright Copyright (C) 2020, Friendica
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\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Model\FileTag;
31 use Friendica\Model\Group;
32 use Friendica\Model\Item;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Temporal;
39 * Return the follow widget
41 * @param string $value optional, default empty
43 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
45 public static function follow($value = "")
47 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
48 '$connect' => DI::l10n()->t('Add New Contact'),
49 '$desc' => DI::l10n()->t('Enter address or web location'),
50 '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
52 '$follow' => DI::l10n()->t('Connect')
57 * Return Find People widget
59 public static function findPeople()
61 $global_dir = DI::config()->get('system', 'directory');
63 if (DI::config()->get('system', 'invitation_only')) {
64 $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
65 if ($x || is_site_admin()) {
66 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
67 . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
73 $nv['findpeople'] = DI::l10n()->t('Find People');
74 $nv['desc'] = DI::l10n()->t('Enter name or interest');
75 $nv['label'] = DI::l10n()->t('Connect/Follow');
76 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
77 $nv['findthem'] = DI::l10n()->t('Find');
78 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
79 $nv['similar'] = DI::l10n()->t('Similar Interests');
80 $nv['random'] = DI::l10n()->t('Random Profile');
81 $nv['inv'] = DI::l10n()->t('Invite Friends');
82 $nv['directory'] = DI::l10n()->t('Global Directory');
83 $nv['global_dir'] = $global_dir;
84 $nv['local_directory'] = DI::l10n()->t('Local Directory');
89 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
93 * Return unavailable networks
95 public static function unavailableNetworks()
97 // Always hide content from these networks
98 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
100 if (!Addon::isEnabled("discourse")) {
101 $networks[] = Protocol::DISCOURSE;
104 if (!Addon::isEnabled("statusnet")) {
105 $networks[] = Protocol::STATUSNET;
108 if (!Addon::isEnabled("pumpio")) {
109 $networks[] = Protocol::PUMPIO;
112 if (!Addon::isEnabled("twitter")) {
113 $networks[] = Protocol::TWITTER;
116 if (DI::config()->get("system", "ostatus_disabled")) {
117 $networks[] = Protocol::OSTATUS;
120 if (!DI::config()->get("system", "diaspora_enabled")) {
121 $networks[] = Protocol::DIASPORA;
124 if (!Addon::isEnabled("pnut")) {
125 $networks[] = Protocol::PNUT;
128 if (!sizeof($networks)) {
132 $network_filter = implode("','", $networks);
134 $network_filter = "AND `network` NOT IN ('$network_filter')";
136 return $network_filter;
140 * Display a generic filter widget based on a list of options
142 * The options array must be the following format:
145 * 'ref' => {filter value},
146 * 'name' => {option name}
151 * @param string $type The filter query string key
152 * @param string $title
153 * @param string $desc
154 * @param string $all The no filter label
155 * @param string $baseUrl The full page request URI
156 * @param array $options
157 * @param string $selected The currently selected filter option value
161 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
163 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
167 parse_str($queryString, $queryArray);
168 unset($queryArray[$type]);
170 if (count($queryArray)) {
171 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
173 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
176 $baseUrl = trim($baseUrl, '?') . '?';
179 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
183 '$selected' => $selected,
184 '$all_label' => $all,
185 '$options' => $options,
191 * Return group membership widget
193 * @param string $baseurl
194 * @param string $selected
198 public static function groups($baseurl, $selected = '')
204 $options = array_map(function ($group) {
206 'ref' => $group['id'],
207 'name' => $group['name']
209 }, Group::getByUserId(local_user()));
213 DI::l10n()->t('Groups'),
215 DI::l10n()->t('Everyone'),
223 * Return contact relationship widget
225 * @param string $baseurl baseurl
226 * @param string $selected optional, default empty
230 public static function contactRels($baseurl, $selected = '')
237 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
238 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
239 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
244 DI::l10n()->t('Relationships'),
246 DI::l10n()->t('All Contacts'),
254 * Return networks widget
256 * @param string $baseurl baseurl
257 * @param string $selected optional, default empty
259 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
261 public static function networks($baseurl, $selected = '')
267 $extra_sql = self::unavailableNetworks();
269 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
274 while ($rr = DBA::fetch($r)) {
275 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
279 if (count($nets) < 2) {
285 DI::l10n()->t('Protocols'),
287 DI::l10n()->t('All Protocols'),
295 * Return file as widget
297 * @param string $baseurl baseurl
298 * @param string $selected optional, default empty
299 * @return string|void
302 public static function fileAs($baseurl, $selected = '')
308 $saved = DI::pConfig()->get(local_user(), 'system', 'filetags');
309 if (!strlen($saved)) {
314 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
315 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
318 usort($terms, function ($a, $b) {
319 return strcmp($a['name'], $b['name']);
324 DI::l10n()->t('Saved Folders'),
326 DI::l10n()->t('Everything'),
334 * Return categories widget
336 * @param string $baseurl baseurl
337 * @param string $selected optional, default empty
338 * @return string|void
339 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341 public static function categories($baseurl, $selected = '')
345 $uid = intval($a->profile['uid']);
347 if (!Feature::isEnabled($uid, 'categories')) {
351 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
352 if (!strlen($saved)) {
357 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
358 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
363 DI::l10n()->t('Categories'),
365 DI::l10n()->t('Everything'),
373 * Show a random selection of five common contacts between the visitor and the viewed profile user.
375 * @param int $uid Viewed profile user ID
376 * @param string $nickname Viewed profile user nickname
377 * @return string|void
378 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
379 * @throws \ImagickException
381 public static function commonFriendsVisitor(int $uid, string $nickname)
383 if (local_user() == $uid) {
387 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
392 $localPCid = Contact::getPublicIdByUserId($uid);
395 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
399 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
404 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
405 if (!DBA::isResult($commonContacts)) {
410 foreach ($commonContacts as $contact) {
412 'url' => Contact::magicLink($contact['url']),
413 'name' => $contact['name'],
414 'photo' => Contact::getThumb($contact),
418 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
419 return Renderer::replaceMacros($tpl, [
420 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
421 '$base' => DI::baseUrl(),
422 '$nickname' => $nickname,
423 '$linkmore' => $total > 5 ? 'true' : '',
424 '$more' => DI::l10n()->t('show more'),
425 '$contacts' => $entries
430 * Insert a tag cloud widget for the present profile.
432 * @param int $limit Max number of displayed tags.
433 * @return string HTML formatted output.
434 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
435 * @throws \ImagickException
437 public static function tagCloud($limit = 50)
441 $uid = intval($a->profile['uid']);
443 if (!$uid || !$a->profile['url']) {
447 if (Feature::isEnabled($uid, 'tagadelic')) {
448 $owner_id = Contact::getIdForURL($a->profile['url'], 0, false);
453 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
460 * @param string $url Base page URL
461 * @param int $uid User ID consulting/publishing posts
462 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
464 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
466 public static function postedByYear(string $url, int $uid, bool $wall)
470 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
472 /* arrange the list in years */
473 $dnow = DateTimeFormat::localNow('Y-m-d');
477 $dthen = Item::firstPostDate($uid, $wall);
479 // Set the start and end date to the beginning of the month
480 $dnow = substr($dnow, 0, 8) . '01';
481 $dthen = substr($dthen, 0, 8) . '01';
484 * Starting with the current month, get the first and last days of every
485 * month down to and including the month of the first post
487 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
488 $dyear = intval(substr($dnow, 0, 4));
489 $dstart = substr($dnow, 0, 8) . '01';
490 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
491 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
492 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
493 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
495 if (empty($ret[$dyear])) {
499 $ret[$dyear][] = [$str, $end_month, $start_month];
500 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
504 if (!DBA::isResult($ret)) {
509 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
510 $cutoff = array_key_exists($cutoff_year, $ret);
512 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
513 '$title' => DI::l10n()->t('Archives'),
514 '$size' => $visible_years,
515 '$cutoff_year' => $cutoff_year,
516 '$cutoff' => $cutoff,
519 '$showless' => DI::l10n()->t('show less'),
520 '$showmore' => DI::l10n()->t('show more')
527 * Display the account types sidebar
528 * The account type value is added as a parameter to the url
530 * @param string $base Basepath
531 * @param int $accounttype Acount type
534 public static function accounttypes(string $base, $accounttype)
537 ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
538 ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
539 ['ref' => 'news', 'name' => DI::l10n()->t('News')],
540 ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
543 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
544 DI::l10n()->t('All'), $base, $accounts, $accounttype);