3 * @file src/Content/Widget.php
5 namespace Friendica\Content;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\FileTag;
15 use Friendica\Model\GContact;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Util\Proxy as ProxyUtils;
20 use Friendica\Util\Strings;
21 use Friendica\Util\Temporal;
26 * Return the follow widget
28 * @param string $value optional, default empty
30 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
32 public static function follow($value = "")
34 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
35 '$connect' => DI::l10n()->t('Add New Contact'),
36 '$desc' => DI::l10n()->t('Enter address or web location'),
37 '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
39 '$follow' => DI::l10n()->t('Connect')
44 * Return Find People widget
46 public static function findPeople()
48 $global_dir = DI::config()->get('system', 'directory');
50 if (DI::config()->get('system', 'invitation_only')) {
51 $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
52 if ($x || is_site_admin()) {
53 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
54 . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
60 $nv['findpeople'] = DI::l10n()->t('Find People');
61 $nv['desc'] = DI::l10n()->t('Enter name or interest');
62 $nv['label'] = DI::l10n()->t('Connect/Follow');
63 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
64 $nv['findthem'] = DI::l10n()->t('Find');
65 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
66 $nv['similar'] = DI::l10n()->t('Similar Interests');
67 $nv['random'] = DI::l10n()->t('Random Profile');
68 $nv['inv'] = DI::l10n()->t('Invite Friends');
69 $nv['directory'] = DI::l10n()->t('Global Directory');
70 $nv['global_dir'] = $global_dir;
71 $nv['local_directory'] = DI::l10n()->t('Local Directory');
76 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
80 * Return unavailable networks
82 public static function unavailableNetworks()
84 // Always hide content from these networks
85 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
87 if (!Addon::isEnabled("discourse")) {
88 $networks[] = Protocol::DISCOURSE;
91 if (!Addon::isEnabled("statusnet")) {
92 $networks[] = Protocol::STATUSNET;
95 if (!Addon::isEnabled("pumpio")) {
96 $networks[] = Protocol::PUMPIO;
99 if (!Addon::isEnabled("twitter")) {
100 $networks[] = Protocol::TWITTER;
103 if (DI::config()->get("system", "ostatus_disabled")) {
104 $networks[] = Protocol::OSTATUS;
107 if (!DI::config()->get("system", "diaspora_enabled")) {
108 $networks[] = Protocol::DIASPORA;
111 if (!Addon::isEnabled("pnut")) {
112 $networks[] = Protocol::PNUT;
115 if (!sizeof($networks)) {
119 $network_filter = implode("','", $networks);
121 $network_filter = "AND `network` NOT IN ('$network_filter')";
123 return $network_filter;
127 * Display a generic filter widget based on a list of options
129 * The options array must be the following format:
132 * 'ref' => {filter value},
133 * 'name' => {option name}
138 * @param string $type The filter query string key
139 * @param string $title
140 * @param string $desc
141 * @param string $all The no filter label
142 * @param string $baseUrl The full page request URI
143 * @param array $options
144 * @param string $selected The currently selected filter option value
148 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
150 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
154 parse_str($queryString, $queryArray);
155 unset($queryArray[$type]);
157 if (count($queryArray)) {
158 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
160 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
163 $baseUrl = trim($baseUrl, '?') . '?';
166 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
170 '$selected' => $selected,
171 '$all_label' => $all,
172 '$options' => $options,
178 * Return contact relationship widget
180 * @param string $baseurl baseurl
181 * @param string $selected optional, default empty
185 public static function contactRels($baseurl, $selected = '')
192 ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
193 ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
194 ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
199 DI::l10n()->t('Relationships'),
201 DI::l10n()->t('All Contacts'),
209 * Return networks widget
211 * @param string $baseurl baseurl
212 * @param string $selected optional, default empty
214 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
216 public static function networks($baseurl, $selected = '')
222 if (!Feature::isEnabled(local_user(), 'networks')) {
226 $extra_sql = self::unavailableNetworks();
228 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
233 while ($rr = DBA::fetch($r)) {
234 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
238 if (count($nets) < 2) {
244 DI::l10n()->t('Protocols'),
246 DI::l10n()->t('All Protocols'),
254 * Return file as widget
256 * @param string $baseurl baseurl
257 * @param string $selected optional, default empty
258 * @return string|void
261 public static function fileAs($baseurl, $selected = '')
267 $saved = DI::pConfig()->get(local_user(), 'system', 'filetags');
268 if (!strlen($saved)) {
273 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
274 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
277 usort($terms, function ($a, $b) {
278 return strcmp($a['name'], $b['name']);
283 DI::l10n()->t('Saved Folders'),
285 DI::l10n()->t('Everything'),
293 * Return categories widget
295 * @param string $baseurl baseurl
296 * @param string $selected optional, default empty
297 * @return string|void
298 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
300 public static function categories($baseurl, $selected = '')
304 $uid = intval($a->profile['uid']);
306 if (!Feature::isEnabled($uid, 'categories')) {
310 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
311 if (!strlen($saved)) {
316 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
317 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
322 DI::l10n()->t('Categories'),
324 DI::l10n()->t('Everything'),
332 * Return common friends visitor widget
334 * @param string $profile_uid uid
335 * @return string|void
336 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
338 public static function commonFriendsVisitor($profile_uid)
340 if (local_user() == $profile_uid) {
346 $cid = Session::getRemoteContactID($profile_uid);
349 if (Profile::getMyURL()) {
350 $contact = DBA::selectFirst('contact', ['id'],
351 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
352 if (DBA::isResult($contact)) {
353 $cid = $contact['id'];
355 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
356 if (DBA::isResult($gcontact)) {
357 $zcid = $gcontact['id'];
363 if ($cid == 0 && $zcid == 0) {
368 $t = GContact::countCommonFriends($profile_uid, $cid);
370 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
378 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
380 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
383 if (!DBA::isResult($r)) {
388 foreach ($r as $rr) {
390 'url' => Contact::magicLink($rr['url']),
391 'name' => $rr['name'],
392 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
397 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
398 return Renderer::replaceMacros($tpl, [
399 '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $t),
400 '$base' => DI::baseUrl(),
401 '$uid' => $profile_uid,
402 '$cid' => (($cid) ? $cid : '0'),
403 '$linkmore' => (($t > 5) ? 'true' : ''),
404 '$more' => DI::l10n()->t('show more'),
410 * Insert a tag cloud widget for the present profile.
412 * @param int $limit Max number of displayed tags.
413 * @return string HTML formatted output.
414 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
415 * @throws \ImagickException
417 public static function tagCloud($limit = 50)
421 $uid = intval($a->profile['uid']);
423 if (!$uid || !$a->profile['url']) {
427 if (Feature::isEnabled($uid, 'tagadelic')) {
428 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
433 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
440 * @param string $url Base page URL
441 * @param int $uid User ID consulting/publishing posts
442 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
444 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
446 public static function postedByYear(string $url, int $uid, bool $wall)
450 if (!Feature::isEnabled($uid, 'archives')) {
454 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
456 /* arrange the list in years */
457 $dnow = DateTimeFormat::localNow('Y-m-d');
461 $dthen = Item::firstPostDate($uid, $wall);
463 // Set the start and end date to the beginning of the month
464 $dnow = substr($dnow, 0, 8) . '01';
465 $dthen = substr($dthen, 0, 8) . '01';
468 * Starting with the current month, get the first and last days of every
469 * month down to and including the month of the first post
471 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
472 $dyear = intval(substr($dnow, 0, 4));
473 $dstart = substr($dnow, 0, 8) . '01';
474 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
475 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
476 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
477 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
479 if (empty($ret[$dyear])) {
483 $ret[$dyear][] = [$str, $end_month, $start_month];
484 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
488 if (!DBA::isResult($ret)) {
493 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
494 $cutoff = array_key_exists($cutoff_year, $ret);
496 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
497 '$title' => DI::l10n()->t('Archives'),
498 '$size' => $visible_years,
499 '$cutoff_year' => $cutoff_year,
500 '$cutoff' => $cutoff,
503 '$showmore' => DI::l10n()->t('show more')