Changeset 393

Show
Ignore:
Timestamp:
08/26/07 04:45:02 (1 year ago)
Author:
ogawa
Message:

support Fiscal Start Month configuration.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/fiscal_yearly_archives.pl

    r392 r393  
    1212 
    1313use MT; 
    14 use MT::Template::Context; 
    1514use MT::Entry; 
    16 use MT::Tag; 
    17 use MT::ObjectTag; 
    18 use MT::Promise qw(force); 
    1915 
    2016our $VERSION = '0.01'; 
    2117 
    2218my $plugin = __PACKAGE__->new({ 
     19    id => 'fiscal_yearly_archives', 
    2320    name => 'FiscalYearlyArchives', 
    2421    description => q(<MT_TRANS phrase="A plugin for building Fiscal Yearly Archives">), 
     
    2825    version => $VERSION, 
    2926    l10n_class => 'FiscalYearlyArchives::L10N', 
     27    system_config_template => 'system_config.tmpl', 
     28    settings => new MT::PluginSettings([ 
     29        ['fiscal_start_month', { Default => 4, Scope => 'system' }], 
     30    ]), 
     31     
    3032}); 
    3133MT->add_plugin($plugin); 
     
    4345            'FiscalYearly' => 
    4446                ArchiveType( 
    45                            name => 'FiscalYearly', 
    46                            archive_label => \&archive_label, 
    47                            archive_file => \&archive_file, 
    48                            archive_title => \&archive_title, 
    49                            date_range => \&date_range, 
    50                            archive_group_iter => \&archive_group_iter, 
    51                            archive_group_entries => \&archive_group_entries, 
    52                            archive_entries_count => \&archive_entries_count, 
    53                            default_archive_templates => [ 
    54                                ArchiveFileTemplate( 
    55                                    label => 'fiscal/yyyy/index.html', 
    56                                    template => 'fiscal/<$MTArchiveFiscalYear$>/%i', 
    57                                    default => 1 
    58                                ), 
    59                            ], 
    60                            dynamic_template => 'fiscal/<$MTArchiveFiscalYear$>', 
    61                            dynamic_support => 1, 
    62                            date_based => 1, 
    63                            # ??? 
    64                            template_params => { 
    65                                datebased_only_archive => 1, 
    66                                datebased_fiscal_yearly_archive => 1, 
    67                                module_fiscal_yearly_archives => 1, 
    68                                main_template => 1, 
    69                                archive_template => 1, 
    70                                archive_class => "datebased-fiscal-yearly-archive", 
    71                            }, 
    72                            ), 
     47                    name => 'FiscalYearly', 
     48                    archive_label => \&archive_label, 
     49                    archive_file => \&archive_file, 
     50                    archive_title => \&archive_title, 
     51                    date_range => \&date_range, 
     52                    archive_group_iter => \&archive_group_iter, 
     53                    archive_group_entries => \&archive_group_entries, 
     54                    archive_entries_count => \&archive_entries_count, 
     55                    default_archive_templates => [ 
     56                        ArchiveFileTemplate( 
     57                            label => 'fiscal/yyyy/index.html', 
     58                            template => 'fiscal/<$MTArchiveFiscalYear$>/%i', 
     59                            default => 1 
     60                        ), 
     61                    ], 
     62                    dynamic_template => 'fiscal/<$MTArchiveFiscalYear$>', 
     63                    dynamic_support => 1, 
     64                    date_based => 1, 
     65                    # ??? 
     66                    template_params => { 
     67                        datebased_only_archive => 1, 
     68                        datebased_fiscal_yearly_archive => 1, 
     69                        module_fiscal_yearly_archives => 1, 
     70                        main_template => 1, 
     71                        archive_template => 1, 
     72                        archive_class => "datebased-fiscal-yearly-archive", 
     73                    }, 
     74                ), 
    7375        } 
    7476    }); 
    7577} 
    7678 
     79# This is a dirty hack to omit multiple get_config_value() calls for 
     80# optimization.  Basically, I don't like the way which a plugin loads 
     81# and stores its configuration by using MT::PluginSettings.  Because I 
     82# believe it should be able to get_and_set to the 'registry' in a more 
     83# flexible and transparent manner. 
     84our $START_MONTH; 
     85sub init_request { 
     86    my $plugin = shift; 
     87    $plugin->SUPER::init_request(@_); 
     88    $START_MONTH = $plugin->get_config_value('fiscal_start_month') || 4; 
     89} 
     90 
     91sub ts2fiscal { 
     92    my ($ts) = @_; 
     93    my ($y, $m) = unpack('A4A2', $ts); 
     94    $y-- if $m < $START_MONTH; 
     95    $y; 
     96} 
     97 
     98# MTArchiveFiscalYear tag 
    7799sub archive_fiscal_year { 
    78100    my ($ctx, %param) = @_; 
     
    80102    my $tag = $ctx->stash('tag'); 
    81103    return $ctx->error(MT->translate("You used an [_1] tag without a date context set up.", "MT$tag")) 
    82         unless defined $ts; 
    83     $ts = start_end_fiscal_year($ts); 
    84     my ($year) = unpack 'A4', $ts; 
    85     $year; 
     104        unless defined $ts; 
     105    ts2fiscal($ts); 
    86106} 
    87107 
    88108sub start_end_fiscal_year { 
    89109    my ($ts) = @_; 
    90     my ($y, $m) = unpack('A4A2', $ts); 
    91     $y-- if $m < 4; 
    92     my $start = sprintf "%04d0401000000", $y; 
     110    my $start_year = ts2fiscal($ts); 
     111    my $start = sprintf("%04d%02d%02d000000", $start_year, $START_MONTH, 1); 
    93112    return $start unless wantarray; 
    94     my $end = sprintf "%04d0331235959", $y + 1; 
     113 
     114    my ($end_year, $end_month, $end_day); 
     115    if ($START_MONTH == 1) { 
     116        ($end_year, $end_month, $end_day) = ($start_year, 12, 31); 
     117    } else { 
     118        ($end_year, $end_month) = ($start_year + 1, $START_MONTH - 1); 
     119        $end_day = MT::Util::days_in($end_month, $end_year); 
     120    } 
     121    my $end = sprintf("%04d%02d%02d235959", $end_year, $end_month, $end_day); 
    95122    ($start, $end); 
    96123} 
     
    111138            start_end_fiscal_year($timestamp); 
    112139    } else { 
    113         my $start = start_end_fiscal_year($timestamp); 
    114         my ($year) = unpack 'A4', $start; 
     140        my $year = ts2fiscal($timestamp); 
    115141        $file = sprintf("04d/index", $year); 
    116142    } 
     
    121147    my ($ctx, $entry_or_ts) = @_; 
    122148    my $ts = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts; 
    123     my $start = start_end_fiscal_year($ts); 
    124     my ($year) = unpack 'A4', $start; 
     149    my $year = ts2fiscal($ts); 
    125150    my $lang = lc MT->current_language || 'en_us'; 
    126151    $lang = 'ja' if lc($lang) eq 'jp'; 
     
    152177        my $ts = sprintf("%04d%02d%02d000000", $row[1], $row[2], 1); 
    153178        my ($start, $end) = start_end_fiscal_year($ts); 
    154         my ($year) = unpack 'A4', $start
     179        my $year = ts2fiscal($ts)
    155180        if ($year == $prev_year) { 
    156181            $count_groups[-1]->{count} += $row[0]; 
     
    178203sub archive_group_entries { 
    179204    my ($ctx, %param) = @_; 
    180     my $ts = sprintf("%04d%02d%02d000000", $param{fiscal_year}, 4, 1) 
     205    my $ts = sprintf("%04d%02d%02d000000", $param{fiscal_year}, $START_MONTH, 1) 
    181206        if %param; 
    182207    my ($start, $end); 
     
    218243        blog_id => $blog->id, 
    219244        status  => MT::Entry::RELEASE(), 
    220         ($ts ? (authored_on => [$start, $end]) : ()), 
    221         ($auth ? (author_id => $auth->id) : ()), 
     245        $ts   ? (authored_on => [$start, $end]) : (), 
     246        $auth ? (author_id => $auth->id) : (), 
    222247    }, { 
    223         ($ts ? (range => { authored_on => 1 }) : ()), 
    224         ($cat ? ('join' => ['MT::Placement', 'entry_id', { category_id => $cat->id }]) : ()), 
     248        $ts   ? (range => { authored_on => 1 }) : (), 
     249        $cat  ? ('join' => ['MT::Placement', 'entry_id', { category_id => $cat->id }]) : (), 
    225250    }); 
    226251    $count; 
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/lib/FiscalYearlyArchives/L10N/en_us.pm

    r388 r393  
    11# $Id$ 
    2 package FiscalYearlyArchives::L10N::ja
     2package FiscalYearlyArchives::L10N::en_us
    33 
    44use strict; 
     
    88our %Lexicon = ( 
    99                'FISCAL-YEARLY_ADV' => 'Fiscal Yearly', 
     10                'FISCAL_MONTH-1' => 'January', 
     11                'FISCAL_MONTH-2' => 'February', 
     12                'FISCAL_MONTH-3' => 'March', 
     13                'FISCAL_MONTH-4' => 'April', 
     14                'FISCAL_MONTH-5' => 'May', 
     15                'FISCAL_MONTH-6' => 'June', 
     16                'FISCAL_MONTH-7' => 'July', 
     17                'FISCAL_MONTH-8' => 'August', 
     18                'FISCAL_MONTH-9' => 'September', 
     19                'FISCAL_MONTH-10' => 'October', 
     20                'FISCAL_MONTH-11' => 'November', 
     21                'FISCAL_MONTH-12' => 'December', 
    1022                ); 
    1123 
  • FiscalYearlyArchives/trunk/FiscalYearlyArchives/lib/FiscalYearlyArchives/L10N/ja.pm

    r389 r393  
    99                'FISCAL-YEARLY_ADV' => '年度別', 
    1010                'A plugin for building Fiscal Yearly Archives' => '年度別アーカイブを生成するプラグイン', 
     11                'Fiscal Start Month' => '年度の開始月', 
     12                'FISCAL_MONTH-1' => '1月', 
     13                'FISCAL_MONTH-2' => '2月', 
     14                'FISCAL_MONTH-3' => '3月', 
     15                'FISCAL_MONTH-4' => '4月', 
     16                'FISCAL_MONTH-5' => '5月', 
     17                'FISCAL_MONTH-6' => '6月', 
     18                'FISCAL_MONTH-7' => '7月', 
     19                'FISCAL_MONTH-8' => '8月', 
     20                'FISCAL_MONTH-9' => '9月', 
     21                'FISCAL_MONTH-10' => '10月', 
     22                'FISCAL_MONTH-11' => '11月', 
     23                'FISCAL_MONTH-12' => '12月', 
    1124                ); 
    1225