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\System;
14 use Friendica\Core\Session;
15 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;
25 use Friendica\Util\XML;
30 * Return the follow widget
32 * @param string $value optional, default empty
34 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
36 public static function follow($value = "")
38 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
39 '$connect' => L10n::t('Add New Contact'),
40 '$desc' => L10n::t('Enter address or web location'),
41 '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
43 '$follow' => L10n::t('Connect')
48 * Return Find People widget
50 public static function findPeople()
53 $global_dir = Config::get('system', 'directory');
55 if (Config::get('system', 'invitation_only')) {
56 $x = intval(PConfig::get(local_user(), 'system', 'invites_remaining'));
57 if ($x || is_site_admin()) {
58 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
59 . L10n::tt('%d invitation available', '%d invitations available', $x)
65 $nv['findpeople'] = L10n::t('Find People');
66 $nv['desc'] = L10n::t('Enter name or interest');
67 $nv['label'] = L10n::t('Connect/Follow');
68 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
69 $nv['findthem'] = L10n::t('Find');
70 $nv['suggest'] = L10n::t('Friend Suggestions');
71 $nv['similar'] = L10n::t('Similar Interests');
72 $nv['random'] = L10n::t('Random Profile');
73 $nv['inv'] = L10n::t('Invite Friends');
74 $nv['directory'] = L10n::t('Global Directory');
75 $nv['global_dir'] = $global_dir;
76 $nv['local_directory'] = L10n::t('Local Directory');
81 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
85 * Return unavailable networks
87 public static function unavailableNetworks()
89 // Always hide content from these networks
90 $networks = ['face', 'apdn'];
92 if (!Addon::isEnabled("statusnet")) {
93 $networks[] = Protocol::STATUSNET;
96 if (!Addon::isEnabled("pumpio")) {
97 $networks[] = Protocol::PUMPIO;
100 if (!Addon::isEnabled("twitter")) {
101 $networks[] = Protocol::TWITTER;
104 if (Config::get("system", "ostatus_disabled")) {
105 $networks[] = Protocol::OSTATUS;
108 if (!Config::get("system", "diaspora_enabled")) {
109 $networks[] = Protocol::DIASPORA;
112 if (!Addon::isEnabled("pnut")) {
113 $networks[] = Protocol::PNUT;
116 if (!sizeof($networks)) {
120 $network_filter = implode("','", $networks);
122 $network_filter = "AND `network` NOT IN ('$network_filter')";
124 return $network_filter;
128 * Display a generic filter widget based on a list of options
130 * The options array must be the following format:
133 * 'ref' => {filter value},
134 * 'name' => {option name}
139 * @param string $type The filter query string key
140 * @param string $title
141 * @param string $desc
142 * @param string $all The no filter label
143 * @param string $baseUrl The full page request URI
144 * @param array $options
145 * @param string $selected The currently selected filter option value
149 private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
151 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
155 parse_str($queryString, $queryArray);
156 unset($queryArray[$type]);
158 if (count($queryArray)) {
159 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
161 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
164 $baseUrl = trim($baseUrl, '?') . '?';
167 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
171 '$selected' => $selected,
172 '$all_label' => $all,
173 '$options' => $options,
179 * Return networks widget
181 * @param string $baseurl baseurl
182 * @param string $selected optional, default empty
186 public static function contactRels($baseurl, $selected = '')
193 ['ref' => 'followers', 'name' => L10n::t('Followers')],
194 ['ref' => 'following', 'name' => L10n::t('Following')],
195 ['ref' => 'mutuals', 'name' => L10n::t('Mutual friends')],
200 L10n::t('Relationships'),
202 L10n::t('All Contacts'),
210 * Return networks widget
212 * @param string $baseurl baseurl
213 * @param string $selected optional, default empty
215 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217 public static function networks($baseurl, $selected = '')
223 if (!Feature::isEnabled(local_user(), 'networks')) {
227 $extra_sql = self::unavailableNetworks();
229 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
234 while ($rr = DBA::fetch($r)) {
235 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
239 if (count($nets) < 2) {
245 L10n::t('Protocols'),
247 L10n::t('All Protocols'),
255 * Return file as widget
257 * @param string $baseurl baseurl
258 * @param string $selected optional, default empty
259 * @return string|void
262 public static function fileAs($baseurl, $selected = '')
268 $saved = PConfig::get(local_user(), 'system', 'filetags');
269 if (!strlen($saved)) {
274 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
275 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
280 L10n::t('Saved Folders'),
282 L10n::t('Everything'),
290 * Return categories widget
292 * @param string $baseurl baseurl
293 * @param string $selected optional, default empty
294 * @return string|void
295 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
297 public static function categories($baseurl, $selected = '')
301 $uid = intval($a->profile['profile_uid']);
303 if (!Feature::isEnabled($uid, 'categories')) {
307 $saved = PConfig::get($uid, 'system', 'filetags');
308 if (!strlen($saved)) {
313 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
314 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
319 L10n::t('Categories'),
321 L10n::t('Everything'),
329 * Return common friends visitor widget
331 * @param string $profile_uid uid
332 * @return string|void
333 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
335 public static function commonFriendsVisitor($profile_uid)
337 if (local_user() == $profile_uid) {
343 $cid = Session::getRemoteContactID($profile_uid);
346 if (Profile::getMyURL()) {
347 $contact = DBA::selectFirst('contact', ['id'],
348 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
349 if (DBA::isResult($contact)) {
350 $cid = $contact['id'];
352 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
353 if (DBA::isResult($gcontact)) {
354 $zcid = $gcontact['id'];
360 if ($cid == 0 && $zcid == 0) {
365 $t = GContact::countCommonFriends($profile_uid, $cid);
367 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
375 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
377 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
380 if (!DBA::isResult($r)) {
385 foreach ($r as $rr) {
387 'url' => Contact::magicLink($rr['url']),
388 'name' => $rr['name'],
389 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
394 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
395 return Renderer::replaceMacros($tpl, [
396 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
397 '$base' => System::baseUrl(),
398 '$uid' => $profile_uid,
399 '$cid' => (($cid) ? $cid : '0'),
400 '$linkmore' => (($t > 5) ? 'true' : ''),
401 '$more' => L10n::t('show more'),
407 * Insert a tag cloud widget for the present profile.
409 * @brief Insert a tag cloud widget for the present profile.
410 * @param int $limit Max number of displayed tags.
411 * @return string HTML formatted output.
412 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
413 * @throws \ImagickException
415 public static function tagCloud($limit = 50)
419 $uid = intval($a->profile['profile_uid']);
421 if (!$uid || !$a->profile['url']) {
425 if (Feature::isEnabled($uid, 'tagadelic')) {
426 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
431 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
438 * @param string $url Base page URL
439 * @param int $uid User ID consulting/publishing posts
440 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
442 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
444 public static function postedByYear(string $url, int $uid, bool $wall)
448 if (!Feature::isEnabled($uid, 'archives')) {
452 $visible_years = PConfig::get($uid, 'system', 'archive_visible_years', 5);
454 /* arrange the list in years */
455 $dnow = DateTimeFormat::localNow('Y-m-d');
459 $dthen = Item::firstPostDate($uid, $wall);
461 // Set the start and end date to the beginning of the month
462 $dnow = substr($dnow, 0, 8) . '01';
463 $dthen = substr($dthen, 0, 8) . '01';
466 * Starting with the current month, get the first and last days of every
467 * month down to and including the month of the first post
469 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
470 $dyear = intval(substr($dnow, 0, 4));
471 $dstart = substr($dnow, 0, 8) . '01';
472 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
473 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
474 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
475 $str = L10n::getDay(DateTimeFormat::utc($dnow, 'F'));
477 if (empty($ret[$dyear])) {
481 $ret[$dyear][] = [$str, $end_month, $start_month];
482 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
486 if (!DBA::isResult($ret)) {
491 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
492 $cutoff = array_key_exists($cutoff_year, $ret);
494 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
495 '$title' => L10n::t('Archives'),
496 '$size' => $visible_years,
497 '$cutoff_year' => $cutoff_year,
498 '$cutoff' => $cutoff,
501 '$showmore' => L10n::t('show more')