friendica-addons/fromapp/fromapp.php

111 lines
3.5 KiB
PHP
Raw Normal View History

2012-09-24 10:20:36 +02:00
<?php
/**
* Name: FromApp
* Description: Change the displayed application you are posting from
* Version: 1.0
* Author: Commander Zot
*
*/
use Friendica\Core\Addon;
use Friendica\Core\PConfig;
2012-09-24 10:20:36 +02:00
function fromapp_install() {
Addon::registerHook('post_local', 'addon/fromapp/fromapp.php', 'fromapp_post_hook');
Addon::registerHook('plugin_settings', 'addon/fromapp/fromapp.php', 'fromapp_settings');
Addon::registerHook('plugin_settings_post', 'addon/fromapp/fromapp.php', 'fromapp_settings_post');
2012-09-24 10:20:36 +02:00
logger("installed fromapp");
}
function fromapp_uninstall() {
Addon::unregisterHook('post_local', 'addon/fromapp/fromapp.php', 'fromapp_post_hook');
Addon::unregisterHook('plugin_settings', 'addon/fromapp/fromapp.php', 'fromapp_settings');
Addon::unregisterHook('plugin_settings_post', 'addon/fromapp/fromapp.php', 'fromapp_settings_post');
2012-09-24 10:20:36 +02:00
logger("removed fromapp");
}
function fromapp_settings_post($a,$post) {
if(! local_user() || (! x($_POST,'fromapp-submit')))
return;
PConfig::set(local_user(),'fromapp','app',$_POST['fromapp-input']);
PConfig::set(local_user(),'fromapp','force',intval($_POST['fromapp-force']));
2012-09-25 02:21:30 +02:00
2012-09-24 10:20:36 +02:00
info( t('Fromapp settings updated.') . EOL);
}
function fromapp_settings(&$a,&$s) {
if(! local_user())
return;
/* Add our stylesheet to the page so we can make our settings look nice */
$a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/fromapp/fromapp.css' . '" media="all" />' . "\r\n";
/* Get the current state of our config variable */
$fromapp = PConfig::get(local_user(),'fromapp', 'app', '');
2012-09-25 02:21:30 +02:00
$force = intval(PConfig::get(local_user(),'fromapp','force'));
2012-09-25 02:21:30 +02:00
$force_enabled = (($force) ? ' checked="checked" ' : '');
2012-09-24 10:20:36 +02:00
/* Add some HTML to the existing form */
2013-12-01 11:51:30 +01:00
$s .= '<span id="settings_fromapp_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromapp_expanded\'); openClose(\'settings_fromapp_inflated\');">';
2012-09-24 10:20:36 +02:00
$s .= '<h3>' . t('FromApp Settings') . '</h3>';
2013-12-01 11:51:30 +01:00
$s .= '</span>';
$s .= '<div id="settings_fromapp_expanded" class="settings-block" style="display: none;">';
$s .= '<span class="fakelink" onclick="openClose(\'settings_fromapp_expanded\'); openClose(\'settings_fromapp_inflated\');">';
$s .= '<h3>' . t('FromApp Settings') . '</h3>';
$s .= '</span>';
2012-09-24 10:20:36 +02:00
$s .= '<div id="fromapp-wrapper">';
2012-09-25 02:21:30 +02:00
$s .= '<label id="fromapp-label" for="fromapp-input">' . t('The application name you would like to show your posts originating from.') . '</label>';
$s .= '<input id="fromapp-input" type="text" name="fromapp-input" value="' . $fromapp . '" ' . '/>';
$s .= '<div class="clear"></div>';
$s .= '<label id="fromapp-force-label" for="fromapp-force">' . t('Use this application name even if another application was used.') . '</label>';
$s .= '<input id="fromapp-force" type="checkbox" name="fromapp-force" value="1" ' . $force_enabled . '/>';
2012-09-24 10:20:36 +02:00
$s .= '</div><div class="clear"></div>';
/* provide a submit button */
2013-11-19 13:57:51 +01:00
$s .= '<div class="settings-submit-wrapper" ><input type="submit" name="fromapp-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
2012-09-24 10:20:36 +02:00
}
function fromapp_post_hook(&$a, &$item)
{
if (! local_user()) {
return;
}
2012-09-24 10:20:36 +02:00
if (local_user() != $item['uid']) {
return;
}
2012-09-24 10:20:36 +02:00
$app = PConfig::get(local_user(), 'fromapp', 'app');
$force = intval(PConfig::get(local_user(), 'fromapp', 'force'));
2012-09-24 10:20:36 +02:00
if (is_null($app) || (! strlen($app))) {
return;
}
2012-09-24 10:20:36 +02:00
if (strlen(trim($item['app'])) && (! $force)) {
2012-09-25 02:21:30 +02:00
return;
}
2012-09-25 02:21:30 +02:00
$apps = explode(',', $app);
$item['app'] = trim($apps[mt_rand(0, count($apps)-1)]);
2012-09-24 10:20:36 +02:00
return;
2013-11-19 13:57:51 +01:00
}