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\Proxy as ProxyUtils;
38 use Friendica\Util\Strings;
39 use Friendica\Util\Temporal;
44 * Return the follow widget
46 * @param string $value optional, default empty
48 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
50 public static function follow($value = "")
52 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
53 '$connect' => DI::l10n()->t('Add New Contact'),
54 '$desc' => DI::l10n()->t('Enter address or web location'),
55 '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
57 '$follow' => DI::l10n()->t('Connect')
62 * Return Find People widget
64 public static function findPeople()
66 $global_dir = DI::config()->get('system', 'directory');
68 if (DI::config()->get('system', 'invitation_only')) {
69 $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
70 if ($x || is_site_admin()) {
71 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
72 . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
78 $nv['findpeople'] = DI::l10n()->t('Find People');
79 $nv['desc'] = DI::l10n()->t('Enter name or interest');
80 $nv['label'] = DI::l10n()->t('Connect/Follow');
81 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
82 $nv['findthem'] = DI::l10n()->t('Find');
83 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
84 $nv['similar'] = DI::l10n()->t('Similar Interests');
85 $nv['random'] = DI::l10n()->t('Random Profile');
86 $nv['inv'] = DI::l10n()->t('Invite Friends');
87 $nv['directory'] = DI::l10n()->t('Global Directory');
88 $nv['global_dir'] = $global_dir;
89 $nv['local_directory'] = DI::l10n()->t('Local Directory');
94 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
98 * Return unavailable networks
100 public static function unavailableNetworks()
102 // Always hide content from these networks
103 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
105 if (!Addon::isEnabled("discourse")) {
106 $networks[] = Protocol::DISCOURSE;
109 if (!Addon::isEnabled("statusnet")) {
110 $networks[] = Protocol::STATUSNET;
113 if (!Addon::isEnabled("pumpio")) {
114 $networks[] = Protocol::PUMPIO;
117 if (!Addon::isEnabled("twitter")) {
118 $networks[] = Protocol::TWITTER;
121 if (DI::config()->get("system", "ostatus_disabled")) {
122 $networks[] = Protocol::OSTATUS;
125 if (!DI::config()->get("system", "diaspora_enabled")) {
126 $networks[] = Protocol::DIASPORA;
129 if (!Addon::isEnabled("pnut")) {
130 $networks[] = Protocol::PNUT;
133 if (!sizeof($networks)) {
137 $network_filter = implode("','", $networks);
139 $network_filter = "AND `network` NOT IN ('$network_filter')";
141 return $network_filter;
145 * Display a generic filter widget based on a list of options
147 * The options array must be the following format:
150 * 'ref' => {filter value},
151 * 'name' => {option name}
156 * @param string $type The filter query string key
157 * @param string $title
158 * @param string $desc
159 * @param string $all The no filter label
160 * @param string $baseUrl The full page request URI
161 * @param array $options
162 * @param string $selected The currently selected filter option value
166 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
168 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
172 parse_str($queryString, $queryArray);
173 unset($queryArray[$type]);
175 if (count($queryArray)) {
176 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
178 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
181 $baseUrl = trim($baseUrl, '?') . '?';
184 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
188 '$selected' => $selected,
189 '$all_label' => $all,
190 '$options' => $options,
196 * Return group membership widget
198 * @param string $baseurl
199 * @param string $selected
203 public static function groups($baseurl, $selected = '')
209 $options = array_map(function ($group) {
211 'ref' => $group['id'],
212 'name' => $group['name']
214 }, Group::getByUserId(local_user()));
218 DI::l10n()->t('Groups'),
220 DI::l10n()->t('Everyone'),
228 * Return contact relationship widget
230 * @param string $baseurl baseurl
231 * @param string $selected optional, default empty
235 public static function contactRels($baseurl, $selected = '')
242 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
243 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
244 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
249 DI::l10n()->t('Relationships'),
251 DI::l10n()->t('All Contacts'),
259 * Return networks widget
261 * @param string $baseurl baseurl
262 * @param string $selected optional, default empty
264 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
266 public static function networks($baseurl, $selected = '')
272 $extra_sql = self::unavailableNetworks();
274 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
279 while ($rr = DBA::fetch($r)) {
280 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
284 if (count($nets) < 2) {
290 DI::l10n()->t('Protocols'),
292 DI::l10n()->t('All Protocols'),
300 * Return file as widget
302 * @param string $baseurl baseurl
303 * @param string $selected optional, default empty
304 * @return string|void
307 public static function fileAs($baseurl, $selected = '')
313 $saved = DI::pConfig()->get(local_user(), 'system', 'filetags');
314 if (!strlen($saved)) {
319 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
320 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
323 usort($terms, function ($a, $b) {
324 return strcmp($a['name'], $b['name']);
329 DI::l10n()->t('Saved Folders'),
331 DI::l10n()->t('Everything'),
339 * Return categories widget
341 * @param string $baseurl baseurl
342 * @param string $selected optional, default empty
343 * @return string|void
344 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
346 public static function categories($baseurl, $selected = '')
350 $uid = intval($a->profile['uid']);
352 if (!Feature::isEnabled($uid, 'categories')) {
356 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
357 if (!strlen($saved)) {
362 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
363 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
368 DI::l10n()->t('Categories'),
370 DI::l10n()->t('Everything'),
378 * Return common friends visitor widget
380 * @param string $profile_uid uid
381 * @return string|void
382 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
384 public static function commonFriendsVisitor($profile_uid)
386 if (local_user() == $profile_uid) {
392 $cid = Session::getRemoteContactID($profile_uid);
395 if (Profile::getMyURL()) {
396 $contact = DBA::selectFirst('contact', ['id'],
397 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
398 if (DBA::isResult($contact)) {
399 $cid = $contact['id'];
401 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
402 if (DBA::isResult($gcontact)) {
403 $zcid = $gcontact['id'];
409 if ($cid == 0 && $zcid == 0) {
414 $t = GContact::countCommonFriends($profile_uid, $cid);
416 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
424 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
426 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
429 if (!DBA::isResult($r)) {
434 foreach ($r as $rr) {
436 'url' => Contact::magicLink($rr['url']),
437 'name' => $rr['name'],
438 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
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')