Skip to content

Commit

Permalink
[#196 state:resolved] Added support for a new callback allowing devel…
Browse files Browse the repository at this point in the history
…opers to modify the template context of plugin config forms.

[#190 state:resolved] Added support for a new dialog developers can spawn which will contain a plugin's settings.
[#189 state:resolved] Added blog_id variable to the template context for all plugin config forms.

DOCUMENTATION:

To spawn a dialog:

  <a href="javascript:void(0)"
    onclick="openDialog(null,'plugin_config_dialog','<query string>');return false;">
    Link Text
  </a>

The querystring should contain:

  * plugin => the plugin's ID
  * blog_id => for setting the scope of the settings
  • Loading branch information
byrnereese committed Nov 11, 2009
1 parent c43db35 commit 65ffac6
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 36 deletions.
7 changes: 7 additions & 0 deletions lib/MT/App/CMS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ sub core_methods {
'itemset_action' => "${pkg}Tools::do_list_action",
'page_action' => "${pkg}Tools::do_page_action",
'cfg_system_settings' => "${pkg}System::cfg_system_settings",
'plugin_config_dialog' => "${pkg}Plugin::cfg_plugin_dialog",
'save_plugin_config' => "${pkg}Plugin::save_config",
'reset_plugin_config' => "${pkg}Plugin::reset_config",
'save_cfg_system' => "${pkg}System::save_cfg_system",
Expand Down Expand Up @@ -1364,6 +1365,12 @@ sub core_menus {
mode => "cfg_plugins",
permission => "manage_plugins",
},
'prefs:test' => {
label => "Test",
order => 110,
dialog => "plugin_config_dialog",
args => { _plugin => 'typepadantispam' },
},
'prefs:ip_info' => {
label => "IP Banning",
mode => 'list',
Expand Down
124 changes: 88 additions & 36 deletions lib/MT/CMS/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ sub cfg_plugins {
$app->load_tmpl( 'cfg_plugin.tmpl', \%param );
}

# TODO move this to MT::Plugin
sub find_plugin_by_id {
my ($id) = @_;
my @plugins = grep { $MT::Plugins{$_}->{object}->id eq $id } keys %MT::Plugins;
# use Data::Dumper;
# MT->log({ message => "Found " . Dumper(@plugins) });
return wantarray ? @plugins : $MT::Plugins{ $plugins[0] };
}

sub cfg_plugin_dialog {
my $app = shift;
my $q = $app->param;
my %param;

my $profile = find_plugin_by_id($app->param('plugin'));
my $plugin = $profile->{object};
my $scope = $app->param('scope') eq 'system' ? 'system' : 'blog:' . $app->blog->id;

my $cfg = $app->config;
$param{can_config} = $app->user->can_manage_plugins;
$param{use_plugins} = $cfg->UsePlugins;
$param{mod_perl} = 1 if $ENV{MOD_PERL};
$param{plugin} = $app->param('plugin');
$param{plugin_name} = $plugin->name;
$param{plugin_sig} = $plugin->{plugin_sig};
my $scope = $app->param('scope');
$param{scope} = $scope;

$param{config_html} = build_plugin_config_html( $app, $plugin, $scope );

$app->load_tmpl( 'dialog/cfg_plugin.tmpl', \%param );
}

sub save_config {
my $app = shift;

Expand Down Expand Up @@ -72,7 +105,12 @@ sub save_config {
return $app->error("Error saving plugin settings: " . $plugin->errstr);
}
}

if ($app->param('dialog')) {
my $tmpl = $app->load_tmpl('dialog/cfg_plugin.tmpl');
$tmpl->param( finish => 1 );
$tmpl->param( plugin_config_saved => 1 );
return $app->build_page($tmpl);
}
$app->add_return_arg( saved => 1 );
$app->add_return_arg( plugin => $profile->{object}->id );
$app->call_return;
Expand Down Expand Up @@ -126,6 +164,52 @@ sub plugin_control {
$app->call_return;
}

sub build_plugin_config_html {
my $app = shift;
my ($plugin, $scope) = @_;

my ($config_html);
my %plugin_param;
my $settings = $plugin->get_config_obj($scope);
$plugin->load_config( \%plugin_param, $scope );
if ( my $snip_tmpl =
$plugin->config_template( \%plugin_param, $scope ) )
{
my $tmpl;
if ( ref $snip_tmpl ne 'MT::Template' ) {
$tmpl = MT->model('template')->new(
type => 'scalarref',
source => ref $snip_tmpl
? $snip_tmpl
: \$snip_tmpl
# TBD: add path for plugin template directory
);
}
else {
$tmpl = $snip_tmpl;
}

# Process template independent of $app to avoid premature
# localization (give plugin a chance to do L10N first).
$tmpl->param( blog_id => $app->blog->id ) if $app->blog;
$tmpl->param( \%plugin_param );

$app->run_callbacks('plugin_template_param' . $plugin->id,
$app, $scope, $tmpl->param, $tmpl);

$config_html = $tmpl->output()
or $config_html = "Error in configuration template: " . $tmpl->errstr;
$config_html = $plugin->translate_templatized($config_html)
if $config_html =~ m/<(?:__trans|mt_trans) /i;
}
else {

# don't list non-configurable plugins for blog scope...
next if $scope ne 'system';
}
return $config_html;
}

sub build_plugin_table {
my $app = shift;
my (%opt) = @_;
Expand Down Expand Up @@ -218,41 +302,7 @@ sub build_plugin_table {
$app->static_path . $plugin->envelope . '/' . $doc_link;
}

my ($config_html);
my %plugin_param;
my $settings = $plugin->get_config_obj($scope);
$plugin->load_config( \%plugin_param, $scope );
if ( my $snip_tmpl =
$plugin->config_template( \%plugin_param, $scope ) )
{
my $tmpl;
if ( ref $snip_tmpl ne 'MT::Template' ) {
$tmpl = MT->model('template')->new(
type => 'scalarref',
source => ref $snip_tmpl
? $snip_tmpl
: \$snip_tmpl

# TBD: add path for plugin template directory
);
}
else {
$tmpl = $snip_tmpl;
}

# Process template independent of $app to avoid premature
# localization (give plugin a chance to do L10N first).
$tmpl->param( \%plugin_param );
$config_html = $tmpl->output()
or $config_html = "Error in configuration template: " . $tmpl->errstr;
$config_html = $plugin->translate_templatized($config_html)
if $config_html =~ m/<(?:__trans|mt_trans) /i;
}
else {

# don't list non-configurable plugins for blog scope...
next if $scope ne 'system';
}
my $config_html = build_plugin_config_html($app, $plugin,$scope);

# Removed for Melody - obsolete
# if ( $last_fld ne $fld ) {
Expand All @@ -268,6 +318,7 @@ sub build_plugin_table {
# }

my $registry = $plugin->registry;
my $settings = $plugin->get_config_obj($scope);
my $row = {
first => $next_is_first,
plugin_name => $plugin_name,
Expand All @@ -291,6 +342,7 @@ sub build_plugin_table {
plugin_num => $id,
plugin_compat_errors => $registry->{compat_errors},
};

my $block_tags = $plugin->registry('tags', 'block');
my $function_tags = $plugin->registry('tags', 'function');
my $modifiers = $plugin->registry('tags', 'modifier');
Expand Down
62 changes: 62 additions & 0 deletions tmpl/cms/dialog/cfg_plugin.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<mt:setvarblock name="page_title"><__trans phrase="Settings for [_1]" params="<$mt:var name="plugin_name"$>"></mt:setvarblock>

<mt:setvarblock name="html_head" append="1">

<mt:if name="finish">
<script type="text/javascript">
<!--
function init() {
closeDialog();
var w = window;
while (w.parent && (w.parent != w)) w = w.parent;
if (w.onUploadDialogClose) {
w.onUploadDialogClose();
}
}
TC.attachLoadEvent( init );
//-->
</script>
</mt:if>
</mt:setvarblock>

<mt:include name="dialog/header.tmpl">

<mt:if name="error">
<mtapp:statusmsg
id="error"
class="error">
<mt:var name="error">
</mtapp:statusmsg>
</mt:if>

<form method="post" action="<$mt:var name="script_url"$>"
id="plugin-<$mt:var name="plugin_id" dirify="1"$>-form">
<input type="hidden" name="__mode" value="save_plugin_config" />
<mt:if name="blog_id">
<input type="hidden" name="blog_id" value="<$mt:var name="blog_id"$>" />
</mt:if>
<input type="hidden" name="return_args" value="<$mt:var name="return_args" escape="html"$>" />
<input type="hidden" name="plugin_sig" value="<$mt:var name="plugin_sig" escape="html"$>" />
<input type="hidden" name="magic_token" value="<$mt:var name="magic_token"$>" />
<input type="hidden" name="dialog" value="1" />
<fieldset>
<$mt:var name="config_html"$>
</fieldset>
<div class="actions-bar settings-actions-bar">
<div class="actions-bar-inner pkg actions">
<button
mt:mode="save_plugin_config"
type="submit"
class="primary-button"
><__trans phrase="Save Changes"></button>
<mt:if name="plugin_settings_id">
<button
onclick="resetPlugin(getByID('plugin-<mt:var name="plugin_id">-form')); return false"
type="submit"
><__trans phrase="Reset to Defaults"></button>
</mt:if>
</div>
</div>
</form>

<mt:include name="dialog/footer.tmpl">

0 comments on commit 65ffac6

Please sign in to comment.