3 * Name: Random Planet, Empirial Version
4 * Description: Sample Friendica 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>
9 use Friendica\Core\Addon;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Logger;
12 use Friendica\Core\PConfig;
14 function planets_install() {
18 * Our demo addon will attach in three places.
19 * The first is just prior to storing a local post.
23 Addon::registerHook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
27 * Then we'll attach into the addon settings page, and also the
28 * settings post hook so that we can create and update
33 Addon::registerHook('addon_settings', 'addon/planets/planets.php', 'planets_settings');
34 Addon::registerHook('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
36 Logger::log("installed planets");
40 function planets_uninstall() {
44 * uninstall unregisters any hooks created with register_hook
45 * during install. It may also delete configuration settings
46 * and any other cleanup.
50 Addon::unregisterHook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
51 Addon::unregisterHook('addon_settings', 'addon/planets/planets.php', 'planets_settings');
52 Addon::unregisterHook('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
55 Logger::log("removed planets");
60 function planets_post_hook($a, &$item) {
64 * An item was posted on the local system.
65 * We are going to look for specific items:
66 * - A status post by a profile owner
67 * - The profile owner must have allowed our addon
71 Logger::log('planets invoked');
73 if(! local_user()) /* non-zero if this is a logged in user of this system */
76 if(local_user() != $item['uid']) /* Does this person own the post? */
79 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
82 /* Retrieve our personal config setting */
84 $active = PConfig::get(local_user(), 'planets', 'enable');
91 * OK, we're allowed to do our stuff.
92 * Here's what we are going to do:
93 * load the list of timezone names, and use that to generate a list of world planets.
94 * Then we'll pick one of those at random and put it in the "location" field for the post.
98 $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'];
100 $planet = array_rand($planets,1);
101 $item['location'] = $planets[$planet];
111 * Callback from the settings post function.
112 * $post contains the $_POST array.
113 * We will make sure we've got a valid user account
114 * and if so set our configuration setting for this person.
118 function planets_settings_post($a,$post) {
121 if($_POST['planets-submit'])
122 PConfig::set(local_user(),'planets','enable',intval($_POST['planets']));
128 * Called from the Addon Setting form.
129 * Add our own settings info to the page.
135 function planets_settings(&$a,&$s) {
140 /* Add our stylesheet to the page so we can make our settings look nice */
142 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
144 /* Get the current state of our config variable */
146 $enabled = PConfig::get(local_user(),'planets','enable');
148 $checked = (($enabled) ? ' checked="checked" ' : '');
150 /* Add some HTML to the existing form */
152 $s .= '<span id="settings_planets_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_planets_expanded\'); openClose(\'settings_planets_inflated\');">';
153 $s .= '<h3>' . L10n::t('Planets') . '</h3>';
155 $s .= '<div id="settings_planets_expanded" class="settings-block" style="display: none;">';
156 $s .= '<span class="fakelink" onclick="openClose(\'settings_planets_expanded\'); openClose(\'settings_planets_inflated\');">';
157 $s .= '<h3>' . L10n::t('Planets') . '</h3>';
160 $s .= '<div class="settings-block">';
161 $s .= '<h3>' . L10n::t('Planets Settings') . '</h3>';
162 $s .= '<div id="planets-enable-wrapper">';
163 $s .= '<label id="planets-enable-label" for="planets-checkbox">' . L10n::t('Enable Planets Addon') . '</label>';
164 $s .= '<input id="planets-checkbox" type="checkbox" name="planets" value="1" ' . $checked . '/>';
165 $s .= '</div><div class="clear"></div></div>';
167 /* provide a submit button */
169 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';