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