Use short form array syntax everywhere
[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 use Friendica\Core\PConfig;
23
24 function randplace_install() {
25
26         /**
27          *
28          * Our demo plugin will attach in three places.
29          * The first is just prior to storing a local post.
30          *
31          */
32
33         register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
34
35         /**
36          *
37          * Then we'll attach into the plugin settings page, and also the
38          * settings post hook so that we can create and update
39          * user preferences.
40          *
41          */
42
43         register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
44         register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
45
46         logger("installed randplace");
47 }
48
49
50 function randplace_uninstall() {
51
52         /**
53          *
54          * uninstall unregisters any hooks created with register_hook
55          * during install. It may also delete configuration settings
56          * and any other cleanup.
57          *
58          */
59
60         unregister_hook('post_local',    'addon/randplace/randplace.php', 'randplace_post_hook');
61         unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
62         unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
63
64
65         logger("removed randplace");
66 }
67
68
69
70 function randplace_post_hook($a, &$item) {
71
72         /**
73          *
74          * An item was posted on the local system.
75          * We are going to look for specific items:
76          *      - A status post by a profile owner
77          *      - The profile owner must have allowed our plugin
78          *
79          */
80
81         logger('randplace invoked');
82
83         if(! local_user())   /* non-zero if this is a logged in user of this system */
84                 return;
85
86         if(local_user() != $item['uid'])    /* Does this person own the post? */
87                 return;
88
89         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
90                 return;
91
92         /* Retrieve our personal config setting */
93
94         $active = PConfig::get(local_user(), 'randplace', 'enable');
95
96         if(! $active)
97                 return;
98
99         /**
100          *
101          * OK, we're allowed to do our stuff.
102          * Here's what we are going to do:
103          * load the list of timezone names, and use that to generate a list of world cities.
104          * Then we'll pick one of those at random and put it in the "location" field for the post.
105          *
106          */
107
108         $cities = [];
109         $zones = timezone_identifiers_list();
110         foreach($zones as $zone) {
111                 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
112                         $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
113         }
114
115         if(! count($cities))
116                 return;
117         $city = array_rand($cities,1);
118         $item['location'] = $cities[$city];
119
120         return;
121 }
122
123
124
125
126 /**
127  *
128  * Callback from the settings post function.
129  * $post contains the $_POST array.
130  * We will make sure we've got a valid user account
131  * and if so set our configuration setting for this person.
132  *
133  */
134
135 function randplace_settings_post($a,$post) {
136         if(! local_user())
137                 return;
138         if($_POST['randplace-submit'])
139                 PConfig::set(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 = PConfig::get(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="randplace-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
179
180 }