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('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('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 * Return networks widget
126 * @param string $baseurl baseurl
127 * @param string $selected optional, default empty
129 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
131 public static function networks($baseurl, $selected = '')
137 if (!Feature::isEnabled(local_user(), 'networks')) {
141 $extra_sql = self::unavailableNetworks();
143 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
148 while ($rr = DBA::fetch($r)) {
149 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
153 if (count($nets) < 2) {
157 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
158 '$title' => L10n::t('Protocols'),
160 '$sel_all' => (($selected == '') ? 'selected' : ''),
161 '$all' => L10n::t('All Protocols'),
168 * Return file as widget
170 * @param string $baseurl baseurl
171 * @param string $selected optional, default empty
172 * @return string|void
173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
175 public static function fileAs($baseurl, $selected = '')
181 $saved = PConfig::get(local_user(), 'system', 'filetags');
182 if (!strlen($saved)) {
188 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
190 foreach ($matches as $mtch)
192 $unescaped = XML::escape(FileTag::decode($mtch[1]));
193 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
197 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
198 '$title' => L10n::t('Saved Folders'),
200 '$sel_all' => (($selected == '') ? 'selected' : ''),
201 '$all' => L10n::t('Everything'),
208 * Return categories widget
210 * @param string $baseurl baseurl
211 * @param string $selected optional, default empty
212 * @return string|void
213 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
215 public static function categories($baseurl, $selected = '')
219 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
223 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
224 if (!strlen($saved)) {
230 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
233 foreach ($matches as $mtch) {
234 $unescaped = XML::escape(FileTag::decode($mtch[1]));
235 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
239 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
240 '$title' => L10n::t('Categories'),
242 '$sel_all' => (($selected == '') ? 'selected' : ''),
243 '$all' => L10n::t('Everything'),
250 * Return common friends visitor widget
252 * @param string $profile_uid uid
253 * @return string|void
254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
256 public static function commonFriendsVisitor($profile_uid)
258 if (local_user() == $profile_uid) {
264 if (!empty($_SESSION['remote'])) {
265 foreach ($_SESSION['remote'] as $visitor) {
266 if ($visitor['uid'] == $profile_uid) {
267 $cid = $visitor['cid'];
274 if (Profile::getMyURL()) {
275 $contact = DBA::selectFirst('contact', ['id'],
276 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
277 if (DBA::isResult($contact)) {
278 $cid = $contact['id'];
280 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
281 if (DBA::isResult($gcontact)) {
282 $zcid = $gcontact['id'];
288 if ($cid == 0 && $zcid == 0) {
293 $t = GContact::countCommonFriends($profile_uid, $cid);
295 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
303 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
305 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
308 if (!DBA::isResult($r)) {
313 foreach ($r as $rr) {
315 'url' => Contact::magicLink($rr['url']),
316 'name' => $rr['name'],
317 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
322 $tpl = Renderer::getMarkupTemplate('remote_friends_common.tpl');
323 return Renderer::replaceMacros($tpl, [
324 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
325 '$base' => System::baseUrl(),
326 '$uid' => $profile_uid,
327 '$cid' => (($cid) ? $cid : '0'),
328 '$linkmore' => (($t > 5) ? 'true' : ''),
329 '$more' => L10n::t('show more'),
335 * Insert a tag cloud widget for the present profile.
337 * @brief Insert a tag cloud widget for the present profile.
338 * @param int $limit Max number of displayed tags.
339 * @return string HTML formatted output.
340 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341 * @throws \ImagickException
343 public static function tagCloud($limit = 50)
347 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
351 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
352 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
357 return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');