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.
22 use Friendica\Core\PConfig;
24 function randplace_install() {
28 * Our demo plugin will attach in three places.
29 * The first is just prior to storing a local post.
33 register_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
37 * Then we'll attach into the plugin settings page, and also the
38 * settings post hook so that we can create and update
43 register_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
44 register_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
46 logger("installed randplace");
50 function randplace_uninstall() {
54 * uninstall unregisters any hooks created with register_hook
55 * during install. It may also delete configuration settings
56 * and any other cleanup.
60 unregister_hook('post_local', 'addon/randplace/randplace.php', 'randplace_post_hook');
61 unregister_hook('plugin_settings', 'addon/randplace/randplace.php', 'randplace_settings');
62 unregister_hook('plugin_settings_post', 'addon/randplace/randplace.php', 'randplace_settings_post');
65 logger("removed randplace");
70 function randplace_post_hook($a, &$item) {
74 * An item was posted on the local system.
75 * We are going to look for specific items:
76 * - A status post by a profile owner
77 * - The profile owner must have allowed our plugin
81 logger('randplace invoked');
83 if(! local_user()) /* non-zero if this is a logged in user of this system */
86 if(local_user() != $item['uid']) /* Does this person own the post? */
89 if($item['parent']) /* If the item has a parent, this is a comment or something else, not a status post. */
92 /* Retrieve our personal config setting */
94 $active = PConfig::get(local_user(), 'randplace', 'enable');
101 * OK, we're allowed to do our stuff.
102 * Here's what we are going to do:
103 * load the list of timezone names, and use that to generate a list of world cities.
104 * Then we'll pick one of those at random and put it in the "location" field for the post.
109 $zones = timezone_identifiers_list();
110 foreach($zones as $zone) {
111 if((strpos($zone,'/')) && (! stristr($zone,'US/')) && (! stristr($zone,'Etc/')))
112 $cities[] = str_replace('_', ' ',substr($zone,strpos($zone,'/') + 1));
117 $city = array_rand($cities,1);
118 $item['location'] = $cities[$city];
128 * Callback from the settings post function.
129 * $post contains the $_POST array.
130 * We will make sure we've got a valid user account
131 * and if so set our configuration setting for this person.
135 function randplace_settings_post($a,$post) {
138 if($_POST['randplace-submit'])
139 PConfig::set(local_user(),'randplace','enable',intval($_POST['randplace']));
145 * Called from the Plugin Setting form.
146 * Add our own settings info to the page.
152 function randplace_settings(&$a,&$s) {
157 /* Add our stylesheet to the page so we can make our settings look nice */
159 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/randplace/randplace.css' . '" media="all" />' . "\r\n";
161 /* Get the current state of our config variable */
163 $enabled = PConfig::get(local_user(),'randplace','enable');
165 $checked = (($enabled) ? ' checked="checked" ' : '');
167 /* Add some HTML to the existing form */
169 $s .= '<div class="settings-block">';
170 $s .= '<h3>' . t('Randplace Settings') . '</h3>';
171 $s .= '<div id="randplace-enable-wrapper">';
172 $s .= '<label id="randplace-enable-label" for="randplace-checkbox">' . t('Enable Randplace Plugin') . '</label>';
173 $s .= '<input id="randplace-checkbox" type="checkbox" name="randplace" value="1" ' . $checked . '/>';
174 $s .= '</div><div class="clear"></div>';
176 /* provide a submit button */
178 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="randplace-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';