4 * Description: Sample Friendica plugin/addon. Set a random place when posting.
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
11 * Addons are registered with the system through the admin
14 * When registration is detected, the system calls the plugin
15 * name_install() function, located in 'addon/name/name.php',
16 * where 'name' is the name of the addon.
17 * If the addon is removed from the configuration list, the
18 * system will call the name_uninstall() function.
23 function randplace_install() {
27 * Our demo plugin will attach in three places.
28 * The first is just prior to storing a local post.
32 register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
36 * Then we'll attach into the plugin settings page, and also the
37 * settings post hook so that we can create and update
42 register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
43 register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
45 logger("installed randplace");
49 function randplace_uninstall() {
53 * uninstall unregisters any hooks created with register_hook
54 * during install. It may also delete configuration settings
55 * and any other cleanup.
59 unregister_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
60 unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
61 unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
64 logger("removed randplace");
69 function randplace_post_hook($a, &$item) {
73 * An item was posted on the local system.
74 * We are going to look for specific items:
75 * - A status post by a profile owner
76 * - The profile owner must have allowed our plugin
80 logger('randplace invoked');
82 if(! local_user()) /* non-zero if this is a logged in user of this system */
85 if(local_user() != $item['uid']) /* Does this person own the post? */
88 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
91 /* Retrieve our personal config setting */
93 $active = get_pconfig(local_user(), 'randplace', 'enable');
100 * OK, we're allowed to do our stuff.
101 * Here's what we are going to do:
102 * load the list of timezone names, and use that to generate a list of world cities.
103 * Then we'll pick one of those at random and put it in the "location" field for the post.
108 $zones = timezone_identifiers_list();
109 foreach($zones as $zone) {
110 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
111 $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
116 $city = array_rand($cities,1);
117 $item['location'] = $cities[$city];
127 * Callback from the settings post function.
128 * $post contains the $_POST array.
129 * We will make sure we've got a valid user account
130 * and if so set our configuration setting for this person.
134 function randplace_settings_post($a,$post) {
137 if($_POST['randplace-submit'])
138 set_pconfig(local_user(),'randplace','enable',intval($_POST['randplace']));
144 * Called from the Plugin Setting form.
145 * Add our own settings info to the page.
151 function randplace_settings(&$a,&$s) {
156 /* Add our stylesheet to the page so we can make our settings look nice */
158 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
160 /* Get the current state of our config variable */
162 $enabled = get_pconfig(local_user(),'randplace','enable');
164 $checked = (($enabled) ? ' checked="checked" ' : '');
166 /* Add some HTML to the existing form */
168 $s .= '<div class="settings-block">';
169 $s .= '<h3>' . t('Randplace Settings') . '</h3>';
170 $s .= '<div id="randplace-enable-wrapper">';
171 $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . t('Enable Randplace Plugin') . '</label>';
172 $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" value="1" ' . $checked . '/>';
173 $s .= '</div><div class="clear"></div>';
175 /* provide a submit button */
177 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="randplace-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';