]> git.mxchange.org Git - friendica-addons.git/blob - planets/planets.php
Merge pull request #166 from annando/master
[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: Tony Baldwin <https://free-haven.org/profile/tony>
8  */
9
10
11 function planets_install() {
12
13         /**
14          * 
15          * Our demo plugin will attach in three places.
16          * The first is just prior to storing a local post.
17          *
18          */
19
20         register_hook('post_local', 'addon/planets/planets.php', 'planets_post_hook');
21
22         /**
23          *
24          * Then we'll attach into the plugin settings page, and also the 
25          * settings post hook so that we can create and update
26          * user preferences.
27          *
28          */
29
30         register_hook('plugin_settings', 'addon/planets/planets.php', 'planets_settings');
31         register_hook('plugin_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
32
33         logger("installed planets");
34 }
35
36
37 function planets_uninstall() {
38
39         /**
40          *
41          * uninstall unregisters any hooks created with register_hook
42          * during install. It may also delete configuration settings
43          * and any other cleanup.
44          *
45          */
46
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');
50
51
52         logger("removed planets");
53 }
54
55
56
57 function planets_post_hook($a, &$item) {
58
59         /**
60          *
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
65          *
66          */
67
68         logger('planets invoked');
69
70         if(! local_user())   /* non-zero if this is a logged in user of this system */
71                 return;
72
73         if(local_user() != $item['uid'])    /* Does this person own the post? */
74                 return;
75
76         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
77                 return;
78
79         /* Retrieve our personal config setting */
80
81         $active = get_pconfig(local_user(), 'planets', 'enable');
82
83         if(! $active)
84                 return;
85
86         /**
87          *
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.
92          *
93          */
94
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');
96
97         $planet = array_rand($planets,1);
98         $item['location'] = $planets[$planet];
99
100         return;
101 }
102
103
104
105
106 /**
107  *
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.
112  *
113  */
114
115 function planets_settings_post($a,$post) {
116         if(! local_user())
117                 return;
118         if($_POST['planets-submit'])
119                 set_pconfig(local_user(),'planets','enable',intval($_POST['planets']));
120 }
121
122
123 /**
124  *
125  * Called from the Plugin Setting form. 
126  * Add our own settings info to the page.
127  *
128  */
129
130
131
132 function planets_settings(&$a,&$s) {
133
134         if(! local_user())
135                 return;
136
137         /* Add our stylesheet to the page so we can make our settings look nice */
138
139         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/planets/planets.css' . '" media="all" />' . "\r\n";
140
141         /* Get the current state of our config variable */
142
143         $enabled = get_pconfig(local_user(),'planets','enable');
144
145         $checked = (($enabled) ? ' checked="checked" ' : '');
146
147         /* Add some HTML to the existing form */
148
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>';
155
156         /* provide a submit button */
157
158         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="planets-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
159
160 }