]> git.mxchange.org Git - friendica-addons.git/blob - randplace/randplace.php
regen webrtc messeages.po file
[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
22 use Friendica\App;
23 use Friendica\Core\Hook;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27
28 function randplace_install() {
29
30         /**
31          *
32          * Our demo addon will attach in three places.
33          * The first is just prior to storing a local post.
34          *
35          */
36
37         Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
38
39         /**
40          *
41          * Then we'll attach into the addon settings page, and also the
42          * settings post hook so that we can create and update
43          * user preferences.
44          *
45          */
46
47         Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
48         Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
49
50         Logger::notice("installed randplace");
51 }
52
53
54 function randplace_uninstall() {
55
56         /**
57          *
58          * This function should undo anything that was done in name_install()
59          *
60          * Except hooks, they are all unregistered automatically and don't need to be unregistered manually.
61          *
62          */
63
64         Logger::notice("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 addon
77          *
78          */
79
80         Logger::notice('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 = DI::pConfig()->get(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 = [];
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                 DI::pConfig()->set(local_user(),'randplace','enable',intval($_POST['randplace']));
139 }
140
141
142 /**
143  *
144  * Called from the Addon Setting form.
145  * Add our own settings info to the page.
146  *
147  */
148
149
150
151 function randplace_settings(App &$a, array &$data)
152 {
153         if(! local_user()) {
154                 return;
155         }
156
157         $enabled = DI::pConfig()->get(local_user(),'randplace','enable');
158
159         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/randplace/');
160         $html = Renderer::replaceMacros($t, [
161                 '$enabled' => ['randplace', DI::l10n()->t('Enable Randplace Addon'), $enabled],
162         ]);
163
164         $data = [
165                 'addon' => 'randplace',
166                 'title' => DI::l10n()->t('Randplace Settings'),
167                 'html'  => $html,
168         ];
169 }