3 * @file src/Content/Widget.php
5 namespace Friendica\Content;
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\Session;
14 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\FileTag;
18 use Friendica\Model\GContact;
19 use Friendica\Model\Item;
20 use Friendica\Model\Profile;
21 use Friendica\Util\DateTimeFormat;
22 use Friendica\Util\Proxy as ProxyUtils;
23 use Friendica\Util\Strings;
24 use Friendica\Util\Temporal;
29 * Return the follow widget
31 * @param string $value optional, default empty
33 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
35 public static function follow($value = "")
37 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
38 '$connect' => L10n::t('Add New Contact'),
39 '$desc' => L10n::t('Enter address or web location'),
40 '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
42 '$follow' => L10n::t('Connect')
47 * Return Find People widget
49 public static function findPeople()
51 $global_dir = Config::get('system', 'directory');
53 if (Config::get('system', 'invitation_only')) {
54 $x = intval(PConfig::get(local_user(), 'system', 'invites_remaining'));
55 if ($x || is_site_admin()) {
56 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
57 . L10n::tt('%d invitation available', '%d invitations available', $x)
63 $nv['findpeople'] = L10n::t('Find People');
64 $nv['desc'] = L10n::t('Enter name or interest');
65 $nv['label'] = L10n::t('Connect/Follow');
66 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
67 $nv['findthem'] = L10n::t('Find');
68 $nv['suggest'] = L10n::t('Friend Suggestions');
69 $nv['similar'] = L10n::t('Similar Interests');
70 $nv['random'] = L10n::t('Random Profile');
71 $nv['inv'] = L10n::t('Invite Friends');
72 $nv['directory'] = L10n::t('Global Directory');
73 $nv['global_dir'] = $global_dir;
74 $nv['local_directory'] = L10n::t('Local Directory');
79 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
83 * Return unavailable networks
85 public static function unavailableNetworks()
87 // Always hide content from these networks
88 $networks = ['face', 'apdn'];
90 if (!Addon::isEnabled("discourse")) {
91 $networks[] = Protocol::DISCOURSE;
94 if (!Addon::isEnabled("statusnet")) {
95 $networks[] = Protocol::STATUSNET;
98 if (!Addon::isEnabled("pumpio")) {
99 $networks[] = Protocol::PUMPIO;
102 if (!Addon::isEnabled("twitter")) {
103 $networks[] = Protocol::TWITTER;
106 if (Config::get("system", "ostatus_disabled")) {
107 $networks[] = Protocol::OSTATUS;
110 if (!Config::get("system", "diaspora_enabled")) {
111 $networks[] = Protocol::DIASPORA;
114 if (!Addon::isEnabled("pnut")) {
115 $networks[] = Protocol::PNUT;
118 if (!sizeof($networks)) {
122 $network_filter = implode("','", $networks);
124 $network_filter = "AND `network` NOT IN ('$network_filter')";
126 return $network_filter;
130 * Display a generic filter widget based on a list of options
132 * The options array must be the following format:
135 * 'ref' => {filter value},
136 * 'name' => {option name}
141 * @param string $type The filter query string key
142 * @param string $title
143 * @param string $desc
144 * @param string $all The no filter label
145 * @param string $baseUrl The full page request URI
146 * @param array $options
147 * @param string $selected The currently selected filter option value
151 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
153 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
157 parse_str($queryString, $queryArray);
158 unset($queryArray[$type]);
160 if (count($queryArray)) {
161 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
163 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
166 $baseUrl = trim($baseUrl, '?') . '?';
169 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
173 '$selected' => $selected,
174 '$all_label' => $all,
175 '$options' => $options,
181 * Return networks widget
183 * @param string $baseurl baseurl
184 * @param string $selected optional, default empty
188 public static function contactRels($baseurl, $selected = '')
195 ['ref' => 'followers', 'name' => L10n::t('Followers')],
196 ['ref' => 'following', 'name' => L10n::t('Following')],
197 ['ref' => 'mutuals', 'name' => L10n::t('Mutual friends')],
202 L10n::t('Relationships'),
204 L10n::t('All Contacts'),
212 * Return networks widget
214 * @param string $baseurl baseurl
215 * @param string $selected optional, default empty
217 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
219 public static function networks($baseurl, $selected = '')
225 if (!Feature::isEnabled(local_user(), 'networks')) {
229 $extra_sql = self::unavailableNetworks();
231 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
236 while ($rr = DBA::fetch($r)) {
237 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
241 if (count($nets) < 2) {
247 L10n::t('Protocols'),
249 L10n::t('All Protocols'),
257 * Return file as widget
259 * @param string $baseurl baseurl
260 * @param string $selected optional, default empty
261 * @return string|void
264 public static function fileAs($baseurl, $selected = '')
270 $saved = PConfig::get(local_user(), 'system', 'filetags');
271 if (!strlen($saved)) {
276 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
277 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
280 usort($terms, function ($a, $b) {
281 return strcmp($a['name'], $b['name']);
286 L10n::t('Saved Folders'),
288 L10n::t('Everything'),
296 * Return categories widget
298 * @param string $baseurl baseurl
299 * @param string $selected optional, default empty
300 * @return string|void
301 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
303 public static function categories($baseurl, $selected = '')
307 $uid = intval($a->profile['profile_uid']);
309 if (!Feature::isEnabled($uid, 'categories')) {
313 $saved = PConfig::get($uid, 'system', 'filetags');
314 if (!strlen($saved)) {
319 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
320 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
325 L10n::t('Categories'),
327 L10n::t('Everything'),
335 * Return common friends visitor widget
337 * @param string $profile_uid uid
338 * @return string|void
339 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341 public static function commonFriendsVisitor($profile_uid)
343 if (local_user() == $profile_uid) {
349 $cid = Session::getRemoteContactID($profile_uid);
352 if (Profile::getMyURL()) {
353 $contact = DBA::selectFirst('contact', ['id'],
354 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
355 if (DBA::isResult($contact)) {
356 $cid = $contact['id'];
358 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
359 if (DBA::isResult($gcontact)) {
360 $zcid = $gcontact['id'];
366 if ($cid == 0 && $zcid == 0) {
371 $t = GContact::countCommonFriends($profile_uid, $cid);
373 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
381 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
383 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
386 if (!DBA::isResult($r)) {
391 foreach ($r as $rr) {
393 'url' => Contact::magicLink($rr['url']),
394 'name' => $rr['name'],
395 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
400 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
401 return Renderer::replaceMacros($tpl, [
402 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
403 '$base' => DI::baseUrl(),
404 '$uid' => $profile_uid,
405 '$cid' => (($cid) ? $cid : '0'),
406 '$linkmore' => (($t > 5) ? 'true' : ''),
407 '$more' => L10n::t('show more'),
413 * Insert a tag cloud widget for the present profile.
415 * @brief Insert a tag cloud widget for the present profile.
416 * @param int $limit Max number of displayed tags.
417 * @return string HTML formatted output.
418 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
419 * @throws \ImagickException
421 public static function tagCloud($limit = 50)
425 $uid = intval($a->profile['profile_uid']);
427 if (!$uid || !$a->profile['url']) {
431 if (Feature::isEnabled($uid, 'tagadelic')) {
432 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
437 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
444 * @param string $url Base page URL
445 * @param int $uid User ID consulting/publishing posts
446 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
448 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
450 public static function postedByYear(string $url, int $uid, bool $wall)
454 if (!Feature::isEnabled($uid, 'archives')) {
458 $visible_years = PConfig::get($uid, 'system', 'archive_visible_years', 5);
460 /* arrange the list in years */
461 $dnow = DateTimeFormat::localNow('Y-m-d');
465 $dthen = Item::firstPostDate($uid, $wall);
467 // Set the start and end date to the beginning of the month
468 $dnow = substr($dnow, 0, 8) . '01';
469 $dthen = substr($dthen, 0, 8) . '01';
472 * Starting with the current month, get the first and last days of every
473 * month down to and including the month of the first post
475 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
476 $dyear = intval(substr($dnow, 0, 4));
477 $dstart = substr($dnow, 0, 8) . '01';
478 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
479 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
480 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
481 $str = L10n::getDay(DateTimeFormat::utc($dnow, 'F'));
483 if (empty($ret[$dyear])) {
487 $ret[$dyear][] = [$str, $end_month, $start_month];
488 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
492 if (!DBA::isResult($ret)) {
497 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
498 $cutoff = array_key_exists($cutoff_year, $ret);
500 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
501 '$title' => L10n::t('Archives'),
502 '$size' => $visible_years,
503 '$cutoff_year' => $cutoff_year,
504 '$cutoff' => $cutoff,
507 '$showmore' => L10n::t('show more')