]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Rename Friendica\Database\dba to Friendica\Database\DBA
[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\System;
14 use Friendica\Database\DBA;
15 use Friendica\Database\DBM;
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" 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                 $networks = array();
84
85                 if (!Addon::isEnabled("appnet")) {
86                         $networks[] = NETWORK_APPNET;
87                 }
88
89                 if (!Addon::isEnabled("fbpost") && !Addon::isEnabled("facebook")) {
90                         $networks[] = NETWORK_FACEBOOK;
91                 }
92
93                 if (!Addon::isEnabled("statusnet")) {
94                         $networks[] = NETWORK_STATUSNET;
95                 }
96
97                 if (!Addon::isEnabled("pumpio")) {
98                         $networks[] = NETWORK_PUMPIO;
99                 }
100
101                 if (!Addon::isEnabled("twitter")) {
102                         $networks[] = NETWORK_TWITTER;
103                 }
104
105                 if (Config::get("system", "ostatus_disabled")) {
106                         $networks[] = NETWORK_OSTATUS;
107                 }
108
109                 if (!Config::get("system", "diaspora_enabled")) {
110                         $networks[] = NETWORK_DIASPORA;
111                 }
112
113                 if (!Addon::isEnabled("pnut")) {
114                         $networks[] = NETWORK_PNUT;
115                 }
116
117                 if (!sizeof($networks)) {
118                         return "";
119                 }
120
121                 $network_filter = implode("','", $networks);
122
123                 $network_filter = "AND `network` NOT IN ('$network_filter')";
124
125                 return $network_filter;
126         }
127
128         /**
129          * Return networks widget
130          *
131          * @param string $baseurl  baseurl
132          * @param string $selected optional, default empty
133          */
134         public static function networks($baseurl, $selected = '')
135         {
136                 if (!local_user()) {
137                         return '';
138                 }
139
140                 if (!Feature::isEnabled(local_user(), 'networks')) {
141                         return '';
142                 }
143
144                 $extra_sql = self::unavailableNetworks();
145
146                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND `network` != '' $extra_sql ORDER BY `network`",
147                         local_user()
148                 );
149
150                 $nets = array();
151                 while ($rr = DBA::fetch($r)) {
152                         /// @TODO If 'network' is not there, this triggers an E_NOTICE
153                         if ($rr['network']) {
154                                 $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
155                         }
156                 }
157                 DBA::close($r);
158
159                 if (count($nets) < 2) {
160                         return '';
161                 }
162
163                 return replace_macros(get_markup_template('nets.tpl'), array(
164                         '$title' => L10n::t('Networks'),
165                         '$desc' => '',
166                         '$sel_all' => (($selected == '') ? 'selected' : ''),
167                         '$all' => L10n::t('All Networks'),
168                         '$nets' => $nets,
169                         '$base' => $baseurl,
170                 ));
171         }
172
173         /**
174          * Return file as widget
175          *
176          * @param string $baseurl  baseurl
177          * @param string $selected optional, default empty
178          */
179         public static function fileAs($baseurl, $selected = '')
180         {
181                 if (!local_user()) {
182                         return '';
183                 }
184
185                 if (!Feature::isEnabled(local_user(), 'filing')) {
186                         return '';
187                 }
188
189                 $saved = PConfig::get(local_user(), 'system', 'filetags');
190                 if (!strlen($saved)) {
191                         return;
192                 }
193
194                 $matches = false;
195                 $terms = array();
196                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
197                 if ($cnt) {
198                         foreach ($matches as $mtch) {
199                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
200                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
201                         }
202                 }
203
204                 return replace_macros(get_markup_template('fileas_widget.tpl'), array(
205                         '$title' => L10n::t('Saved Folders'),
206                         '$desc' => '',
207                         '$sel_all' => (($selected == '') ? 'selected' : ''),
208                         '$all' => L10n::t('Everything'),
209                         '$terms' => $terms,
210                         '$base' => $baseurl,
211                 ));
212         }
213
214         /**
215          * Return categories widget
216          *
217          * @param string $baseurl  baseurl
218          * @param string $selected optional, default empty
219          */
220         public static function categories($baseurl, $selected = '')
221         {
222                 $a = get_app();
223
224                 if (!Feature::isEnabled($a->profile['profile_uid'], 'categories')) {
225                         return '';
226                 }
227
228                 $saved = PConfig::get($a->profile['profile_uid'], 'system', 'filetags');
229                 if (!strlen($saved)) {
230                         return;
231                 }
232
233                 $matches = false;
234                 $terms = array();
235                 $cnt = preg_match_all('/<(.*?)>/', $saved, $matches, PREG_SET_ORDER);
236
237                 if ($cnt) {
238                         foreach ($matches as $mtch) {
239                                 $unescaped = xmlify(file_tag_decode($mtch[1]));
240                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
241                         }
242                 }
243
244                 return replace_macros(get_markup_template('categories_widget.tpl'), array(
245                         '$title' => L10n::t('Categories'),
246                         '$desc' => '',
247                         '$sel_all' => (($selected == '') ? 'selected' : ''),
248                         '$all' => L10n::t('Everything'),
249                         '$terms' => $terms,
250                         '$base' => $baseurl,
251                 ));
252         }
253
254         /**
255          * Return common friends visitor widget
256          *
257          * @param string $profile_uid uid
258          */
259         public static function commonFriendsVisitor($profile_uid)
260         {
261                 if (local_user() == $profile_uid) {
262                         return;
263                 }
264
265                 $cid = $zcid = 0;
266
267                 if (!empty($_SESSION['remote'])) {
268                         foreach ($_SESSION['remote'] as $visitor) {
269                                 if ($visitor['uid'] == $profile_uid) {
270                                         $cid = $visitor['cid'];
271                                         break;
272                                 }
273                         }
274                 }
275
276                 if (!$cid) {
277                         if (Profile::getMyURL()) {
278                                 $contact = DBA::selectFirst('contact', ['id'],
279                                                 ['nurl' => normalise_link(Profile::getMyURL()), 'uid' => $profile_uid]);
280                                 if (DBM::is_result($contact)) {
281                                         $cid = $contact['id'];
282                                 } else {
283                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => normalise_link(Profile::getMyURL())]);
284                                         if (DBM::is_result($gcontact)) {
285                                                 $zcid = $gcontact['id'];
286                                         }
287                                 }
288                         }
289                 }
290
291                 if ($cid == 0 && $zcid == 0) {
292                         return;
293                 }
294
295                 if ($cid) {
296                         $t = GContact::countCommonFriends($profile_uid, $cid);
297                 } else {
298                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
299                 }
300
301                 if (!$t) {
302                         return;
303                 }
304
305                 if ($cid) {
306                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
307                 } else {
308                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
309                 }
310
311                 return replace_macros(get_markup_template('remote_friends_common.tpl'), array(
312                         '$desc' => L10n::tt("%d contact in common", "%d contacts in common", $t),
313                         '$base' => System::baseUrl(),
314                         '$uid' => $profile_uid,
315                         '$cid' => (($cid) ? $cid : '0'),
316                         '$linkmore' => (($t > 5) ? 'true' : ''),
317                         '$more' => L10n::t('show more'),
318                         '$items' => $r)
319                 );
320         }
321
322         /**
323          * Insert a tag cloud widget for the present profile.
324          *
325          * @brief Insert a tag cloud widget for the present profile.
326          * @param int     $limit Max number of displayed tags.
327          * @return string HTML formatted output.
328          */
329         public static function tagCloud($limit = 50)
330         {
331                 $a = get_app();
332
333                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
334                         return '';
335                 }
336
337                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
338                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
339
340                         if (!$owner_id) {
341                                 return '';
342                         }
343                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
344                 }
345
346                 return '';
347         }
348 }