]> git.mxchange.org Git - friendica.git/blob - src/Content/Nav.php
Merge remote-tracking branch 'upstream/2022.09-rc' into quote-uri-id
[friendica.git] / src / Content / Nav.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\App;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Profile;
32 use Friendica\Model\User;
33
34 class Nav
35 {
36         private static $selected = [
37                 'global'    => null,
38                 'community' => null,
39                 'network'   => null,
40                 'home'      => null,
41                 'profiles'  => null,
42                 'introductions' => null,
43                 'notifications' => null,
44                 'messages'  => null,
45                 'directory' => null,
46                 'settings'  => null,
47                 'contacts'  => null,
48                 'delegation'=> null,
49                 'events'    => null,
50                 'register'  => null
51         ];
52
53         /**
54          * An array of HTML links provided by addons providing a module via the app_menu hook
55          *
56          * @var array
57          */
58         private static $app_menu = null;
59
60         /**
61          * Set a menu item in navbar as selected
62          *
63          * @param string $item
64          */
65         public static function setSelected(string $item)
66         {
67                 self::$selected[$item] = 'selected';
68         }
69
70         /**
71          * Build page header and site navigation bars
72          *
73          * @param  App    $a
74          * @return string
75          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
76          */
77         public static function build(App $a): string
78         {
79                 // Placeholder div for popup panel
80                 $nav = '<div id="panel" style="display: none;"></div>';
81
82                 $nav_info = self::getInfo($a);
83
84                 $tpl = Renderer::getMarkupTemplate('nav.tpl');
85
86                 $nav .= Renderer::replaceMacros($tpl, [
87                         '$sitelocation' => $nav_info['sitelocation'],
88                         '$nav'          => $nav_info['nav'],
89                         '$banner'       => $nav_info['banner'],
90                         '$emptynotifications' => DI::l10n()->t('Nothing new here'),
91                         '$userinfo'     => $nav_info['userinfo'],
92                         '$sel'          => self::$selected,
93                         '$apps'         => self::getAppMenu(),
94                         '$home'         => DI::l10n()->t('Go back'),
95                         '$clear_notifs' => DI::l10n()->t('Clear notifications'),
96                         '$search_hint'  => DI::l10n()->t('@name, !forum, #tags, content')
97                 ]);
98
99                 Hook::callAll('page_header', $nav);
100
101                 return $nav;
102         }
103
104         /**
105          * Returns the addon app menu
106          *
107          * @return array
108          */
109         public static function getAppMenu(): array
110         {
111                 if (is_null(self::$app_menu)) {
112                         self::populateAppMenu();
113                 }
114
115                 return self::$app_menu;
116         }
117
118         /**
119          * Fills the apps static variable with apps that require a menu
120          *
121          * @return void
122          */
123         private static function populateAppMenu()
124         {
125                 self::$app_menu = [];
126
127                 //Don't populate apps_menu if apps are private
128                 $privateapps = DI::config()->get('config', 'private_addons', false);
129                 if (local_user() || !$privateapps) {
130                         $arr = ['app_menu' => self::$app_menu];
131
132                         Hook::callAll('app_menu', $arr);
133
134                         self::$app_menu = $arr['app_menu'];
135                 }
136         }
137
138         /**
139          * Prepares a list of navigation links
140          *
141          * @param  App   $a
142          * @return array Navigation links
143          *    string 'sitelocation' => The webbie (username@site.com)
144          *    array 'nav' => Array of links used in the nav menu
145          *    string 'banner' => Formatted html link with banner image
146          *    array 'userinfo' => Array of user information (name, icon)
147          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
148          */
149         private static function getInfo(App $a): array
150         {
151                 $ssl_state = (bool) local_user();
152
153                 /*
154                  * Our network is distributed, and as you visit friends some of the
155                  * sites look exactly the same - it isn't always easy to know where you are.
156                  * Display the current site location as a navigation aid.
157                  */
158
159                 $myident = !empty($a->getLoggedInUserNickname()) ? $a->getLoggedInUserNickname() . '@' : '';
160
161                 $sitelocation = $myident . substr(DI::baseUrl()->get($ssl_state), strpos(DI::baseUrl()->get($ssl_state), '//') + 2);
162
163                 $nav = [
164                         'admin'         => null,
165                         'apps'          => null,
166                         'community'     => null,
167                         'home'          => null,
168                         'events'        => null,
169                         'login'         => null,
170                         'logout'        => null,
171                         'langselector'  => null,
172                         'messages'      => null,
173                         'network'       => null,
174                         'notifications' => null,
175                         'remote'        => null,
176                         'search'        => null,
177                         'usermenu'      => [],
178                 ];
179
180                 // Display login or logout
181                 $userinfo = null;
182
183                 // nav links: array of array('href', 'text', 'extra css classes', 'title')
184                 if (Session::isAuthenticated()) {
185                         $nav['logout'] = ['logout', DI::l10n()->t('Logout'), '', DI::l10n()->t('End this session')];
186                 } else {
187                         $nav['login'] = ['login', DI::l10n()->t('Login'), (DI::args()->getModuleName() == 'login' ? 'selected' : ''), DI::l10n()->t('Sign in')];
188                 }
189
190                 if ($a->isLoggedIn()) {
191                         // user menu
192                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
193                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname() . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
194                         $nav['usermenu'][] = ['photos/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
195                         $nav['usermenu'][] = ['profile/' . $a->getLoggedInUserNickname() . '/media', DI::l10n()->t('Media'), '', DI::l10n()->t('Your postings with media')];
196                         $nav['usermenu'][] = ['events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
197                         $nav['usermenu'][] = ['notes/', DI::l10n()->t('Personal notes'), '', DI::l10n()->t('Your personal notes')];
198
199                         // user info
200                         $contact = DBA::selectFirst('contact', ['id', 'url', 'avatar', 'micro', 'name', 'nick', 'baseurl', 'updated'], ['uid' => $a->getLoggedInUserId(), 'self' => true]);
201                         $userinfo = [
202                                 'icon' => Contact::getMicro($contact),
203                                 'name' => $contact['name'],
204                         ];
205                 }
206
207                 // "Home" should also take you home from an authenticated remote profile connection
208                 $homelink = Profile::getMyURL();
209                 if (! $homelink) {
210                         $homelink = Session::get('visitor_home', '');
211                 }
212
213                 if ((DI::args()->getModuleName() != 'home') && (! (local_user()))) {
214                         $nav['home'] = [$homelink, DI::l10n()->t('Home'), '', DI::l10n()->t('Home Page')];
215                 }
216
217                 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !Session::isAuthenticated()) {
218                         $nav['register'] = ['register', DI::l10n()->t('Register'), '', DI::l10n()->t('Create an account')];
219                 }
220
221                 $help_url = 'help';
222
223                 if (!DI::config()->get('system', 'hide_help')) {
224                         $nav['help'] = [$help_url, DI::l10n()->t('Help'), '', DI::l10n()->t('Help and documentation')];
225                 }
226
227                 if (count(self::getAppMenu()) > 0) {
228                         $nav['apps'] = ['apps', DI::l10n()->t('Apps'), '', DI::l10n()->t('Addon applications, utilities, games')];
229                 }
230
231                 if (local_user() || !DI::config()->get('system', 'local_search')) {
232                         $nav['search'] = ['search', DI::l10n()->t('Search'), '', DI::l10n()->t('Search site content')];
233
234                         $nav['searchoption'] = [
235                                 DI::l10n()->t('Full Text'),
236                                 DI::l10n()->t('Tags'),
237                                 DI::l10n()->t('Contacts')
238                         ];
239
240                         if (DI::config()->get('system', 'poco_local_search')) {
241                                 $nav['searchoption'][] = DI::l10n()->t('Forums');
242                         }
243                 }
244
245                 $gdirpath = 'directory';
246
247                 if (strlen(DI::config()->get('system', 'singleuser'))) {
248                         $gdir = DI::config()->get('system', 'directory');
249                         if (strlen($gdir)) {
250                                 $gdirpath = Profile::zrl($gdir, true);
251                         }
252                 }
253
254                 if ((local_user() || DI::config()->get('system', 'community_page_style') != CP_NO_COMMUNITY_PAGE) &&
255                         !(DI::config()->get('system', 'community_page_style') == CP_NO_INTERNAL_COMMUNITY)) {
256                         $nav['community'] = ['community', DI::l10n()->t('Community'), '', DI::l10n()->t('Conversations on this and other servers')];
257                 }
258
259                 if (local_user()) {
260                         $nav['events'] = ['events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
261                 }
262
263                 $nav['directory'] = [$gdirpath, DI::l10n()->t('Directory'), '', DI::l10n()->t('People directory')];
264
265                 $nav['about'] = ['friendica', DI::l10n()->t('Information'), '', DI::l10n()->t('Information about this friendica instance')];
266
267                 if (DI::config()->get('system', 'tosdisplay')) {
268                         $nav['tos'] = ['tos', DI::l10n()->t('Terms of Service'), '', DI::l10n()->t('Terms of Service of this Friendica instance')];
269                 }
270
271                 // The following nav links are only show to logged in users
272                 if (local_user() && !empty($a->getLoggedInUserNickname())) {
273                         $nav['network'] = ['network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
274
275                         $nav['home'] = ['profile/' . $a->getLoggedInUserNickname(), DI::l10n()->t('Home'), '', DI::l10n()->t('Your posts and conversations')];
276
277                         // Don't show notifications for public communities
278                         if (Session::get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
279                                 $nav['introductions'] = ['notifications/intros', DI::l10n()->t('Introductions'), '', DI::l10n()->t('Friend Requests')];
280                                 $nav['notifications'] = ['notifications',       DI::l10n()->t('Notifications'), '', DI::l10n()->t('Notifications')];
281                                 $nav['notifications']['all'] = ['notifications/system', DI::l10n()->t('See all notifications'), '', ''];
282                                 $nav['notifications']['mark'] = ['', DI::l10n()->t('Mark as seen'), '', DI::l10n()->t('Mark all system notifications as seen')];
283                         }
284
285                         $nav['messages'] = ['message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
286                         $nav['messages']['inbox'] = ['message', DI::l10n()->t('Inbox'), '', DI::l10n()->t('Inbox')];
287                         $nav['messages']['outbox'] = ['message/sent', DI::l10n()->t('Outbox'), '', DI::l10n()->t('Outbox')];
288                         $nav['messages']['new'] = ['message/new', DI::l10n()->t('New Message'), '', DI::l10n()->t('New Message')];
289
290                         if (User::hasIdentities(DI::session()->get('submanage') ?: local_user())) {
291                                 $nav['delegation'] = ['delegation', DI::l10n()->t('Accounts'), '', DI::l10n()->t('Manage other pages')];
292                         }
293
294                         $nav['settings'] = ['settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
295
296                         $nav['contacts'] = ['contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
297                 }
298
299                 // Show the link to the admin configuration page if user is admin
300                 if ($a->isSiteAdmin()) {
301                         $nav['admin'] = ['admin/', DI::l10n()->t('Admin'), '', DI::l10n()->t('Site setup and configuration')];
302                 }
303
304                 $nav['navigation'] = ['navigation/', DI::l10n()->t('Navigation'), '', DI::l10n()->t('Site map')];
305
306                 // Provide a banner/logo/whatever
307                 $banner = DI::config()->get('system', 'banner');
308                 if (is_null($banner)) {
309                         $banner = '<a href="https://friendi.ca"><img id="logo-img" width="32" height="32" src="images/friendica.svg" alt="logo" /></a><span id="logo-text"><a href="https://friendi.ca">Friendica</a></span>';
310                 }
311
312                 $nav_info = [
313                         'banner'       => $banner,
314                         'nav'          => $nav,
315                         'sitelocation' => $sitelocation,
316                         'userinfo'     => $userinfo,
317                 ];
318
319                 Hook::callAll('nav_info', $nav_info);
320
321                 return $nav_info;
322         }
323 }