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