4 * Description: Sample Friendica addon. Set a random place when posting.
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
11 * Addons are registered with the system through the admin
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.
23 use Friendica\Core\Hook;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Renderer;
28 function randplace_install()
31 * Our demo addon will attach in three places.
32 * The first is just prior to storing a local post.
34 Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
37 * Then we'll attach into the addon settings page, and also the
38 * settings post hook so that we can create and update
41 Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
42 Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
44 Logger::notice("installed randplace");
47 function randplace_uninstall()
50 * This function should undo anything that was done in name_install()
52 * Except hooks, they are all unregistered automatically and don't need to be unregistered manually.
54 Logger::notice("removed randplace");
57 function randplace_post_hook(App $a, &$item)
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
65 Logger::notice('randplace invoked');
67 if (!DI::userSession()->getLocalUserId()) {
68 /* non-zero if this is a logged in user of this system */
72 if (DI::userSession()->getLocalUserId() != $item['uid']) {
73 /* Does this person own the post? */
77 if ($item['parent']) {
78 /* If the item has a parent, this is a comment or something else, not a status post. */
82 /* Retrieve our personal config setting */
84 $active = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'randplace', 'enable');
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.
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));
107 if (!count($cities)) {
111 $city = array_rand($cities,1);
112 $item['location'] = $cities[$city];
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.
123 function randplace_settings_post(App $a, $post)
125 if (!DI::userSession()->getLocalUserId()) {
129 if ($_POST['randplace-submit']) {
130 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'randplace', 'enable', intval($_POST['randplace']));
136 * Called from the Addon Setting form.
137 * Add our own settings info to the page.
139 function randplace_settings(App &$a, array &$data)
141 if(!DI::userSession()->getLocalUserId()) {
145 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(),'randplace','enable');
147 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/randplace/');
148 $html = Renderer::replaceMacros($t, [
149 '$enabled' => ['randplace', DI::l10n()->t('Enable Randplace Addon'), $enabled],
153 'addon' => 'randplace',
154 'title' => DI::l10n()->t('Randplace Settings'),