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