Merge pull request #556 from tobiasd/20180321-nl
[friendica-addons.git] / numfriends / numfriends.php
1 <?php
2 /**
3  * Name: Numfriends
4  * Description: Change number of contacts shown of profile sidebar
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8 use Friendica\Core\Addon;
9 use Friendica\Core\L10n;
10 use Friendica\Core\PConfig;
11
12 function numfriends_install() {
13
14         Addon::registerHook('addon_settings', 'addon/numfriends/numfriends.php', 'numfriends_settings');
15         Addon::registerHook('addon_settings_post', 'addon/numfriends/numfriends.php', 'numfriends_settings_post');
16
17         logger("installed numfriends");
18 }
19
20
21 function numfriends_uninstall() {
22
23         Addon::unregisterHook('addon_settings', 'addon/numfriends/numfriends.php', 'numfriends_settings');
24         Addon::unregisterHook('addon_settings_post', 'addon/numfriends/numfriends.php', 'numfriends_settings_post');
25
26
27         logger("removed numfriends");
28 }
29
30 /**
31  *
32  * Callback from the settings post function.
33  * $post contains the $_POST array.
34  * We will make sure we've got a valid user account
35  * and if so set our configuration setting for this person.
36  *
37  */
38 function numfriends_settings_post($a,$post) {
39         if(! local_user() || (! x($_POST,'numfriends-submit')))
40                 return;
41
42         PConfig::set(local_user(),'system','display_friend_count',intval($_POST['numfriends']));
43         info( L10n::t('Numfriends settings updated.') . EOL);
44 }
45
46
47 /**
48  *
49  * Called from the Addon Setting form. 
50  * Add our own settings info to the page.
51  *
52  */
53 function numfriends_settings(&$a, &$s)
54 {
55         if (! local_user()) {
56                 return;
57         }
58
59         /* Add our stylesheet to the page so we can make our settings look nice */
60
61         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/numfriends/numfriends.css' . '" media="all" />' . "\r\n";
62
63         /* Get the current state of our config variable */
64
65         $numfriends = PConfig::get(local_user(), 'system', 'display_friend_count', 24);
66         
67         /* Add some HTML to the existing form */
68
69         $s .= '<div class="settings-block">';
70         $s .= '<h3>' . L10n::t('Numfriends Settings') . '</h3>';
71         $s .= '<div id="numfriends-wrapper">';
72         $s .= '<label id="numfriends-label" for="numfriends">' . L10n::t('How many contacts to display on profile sidebar') . '</label>';
73         $s .= '<input id="numfriends-input" type="text" name="numfriends" value="' . intval($numfriends) . '" ' . '/>';
74         $s .= '</div><div class="clear"></div>';
75
76         /* provide a submit button */
77
78         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="numfriends-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
79 }