From 8126dfe6604256c580a74c05d60037e37cd4ba2d Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 27 Nov 2010 14:41:18 -0500 Subject: [PATCH] [#299 state:resolved] Integrated the widget set loop functionality into Melody. --- lib/MT/Template/ContextHandlers.pm | 252 +++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/lib/MT/Template/ContextHandlers.pm b/lib/MT/Template/ContextHandlers.pm index 5fedf80fa..5d76daf90 100644 --- a/lib/MT/Template/ContextHandlers.pm +++ b/lib/MT/Template/ContextHandlers.pm @@ -56,6 +56,7 @@ sub core_tags { 'IfCategory?' => \&_hdlr_if_category, 'EntryIfCategory?' => \&_hdlr_if_category, + 'WidgetSetLoop' => \&_hdlr_widget_loop, 'IfExternalUserManagement?' => \&_hdlr_if_external_user_management, @@ -376,6 +377,13 @@ sub core_tags { Link => \&_hdlr_link, WidgetManager => \&_hdlr_widget_manager, WidgetSet => \&_hdlr_widget_manager, + 'WidgetSetName' => \&_hdlr_widget_set_name, + 'WidgetSetID' => \&_hdlr_widget_set_id, + 'WidgetContent' => \&_hdlr_widget_content, + 'WidgetCount' => \&_hdlr_widget_count, + 'WidgetName' => \&_hdlr_widget_name, + 'WidgetID' => \&_hdlr_widget_id, + 'WidgetIdentifier' => \&_hdlr_widget_identifier, ErrorMessage => \&_hdlr_error_message, @@ -21923,6 +21931,250 @@ sub _hdlr_if_commenter_registration_allowed { && ( $blog && $blog->allow_commenter_regist ); } + +#####Widgetset Loop tags + +########################################################################### + +=head2 WidgetCount + +Function tag that returns the number of widgets in a widget set. + +B + +=over 4 + +=item * name (required) +Name of the widget set. + +=back + +=for tags widgets + +=cut + + +sub _hdlr_widget_count { + my ($ctx, $args, $cond) = @_; + + my $plugin = MT->component('WidgetSetLoop'); + my $ws = $args->{name} or return $ctx->error($plugin->translate('WidgetSet name required.')); + my $blog_id = $args->{blog_id} || $ctx->stash('blog_id') || 0; + + my $widgetset = MT->model('template')->load({ name => $ws, + blog_id => $blog_id ? [ 0, $blog_id ] : 0, + type => 'widgetset' }, + { sort => 'blog_id', + direction => 'descend' }) + or return $ctx->error(MT->translate( "Specified WidgetSet '[_1]' not found.", $ws )); + + return scalar( split(/,/, $widgetset->modulesets) ); +} + +########################################################################### + +=head2 WidgetSetLoop + +Block loop tag that lets you loop over the contents of a widget set instead of loading the +widgets all at once. + +B + +=over 4 + +=item * name (required) +Name of the widget set. + +=item * blog_id (optional) +Load widgetset from another blog. This will not load them with the context of the other blog. +Rather, it is intended to let blogs mix and match each other's widget sets or to let one blog +host widgets for several others without having to make them into system widgets. + +=back + +It provides the following meta variables: + +=over 4 + +=item * __size__ The size of the widget set. +=item * __first__ Boolean variable set when the current iteration is the first widget in the set. +=item * __last__ Boolean variable set when the current iteration is the last widget in the set. +=item * __index__ An integer variable that is the current position in the widget loop. +=item * __odd__ Boolean variable set when the current iteration is even numbered in the loop. +=item * __even__ Boolean variable set when the current iteration is odd numbered in the loop. + +=back + +=for tags widgets + +=cut + +sub _hdlr_widget_loop { + my ($ctx, $args, $cond) = @_; + my $tmpl_name = $args->{name} + or return $ctx->error(MT->translate("Template name is required.")); + my $blog_id = $args->{blog_id} || $ctx->stash('blog_id') || 0; + + my $tmpl = MT->model('template')->load({ name => $tmpl_name, + blog_id => $blog_id ? [ 0, $blog_id ] : 0, + type => 'widgetset' }, + { sort => 'blog_id', + direction => 'descend' }) + or return $ctx->error(MT->translate( "Specified WidgetSet '[_1]' not found.", $tmpl_name )); + + my $modulesets = $tmpl->modulesets; + my @selected = split ',', $modulesets; + my $vars = $ctx->{__stash}{vars} ||= {}; + my $out = ''; + my $builder = $ctx->stash('builder'); + my $tokens = $ctx->stash('tokens'); + $ctx->stash('widgetset', $tmpl); + local $vars->{__size__} = scalar(@selected); + my $glue = $args->{glue} || ''; + for (my $index = 0; $index <= $#selected; $index++) { + my $widget = MT->model('template')->load({ id => $selected[$index] }); + local $vars->{__first__} = ($index == 0); + local $vars->{__last__} = ($index == $#selected-1); + local $vars->{__index__} = $index; + local $vars->{__odd__} = $index % 2 == 1; + local $vars->{__even__} = $index % 2 == 0; + $ctx->stash('widget', $widget); + my $res = $builder->build($ctx, $tokens, $cond); + return $ctx->error($builder->errstr) unless defined $res; + + $out .= ($index < $#selected and $glue ? sprintf('%s%s', $res, $glue) : $res); + } + + $ctx->stash('widgeset', undef); + + return $out; +} + +########################################################################### + +=head2 WidgetSetName + +Used within a WidgetSetLoop context. This returns the name of the widget set. + +=for tags widgets + +=cut + +sub _hdlr_widget_set_name { + my ($ctx, $args) = @_; + my $ws = $ctx->stash('widgetset') + or return $ctx->error('You called a WidgetSetName outside of a WidgetLoop context'); + return $ws->name; +} + +########################################################################### + +=head2 WidgetSetId + +Used within a WidgetSetLoop context. This returns the ID number of the widget set. + +=for tags widgets + +=cut + + +sub _hdlr_widget_set_id { + my ($ctx, $args) = @_; + my $ws = $ctx->stash('widgetset') + or return $ctx->error('You called a WidgetSetID outside of a WidgetLoop context'); + return $ws->id; +} + +########################################################################### + +=head2 WidgetContent + +Used within a WidgetSetLoop context. This returns the content of the widget. It supports the +caching options that provides. Briefly, these are: + +=over 4 + +=item * cache +=item * cache_key +=item * ttl + +=back + +=for tags widgets + +=cut + + +sub _hdlr_widget_content { + my ($ctx, $args, $cond) = @_; + my $widget = $ctx->stash('widget') + or return $ctx->error('No widget could be loaded.'); ###TODO more robust error handling! + return '' unless $widget->text; + + local $args->{widget} = $widget->name; + + return MT::Template::Context::_include_module(@_); +} + +sub _hdlr_widget_return_property +{ + my ($ctx, $args, $method) = @_; + my $widget = $ctx->stash('widget') + or return $ctx->error('No widget could be loaded.'); + return '' unless $widget->$method; + + return $widget->$method; +} + +########################################################################### + +=head2 WidgetName + +Used within a WidgetSetLoop context. This returns the name of the widget currently in context. + +=for tags widgets + +=cut + + +sub _hdlr_widget_name { + my ($ctx, $args) = @_; + return _hdlr_widget_return_property($ctx, $args, 'name'); +} + +########################################################################### + +=head2 WidgetID + +Used within a WidgetSetLoop context. This returns the id of the widget currenlty in context. + +=for tags widgets + +=cut + + +sub _hdlr_widget_id { + my ($ctx, $args) = @_; + return _hdlr_widget_return_property($ctx, $args, 'id'); +} + +########################################################################### + +=head2 WidgetIdentifier + +Used within a WidgetSetLoop context. This returns the identifier of the widget currently in context. + +=for tags widgets + +=cut + + +sub _hdlr_widget_identifier { + my ($ctx, $args) = @_; + return _hdlr_widget_return_property($ctx, $args, 'identifier'); +} + + 1; __END__