]> git.mxchange.org Git - friendica-addons.git/blob - planets/planets.php
Do not meddle in Imperial affairs.
[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: Darth Baldwin <darthvader@death.star>
8  * 
9  * 
10  * 
11  *
12  * Addons are registered with the system in the
13  * .htconfig.php file.
14  *
15  * $a->config['system']['addon'] = 'plugin1,plugin2,etc.';
16  *
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.
22  *
23  */
24
25
26 function planets_install() {
27
28         /**
29          * 
30          * Our demo plugin will attach in three places.
31          * The first is just prior to storing a local post.
32          *
33          */
34
35         register_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
36
37         /**
38          *
39          * Then we'll attach into the plugin settings page, and also the 
40          * settings post hook so that we can create and update
41          * user preferences.
42          *
43          */
44
45         register_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
46         register_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
47
48         logger("installed planets");
49 }
50
51
52 function planets_uninstall() {
53
54         /**
55          *
56          * uninstall unregisters any hooks created with register_hook
57          * during install. It may also delete configuration settings
58          * and any other cleanup.
59          *
60          */
61
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');
65
66
67         logger("removed planets");
68 }
69
70
71
72 function planets_post_hook($a, &$item) {
73
74         /**
75          *
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
80          *
81          */
82
83         logger('planets invoked');
84
85         if(! local_user())   /* non-zero if this is a logged in user of this system */
86                 return;
87
88         if(local_user() != $item['uid'])    /* Does this person own the post? */
89                 return;
90
91         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
92                 return;
93
94         /* Retrieve our personal config setting */
95
96         $active = get_pconfig(local_user(), 'planets', 'enable');
97
98         if(! $active)
99                 return;
100
101         /**
102          *
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.
107          *
108          */
109
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));
115 #       }
116
117 #       if(! count($planets))
118 #               return;
119         $planet = array_rand($planets,1);
120         $item['location'] = $planets[$planet];
121
122         return;
123 }
124
125
126
127
128 /**
129  *
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.
134  *
135  */
136
137 function planets_settings_post($a,$post) {
138         if(! local_user())
139                 return;
140         if($_POST['planets-submit'])
141                 set_pconfig(local_user(),'planets','enable',intval($_POST['planets']));
142 }
143
144
145 /**
146  *
147  * Called from the Plugin Setting form. 
148  * Add our own settings info to the page.
149  *
150  */
151
152
153
154 function planets_settings(&$a,&$s) {
155
156         if(! local_user())
157                 return;
158
159         /* Add our stylesheet to the page so we can make our settings look nice */
160
161         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
162
163         /* Get the current state of our config variable */
164
165         $enabled = get_pconfig(local_user(),'planets','enable');
166
167         $checked = (($enabled) ? ' checked="checked" ' : '');
168
169         /* Add some HTML to the existing form */
170
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>';
177
178         /* provide a submit button */
179
180         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
181
182 }