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