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\Core\Session;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\FileTag;
32 use Friendica\Model\GContact;
33 use Friendica\Model\Group;
34 use Friendica\Model\Item;
35 use Friendica\Model\Profile;
36 use Friendica\Util\DateTimeFormat;
37 use Friendica\Util\Strings;
38 use Friendica\Util\Temporal;
43 * Return the follow widget
45 * @param string $value optional, default empty
47 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
49 public static function follow($value = "")
51 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
52 '$connect' => DI::l10n()->t('Add New Contact'),
53 '$desc' => DI::l10n()->t('Enter address or web location'),
54 '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
56 '$follow' => DI::l10n()->t('Connect')
61 * Return Find People widget
63 public static function findPeople()
65 $global_dir = DI::config()->get('system', 'directory');
67 if (DI::config()->get('system', 'invitation_only')) {
68 $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
69 if ($x || is_site_admin()) {
70 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
71 . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
77 $nv['findpeople'] = DI::l10n()->t('Find People');
78 $nv['desc'] = DI::l10n()->t('Enter name or interest');
79 $nv['label'] = DI::l10n()->t('Connect/Follow');
80 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
81 $nv['findthem'] = DI::l10n()->t('Find');
82 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
83 $nv['similar'] = DI::l10n()->t('Similar Interests');
84 $nv['random'] = DI::l10n()->t('Random Profile');
85 $nv['inv'] = DI::l10n()->t('Invite Friends');
86 $nv['directory'] = DI::l10n()->t('Global Directory');
87 $nv['global_dir'] = $global_dir;
88 $nv['local_directory'] = DI::l10n()->t('Local Directory');
93 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
97 * Return unavailable networks
99 public static function unavailableNetworks()
101 // Always hide content from these networks
102 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
104 if (!Addon::isEnabled("discourse")) {
105 $networks[] = Protocol::DISCOURSE;
108 if (!Addon::isEnabled("statusnet")) {
109 $networks[] = Protocol::STATUSNET;
112 if (!Addon::isEnabled("pumpio")) {
113 $networks[] = Protocol::PUMPIO;
116 if (!Addon::isEnabled("twitter")) {
117 $networks[] = Protocol::TWITTER;
120 if (DI::config()->get("system", "ostatus_disabled")) {
121 $networks[] = Protocol::OSTATUS;
124 if (!DI::config()->get("system", "diaspora_enabled")) {
125 $networks[] = Protocol::DIASPORA;
128 if (!Addon::isEnabled("pnut")) {
129 $networks[] = Protocol::PNUT;
132 if (!sizeof($networks)) {
136 $network_filter = implode("','", $networks);
138 $network_filter = "AND `network` NOT IN ('$network_filter')";
140 return $network_filter;
144 * Display a generic filter widget based on a list of options
146 * The options array must be the following format:
149 * 'ref' => {filter value},
150 * 'name' => {option name}
155 * @param string $type The filter query string key
156 * @param string $title
157 * @param string $desc
158 * @param string $all The no filter label
159 * @param string $baseUrl The full page request URI
160 * @param array $options
161 * @param string $selected The currently selected filter option value
165 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
167 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
171 parse_str($queryString, $queryArray);
172 unset($queryArray[$type]);
174 if (count($queryArray)) {
175 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
177 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
180 $baseUrl = trim($baseUrl, '?') . '?';
183 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
187 '$selected' => $selected,
188 '$all_label' => $all,
189 '$options' => $options,
195 * Return group membership widget
197 * @param string $baseurl
198 * @param string $selected
202 public static function groups($baseurl, $selected = '')
208 $options = array_map(function ($group) {
210 'ref' => $group['id'],
211 'name' => $group['name']
213 }, Group::getByUserId(local_user()));
217 DI::l10n()->t('Groups'),
219 DI::l10n()->t('Everyone'),
227 * Return contact relationship widget
229 * @param string $baseurl baseurl
230 * @param string $selected optional, default empty
234 public static function contactRels($baseurl, $selected = '')
241 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
242 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
243 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
248 DI::l10n()->t('Relationships'),
250 DI::l10n()->t('All Contacts'),
258 * Return networks widget
260 * @param string $baseurl baseurl
261 * @param string $selected optional, default empty
263 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
265 public static function networks($baseurl, $selected = '')
271 $extra_sql = self::unavailableNetworks();
273 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
278 while ($rr = DBA::fetch($r)) {
279 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
283 if (count($nets) < 2) {
289 DI::l10n()->t('Protocols'),
291 DI::l10n()->t('All Protocols'),
299 * Return file as widget
301 * @param string $baseurl baseurl
302 * @param string $selected optional, default empty
303 * @return string|void
306 public static function fileAs($baseurl, $selected = '')
312 $saved = DI::pConfig()->get(local_user(), 'system', 'filetags');
313 if (!strlen($saved)) {
318 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
319 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
322 usort($terms, function ($a, $b) {
323 return strcmp($a['name'], $b['name']);
328 DI::l10n()->t('Saved Folders'),
330 DI::l10n()->t('Everything'),
338 * Return categories widget
340 * @param string $baseurl baseurl
341 * @param string $selected optional, default empty
342 * @return string|void
343 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
345 public static function categories($baseurl, $selected = '')
349 $uid = intval($a->profile['uid']);
351 if (!Feature::isEnabled($uid, 'categories')) {
355 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
356 if (!strlen($saved)) {
361 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
362 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
367 DI::l10n()->t('Categories'),
369 DI::l10n()->t('Everything'),
377 * Return common friends visitor widget
379 * @param string $profile_uid uid
380 * @return string|void
381 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
383 public static function commonFriendsVisitor($profile_uid)
385 if (local_user() == $profile_uid) {
391 $cid = Session::getRemoteContactID($profile_uid);
394 if (Profile::getMyURL()) {
395 $contact = DBA::selectFirst('contact', ['id'],
396 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
397 if (DBA::isResult($contact)) {
398 $cid = $contact['id'];
400 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
401 if (DBA::isResult($gcontact)) {
402 $zcid = $gcontact['id'];
408 if ($cid == 0 && $zcid == 0) {
413 $t = GContact::countCommonFriends($profile_uid, $cid);
415 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
423 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
425 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
428 if (!DBA::isResult($r)) {
433 foreach ($r as $rr) {
434 $contact = Contact::getByURL($rr['url']);
436 'url' => Contact::magicLink($rr['url']),
437 'name' => $contact['name'] ?? $rr['name'],
438 'photo' => Contact::getThumb($contact, $rr['photo']),
443 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
444 return Renderer::replaceMacros($tpl, [
445 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $t),
446 '$base' => DI::baseUrl(),
447 '$uid' => $profile_uid,
448 '$cid' => (($cid) ? $cid : '0'),
449 '$linkmore' => (($t > 5) ? 'true' : ''),
450 '$more' => DI::l10n()->t('show more'),
456 * Insert a tag cloud widget for the present profile.
458 * @param int $limit Max number of displayed tags.
459 * @return string HTML formatted output.
460 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
461 * @throws \ImagickException
463 public static function tagCloud($limit = 50)
467 $uid = intval($a->profile['uid']);
469 if (!$uid || !$a->profile['url']) {
473 if (Feature::isEnabled($uid, 'tagadelic')) {
474 $owner_id = Contact::getIdForURL($a->profile['url'], 0, false);
479 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
486 * @param string $url Base page URL
487 * @param int $uid User ID consulting/publishing posts
488 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
490 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
492 public static function postedByYear(string $url, int $uid, bool $wall)
496 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
498 /* arrange the list in years */
499 $dnow = DateTimeFormat::localNow('Y-m-d');
503 $dthen = Item::firstPostDate($uid, $wall);
505 // Set the start and end date to the beginning of the month
506 $dnow = substr($dnow, 0, 8) . '01';
507 $dthen = substr($dthen, 0, 8) . '01';
510 * Starting with the current month, get the first and last days of every
511 * month down to and including the month of the first post
513 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
514 $dyear = intval(substr($dnow, 0, 4));
515 $dstart = substr($dnow, 0, 8) . '01';
516 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
517 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
518 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
519 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
521 if (empty($ret[$dyear])) {
525 $ret[$dyear][] = [$str, $end_month, $start_month];
526 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
530 if (!DBA::isResult($ret)) {
535 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
536 $cutoff = array_key_exists($cutoff_year, $ret);
538 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
539 '$title' => DI::l10n()->t('Archives'),
540 '$size' => $visible_years,
541 '$cutoff_year' => $cutoff_year,
542 '$cutoff' => $cutoff,
545 '$showmore' => DI::l10n()->t('show more')