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 in the
14 * $a->config['system']['addon'] = 'plugin1,plugin2,etc.';
16 * When registration is detected, the system calls the plugin
17 * name_install() function, located in 'addon/name/name.php',
18 * where 'name' is the name of the addon.
19 * If the addon is removed from the configuration list, the
20 * system will call the name_uninstall() function.
25 function randplace_install() {
29 * Our demo plugin will attach in three places.
30 * The first is just prior to storing a local post.
34 register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
38 * Then we'll attach into the plugin settings page, and also the
39 * settings post hook so that we can create and update
44 register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
45 register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
47 logger("installed randplace");
51 function randplace_uninstall() {
55 * uninstall unregisters any hooks created with register_hook
56 * during install. It may also delete configuration settings
57 * and any other cleanup.
61 unregister_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
62 unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
63 unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
66 logger("removed randplace");
71 function randplace_post_hook($a, &$item) {
75 * An item was posted on the local system.
76 * We are going to look for specific items:
77 * - A status post by a profile owner
78 * - The profile owner must have allowed our plugin
82 logger('randplace invoked');
84 if(! local_user()) /* non-zero if this is a logged in user of this system */
87 if(local_user() != $item['uid']) /* Does this person own the post? */
90 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
93 /* Retrieve our personal config setting */
95 $active = get_pconfig(local_user(), 'randplace', 'enable');
102 * OK, we're allowed to do our stuff.
103 * Here's what we are going to do:
104 * load the list of timezone names, and use that to generate a list of world cities.
105 * Then we'll pick one of those at random and put it in the "location" field for the post.
110 $zones = timezone_identifiers_list();
111 foreach($zones as $zone) {
112 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
113 $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
118 $city = array_rand($cities,1);
119 $item['location'] = $cities[$city];
129 * Callback from the settings post function.
130 * $post contains the $_POST array.
131 * We will make sure we've got a valid user account
132 * and if so set our configuration setting for this person.
136 function randplace_settings_post($a,$post) {
139 if($_POST['randplace-submit'])
140 set_pconfig(local_user(),'randplace','enable',intval($_POST['randplace']));
146 * Called from the Plugin Setting form.
147 * Add our own settings info to the page.
153 function randplace_settings(&$a,&$s) {
158 /* Add our stylesheet to the page so we can make our settings look nice */
160 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
162 /* Get the current state of our config variable */
164 $enabled = get_pconfig(local_user(),'randplace','enable');
166 $checked = (($enabled) ? ' checked="checked" ' : '');
168 /* Add some HTML to the existing form */
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>';
177 /* provide a submit button */
179 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="randplace-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';