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