--- /dev/null
+package Newslash::Plugin::NewslashHelpers;
+use Mojo::Base 'Mojolicious::Plugin';
+use Mojo::JSON qw(to_json from_json);
+
+use Template;
+
+sub register {
+ my ($self, $app, $conf) = @_;
+ $self->{app} = $app;
+
+ for my $k (qw[boxes]) {
+ $app->helper($k => $self->can("_$k"))
+ }
+}
+
+sub _boxes {
+ my $c = shift;
+
+ my $users = $c->model('users');
+ my $user = $c->stash("user");
+ my $user_boxes = $users->sidebar_items(uid => $user->{uid});
+
+ my $items = [];
+ my $tt = Template->new();
+
+ for my $box (@$user_boxes) {
+ #$c->app->log->debug("load box $box->{name}");
+ my $q_params = {};
+ if ($box->{query_params}) {
+ $q_params = from_json($box->{query_params});
+ }
+ else {
+ if (!$box->{type} || $box->{type} ne "custom") {
+ push @$items, { type => $box->{type}, name => $box->{name}, contents => $box->{template} };
+ next;
+ }
+ }
+ if (!$q_params) {
+ $c->app->log->warn("Sidebar: invalid query_params in $box->{name}");
+ next;
+ }
+ $q_params->{limit} = $box->{limit} || 20;
+
+ # regularize query parameters
+ my @keys = sort { $a cmp $b } keys %$q_params;
+ my @params;
+ for my $k (@keys) {
+ push @params, $k;
+ push @params, $q_params->{$k};
+ }
+ push @params, "_user";
+ push @params, $user;
+
+ my $rs;
+ if ($box->{type} eq "custom") {
+ my $custom_boxes = $c->app->custom_boxes;
+ if ($custom_boxes) {
+ $rs = $custom_boxes->query($box->{model}, $c, @params);
+ }
+ }
+ else {
+ $rs = $c->model_cache->select($box->{model}, 60, @params);
+ }
+
+ my $result = '';
+ my $template = $box->{template};
+ next if !$rs || !$template;
+
+ if ($tt->process(\$template, {items => $rs}, \$result)) {
+ push @$items, { type => $box->{type}, name => $box->{name}, contents => $result };
+ }
+ }
+ return $items;
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::NewslashHelper - Newslash helpers plugin
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $app->plugin('Newslash::Plugin::NewslashHelpers');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::NewslashHelpers> is collection of helpers for L<Newslash>.
+
+
+=head1 HELPERS
+
+L<Mojolicious::Plugin::NewslashHelpers> implements the following helpers.
+
+=head2 boxes
+
+ [% helpers.boxes() %]
+
+Fetch box contents for current user and returns them.
+
+=head1 METHODS
+
+=head2 register
+
+ $plugin->register(Mojolicious->new);
+
+Register helpers in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut
$vars->{Newslash} = {};
$vars->{Newslash}->{config} = $c->app->config;
+ $vars->{helpers} = {};
+
+ # register helpers
+ my $helpers = $renderer->helpers;
+ for my $helper (keys %$helpers) {
+ $vars->{helpers}->{$helper} = sub { $helpers->{$helper}->($c, @_); };
+ }
+
if ($app->renderer_helper) {
my $funcs = $app->renderer_helper->get_functions('Newslash');
for my $name (keys %$funcs) {
$vars->{NS}->{$k} = $self->{NS_plugin};
}
}
- $vars->{NS}->{sidebar_items} = sidebar_func($c);
+
for my $key (keys %{$app->config}) {
$vars->{$key} = $app->config->{$key};
}
return $self->{tt}->error();
}
-sub sidebar_func {
- my $c = shift;
-
- my $f = sub {
- my $self = shift;
- #my $boxes = $c->model('boxes');
- my $users = $c->model('users');
- my $user = $c->stash("user");
- my $user_boxes = $users->sidebar_items(uid => $user->{uid});
-
- my $items = [];
- my $tt = Template->new();
-
- for my $box (@$user_boxes) {
- #$c->app->log->debug("load box $box->{name}");
- my $q_params = {};
- if ($box->{query_params}) {
- $q_params = from_json($box->{query_params});
- }
- else {
- if (!$box->{type} || $box->{type} ne "custom") {
- push @$items, { type => $box->{type}, name => $box->{name}, contents => $box->{template} };
- next;
- }
- }
- if (!$q_params) {
- $c->app->log->warn("Sidebar: invalid query_params in $box->{name}");
- next;
- }
- $q_params->{limit} = $box->{limit} || 20;
-
- # regularize query parameters
- my @keys = sort { $a cmp $b } keys %$q_params;
- my @params;
- for my $k (@keys) {
- push @params, $k;
- push @params, $q_params->{$k};
- }
- push @params, "_user";
- push @params, $user;
-
- my $rs;
- if ($box->{type} eq "custom") {
- my $custom_boxes = $c->app->custom_boxes;
- if ($custom_boxes) {
- $rs = $custom_boxes->query($box->{model}, $c, @params);
- }
- }
- else {
- $rs = $c->model_cache->select($box->{model}, 60, @params);
- }
-
- my $result = '';
- my $template = $box->{template};
- next if !$rs || !$template;
-
- if ($tt->process(\$template, {items => $rs}, \$result)) {
- push @$items, { type => $box->{type}, name => $box->{name}, contents => $result };
- }
- }
- return $items;
- };
- return $f;
-}
1;
# secret key for hasing
$app->secrets([$app->config->{System}->{secret_key},]);
+ # Helpers for Newslash
+ $app->plugin('Newslash::Plugin::NewslashHelpers');
+
# stash for plugins
#$app->config->{_Plugins} = {};
[%- IF page.type == "timeline" || page.type == "archive" || page.type == "tag" -%]
[%- ad_code("sidebar-top") -%]
- [%- FOREACH box IN NS.sidebar_items() -%]
+ [%#- FOREACH box IN NS.sidebar_items() -%]
+ [%- FOREACH box IN helpers.boxes() -%]
<div class="sidebar-item [% box.type %] [% box.name %]">
[%- box.contents -%]
</div>