update SV translation of blackout THX Bjoessi
[friendica-addons.git/.git] / randplace / randplace.php
1 <?php
2 /**
3  * Name: Random place
4  * Description: Sample Friendica 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 addon
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 use Friendica\Core\Hook;
22 use Friendica\Core\L10n;
23 use Friendica\Core\Logger;
24 use Friendica\Core\PConfig;
25
26 function randplace_install() {
27
28         /**
29          *
30          * Our demo addon will attach in three places.
31          * The first is just prior to storing a local post.
32          *
33          */
34
35         Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
36
37         /**
38          *
39          * Then we'll attach into the addon settings page, and also the
40          * settings post hook so that we can create and update
41          * user preferences.
42          *
43          */
44
45         Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
46         Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
47
48         Logger::log("installed randplace");
49 }
50
51
52 function randplace_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         Hook::unregister('post_local',    'addon/randplace/randplace.php', 'randplace_post_hook');
63         Hook::unregister('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
64         Hook::unregister('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
65
66
67         Logger::log("removed randplace");
68 }
69
70
71
72 function randplace_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 addon
80          *
81          */
82
83         Logger::log('randplace 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 = PConfig::get(local_user(), 'randplace', '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 cities.
106          * Then we'll pick one of those at random and put it in the "location" field for the post.
107          *
108          */
109
110         $cities = [];
111         $zones = timezone_identifiers_list();
112         foreach($zones as $zone) {
113                 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
114                         $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
115         }
116
117         if(! count($cities))
118                 return;
119         $city = array_rand($cities,1);
120         $item['location'] = $cities[$city];
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 randplace_settings_post($a,$post) {
138         if(! local_user())
139                 return;
140         if($_POST['randplace-submit'])
141                 PConfig::set(local_user(),'randplace','enable',intval($_POST['randplace']));
142 }
143
144
145 /**
146  *
147  * Called from the Addon Setting form.
148  * Add our own settings info to the page.
149  *
150  */
151
152
153
154 function randplace_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->getBaseURL() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
162
163         /* Get the current state of our config variable */
164
165         $enabled = PConfig::get(local_user(),'randplace','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>' . L10n::t('Randplace Settings') . '</h3>';
173         $s .= '<div id="randplace-enable-wrapper">';
174         $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . L10n::t('Enable Randplace Addon') . '</label>';
175         $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" 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="randplace-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
181
182 }