]> git.mxchange.org Git - friendica.git/blob - addon/randplace/randplace.php
Merge branch 'master' of git://github.com/friendika/friendika
[friendica.git] / addon / randplace / randplace.php
1 <?php
2
3 /**
4  * Sample Friendika plugin/addon
5  *
6  * Addon Name: randplace
7  *
8  */
9
10 /**
11  *
12  * Addons are registered with the system in the
13  * .htconfig.php file.
14  *
15  * $a->config['system']['addon'] = 'plugin1,plugin2,etc.';
16  *
17  * When registration is detected, the system calls the plugin
18  * name_install() function, located in 'addon/name/name.php',
19  * where 'name' is the name of the addon.
20  * If the addon is removed from the configuration list, the 
21  * system will call the name_uninstall() function.
22  *
23  */
24
25
26 function randplace_install() {
27
28         /**
29          * 
30          * Our demo plugin will attach in three places.
31          * The first is just prior to storing a local post.
32          *
33          */
34
35         register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
36
37         /**
38          *
39          * Then we'll attach into the plugin settings page, and also the 
40          * settings post hook so that we can create and update
41          * user preferences.
42          *
43          */
44
45         register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
46         register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
47
48         logger("installed randplace");
49 }
50
51
52 function randplace_uninstall() {
53
54         /**
55          *
56          * uninstall unregisters any hooks created with register_hook
57          * during install. It may also delete configuration settings
58          * and any other cleanup.
59          *
60          */
61
62         unregister_hook('post_local',    'addon/randplace/randplace.php', 'randplace_post_hook');
63         unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
64         unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
65
66
67         logger("removed randplace");
68 }
69
70
71
72 function randplace_post_hook($a, &$item) {
73
74         /**
75          *
76          * An item was posted on the local system.
77          * We are going to look for specific items:
78          *      - A status post by a profile owner
79          *      - The profile owner must have allowed our plugin
80          *
81          */
82
83         logger('randplace invoked');
84
85         if(! local_user())   /* non-zero if this is a logged in user of this system */
86                 return;
87
88         if(local_user() != $item['uid'])    /* Does this person own the post? */
89                 return;
90
91         if($item['parent'])   /* If the item has a parent, this is a comment or something else, not a status post. */
92                 return;
93
94         /* Retrieve our personal config setting */
95
96         $active = get_pconfig(local_user(), 'randplace', 'enable');
97
98         if(! $active)
99                 return;
100
101         /**
102          *
103          * OK, we're allowed to do our stuff.
104          * Here's what we are going to do:
105          * load the list of timezone names, and use that to generate a list of world cities.
106          * Then we'll pick one of those at random and put it in the "location" field for the post.
107          *
108          */
109
110         $cities = array();
111         $zones = timezone_identifiers_list();
112         foreach($zones as $zone) {
113                 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
114                         $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
115         }
116
117         if(! count($cities))
118                 return;
119         $city = array_rand($cities,1);
120         $item['location'] = $cities[$city];
121
122         return;
123 }
124
125
126
127
128 /**
129  *
130  * Callback from the settings post function.
131  * $post contains the $_POST array.
132  * We will make sure we've got a valid user account
133  * and if so set our configuration setting for this person.
134  *
135  */
136
137 function randplace_settings_post($a,$post) {
138         if(! local_user())
139                 return;
140         set_pconfig(local_user(),'randplace','enable',intval($_POST['randplace']));
141 }
142
143
144 /**
145  *
146  * Called from the Plugin Setting form. 
147  * Add our own settings info to the page.
148  *
149  */
150
151
152
153 function randplace_settings(&$a,&$s) {
154
155         if(! local_user())
156                 return;
157
158         /* Add our stylesheet to the page so we can make our settings look nice */
159
160         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
161
162         /* Get the current state of our config variable */
163
164         $enabled = get_pconfig(local_user(),'randplace','enable');
165
166         $checked = (($enabled) ? ' checked="checked" ' : '');
167
168         /* Add some HTML to the existing form */
169
170         $s .= '<div class="settings-block">';
171         $s .= '<h3>' . t('Randplace Settings') . '</h3>';
172         $s .= '<div id="randplace-enable-wrapper">';
173         $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . t('Enable Randplace Plugin') . '</label>';
174         $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" value="1" ' . $checked . '/>';
175         $s .= '</div><div class="clear"></div>';
176
177         /* provide a submit button */
178
179         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
180
181 }