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