| 39 | | tags => { |
|---|
| 40 | | function => { |
|---|
| 41 | | ArchiveFiscalYear => \&archive_fiscal_year, |
|---|
| 42 | | }, |
|---|
| 43 | | }, |
|---|
| 44 | | 'archive_types' => { |
|---|
| 45 | | 'FiscalYearly' => |
|---|
| 46 | | ArchiveType( |
|---|
| 47 | | name => 'FiscalYearly', |
|---|
| 48 | | archive_label => \&archive_label, |
|---|
| 49 | | archive_file => \&archive_file, |
|---|
| 50 | | archive_title => \&archive_title, |
|---|
| 51 | | date_range => \&date_range, |
|---|
| 52 | | next_archive_entry => \&next_archive_entry, |
|---|
| 53 | | previous_archive_entry => \&previous_archive_entry, |
|---|
| 54 | | archive_group_iter => \&archive_group_iter, |
|---|
| 55 | | archive_group_entries => \&archive_group_entries, |
|---|
| 56 | | archive_entries_count => \&archive_entries_count, |
|---|
| 57 | | default_archive_templates => [ |
|---|
| 58 | | ArchiveFileTemplate( |
|---|
| 59 | | label => 'fiscal/yyyy/index.html', |
|---|
| 60 | | template => 'fiscal/<$MTArchiveFiscalYear$>/%i', |
|---|
| 61 | | default => 1 |
|---|
| 62 | | ), |
|---|
| 63 | | ], |
|---|
| 64 | | dynamic_template => 'fiscal/<$MTArchiveFiscalYear$>', |
|---|
| 65 | | dynamic_support => 1, |
|---|
| 66 | | date_based => 1, |
|---|
| 67 | | # ??? i don't know what it really means ??? |
|---|
| 68 | | template_params => { |
|---|
| 69 | | datebased_only_archive => 1, |
|---|
| 70 | | datebased_fiscal_yearly_archive => 1, |
|---|
| 71 | | module_fiscal_yearly_archives => 1, |
|---|
| 72 | | main_template => 1, |
|---|
| 73 | | archive_template => 1, |
|---|
| 74 | | archive_class => "datebased-fiscal-yearly-archive", |
|---|
| 75 | | }, |
|---|
| 76 | | ), |
|---|
| 77 | | } |
|---|
| | 39 | tags => { |
|---|
| | 40 | function => { |
|---|
| | 41 | ArchiveFiscalYear => \&archive_fiscal_year, |
|---|
| | 42 | }, |
|---|
| | 43 | }, |
|---|
| | 44 | archive_types => { |
|---|
| | 45 | 'FiscalYearly' => 'FiscalYearlyArchives::FiscalYearly', |
|---|
| | 46 | 'Author-FiscalYearly' => 'FiscalYearlyArchives::AuthorFiscalYearly', |
|---|
| | 47 | 'Category-FiscalYearly' => 'FiscalYearlyArchives::CategoryFiscalYearly', |
|---|
| | 48 | }, |
|---|
| 119 | | sub start_end_fiscal_year { |
|---|
| 120 | | my ($ts) = @_; |
|---|
| 121 | | my ($start_year, $start_month) = (ts2fiscal($ts), fiscal_start_month()); |
|---|
| 122 | | my $start = sprintf("%04d%02d%02d000000", $start_year, $start_month, 1); |
|---|
| 123 | | return $start unless wantarray; |
|---|
| 124 | | |
|---|
| 125 | | my ($end_year, $end_month, $end_day); |
|---|
| 126 | | if ($start_month == 1) { |
|---|
| 127 | | ($end_year, $end_month, $end_day) = ($start_year, 12, 31); |
|---|
| 128 | | } else { |
|---|
| 129 | | ($end_year, $end_month) = ($start_year + 1, $start_month - 1); |
|---|
| 130 | | $end_day = MT::Util::days_in($end_month, $end_year); |
|---|
| 131 | | } |
|---|
| 132 | | my $end = sprintf("%04d%02d%02d235959", $end_year, $end_month, $end_day); |
|---|
| 133 | | ($start, $end); |
|---|
| 134 | | } |
|---|
| 135 | | |
|---|
| 136 | | sub archive_label { |
|---|
| 137 | | $plugin->translate('FISCAL-YEARLY_ADV'); |
|---|
| 138 | | } |
|---|
| 139 | | |
|---|
| 140 | | sub archive_file { |
|---|
| 141 | | my ($ctx, %param) = @_; |
|---|
| 142 | | my $timestamp = $param{Timestamp}; |
|---|
| 143 | | my $file_tmpl = $param{Template}; |
|---|
| 144 | | my $blog = $ctx->{__stash}{blog}; |
|---|
| 145 | | |
|---|
| 146 | | my $file; |
|---|
| 147 | | if ($file_tmpl) { |
|---|
| 148 | | ($ctx->{current_timestamp}, $ctx->{current_timestamp_end}) = |
|---|
| 149 | | start_end_fiscal_year($timestamp); |
|---|
| 150 | | } else { |
|---|
| 151 | | my $year = ts2fiscal($timestamp); |
|---|
| 152 | | $file = sprintf("04d/index", $year); |
|---|
| 153 | | } |
|---|
| 154 | | $file; |
|---|
| 155 | | } |
|---|
| 156 | | |
|---|
| 157 | | sub archive_title { |
|---|
| 158 | | my ($ctx, $entry_or_ts) = @_; |
|---|
| 159 | | my $ts = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts; |
|---|
| 160 | | my $year = ts2fiscal($ts); |
|---|
| 161 | | my $lang = lc MT->current_language || 'en_us'; |
|---|
| 162 | | $lang = 'ja' if lc($lang) eq 'jp'; |
|---|
| 163 | | $lang eq 'ja' ? $year . '年度' : $year; |
|---|
| 164 | | } |
|---|
| 165 | | |
|---|
| 166 | | sub date_range { start_end_fiscal_year(@_) } |
|---|
| 167 | | |
|---|
| 168 | | sub next_archive_entry { _adjacent_archive_entry(@_, 'next' ) } |
|---|
| 169 | | sub previous_archive_entry { _adjacent_archive_entry(@_, 'previous') } |
|---|
| 170 | | |
|---|
| 171 | | sub _adjacent_archive_entry { |
|---|
| 172 | | my ( $param, $order ) = @_; |
|---|
| 173 | | |
|---|
| 174 | | $order = ( $order eq 'previous' ) ? 'descend' : 'ascend'; |
|---|
| 175 | | my $ts = $param->{ts}; |
|---|
| 176 | | my $blog_id = $param->{blog_id} || ($param->{blog} ? $param->{blog}->id : undef); |
|---|
| 177 | | |
|---|
| 178 | | # if $param->{entry} given, override $ts and $blog_id. |
|---|
| 179 | | if (my $e = $param->{entry}) { |
|---|
| 180 | | $ts = $e->authored_on; |
|---|
| 181 | | $blog_id = $e->blog_id; |
|---|
| 182 | | } |
|---|
| 183 | | my ( $start, $end ) = start_end_fiscal_year($ts); |
|---|
| 184 | | $ts = ( $order eq 'descend' ) ? $start : $end; |
|---|
| 185 | | |
|---|
| 186 | | my $entry = MT::Entry->load({ |
|---|
| 187 | | status => MT::Entry::RELEASE(), |
|---|
| 188 | | $blog_id ? ( blog_id => $blog_id ) : (), |
|---|
| 189 | | }, { |
|---|
| 190 | | limit => 1, |
|---|
| 191 | | 'sort' => 'authored_on', |
|---|
| 192 | | direction => $order, |
|---|
| 193 | | start_val => $ts, |
|---|
| 194 | | }); |
|---|
| 195 | | $entry; |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | sub archive_group_iter { |
|---|
| 199 | | my ($ctx, $args) = @_; |
|---|
| 200 | | my $blog = $ctx->stash('blog'); |
|---|
| 201 | | |
|---|
| 202 | | my $sort_order = ($args->{sort_order} || '') eq 'ascend' ? 'ascend' : 'descend'; |
|---|
| 203 | | my $order = $sort_order eq 'ascend' ? 'asc' : 'desc'; |
|---|
| 204 | | |
|---|
| 205 | | my $iter = MT->model('entry')->count_group_by({ |
|---|
| 206 | | blog_id => $blog->id, |
|---|
| 207 | | status => MT::Entry::RELEASE(), |
|---|
| 208 | | }, { |
|---|
| 209 | | group => ["extract(year from authored_on)", "extract(month from authored_on)"], |
|---|
| 210 | | sort => "extract(year from authored_on) $order, extract(month from authored_on) $order", |
|---|
| 211 | | }) |
|---|
| 212 | | or return $ctx->error("Couldn't get FiscalYearly archive list"); |
|---|
| 213 | | |
|---|
| 214 | | # dirrty! |
|---|
| 215 | | my @count_groups; |
|---|
| 216 | | my $prev_year; |
|---|
| 217 | | while (my @row = $iter->()) { |
|---|
| 218 | | my $ts = sprintf("%04d%02d%02d000000", $row[1], $row[2], 1); |
|---|
| 219 | | my ($start, $end) = start_end_fiscal_year($ts); |
|---|
| 220 | | my $year = ts2fiscal($ts); |
|---|
| 221 | | if (defined $prev_year && $prev_year == $year) { |
|---|
| 222 | | $count_groups[-1]->{count} += $row[0]; |
|---|
| 223 | | } else { |
|---|
| 224 | | push @count_groups, { |
|---|
| 225 | | count => $row[0], |
|---|
| 226 | | fiscal_year => $year, |
|---|
| 227 | | start => $start, |
|---|
| 228 | | end => $end, |
|---|
| 229 | | }; |
|---|
| 230 | | $prev_year = $year; |
|---|
| 231 | | } |
|---|
| 232 | | } |
|---|
| 233 | | my $lastn = $args->{lastn}; |
|---|
| 234 | | splice(@count_groups, $lastn) if $lastn; |
|---|
| 235 | | |
|---|
| 236 | | return sub { |
|---|
| 237 | | while (my $group = shift(@count_groups)) { |
|---|
| 238 | | return ($group->{count}, %$group); |
|---|
| 239 | | } |
|---|
| 240 | | undef; |
|---|
| 241 | | }; |
|---|
| 242 | | } |
|---|
| 243 | | |
|---|
| 244 | | sub archive_group_entries { |
|---|
| 245 | | my ($ctx, %param) = @_; |
|---|
| 246 | | my $ts = sprintf("%04d%02d%02d000000", $param{fiscal_year}, fiscal_start_month(), 1) |
|---|
| 247 | | if %param; |
|---|
| 248 | | my ($start, $end); |
|---|
| 249 | | if ($ts) { |
|---|
| 250 | | ($start, $end) = start_end_fiscal_year($ts); |
|---|
| 251 | | $ctx->{current_timestamp} = $start; |
|---|
| 252 | | $ctx->{current_timestamp_end} = $end; |
|---|
| 253 | | } else { |
|---|
| 254 | | $start = $ctx->{current_timestamp}; |
|---|
| 255 | | $end = $ctx->{current_timestamp_end}; |
|---|
| 256 | | } |
|---|
| 257 | | my $blog = $ctx->stash('blog'); |
|---|
| 258 | | my @entries = MT->model('entry')->load({ |
|---|
| 259 | | blog_id => $blog->id, |
|---|
| 260 | | status => MT::Entry::RELEASE(), |
|---|
| 261 | | authored_on => [$start, $end], |
|---|
| 262 | | }, { |
|---|
| 263 | | range => { authored_on => 1 }, |
|---|
| 264 | | 'sort' => 'authored_on', |
|---|
| 265 | | 'direction' => 'descend', |
|---|
| 266 | | }) |
|---|
| 267 | | or return $ctx->error("Couldn't get FiscalYearly archive list"); |
|---|
| 268 | | \@entries; |
|---|
| 269 | | } |
|---|
| 270 | | |
|---|
| 271 | | sub archive_entries_count { |
|---|
| 272 | | my ($blog, $at, $entry) = @_; |
|---|
| 273 | | my $ts = $entry->authored_on; |
|---|
| 274 | | my ($start, $end) = start_end_fiscal_year($ts) |
|---|
| 275 | | if $ts; |
|---|
| 276 | | my $count = MT->model('entry')->count({ |
|---|
| 277 | | blog_id => $blog->id, |
|---|
| 278 | | status => MT::Entry::RELEASE(), |
|---|
| 279 | | $ts ? (authored_on => [$start, $end]) : (), |
|---|
| 280 | | }, { |
|---|
| 281 | | $ts ? (range => { authored_on => 1 }) : (), |
|---|
| 282 | | }); |
|---|
| 283 | | $count; |
|---|
| 284 | | } |
|---|
| 285 | | |
|---|