Improve comment
[friendica-addons.git/.git] / startpage / startpage.php
1 <?php
2 /**
3  * Name: Start Page
4  * Description: Set a preferred page to load on login from home page
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  *
8  */
9 use Friendica\Core\Hook;
10 use Friendica\DI;
11
12 function startpage_install() {
13         Hook::register('home_init', 'addon/startpage/startpage.php', 'startpage_home_init');
14         Hook::register('addon_settings', 'addon/startpage/startpage.php', 'startpage_settings');
15         Hook::register('addon_settings_post', 'addon/startpage/startpage.php', 'startpage_settings_post');
16 }
17
18 function startpage_home_init($a, $b)
19 {
20         if (!local_user()) {
21                 return;
22         }
23
24         $page = DI::pConfig()->get(local_user(), 'startpage', 'startpage');
25         if (strlen($page)) {
26                 DI::baseUrl()->redirect($page);
27         }
28         return;
29 }
30
31 /**
32  *
33  * Callback from the settings post function.
34  * $post contains the $_POST array.
35  * We will make sure we've got a valid user account
36  * and if so set our configuration setting for this person.
37  *
38  */
39
40 function startpage_settings_post($a, $post)
41 {
42         if (!local_user()) {
43                 return;
44         }
45
46         if (!empty($_POST['startpage-submit'])) {
47                 DI::pConfig()->set(local_user(), 'startpage', 'startpage', strip_tags(trim($_POST['startpage'])));
48         }
49 }
50
51 /**
52  *
53  * Called from the Addon Setting form.
54  * Add our own settings info to the page.
55  *
56  */
57 function startpage_settings(&$a, &$s)
58 {
59         if (!local_user()) {
60                 return;
61         }
62
63         /* Add our stylesheet to the page so we can make our settings look nice */
64
65         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/startpage/startpage.css' . '" media="all" />' . "\r\n";
66
67         /* Get the current state of our config variable */
68
69         $page = DI::pConfig()->get(local_user(), 'startpage', 'startpage');
70
71         /* Add some HTML to the existing form */
72
73         $s .= '<span id="settings_startpage_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_startpage_expanded\'); openClose(\'settings_startpage_inflated\');">';
74         $s .= '<h3>' . DI::l10n()->t('Startpage') . '</h3>';
75         $s .= '</span>';
76         $s .= '<div id="settings_startpage_expanded" class="settings-block" style="display: none;">';
77         $s .= '<span class="fakelink" onclick="openClose(\'settings_startpage_expanded\'); openClose(\'settings_startpage_inflated\');">';
78         $s .= '<h3>' . DI::l10n()->t('Startpage') . '</h3>';
79         $s .= '</span>';
80         $s .= '<div id="startpage-page-wrapper">';
81         $s .= '<label id="startpage-page-label" for="startpage-page">' . DI::l10n()->t('Home page to load after login  - leave blank for profile wall') . '</label>';
82         $s .= '<input id="startpage-page" type="text" name="startpage" value="' . $page . '" />';
83         $s .= '</div><div class="clear"></div>';
84         $s .= '<div id="startpage-desc">' . DI::l10n()->t('Examples: &quot;network&quot; or &quot;notifications/system&quot;') . '</div>';
85
86         /* provide a submit button */
87
88         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="startpage-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
89 }