]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
Refactor namespaces
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @file src/Content/Widget.php
4  */
5 namespace Friendica\Content;
6
7 use Friendica\Core\Addon;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\PConfig;
11 use Friendica\Core\Protocol;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\FileTag;
17 use Friendica\Model\GContact;
18 use Friendica\Model\Profile;
19 use Friendica\Util\Proxy as ProxyUtils;
20 use Friendica\Util\Strings;
21 use Friendica\Util\XML;
22
23 class Widget
24 {
25         /**
26          * Return the follow widget
27          *
28          * @param string $value optional, default empty
29          * @return string
30          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
31          */
32         public static function follow($value = "")
33         {
34                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('follow.tpl'), array(
35                         '$connect' => L10n::t('Add New Contact'),
36                         '$desc' => L10n::t('Enter address or web location'),
37                         '$hint' => L10n::t('Example: bob@example.com, http://example.com/barbara'),
38                         '$value' => $value,
39                         '$follow' => L10n::t('Connect')
40                 ));
41         }
42
43         /**
44          * Return Find People widget
45          */
46         public static function findPeople()
47         {
48                 $a = \get_app();
49                 $global_dir = Config::get('system', 'directory');
50
51                 if (Config::get('system', 'invitation_only')) {
52                         $x = PConfig::get(local_user(), 'system', 'invites_remaining');
53                         if ($x || is_site_admin()) {
54                                 $a->page['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
55                                         . L10n::tt('%d invitation available', '%d invitations available', $x)
56                                         . '</div>';
57                         }
58                 }
59
60                 $nv = [];
61                 $nv['findpeople'] = L10n::t('Find People');
62                 $nv['desc'] = L10n::t('Enter name or interest');
63                 $nv['label'] = L10n::t('Connect/Follow');
64                 $nv['hint'] = L10n::t('Examples: Robert Morgenstein, Fishing');
65                 $nv['findthem'] = L10n::t('Find');
66                 $nv['suggest'] = L10n::t('Friend Suggestions');
67                 $nv['similar'] = L10n::t('Similar Interests');
68                 $nv['random'] = L10n::t('Random Profile');
69                 $nv['inv'] = L10n::t('Invite Friends');
70                 $nv['directory'] = L10n::t('Global Directory');
71                 $nv['global_dir'] = $global_dir;
72                 $nv['local_directory'] = L10n::t('Local Directory');
73
74                 $aside = [];
75                 $aside['$nv'] = $nv;
76
77                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('peoplefind.tpl'), $aside);
78         }
79
80         /**
81          * Return unavailable networks
82          */
83         public static function unavailableNetworks()
84         {
85                 // Always hide content from these networks
86                 $networks = ['face', 'apdn'];
87
88                 if (!Addon::isEnabled("statusnet")) {
89                         $networks[] = Protocol::STATUSNET;
90                 }
91
92                 if (!Addon::isEnabled("pumpio")) {
93                         $networks[] = Protocol::PUMPIO;
94                 }
95
96                 if (!Addon::isEnabled("twitter")) {
97                         $networks[] = Protocol::TWITTER;
98                 }
99
100                 if (Config::get("system", "ostatus_disabled")) {
101                         $networks[] = Protocol::OSTATUS;
102                 }
103
104                 if (!Config::get("system", "diaspora_enabled")) {
105                         $networks[] = Protocol::DIASPORA;
106                 }
107
108                 if (!Addon::isEnabled("pnut")) {
109                         $networks[] = Protocol::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          * @return string
129          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
130          */
131         public static function networks($baseurl, $selected = '')
132         {
133                 if (!local_user()) {
134                         return '';
135                 }
136
137                 if (!Feature::isEnabled(local_user(), 'networks')) {
138                         return '';
139                 }
140
141                 $extra_sql = self::unavailableNetworks();
142
143                 $r = DBA::p("SELECT DISTINCT(`network`) FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql ORDER BY `network`",
144                         local_user()
145                 );
146
147                 $nets = array();
148                 while ($rr = DBA::fetch($r)) {
149                         $nets[] = array('ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' ));
150                 }
151                 DBA::close($r);
152
153                 if (count($nets) < 2) {
154                         return '';
155                 }
156
157                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('nets.tpl'), array(
158                         '$title' => L10n::t('Protocols'),
159                         '$desc' => '',
160                         '$sel_all' => (($selected == '') ? 'selected' : ''),
161                         '$all' => L10n::t('All Protocols'),
162                         '$nets' => $nets,
163                         '$base' => $baseurl,
164                 ));
165         }
166
167         /**
168          * Return file as widget
169          *
170          * @param string $baseurl  baseurl
171          * @param string $selected optional, default empty
172          * @return string|void
173          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174          */
175         public static function fileAs($baseurl, $selected = '')
176         {
177                 if (!local_user()) {
178                         return '';
179                 }
180
181                 $saved = PConfig::get(local_user(), 'system', 'filetags');
182                 if (!strlen($saved)) {
183                         return;
184                 }
185
186                 $matches = false;
187                 $terms = array();
188                 $cnt = preg_match_all('/\[(.*?)\]/', $saved, $matches, PREG_SET_ORDER);
189                 if ($cnt) {
190                         foreach ($matches as $mtch)
191                         {
192                                 $unescaped = XML::escape(FileTag::decode($mtch[1]));
193                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
194                         }
195                 }
196
197                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('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          * @return string|void
213          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
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 = XML::escape(FileTag::decode($mtch[1]));
235                                 $terms[] = array('name' => $unescaped, 'selected' => (($selected == $unescaped) ? 'selected' : ''));
236                         }
237                 }
238
239                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('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          * @return string|void
254          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255          */
256         public static function commonFriendsVisitor($profile_uid)
257         {
258                 if (local_user() == $profile_uid) {
259                         return;
260                 }
261
262                 $cid = $zcid = 0;
263
264                 if (!empty($_SESSION['remote'])) {
265                         foreach ($_SESSION['remote'] as $visitor) {
266                                 if ($visitor['uid'] == $profile_uid) {
267                                         $cid = $visitor['cid'];
268                                         break;
269                                 }
270                         }
271                 }
272
273                 if (!$cid) {
274                         if (Profile::getMyURL()) {
275                                 $contact = DBA::selectFirst('contact', ['id'],
276                                                 ['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
277                                 if (DBA::isResult($contact)) {
278                                         $cid = $contact['id'];
279                                 } else {
280                                         $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
281                                         if (DBA::isResult($gcontact)) {
282                                                 $zcid = $gcontact['id'];
283                                         }
284                                 }
285                         }
286                 }
287
288                 if ($cid == 0 && $zcid == 0) {
289                         return;
290                 }
291
292                 if ($cid) {
293                         $t = GContact::countCommonFriends($profile_uid, $cid);
294                 } else {
295                         $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
296                 }
297
298                 if (!$t) {
299                         return;
300                 }
301
302                 if ($cid) {
303                         $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
304                 } else {
305                         $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
306                 }
307
308                 if (!DBA::isResult($r)) {
309                         return;
310                 }
311
312                 $entries = [];
313                 foreach ($r as $rr) {
314                         $entry = [
315                                 'url'   => Contact::magicLink($rr['url']),
316                                 'name'  => $rr['name'],
317                                 'photo' => ProxyUtils::proxifyUrl($rr['photo'], false, ProxyUtils::SIZE_THUMB),
318                         ];
319                         $entries[] = $entry;
320                 }
321
322                 $tpl = Renderer::getMarkupTemplate('remote_friends_common.tpl');
323                 return Renderer::replaceMacros($tpl, [
324                         '$desc'     => L10n::tt("%d contact in common", "%d contacts in common", $t),
325                         '$base'     => System::baseUrl(),
326                         '$uid'      => $profile_uid,
327                         '$cid'      => (($cid) ? $cid : '0'),
328                         '$linkmore' => (($t > 5) ? 'true' : ''),
329                         '$more'     => L10n::t('show more'),
330                         '$items'    => $entries
331                 ]);
332         }
333
334         /**
335          * Insert a tag cloud widget for the present profile.
336          *
337          * @brief Insert a tag cloud widget for the present profile.
338          * @param int $limit Max number of displayed tags.
339          * @return string HTML formatted output.
340          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341          * @throws \ImagickException
342          */
343         public static function tagCloud($limit = 50)
344         {
345                 $a = \get_app();
346
347                 if (!$a->profile['profile_uid'] || !$a->profile['url']) {
348                         return '';
349                 }
350
351                 if (Feature::isEnabled($a->profile['profile_uid'], 'tagadelic')) {
352                         $owner_id = Contact::getIdForURL($a->profile['url'], 0, true);
353
354                         if (!$owner_id) {
355                                 return '';
356                         }
357                         return Widget\TagCloud::getHTML($a->profile['profile_uid'], $limit, $owner_id, 'wall');
358                 }
359
360                 return '';
361         }
362 }