1 package Newslash::Plugin::CustomBoxes;
2 use Mojo::Base 'Mojolicious::Plugin';
4 use DateTime::Format::MySQL;
7 my ($self, $app, $conf) = @_;
10 $app->helper(custom_boxes => sub { state $custom_boxes = $self });
14 my ($self, $model, $c, @rest) = @_;
17 return $self->_poll($params) if $model eq "poll";
18 return $self->_mysite($params) if $model eq "mysite";
19 return $self->_thisday($params) if $model eq "thisday";
23 sub app { return shift->{app}; }
26 my ($self, $params) = @_;
27 my $polls = $self->app->model("polls");
28 my $polls_config = $self->app->config("Polls") || {};
31 if ($polls_config->{TTL} &&
32 $polls_config->{TTL_unit} =~ m/\A(years|months|days|hours|minutes|seconds)\z/) {
33 my $base_dt = DateTime->now;
34 $base_dt->subtract($polls_config->{TTL_unit} => $polls_config->{TTL});
36 $limit_timestamp = DateTime::Format::MySQL->format_datetime($base_dt);
39 my $poll = $polls->select(order_by => { create_time => "DESC" },
40 create_time => { gt => $limit_timestamp },
49 my ($self, $params) = @_;
50 my $user = $params->{_user} || {};
51 my $my_links = $user->{config}->{ui}->{my_links};;
52 $my_links = "" if !defined $my_links;
58 my ($self, $params) = @_;
59 my $user = $params->{_user} || {};
60 my $offset_sec = $user->{config}->{ui}->{offset_sec} || 0;
61 my $stories = $self->app->model("stories");
63 my $now = DateTime->now;
64 $now->add(seconds => -$offset_sec) if $offset_sec;
67 for (my $year = $now->year; $year > 2000; $year--) {
68 my $rs = $stories->select(year => $year,
71 offset_sec => $offset_sec,
72 order_by => { hits => "DESC" },
76 $rs->[0]->{year} = $year;
80 my @rs = sort { $b->{hits} <=> $a->{hits} } @items;
82 my $limit = $params->{limit} || 8;
86 @rs = sort { $b->{year} <=> $a->{year} } @rs;