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