## This file is auto-generated (at 2003-05-31T11:44:28Z).
## Do not edit by hand!

=head1 NAME

SuikaWiki::Plugin::BugTrack --- Wiki BugTracking System
<IW:SuikaWiki:"SuikaWiki//BugTrack">


=cut

package SuikaWiki::Plugin::BugTrack;
use strict;
our $VERSION = q(2003.0531.1144);

my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';

sub import {
    my $self = shift;
    $self->SuikaWiki::Plugin::regist (qw/wikibt__bug_table_header wikiview wikiview_resource wikibt__bug_item wikiform_input/);
}

sub property ($;@) {
    shift;
    my %prop = (
                name     => q(Wiki BugTracking System),
                version  => $VERSION,
                uri      => q(IW:SuikaWiki:"SuikaWiki//BugTrack"),
                description	=> q(),
                provide  => [qw/wikibt__bug_table_header wikiview wikiview_resource wikibt__bug_item wikiform_input/],
		partinfo => {
  q(wikiform_input/bt__bug_list) => {FullName => q(Bug list), description => q()},
  q(wikibt__bug_table_header/no) => {FullName => q(Table heading cell), description => q()},
  q(wikibt__bug_table_header/subject) => {FullName => q(Table heading cell), description => q()},
  q(wikibt__bug_table_header/priority) => {FullName => q(Table heading cell), description => q()},
  q(wikibt__bug_table_header/status) => {FullName => q(Table heading cell), description => q()},
  q(wikibt__bug_table_header/category) => {FullName => q(Table heading cell), description => q()},
  q(wikibt__bug_table_header/-bare_text) => {FullName => q(#text), description => q()},
  q(wikibt__bug_item/-bare_text) => {FullName => q(#text), description => q()},
  q(wikibt__bug_item/no) => {FullName => q(Bug number), description => q()},
  q(wikibt__bug_item/subject) => {FullName => q(Bug subject), description => q()},
  q(wikibt__bug_item/category) => {FullName => q(Bug category), description => q()},
  q(wikibt__bug_item/status) => {FullName => q(Bug status), description => q()},
  q(wikibt__bug_item/priority) => {FullName => q(Bug priority), description => q()},
  q(wikiview_resource/bt__bug_no) => {FullName => q(Bug number), description => q()},
  q(wikiform_input/bt__create_new_bug) => {FullName => q(Create a new bug page), description => q()},
  q(wikiview/bt__create_new_bug) => {FullName => q(Create a new bug page), description => q()},
},
    );
    if (scalar @_ == 0) {
        \%prop;
    } else {
        @prop{@_};
    }
}
sub load_formatter ($$%) {
    my $self = shift;
    my ($fmt, %q) = @_;
    unless ($q{type}) {
        #
    }
    elsif ($q{type} eq 'wikibt__bug_item') {
      $fmt->{q(-bare_text)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (type => '#text', value => $p->{-bare_text});
$r };

      $fmt->{q(category)} = sub { my ($r, $p, $o) = ('', @_);
$r = __PACKAGE__->make_cell ($o, $p, $o->resource ('Plugin:BugTrack:Category:'.($o->{bt__bug}->{prop}->{Category}||'(default)')));
$r };

      $fmt->{q(no)} = sub { my ($r, $p, $o) = ('', @_);
$r = __PACKAGE__->make_cell ($o, $p, SuikaWiki::Plugin::WikiLinking->to_wikipage_in_html ({
	label	=> qq'%link-to-it(label=>{$o->{bt__bug}->{bug_no}},description=>{%page-name(absolute);}p);',
}, {
	base	=> $o->{bt_bug}->{bug_ns},
	page_name_relative	=> $o->{bt__bug}->{bug_page},
}, {
	o	=> $o,
}));
$r };

      $fmt->{q(priority)} = sub { my ($r, $p, $o) = ('', @_);
$r = __PACKAGE__->make_cell ($o, $p, $o->resource ('Plugin:BugTrack:Priority:'.($o->{bt__bug}->{prop}->{Priority}||'(default)')));
$r };

      $fmt->{q(status)} = sub { my ($r, $p, $o) = ('', @_);
$r = __PACKAGE__->make_cell ($o, $p, $o->resource ('Plugin:BugTrack:Status:'.($o->{bt__bug}->{prop}->{Status}||'(default)')));
$r };

      $fmt->{q(subject)} = sub { my ($r, $p, $o) = ('', @_);
$r = __PACKAGE__->make_cell ($o, $p, SuikaWiki::Plugin::WikiLinking->to_wikipage_in_html ({
	label	=> qq'%link-to-it(label=>{$o->{bt__bug}->{prop}->{Subject}},description=>{%page-name(absolute);}p);',
}, {
	base	=> $o->{bt_bug}->{bug_ns},
	page_name_relative	=> $o->{bt__bug}->{bug_page},
	_with_lm	=> 1,
}, {
	o	=> $o,
}));
$r };

    }
    elsif ($q{type} eq 'wikibt__bug_table_header') {
      $fmt->{q(-bare_text)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (type => '#text', value => $p->{-bare_text});
$r };

      $fmt->{q(category)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'th');
$r->append_node ($o->resource ('Plugin:BugTrack:Column:'.$p->{-rule_name}), node_or_text => 1);
$r->set_attribute (scope => 'cols');
$r };

      $fmt->{q(no)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'th');
$r->append_node ($o->resource ('Plugin:BugTrack:Column:'.$p->{-rule_name}), node_or_text => 1);
$r->set_attribute (scope => 'cols');
$r };

      $fmt->{q(priority)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'th');
$r->append_node ($o->resource ('Plugin:BugTrack:Column:'.$p->{-rule_name}), node_or_text => 1);
$r->set_attribute (scope => 'cols');
$r };

      $fmt->{q(status)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'th');
$r->append_node ($o->resource ('Plugin:BugTrack:Column:'.$p->{-rule_name}), node_or_text => 1);
$r->set_attribute (scope => 'cols');
$r };

      $fmt->{q(subject)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'th');
$r->append_node ($o->resource ('Plugin:BugTrack:Column:'.$p->{-rule_name}), node_or_text => 1);
$r->set_attribute (scope => 'cols');
$r };

    }
    elsif ($q{type} eq 'wikiform_input') {
      $fmt->{q(bt__bug_list)} = sub { my ($r, $p, $o) = ('', @_);
$p->{ns} ||= $o->{page};
$p->{ns} .= '//' if substr ($p->{ns}, -2) ne '//';
my @bugs = grep m!//[0-9]+$!, $o->_database->list_items ({ns => $p->{ns}, type => 'key', recursive => 0});
my $props = '%no;%status;%priority;%category;%subject;';
my $fmt = $o->formatter ('bt--bug-table-header');
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'table');
$r->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'thead')
  ->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'tr')
  ->append_node ($fmt->replace ($props, $o, {formatter => $fmt}));
$r->append_text ("\n");
$fmt = $o->formatter ('bt--bug-item');
my $tb = $r->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'tbody');
my $weight = {
	Priority	=> {high => 1, normal => 2, low => 3},
	Status	=> {open => 1, reserved => 2, closed => 3},
};
for $_ (sort {
  	   $weight->{Status}->{$a->{prop}->{Status}||'open'}
  	   <=> $weight->{Status}->{$b->{prop}->{Status}||'open'}
  	|| $weight->{Priority}->{$a->{prop}->{Priority}||'normal'}
  	   <=> $weight->{Priority}->{$b->{prop}->{Priority}||'normal'}
  	|| $a->{prop}->{Category} cmp $b->{prop}->{Category}
  	|| $b->{bug_no} <=> $a->{bug_no}
  } map {{
  	bug_ns => $p->{ns},
  	bug_page	=> $_,
  	bug_no	=> do {$_ =~ m!//([0-9]+)$!; $1},
  	prop	=> __PACKAGE__->get_bug_properties ($_),
  }} @bugs) {
  $o->{bt__bug} = $_;
  $tb->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'tr')
     ->append_node ($fmt->replace ($props, $o, {formatter => $fmt}));
  $tb->append_text ("\n");
}
$r };

      $fmt->{q(bt__create_new_bug)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'form');
$r->set_attribute (action => $o->uri ('wiki'));
$r->set_attribute (method => 'post');
$r->set_attribute ('accept-charset' => 'iso-2022-jp, utf-8');
$r->set_attribute (class => 'bt--create-bug');
for ($r->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'input')) {
  $_->set_attribute (type => 'hidden');
  $_->set_attribute (name => 'mode');
  $_->set_attribute (value => 'bt--bug-create');
}
for ($r->append_new_node (namespace_uri => $NS_XHTML1, local_name => 'input')) {
  $_->set_attribute (type => 'hidden');
  $_->set_attribute (name => 'mypage');
  $_->set_attribute (value => ($p->{ns} || $o->{page}));
}
$r->append_node ($p->{content}, node_or_text => 1);
$r };

    }
    elsif ($q{type} eq 'wikiview') {
      $fmt->{q(bt__create_new_bug)} = sub { my ($r, $p, $o) = ('', @_);
my @page = ('#?SuikaWiki/0.9');
for (qw/Subject Priority Status Category/) {
  push @page, '- ' . $_ . ': ' . $main::form{'wikiform__'.$_};
}
push @page, '- Creation-Date: ' . main::_rfc3339_date (time);
push @page, '';
push @page, '[1] ' . $main::form{'wikiform__body'};

$p->{ns} ||= $o->{page};
$p->{ns} .= '//' if substr ($p->{ns}, -2) ne '//';
my $page = $p->{ns} . __PACKAGE__->get_new_bug_number ($p->{ns});
$main::database{$page} = join "\n", @page;
$o->{bt__bug} = {
  	bug_ns => $p->{ns},
  	bug_page	=> $page,
  	bug_no	=> do {$page =~ m!//([0-9]+)$!; $1},
  	prop	=> __PACKAGE__->get_bug_properties ($page),
};
$r };

    }
    elsif ($q{type} eq 'wikiview_resource') {
      $fmt->{q(bt__bug_no)} = sub { my ($r, $p, $o) = ('', @_);
$r = SuikaWiki::Markup::XML->new (type => '#text', value => $o->{bt__bug}->{bug_no});
$r };

    }
} # sub load_formatter

sub get_bug_properties ($$) {
  my (undef, $bug_page) = @_;
  my (undef, $bug_info) = (SuikaWiki::Plugin->magic_and_content ($main::database{ $bug_page }));
  my %bug_prop;
  for (split /\n/, $bug_info) {
    tr/\x0A\x0D//d;
    if (/^-\s*([0-9A-Za-z-]+)\s*:\s*(\S.*\S)/) {
      $bug_prop{$1} = $2;
    } elsif (!/^-/) {
      last;
    }
  }
  \%bug_prop;
}

sub make_cell ($$$$) {
  my (undef, $o, $p, $content) = @_;
  my $r = SuikaWiki::Markup::XML->new (namespace_uri => $NS_XHTML1, local_name => 'td');
  if ($p->{is_header}) {
    $r->local_name ('th');
    $r->set_attribute (scope => 'rows');
  }
  $r->append_node ($content, node_or_text => 1);
  $r;
}

sub get_new_bug_number ($$) {
  my (undef, $bug_ns) = @_;
  my $no = 0;
  for (SuikaWiki::Plugin->_database->list_items ({ns => $bug_ns, type => 'key', recursive => 0})) {
    if (m!//([0-9]+)$!) {
      $no = 0+$1 if $1 > $no;
    }
  }
  ++$no;
}


SuikaWiki::View->definition (bt__bug_create => {
	media => {type => 'text/html', charset => 1, expires => 60},
	xmedia => {type => 'application/xhtml+xml', charset => 1, expires => 60},
	template => <<'EOH'});
%bt--create-new-bug;
%html-document(title=>{%res(name=>{Plugin:BugTrack:Created:WebPageTitle});}p,link-meta=>{%predefined-template(name=>links);%html-meta(name=>ROBOTS,content=>NOINDEX);}p,content=>{
  %section(level=>1,add-to-toc=>0,type=>body,title=>{%page-name;}p,heading,content=>{
    %section(id=>tools1,class=>tools,add-to-toc=>0,content=>{%predefined-template(name=>navbar);}p);
    %section(level=>2,add-to-toc=>0,content=>{
      %paragraph(content=>{%res(name=>{Plugin:BugTrack:Created:Message});}p);
    }p);
    %section(id=>footer,add-to-toc=>0,content=>{%predefined-template(name=>footer);}p);
  }p);
}p);
EOH

=head1 LICENSE

Copyright 2003 Wakaba <w@suika.fam.cx>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.

=cut

1;