]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Merge branch '2018.08-rc'
[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                         /// @TODO If 'network' is not there, this triggers an E_NOTICE
146                         if ($rr['network']) {
147                                 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
148                         }
149                 }
150                 DBA::close($r);
151
152                 if (count($nets) < 2) {
153                         return '';
154                 }
155
156                 return replace_macros(get_markup_template('nets.tpl'), array(
157                         '$title' => L10n::t('Networks'),
158                         '$desc' => '',
159                         '$sel_all' => (($selected == '') ? 'selected' : ''),
160                         '$all' => L10n::t('All Networks'),
161                         '$nets' => $nets,
162                         '$base' => $baseurl,
163                 ));
164         }
165
166         /**
167          * Return file as widget
168          *
169          * @param string $baseurl  baseurl
170          * @param string $selected optional, default empty
171          */
172         public static function fileAs($baseurl, $selected = '')
173         {
174                 if (!local_user()) {
175                         return '';
176                 }
177
178                 if (!Feature::isEnabled(local_user(), 'filing')) {
179                         return '';
180                 }
181
182                 $saved = PConfig::get(local_user(), 'system', 'filetags');
183                 if (!strlen($saved)) {
184                         return;
185                 }
186
187                 $matches = false;
188                 $terms = array();
189                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
190                 if ($cnt) {
191                         foreach ($matches as $mtch) {
192                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
193                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
194                         }
195                 }
196
197                 return replace_macros(get_markup_template('fileas_widget.tpl'), array(
198                         '$title' => L10n::t('Saved Folders'),
199                         '$desc' => '',
200                         '$sel_all' => (($selected == '') ? 'selected' : ''),
201                         '$all' => L10n::t('Everything'),
202                         '$terms' => $terms,
203                         '$base' => $baseurl,
204                 ));
205         }
206
207         /**
208          * Return categories widget
209          *
210          * @param string $baseurl  baseurl
211          * @param string $selected optional, default empty
212          */
213         public static function categories($baseurl, $selected = '')
214         {
215                 $a = get_app();
216
217                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
218                         return '';
219                 }
220
221                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
222                 if (!strlen($saved)) {
223                         return;
224                 }
225
226                 $matches = false;
227                 $terms = array();
228                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
229
230                 if ($cnt) {
231                         foreach ($matches as $mtch) {
232                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
233                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
234                         }
235                 }
236
237                 return replace_macros(get_markup_template('categories_widget.tpl'), array(
238                         '$title' => L10n::t('Categories'),
239                         '$desc' => '',
240                         '$sel_all' => (($selected == '') ? 'selected' : ''),
241                         '$all' => L10n::t('Everything'),
242                         '$terms' => $terms,
243                         '$base' => $baseurl,
244                 ));
245         }
246
247         /**
248          * Return common friends visitor widget
249          *
250          * @param string $profile_uid uid
251          */
252         public static function commonFriendsVisitor($profile_uid)
253         {
254                 if (local_user() == $profile_uid) {
255                         return;
256                 }
257
258                 $cid = $zcid = 0;
259
260                 if (!empty($_SESSION['remote'])) {
261                         foreach ($_SESSION['remote'] as $visitor) {
262                                 if ($visitor['uid'] == $profile_uid) {
263                                         $cid = $visitor['cid'];
264                                         break;
265                                 }
266                         }
267                 }
268
269                 if (!$cid) {
270                         if (Profile::getMyURL()) {
271                                 $contact = DBA::selectFirst('contact', ['id'],
272                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
273                                 if (DBA::isResult($contact)) {
274                                         $cid = $contact['id'];
275                                 } else {
276                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
277                                         if (DBA::isResult($gcontact)) {
278                                                 $zcid = $gcontact['id'];
279                                         }
280                                 }
281                         }
282                 }
283
284                 if ($cid == 0 && $zcid == 0) {
285                         return;
286                 }
287
288                 if ($cid) {
289                         $t = GContact::countCommonFriends($profile_uid, $cid);
290                 } else {
291                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
292                 }
293
294                 if (!$t) {
295                         return;
296                 }
297
298                 if ($cid) {
299                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
300                 } else {
301                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
302                 }
303
304                 return replace_macros(get_markup_template('remote_friends_common.tpl'), array(
305                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
306                         '$base' => System::baseUrl(),
307                         '$uid' => $profile_uid,
308                         '$cid' => (($cid) ? $cid : '0'),
309                         '$linkmore' => (($t > 5) ? 'true' : ''),
310                         '$more' => L10n::t('show more'),
311                         '$items' => $r)
312                 );
313         }
314
315         /**
316          * Insert a tag cloud widget for the present profile.
317          *
318          * @brief Insert a tag cloud widget for the present profile.
319          * @param int     $limit Max number of displayed tags.
320          * @return string HTML formatted output.
321          */
322         public static function tagCloud($limit = 50)
323         {
324                 $a = get_app();
325
326                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
327                         return '';
328                 }
329
330                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
331                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
332
333                         if (!$owner_id) {
334                                 return '';
335                         }
336                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
337                 }
338
339                 return '';
340         }
341 }