]> git.mxchange.org Git - friendica-addons.git/blob - krynn/krynn.php
Merge branch '3.6-release'
[friendica-addons.git] / krynn / krynn.php
1 <?php
2 /**
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
5  * Version: 1.0
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>
9  *
10  *"My body was my sacrifice... for my magic. This damage is permanent." - Raistlin Majere
11  */
12 use Friendica\Core\Addon;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15
16 function krynn_install() {
17
18         /**
19          *
20          * Our demo addon will attach in three places.
21          * The first is just prior to storing a local post.
22          *
23          */
24
25         Addon::registerHook('post_local', 'addon/krynn/krynn.php', 'krynn_post_hook');
26
27         /**
28          *
29          * Then we'll attach into the addon settings page, and also the
30          * settings post hook so that we can create and update
31          * user preferences.
32          *
33          */
34
35         Addon::registerHook('addon_settings', 'addon/krynn/krynn.php', 'krynn_settings');
36         Addon::registerHook('addon_settings_post', 'addon/krynn/krynn.php', 'krynn_settings_post');
37
38         logger("installed krynn");
39 }
40
41
42 function krynn_uninstall() {
43
44         /**
45          *
46          * uninstall unregisters any hooks created with register_hook
47          * during install. It may also delete configuration settings
48          * and any other cleanup.
49          *
50          */
51
52         Addon::unregisterHook('post_local',    'addon/krynn/krynn.php', 'krynn_post_hook');
53         Addon::unregisterHook('addon_settings', 'addon/krynn/krynn.php', 'krynn_settings');
54         Addon::unregisterHook('addon_settings_post', 'addon/krynn/krynn.php', 'krynn_settings_post');
55
56
57         logger("removed krynn");
58 }
59
60
61
62 function krynn_post_hook($a, &$item) {
63
64         /**
65          *
66          * An item was posted on the local system.
67          * We are going to look for specific items:
68          *      - A status post by a profile owner
69          *      - The profile owner must have allowed our addon
70          *
71          */
72
73         logger('krynn invoked');
74
75         if(! local_user())   /* non-zero if this is a logged in user of this system */
76                 return;
77
78         if(local_user() != $item['uid'])    /* Does this person own the post? */
79                 return;
80
81         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
82                 return;
83
84         /* Retrieve our personal config setting */
85
86         $active = PConfig::get(local_user(), 'krynn', 'enable');
87
88         if(! $active)
89                 return;
90
91         /**
92          *
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 krynn locales.
96          * Then we'll pick one of those at random and put it in the "location" field for the post.
97          *
98          */
99
100         $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'];
101
102         $planet = array_rand($krynn,1);
103         $item['location'] = $krynn[$planet];
104
105         return;
106 }
107
108
109
110
111 /**
112  *
113  * Callback from the settings post function.
114  * $post contains the $_POST array.
115  * We will make sure we've got a valid user account
116  * and if so set our configuration setting for this person.
117  *
118  */
119
120 function krynn_settings_post($a,$post) {
121         if(! local_user())
122                 return;
123         if($_POST['krynn-submit'])
124                 PConfig::set(local_user(),'krynn','enable',intval($_POST['krynn']));
125 }
126
127
128 /**
129  *
130  * Called from the addon Setting form.
131  * Add our own settings info to the page.
132  *
133  */
134
135
136
137 function krynn_settings(&$a,&$s) {
138
139         if(! local_user())
140                 return;
141
142         /* Add our stylesheet to the page so we can make our settings look nice */
143
144         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/krynn/krynn.css' . '" media="all" />' . "\r\n";
145
146         /* Get the current state of our config variable */
147
148         $enabled = PConfig::get(local_user(),'krynn','enable');
149
150         $checked = (($enabled) ? ' checked="checked" ' : '');
151
152         /* Add some HTML to the existing form */
153
154     $s .= '<span id="settings_krynn_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_krynn_expanded\'); openClose(\'settings_krynn_inflated\');">';
155         $s .= '<h3>' . L10n::t('Krynn') . '</h3>';
156         $s .= '</span>';
157         $s .= '<div id="settings_krynn_expanded" class="settings-block" style="display: none;">';
158         $s .= '<span class="fakelink" onclick="openClose(\'settings_krynn_expanded\'); openClose(\'settings_krynn_inflated\');">';
159         $s .= '<h3>' . L10n::t('Krynn') . '</h3>';
160         $s .= '</span>';
161
162
163     $s .= '<div class="settings-block">';
164         $s .= '<h3>' . L10n::t('Krynn Settings') . '</h3>';
165         $s .= '<div id="krynn-enable-wrapper">';
166         $s .= '<label id="krynn-enable-label" for="krynn-checkbox">' . L10n::t('Enable Krynn Addon') . '</label>';
167         $s .= '<input id="krynn-checkbox" type="checkbox" name="krynn" value="1" ' . $checked . '/>';
168         $s .= '</div><div class="clear"></div></div>';
169         /* provide a submit button */
170
171         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="krynn-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
172
173 }
174
175