Skip to content

Commit

Permalink
[#371 state:resolved] Updated to the latest version of Config Assista…
Browse files Browse the repository at this point in the history
…nt to squash deprecation warnings.
  • Loading branch information
byrnereese authored and jayallen committed Oct 23, 2010
1 parent fc840e2 commit 99ed3cc
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 52 deletions.
6 changes: 5 additions & 1 deletion addons/ConfigAssistant.plugin/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ key: ConfigAssistant
author_link: http://www.majordojo.com/
author_name: Byrne Reese
description: This plugin provides a simple YAML based framework for creating plugin and theme configuration options.
version: 2.0.1
version: 2.1.3
static_version: 4
schema_version: 2

Expand Down Expand Up @@ -63,8 +63,12 @@ config_types:
handler: $ConfigAssistant::ConfigAssistant::Plugin::type_link_group
category:
handler: $ConfigAssistant::ConfigAssistant::Plugin::type_category
category_list:
handler: $ConfigAssistant::ConfigAssistant::Plugin::type_category_list
folder:
handler: $ConfigAssistant::ConfigAssistant::Plugin::type_folder
folder_list:
handler: $ConfigAssistant::ConfigAssistant::Plugin::type_folder_list

upgrade_functions:
static_copy:
Expand Down
62 changes: 47 additions & 15 deletions addons/ConfigAssistant.plugin/lib/ConfigAssistant/Init.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ sub init_app {
# runs it sees it and will run the upgrade_function.
# If this isn't the upgrade screen, just quit.
my $cfg = MT->config('PluginSchemaVersion');
if ( $cfg->{$plugin->id} eq '' ) {
# $cfg->{$plugin->id} = '0.1'; ### UNCOMMENT TO TEST UPGRADE ###
if ( ($cfg->{$plugin->id}||'') eq '' ) {
# There is no schema version set. Set one!
$cfg->{$plugin->id} = '0.1';
}
Expand Down Expand Up @@ -197,33 +198,32 @@ sub load_tags {
# First initialize all the tags associated with themes
my @sets = keys %{ $r->{'template_sets'} };
foreach my $set (@sets) {
if ( $r->{'template_sets'}->{$set}->{'options'} ) {
if ( $obj->registry('template_sets', $set, 'options') ) {
foreach my $opt (
keys %{ $r->{'template_sets'}->{$set}->{'options'} } )
keys %{ $obj->registry('template_sets', $set, 'options') } )
{
my $option =
$r->{'template_sets'}->{$set}->{'options'}->{$opt};
my $option = $obj->registry('template_sets', $set, 'options', $opt);

# If the option does not define a tag name,
# then there is no need to register one
next if ( !defined( $option->{tag} ) );
my $tag = $option->{tag};

# TODO - there is the remote possibility that a template set
# will attempt to register a duplicate tag. This case needs to be
# handled properly. Or does it?
# Note: the tag handler takes into consideration the blog_id, the
# template set id and the option/setting name.
# TODO - there is the remote possibility that a template set
# will attempt to register a duplicate tag. This case needs to be
# handled properly. Or does it?
# Note: the tag handler takes into consideration the blog_id, the
# template set id and the option/setting name.
if ( $tag =~ s/\?$// ) {
$tags->{block}->{$tag} = sub {
my $blog = $_[0]->stash('blog');
my $bset = $blog->template_set;
$_[0]->stash( 'field', $bset . '_' . $opt );
$_[0]->stash( 'plugin_ns',
find_theme_plugin($bset)->id );
find_theme_plugin($bset)->id );
$_[0]->stash( 'scope', 'blog' );
runner( '_hdlr_field_cond',
'ConfigAssistant::Plugin', @_ );
'ConfigAssistant::Plugin', @_ );
};
}
elsif ( $tag ne '' ) {
Expand Down Expand Up @@ -279,9 +279,25 @@ sub load_tags {
$_[0]->stash( 'plugin_ns',
find_theme_plugin($bset)->id );
$_[0]->stash( 'scope', 'blog' );
$_[0]->stash( 'show_children', (defined $option->{show_children} ? $option->{show_children} : 1 ) );
runner( '_hdlr_field_link_group',
'ConfigAssistant::Plugin', @_ );
};
} elsif ($option->{'type'} eq 'category_list' or $option->{'type'} eq 'folder_list') {
my $t = $option->{'type'};
my $tag_type = $t eq 'category_list' ? 'Categories' : 'Folders';
my $obj_class = substr($t, 0, index($t, '_list'));
$tags->{block}->{$tag . $tag_type} = sub {
$_[0]->stash('obj_class', $obj_class);
my $blog = $_[0]->stash('blog');
my $bset = $blog->template_set;
$_[0]->stash( 'field', $bset . '_' . $opt );
$_[0]->stash( 'plugin_ns',
find_theme_plugin($bset)->id );
$_[0]->stash( 'scope', 'blog' );
runner( '_hdlr_field_category_list',
'ConfigAssistant::Plugin', @_ );
};
}
}
}
Expand Down Expand Up @@ -340,7 +356,6 @@ sub load_tags {
'ConfigAssistant::Plugin', @_ );
};


} elsif ($option->{'type'} eq 'file') {
$tags->{block}->{$tag . 'Asset'} = sub {
my $blog = $_[0]->stash('blog');
Expand All @@ -352,6 +367,7 @@ sub load_tags {
runner( '_hdlr_field_asset',
'ConfigAssistant::Plugin', @_ );
};

} elsif ($option->{'type'} eq 'link-group') {
$tags->{block}->{$tag . 'Links'} = sub {
my $blog = $_[0]->stash('blog');
Expand All @@ -363,12 +379,28 @@ sub load_tags {
runner( '_hdlr_field_link_group',
'ConfigAssistant::Plugin', @_ );
};

} elsif ($option->{'type'} eq 'category_list' or $option->{'type'} eq 'folder_list') {
my $t = $option->{'type'};
my $tag_type = $t eq 'category_list' ? 'Categories' : 'Folders';
my $obj_class = substr($t, 0, index($t, '_list'));
$tags->{block}->{$tag . $tag_type} = sub {
$_[0]->stash('obj_class', $obj_class);
my $blog = $_[0]->stash('blog');
my $bset = $blog->template_set;
$_[0]->stash( 'field', $bset . '_' . $opt );
$_[0]->stash( 'plugin_ns',
find_theme_plugin($bset)->id );
$_[0]->stash( 'scope', 'blog' );
runner( '_hdlr_field_category_list',
'ConfigAssistant::Plugin', @_ );
};
}
}
}

# Create plugin-specific tags to the static content
if ( $r->{'static_version'} ) {
if ( $r && $r->{'static_version'} ) {
# Create the plugin-specific static file path tag, such as "ConfigAssistantStaticFilePath."
my $tag;
$tag = $obj->id . 'StaticFilePath';
Expand All @@ -382,7 +414,7 @@ sub load_tags {
};
# Create the plugin-specific static web path tag, such as "ConfigAssistantStaticWebPath."
$tag = $obj->id . 'StaticWebPath';
my $url = $app->config('StaticWebPath');
my $url = $app->static_path;
$url .= '/' unless $url =~ m!/$!;
$url .= 'support/plugins/'.$obj->id.'/';
$tags->{function}->{$tag} = sub {
Expand Down
113 changes: 98 additions & 15 deletions addons/ConfigAssistant.plugin/lib/ConfigAssistant/Plugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sub tag_plugin_static_web_path {
$ctx->stash('tag'), $sig)
);
} elsif ( $obj->registry('static_version') ) {
my $url = MT->config('StaticWebPath');
my $url = MT->instance->static_path;
$url .= '/' unless $url =~ m!/$!;
$url .= 'support/plugins/'.$obj->id.'/';
return $url;
Expand All @@ -45,8 +45,8 @@ sub tag_plugin_static_file_path {
$ctx->stash('tag'), $sig)
);
} elsif ( $obj->registry('static_version') ) {
my $url = File::Spec->catdir( MT->config('StaticFilePath'), 'support', 'plugins', $obj->id );
return $url;
return File::Spec->catdir(
MT->instance->static_file_path, 'support', 'plugins', $obj->id );
} else {
return $ctx->error(
MT->translate(
Expand All @@ -60,7 +60,7 @@ sub tag_plugin_static_file_path {
sub theme_options {
my $app = shift;
my ($param) = @_;
my $q = $app->{query};
my $q = $app->can('query') ? $app->query : $app->param;
my $blog = $app->blog;

$param ||= {};
Expand Down Expand Up @@ -115,6 +115,15 @@ sub theme_options {
}
next unless $cond->();
}
if (!$field->{'type'}) {
MT->log(
{
message => "Option '$optname' in template set '$ts' did not declare a type. Skipping"
}
);
next;
}


my $field_id = $ts . '_' . $optname;

Expand Down Expand Up @@ -258,7 +267,7 @@ sub theme_options {
# Code for this method taken from MT::CMS::Plugin
sub save_config {
my $app = shift;
my $q = $app->param;
my $q = $app->can('query') ? $app->query : $app->param;
my $plugin_sig = $q->param('plugin_sig');
my $profile = $MT::Plugins{$plugin_sig};
my $blog_id = $q->param('blog_id');
Expand Down Expand Up @@ -443,7 +452,7 @@ sub type_colorpicker {
sub type_link_group {
my $app = shift;
my ( $ctx, $field_id, $field, $value ) = @_;
my $static = $app->config->StaticWebPath;
my $static = $app->static_path;
$value = '"[]"' if (!$value || $value eq '');
eval "\$value = $value";
if ($@) { $value = '"[]"'; }
Expand Down Expand Up @@ -673,7 +682,7 @@ sub type_radio_image {
my $app = shift;
my ( $ctx, $field_id, $field, $value ) = @_;
my $out;
my $static = $app->config->StaticWebPath;
my $static = $app->static_path;
$out .= " <ul class=\"pkg\">\n";
while ( $field->{values} =~ /\"([^\"]*)\":\"([^\"]*)\",?/g ) {
my ($url,$label) = ($1,$2);
Expand Down Expand Up @@ -812,6 +821,46 @@ sub type_category {
return $out;
}

sub type_folder_list {
my $app = shift;
my ( $ctx, $field_id, $field, $value ) = @_;
$ctx->stash('object_class', 'folder');
return type_category_list($app, @_);
}

sub type_category_list {
my $app = shift;
my ( $ctx, $field_id, $field, $value ) = @_;
$value = defined($value) ? $value: 0;
my @values = ref $value eq 'ARRAY' ? @$value : (0);
my $out;
my $obj_class = $ctx->stash('object_class') || 'category';

my $params = { };
$params->{blog_id} = $app->blog->id;
$params->{parent} = 0 unless $ctx->stash('show_children');

my @cats = MT->model($obj_class)->load( $params,
{ sort => 'label' });
$out .= " <select style=\"width: 300px;height:100px\" name=\"$field_id\" multiple=\"true\">\n";
foreach my $cat (@cats) {
my $found = 0;
foreach (@values) {
if ($cat->id == $_) {
$found = 1;
}
}
$out .=
" <option value=\""
. $cat->id . "\" "
. ( $found ? " selected" : "" ) . ">"
. $cat->label
. "</option>\n";
}
$out .= " </select>\n";
return $out;
}

sub type_folder {
my $app = shift;
my ( $ctx, $field_id, $field, $value ) = @_;
Expand Down Expand Up @@ -889,6 +938,34 @@ sub _hdlr_field_array_contains {
return MT::Template::Context::_hdlr_pass_tokens_else(@_);
}

sub _hdlr_field_category_list {
my $plugin = shift;
my ( $ctx, $args, $cond ) = @_;
my $field = $ctx->stash('field')
or return _no_field($ctx);
my $value = _get_field_value($ctx);
my @ids = split(/,/, $value);
my $class = $ctx->stash('obj_class');

my @categories = MT->model( $class )->load({ id => \@ids });
my $out = '';
my $vars = $ctx->{__stash}{vars};
my $glue = $args->{glue};
for (my $index = 0; $index <= $#categories; $index++) {

local $vars->{__first__} = $index == 0;
local $vars->{__last__} = $index == $#categories;
local $vars->{__odd__} = $index % 2 == 1;
local $vars->{__even__} = $index % 2 == 0;
local $vars->{__index__} = $index;
local $vars->{__size__} = scalar(@categories);

$ctx->stash('category', $categories[$index]);
$out .= $ctx->slurp( $args, $cond ) . ($glue && $index< $#categories ? $glue : '' );
}
return $out;
}

sub _get_field_value {
my ($ctx) = @_;
my $plugin_ns = $ctx->stash('plugin_ns');
Expand All @@ -907,6 +984,9 @@ sub _get_field_value {
else {
$value = $plugin->get_config_value($field);
}
if (ref $value eq 'ARRAY') {
$value = join (',', @$value);
}
return $value;
}

Expand Down Expand Up @@ -1157,9 +1237,10 @@ sub plugin_options {
sub entry_search_api_prep {
my $app = MT->instance;
my ($terms, $args, $blog_id) = @_;
my $q = $app->can('query') ? $app->query : $app->param;

$terms->{blog_id} = $blog_id if $blog_id;
$terms->{status} = $app->param('status') if ($app->param('status'));
$terms->{status} = $q->param('status') if ($q->param('status'));

my $search_api = $app->registry("search_apis");
my $api = $search_api->{entry};
Expand All @@ -1176,9 +1257,10 @@ sub entry_search_api_prep {

sub list_entry_mini {
my $app = shift;
my $q = $app->can('query') ? $app->query : $app->param;

my $blog_id = $app->param('blog_id') || 0;
my $obj_type = $app->param('class') || 'entry';
my $blog_id = $q->param('blog_id') || 0;
my $obj_type = $q->param('class') || 'entry';
my $pkg = $app->model($obj_type) or return "Invalid request: unknown class $obj_type";

my $terms;
Expand All @@ -1200,8 +1282,8 @@ sub list_entry_mini {
params => {
panel_searchable => 1,
edit_blog_id => $blog_id,
edit_field => $app->param('edit_field'),
search => $app->param('search'),
edit_field => $q->param('edit_field'),
search => $q->param('search'),
blog_id => $blog_id,
},
code => sub {
Expand Down Expand Up @@ -1234,13 +1316,14 @@ sub list_entry_mini {

sub select_entry {
my $app = shift;
my $q = $app->can('query') ? $app->query : $app->param;

my $class = $app->param('class') || 'entry';
my $obj_id = $app->param('id')
my $class = $q->param('class') || 'entry';
my $obj_id = $q->param('id')
or return $app->errtrans('No id');
my $obj = MT->model($class)->load($obj_id)
or return $app->errtrans( 'No entry #[_1]', $obj_id );
my $edit_field = $app->param('edit_field')
my $edit_field = $q->param('edit_field')
or return $app->errtrans('No edit_field');

my $plugin = MT->component('ConfigAssistant') or die "OMG NO COMPONENT!?!";
Expand Down
Loading

0 comments on commit 99ed3cc

Please sign in to comment.