3 * @file src/Content/Widget.php
5 namespace Friendica\Content;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Feature;
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Contact;
18 use Friendica\Model\FileTag;
19 use Friendica\Model\GContact;
20 use Friendica\Model\Profile;
21 use Friendica\Util\Proxy as ProxyUtils;
22 use Friendica\Util\Strings;
23 use Friendica\Util\XML;
28 * Return the follow widget
30 * @param string $value optional, default empty
32 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
34 public static function follow($value = "")
36 return Renderer::replaceMacros(Renderer::getMarkupTemplate('follow.tpl'), array(
37 '$connect' => L10n::t('Add New Contact'),
38 '$desc' => L10n::t('Enter address or web location'),
39 '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
41 '$follow' => L10n::t('Connect')
46 * Return Find People widget
48 public static function findPeople()
51 $global_dir = Config::get('system', 'directory');
53 if (Config::get('system', 'invitation_only')) {
54 $x = PConfig::get(local_user(), 'system', 'invites_remaining');
55 if ($x || is_site_admin()) {
56 $a->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('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("statusnet")) {
91 $networks[] = Protocol::STATUSNET;
94 if (!Addon::isEnabled("pumpio")) {
95 $networks[] = Protocol::PUMPIO;
98 if (!Addon::isEnabled("twitter")) {
99 $networks[] = Protocol::TWITTER;
102 if (Config::get("system", "ostatus_disabled")) {
103 $networks[] = Protocol::OSTATUS;
106 if (!Config::get("system", "diaspora_enabled")) {
107 $networks[] = Protocol::DIASPORA;
110 if (!Addon::isEnabled("pnut")) {
111 $networks[] = Protocol::PNUT;
114 if (!sizeof($networks)) {
118 $network_filter = implode("','", $networks);
120 $network_filter = "AND `network` NOT IN ('$network_filter')";
122 return $network_filter;
126 * Return networks widget
128 * @param string $baseurl baseurl
129 * @param string $selected optional, default empty
131 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
133 public static function networks($baseurl, $selected = '')
139 if (!Feature::isEnabled(local_user(), 'networks')) {
143 $extra_sql = self::unavailableNetworks();
145 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
150 while ($rr = DBA::fetch($r)) {
151 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
155 if (count($nets) < 2) {
159 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
160 '$title' => L10n::t('Protocols'),
162 '$sel_all' => (($selected == '') ? 'selected' : ''),
163 '$all' => L10n::t('All Protocols'),
170 * Return file as widget
172 * @param string $baseurl baseurl
173 * @param string $selected optional, default empty
174 * @return string|void
175 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177 public static function fileAs($baseurl, $selected = '')
183 $saved = PConfig::get(local_user(), 'system', 'filetags');
184 if (!strlen($saved)) {
190 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
192 foreach ($matches as $mtch)
194 $unescaped = XML::escape(FileTag::decode($mtch[1]));
195 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
199 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
200 '$title' => L10n::t('Saved Folders'),
202 '$sel_all' => (($selected == '') ? 'selected' : ''),
203 '$all' => L10n::t('Everything'),
210 * Return categories widget
212 * @param string $baseurl baseurl
213 * @param string $selected optional, default empty
214 * @return string|void
215 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
217 public static function categories($baseurl, $selected = '')
221 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
225 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
226 if (!strlen($saved)) {
232 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
235 foreach ($matches as $mtch) {
236 $unescaped = XML::escape(FileTag::decode($mtch[1]));
237 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
241 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
242 '$title' => L10n::t('Categories'),
244 '$sel_all' => (($selected == '') ? 'selected' : ''),
245 '$all' => L10n::t('Everything'),
252 * Return common friends visitor widget
254 * @param string $profile_uid uid
255 * @return string|void
256 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
258 public static function commonFriendsVisitor($profile_uid)
260 if (local_user() == $profile_uid) {
266 if (!empty($_SESSION['remote'])) {
267 foreach ($_SESSION['remote'] as $visitor) {
268 if ($visitor['uid'] == $profile_uid) {
269 $cid = $visitor['cid'];
276 if (Profile::getMyURL()) {
277 $contact = DBA::selectFirst('contact', ['id'],
278 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
279 if (DBA::isResult($contact)) {
280 $cid = $contact['id'];
282 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
283 if (DBA::isResult($gcontact)) {
284 $zcid = $gcontact['id'];
290 if ($cid == 0 && $zcid == 0) {
295 $t = GContact::countCommonFriends($profile_uid, $cid);
297 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
305 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
307 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
310 if (!DBA::isResult($r)) {
315 foreach ($r as $rr) {
317 'url' => Contact::magicLink($rr['url']),
318 'name' => $rr['name'],
319 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
324 $tpl = Renderer::getMarkupTemplate('remote_friends_common.tpl');
325 return Renderer::replaceMacros($tpl, [
326 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
327 '$base' => System::baseUrl(),
328 '$uid' => $profile_uid,
329 '$cid' => (($cid) ? $cid : '0'),
330 '$linkmore' => (($t > 5) ? 'true' : ''),
331 '$more' => L10n::t('show more'),
337 * Insert a tag cloud widget for the present profile.
339 * @brief Insert a tag cloud widget for the present profile.
340 * @param int $limit Max number of displayed tags.
341 * @return string HTML formatted output.
342 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
343 * @throws \ImagickException
345 public static function tagCloud($limit = 50)
349 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
353 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
354 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
359 return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');