]> git.mxchange.org Git - friendica-addons.git/blob - randplace/randplace.php
Merge pull request #1294 from annando/no-boot
[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\Core\Session;
27 use Friendica\DI;
28
29 function randplace_install()
30 {
31         /*
32          * Our demo addon will attach in three places.
33          * The first is just prior to storing a local post.
34          */
35         Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
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         Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
43         Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
44
45         Logger::notice("installed randplace");
46 }
47
48 function randplace_uninstall()
49 {
50         /*
51          * This function should undo anything that was done in name_install()
52          *
53          * Except hooks, they are all unregistered automatically and don't need to be unregistered manually.
54          */
55         Logger::notice("removed randplace");
56 }
57
58 function randplace_post_hook(App $a, &$item)
59 {
60         /*
61          * An item was posted on the local system.
62          * We are going to look for specific items:
63          *      - A status post by a profile owner
64          *      - The profile owner must have allowed our addon
65          */
66         Logger::notice('randplace invoked');
67
68         if (!Session::getLocalUser()) {
69                 /* non-zero if this is a logged in user of this system */
70                 return;
71         }
72
73         if (Session::getLocalUser() != $item['uid']) {
74                 /* Does this person own the post? */
75                 return;
76         }
77
78         if ($item['parent']) {
79                 /* If the item has a parent, this is a comment or something else, not a status post. */
80                 return;
81         }
82
83         /* Retrieve our personal config setting */
84
85         $active = DI::pConfig()->get(Session::getLocalUser(), 'randplace', 'enable');
86
87         if (!$active) {
88                 return;
89         }
90
91         /**
92          *
93          * OK, we're allowed to do our stuff.
94          * Here's what we are going to do:
95          * load the list of timezone names, and use that to generate a list of world cities.
96          * Then we'll pick one of those at random and put it in the "location" field for the post.
97          *
98          */
99
100         $cities = [];
101         $zones = timezone_identifiers_list();
102         foreach($zones as $zone) {
103                 if ((strpos($zone, '/')) && (! stristr($zone, 'US/')) && (! stristr($zone, 'Etc/'))) {
104                         $cities[] = str_replace('_', ' ',substr($zone, strpos($zone, '/') + 1));
105                 }
106         }
107
108         if (!count($cities)) {
109                 return;
110         }
111
112         $city = array_rand($cities,1);
113         $item['location'] = $cities[$city];
114
115         return;
116 }
117
118 /**
119  * Callback from the settings post function.
120  * $post contains the $_POST array.
121  * We will make sure we've got a valid user account
122  * and if so set our configuration setting for this person.
123  */
124 function randplace_settings_post(App $a, $post)
125 {
126         if (!Session::getLocalUser()) {
127                 return;
128         }
129
130         if ($_POST['randplace-submit']) {
131                 DI::pConfig()->set(Session::getLocalUser(), 'randplace', 'enable', intval($_POST['randplace']));
132         }
133 }
134
135
136 /**
137  * Called from the Addon Setting form.
138  * Add our own settings info to the page.
139  */
140 function randplace_settings(App &$a, array &$data)
141 {
142         if(!Session::getLocalUser()) {
143                 return;
144         }
145
146         $enabled = DI::pConfig()->get(Session::getLocalUser(),'randplace','enable');
147
148         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/randplace/');
149         $html = Renderer::replaceMacros($t, [
150                 '$enabled' => ['randplace', DI::l10n()->t('Enable Randplace Addon'), $enabled],
151         ]);
152
153         $data = [
154                 'addon' => 'randplace',
155                 'title' => DI::l10n()->t('Randplace Settings'),
156                 'html'  => $html,
157         ];
158 }