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: Darth Baldwin <darthvader@death.star>
12 * Addons are registered with the system in the
15 * $a->config['system']['addon'] = 'plugin1,plugin2,etc.';
17 * When registration is detected, the system calls the plugin
18 * name_install() function, located in 'addon/name/name.php',
19 * where 'name' is the name of the addon.
20 * If the addon is removed from the configuration list, the
21 * system will call the name_uninstall() function.
26 function planets_install() {
30 * Our demo plugin will attach in three places.
31 * The first is just prior to storing a local post.
35 register_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
39 * Then we'll attach into the plugin settings page, and also the
40 * settings post hook so that we can create and update
45 register_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
46 register_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
48 logger("installed planets");
52 function planets_uninstall() {
56 * uninstall unregisters any hooks created with register_hook
57 * during install. It may also delete configuration settings
58 * and any other cleanup.
62 unregister_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
63 unregister_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
64 unregister_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
67 logger("removed planets");
72 function planets_post_hook($a, &$item) {
76 * An item was posted on the local system.
77 * We are going to look for specific items:
78 * - A status post by a profile owner
79 * - The profile owner must have allowed our plugin
83 logger('planets invoked');
85 if(! local_user()) /* non-zero if this is a logged in user of this system */
88 if(local_user() != $item['uid']) /* Does this person own the post? */
91 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
94 /* Retrieve our personal config setting */
96 $active = get_pconfig(local_user(), 'planets', 'enable');
103 * OK, we're allowed to do our stuff.
104 * Here's what we are going to do:
105 * load the list of timezone names, and use that to generate a list of world planets.
106 * Then we'll pick one of those at random and put it in the "location" field for the post.
110 $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');
111 # $zones = timezone_identifiers_list();
112 # foreach($zones as $zone) {
113 # if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
114 # $planets[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
117 # if(! count($planets))
119 $planet = array_rand($planets,1);
120 $item['location'] = $planets[$planet];
130 * Callback from the settings post function.
131 * $post contains the $_POST array.
132 * We will make sure we've got a valid user account
133 * and if so set our configuration setting for this person.
137 function planets_settings_post($a,$post) {
140 if($_POST['planets-submit'])
141 set_pconfig(local_user(),'planets','enable',intval($_POST['planets']));
147 * Called from the Plugin Setting form.
148 * Add our own settings info to the page.
154 function planets_settings(&$a,&$s) {
159 /* Add our stylesheet to the page so we can make our settings look nice */
161 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
163 /* Get the current state of our config variable */
165 $enabled = get_pconfig(local_user(),'planets','enable');
167 $checked = (($enabled) ? ' checked="checked" ' : '');
169 /* Add some HTML to the existing form */
171 $s .= '<div class="settings-block">';
172 $s .= '<h3>' . t('Planets Settings') . '</h3>';
173 $s .= '<div id="planets-enable-wrapper">';
174 $s .= '<label id="planets-enable-label" for="planets-checkbox">' . t('Enable Planets Plugin') . '</label>';
175 $s .= '<input id="planets-checkbox" type="checkbox" name="planets" value="1" ' . $checked . '/>';
176 $s .= '</div><div class="clear"></div>';
178 /* provide a submit button */
180 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';