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 * Show a random selection of five common contacts between the visitor and the viewed profile user.
379 * @param int $uid Viewed profile user ID
380 * @param string $nickname Viewed profile user nickname
381 * @return string|void
382 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
383 * @throws \ImagickException
385 public static function commonFriendsVisitor(int $uid, string $nickname)
387 if (local_user() == $uid) {
391 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
396 $localPCid = Contact::getPublicIdByUserId($uid);
399 'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
403 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
408 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
409 if (!DBA::isResult($commonContacts)) {
414 foreach ($commonContacts as $contact) {
416 'url' => Contact::magicLink($contact['url']),
417 'name' => $contact['name'],
418 'photo' => Contact::getThumb($contact),
422 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
423 return Renderer::replaceMacros($tpl, [
424 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
425 '$base' => DI::baseUrl(),
426 '$nickname' => $nickname,
427 '$linkmore' => $total > 5 ? 'true' : '',
428 '$more' => DI::l10n()->t('show more'),
429 '$contacts' => $entries
434 * Insert a tag cloud widget for the present profile.
436 * @param int $limit Max number of displayed tags.
437 * @return string HTML formatted output.
438 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
439 * @throws \ImagickException
441 public static function tagCloud($limit = 50)
445 $uid = intval($a->profile['uid']);
447 if (!$uid || !$a->profile['url']) {
451 if (Feature::isEnabled($uid, 'tagadelic')) {
452 $owner_id = Contact::getIdForURL($a->profile['url'], 0, false);
457 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
464 * @param string $url Base page URL
465 * @param int $uid User ID consulting/publishing posts
466 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
468 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
470 public static function postedByYear(string $url, int $uid, bool $wall)
474 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
476 /* arrange the list in years */
477 $dnow = DateTimeFormat::localNow('Y-m-d');
481 $dthen = Item::firstPostDate($uid, $wall);
483 // Set the start and end date to the beginning of the month
484 $dnow = substr($dnow, 0, 8) . '01';
485 $dthen = substr($dthen, 0, 8) . '01';
488 * Starting with the current month, get the first and last days of every
489 * month down to and including the month of the first post
491 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
492 $dyear = intval(substr($dnow, 0, 4));
493 $dstart = substr($dnow, 0, 8) . '01';
494 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
495 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
496 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
497 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
499 if (empty($ret[$dyear])) {
503 $ret[$dyear][] = [$str, $end_month, $start_month];
504 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
508 if (!DBA::isResult($ret)) {
513 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
514 $cutoff = array_key_exists($cutoff_year, $ret);
516 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
517 '$title' => DI::l10n()->t('Archives'),
518 '$size' => $visible_years,
519 '$cutoff_year' => $cutoff_year,
520 '$cutoff' => $cutoff,
523 '$showmore' => DI::l10n()->t('show more')