]> git.mxchange.org Git - friendica.git/blob - src/Content/Nav.php
dbc6d17a3265765b6728bf1cbaed4458607413c6
[friendica.git] / src / Content / Nav.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\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($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)
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()
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         private static function populateAppMenu()
122         {
123                 self::$app_menu = [];
124
125                 //Don't populate apps_menu if apps are private
126                 $privateapps = DI::config()->get('config', 'private_addons', false);
127                 if (local_user() || !$privateapps) {
128                         $arr = ['app_menu' => self::$app_menu];
129
130                         Hook::callAll('app_menu', $arr);
131
132                         self::$app_menu = $arr['app_menu'];
133                 }
134         }
135
136         /**
137          * Prepares a list of navigation links
138          *
139          * @param  App   $a
140          * @return array Navigation links
141          *    string 'sitelocation' => The webbie (username@site.com)
142          *    array 'nav' => Array of links used in the nav menu
143          *    string 'banner' => Formatted html link with banner image
144          *    array 'userinfo' => Array of user information (name, icon)
145          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
146          */
147         private static function getInfo(App $a): array
148         {
149                 $ssl_state = (bool) local_user();
150
151                 /*
152                  * Our network is distributed, and as you visit friends some of the
153                  * sites look exactly the same - it isn't always easy to know where you are.
154                  * Display the current site location as a navigation aid.
155                  */
156
157                 $myident = !empty($a->getNickname() ? $a->getNickname() . '@' : '');
158
159                 $sitelocation = $myident . substr(DI::baseUrl()->get($ssl_state), strpos(DI::baseUrl()->get($ssl_state), '//') + 2);
160
161                 $nav = [
162                         'admin'         => null,
163                         'apps'          => null,
164                         'community'     => null,
165                         'home'          => null,
166                         'events'        => null,
167                         'login'         => null,
168                         'logout'        => null,
169                         'langselector'  => null,
170                         'messages'      => null,
171                         'network'       => null,
172                         'notifications' => null,
173                         'remote'        => null,
174                         'search'        => null,
175                         'usermenu'      => [],
176                 ];
177
178                 // Display login or logout
179                 $userinfo = null;
180
181                 // nav links: array of array('href', 'text', 'extra css classes', 'title')
182                 if (Session::isAuthenticated()) {
183                         $nav['logout'] = ['logout', DI::l10n()->t('Logout'), '', DI::l10n()->t('End this session')];
184                 } else {
185                         $nav['login'] = ['login', DI::l10n()->t('Login'), (DI::module()->getName() == 'login' ? 'selected' : ''), DI::l10n()->t('Sign in')];
186                 }
187
188                 if (local_user()) {
189                         if (!empty($a->user)) {
190                                 // user menu
191                                 $nav['usermenu'][] = ['profile/' . $a->getNickname(), DI::l10n()->t('Status'), '', DI::l10n()->t('Your posts and conversations')];
192                                 $nav['usermenu'][] = ['profile/' . $a->getNickname() . '/profile', DI::l10n()->t('Profile'), '', DI::l10n()->t('Your profile page')];
193                                 $nav['usermenu'][] = ['photos/' . $a->getNickname(), DI::l10n()->t('Photos'), '', DI::l10n()->t('Your photos')];
194                                 $nav['usermenu'][] = ['videos/' . $a->getNickname(), DI::l10n()->t('Videos'), '', DI::l10n()->t('Your videos')];
195                                 $nav['usermenu'][] = ['events/', DI::l10n()->t('Events'), '', DI::l10n()->t('Your events')];
196                                 $nav['usermenu'][] = ['notes/', DI::l10n()->t('Personal notes'), '', DI::l10n()->t('Your personal notes')];
197
198                                 // user info
199                                 $contact = DBA::selectFirst('contact', ['micro'], ['uid' => $a->getUserId(), 'self' => true]);
200                                 $userinfo = [
201                                         'icon' => (DBA::isResult($contact) ? DI::baseUrl()->remove($contact['micro']) : Contact::DEFAULT_AVATAR_MICRO),
202                                         'name' => $a->getUserValue('username'),
203                                 ];
204                         } else {
205                                 DI::logger()->warning('Empty $a->user for local user', ['local_user' => local_user(), '$a' => $a]);
206                         }
207                 }
208
209                 // "Home" should also take you home from an authenticated remote profile connection
210                 $homelink = Profile::getMyURL();
211                 if (! $homelink) {
212                         $homelink = Session::get('visitor_home', '');
213                 }
214
215                 if ((DI::module()->getName() != 'home') && (! (local_user()))) {
216                         $nav['home'] = [$homelink, DI::l10n()->t('Home'), '', DI::l10n()->t('Home Page')];
217                 }
218
219                 if (intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !Session::isAuthenticated()) {
220                         $nav['register'] = ['register', DI::l10n()->t('Register'), '', DI::l10n()->t('Create an account')];
221                 }
222
223                 $help_url = 'help';
224
225                 if (!DI::config()->get('system', 'hide_help')) {
226                         $nav['help'] = [$help_url, DI::l10n()->t('Help'), '', DI::l10n()->t('Help and documentation')];
227                 }
228
229                 if (count(self::getAppMenu()) > 0) {
230                         $nav['apps'] = ['apps', DI::l10n()->t('Apps'), '', DI::l10n()->t('Addon applications, utilities, games')];
231                 }
232
233                 if (local_user() || !DI::config()->get('system', 'local_search')) {
234                         $nav['search'] = ['search', DI::l10n()->t('Search'), '', DI::l10n()->t('Search site content')];
235
236                         $nav['searchoption'] = [
237                                 DI::l10n()->t('Full Text'),
238                                 DI::l10n()->t('Tags'),
239                                 DI::l10n()->t('Contacts')
240                         ];
241
242                         if (DI::config()->get('system', 'poco_local_search')) {
243                                 $nav['searchoption'][] = DI::l10n()->t('Forums');
244                         }
245                 }
246
247                 $gdirpath = 'directory';
248
249                 if (strlen(DI::config()->get('system', 'singleuser'))) {
250                         $gdir = DI::config()->get('system', 'directory');
251                         if (strlen($gdir)) {
252                                 $gdirpath = Profile::zrl($gdir, true);
253                         }
254                 }
255
256                 if ((local_user() || DI::config()->get('system', 'community_page_style') != CP_NO_COMMUNITY_PAGE) &&
257                         !(DI::config()->get('system', 'community_page_style') == CP_NO_INTERNAL_COMMUNITY)) {
258                         $nav['community'] = ['community', DI::l10n()->t('Community'), '', DI::l10n()->t('Conversations on this and other servers')];
259                 }
260
261                 if (local_user()) {
262                         $nav['events'] = ['events', DI::l10n()->t('Events'), '', DI::l10n()->t('Events and Calendar')];
263                 }
264
265                 $nav['directory'] = [$gdirpath, DI::l10n()->t('Directory'), '', DI::l10n()->t('People directory')];
266
267                 $nav['about'] = ['friendica', DI::l10n()->t('Information'), '', DI::l10n()->t('Information about this friendica instance')];
268
269                 if (DI::config()->get('system', 'tosdisplay')) {
270                         $nav['tos'] = ['tos', DI::l10n()->t('Terms of Service'), '', DI::l10n()->t('Terms of Service of this Friendica instance')];
271                 }
272
273                 // The following nav links are only show to logged in users
274                 if (local_user() && !empty($a->user)) {
275                         $nav['network'] = ['network', DI::l10n()->t('Network'), '', DI::l10n()->t('Conversations from your friends')];
276
277                         $nav['home'] = ['profile/' . $a->getNickname(), DI::l10n()->t('Home'), '', DI::l10n()->t('Your posts and conversations')];
278
279                         // Don't show notifications for public communities
280                         if (Session::get('page_flags', '') != User::PAGE_FLAGS_COMMUNITY) {
281                                 $nav['introductions'] = ['notifications/intros', DI::l10n()->t('Introductions'), '', DI::l10n()->t('Friend Requests')];
282                                 $nav['notifications'] = ['notifications',       DI::l10n()->t('Notifications'), '', DI::l10n()->t('Notifications')];
283                                 $nav['notifications']['all'] = ['notifications/system', DI::l10n()->t('See all notifications'), '', ''];
284                                 $nav['notifications']['mark'] = ['', DI::l10n()->t('Mark as seen'), '', DI::l10n()->t('Mark all system notifications seen')];
285                         }
286
287                         $nav['messages'] = ['message', DI::l10n()->t('Messages'), '', DI::l10n()->t('Private mail')];
288                         $nav['messages']['inbox'] = ['message', DI::l10n()->t('Inbox'), '', DI::l10n()->t('Inbox')];
289                         $nav['messages']['outbox'] = ['message/sent', DI::l10n()->t('Outbox'), '', DI::l10n()->t('Outbox')];
290                         $nav['messages']['new'] = ['message/new', DI::l10n()->t('New Message'), '', DI::l10n()->t('New Message')];
291
292                         if (User::hasIdentities(DI::session()->get('submanage') ?: local_user())) {
293                                 $nav['delegation'] = ['delegation', DI::l10n()->t('Accounts'), '', DI::l10n()->t('Manage other pages')];
294                         }
295
296                         $nav['settings'] = ['settings', DI::l10n()->t('Settings'), '', DI::l10n()->t('Account settings')];
297
298                         $nav['contacts'] = ['contact', DI::l10n()->t('Contacts'), '', DI::l10n()->t('Manage/edit friends and contacts')];
299                 }
300
301                 // Show the link to the admin configuration page if user is admin
302                 if (is_site_admin()) {
303                         $nav['admin'] = ['admin/', DI::l10n()->t('Admin'), '', DI::l10n()->t('Site setup and configuration')];
304                 }
305
306                 $nav['navigation'] = ['navigation/', DI::l10n()->t('Navigation'), '', DI::l10n()->t('Site map')];
307
308                 // Provide a banner/logo/whatever
309                 $banner = DI::config()->get('system', 'banner');
310                 if (is_null($banner)) {
311                         $banner = '<a href="https://friendi.ca"><img id="logo-img" src="images/friendica-32.png" alt="logo" /></a><span id="logo-text"><a href="https://friendi.ca">Friendica</a></span>';
312                 }
313
314                 $nav_info = [
315                         'banner'       => $banner,
316                         'nav'          => $nav,
317                         'sitelocation' => $sitelocation,
318                         'userinfo'     => $userinfo,
319                 ];
320
321                 Hook::callAll('nav_info', $nav_info);
322
323                 return $nav_info;
324         }
325 }