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\Strings;
22 use Friendica\Util\XML;
24 require_once 'boot.php';
25 require_once 'include/dba.php';
30 * Return the follow widget
32 * @param string $value optional, default empty
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 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 `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
173 public static function fileAs($baseurl, $selected = '')
179 $saved = PConfig::get(local_user(), 'system', 'filetags');
180 if (!strlen($saved)) {
186 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
188 foreach ($matches as $mtch)
190 $unescaped = XML::escape(FileTag::decode($mtch[1]));
191 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
195 return Renderer::replaceMacros(Renderer::getMarkupTemplate('fileas_widget.tpl'), array(
196 '$title' => L10n::t('Saved Folders'),
198 '$sel_all' => (($selected == '') ? 'selected' : ''),
199 '$all' => L10n::t('Everything'),
206 * Return categories widget
208 * @param string $baseurl baseurl
209 * @param string $selected optional, default empty
211 public static function categories($baseurl, $selected = '')
215 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
219 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
220 if (!strlen($saved)) {
226 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
229 foreach ($matches as $mtch) {
230 $unescaped = XML::escape(FileTag::decode($mtch[1]));
231 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
235 return Renderer::replaceMacros(Renderer::getMarkupTemplate('categories_widget.tpl'), array(
236 '$title' => L10n::t('Categories'),
238 '$sel_all' => (($selected == '') ? 'selected' : ''),
239 '$all' => L10n::t('Everything'),
246 * Return common friends visitor widget
248 * @param string $profile_uid uid
250 public static function commonFriendsVisitor($profile_uid)
252 if (local_user() == $profile_uid) {
258 if (!empty($_SESSION['remote'])) {
259 foreach ($_SESSION['remote'] as $visitor) {
260 if ($visitor['uid'] == $profile_uid) {
261 $cid = $visitor['cid'];
268 if (Profile::getMyURL()) {
269 $contact = DBA::selectFirst('contact', ['id'],
270 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
271 if (DBA::isResult($contact)) {
272 $cid = $contact['id'];
274 $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
275 if (DBA::isResult($gcontact)) {
276 $zcid = $gcontact['id'];
282 if ($cid == 0 && $zcid == 0) {
287 $t = GContact::countCommonFriends($profile_uid, $cid);
289 $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
297 $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
299 $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
302 return Renderer::replaceMacros(Renderer::getMarkupTemplate('remote_friends_common.tpl'), array(
303 '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
304 '$base' => System::baseUrl(),
305 '$uid' => $profile_uid,
306 '$cid' => (($cid) ? $cid : '0'),
307 '$linkmore' => (($t > 5) ? 'true' : ''),
308 '$more' => L10n::t('show more'),
314 * Insert a tag cloud widget for the present profile.
316 * @brief Insert a tag cloud widget for the present profile.
317 * @param int $limit Max number of displayed tags.
318 * @return string HTML formatted output.
320 public static function tagCloud($limit = 50)
324 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
328 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
329 $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
334 return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');