Merge branch '3.6-release'
[friendica-addons.git] / planets / planets.php
1 <?php
2 /**
3  * Name: Random Planet, Empirial Version
4  * Description: Sample Friendica addon. Set a random planet from the Emprire when posting.
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * Author: Tony Baldwin <https://free-haven.org/profile/tony>
8  */
9 use Friendica\Core\Addon;
10 use Friendica\Core\L10n;
11 use Friendica\Core\PConfig;
12
13 function planets_install() {
14
15         /**
16          *
17          * Our demo addon will attach in three places.
18          * The first is just prior to storing a local post.
19          *
20          */
21
22         Addon::registerHook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
23
24         /**
25          *
26          * Then we'll attach into the addon settings page, and also the
27          * settings post hook so that we can create and update
28          * user preferences.
29          *
30          */
31
32         Addon::registerHook('addon_settings', 'addon/planets/planets.php', 'planets_settings');
33         Addon::registerHook('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
34
35         logger("installed planets");
36 }
37
38
39 function planets_uninstall() {
40
41         /**
42          *
43          * uninstall unregisters any hooks created with register_hook
44          * during install. It may also delete configuration settings
45          * and any other cleanup.
46          *
47          */
48
49         Addon::unregisterHook('post_local',    'addon/planets/planets.php', 'planets_post_hook');
50         Addon::unregisterHook('addon_settings', 'addon/planets/planets.php', 'planets_settings');
51         Addon::unregisterHook('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
52
53
54         logger("removed planets");
55 }
56
57
58
59 function planets_post_hook($a, &$item) {
60
61         /**
62          *
63          * An item was posted on the local system.
64          * We are going to look for specific items:
65          *      - A status post by a profile owner
66          *      - The profile owner must have allowed our addon
67          *
68          */
69
70         logger('planets invoked');
71
72         if(! local_user())   /* non-zero if this is a logged in user of this system */
73                 return;
74
75         if(local_user() != $item['uid'])    /* Does this person own the post? */
76                 return;
77
78         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
79                 return;
80
81         /* Retrieve our personal config setting */
82
83         $active = PConfig::get(local_user(), 'planets', 'enable');
84
85         if(! $active)
86                 return;
87
88         /**
89          *
90          * OK, we're allowed to do our stuff.
91          * Here's what we are going to do:
92          * load the list of timezone names, and use that to generate a list of world planets.
93          * Then we'll pick one of those at random and put it in the "location" field for the post.
94          *
95          */
96
97         $planets = ['Alderaan','Tatooine','Dagobah','Polis Massa','Coruscant','Hoth','Endor','Kamino','Rattatak','Mustafar','Iego','Geonosis','Felucia','Dantooine','Ansion','Artaru','Bespin','Boz Pity','Cato Neimoidia','Christophsis','Kashyyyk','Kessel','Malastare','Mygeeto','Nar Shaddaa','Ord Mantell','Saleucami','Subterrel','Death Star','Teth','Tund','Utapau','Yavin'];
98
99         $planet = array_rand($planets,1);
100         $item['location'] = $planets[$planet];
101
102         return;
103 }
104
105
106
107
108 /**
109  *
110  * Callback from the settings post function.
111  * $post contains the $_POST array.
112  * We will make sure we've got a valid user account
113  * and if so set our configuration setting for this person.
114  *
115  */
116
117 function planets_settings_post($a,$post) {
118         if(! local_user())
119                 return;
120         if($_POST['planets-submit'])
121                 PConfig::set(local_user(),'planets','enable',intval($_POST['planets']));
122 }
123
124
125 /**
126  *
127  * Called from the Addon Setting form.
128  * Add our own settings info to the page.
129  *
130  */
131
132
133
134 function planets_settings(&$a,&$s) {
135
136         if(! local_user())
137                 return;
138
139         /* Add our stylesheet to the page so we can make our settings look nice */
140
141         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
142
143         /* Get the current state of our config variable */
144
145         $enabled = PConfig::get(local_user(),'planets','enable');
146
147         $checked = (($enabled) ? ' checked="checked" ' : '');
148
149         /* Add some HTML to the existing form */
150
151     $s .= '<span id="settings_planets_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_planets_expanded\'); openClose(\'settings_planets_inflated\');">';
152         $s .= '<h3>' . L10n::t('Planets') . '</h3>';
153         $s .= '</span>';
154         $s .= '<div id="settings_planets_expanded" class="settings-block" style="display: none;">';
155         $s .= '<span class="fakelink" onclick="openClose(\'settings_planets_expanded\'); openClose(\'settings_planets_inflated\');">';
156         $s .= '<h3>' . L10n::t('Planets') . '</h3>';
157         $s .= '</span>';
158
159     $s .= '<div class="settings-block">';
160         $s .= '<h3>' . L10n::t('Planets Settings') . '</h3>';
161         $s .= '<div id="planets-enable-wrapper">';
162         $s .= '<label id="planets-enable-label" for="planets-checkbox">' . L10n::t('Enable Planets Addon') . '</label>';
163         $s .= '<input id="planets-checkbox" type="checkbox" name="planets" value="1" ' . $checked . '/>';
164         $s .= '</div><div class="clear"></div></div>';
165
166         /* provide a submit button */
167
168         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
169
170 }