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