Friendika -> Friendica (randplace)
[friendica-addons.git] / randplace / randplace.php
1 <?php
2 /**
3  * Name: Random place
4  * Description: Sample Friendica plugin/addon. Set a random place when posting.
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * 
8  * 
9  * 
10  *
11  * Addons are registered with the system in the
12  * .htconfig.php file.
13  *
14  * $a->config['system']['addon'] = 'plugin1,plugin2,etc.';
15  *
16  * When registration is detected, the system calls the plugin
17  * name_install() function, located in 'addon/name/name.php',
18  * where 'name' is the name of the addon.
19  * If the addon is removed from the configuration list, the 
20  * system will call the name_uninstall() function.
21  *
22  */
23
24
25 function randplace_install() {
26
27         /**
28          * 
29          * Our demo plugin will attach in three places.
30          * The first is just prior to storing a local post.
31          *
32          */
33
34         register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
35
36         /**
37          *
38          * Then we'll attach into the plugin settings page, and also the 
39          * settings post hook so that we can create and update
40          * user preferences.
41          *
42          */
43
44         register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
45         register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
46
47         logger("installed randplace");
48 }
49
50
51 function randplace_uninstall() {
52
53         /**
54          *
55          * uninstall unregisters any hooks created with register_hook
56          * during install. It may also delete configuration settings
57          * and any other cleanup.
58          *
59          */
60
61         unregister_hook('post_local',    'addon/randplace/randplace.php', 'randplace_post_hook');
62         unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
63         unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
64
65
66         logger("removed randplace");
67 }
68
69
70
71 function randplace_post_hook($a, &$item) {
72
73         /**
74          *
75          * An item was posted on the local system.
76          * We are going to look for specific items:
77          *      - A status post by a profile owner
78          *      - The profile owner must have allowed our plugin
79          *
80          */
81
82         logger('randplace invoked');
83
84         if(! local_user())   /* non-zero if this is a logged in user of this system */
85                 return;
86
87         if(local_user() != $item['uid'])    /* Does this person own the post? */
88                 return;
89
90         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
91                 return;
92
93         /* Retrieve our personal config setting */
94
95         $active = get_pconfig(local_user(), 'randplace', 'enable');
96
97         if(! $active)
98                 return;
99
100         /**
101          *
102          * OK, we're allowed to do our stuff.
103          * Here's what we are going to do:
104          * load the list of timezone names, and use that to generate a list of world cities.
105          * Then we'll pick one of those at random and put it in the "location" field for the post.
106          *
107          */
108
109         $cities = array();
110         $zones = timezone_identifiers_list();
111         foreach($zones as $zone) {
112                 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
113                         $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
114         }
115
116         if(! count($cities))
117                 return;
118         $city = array_rand($cities,1);
119         $item['location'] = $cities[$city];
120
121         return;
122 }
123
124
125
126
127 /**
128  *
129  * Callback from the settings post function.
130  * $post contains the $_POST array.
131  * We will make sure we've got a valid user account
132  * and if so set our configuration setting for this person.
133  *
134  */
135
136 function randplace_settings_post($a,$post) {
137         if(! local_user())
138                 return;
139         set_pconfig(local_user(),'randplace','enable',intval($_POST['randplace']));
140 }
141
142
143 /**
144  *
145  * Called from the Plugin Setting form. 
146  * Add our own settings info to the page.
147  *
148  */
149
150
151
152 function randplace_settings(&$a,&$s) {
153
154         if(! local_user())
155                 return;
156
157         /* Add our stylesheet to the page so we can make our settings look nice */
158
159         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
160
161         /* Get the current state of our config variable */
162
163         $enabled = get_pconfig(local_user(),'randplace','enable');
164
165         $checked = (($enabled) ? ' checked="checked" ' : '');
166
167         /* Add some HTML to the existing form */
168
169         $s .= '<div class="settings-block">';
170         $s .= '<h3>' . t('Randplace Settings') . '</h3>';
171         $s .= '<div id="randplace-enable-wrapper">';
172         $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . t('Enable Randplace Plugin') . '</label>';
173         $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" value="1" ' . $checked . '/>';
174         $s .= '</div><div class="clear"></div>';
175
176         /* provide a submit button */
177
178         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
179
180 }