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\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\FileTag;
17 use Friendica\Model\GContact;
18 use Friendica\Model\Item;
19 use Friendica\Model\Profile;
20 use Friendica\Util\DateTimeFormat;
21 use Friendica\Util\Proxy as ProxyUtils;
22 use Friendica\Util\Strings;
23 use Friendica\Util\Temporal;
24 use Friendica\Util\XML;
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()
52 $global_dir = Config::get('system', 'directory');
54 if (Config::get('system', 'invitation_only')) {
55 $x = intval(PConfig::get(local_user(), 'system', 'invites_remaining'));
56 if ($x || is_site_admin()) {
57 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
58 . L10n::tt('%d invitation available', '%d invitations available', $x)
64 $nv['findpeople'] = L10n::t('Find People');
65 $nv['desc'] = L10n::t('Enter name or interest');
66 $nv['label'] = L10n::t('Connect/Follow');
67 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
68 $nv['findthem'] = L10n::t('Find');
69 $nv['suggest'] = L10n::t('Friend Suggestions');
70 $nv['similar'] = L10n::t('Similar Interests');
71 $nv['random'] = L10n::t('Random Profile');
72 $nv['inv'] = L10n::t('Invite Friends');
73 $nv['directory'] = L10n::t('Global Directory');
74 $nv['global_dir'] = $global_dir;
75 $nv['local_directory'] = L10n::t('Local Directory');
80 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
84 * Return unavailable networks
86 public static function unavailableNetworks()
88 // Always hide content from these networks
89 $networks = ['face', 'apdn'];
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 (Config::get("system", "ostatus_disabled")) {
104 $networks[] = Protocol::OSTATUS;
107 if (!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 networks widget
180 * @param string $baseurl baseurl
181 * @param string $selected optional, default empty
185 public static function contactRels($baseurl, $selected = '')
192 ['ref' => 'followers', 'name' => L10n::t('Followers')],
193 ['ref' => 'following', 'name' => L10n::t('Following')],
194 ['ref' => 'mutuals', 'name' => L10n::t('Mutual friends')],
199 L10n::t('Relationships'),
201 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 L10n::t('Protocols'),
246 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 = PConfig::get(local_user(), 'system', 'filetags');
268 if (!strlen($saved)) {
273 foreach (FileTag::fileToArray($saved) as $savedFolderName) {
274 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
279 L10n::t('Saved Folders'),
281 L10n::t('Everything'),
289 * Return categories widget
291 * @param string $baseurl baseurl
292 * @param string $selected optional, default empty
293 * @return string|void
294 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
296 public static function categories($baseurl, $selected = '')
300 $uid = intval($a->profile['profile_uid']);
302 if (!Feature::isEnabled($uid, 'categories')) {
306 $saved = PConfig::get($uid, 'system', 'filetags');
307 if (!strlen($saved)) {
312 foreach (FileTag::fileToArray($saved, 'category') as $savedFolderName) {
313 $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
318 L10n::t('Categories'),
320 L10n::t('Everything'),
328 * Return common friends visitor widget
330 * @param string $profile_uid uid
331 * @return string|void
332 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
334 public static function commonFriendsVisitor($profile_uid)
336 if (local_user() == $profile_uid) {
342 $cid = remote_user($profile_uid);
345 if (Profile::getMyURL()) {
346 $contact = DBA::selectFirst('contact', ['id'],
347 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
348 if (DBA::isResult($contact)) {
349 $cid = $contact['id'];
351 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
352 if (DBA::isResult($gcontact)) {
353 $zcid = $gcontact['id'];
359 if ($cid == 0 && $zcid == 0) {
364 $t = GContact::countCommonFriends($profile_uid, $cid);
366 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
374 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
376 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
379 if (!DBA::isResult($r)) {
384 foreach ($r as $rr) {
386 'url' => Contact::magicLink($rr['url']),
387 'name' => $rr['name'],
388 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
393 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
394 return Renderer::replaceMacros($tpl, [
395 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
396 '$base' => System::baseUrl(),
397 '$uid' => $profile_uid,
398 '$cid' => (($cid) ? $cid : '0'),
399 '$linkmore' => (($t > 5) ? 'true' : ''),
400 '$more' => L10n::t('show more'),
406 * Insert a tag cloud widget for the present profile.
408 * @brief Insert a tag cloud widget for the present profile.
409 * @param int $limit Max number of displayed tags.
410 * @return string HTML formatted output.
411 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
412 * @throws \ImagickException
414 public static function tagCloud($limit = 50)
418 $uid = intval($a->profile['profile_uid']);
420 if (!$uid || !$a->profile['url']) {
424 if (Feature::isEnabled($uid, 'tagadelic')) {
425 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
430 return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
437 * @param string $url Base page URL
438 * @param int $uid User ID consulting/publishing posts
439 * @param bool $wall True: Posted by User; False: Posted to User (network timeline)
441 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
443 public static function postedByYear(string $url, int $uid, bool $wall)
447 if (!Feature::isEnabled($uid, 'archives')) {
451 $visible_years = PConfig::get($uid, 'system', 'archive_visible_years', 5);
453 /* arrange the list in years */
454 $dnow = DateTimeFormat::localNow('Y-m-d');
458 $dthen = Item::firstPostDate($uid, $wall);
460 // Set the start and end date to the beginning of the month
461 $dnow = substr($dnow, 0, 8) . '01';
462 $dthen = substr($dthen, 0, 8) . '01';
465 * Starting with the current month, get the first and last days of every
466 * month down to and including the month of the first post
468 while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
469 $dyear = intval(substr($dnow, 0, 4));
470 $dstart = substr($dnow, 0, 8) . '01';
471 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
472 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
473 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
474 $str = L10n::getDay(DateTimeFormat::utc($dnow, 'F'));
476 if (empty($ret[$dyear])) {
480 $ret[$dyear][] = [$str, $end_month, $start_month];
481 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
485 if (!DBA::isResult($ret)) {
490 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
491 $cutoff = array_key_exists($cutoff_year, $ret);
493 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
494 '$title' => L10n::t('Archives'),
495 '$size' => $visible_years,
496 '$cutoff_year' => $cutoff_year,
497 '$cutoff' => $cutoff,
500 '$showmore' => L10n::t('show more')