]> git.mxchange.org Git - friendica.git/blob - src/Content/Widget.php
`q` is now completely removed
[friendica.git] / src / Content / Widget.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
24 use Friendica\Core\Addon;
25 use Friendica\Core\Cache\Duration;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Group;
32 use Friendica\Model\Item;
33 use Friendica\Model\Post;
34 use Friendica\Util\DateTimeFormat;
35 use Friendica\Util\Temporal;
36
37 class Widget
38 {
39         /**
40          * Return the follow widget
41          *
42          * @param string $value optional, default empty
43          * @return string
44          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
45          */
46         public static function follow($value = "")
47         {
48                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/follow.tpl'), array(
49                         '$connect' => DI::l10n()->t('Add New Contact'),
50                         '$desc' => DI::l10n()->t('Enter address or web location'),
51                         '$hint' => DI::l10n()->t('Example: bob@example.com, http://example.com/barbara'),
52                         '$value' => $value,
53                         '$follow' => DI::l10n()->t('Connect')
54                 ));
55         }
56
57         /**
58          * Return Find People widget
59          */
60         public static function findPeople()
61         {
62                 $global_dir = DI::config()->get('system', 'directory');
63
64                 if (DI::config()->get('system', 'invitation_only')) {
65                         $x = intval(DI::pConfig()->get(local_user(), 'system', 'invites_remaining'));
66                         if ($x || is_site_admin()) {
67                                 DI::page()['aside'] .= '<div class="side-link widget" id="side-invite-remain">'
68                                         . DI::l10n()->tt('%d invitation available', '%d invitations available', $x)
69                                         . '</div>';
70                         }
71                 }
72
73                 $nv = [];
74                 $nv['findpeople'] = DI::l10n()->t('Find People');
75                 $nv['desc'] = DI::l10n()->t('Enter name or interest');
76                 $nv['label'] = DI::l10n()->t('Connect/Follow');
77                 $nv['hint'] = DI::l10n()->t('Examples: Robert Morgenstein, Fishing');
78                 $nv['findthem'] = DI::l10n()->t('Find');
79                 $nv['suggest'] = DI::l10n()->t('Friend Suggestions');
80                 $nv['similar'] = DI::l10n()->t('Similar Interests');
81                 $nv['random'] = DI::l10n()->t('Random Profile');
82                 $nv['inv'] = DI::l10n()->t('Invite Friends');
83                 $nv['directory'] = DI::l10n()->t('Global Directory');
84                 $nv['global_dir'] = $global_dir;
85                 $nv['local_directory'] = DI::l10n()->t('Local Directory');
86
87                 $aside = [];
88                 $aside['$nv'] = $nv;
89
90                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/peoplefind.tpl'), $aside);
91         }
92
93         /**
94          * Return unavailable networks as array
95          */
96         public static function unavailableNetworksAsArray()
97         {
98                 // Always hide content from these networks
99                 $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET];
100
101                 if (!Addon::isEnabled("discourse")) {
102                         $networks[] = Protocol::DISCOURSE;
103                 }
104
105                 if (!Addon::isEnabled("statusnet")) {
106                         $networks[] = Protocol::STATUSNET;
107                 }
108
109                 if (!Addon::isEnabled("pumpio")) {
110                         $networks[] = Protocol::PUMPIO;
111                 }
112
113                 if (!Addon::isEnabled("twitter")) {
114                         $networks[] = Protocol::TWITTER;
115                 }
116
117                 if (DI::config()->get("system", "ostatus_disabled")) {
118                         $networks[] = Protocol::OSTATUS;
119                 }
120
121                 if (!DI::config()->get("system", "diaspora_enabled")) {
122                         $networks[] = Protocol::DIASPORA;
123                 }
124
125                 if (!Addon::isEnabled("pnut")) {
126                         $networks[] = Protocol::PNUT;
127                 }
128                 return $networks;
129         }
130
131         /**
132          * Return unavailable networks
133          */
134         public static function unavailableNetworks()
135         {
136                 $networks = self::unavailableNetworksAsArray();
137
138                 if (!sizeof($networks)) {
139                         return "";
140                 }
141
142                 $network_filter = implode("','", $networks);
143
144                 $network_filter = "AND `network` NOT IN ('$network_filter')";
145
146                 return $network_filter;
147         }
148
149         /**
150          * Display a generic filter widget based on a list of options
151          *
152          * The options array must be the following format:
153          * [
154          *    [
155          *      'ref' => {filter value},
156          *      'name' => {option name}
157          *    ],
158          *    ...
159          * ]
160          *
161          * @param string $type The filter query string key
162          * @param string $title
163          * @param string $desc
164          * @param string $all The no filter label
165          * @param string $baseUrl The full page request URI
166          * @param array  $options
167          * @param string $selected The currently selected filter option value
168          * @return string
169          * @throws \Exception
170          */
171         private static function filter($type, $title, $desc, $all, $baseUrl, array $options, $selected = null)
172         {
173                 $queryString = parse_url($baseUrl, PHP_URL_QUERY);
174                 $queryArray = [];
175
176                 if ($queryString) {
177                         parse_str($queryString, $queryArray);
178                         unset($queryArray[$type]);
179
180                         if (count($queryArray)) {
181                                 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?' . http_build_query($queryArray) . '&';
182                         } else {
183                                 $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?')) . '?';
184                         }
185                 } else {
186                         $baseUrl = trim($baseUrl, '?') . '?';
187                 }
188
189                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/filter.tpl'), [
190                         '$type'      => $type,
191                         '$title'     => $title,
192                         '$desc'      => $desc,
193                         '$selected'  => $selected,
194                         '$all_label' => $all,
195                         '$options'   => $options,
196                         '$base'      => $baseUrl,
197                 ]);
198         }
199
200         /**
201          * Return group membership widget
202          *
203          * @param string $baseurl
204          * @param string $selected
205          * @return string
206          * @throws \Exception
207          */
208         public static function groups($baseurl, $selected = '')
209         {
210                 if (!local_user()) {
211                         return '';
212                 }
213
214                 $options = array_map(function ($group) {
215                         return [
216                                 'ref'  => $group['id'],
217                                 'name' => $group['name']
218                         ];
219                 }, Group::getByUserId(local_user()));
220
221                 return self::filter(
222                         'group',
223                         DI::l10n()->t('Groups'),
224                         '',
225                         DI::l10n()->t('Everyone'),
226                         $baseurl,
227                         $options,
228                         $selected
229                 );
230         }
231
232         /**
233          * Return contact relationship widget
234          *
235          * @param string $baseurl  baseurl
236          * @param string $selected optional, default empty
237          * @return string
238          * @throws \Exception
239          */
240         public static function contactRels($baseurl, $selected = '')
241         {
242                 if (!local_user()) {
243                         return '';
244                 }
245
246                 $options = [
247                         ['ref' => 'followers', 'name' => DI::l10n()->t('Followers')],
248                         ['ref' => 'following', 'name' => DI::l10n()->t('Following')],
249                         ['ref' => 'mutuals', 'name' => DI::l10n()->t('Mutual friends')],
250                 ];
251
252                 return self::filter(
253                         'rel',
254                         DI::l10n()->t('Relationships'),
255                         '',
256                         DI::l10n()->t('All Contacts'),
257                         $baseurl,
258                         $options,
259                         $selected
260                 );
261         }
262
263         /**
264          * Return networks widget
265          *
266          * @param string $baseurl  baseurl
267          * @param string $selected optional, default empty
268          * @return string
269          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
270          */
271         public static function networks($baseurl, $selected = '')
272         {
273                 if (!local_user()) {
274                         return '';
275                 }
276
277                 $extra_sql = self::unavailableNetworks();
278
279                 $r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`",
280                         local_user()
281                 );
282
283                 $nets = array();
284                 while ($rr = DBA::fetch($r)) {
285                         $nets[] = ['ref' => $rr['network'], 'name' => ContactSelector::networkToName($rr['network'])];
286                 }
287                 DBA::close($r);
288
289                 if (count($nets) < 2) {
290                         return '';
291                 }
292
293                 return self::filter(
294                         'nets',
295                         DI::l10n()->t('Protocols'),
296                         '',
297                         DI::l10n()->t('All Protocols'),
298                         $baseurl,
299                         $nets,
300                         $selected
301                 );
302         }
303
304         /**
305          * Return file as widget
306          *
307          * @param string $baseurl  baseurl
308          * @param string $selected optional, default empty
309          * @return string|void
310          * @throws \Exception
311          */
312         public static function fileAs($baseurl, $selected = '')
313         {
314                 if (!local_user()) {
315                         return '';
316                 }
317
318                 $terms = [];
319                 foreach (Post\Category::getArray(local_user(), Post\Category::FILE) as $savedFolderName) {
320                         $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
321                 }
322
323                 return self::filter(
324                         'file',
325                         DI::l10n()->t('Saved Folders'),
326                         '',
327                         DI::l10n()->t('Everything'),
328                         $baseurl,
329                         $terms,
330                         $selected
331                 );
332         }
333
334         /**
335          * Return categories widget
336          *
337          * @param string $baseurl  baseurl
338          * @param string $selected optional, default empty
339          * @return string|void
340          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
341          */
342         public static function categories($baseurl, $selected = '')
343         {
344                 $a = DI::app();
345
346                 $uid = intval($a->getProfileOwner());
347
348                 if (!Feature::isEnabled($uid, 'categories')) {
349                         return '';
350                 }
351
352                 $terms = array();
353                 foreach (Post\Category::getArray(local_user(), Post\Category::CATEGORY) as $savedFolderName) {
354                         $terms[] = ['ref' => $savedFolderName, 'name' => $savedFolderName];
355                 }
356
357                 return self::filter(
358                         'category',
359                         DI::l10n()->t('Categories'),
360                         '',
361                         DI::l10n()->t('Everything'),
362                         $baseurl,
363                         $terms,
364                         $selected
365                 );
366         }
367
368         /**
369          * Show a random selection of five common contacts between the visitor and the viewed profile user.
370          *
371          * @param int    $uid      Viewed profile user ID
372          * @param string $nickname Viewed profile user nickname
373          * @return string|void
374          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
375          * @throws \ImagickException
376          */
377         public static function commonFriendsVisitor(int $uid, string $nickname)
378         {
379                 if (local_user() == $uid) {
380                         return '';
381                 }
382
383                 $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
384                 if (!$visitorPCid) {
385                         return '';
386                 }
387
388                 $localPCid = Contact::getPublicIdByUserId($uid);
389
390                 $condition = [
391                         'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
392                         $localPCid,
393                 ];
394
395                 $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
396                 if (!$total) {
397                         return '';
398                 }
399
400                 $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
401                 if (!DBA::isResult($commonContacts)) {
402                         return '';
403                 }
404
405                 $entries = [];
406                 foreach ($commonContacts as $contact) {
407                         $entries[] = [
408                                 'url'   => Contact::magicLinkByContact($contact),
409                                 'name'  => $contact['name'],
410                                 'photo' => Contact::getThumb($contact),
411                         ];
412                 }
413
414                 $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
415                 return Renderer::replaceMacros($tpl, [
416                         '$desc'     => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
417                         '$base'     => DI::baseUrl(),
418                         '$nickname' => $nickname,
419                         '$linkmore' => $total > 5 ? 'true' : '',
420                         '$more'     => DI::l10n()->t('show more'),
421                         '$contacts' => $entries
422                 ]);
423         }
424
425         /**
426          * Insert a tag cloud widget for the present profile.
427          *
428          * @param int $uid   User ID
429          * @param int $limit Max number of displayed tags.
430          * @return string HTML formatted output.
431          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
432          * @throws \ImagickException
433          */
434         public static function tagCloud(int $uid, int $limit = 50)
435         {
436                 if (empty($uid)) {
437                         return '';
438                 }
439
440                 if (Feature::isEnabled($uid, 'tagadelic')) {
441                         $owner_id = Contact::getPublicIdByUserId($uid);
442
443                         if (!$owner_id) {
444                                 return '';
445                         }
446                         return Widget\TagCloud::getHTML($uid, $limit, $owner_id, 'wall');
447                 }
448
449                 return '';
450         }
451
452         /**
453          * @param string $url Base page URL
454          * @param int    $uid User ID consulting/publishing posts
455          * @param bool   $wall True: Posted by User; False: Posted to User (network timeline)
456          * @return string
457          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
458          */
459         public static function postedByYear(string $url, int $uid, bool $wall)
460         {
461                 $o = '';
462
463                 $visible_years = DI::pConfig()->get($uid, 'system', 'archive_visible_years', 5);
464
465                 /* arrange the list in years */
466                 $dnow = DateTimeFormat::localNow('Y-m-d');
467
468                 $ret = [];
469
470                 $cachekey = 'Widget::postedByYear' . $uid . '-' . (int)$wall;
471                 $dthen = DI::cache()->get($cachekey);
472                 if (empty($dthen)) {
473                         $dthen = Item::firstPostDate($uid, $wall);
474                         DI::cache()->set($cachekey, $dthen, Duration::HOUR);
475                 }
476
477                 if ($dthen) {
478                         // Set the start and end date to the beginning of the month
479                         $dnow = substr($dnow, 0, 8) . '01';
480                         $dthen = substr($dthen, 0, 8) . '01';
481
482                         /*
483                          * Starting with the current month, get the first and last days of every
484                          * month down to and including the month of the first post
485                          */
486                         while (substr($dnow, 0, 7) >= substr($dthen, 0, 7)) {
487                                 $dyear = intval(substr($dnow, 0, 4));
488                                 $dstart = substr($dnow, 0, 8) . '01';
489                                 $dend = substr($dnow, 0, 8) . Temporal::getDaysInMonth(intval($dnow), intval(substr($dnow, 5)));
490                                 $start_month = DateTimeFormat::utc($dstart, 'Y-m-d');
491                                 $end_month = DateTimeFormat::utc($dend, 'Y-m-d');
492                                 $str = DI::l10n()->getDay(DateTimeFormat::utc($dnow, 'F'));
493
494                                 if (empty($ret[$dyear])) {
495                                         $ret[$dyear] = [];
496                                 }
497
498                                 $ret[$dyear][] = [$str, $end_month, $start_month];
499                                 $dnow = DateTimeFormat::utc($dnow . ' -1 month', 'Y-m-d');
500                         }
501                 }
502
503                 if (!DBA::isResult($ret)) {
504                         return $o;
505                 }
506
507
508                 $cutoff_year = intval(DateTimeFormat::localNow('Y')) - $visible_years;
509                 $cutoff = array_key_exists($cutoff_year, $ret);
510
511                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('widget/posted_date.tpl'),[
512                         '$title' => DI::l10n()->t('Archives'),
513                         '$size' => $visible_years,
514                         '$cutoff_year' => $cutoff_year,
515                         '$cutoff' => $cutoff,
516                         '$url' => $url,
517                         '$dates' => $ret,
518                         '$showless' => DI::l10n()->t('show less'),
519                         '$showmore' => DI::l10n()->t('show more')
520                 ]);
521
522                 return $o;
523         }
524
525         /**
526          * Display the account types sidebar
527          * The account type value is added as a parameter to the url
528          * 
529          * @param string $base        Basepath
530          * @param int    $accounttype Acount type
531          * @return string
532          */
533         public static function accounttypes(string $base, $accounttype)
534         {
535                 $accounts = [
536                         ['ref' => 'person', 'name' => DI::l10n()->t('Persons')],
537                         ['ref' => 'organisation', 'name' => DI::l10n()->t('Organisations')],
538                         ['ref' => 'news', 'name' => DI::l10n()->t('News')],
539                         ['ref' => 'community', 'name' => DI::l10n()->t('Forums')],
540                 ];
541
542                 return self::filter('accounttype', DI::l10n()->t('Account Types'), '',
543                         DI::l10n()->t('All'), $base, $accounts, $accounttype);
544         }
545 }