package comment;
use strict;
use record;
use project;
use attachment;
@comment::ISA=qw(record);
sub new{
my $className=shift;
my $primaryKey=shift; #新しいレコードの場合空
my $t=&record::init($className,$primaryKey);
if ($t->isNew) {$t->{date}="now";}
$t;
}
use table;
sub edit {
my $t=shift;
my $tbl=new table;
$tbl->addRow
->addCol("内容")
->addCol($t->multiText("content",(rows=>10,cols=>60)));
if(1) {
$tbl->addRow
->addCol("添付ファイル")
->addCol("");
}
if ($t->{content}) {
$tbl->addRow->addCol->addCol
(qq(
コメントを削除するには、上の欄を一度消して、
DELETE と入力してください(空白、改行なし)
));
}
&tmpl::default
("コメント","Comment ",
&html::form
($tbl->out . &html::button ,
"commentDone",
{mainkey=>$t->{mainkey},
parent=>$t->{parent},
project=>$t->{project}},1
));
}
sub columnProp {
return
(
"project"=>"n",
"parent"=>"n"
);
}
sub view {
my $t=shift;
use user;
my $prj=new project($t->{project});
my $parent=new table;
$parent->addRow;
$parent->addCol
(
qq(
| 親コメント: @{[new comment($t->{parent})->link]} |
". &html::titledBox ("内容", qq( Commented by: @{[ &user::link($t->{poster})]} at $t->{date}
@{[&html::preBreak(&html::escape($t->{content}))]}
$att
$edit
)
)."
".
&html::titledBox("返信", $tbl->out ),
160
);
}
sub getAttachment {
my $t=shift;
my $a=&sql::select1("attachment", "comment", $t->primaryKey);
if ($a) {
return bless $a,"attachment";
}
return 0;
}
sub attachFile {
my $t=shift;
my $fileName=shift;
my $content=shift;
my $a=$t->getAttachment;
if (!$a) {
$a=new attachment;
$a->{comment}=$t->primaryKey;
}
if ($fileName =~ /\.[\w\d]+/) {
$a->{filename}=$&;
$a->insertOrUpdate;
$a->upload($content);
} else {
die("ファイル名拡張子が不正です $fileName");
}
}
sub replies {
my $t=shift;
my $depth=shift;
my $buf="";
if ($depth>=2) {
my $it=&sql::select
(qq(
select sum(1) as ress from comment where parent=$t->{mainkey} group by parent
));
if (my $r=$it->next) {
$buf.="(さらに$r->{ress}個の返信)";
}
$buf .= "
";
return $buf;
}
$buf .="
";
my $it=&sql::select
(qq(
select * from comment where parent=$t->{mainkey}
));
while (my $r=$it->next) {
bless $r,"comment";
$buf.=" " x $depth;
$buf.=$r->link;
$buf.=$r->replies($depth+1);
}
$buf;
}
sub jsubstr {
my ($s,$b,$l)=@_;
if (!$l) {$l=100000;}
my $e=$b+$l;
my ($bb,$ee,$c);
my @e=unpack("C*",$s);
for (my $i=0 ; $i<@e ; $i++) {
if ($c>=$b && !defined($bb) ) {
$bb=$i;
}
if ($c>=$e && !defined($ee) ) {
$ee=$i;
}
if ($e[$i]>=128) {$i++;}
$c++;
}
$ee=length($s) if (!$ee);
substr($s,$bb,$ee-$bb);
}
sub digest {
my $c=shift;
my $l=shift;
if (!$l) {$l=20; }
if (length($c)>$l*2) {
$c=jsubstr($c,0,$l)."...";
}
$c;
}
sub miniLink {
my $t=shift;
my $c=&digest(&html::escape($t->{content}),10);
my $d=$t->date;
$d =~ /([0-9][0-9]):([0-9][0-9])/;
$d = "$1:$2";
my $p=new project($t->{project});
my $showProject=qq(
on ).$p->link2Top($p->{title});
qq(@{[$t->comIcon]}
@{[ &html::link($c,"viewComment",{mainkey=>$t->{mainkey}}) ]}
$d by
@{[&user::link($t->{poster})]}
$showProject
);
}
sub link {
my $t=shift;
my $showProject=shift;
my $c=&digest(&html::escape($t->{content}));
use userID2Name;
my $d=$t->date;
if ($showProject) {
my $p=new project($t->{project});
$showProject=qq( on ).$p->link2Top($p->{title});
}
qq(@{[$t->comIcon]}
@{[ &html::link($c,"viewComment",{mainkey=>$t->{mainkey}}) ]}
$d by
@{[&html::escape(&userID2Name::getName($t->{poster}))]}
$showProject
);
}
sub date{
my $t=shift;
my $d=$t->{date};
$d =~s/\..*//;
$d =~s/\+.*//;
$d =~s/:[0-9][0-9]$//;
$d =~s/^[0-9][0-9]//;
$d;
}
sub recurseAll {
my $project=shift;
my $it=&sql::select
(qq(
select * from comment
where project=@{[&sql::i($project)]}
order by date desc
));
my @coms;
my %id2coms;
while (my $r=$it->next) {
bless $r,"comment";
$r->{childs}=[];
unshift @coms,$r;
$id2coms{$r->primaryKey}=$r;
}
my @roots;
for my $r (@coms) {
if ($r->{parent}) {
my $p=$id2coms{$r->{parent}};
if ($p) {
push @{$p->{childs}},$r;
}
} else {
unshift @roots,$r;
}
}
@roots;
}
sub dispAll {
my $project=shift;
my @roots=&recurseAll($project);
my $buf="";
for my $c (@roots) {
$buf .= $c->tree;
}
$buf;
}
sub tree {
my $t=shift;
my $ind=shift;
my $buf= $ind.$t->link . "
";
exit if (length($ind)>200);
for my $c (@{$t->{childs}}) {
$buf .= $c->tree("$ind ");
}
$buf;
}
sub readAll {
my $project=shift;
my $prj=new project($project);
my @coms=&recurseAll($project);
my $buf;
for my $r (@coms) {
$buf.=$r->treeAll("")."
";
}
my $tbl=new table;
$tbl->addRow->addCol($buf);
&tmpl::default("コメント","comments",
$prj->link2Top."
".$tbl->out."
".$prj->link2Top);
}
sub treeAll {
my $t=shift;
my $ind=shift;
my $tbl=new table;
$tbl->addRow->addCol($ind);
$tbl->addCol($t->linkFromFull);
exit if (length($ind)>200);
my $buf;
for my $c (@{$t->{childs}}) {
$buf.=$c->treeAll("$ind ");
}
$tbl->out.$buf;
}
sub linkFromFull {
my $t=shift;
my $d=$t->date;
my $ic=$t->comIcon;
qq(@{[ &html::link($ic,"viewComment",{mainkey=>$t->{mainkey}}) ]}
@{[&html::escape($t->{content})]}$d by
@{[&html::escape(&userID2Name::getName($t->{poster}))]}
);
}
sub comIcon {
my $t=shift;
if ($t->hasAttachment) {
return qq(
);
} else {
return qq(
);
}
}
my %hat;
sub hasAttachment {
my $t=shift;
if (!$hat{ok}) {
my $it=&sql::select (qq(select * from attachment;));
while (my $r=$it->next) {
$hat{$r->{comment}}=1;
}
$hat{ok}=1;
}
return $hat{$t->primaryKey};
}
1;