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;
26 use Friendica\Core\Session;
29 function randplace_install()
32 * Our demo addon will attach in three places.
33 * The first is just prior to storing a local post.
35 Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
38 * Then we'll attach into the addon settings page, and also the
39 * settings post hook so that we can create and update
42 Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
43 Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
45 Logger::notice("installed randplace");
48 function randplace_uninstall()
51 * This function should undo anything that was done in name_install()
53 * Except hooks, they are all unregistered automatically and don't need to be unregistered manually.
55 Logger::notice("removed randplace");
58 function randplace_post_hook(App $a, &$item)
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
66 Logger::notice('randplace invoked');
68 if (!Session::getLocalUser()) {
69 /* non-zero if this is a logged in user of this system */
73 if (Session::getLocalUser() != $item['uid']) {
74 /* Does this person own the post? */
78 if ($item['parent']) {
79 /* If the item has a parent, this is a comment or something else, not a status post. */
83 /* Retrieve our personal config setting */
85 $active = DI::pConfig()->get(Session::getLocalUser(), 'randplace', 'enable');
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.
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));
108 if (!count($cities)) {
112 $city = array_rand($cities,1);
113 $item['location'] = $cities[$city];
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.
124 function randplace_settings_post(App $a, $post)
126 if (!Session::getLocalUser()) {
130 if ($_POST['randplace-submit']) {
131 DI::pConfig()->set(Session::getLocalUser(), 'randplace', 'enable', intval($_POST['randplace']));
137 * Called from the Addon Setting form.
138 * Add our own settings info to the page.
140 function randplace_settings(App &$a, array &$data)
142 if(!Session::getLocalUser()) {
146 $enabled = DI::pConfig()->get(Session::getLocalUser(),'randplace','enable');
148 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/randplace/');
149 $html = Renderer::replaceMacros($t, [
150 '$enabled' => ['randplace', DI::l10n()->t('Enable Randplace Addon'), $enabled],
154 'addon' => 'randplace',
155 'title' => DI::l10n()->t('Randplace Settings'),