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