1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<!-- mason kludge -->
<%= header('Map tables') %>
<SCRIPT>
var gSafeOnload = new Array();
var gSafeOnsubmit = new Array();
window.onload = SafeOnload;
function SafeAddOnLoad(f) {
gSafeOnload[gSafeOnload.length] = f;
}
function SafeOnload() {
for (var i=0;i<gSafeOnload.length;i++)
gSafeOnload[i]();
}
function SafeAddOnSubmit(f) {
gSafeOnsubmit[gSafeOnsubmit.length] = f;
}
function SafeOnsubmit() {
for (var i=0;i<gSafeOnsubmit.length;i++)
gSafeOnsubmit[i]();
}
</SCRIPT>
<FORM NAME="OneTrueForm" METHOD="POST" ACTION="meta-import.cgi">
<%
#one
unless ( $cgi->param('magic') ) {
#oops, silly
#my $fh = $cgi->upload('csvfile');
##warn $cgi;
##warn $fh;
#
#use Archive::Tar;
#$tar = Archive::Tar->new();
#$tar->create_archive($fh); #or die $tar->error;
#haha for now
my @files = qw(
authserv credtype dunprev invoice pmtdet product taxplan
ccdet customer genlog ledger pops pubvars
cchist discplan glacct origco prodcat recur users
credcode dundet invline payment prodclas repforms webserv
);
%>
<INPUT TYPE="hidden" NAME="magic" VALUE="process">
<%= hashmaker('schema', \@files, [ grep { ! /^h_/ } dbdef->tables ] ) %>
<br><INPUT TYPE="submit" VALUE="done">
<%
} elsif ( $cgi->param('magic') eq 'process' ) {
%>
<INPUT TYPE="hidden" NAME="magic" VALUE="process2">
<%
my $schema_string = $cgi->param('schema');
%><INPUT TYPE="hidden" NAME="schema" VALUE="<%=$schema_string%>"><%
my %schema = map { /^\s*(\w+)\s*=>\s*(\w+)\s*$/
or die "guru meditation #420: $_";
( $1 => $2 );
}
split( /\n/, $schema_string );
#*** should be in global.asa/handler.pl like the rest
eval 'use Text::CSV_XS;';
foreach my $table ( keys %schema ) {
my $csv = Text::CSV_XS->new({ 'binary'=>1 });
open(FILE,"</home/ivan/intergate/legacy/csvdir/$table")
or die "can't /home/ivan/intergate/legacy/csvdir/$table: $!";
my $header = lc(<FILE>);
close FILE;
$csv->parse($header) or die;
my @from_columns = $csv->fields;
my @fs_columns = dbdef->table($schema{$table})->columns;
%>
<%= hashmaker($table, \@from_columns, \@fs_columns, $table, $schema{$table} ) %>
<br><hr><br>
<%
}
%>
<br><INPUT TYPE="submit" VALUE="done">
<%
} elsif ( $cgi->param('magic') eq 'process2' ) {
print "<pre>\n";
#false laziness with above
my $schema_string = $cgi->param('schema');
my %schema = map { /^\s*(\w+)\s*=>\s*(\w+)\s*$/
or die "guru meditation #420: $_";
( $1 => $2 );
}
split( /\n/, $schema_string );
foreach my $table ( keys %schema ) {
( my $spaces = $table ) =~ s/./ /g;
print "'$table' => { 'table' => '$schema{$table}',\n".
#(length($table) x ' '). " 'map' => {\n";
"$spaces 'map' => {\n";
my %map = map { /^\s*(\w+)\s*=>\s*(\w+)\s*$/
or die "guru meditation #420: $_";
( $1 => $2 );
}
split( /\n/, $cgi->param($table) );
foreach ( keys %map ) {
print "$spaces '$_' => '$map{$_}',\n";
}
print "$spaces },\n";
print "$spaces },\n";
}
print "\n</pre>";
} else {
warn "unrecognized magic: ". $cgi->param('magic');
}
%>
</FORM>
</BODY>
</HTML>
<%
#hashmaker widget
sub hashmaker {
my($name, $from, $to, $labelfrom, $labelto) = @_;
$fromsize = scalar(@$from);
$tosize = scalar(@$to);
"<TABLE><TR><TH>$labelfrom</TH><TH>$labelto</TH></TR><TR><TD>".
qq!<SELECT NAME="${name}_from" SIZE=$fromsize>\n!.
join("\n", map { qq!<OPTION VALUE="$_">$_</OPTION>! } sort { $a cmp $b } @$from ).
"</SELECT>\n".
'</TD><TD>'.
qq!<SELECT NAME="${name}_to" SIZE=$tosize>\n!.
join("\n", map { qq!<OPTION VALUE="$_">$_</OPTION>! } sort { $a cmp $b } @$to ).
"</SELECT>\n".
'</TD></TR>'.
'<TR><TD COLSPAN=2>'.
qq!<INPUT TYPE="button" VALUE="map" onClick="toke_$name(this.form)">!.
'</TD></TR><TR><TD COLSPAN=2>'.
qq!<TEXTAREA NAME="$name" COLS=80 ROWS=8></TEXTAREA>!.
'</TD></TR></TABLE>'.
"<script>
function toke_$name() {
fromObject = document.OneTrueForm.${name}_from;
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options[i].selected)
fromname = deleteOption_$name(fromObject,i);
}
toObject = document.OneTrueForm.${name}_to;
for (var i=toObject.options.length-1;i>-1;i--) {
if (toObject.options[i].selected)
toname = deleteOption_$name(toObject,i);
}
document.OneTrueForm.$name.value = document.OneTrueForm.$name.value + fromname + ' => ' + toname + '\\n';
}
function deleteOption_$name(object,index) {
value = object.options[index].value;
object.options[index] = null;
return value;
}
</script>".
'';
}
%>
|