1 package Newslash::Web::Controller::Archive;
2 use Mojo::Base 'Mojolicious::Controller';
3 use Mojo::Util qw(dumper);
4 use Mojo::JSON qw(decode_json encode_json);
8 use DateTime::Format::MySQL;
9 use Newslash::Util::Items;
12 my ($c, $type, $unit) = @_;
14 my $year = $c->stash('year');
15 my $month = $c->stash('month');
16 my $day = $c->stash('day');
21 if (!$year || $year !~ m/^\d{2}$/) {
22 $year = DateTime->now->year - 2000;
23 $month = DateTime->now->month;
24 $month = "0" . $month if ($month < 10);
29 if (!$month || $month !~ m/^\d{2}$/) {
32 if ($unit eq "month" || $unit eq "day") {
37 if (!$day || $day !~ m/^\d{2}$/) {
44 if ($unit eq "year") {
45 $r_path = "/$type/$year/" if $do_redirect;
46 return ($year, $month, $day, $r_path);
48 if ($unit eq "month") {
49 $r_path = "/$type/$year/$month/" if $do_redirect;
50 return ($year, $month, $day, $r_path);
53 $r_path = "/$type/$year/$month/$day/" if $do_redirect;
54 return ($year, $month, $day, $r_path);
58 $year = DateTime->now->year - 2000;
59 $month = DateTime->now->month;
60 $month = "0" . $month if ($month < 10);
62 $r_path = "/$type/$year/$month/$day/";
63 return ($year, $month, $day, $r_path);
70 my $user = $c->stash('user');
71 my $model = $params->{model};
72 my $type = $params->{type};
73 my $unit = $params->{unit} || "day";
75 my ($year, $month, $day, $r_path) = _check_params($c, $type, $unit);
79 $c->redirect_to($r_path);
84 my $offset_sec = $user->{config}->{ui}->{offset_sec} || 0;
85 my $q_params = { year => $year,
88 offset_sec => $offset_sec,
89 order_by => {create_time => "DESC"},
92 my $unwanted_keys = [];
93 $unwanted_keys = [qw(month day)] if ($unit eq "year");
94 $unwanted_keys = [qw(day)] if ($unit eq "month");
95 for my $k (@$unwanted_keys) {
96 delete $q_params->{$k};
99 if (!$user->{author}) {
100 $q_params->{hide_future} = 1;
101 $q_params->{public_only} = 1;
104 my $items = $model->select(%$q_params);
106 delete $q_params->{$unit};
107 delete $q_params->{order_by};
108 my $counts = $model->count(%$q_params);
113 my $dt = DateTime->new(year => $year,
116 my $prev_items = $model->select(until => $dt,
117 offset_sec => $offset_sec,
118 order_by => {create_time => "DESC"},
119 hide_future => $user->{author} ? 0 : 1,
120 public_only => $user->{author} ? 0 : 1,
124 $dt->add(days => 1) if $unit eq "day";
125 $dt->add(months => 1) if $unit eq "month";
126 my $next_items = $model->select(since => $dt,
127 offset_sec => $offset_sec,
128 order_by => {create_time => "ASC"},
129 hide_future => $user->{author} ? 0 : 1,
130 public_only => $user->{author} ? 0 : 1,
133 if ($prev_items && @$prev_items) {
134 $prev = Newslash::Util::Items->get_date_string_from_item($prev_items->[0], $offset_sec, $unit);
137 if ($next_items && @$next_items) {
138 $next = Newslash::Util::Items->get_date_string_from_item($next_items->[0], $offset_sec, $unit);
143 $dt = DateTime->new(year => $year,
146 my ($prev_month, $prev_month_date);
147 $dt->add(days => -1);
148 $prev_items = $model->select(until => $dt,
149 offset_sec => $offset_sec,
150 order_by => {create_time => "DESC"},
151 hide_future => $user->{author} ? 0 : 1,
152 public_only => $user->{author} ? 0 : 1,
155 if ($prev_items && @$prev_items) {
156 $dt = $c->format_timestamp(mysql => $prev_items->[0]->{create_time},
157 format => "datetime",
160 $dt->set(day => 1, hour => 0, minute => 0, second => 0);
161 $prev_items = $model->select(since => $dt,
162 offset_sec => $offset_sec,
163 order_by => {create_time => "ASC"},
164 hide_future => $user->{author} ? 0 : 1,
165 public_only => $user->{author} ? 0 : 1,
168 if ($prev_items && @$prev_items) {
169 $prev_month_date = Newslash::Util::Items->get_date_string_from_item($prev_items->[0], $offset_sec, "day");
170 $prev_month_date =~ m/^\d+\/0?(\d+)/;
176 $dt = DateTime->new(year => $year,
179 my ($next_month, $next_month_date);
180 $dt->add(months => 1);
181 $next_items = $model->select(since => $dt,
182 offset_sec => $offset_sec,
183 order_by => {create_time => "ASC"},
184 hide_future => $user->{author} ? 0 : 1,
185 public_only => $user->{author} ? 0 : 1,
188 if ($next_items && @$next_items) {
189 $next_month_date = Newslash::Util::Items->get_date_string_from_item($next_items->[0], $offset_sec, "day");
190 $next_month_date =~ m/^\d+\/0?(\d+)/;
194 my $page = { content_type => $type,
201 $c->render(template => 'archive/index',
207 prev_month => $prev_month,
208 next_month => $next_month,
209 prev_month_date => $prev_month_date,
210 next_month_date => $next_month_date,
211 no_ad => (@items == 0),
221 $c->_render_archive(model => $c->model("stories"),
228 $c->_render_archive(model => $c->model("journals"),
235 $c->_render_archive(model => $c->model("comments"),
242 $c->_render_archive(model => $c->model("submissions"),
243 type => "submission",
249 $c->_render_archive(model => $c->model("polls"),