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\Profile;
19 use Friendica\Util\Proxy as ProxyUtils;
20 use Friendica\Util\Strings;
21 use Friendica\Util\XML;
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' => L10n::t('Add New Contact'),
36 '$desc' => L10n::t('Enter address or web location'),
37 '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
39 '$follow' => L10n::t('Connect')
44 * Return Find People widget
46 public static function findPeople()
49 $global_dir = Config::get('system', 'directory');
51 if (Config::get('system', 'invitation_only')) {
52 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
53 if ($x || is_site_admin()) {
54 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
55 . L10n::tt('%d invitation available', '%d invitations available', $x)
61 $nv['findpeople'] = L10n::t('Find People');
62 $nv['desc'] = L10n::t('Enter name or interest');
63 $nv['label'] = L10n::t('Connect/Follow');
64 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
65 $nv['findthem'] = L10n::t('Find');
66 $nv['suggest'] = L10n::t('Friend Suggestions');
67 $nv['similar'] = L10n::t('Similar Interests');
68 $nv['random'] = L10n::t('Random Profile');
69 $nv['inv'] = L10n::t('Invite Friends');
70 $nv['directory'] = L10n::t('Global Directory');
71 $nv['global_dir'] = $global_dir;
72 $nv['local_directory'] = L10n::t('Local Directory');
77 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
81 * Return unavailable networks
83 public static function unavailableNetworks()
85 // Always hide content from these networks
86 $networks = ['face', 'apdn'];
88 if (!Addon::isEnabled("statusnet")) {
89 $networks[] = Protocol::STATUSNET;
92 if (!Addon::isEnabled("pumpio")) {
93 $networks[] = Protocol::PUMPIO;
96 if (!Addon::isEnabled("twitter")) {
97 $networks[] = Protocol::TWITTER;
100 if (Config::get("system", "ostatus_disabled")) {
101 $networks[] = Protocol::OSTATUS;
104 if (!Config::get("system", "diaspora_enabled")) {
105 $networks[] = Protocol::DIASPORA;
108 if (!Addon::isEnabled("pnut")) {
109 $networks[] = Protocol::PNUT;
112 if (!sizeof($networks)) {
116 $network_filter = implode("','", $networks);
118 $network_filter = "AND `network` NOT IN ('$network_filter')";
120 return $network_filter;
124 * @param string $type
125 * @param string $title
126 * @param string $desc
128 * @param string $baseUrl
129 * @param array $options
130 * @param string $selected
134 public static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
136 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
140 parse_str($queryString, $queryArray);
141 unset($queryArray[$type]);
143 if (count($queryArray)) {
144 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
146 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
149 $baseUrl = trim($baseUrl, '?') . '?';
152 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
156 '$selected' => $selected,
157 '$all_label' => $all,
158 '$options' => $options,
164 * Return networks widget
166 * @param string $baseurl baseurl
167 * @param string $selected optional, default empty
169 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
171 public static function networks($baseurl, $selected = '')
177 if (!Feature::isEnabled(local_user(), 'networks')) {
181 $extra_sql = self::unavailableNetworks();
183 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
188 while ($rr = DBA::fetch($r)) {
189 $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
193 if (count($nets) < 2) {
199 L10n::t('Protocols'),
201 L10n::t('All Protocols'),
209 * Return file as widget
211 * @param string $baseurl baseurl
212 * @param string $selected optional, default empty
213 * @return string|void
214 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
216 public static function fileAs($baseurl, $selected = '')
222 $saved = PConfig::get(local_user(), 'system', 'filetags');
223 if (!strlen($saved)) {
229 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
231 foreach ($matches as $mtch)
233 $unescaped = XML::escape(FileTag::decode($mtch[1]));
234 $terms[] = ['ref' => $unescaped, 'name' => $unescaped];
240 L10n::t('Saved Folders'),
242 L10n::t('Everything'),
250 * Return categories widget
252 * @param string $baseurl baseurl
253 * @param string $selected optional, default empty
254 * @return string|void
255 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
257 public static function categories($baseurl, $selected = '')
261 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
265 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
266 if (!strlen($saved)) {
272 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
275 foreach ($matches as $mtch) {
276 $unescaped = XML::escape(FileTag::decode($mtch[1]));
277 $terms[] = ['ref' => $unescaped, 'name' => $unescaped];
283 L10n::t('Categories'),
285 L10n::t('Everything'),
293 * Return common friends visitor widget
295 * @param string $profile_uid uid
296 * @return string|void
297 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
299 public static function commonFriendsVisitor($profile_uid)
301 if (local_user() == $profile_uid) {
307 if (!empty($_SESSION['remote'])) {
308 foreach ($_SESSION['remote'] as $visitor) {
309 if ($visitor['uid'] == $profile_uid) {
310 $cid = $visitor['cid'];
317 if (Profile::getMyURL()) {
318 $contact = DBA::selectFirst('contact', ['id'],
319 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
320 if (DBA::isResult($contact)) {
321 $cid = $contact['id'];
323 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
324 if (DBA::isResult($gcontact)) {
325 $zcid = $gcontact['id'];
331 if ($cid == 0 && $zcid == 0) {
336 $t = GContact::countCommonFriends($profile_uid, $cid);
338 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
346 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
348 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
351 if (!DBA::isResult($r)) {
356 foreach ($r as $rr) {
358 'url' => Contact::magicLink($rr['url']),
359 'name' => $rr['name'],
360 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
365 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
366 return Renderer::replaceMacros($tpl, [
367 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
368 '$base' => System::baseUrl(),
369 '$uid' => $profile_uid,
370 '$cid' => (($cid) ? $cid : '0'),
371 '$linkmore' => (($t > 5) ? 'true' : ''),
372 '$more' => L10n::t('show more'),
378 * Insert a tag cloud widget for the present profile.
380 * @brief Insert a tag cloud widget for the present profile.
381 * @param int $limit Max number of displayed tags.
382 * @return string HTML formatted output.
383 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
384 * @throws \ImagickException
386 public static function tagCloud($limit = 50)
390 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
394 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
395 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
400 return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');