]> git.mxchange.org Git - friendica-addons.git/blob - krynn/krynn.php
6c1790021067dc3b8501ef1b48956b4ffe41d734
[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 friendica 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\App;
14 use Friendica\Core\Hook;
15 use Friendica\Core\Logger;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Session;
18 use Friendica\DI;
19
20 function krynn_install()
21 {
22         /**
23          * Our demo addon will attach in three places.
24          * The first is just prior to storing a local post.
25          */
26         Hook::register('post_local', 'addon/krynn/krynn.php', 'krynn_post_hook');
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         Hook::register('addon_settings', 'addon/krynn/krynn.php', 'krynn_settings');
34         Hook::register('addon_settings_post', 'addon/krynn/krynn.php', 'krynn_settings_post');
35
36         Logger::notice("installed krynn");
37 }
38
39 function krynn_post_hook(App $a, &$item)
40 {
41         /**
42          * An item was posted on the local system.
43          * We are going to look for specific items:
44          *      - A status post by a profile owner
45          *      - The profile owner must have allowed our addon
46          */
47         if (!Session::getLocalUser()) {
48                 /* non-zero if this is a logged in user of this system */
49                 return;
50         }
51
52         if (Session::getLocalUser() != $item['uid']) {
53                 /* Does this person own the post? */
54                 return;
55         }
56
57         if ($item['parent']) {
58                 /* If the item has a parent, this is a comment or something else, not a status post. */
59                 return;
60         }
61
62         /* Retrieve our personal config setting */
63         $active = DI::pConfig()->get(Session::getLocalUser(), 'krynn', 'enable');
64
65         if (!$active) {
66                 return;
67         }
68
69         /**
70          *
71          * OK, we're allowed to do our stuff.
72          * Here's what we are going to do:
73          * load the list of timezone names, and use that to generate a list of krynn locales.
74          * Then we'll pick one of those at random and put it in the "location" field for the post.
75          *
76          */
77
78         $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'];
79
80         $planet = array_rand($krynn,1);
81         $item['location'] = $krynn[$planet];
82
83         return;
84 }
85
86 /**
87  * Callback from the settings post function.
88  * $post contains the $_POST array.
89  * We will make sure we've got a valid user account
90  * and if so set our configuration setting for this person.
91  */
92 function krynn_settings_post(App $a, $post)
93 {
94         if (!Session::getLocalUser()) {
95                 return;
96         }
97
98         if ($_POST['krynn-submit']) {
99                 DI::pConfig()->set(Session::getLocalUser(),'krynn','enable',intval($_POST['krynn']));
100         }
101 }
102
103 /**
104  * Called from the addon Setting form.
105  * Add our own settings info to the page.
106  */
107 function krynn_settings(App &$a, array &$data)
108 {
109         if (!Session::getLocalUser()) {
110                 return;
111         }
112
113         $enabled = DI::pConfig()->get(Session::getLocalUser(),'krynn','enable');
114
115         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/krynn/');
116         $html = Renderer::replaceMacros($t, [
117                 '$enabled' => ['krynn', DI::l10n()->t('Enable Krynn Addon'), $enabled],
118         ]);
119
120         $data = [
121                 'addon' => 'krynn',
122                 'title' => DI::l10n()->t('Krynn Settings'),
123                 'html'  => $html,
124         ];
125 }
126
127