3 * Name: Dragonlance Krynn locales
4 * Description: Set a random locale from the Dragonlance Realm of Krynn when posting. Based on the planets frindica addon by Mike Macgirvin and Tony Baldwin
6 * Planets Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7 * Planets Author: Tony Baldwin <https://free-haven.org/profile/tony>
8 * Author: Dylan Thiedeke <https://theronin.net/profile/swathe>
10 *"My body was my sacrifice... for my magic. This damage is permanent." - Raistlin Majere
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\PConfig;
18 function krynn_install() {
22 * Our demo addon will attach in three places.
23 * The first is just prior to storing a local post.
27 Hook::register('post_local', 'addon/krynn/krynn.php', 'krynn_post_hook');
31 * Then we'll attach into the addon settings page, and also the
32 * settings post hook so that we can create and update
37 Hook::register('addon_settings', 'addon/krynn/krynn.php', 'krynn_settings');
38 Hook::register('addon_settings_post', 'addon/krynn/krynn.php', 'krynn_settings_post');
40 Logger::log("installed krynn");
44 function krynn_uninstall() {
48 * uninstall unregisters any hooks created with register_hook
49 * during install. It may also delete configuration settings
50 * and any other cleanup.
54 Hook::unregister('post_local', 'addon/krynn/krynn.php', 'krynn_post_hook');
55 Hook::unregister('addon_settings', 'addon/krynn/krynn.php', 'krynn_settings');
56 Hook::unregister('addon_settings_post', 'addon/krynn/krynn.php', 'krynn_settings_post');
59 Logger::log("removed krynn");
64 function krynn_post_hook($a, &$item) {
68 * An item was posted on the local system.
69 * We are going to look for specific items:
70 * - A status post by a profile owner
71 * - The profile owner must have allowed our addon
75 Logger::log('krynn invoked');
77 if(! local_user()) /* non-zero if this is a logged in user of this system */
80 if(local_user() != $item['uid']) /* Does this person own the post? */
83 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
86 /* Retrieve our personal config setting */
88 $active = DI::pConfig()->get(local_user(), 'krynn', 'enable');
95 * OK, we're allowed to do our stuff.
96 * Here's what we are going to do:
97 * load the list of timezone names, and use that to generate a list of krynn locales.
98 * Then we'll pick one of those at random and put it in the "location" field for the post.
102 $krynn = ['Ansalon','Abanasinia','Solace','Haven','Gateway','Qualinost','Ankatavaka','Pax Tharkas','Ergoth','Newsea','Straights of Schallsea','Plains of Dust','Tarsis','Barren Hills','Que Shu','Citadel of Light','Solinari','Hedge Maze','Tower of High Sorcery','Inn of the Last Home','Last Heroes Tomb','Academy of Sorcery','Gods Row','Temple of Majere','Temple of Kiri-Jolith','Temple of Mishakal','Temple of Zeboim','The Trough','Sad Town','Xak Tsaroth','Zhaman','Skullcap','Saifhum','Karthay','Mithas','Kothas','Silver Dragon Mountain','Silvanesti'];
104 $planet = array_rand($krynn,1);
105 $item['location'] = $krynn[$planet];
115 * Callback from the settings post function.
116 * $post contains the $_POST array.
117 * We will make sure we've got a valid user account
118 * and if so set our configuration setting for this person.
122 function krynn_settings_post($a,$post) {
125 if($_POST['krynn-submit'])
126 PConfig::set(local_user(),'krynn','enable',intval($_POST['krynn']));
132 * Called from the addon Setting form.
133 * Add our own settings info to the page.
139 function krynn_settings(&$a,&$s) {
144 /* Add our stylesheet to the page so we can make our settings look nice */
146 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/krynn/krynn.css' . '" media="all" />' . "\r\n";
148 /* Get the current state of our config variable */
150 $enabled = DI::pConfig()->get(local_user(),'krynn','enable');
152 $checked = (($enabled) ? ' checked="checked" ' : '');
154 /* Add some HTML to the existing form */
156 $s .= '<span id="settings_krynn_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_krynn_expanded\'); openClose(\'settings_krynn_inflated\');">';
157 $s .= '<h3>' . L10n::t('Krynn') . '</h3>';
159 $s .= '<div id="settings_krynn_expanded" class="settings-block" style="display: none;">';
160 $s .= '<span class="fakelink" onclick="openClose(\'settings_krynn_expanded\'); openClose(\'settings_krynn_inflated\');">';
161 $s .= '<h3>' . L10n::t('Krynn') . '</h3>';
165 $s .= '<div class="settings-block">';
166 $s .= '<h3>' . L10n::t('Krynn Settings') . '</h3>';
167 $s .= '<div id="krynn-enable-wrapper">';
168 $s .= '<label id="krynn-enable-label" for="krynn-checkbox">' . L10n::t('Enable Krynn Addon') . '</label>';
169 $s .= '<input id="krynn-checkbox" type="checkbox" name="krynn" value="1" ' . $checked . '/>';
170 $s .= '</div><div class="clear"></div></div>';
171 /* provide a submit button */
173 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="krynn-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';