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() {
32 * Our demo addon will attach in three places.
33 * The first is just prior to storing a local post.
37 Hook::register('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
41 * Then we'll attach into the addon settings page, and also the
42 * settings post hook so that we can create and update
47 Hook::register('addon_settings', 'addon/randplace/randplace.php', 'randplace_settings');
48 Hook::register('addon_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
50 Logger::notice("installed randplace");
54 function randplace_uninstall() {
58 * This function should undo anything that was done in name_install()
60 * Except hooks, they are all unregistered automatically and don't need to be unregistered manually.
64 Logger::notice("removed randplace");
69 function randplace_post_hook($a, &$item) {
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
80 Logger::notice('randplace invoked');
82 if(! local_user()) /* non-zero if this is a logged in user of this system */
85 if(local_user() != $item['uid']) /* Does this person own the post? */
88 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
91 /* Retrieve our personal config setting */
93 $active = DI::pConfig()->get(local_user(), 'randplace', 'enable');
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.
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));
116 $city = array_rand($cities,1);
117 $item['location'] = $cities[$city];
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.
134 function randplace_settings_post($a,$post) {
137 if($_POST['randplace-submit'])
138 DI::pConfig()->set(local_user(),'randplace','enable',intval($_POST['randplace']));
144 * Called from the Addon Setting form.
145 * Add our own settings info to the page.
151 function randplace_settings(App &$a, array &$data)
157 $enabled = DI::pConfig()->get(local_user(),'randplace','enable');
159 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/randplace/');
160 $html = Renderer::replaceMacros($t, [
161 '$enabled' => ['randplace', DI::l10n()->t('Enable Randplace Addon'), $enabled],
165 'addon' => 'randplace',
166 'title' => DI::l10n()->t('Randplace Settings'),