]> git.mxchange.org Git - friendica-addons.git/blob - planets/planets.php
added PL translation for SAML addon THX strebski
[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          *
20          * Our demo addon will attach in three places.
21          * The first is just prior to storing a local post.
22          *
23          */
24
25         Hook::register('post_local', 'addon/planets/planets.php', 'planets_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         Hook::register('addon_settings', 'addon/planets/planets.php', 'planets_settings');
36         Hook::register('addon_settings_post', 'addon/planets/planets.php', 'planets_settings_post');
37
38         Logger::notice("installed planets");
39 }
40
41 function planets_post_hook($a, &$item) {
42
43         /**
44          *
45          * An item was posted on the local system.
46          * We are going to look for specific items:
47          *      - A status post by a profile owner
48          *      - The profile owner must have allowed our addon
49          *
50          */
51
52         Logger::notice('planets invoked');
53
54         if(! local_user())   /* non-zero if this is a logged in user of this system */
55                 return;
56
57         if(local_user() != $item['uid'])    /* Does this person own the post? */
58                 return;
59
60         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
61                 return;
62
63         /* Retrieve our personal config setting */
64
65         $active = DI::pConfig()->get(local_user(), 'planets', 'enable');
66
67         if(! $active)
68                 return;
69
70         /**
71          *
72          * OK, we're allowed to do our stuff.
73          * Here's what we are going to do:
74          * load the list of timezone names, and use that to generate a list of world planets.
75          * Then we'll pick one of those at random and put it in the "location" field for the post.
76          *
77          */
78
79         $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'];
80
81         $planet = array_rand($planets,1);
82         $item['location'] = $planets[$planet];
83
84         return;
85 }
86
87
88
89
90 /**
91  *
92  * Callback from the settings post function.
93  * $post contains the $_POST array.
94  * We will make sure we've got a valid user account
95  * and if so set our configuration setting for this person.
96  *
97  */
98
99 function planets_settings_post($a,$post) {
100         if(! local_user())
101                 return;
102         if($_POST['planets-submit'])
103                 DI::pConfig()->set(local_user(),'planets','enable',intval($_POST['planets']));
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(App &$a, array &$data)
117 {
118         if(! local_user()) {
119                 return;
120         }
121
122         $enabled = DI::pConfig()->get(local_user(),'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 }