]> git.mxchange.org Git - friendica-addons.git/blob - planets/planets.php
Langfilter: Use two letter code for the language / Bluesky: Remove callstack
[friendica-addons.git] / planets / planets.php
1 <?php
2 /**
3  * Name: Random Planet, Empirial Version
4  * Description: Sample Friendica addon. Set a random planet from the Emprire when posting.
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  * Author: Tony Baldwin <https://free-haven.org/profile/tony>
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15
16 function planets_install()
17 {
18         /**
19          * Our demo addon will attach in three places.
20          * The first is just prior to storing a local post.
21          */
22         Hook::register('post_local', 'addon/planets/planets.php', 'planets_post_hook');
23
24         /**
25          * Then we'll attach into the addon settings page, and also the
26          * settings post hook so that we can create and update
27          * user preferences.
28          */
29         Hook::register('addon_settings', 'addon/planets/planets.php', 'planets_settings');
30         Hook::register('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
31
32         Logger::notice("installed planets");
33 }
34
35 /**
36  * An item was posted on the local system.
37  * We are going to look for specific items:
38  *      - A status post by a profile owner
39  *      - The profile owner must have allowed our addon
40  */
41 function planets_post_hook(&$item)
42 {
43         Logger::notice('planets invoked');
44
45         if (!DI::userSession()->getLocalUserId()) {
46                 /* non-zero if this is a logged in user of this system */
47                 return;
48         }
49
50         if (DI::userSession()->getLocalUserId() != $item['uid']) {
51                 /* Does this person own the post? */
52                 return;
53         }
54
55         if ($item['parent']) {
56                 /* If the item has a parent, this is a comment or something else, not a status post. */
57                 return;
58         }
59
60         /* Retrieve our personal config setting */
61         $active = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'planets', 'enable');
62
63         if (!$active) {
64                 return;
65         }
66
67         /**
68          *
69          * OK, we're allowed to do our stuff.
70          * Here's what we are going to do:
71          * load the list of timezone names, and use that to generate a list of world planets.
72          * Then we'll pick one of those at random and put it in the "location" field for the post.
73          *
74          */
75
76         $planets = ['Alderaan','Tatooine','Dagobah','Polis Massa','Coruscant','Hoth','Endor','Kamino','Rattatak','Mustafar','Iego','Geonosis','Felucia','Dantooine','Ansion','Artaru','Bespin','Boz Pity','Cato Neimoidia','Christophsis','Kashyyyk','Kessel','Malastare','Mygeeto','Nar Shaddaa','Ord Mantell','Saleucami','Subterrel','Death Star','Teth','Tund','Utapau','Yavin'];
77
78         $planet = array_rand($planets,1);
79         $item['location'] = $planets[$planet];
80
81         return;
82 }
83
84
85
86
87 /**
88  *
89  * Callback from the settings post function.
90  * $post contains the $_POST array.
91  * We will make sure we've got a valid user account
92  * and if so set our configuration setting for this person.
93  *
94  */
95
96 function planets_settings_post($post)
97 {
98         if (!DI::userSession()->getLocalUserId()) {
99                 return;
100         }
101         if ($_POST['planets-submit']) {
102                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'planets', 'enable' ,intval($_POST['planets']));
103         }
104 }
105
106
107 /**
108  *
109  * Called from the Addon Setting form.
110  * Add our own settings info to the page.
111  *
112  */
113
114
115
116 function planets_settings(array &$data)
117 {
118         if(!DI::userSession()->getLocalUserId()) {
119                 return;
120         }
121
122         $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(),'planets','enable');
123
124         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/planets/');
125         $html = Renderer::replaceMacros($t, [
126                 '$enabled' => ['planets', DI::l10n()->t('Enable Planets Addon'), $enabled],
127         ]);
128
129         $data = [
130                 'addon' => 'planets',
131                 'title' => DI::l10n()->t('Planets Settings'),
132                 'html'  => $html,
133         ];
134 }