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 if (!Feature::isEnabled(local_user(), 'networks')) {
276 $extra_sql = self::unavailableNetworks();
278 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
283 while ($rr = DBA::fetch($r)) {
284 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
288 if (count($nets) < 2) {
294 DI::l10n()->t('Protocols'),
296 DI::l10n()->t('All Protocols'),
304 * Return file as widget
306 * @param string $baseurl baseurl
307 * @param string $selected optional, default empty
308 * @return string|void
311 public static function fileAs($baseurl, $selected = '')
317 $saved = DI::pConfig()->get(local_user(), 'system', 'filetags');
318 if (!strlen($saved)) {
323 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
324 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
327 usort($terms, function ($a, $b) {
328 return strcmp($a['name'], $b['name']);
333 DI::l10n()->t('Saved Folders'),
335 DI::l10n()->t('Everything'),
343 * Return categories widget
345 * @param string $baseurl baseurl
346 * @param string $selected optional, default empty
347 * @return string|void
348 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
350 public static function categories($baseurl, $selected = '')
354 $uid = intval($a->profile['uid']);
356 if (!Feature::isEnabled($uid, 'categories')) {
360 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
361 if (!strlen($saved)) {
366 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
367 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
372 DI::l10n()->t('Categories'),
374 DI::l10n()->t('Everything'),
382 * Return common friends visitor widget
384 * @param string $profile_uid uid
385 * @return string|void
386 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
388 public static function commonFriendsVisitor($profile_uid)
390 if (local_user() == $profile_uid) {
396 $cid = Session::getRemoteContactID($profile_uid);
399 if (Profile::getMyURL()) {
400 $contact = DBA::selectFirst('contact', ['id'],
401 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
402 if (DBA::isResult($contact)) {
403 $cid = $contact['id'];
405 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
406 if (DBA::isResult($gcontact)) {
407 $zcid = $gcontact['id'];
413 if ($cid == 0 && $zcid == 0) {
418 $t = GContact::countCommonFriends($profile_uid, $cid);
420 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
428 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
430 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
433 if (!DBA::isResult($r)) {
438 foreach ($r as $rr) {
440 'url' => Contact::magicLink($rr['url']),
441 'name' => $rr['name'],
442 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
447 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
448 return Renderer::replaceMacros($tpl, [
449 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $t),
450 '$base' => DI::baseUrl(),
451 '$uid' => $profile_uid,
452 '$cid' => (($cid) ? $cid : '0'),
453 '$linkmore' => (($t > 5) ? 'true' : ''),
454 '$more' => DI::l10n()->t('show more'),
460 * Insert a tag cloud widget for the present profile.
462 * @param int $limit Max number of displayed tags.
463 * @return string HTML formatted output.
464 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
465 * @throws \ImagickException
467 public static function tagCloud($limit = 50)
471 $uid = intval($a->profile['uid']);
473 if (!$uid || !$a->profile['url']) {
477 if (Feature::isEnabled($uid, 'tagadelic')) {
478 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
483 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
490 * @param string $url Base page URL
491 * @param int $uid User ID consulting/publishing posts
492 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
494 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
496 public static function postedByYear(string $url, int $uid, bool $wall)
500 if (!Feature::isEnabled($uid, 'archives')) {
504 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
506 /* arrange the list in years */
507 $dnow = DateTimeFormat::localNow('Y-m-d');
511 $dthen = Item::firstPostDate($uid, $wall);
513 // Set the start and end date to the beginning of the month
514 $dnow = substr($dnow, 0, 8) . '01';
515 $dthen = substr($dthen, 0, 8) . '01';
518 * Starting with the current month, get the first and last days of every
519 * month down to and including the month of the first post
521 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
522 $dyear = intval(substr($dnow, 0, 4));
523 $dstart = substr($dnow, 0, 8) . '01';
524 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
525 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
526 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
527 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
529 if (empty($ret[$dyear])) {
533 $ret[$dyear][] = [$str, $end_month, $start_month];
534 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
538 if (!DBA::isResult($ret)) {
543 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
544 $cutoff = array_key_exists($cutoff_year, $ret);
546 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
547 '$title' => DI::l10n()->t('Archives'),
548 '$size' => $visible_years,
549 '$cutoff_year' => $cutoff_year,
550 '$cutoff' => $cutoff,
553 '$showmore' => DI::l10n()->t('show more')