]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Merge branch 'develop' of https://github.com/friendica/friendica into develop
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @file src/Content/Widget.php
4  */
5 namespace Friendica\Content;
6
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\System;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\GContact;
18 use Friendica\Model\Profile;
19
20 require_once 'boot.php';
21 require_once 'include/dba.php';
22
23 class Widget
24 {
25         /**
26          * Return the follow widget
27          *
28          * @param string $value optional, default empty
29          */
30         public static function follow($value = "")
31         {
32                 return replace_macros(get_markup_template('follow.tpl'), array(
33                         '$connect' => L10n::t('Add New Contact'),
34                         '$desc' => L10n::t('Enter address or web location'),
35                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
36                         '$value' => $value,
37                         '$follow' => L10n::t('Connect')
38                 ));
39         }
40
41         /**
42          * Return Find People widget
43          */
44         public static function findPeople()
45         {
46                 $a = get_app();
47                 $global_dir = Config::get('system', 'directory');
48
49                 if (Config::get('system', 'invitation_only')) {
50                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
51                         if ($x || is_site_admin()) {
52                                 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
53                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
54                                         . '</div>';
55                         }
56                 }
57
58                 $nv = [];
59                 $nv['findpeople'] = L10n::t('Find People');
60                 $nv['desc'] = L10n::t('Enter name or interest');
61                 $nv['label'] = L10n::t('Connect/Follow');
62                 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
63                 $nv['findthem'] = L10n::t('Find');
64                 $nv['suggest'] = L10n::t('Friend Suggestions');
65                 $nv['similar'] = L10n::t('Similar Interests');
66                 $nv['random'] = L10n::t('Random Profile');
67                 $nv['inv'] = L10n::t('Invite Friends');
68                 $nv['directory'] = L10n::t('Global Directory');
69                 $nv['global_dir'] = $global_dir;
70                 $nv['local_directory'] = L10n::t('Local Directory');
71
72                 $aside = [];
73                 $aside['$nv'] = $nv;
74
75                 return replace_macros(get_markup_template('peoplefind.tpl'), $aside);
76         }
77
78         /**
79          * Return unavailable networks
80          */
81         public static function unavailableNetworks()
82         {
83                 // Always hide content from these networks
84                 $networks = ['face', 'apdn'];
85
86                 if (!Addon::isEnabled("statusnet")) {
87                         $networks[] = Protocol::STATUSNET;
88                 }
89
90                 if (!Addon::isEnabled("pumpio")) {
91                         $networks[] = Protocol::PUMPIO;
92                 }
93
94                 if (!Addon::isEnabled("twitter")) {
95                         $networks[] = Protocol::TWITTER;
96                 }
97
98                 if (Config::get("system", "ostatus_disabled")) {
99                         $networks[] = Protocol::OSTATUS;
100                 }
101
102                 if (!Config::get("system", "diaspora_enabled")) {
103                         $networks[] = Protocol::DIASPORA;
104                 }
105
106                 if (!Addon::isEnabled("pnut")) {
107                         $networks[] = Protocol::PNUT;
108                 }
109
110                 if (!sizeof($networks)) {
111                         return "";
112                 }
113
114                 $network_filter = implode("','", $networks);
115
116                 $network_filter = "AND `network` NOT IN ('$network_filter')";
117
118                 return $network_filter;
119         }
120
121         /**
122          * Return networks widget
123          *
124          * @param string $baseurl  baseurl
125          * @param string $selected optional, default empty
126          */
127         public static function networks($baseurl, $selected = '')
128         {
129                 if (!local_user()) {
130                         return '';
131                 }
132
133                 if (!Feature::isEnabled(local_user(), 'networks')) {
134                         return '';
135                 }
136
137                 $extra_sql = self::unavailableNetworks();
138
139                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
140                         local_user()
141                 );
142
143                 $nets = array();
144                 while ($rr = DBA::fetch($r)) {
145                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
146                 }
147                 DBA::close($r);
148
149                 if (count($nets) < 2) {
150                         return '';
151                 }
152
153                 return replace_macros(get_markup_template('nets.tpl'), array(
154                         '$title' => L10n::t('Networks'),
155                         '$desc' => '',
156                         '$sel_all' => (($selected == '') ? 'selected' : ''),
157                         '$all' => L10n::t('All Networks'),
158                         '$nets' => $nets,
159                         '$base' => $baseurl,
160                 ));
161         }
162
163         /**
164          * Return file as widget
165          *
166          * @param string $baseurl  baseurl
167          * @param string $selected optional, default empty
168          */
169         public static function fileAs($baseurl, $selected = '')
170         {
171                 if (!local_user()) {
172                         return '';
173                 }
174
175                 if (!Feature::isEnabled(local_user(), 'filing')) {
176                         return '';
177                 }
178
179                 $saved = PConfig::get(local_user(), 'system', 'filetags');
180                 if (!strlen($saved)) {
181                         return;
182                 }
183
184                 $matches = false;
185                 $terms = array();
186                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
187                 if ($cnt) {
188                         foreach ($matches as $mtch) {
189                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
190                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
191                         }
192                 }
193
194                 return replace_macros(get_markup_template('fileas_widget.tpl'), array(
195                         '$title' => L10n::t('Saved Folders'),
196                         '$desc' => '',
197                         '$sel_all' => (($selected == '') ? 'selected' : ''),
198                         '$all' => L10n::t('Everything'),
199                         '$terms' => $terms,
200                         '$base' => $baseurl,
201                 ));
202         }
203
204         /**
205          * Return categories widget
206          *
207          * @param string $baseurl  baseurl
208          * @param string $selected optional, default empty
209          */
210         public static function categories($baseurl, $selected = '')
211         {
212                 $a = get_app();
213
214                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
215                         return '';
216                 }
217
218                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
219                 if (!strlen($saved)) {
220                         return;
221                 }
222
223                 $matches = false;
224                 $terms = array();
225                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
226
227                 if ($cnt) {
228                         foreach ($matches as $mtch) {
229                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
230                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
231                         }
232                 }
233
234                 return replace_macros(get_markup_template('categories_widget.tpl'), array(
235                         '$title' => L10n::t('Categories'),
236                         '$desc' => '',
237                         '$sel_all' => (($selected == '') ? 'selected' : ''),
238                         '$all' => L10n::t('Everything'),
239                         '$terms' => $terms,
240                         '$base' => $baseurl,
241                 ));
242         }
243
244         /**
245          * Return common friends visitor widget
246          *
247          * @param string $profile_uid uid
248          */
249         public static function commonFriendsVisitor($profile_uid)
250         {
251                 if (local_user() == $profile_uid) {
252                         return;
253                 }
254
255                 $cid = $zcid = 0;
256
257                 if (!empty($_SESSION['remote'])) {
258                         foreach ($_SESSION['remote'] as $visitor) {
259                                 if ($visitor['uid'] == $profile_uid) {
260                                         $cid = $visitor['cid'];
261                                         break;
262                                 }
263                         }
264                 }
265
266                 if (!$cid) {
267                         if (Profile::getMyURL()) {
268                                 $contact = DBA::selectFirst('contact', ['id'],
269                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
270                                 if (DBA::isResult($contact)) {
271                                         $cid = $contact['id'];
272                                 } else {
273                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
274                                         if (DBA::isResult($gcontact)) {
275                                                 $zcid = $gcontact['id'];
276                                         }
277                                 }
278                         }
279                 }
280
281                 if ($cid == 0 && $zcid == 0) {
282                         return;
283                 }
284
285                 if ($cid) {
286                         $t = GContact::countCommonFriends($profile_uid, $cid);
287                 } else {
288                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
289                 }
290
291                 if (!$t) {
292                         return;
293                 }
294
295                 if ($cid) {
296                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
297                 } else {
298                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
299                 }
300
301                 return replace_macros(get_markup_template('remote_friends_common.tpl'), array(
302                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
303                         '$base' => System::baseUrl(),
304                         '$uid' => $profile_uid,
305                         '$cid' => (($cid) ? $cid : '0'),
306                         '$linkmore' => (($t > 5) ? 'true' : ''),
307                         '$more' => L10n::t('show more'),
308                         '$items' => $r)
309                 );
310         }
311
312         /**
313          * Insert a tag cloud widget for the present profile.
314          *
315          * @brief Insert a tag cloud widget for the present profile.
316          * @param int     $limit Max number of displayed tags.
317          * @return string HTML formatted output.
318          */
319         public static function tagCloud($limit = 50)
320         {
321                 $a = get_app();
322
323                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
324                         return '';
325                 }
326
327                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
328                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
329
330                         if (!$owner_id) {
331                                 return '';
332                         }
333                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
334                 }
335
336                 return '';
337         }
338 }