#============================================================
# FormCSVConfig
#============================================================
package FormCSVConfig;
use Common;
@ISA = qw(Common);
#公開したいサブルーチン
#@EXPORT = qw(erfc tan);
@EXPORT_OK = qw();
use strict;
use Jcode;
use CSV;
#==========================================
# 大域変数
#==========================================
#==========================================
# 変数取得関数
#==========================================
sub pConfig { return shift->{pConfig}; }
sub pFormItem { return shift->{pFormItem}; }
sub pParams { return shift->{pParams}; }
#============================================================
# コンストラクタ、デストラクタ
#============================================================
sub new
{
my ($module) = @_;
my $this = {};
bless $this;
# $this->SUPER::new(@_);
return $this;
}
sub DESTROY
{
my $this = shift;
$this->SUPER::DESTROY(@_);
}
#============================================================
# メンバー関数
#============================================================
sub ReadCSVFile
{
my ($this, $infile, $pHash) = @_;
my %Config;
my @FormItem;
my %FormItem;
my %Label;
my @DBList;
my $csv = new CSV;
$csv->Open($infile, "r", 0) or return undef;
#print "Configuration:\n";
while(!$csv->eof()) {
my @a = $csv->ReadNextLine();
# last if($a[0] eq '');
last if($a[0] eq 'FormType' or $a[0] eq 'Checkbox' or $a[0] eq 'Select' or
$a[0] eq 'Textbox' or $a[0] eq 'TextArea' or $a[0] eq 'Submit');
if($a[0] ne '') {
$Config{$a[0]} = $a[1];
}
}
#foreach my $key (sort keys %Config) {
# print " $key: $Config{$key}\n";
#}
#print "Parameters:\n";
while(!$csv->eof()) {
my @a = $csv->ReadNextLine();
$a[0] = lc $a[0];
if($a[0] eq 'checkbox') {
my $FormName = $a[1];
$Label{$a[1]} = $a[2];
my @v;
my $c = 0;
while(!$csv->eof()) {
my @a = $csv->ReadNextLine();
last if($a[0] eq '' and $a[1] eq '');
$v[$c++] = {Key => $a[1], Label => $a[2], DefVal => $a[3]};
$Label{$a[1]} = $a[2];
push(@DBList, $a[1]);
}
$FormItem{"Checkbox:$FormName"} = {
FormType => 'Checkbox', FormName => $FormName, Label => $a[2],
Pre => $a[3], Post => $a[4], pSelections => \@v
};
push(@FormItem, $FormItem{"Checkbox:$FormName"});
}
elsif($a[0] eq 'select') {
my $FormName = $a[1];
my $Label = $a[2];
$Label{$a[1]} = $a[2];
my @v;
my $c = 0;
while(!$csv->eof()) {
my @a = $csv->ReadNextLine();
last if($a[0] eq '' and $a[1] eq '');
$v[$c++] = {Key => $a[1], Label => $a[2], DefVal => $a[3]};
$Label{$a[1]} = $a[2];
}
$FormItem{"Select:$FormName"} = {FormType => 'Select', FormName => $FormName, Label => $Label, pSelections => \@v};
push(@FormItem, $FormItem{"Select:$FormName"});
push(@DBList, $FormName);
}
elsif($a[0] eq 'textbox') {
my $FormName = $a[1];
$FormItem{"Textbox:$FormName"} = {FormType => 'Textbox', FormName => $a[1], Label => $a[2], Length => $a[3]};
push(@FormItem, $FormItem{"Textbox:$FormName"});
$Label{$a[1]} = $a[2];
push(@DBList, $FormName);
}
elsif($a[0] eq 'textarea') {
my $FormName = $a[1];
$FormItem{"TextArea:$FormName"} = {FormType => 'TextArea', FormName => $a[1], Label => $a[2], Length => $a[3], nRow => $a[4]};
push(@FormItem, $FormItem{"TextArea:$FormName"});
$Label{$a[1]} = $a[2];
push(@DBList, $FormName);
}
elsif($a[0] eq 'submit') {
my $FormName = $a[1];
$FormItem{"Submit:$FormName"} = {FormType => 'Submit', FormName => $FormName, Label => $a[2]};
push(@FormItem, $FormItem{"Submit:$FormName"});
$Label{$a[1]} = $a[2];
}
}
if(0) {
foreach my $key (sort keys %FormItem) {
my ($FormType, $FormName) = ($key =~ /^(\w+):(\w+)$/);
print " $FormType: $FormName\n";
my $pSel = $FormItem{$key}->{pSelections};
if($pSel) {
for(my $i = 0 ; $i < @$pSel ; $i++) {
print " $pSel->[$i]{Key}: $pSel->[$i]{Label} [def=$pSel->[$i]{Defval}]\n";
}
}
}
}
$this->{pConfig} = \%Config;
$this->{pFormItem} = \%FormItem;
$this->{pFormItemList} = \@FormItem;
$this->{pLabel} = \%Label;
$this->{pDBList} = \@DBList;
my $pParams = $this->BuildReplaceHash($pHash);
$this->{pParams} = $pParams;
return (\%Config, \%FormItem, $pParams, \%Label, \@DBList);
}
sub BuildReplaceHash
{
my ($this, $pHash, $TargetCharCode, $SourceCharCode, $App) = @_;
my $pFormItem = $this->pFormItem();
#print "FormString variables\n";
my $pParams = {};
foreach my $key (sort keys %$pFormItem) {
my ($FormType, $FormName) = ($key =~ /^(\w+):(\w+)$/);
#print " $FormType: $FormName\n";
my $pSel = $pFormItem->{$key}{pSelections};
my ($key1, $key2);
if($FormType eq 'Checkbox') {
$this->FillCheckboxParams($pHash, $pFormItem->{$key}, "");
$key1 = "${FormName}FormString";
$pParams->{$key1} = $this->GetCheckboxListFormString($pHash, $pFormItem->{$key});
$key2 = "${FormName}String";
$pParams->{$key2} = $this->GetCheckboxListString($pHash, $pFormItem->{$key});
#print " $key1: $pParams->{$key1}\n";
#print " $key2: $pParams->{$key2}\n";
}
elsif($FormType eq 'Select') {
$key1 = "${FormName}FormString";
$pParams->{$key1} = $this->GetSelectionFormString($pHash, $pFormItem->{$key});
$key2 = "${FormName}String";
$pParams->{$key2} = $this->GetSelectionString($pHash, $pFormItem->{$key});
#print " $key1: $pParams->{$key1}\n";
#print " $key2: $pParams->{$key2}
\n";
}
elsif($FormType eq 'Textbox') {
$key1 = "${FormName}FormString";
$pParams->{$key1} = $this->GetTextboxFormString($pHash, $pFormItem->{$key});
$pParams->{$FormName} = $pHash->{$FormName};
#print " $key1: $pParams->{$key1}\n";
}
elsif($FormType eq 'TextArea') {
$key1 = "${FormName}FormString";
$pParams->{$key1} = $this->GetTextAreaFormString($pHash, $pFormItem->{$key});
$pParams->{$FormName} = $pHash->{$FormName};
#print " $key1: $pParams->{$key1}\n";
}
elsif($FormType eq 'Submit') {
$key1 = "${FormName}FormString";
$pParams->{$key1} = $this->GetSubmitFormString($pHash, $pFormItem->{$key});
$pParams->{$FormName} = $pHash->{$FormName};
#print " $key1: $pParams->{$key1}\n";
#print " $key2: $pParams->{$key2}\n";
}
if($TargetCharCode and $TargetCharCode ne 'ascii' and $TargetCharCode ne $SourceCharCode) {
Jcode::convert(\$pParams->{$key1}, $TargetCharCode, $SourceCharCode) if($key1 and $pParams->{$key1} ne '');
Jcode::convert(\$pParams->{$key2}, $TargetCharCode, $SourceCharCode) if($key2 and $pParams->{$key2} ne '');
}
}
return $pParams;
}
sub FillCheckboxParams
{
my ($this, $pParams, $FormItem, $value) = @_;
my $FormName = $FormItem->{FormName};
my $pSelections = $FormItem->{pSelections};
for(my $i = 0 ; $i < @$pSelections ; $i++) {
my $key = $pSelections->[$i]{Key};
my $label = $pSelections->[$i]{Label};
my $DefVal = $pSelections->[$i]{DefVal};
if(!defined $pParams->{$key}) {
if(defined $value) {
$pParams->{$key} = $value
}
else {
$pParams->{$key} = $DefVal
}
}
}
}
sub GetFormItemString
{
my ($this, $pParams, $FormItem, $Key) = @_;
my $pSel = $FormItem->{pSelections};
my $FormName = $FormItem->{FormName};
$Key = $pParams->{$FormName} if(!defined $FormName);
for(my $i = 0 ; $i < @$pSel ; $i++) {
if($Key eq $pSel->[$i]{Key}) {
return $pSel->[$i]{Label};
}
}
return $Key;
}
sub GetSubmitFormString {
my ($this, $pParams, $FormItem, $CurVal) = @_;
my $FormName = $FormItem->{FormName};
my $Label = $FormItem->{Label};
return "";
}
sub GetTextAreaFormString {
my ($this, $pParams, $FormItem, $CurVal) = @_;
my $FormName = $FormItem->{FormName};
$CurVal = $pParams->{$FormName} if(!defined $CurVal);
my $Length = $FormItem->{Length};
my $nRow = $FormItem->{nRow};
return "";
}
sub GetTextboxFormString {
my ($this, $pParams, $FormItem, $CurVal) = @_;
my $FormName = $FormItem->{FormName};
$CurVal = $pParams->{$FormName} if(!defined $CurVal);
#print "$FormName: [$CurVal]
\n";
my $Length = $FormItem->{Length};
return "";
}
sub GetSelectionString {
my ($this, $pParams, $FormItem, $CurVal) = @_;
my $pSel = $FormItem->{pSelections};
my $FormName = $FormItem->{FormName};
$CurVal = $pParams->{$FormName} if(!defined $CurVal);
for(my $i = 0 ; $i < @$pSel ; $i++) {
my $key = $pSel->[$i]{Key};
my $label = $pSel->[$i]{Label};
if($key eq $CurVal) {
return $label;
}
}
return 'Unkown';
}
sub GetSelectionFormString {
my ($this, $pParams, $FormItem, $CurVal) = @_;
my $pSel = $FormItem->{pSelections};
my $FormName = $FormItem->{FormName};
$CurVal = $pParams->{$FormName} if(!defined $CurVal);
my $str = "\n";
return $str;
}
sub GetCheckboxListString
{
my ($this, $pParams, $FormItem) = @_;
my $pSel = $FormItem->{pSelections};
my $FormName = $FormItem->{FormName};
my $pre = $FormItem->{Pre};
my $post = $FormItem->{Post};
my $s = '';
for(my $i = 0 ; $i < @$pSel ; $i++) {
my $key = $pSel->[$i]{Key};
my $label = $pSel->[$i]{Label};
my $CurVal = $pParams->{$key};
if($CurVal eq 'ON') {
$s .= "$pre$label$post";
}
}
return $s;
}
sub GetCheckboxListFormString
{
my ($this, $pParams, $FormItem) = @_;
my $pSel = $FormItem->{pSelections};
my $FormName = $FormItem->{FormName};
my $pre = $FormItem->{Pre};
my $post = $FormItem->{Post};
my $s = '';
for(my $i = 0 ; $i < @$pSel ; $i++) {
my $key = $pSel->[$i]{Key};
my $label = $pSel->[$i]{Label};
my $DefVal = $pSel->[$i]{DefVal};
my $CurVal = $pParams->{$key};
my $Val = (defined $CurVal)? $CurVal : $DefVal;
#print "$FormName: [$CurVal]
\n";
my $Checked = ($Val eq 'ON')? ' checked' : '';
$s .= "$label
\n";
}
return $s;
}
#================================================================================
# Utility関数
#================================================================================
sub MakeCSVFile
{
my ($this, $DB, $OutFilePath, $CSVFileCharCode, $DBTableName, $sql, %args) = @_;
my $IsPrint = (defined $args{IsPrint})? $args{IsPrint} : 0;
my $pLabel = (defined $args{pLabel})? $args{pLabel} : $this->{pLabel};
my $DBName = (defined $args{DBName})? $args{DBName} : $DB->{DBName};
my $pDBFields = (defined $args{pDBFields})? $args{pDBFields} : sort $DB->GetFields($DBName, $DBTableName, 0);
# my $pDBFields = $args{pDBFields};
my $pFirstDBList = (defined $args{pFirstDBList})? $args{pFirstDBList} : [qw(sn EMail Name JName)];
my $pLastDBList = (defined $args{pLastDBList})? $args{pLastDBList} : [qw(IPAddress LastUpdateTime LastLogonTime)];
my $out = new CSV;
if(!$out->Open($OutFilePath, "w", 0)) {
return 0;
}
my @DBFields = ($pDBFields)? @$pDBFields : ();
my @keys;
$DB->Search($DBTableName, $sql, "");
my $nHit = $DB->nHit();
for(my $i = 0 ; $i < $nHit ; $i++) {
my %Hit = $DB->GetNextHit();
if(@DBFields == 0) {
@DBFields = sort keys %Hit;
@DBFields = (@$pFirstDBList, Utils::DeleteItemFromList(\@DBFields, $pFirstDBList)) if($pFirstDBList);
@DBFields = (Utils::DeleteItemFromList(\@DBFields, $pLastDBList), @$pLastDBList) if($pLastDBList);
@keys = @DBFields;
if($pLabel) {
for(my $i = 0 ; $i < @keys ; $i++) {
if($pLabel->{$keys[$i]}) {
$keys[$i] = $pLabel->{$keys[$i]};
Jcode::convert(\$keys[$i], $CSVFileCharCode) if(defined $CSVFileCharCode and $CSVFileCharCode ne 'ascii');
}
}
}
$out->WriteDataLine(\@keys);
}
my $pHash = $this->BuildReplaceHash(\%Hit);
Utils::MergeHash($pHash, \%Hit);
my @a;
for(my $i = 0 ; $i < @DBFields ; $i++) {
my $key = $DBFields[$i];
my $val = ($pHash->{"${key}String"})? $pHash->{"${key}String"} : $pHash->{$key};
Jcode::convert(\$val, $CSVFileCharCode) if(defined $CSVFileCharCode and $CSVFileCharCode ne 'ascii');
$a[$i] = $val;
}
$out->WriteDataLine(\@a);
}
$out->Close();
return $nHit;
}
sub MakeInputFormTemplateHTML
{
my ($this, $pParams, $OutFilePath, $OutputFileCharCode, $OutputFileCharSet, $target, %args) = @_;
$OutputFileCharCode = $pParams->{WebCharCode} if(!defined $OutputFileCharCode);
$OutputFileCharSet = $pParams->{WebCharSet} if(!defined $OutputFileCharSet);
#print("CharCode:[$OutputFileCharCode]
\n");
my $pHash = $this->BuildReplaceHash($pParams); #, $OutputFileCharSet);#, $SourceCharCode);
Utils::MergeHash($pHash, \%args);
Utils::MergeHash($pHash, $pParams);
my $pFormItemList = $this->{pFormItemList};
my $pLabel = $this->{pLabel};
my $App = new MyCGIApplication;
$App->Initialize();
if(!$App->SetOutputMode('WriteFile:HTML', undef, $OutFilePath)) {
return 0;
}
$App->SetPrintCharCode($OutputFileCharCode);
# $App->SetHTMLHeader("Input: $pHash->{EnqueteName}", $pParams->{WebCharSet}, '_self');
if(defined $OutputFileCharCode and $OutputFileCharCode ne 'ascii') {
Jcode::convert(\$pHash->{PageTitle}, $OutputFileCharCode, 'sjis');
}
$App->InitHTML("Input: $pHash->{PageTitle}", $OutputFileCharSet, $target, undef, 0);
$App->PrintRawHTML("