Show
Ignore:
Timestamp:
05/21/08 16:50:44 (8 months ago)
Author:
ogawa
Message:

Now being compatible with MT4.15 ArchiveType?.
Add Category-FiscalYearly? and Author-FiscalYearly? archivers.

Location:
FiscalYearlyArchives/trunk/FiscalYearlyArchives
Files:
4 added
4 modified

Legend:

Unmodified
Added
Removed
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/fiscal_yearly_archives.pl

    r454 r455  
    66# personal use. If you distribute it, please keep this notice intact. 
    77# 
    8 # Copyright (c) 2007 Hirotaka Ogawa 
     8# Copyright (c) 2007-2008 Hirotaka Ogawa 
    99 
    1010package MT::Plugin::FiscalYearlyArchives; 
    1111use strict; 
    12 use base qw(MT::Plugin); 
     12use base qw( MT::Plugin ); 
    1313 
    1414use MT; 
    15 use MT::Entry; 
    1615 
    17 our $VERSION = '0.03'; 
     16our $VERSION = '0.04'; 
    1817 
    1918my $plugin = __PACKAGE__->new({ 
     
    2827    system_config_template => 'system_config.tmpl', 
    2928    settings => new MT::PluginSettings([ 
    30         ['fiscal_start_month', { Default => 4, Scope => 'system' }], 
     29        [ 'fiscal_start_month', { Default => 4, Scope => 'system' } ], 
    3130    ]), 
    3231}); 
    3332MT->add_plugin($plugin); 
    3433 
    35 use MT::WeblogPublisher; 
     34sub instance { $plugin } 
     35 
    3636sub init_registry { 
    3737    my $plugin = shift; 
    3838    $plugin->registry({ 
    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        }, 
    7849    }); 
    7950} 
    8051 
    81 # a dirty hack to avoid multiple get_config_value() calls for 
    82 # optimization 
    83 { 
    84     my $start_month; 
    85  
    86     # fetch 'fiscal_start_month' at the first time the method is 
    87     # called, and cache it to $start_month 
    88     sub fiscal_start_month { 
    89         return $start_month if $start_month; 
    90         $start_month = $plugin->get_config_value('fiscal_start_month') || 4; 
    91     } 
    92  
    93     # invalidate $start_month cache everytime init_request() called 
    94     sub init_request { 
    95         my $plugin = shift; 
    96         $plugin->SUPER::init_request(@_); 
    97         $start_month = undef; 
    98     } 
    99 } 
    100  
    101 sub ts2fiscal { 
    102     my ($ts) = @_; 
    103     my ($y, $m) = unpack('A4A2', $ts); 
    104     my $start_month = fiscal_start_month(); 
    105     $y-- if $m < $start_month; 
    106     $y; 
    107 } 
    108  
    10952# MTArchiveFiscalYear tag 
     53use FiscalYearlyArchives::Util qw( ts2fiscal ); 
    11054sub archive_fiscal_year { 
    11155    my ($ctx, %param) = @_; 
     
    11357    my $tag = $ctx->stash('tag'); 
    11458    return $ctx->error(MT->translate("You used an [_1] tag without a date context set up.", "MT$tag")) 
    115         unless defined $ts; 
     59        unless defined $ts; 
    11660    ts2fiscal($ts); 
    11761} 
    11862 
    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 . '&#24180;&#24230;' : $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  
    286631; 
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/lib/FiscalYearlyArchives/L10N.pm

    r388 r455  
    22package FiscalYearlyArchives::L10N; 
    33use strict; 
    4 use base 'MT::Plugin::L10N'; 
     4use base qw( MT::Plugin::L10N ); 
    55 
    661; 
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/lib/FiscalYearlyArchives/L10N/en_us.pm

    r402 r455  
    11# $Id$ 
    22package FiscalYearlyArchives::L10N::en_us; 
    3  
    43use strict; 
    5 use base 'FiscalYearlyArchives::L10N'; 
     4use base qw( FiscalYearlyArchives::L10N ); 
    65use vars qw( %Lexicon ); 
    76 
    87our %Lexicon = ( 
    98    'FISCAL-YEARLY_ADV' => 'Fiscal Yearly', 
     9    'AUTHOR-FISCAL-YEARLY_ADV' => 'Author Fiscal Yearly', 
     10    'CATEGORY-FISCAL-YEARLY_ADV' => 'Category Fiscal Yearly', 
    1011    'FISCAL_MONTH-1' => 'January', 
    1112    'FISCAL_MONTH-2' => 'February', 
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/lib/FiscalYearlyArchives/L10N/ja.pm

    r402 r455  
    11# $Id$ 
    22package FiscalYearlyArchives::L10N::ja; 
    3  
    43use strict; 
    5 use base 'FiscalYearlyArchives::L10N'; 
     4use base qw( FiscalYearlyArchives::L10N ); 
    65use vars qw( %Lexicon ); 
    76 
    87our %Lexicon = ( 
    98    'FISCAL-YEARLY_ADV' => '年度別', 
     9    'AUTHOR-FISCAL-YEARLY_ADV' => 'ユーザー-年度別', 
     10    'CATEGORY-FISCAL-YEARLY_ADV' => 'カテゴリ-年度別', 
    1011    'A plugin for building Fiscal Yearly Archives' => '年度別アーカイブを生成するプラグイン', 
    1112    'Fiscal Start Month' => '年度の開始月',