3 * Name: Random Planet, Empirial Version
4 * Description: Sample Friendica plugin/addon. Set a random planet from the Emprire when posting.
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7 * Author: Tony Baldwin <https://free-haven.org/profile/tony>
11 function planets_install() {
15 * Our demo plugin will attach in three places.
16 * The first is just prior to storing a local post.
20 register_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
24 * Then we'll attach into the plugin settings page, and also the
25 * settings post hook so that we can create and update
30 register_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
31 register_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
33 logger("installed planets");
37 function planets_uninstall() {
41 * uninstall unregisters any hooks created with register_hook
42 * during install. It may also delete configuration settings
43 * and any other cleanup.
47 unregister_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
48 unregister_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
49 unregister_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
52 logger("removed planets");
57 function planets_post_hook($a, &$item) {
61 * An item was posted on the local system.
62 * We are going to look for specific items:
63 * - A status post by a profile owner
64 * - The profile owner must have allowed our plugin
68 logger('planets invoked');
70 if(! local_user()) /* non-zero if this is a logged in user of this system */
73 if(local_user() != $item['uid']) /* Does this person own the post? */
76 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
79 /* Retrieve our personal config setting */
81 $active = get_pconfig(local_user(), 'planets', 'enable');
88 * OK, we're allowed to do our stuff.
89 * Here's what we are going to do:
90 * load the list of timezone names, and use that to generate a list of world planets.
91 * Then we'll pick one of those at random and put it in the "location" field for the post.
95 $planets = array('Alderaan','Tatooine','Dagoba','Polis Massa','Coruscant','Hoth','Endor','Kamino','Rattatak','Mustafar','Iego','Geonosis','Felucia','Dantooine','Ansion','Artaru','Bespin','Boz Pity','Cato Neimoidia','Christophsis','Kashyyk','Kessel','Malastare','Mygeeto','Nar Shaddaa','Ord Mantell','Saleucami','Subterrel','Death Star','Teth','Tund','Utapau','Yavin');
97 $planet = array_rand($planets,1);
98 $item['location'] = $planets[$planet];
108 * Callback from the settings post function.
109 * $post contains the $_POST array.
110 * We will make sure we've got a valid user account
111 * and if so set our configuration setting for this person.
115 function planets_settings_post($a,$post) {
118 if($_POST['planets-submit'])
119 set_pconfig(local_user(),'planets','enable',intval($_POST['planets']));
125 * Called from the Plugin Setting form.
126 * Add our own settings info to the page.
132 function planets_settings(&$a,&$s) {
137 /* Add our stylesheet to the page so we can make our settings look nice */
139 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
141 /* Get the current state of our config variable */
143 $enabled = get_pconfig(local_user(),'planets','enable');
145 $checked = (($enabled) ? ' checked="checked" ' : '');
147 /* Add some HTML to the existing form */
149 $s .= '<div class="settings-block">';
150 $s .= '<h3>' . t('Planets Settings') . '</h3>';
151 $s .= '<div id="planets-enable-wrapper">';
152 $s .= '<label id="planets-enable-label" for="planets-checkbox">' . t('Enable Planets Plugin') . '</label>';
153 $s .= '<input id="planets-checkbox" type="checkbox" name="planets" value="1" ' . $checked . '/>';
154 $s .= '</div><div class="clear"></div>';
156 /* provide a submit button */
158 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';