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