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>
10 use Friendica\Core\PConfig;
12 function planets_install() {
16 * Our demo plugin will attach in three places.
17 * The first is just prior to storing a local post.
21 register_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
25 * Then we'll attach into the plugin settings page, and also the
26 * settings post hook so that we can create and update
31 register_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
32 register_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
34 logger("installed planets");
38 function planets_uninstall() {
42 * uninstall unregisters any hooks created with register_hook
43 * during install. It may also delete configuration settings
44 * and any other cleanup.
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');
53 logger("removed planets");
58 function planets_post_hook($a, &$item) {
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
69 logger('planets invoked');
71 if(! local_user()) /* non-zero if this is a logged in user of this system */
74 if(local_user() != $item['uid']) /* Does this person own the post? */
77 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
80 /* Retrieve our personal config setting */
82 $active = PConfig::get(local_user(), 'planets', 'enable');
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.
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');
98 $planet = array_rand($planets,1);
99 $item['location'] = $planets[$planet];
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.
116 function planets_settings_post($a,$post) {
119 if($_POST['planets-submit'])
120 PConfig::set(local_user(),'planets','enable',intval($_POST['planets']));
126 * Called from the Plugin Setting form.
127 * Add our own settings info to the page.
133 function planets_settings(&$a,&$s) {
138 /* Add our stylesheet to the page so we can make our settings look nice */
140 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
142 /* Get the current state of our config variable */
144 $enabled = PConfig::get(local_user(),'planets','enable');
146 $checked = (($enabled) ? ' checked="checked" ' : '');
148 /* Add some HTML to the existing form */
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>';
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>';
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>';
165 /* provide a submit button */
167 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';