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