Some code cleanup. Created subroutines for ISBN checksum,
[koha.git] / acqui.simple / marcimport.pl
1 #!/usr/bin/perl
2
3 # Script for handling import of MARC data into Koha db
4 #   and Z39.50 lookups
5
6 # Koha library project  www.koha.org
7
8 # Licensed under the GPL
9
10 #use strict;
11
12 # standard or CPAN modules used
13 use CGI;
14 use DBI;
15
16 # Koha modules used
17 use C4::Database;
18 use C4::Acquisitions;
19 use C4::Output;
20
21 #------------------
22 # Constants
23
24 # HTML colors for alternating lines
25 my $lc1='#dddddd';
26 my $lc2='#ddaaaa';
27
28 my %tagtext = (
29     '001' => 'Control number',
30     '003' => 'Control number identifier',
31     '005' => 'Date and time of latest transaction',
32     '006' => 'Fixed-length data elements -- additional material characteristics',
33     '007' => 'Physical description fixed field',
34     '008' => 'Fixed length data elements',
35     '010' => 'LCCN',
36     '015' => 'LCCN Cdn',
37     '020' => 'ISBN',
38     '022' => 'ISSN',
39     '037' => 'Source of acquisition',
40     '040' => 'Cataloging source',
41     '041' => 'Language code',
42     '043' => 'Geographic area code',
43     '050' => 'Library of Congress call number',
44     '060' => 'National Library of Medicine call number',
45     '082' => 'Dewey decimal call number',
46     '100' => 'Main entry -- Personal name',
47     '110' => 'Main entry -- Corporate name',
48     '130' => 'Main entry -- Uniform title',
49     '240' => 'Uniform title',
50     '245' => 'Title statement',
51     '246' => 'Varying form of title',
52     '250' => 'Edition statement',
53     '256' => 'Computer file characteristics',
54     '260' => 'Publication, distribution, etc.',
55     '263' => 'Projected publication date',
56     '300' => 'Physical description',
57     '306' => 'Playing time',
58     '440' => 'Series statement / Added entry -- Title',
59     '490' => 'Series statement',
60     '500' => 'General note',
61     '504' => 'Bibliography, etc. note',
62     '505' => 'Formatted contents note',
63     '508' => 'Creation/production credits note',
64     '510' => 'Citation/references note',
65     '511' => 'Participant or performer note',
66     '520' => 'Summary, etc. note',
67     '521' => 'Target audience note (ie age)',
68     '530' => 'Additional physical form available note',
69     '538' => 'System details note',
70     '586' => 'Awards note',
71     '600' => 'Subject added entry -- Personal name',
72     '610' => 'Subject added entry -- Corporate name',
73     '650' => 'Subject added entry -- Topical term',
74     '651' => 'Subject added entry -- Geographic name',
75     '656' => 'Index term -- Occupation',
76     '700' => 'Added entry -- Personal name',
77     '710' => 'Added entry -- Corporate name',
78     '730' => 'Added entry -- Uniform title',
79     '740' => 'Added entry -- Uncontrolled related/analytical title',
80     '800' => 'Series added entry -- Personal name',
81     '830' => 'Series added entry -- Uniform title',
82     '852' => 'Location',
83     '856' => 'Electronic location and access',
84 );
85
86 #-------------
87 # Initialize
88
89 my $userid=$ENV{'REMOTE_USER'};
90
91 my $input = new CGI;
92 my $dbh=C4Connect;
93
94 #-------------
95 # Display output
96 print $input->header;
97 print startpage();
98 print startmenu('acquisitions');
99
100 #-------------
101 # Process input parameters
102 my $file=$input->param('file');
103
104 if ($input->param('z3950queue')) {
105     my $query=$input->param('query');
106  
107     my @serverlist;
108
109     my $isbngood=1;
110     if ($input->param('type') eq 'isbn') {
111         $isbngood=CheckIsbn($query);
112     }
113     if ($isbngood) {
114         foreach ($input->param) {
115           if (/S-(.*)/) {
116             my $server=$1;
117             if ($server eq 'MAN') {
118                 push @serverlist, "MAN/".$input->param('manualz3950server')."//"
119 ;
120             } else {
121                 push @serverlist, $server;
122             }
123           }
124         }
125
126         Addz3950queue($input->param('query'), $input->param('type'), 
127                 $input->param('rand'), @serverlist);
128     } else {
129         print "<font color=red size=+1>$query is not a valid ISBN
130         Number</font><p>\n";
131     }
132 }
133
134 sub Addz3950queue {
135     use strict;
136     my (
137         $query,         # value to look up
138         $type,          # type of value ("isbn", "lccn", etc).
139         $requestid,
140         @z3950list,     # list of z3950 servers to query
141     )=@_;
142
143     my (
144         @serverlist,
145         $server,
146         $failed,
147     );
148
149         # list of servers: entry can be a fully qualified URL-type entry
150         #   or simply just a server ID number.
151
152         my $sth=$dbh->prepare("select host,port,db,userid,password 
153           from z3950servers 
154           where id=? ");
155         foreach $server (@z3950list) {
156             if ($server =~ /:/ ) {
157                 push @serverlist, $server;
158             } else {
159                 $sth->execute($server);
160                 my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
161                 push @serverlist, "$server/$host\:$port/$db/$userid/$password";
162             }
163         }
164
165         my $serverlist='';
166         foreach (@serverlist) {
167             $serverlist.="$_ ";
168         }
169         chop $serverlist;
170
171         # Don't allow reinsertion of the same request number.
172         my $sth=$dbh->prepare("select identifier from z3950queue 
173                 where identifier=?");
174         $sth->execute($requestid);
175         unless ($sth->rows) {
176             $sth=$dbh->prepare("insert into z3950queue 
177                 (term,type,servers, identifier) 
178                 values (?, ?, ?, ?)");
179             $sth->execute($query, $type, $serverlist, $requestid);
180         }
181 } # sub
182
183 #--------------------------------------
184 sub CheckIsbn {
185         my ($q)=@_ ;
186
187         my $isbngood = 0;
188
189         $q=~s/[^X\d]//g;
190         $q=~s/X.//g;
191         if (length($q)==10) {
192             my $checksum=substr($q,9,1);
193             my $isbn=substr($q,0,9);
194             my $i;
195             my $c=0;
196             for ($i=0; $i<9; $i++) {
197                 my $digit=substr($q,$i,1);
198                 $c+=$digit*(10-$i);
199             }
200             $c=int(11-($c/11-int($c/11))*11+.1);
201             ($c==10) && ($c='X');
202             if ($c eq $checksum) {
203                 $isbngood=1;
204             } else {
205                 $isbngood=0;
206             }
207         } else {
208             $isbngood=0;
209         }
210
211         return $isbngood;
212
213 } # sub CheckIsbn
214
215
216
217 if (my $data=$input->param('uploadmarc')) {
218     my $name=$input->param('name');
219     ($name) || ($name=$data);
220     my $marcrecord='';
221     if (length($data)>0) {
222         while (<$data>) {
223             $marcrecord.=$_;
224         }
225     }
226     my $q_marcrecord=$dbh->quote($marcrecord);
227     my $q_name=$dbh->quote($name);
228     my $sth=$dbh->prepare("insert into uploadedmarc (marc,name) values ($q_marcrecord, $q_name)");
229     $sth->execute;
230 }
231
232
233 if ($input->param('insertnewrecord')) {
234     my $isbn=$input->param('isbn');
235     my $issn=$input->param('issn');
236     my $lccn=$input->param('lccn');
237     my $q_origisbn=$dbh->quote($input->param('origisbn'));
238     my $q_origissn=$dbh->quote($input->param('origissn'));
239     my $q_origlccn=$dbh->quote($input->param('origlccn'));
240     my $q_origcontrolnumber=$dbh->quote($input->param('origcontrolnumber'));
241     my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
242     my $q_issn=$dbh->quote((($issn) || ('NIL')));
243     my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
244     my $sth=$dbh->prepare("insert into marcrecorddone values ($q_origisbn, $q_origissn, $q_origlccn, $q_origcontrolnumber)");
245     $sth->execute;
246     my $sth=$dbh->prepare("select biblionumber,biblioitemnumber from biblioitems where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn");
247     $sth->execute;
248     my $biblionumber=0;
249     my $biblioitemnumber=0;
250     print "<center>\n";
251     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file>New Record</a> | <a href=marcimport.pl>New File</a><br>\n";
252     if ($sth->rows) {
253         ($biblionumber, $biblioitemnumber) = $sth->fetchrow;
254         my $title=$input->param('title');
255         print << "EOF";
256         <table border=0 width=50% cellpadding=10 cellspacing=0>
257         <tr><th bgcolor=black><font color=white>Record already in database</font></th></tr>
258         <tr><td bgcolor=#dddddd>$title is already in the database with biblionumber $biblionumber and biblioitemnumber $biblioitemnumber</td></tr>
259         </table>
260         <p>
261 EOF
262     } else {
263         my $q_title=$dbh->quote($input->param('title'));
264         my $q_subtitle=$dbh->quote($input->param('subtitle'));
265         my $q_author=$dbh->quote($input->param('author'));
266         my $q_copyrightdate=$dbh->quote($input->param('copyrightdate'));
267         my $q_seriestitle=$dbh->quote($input->param('seriestitle'));
268         $sth=$dbh->prepare("select biblionumber from biblio where title=$q_title and author=$q_author and copyrightdate=$q_copyrightdate and seriestitle=$q_seriestitle");
269         $sth->execute;
270         if ($sth->rows) {
271             ($biblionumber) = $sth->fetchrow;
272         } else {
273             $sth=$dbh->prepare("select max(biblionumber) from biblio");
274             $sth->execute;
275             ($biblionumber) = $sth->fetchrow;
276             $biblionumber++;
277             $sth=$dbh->prepare("insert into biblio (biblionumber, title, author, copyrightdate, seriestitle) values ($biblionumber, $q_title, $q_author, $q_copyrightdate, $q_seriestitle)");
278             $sth->execute;
279             $sth=$dbh->prepare("insert into bibliosubtitle (biblionumber,subtitle) values ($biblionumber, $q_subtitle)");
280             $sth->execute;
281         }
282         $sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
283         $sth->execute;
284         ($biblioitemnumber) = $sth->fetchrow;
285         $biblioitemnumber++;
286         my $q_isbn=$dbh->quote($isbn);
287         my $q_issn=$dbh->quote($issn);
288         my $q_lccn=$dbh->quote($lccn);
289         my $q_volume=$dbh->quote($input->param('volume'));
290         my $q_number=$dbh->quote($input->param('number'));
291         my $q_itemtype=$dbh->quote($input->param('itemtype'));
292         my $q_dewey=$dbh->quote($input->param('dewey'));
293         my $q_subclass=$dbh->quote($input->param('subclass'));
294         my $q_publicationyear=$dbh->quote($input->param('publicationyear'));
295         my $q_publishercode=$dbh->quote($input->param('publishercode'));
296         my $q_volumedate=$dbh->quote($input->param('volumedate'));
297         my $q_volumeddesc=$dbh->quote($input->param('volumeddesc'));
298         my $q_illus=$dbh->quote($input->param('illustrator'));
299         my $q_pages=$dbh->quote($input->param('pages'));
300         my $q_notes=$dbh->quote($input->param('note'));
301         my $q_size=$dbh->quote($input->param('size'));
302         my $q_place=$dbh->quote($input->param('place'));
303         my $q_marc=$dbh->quote($input->param('marc'));
304
305         $sth=$dbh->prepare("insert into biblioitems (biblioitemnumber, biblionumber, volume, number, itemtype, isbn, issn, dewey, subclass, publicationyear, publishercode, volumedate, volumeddesc, illus, pages, notes, size, place, lccn, marc) values ($biblioitemnumber, $biblionumber, $q_volume, $q_number, $q_itemtype, $q_isbn, $q_issn, $q_dewey, $q_subclass, $q_publicationyear, $q_publishercode, $q_volumedate, $q_volumeddesc, $q_illus, $q_pages, $q_notes, $q_size, $q_place, $q_lccn, $q_marc)");
306         $sth->execute;
307         my $subjectheadings=$input->param('subject');
308         my $additionalauthors=$input->param('additionalauthors');
309         my @subjectheadings=split(/\n/,$subjectheadings);
310         my $subjectheading;
311         foreach $subjectheading (@subjectheadings) {
312             # remove any line ending characters (Ctrl-J or M)
313             $subjectheading=~s/\013//g;
314             $subjectheading=~s/\010//g;
315             # convert to upper case
316             $subjectheading=uc($subjectheading);
317             chomp ($subjectheading);
318             while (ord(substr($subjectheading, length($subjectheading)-1, 1))<14) {
319                 chop $subjectheading;
320             }
321             # quote value
322             my $q_subjectheading=$dbh->quote($subjectheading);
323             $sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
324                 values ($biblionumber, $q_subjectheading)");
325             $sth->execute;
326         }
327         my @additionalauthors=split(/\n/,$additionalauthors);
328         my $additionalauthor;
329         foreach $additionalauthor (@additionalauthors) {
330             # remove any line ending characters (Ctrl-L or Ctrl-M)
331             $additionalauthor=~s/\013//g;
332             $additionalauthor=~s/\010//g;
333             # convert to upper case
334             $additionalauthor=uc($additionalauthor);
335             # quote value
336             my $q_additionalauthor=$dbh->quote($additionalauthor);
337             $sth=$dbh->prepare("insert into additionalauthors (biblionumber,author) values ($biblionumber, $q_additionalauthor)");
338             $sth->execute;
339         }
340
341         my $title=$input->param('title');
342         print << "EOF";
343         <table cellpadding=10 cellspacing=0 border=0 width=50%>
344         <tr><th bgcolor=black><font color=white>Record entered into database</font></th></tr>
345         <tr><td bgcolor=#dddddd>$title has been entered into the database with biblionumber
346         $biblionumber and biblioitemnumber $biblioitemnumber</td></tr>
347         </table>
348 EOF
349     }
350     my $title=$input->param('title');
351
352     # Get next barcode, or pick random one if none exist yet
353     $sth=$dbh->prepare("select max(barcode) from items");
354     $sth->execute;
355     my ($barcode) = $sth->fetchrow;
356     $barcode++;
357     if ($barcode==1) {
358         $barcode=int(rand()*1000000);
359     }
360
361     my $branchselect=GetKeyTableSelectOptions(
362                 $dbh, 'branches', 'branchcode', 'branchname', 0);
363
364     print << "EOF";
365     <table border=0 cellpadding=10 cellspacing=0>
366     <tr><th bgcolor=black><font color=white>
367 Add a New Item for $title
368 </font>
369 </th></tr>
370 <tr><td bgcolor=#dddddd>
371 <form>
372 <input type=hidden name=newitem value=1>
373 <input type=hidden name=biblionumber value=$biblionumber>
374 <input type=hidden name=biblioitemnumber value=$biblioitemnumber>
375 <input type=hidden name=file value=$file>
376 <table border=0>
377 <tr><td>BARCODE</td><td><input name=barcode size=10 value=$barcode>
378
379 Home Branch: <select name=homebranch> $branchselect </select></td></tr>
380
381 </tr><td>Replacement Price:</td><td><input name=replacementprice size=10></td></tr>
382 <tr><td>Notes</td><td><textarea name=notes rows=4 cols=40
383 wrap=physical></textarea></td></tr>
384 </table>
385 </td></tr>
386 </table>
387 <p>
388 <input type=submit value="Add Item">
389 </form>
390 EOF
391 print endmenu();
392 print endpage();
393
394 exit;
395 }
396
397 if ($input->param('newitem')) {
398     my $barcode=$input->param('barcode');
399     my $q_barcode=$dbh->quote($barcode);
400     my $q_notes=$dbh->quote($input->param('notes'));
401     my $q_homebranch=$dbh->quote($input->param('homebranch'));
402     my $biblionumber=$input->param('biblionumber');
403     my $biblioitemnumber=$input->param('biblioitemnumber');
404     my $replacementprice=($input->param('replacementprice') || 0);
405     my $sth=$dbh->prepare("select barcode from items where
406     barcode=$q_barcode");
407     $sth->execute;
408     if ($sth->rows) {
409         print "<font color=red>Barcode '$barcode' has already been assigned.</font><p>\n";
410     } else {
411         $sth=$dbh->prepare("select max(itemnumber) from items");
412         $sth->execute;
413         my ($itemnumber) = $sth->fetchrow;
414         $itemnumber++;
415         my @datearr=localtime(time);
416         my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
417         $sth=$dbh->prepare("insert into items (itemnumber, biblionumber, biblioitemnumber, barcode, itemnotes, homebranch, holdingbranch, dateaccessioned, replacementprice) values ($itemnumber, $biblionumber, $biblioitemnumber, $q_barcode, $q_notes, $q_homebranch, 'STWE', '$date', $replacementprice)");
418         $sth->execute;
419     }
420 }
421
422
423 my $menu = $input->param('menu');
424 if ($file) {
425     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
426     my $qisbn=$input->param('isbn');
427     my $qissn=$input->param('issn');
428     my $qlccn=$input->param('lccn');
429     my $qcontrolnumber=$input->param('controlnumber');
430     if ($qisbn || $qissn || $qlccn || $qcontrolnumber) {
431         print "<a href=$ENV{'SCRIPT_NAME'}>New File</a><hr>\n";
432         #open (F, "$file");
433         #my $data=<F>;
434         my $data;
435         if ($file=~/Z-(\d+)/) {
436             my $id=$1;
437             my $resultsid=$input->param('resultsid');
438             my $sth=$dbh->prepare("select results from z3950results where id=$resultsid");
439             $sth->execute;
440             ($data) = $sth->fetchrow;
441         } else {
442             my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file");
443             $sth->execute;
444             ($data) = $sth->fetchrow;
445         }
446
447         $splitchar=chr(29);
448         my @records;
449         foreach $record (split(/$splitchar/, $data)) {
450             my $marctext="<table border=0 cellspacing=0>\n";
451             $marctext.="<tr><th colspan=3 bgcolor=black><font color=white>MARC RECORD</font></th></tr>\n";
452             $leader=substr($record,0,24);
453             $marctext.="<tr><td>Leader:</td><td colspan=2>$leader</td></tr>\n";
454             $record=substr($record,24);
455             $splitchar2=chr(30);
456             my $directory=0;
457             my $tagcounter=0;
458             my %tag;
459             my @record;
460             foreach $field (split(/$splitchar2/, $record)) {
461                 my %field;
462                 ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
463                 unless ($directory) {
464                     $directory=$field;
465                     my $itemcounter=1;
466                     $counter=0;
467                     while ($item=substr($directory,0,12)) {
468                         $tag=substr($directory,0,3);
469                         $length=substr($directory,3,4);
470                         $start=substr($directory,7,6);
471                         $directory=substr($directory,12);
472                         $tag{$counter}=$tag;
473                         $counter++;
474                     }
475                     $directory=1;
476                     next;
477                 }
478                 $tag=$tag{$tagcounter};
479                 $tagcounter++;
480                 $field{'tag'}=$tag;
481                 $marctext.="<tr><td bgcolor=$color valign=top>$tagtext{$tag}</td><td bgcolor=$color valign=top>$tag</td>";
482                 $splitchar3=chr(31);
483                 my @subfields=split(/$splitchar3/, $field);
484                 $indicator=$subfields[0];
485                 $field{'indicator'}=$indicator;
486                 my $firstline=1;
487                 if ($#subfields==0) {
488                     $marctext.="<td bgcolor=$color valign=top>$indicator</td></tr>";
489                 } else {
490                     my %subfields;
491                     $marctext.="<td bgcolor=$color valign=top><table border=0 cellspacing=0>\n";
492                     my $color2=$color;
493                     for ($i=1; $i<=$#subfields; $i++) {
494                         ($color2 eq $lc1) ? ($color2=$lc2) : ($color2=$lc1);
495                         my $text=$subfields[$i];
496                         my $subfieldcode=substr($text,0,1);
497                         my $subfield=substr($text,1);
498                         $marctext.="<tr><td colour=$color2><table border=0 cellpadding=0 cellspacing=0><tr><td>$subfieldcode </td></tr></table></td><td colour=$color2>$subfield</td></tr>\n";
499                         if ($subfields{$subfieldcode}) {
500                             my $subfieldlist=$subfields{$subfieldcode};
501                             my @subfieldlist=@$subfieldlist;
502                             if ($#subfieldlist>=0) {
503                                 push (@subfieldlist, $subfield);
504                             } else {
505                                 @subfieldlist=($subfields{$subfieldcode}, $subfield);
506                             }
507                             $subfields{$subfieldcode}=\@subfieldlist;
508                         } else {
509                             $subfields{$subfieldcode}=$subfield;
510                         }
511                     }
512                     $marctext.="</table></td></tr>\n";
513                     $field{'subfields'}=\%subfields;
514                 }
515                 push (@record, \%field);
516             }
517             $marctext.="</table>\n";
518             $marctext{\@record}=$marctext;
519             $marc{\@record}=$record;
520             push (@records, \@record);
521             $counter++;
522         }
523 RECORD:
524         foreach $record (@records) {
525             my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $additionalauthors, $illustrator, $copyrightdate, $seriestitle);
526             my $marctext=$marctext{$record};
527             my $marc=$marc{$record};
528             foreach $field (@$record) {
529                 if ($field->{'tag'} eq '001') {
530                     $controlnumber=$field->{'indicator'};
531                 }
532                 if ($field->{'tag'} eq '010') {
533                     $lccn=$field->{'subfields'}->{'a'};
534                     $lccn=~s/^\s*//;
535                     ($lccn) = (split(/\s+/, $lccn))[0];
536                 }
537                 if ($field->{'tag'} eq '015') {
538                     $lccn=$field->{'subfields'}->{'a'};
539                     $lccn=~s/^\s*//;
540                     $lccn=~s/^C//;
541                     ($lccn) = (split(/\s+/, $lccn))[0];
542                 }
543                 if ($field->{'tag'} eq '020') {
544                     $isbn=$field->{'subfields'}->{'a'};
545                     ($isbn=~/^ARRAY/) && ($isbn=$$isbn[0]);
546                     $isbn=~s/[^\d]*//g;
547                 }
548                 if ($field->{'tag'} eq '022') {
549                     $issn=$field->{'subfields'}->{'a'};
550                     $issn=~s/^\s*//;
551                     ($issn) = (split(/\s+/, $issn))[0];
552                 }
553                 if ($field->{'tag'} eq '082') {
554                     $dewey=$field->{'subfields'}->{'a'};
555                     $dewey=~s/\///g;
556                     if (@$dewey) {
557                         $dewey=$$dewey[0];
558                     }
559                     #$dewey=~s/\///g;
560                 }
561                 if ($field->{'tag'} eq '100') {
562                     $author=$field->{'subfields'}->{'a'};
563                 }
564                 if ($field->{'tag'} eq '245') {
565                     $title=$field->{'subfields'}->{'a'};
566                     $title=~s/ \/$//;
567                     $subtitle=$field->{'subfields'}->{'b'};
568                     $subtitle=~s/ \/$//;
569                 }
570                 if ($field->{'tag'} eq '260') {
571                     $place=$field->{'subfields'}->{'a'};
572                     if (@$place) {
573                         $place=$$place[0];
574                     }
575                     $place=~s/\s*:$//g;
576                     $publisher=$field->{'subfields'}->{'b'};
577                     if (@$publisher) {
578                         $publisher=$$publisher[0];
579                     }
580                     $publisher=~s/\s*:$//g;
581                     $publicationyear=$field->{'subfields'}->{'c'};
582                     if ($publicationyear=~/c(\d\d\d\d)/) {
583                         $copyrightdate=$1;
584                     }
585                     if ($publicationyear=~/[^c](\d\d\d\d)/) {
586                         $publicationyear=$1;
587                     } elsif ($copyrightdate) {
588                         $publicationyear=$copyrightdate;
589                     } else {
590                         $publicationyear=~/(\d\d\d\d)/;
591                         $publicationyear=$1;
592                     }
593                 }
594                 if ($field->{'tag'} eq '300') {
595                     $pages=$field->{'subfields'}->{'a'};
596                     $pages=~s/ \;$//;
597                     $size=$field->{'subfields'}->{'c'};
598                     $pages=~s/\s*:$//g;
599                     $size=~s/\s*:$//g;
600                 }
601                 if ($field->{'tag'} eq '362') {
602                     if ($field->{'subfields'}->{'a'}=~/(\d+).*(\d+)/) {
603                         $volume=$1;
604                         $number=$2;
605                     }
606                 }
607                 if ($field->{'tag'} eq '440') {
608                     $seriestitle=$field->{'subfields'}->{'a'};
609                     if ($field->{'subfields'}->{'v'}=~/(\d+).*(\d+)/) {
610                         $volume=$1;
611                         $number=$2;
612                     }
613                 }
614                 if ($field->{'tag'} eq '700') {
615                     my $name=$field->{'subfields'}->{'a'};
616                     if ($field->{'subfields'}->{'c'}=~/ill/) {
617                         $additionalauthors.="$name\n";
618                     } else {
619                         $illustrator=$name;
620                     }
621                 }
622                 if ($field->{'tag'} =~/^5/) {
623                     $note.="$field->{'subfields'}->{'a'}\n";
624                 }
625                 if ($field->{'tag'} =~/65\d/) {
626                     my $subject=$field->{'subfields'}->{'a'};
627                     $subject=~s/\.$//;
628                     if ($gensubdivision=$field->{'subfields'}->{'x'}) {
629                         my @sub=@$gensubdivision;
630                         if ($#sub>=0) {
631                             foreach $s (@sub) {
632                                 $s=~s/\.$//;
633                                 $subject.=" -- $s";
634                             }
635                         } else {
636                             $gensubdivision=~s/\.$//;
637                             $subject.=" -- $gensubdivision";
638                         }
639                     }
640                     if ($chronsubdivision=$field->{'subfields'}->{'y'}) {
641                         my @sub=@$chronsubdivision;
642                         if ($#sub>=0) {
643                             foreach $s (@sub) {
644                                 $s=~s/\.$//;
645                                 $subject.=" -- $s";
646                             }
647                         } else {
648                             $chronsubdivision=~s/\.$//;
649                             $subject.=" -- $chronsubdivision";
650                         }
651                     }
652                     if ($geosubdivision=$field->{'subfields'}->{'z'}) {
653                         my @sub=@$geosubdivision;
654                         if ($#sub>=0) {
655                             foreach $s (@sub) {
656                                 $s=~s/\.$//;
657                                 $subject.=" -- $s";
658                             }
659                         } else {
660                             $geosubdivision=~s/\.$//;
661                             $subject.=" -- $geosubdivision";
662                         }
663                     }
664                     push @subjects, $subject;
665                 }
666             }
667             $titleinput=$input->textfield(-name=>'title', -default=>$title, -size=>40);
668             $marcinput=$input->hidden(-name=>'marc', -default=>$marc);
669             $subtitleinput=$input->textfield(-name=>'subtitle', -default=>$subtitle, -size=>40);
670             $authorinput=$input->textfield(-name=>'author', -default=>$author);
671             $illustratorinput=$input->textfield(-name=>'illustrator', -default=>$illustrator);
672             $additionalauthorsinput=$input->textarea(-name=>'additionalauthors', -default=>$additionalauthors, -rows=>4, -cols=>20);
673             my $subject='';
674             foreach (@subjects) {
675                 $subject.="$_\n";
676             }
677             $subjectinput=$input->textarea(-name=>'subject', -default=>$subject, -rows=>4, -cols=>40);
678             $noteinput=$input->textarea(-name=>'note', -default=>$note, -rows=>4, -cols=>40, -wrap=>'physical');
679             $copyrightinput=$input->textfield(-name=>'copyrightdate', -default=>$copyrightdate);
680             $seriestitleinput=$input->textfield(-name=>'seriestitle', -default=>$seriestitle);
681             $volumeinput=$input->textfield(-name=>'volume', -default=>$volume);
682             $volumedateinput=$input->textfield(-name=>'volumedate', -default=>$volumedate);
683             $volumeddescinput=$input->textfield(-name=>'volumeddesc', -default=>$volumeddesc);
684             $numberinput=$input->textfield(-name=>'number', -default=>$number);
685             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
686             $issninput=$input->textfield(-name=>'issn', -default=>$issn);
687             $lccninput=$input->textfield(-name=>'lccn', -default=>$lccn);
688             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
689             $deweyinput=$input->textfield(-name=>'dewey', -default=>$dewey);
690             $cleanauthor=$author;
691             $cleanauthor=~s/[^A-Za-z]//g;
692             $subclassinput=$input->textfield(-name=>'subclass', -default=>uc(substr($cleanauthor,0,3)));
693             $publisherinput=$input->textfield(-name=>'publishercode', -default=>$publisher);
694             $pubyearinput=$input->textfield(-name=>'publicationyear', -default=>$publicationyear);
695             $placeinput=$input->textfield(-name=>'place', -default=>$place);
696             $pagesinput=$input->textfield(-name=>'pages', -default=>$pages);
697             $sizeinput=$input->textfield(-name=>'size', -default=>$size);
698             $fileinput=$input->hidden(-name=>'file', -default=>$file);
699             $origisbn=$input->hidden(-name=>'origisbn', -default=>$isbn);
700             $origissn=$input->hidden(-name=>'origissn', -default=>$issn);
701             $origlccn=$input->hidden(-name=>'origlccn', -default=>$lccn);
702             $origcontrolnumber=$input->hidden(-name=>'origcontrolnumber', -default=>$controlnumber);
703
704             #print "<PRE>getting itemtypeselect</PRE>\n";
705             $itemtypeselect=&GetKeyTableSelectOptions(
706                 $dbh, 'itemtypes', 'itemtype', 'description', 1);
707             #print "<PRE>it=$itemtypeselect</PRE>\n";
708
709             ($qissn) || ($qissn='NIL');
710             ($qlccn) || ($qlccn='NIL');
711             ($qisbn) || ($qisbn='NIL');
712             ($qcontrolnumber) || ($qcontrolnumber='NIL');
713             $controlnumber=~s/\s+//g;
714
715             unless (($isbn eq $qisbn) || ($issn eq $qissn) || ($lccn eq $qlccn) || ($controlnumber eq $qcontrolnumber)) {
716                 #print "<PRE>Skip record $isbn $issn $lccn </PRE>\n";
717                 next RECORD;
718             }
719
720             print << "EOF";
721             <center>
722             <h1>New Record</h1>
723             Full MARC Record available at bottom
724             <form method=post>
725             <table border=1>
726             <tr><td>Title</td><td>$titleinput</td></tr>
727             <tr><td>Subtitle</td><td>$subtitleinput</td></tr>
728             <tr><td>Author</td><td>$authorinput</td></tr>
729             <tr><td>Additional Authors</td><td>$additionalauthorsinput</td></tr>
730             <tr><td>Illustrator</td><td>$illustratorinput</td></tr>
731             <tr><td>Copyright</td><td>$copyrightinput</td></tr>
732             <tr><td>Series Title</td><td>$seriestitleinput</td></tr>
733             <tr><td>Volume</td><td>$volumeinput</td></tr>
734             <tr><td>Number</td><td>$numberinput</td></tr>
735             <tr><td>Volume Date</td><td>$volumedateinput</td></tr>
736             <tr><td>Volume Description</td><td>$volumeddescinput</td></tr>
737             <tr><td>Subject</td><td>$subjectinput</td></tr>
738             <tr><td>Notes</td><td>$noteinput</td></tr>
739             <tr><td>Item Type</td><td><select name=itemtype>$itemtypeselect</select></td></tr>
740             <tr><td>ISBN</td><td>$isbninput</td></tr>
741             <tr><td>ISSN</td><td>$issninput</td></tr>
742             <tr><td>LCCN</td><td>$lccninput</td></tr>
743             <tr><td>Dewey</td><td>$deweyinput</td></tr>
744             <tr><td>Subclass</td><td>$subclassinput</td></tr>
745             <tr><td>Publication Year</td><td>$pubyearinput</td></tr>
746             <tr><td>Publisher</td><td>$publisherinput</td></tr>
747             <tr><td>Place</td><td>$placeinput</td></tr>
748             <tr><td>Pages</td><td>$pagesinput</td></tr>
749             <tr><td>Size</td><td>$sizeinput</td></tr>
750             </table>
751             <input type=submit>
752             <input type=hidden name=insertnewrecord value=1>
753             $fileinput
754             $marcinput
755             $origisbn
756             $origissn
757             $origlccn
758             $origcontrolnumber
759             </form>
760             $marctext
761 EOF
762         }
763     } else {
764         #open (F, "$file");
765         #my $data=<F>;
766         my $data;
767         my $name;
768         my $z3950=0;
769         if ($file=~/Z-(\d+)/) {
770             print << "EOF";
771 <center>
772 <p>
773 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
774 <p>
775 <table border=0 cellpadding=10 cellspacing=0>
776 <tr><th bgcolor=black><font color=white>Select a Record to Import</font></th></tr>
777 <tr><td bgcolor=#dddddd>
778 EOF
779             my $id=$1;
780             my $sth=$dbh->prepare("select servers from z3950queue where id=$id");
781             $sth->execute;
782             my ($servers) = $sth->fetchrow;
783             my $serverstring;
784             my $starttimer=time();
785             foreach $serverstring (split(/\s+/, $servers)) {
786                 my ($name, $server, $database, $auth) = split(/\//, $serverstring, 4);
787                 if ($name eq 'MAN') {
788                     print "$server/$database<br>\n";
789                 } else {
790                     my $sti=$dbh->prepare("select name from
791                     z3950servers where id=$name");
792                     $sti->execute;
793                     my ($longname)=$sti->fetchrow;
794                     print "<a name=SERVER-$name></a>\n";
795                     if ($longname) {
796                         print "$longname \n";
797                     } else {
798                         print "$server/$database \n";
799                     }
800                 }
801                 my $q_server=$dbh->quote($serverstring);
802                 my $startrecord=$input->param("ST-$name");
803                 ($startrecord) || ($startrecord='0');
804                 my $sti=$dbh->prepare("select numrecords,id,results,startdate,enddate from z3950results where queryid=$id and server=$q_server");
805                 $sti->execute;
806                 ($numrecords,$resultsid,$data,$startdate,$enddate) = $sti->fetchrow;
807                 my $serverplaceholder='';
808                 foreach ($input->param) {
809                     (next) unless (/ST-(.+)/);
810                     my $serverid=$1;
811                     (next) if ($serverid eq $name);
812                     my $place=$input->param("ST-$serverid");
813                     $serverplaceholder.="\&ST-$serverid=$place";
814                 }
815                 if ($numrecords) {
816                     my $previous='';
817                     my $next='';
818                     if ($startrecord>0) {
819                         $previous="<a href=".$ENV{'SCRIPT_NAME'}."?file=Z-$id&menu=z3950$serverplaceholder\&ST-$name=".($startrecord-10)."#SERVER-$name>Previous</a>";
820                     }
821                     my $highest;
822                     $highest=$startrecord+10;
823                     ($highest>$numrecords) && ($highest=$numrecords);
824                     if ($numrecords>$startrecord+10) {
825                         $next="<a href=".$ENV{'SCRIPT_NAME'}."?file=Z-$id&menu=z3950$serverplaceholder\&ST-$name=$highest#SERVER-$name>Next</a>";
826                     }
827                     print "<font size=-1>[Viewing ".($startrecord+1)." to ".$highest." of $numrecords records]  $previous | $next </font><br>\n";
828                 } else {
829                     print "<br>\n";
830                 }
831                 print "<ul>\n";
832                 my $stj=$dbh->prepare("update z3950results set highestseen=".($startrecord+10)." where id=$resultsid");
833                 $stj->execute;
834                 if ($sti->rows == 0) {
835                     print "pending...";
836                 } elsif ($enddate == 0) {
837                     my $now=time();
838                     my $elapsed=$now-$startdate;
839                     my $elapsedtime='';
840                     if ($elapsed>60) {
841                         $elapsedtime=sprintf "%d minutes",($elapsed/60);
842                     } else {
843                         $elapsedtime=sprintf "%d seconds",$elapsed;
844                     }
845                     print "<font color=red>processing... ($elapsedtime)</font>";
846                 } elsif ($numrecords) {
847                     my $splitchar=chr(29);
848                     my @records=split(/$splitchar/, $data);
849                     $data='';
850                     for ($i=$startrecord; $i<$startrecord+10; $i++) {
851                         $data.=$records[$i].$splitchar;
852                     }
853                     @records=parsemarcdata($data);
854                     my $counter=0;
855                     foreach $record (@records) {
856                         $counter++;
857                         #(next) unless ($counter>=$startrecord && $counter<=$startrecord+10);
858                         my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
859                         foreach $field (@$record) {
860                             if ($field->{'tag'} eq '001') {
861                                 $controlnumber=$field->{'indicator'};
862                             }
863                             if ($field->{'tag'} eq '010') {
864                                 $lccn=$field->{'subfields'}->{'a'};
865                                 $lccn=~s/^\s*//;
866                                 ($lccn) = (split(/\s+/, $lccn))[0];
867                             }
868                             if ($field->{'tag'} eq '015') {
869                                 $lccn=$field->{'subfields'}->{'a'};
870                                 $lccn=~s/^\s*//;
871                                 $lccn=~s/^C//;
872                                 ($lccn) = (split(/\s+/, $lccn))[0];
873                             }
874                             if ($field->{'tag'} eq '020') {
875                                 $isbn=$field->{'subfields'}->{'a'};
876                                 ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
877                                 $isbn=~s/[^\d]*//g;
878                             }
879                             if ($field->{'tag'} eq '022') {
880                                 $issn=$field->{'subfields'}->{'a'};
881                                 $issn=~s/^\s*//;
882                                 ($issn) = (split(/\s+/, $issn))[0];
883                             }
884                             if ($field->{'tag'} eq '100') {
885                                 $author=$field->{'subfields'}->{'a'};
886                             }
887                             if ($field->{'tag'} eq '245') {
888                                 $title=$field->{'subfields'}->{'a'};
889                                 $title=~s/ \/$//;
890                                 $subtitle=$field->{'subfields'}->{'b'};
891                                 $subtitle=~s/ \/$//;
892                             }
893                         }
894                         my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
895                         my $q_issn=$dbh->quote((($issn) || ('NIL')));
896                         my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
897                         my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
898                         my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
899                         $sth->execute;
900                         my $donetext='';
901                         if ($sth->rows) {
902                             $donetext="DONE";
903                         }
904                         $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
905                         $sth->execute;
906                         if ($sth->rows) {
907                             $donetext="DONE";
908                         }
909                         ($author) && ($author="by $author");
910                         if ($isbn) {
911                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&isbn=$isbn>$title $subtitle $author</a> $donetext<br>\n";
912                         } elsif ($lccn) {
913                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&lccn=$lccn>$title $subtitle $author</a> $donetext<br>\n";
914                         } elsif ($issn) {
915                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&issn=$issn>$title $subtitle $author</a><br> $donetext\n";
916                         } elsif ($controlnumber) {
917                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&controlnumber=$controlnumber>$title $subtitle $author</a><br> $donetext\n";
918                         } else {
919                             print "Error: Contact steve regarding $title by $author<br>\n";
920                         }
921                     }
922                     print "<p>\n";
923                 } else {
924                     print "No records returned.<p>\n";
925                 }
926                 print "</ul>\n";
927             }
928             my $elapsed=time()-$starttimer;
929             print "<hr>It took $elapsed seconds to process this page.\n";
930         } else {
931             my $sth=$dbh->prepare("select marc,name from uploadedmarc where id=$file");
932             $sth->execute;
933             ($data, $name) = $sth->fetchrow;
934             print << "EOF";
935 <center>
936 <p>
937 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
938 <p>
939 <table border=0 cellpadding=10 cellspacing=0>
940 <tr><th bgcolor=black><font color=white>Select a Record to Import<br>from $name</font></th></tr>
941 <tr><td bgcolor=#dddddd>
942 EOF
943             
944             my @records=parsemarcdata($data);
945             foreach $record (@records) {
946                 my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
947                 foreach $field (@$record) {
948                     if ($field->{'tag'} eq '001') {
949                         $controlnumber=$field->{'indicator'};
950                     }
951                     if ($field->{'tag'} eq '010') {
952                         $lccn=$field->{'subfields'}->{'a'};
953                         $lccn=~s/^\s*//;
954                         ($lccn) = (split(/\s+/, $lccn))[0];
955                     }
956                     if ($field->{'tag'} eq '015') {
957                         $lccn=$field->{'subfields'}->{'a'};
958                         $lccn=~s/^\s*//;
959                         $lccn=~s/^C//;
960                         ($lccn) = (split(/\s+/, $lccn))[0];
961                     }
962                     if ($field->{'tag'} eq '020') {
963                         $isbn=$field->{'subfields'}->{'a'};
964                         ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
965                         $isbn=~s/[^\d]*//g;
966                     }
967                     if ($field->{'tag'} eq '022') {
968                         $issn=$field->{'subfields'}->{'a'};
969                         $issn=~s/^\s*//;
970                         ($issn) = (split(/\s+/, $issn))[0];
971                     }
972                     if ($field->{'tag'} eq '100') {
973                         $author=$field->{'subfields'}->{'a'};
974                     }
975                     if ($field->{'tag'} eq '245') {
976                         $title=$field->{'subfields'}->{'a'};
977                         $title=~s/ \/$//;
978                         $subtitle=$field->{'subfields'}->{'b'};
979                         $subtitle=~s/ \/$//;
980                     }
981                 }
982                 my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
983                 my $q_issn=$dbh->quote((($issn) || ('NIL')));
984                 my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
985                 my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
986                 my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
987                 $sth->execute;
988                 my $donetext='';
989                 if ($sth->rows) {
990                     $donetext="DONE";
991                 }
992                 $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
993                 $sth->execute;
994                 if ($sth->rows) {
995                     $donetext="DONE";
996                 }
997                 ($author) && ($author="by $author");
998                 if ($isbn) {
999                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&isbn=$isbn>$title$subtitle $author</a> $donetext<br>\n";
1000                 } elsif ($lccn) {
1001                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&lccn=$lccn>$title$subtitle $author</a> $donetext<br>\n";
1002                 } elsif ($issn) {
1003                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&issn=$issn>$title$subtitle $author</a><br> $donetext\n";
1004                 } elsif ($controlnumber) {
1005                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&controlnumber=$controlnumber>$title by $author</a><br> $donetext\n";
1006                 } else {
1007                     print "Error: Contact steve regarding $title by $author<br>\n";
1008                 }
1009             }
1010         }
1011         print "</td></tr></table>\n";
1012     }
1013 } else {
1014
1015 SWITCH:
1016     {
1017         if ($menu eq 'z3950') { z3950(); last SWITCH; }
1018         if ($menu eq 'uploadmarc') { uploadmarc(); last SWITCH; }
1019         if ($menu eq 'manual') { manual(); last SWITCH; }
1020         mainmenu();
1021     }
1022
1023 }
1024
1025
1026 sub z3950 {
1027     my $sth=$dbh->prepare("select id,term,type,done,numrecords,length(results),startdate,enddate,servers from z3950queue order by id desc limit 20");
1028     $sth->execute;
1029     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
1030     print "<table border=0><tr><td valign=top>\n";
1031     print "<h2>Results of Z39.50 searches</h2>\n";
1032     print "<a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Refresh</a><br>\n<ul>\n";
1033     while (my ($id, $term, $type, $done, $numrecords, $length, $startdate, $enddate, $servers) = $sth->fetchrow) {
1034         $type=uc($type);
1035         $term=~s/</&lt;/g;
1036         $term=~s/>/&gt;/g;
1037         my $sti=$dbh->prepare("select id,server,startdate,enddate,numrecords from z3950results where queryid=$id");
1038         $sti->execute;
1039         if ($sti->rows) {
1040             my $processing=0;
1041             my $realenddate=0;
1042             my $totalrecords=0;
1043             while (my ($r_id,$r_server,$r_startdate,$r_enddate,$r_numrecords) = $sti->fetchrow) {
1044                 if ($r_enddate==0) {
1045                     $processing=1;
1046                 } else {
1047                     if ($r_enddate>$realenddate) {
1048                         $realenddate=$r_enddate;
1049                     }
1050                 }
1051
1052                 $totalrecords+=$r_numrecords;
1053             }
1054             if ($processing) {
1055                 my $elapsed=time()-$startdate;
1056                 my $elapsedtime='';
1057                 if ($elapsed>60) {
1058                     $elapsedtime=sprintf "%d minutes",($elapsed/60);
1059                 } else {
1060                     $elapsedtime=sprintf "%d seconds",$elapsed;
1061                 }
1062                 if ($totalrecords) {
1063                     $totalrecords="$totalrecords found.";
1064                 } else {
1065                     $totalrecords='';
1066                 }
1067                 print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1 color=red>Processing... $totalrecords ($elapsedtime)</font><br>\n";
1068             } else {
1069                 my $elapsed=$realenddate-$startdate;
1070                 my $elapsedtime='';
1071                 if ($elapsed>60) {
1072                     $elapsedtime=sprintf "%d minutes",($elapsed/60);
1073                 } else {
1074                     $elapsedtime=sprintf "%d seconds",$elapsed;
1075                 }
1076                 if ($totalrecords) {
1077                     $totalrecords="$totalrecords found.";
1078                 } else {
1079                     $totalrecords='';
1080                 }
1081                 print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Done. $totalrecords ($elapsedtime)</font><br>\n";
1082             }
1083         } else {
1084             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Pending</font><br>\n";
1085         }
1086     }
1087     print "</ul>\n";
1088     print "</td><td valign=top width=30%>\n";
1089     my $sth=$dbh->prepare("select id,name,checked from z3950servers order by rank");
1090     $sth->execute;
1091     my $serverlist='';
1092     while (my ($id, $name, $checked) = $sth->fetchrow) {
1093         ($checked) ? ($checked='checked') : ($checked='');
1094         $serverlist.="<input type=checkbox name=S-$id $checked> $name<br>\n";
1095     }
1096     $serverlist.="<input type=checkbox name=S-MAN> <input name=manualz3950server size=25 value=otherserver:210/DATABASE>\n";
1097     
1098     my $rand=rand(1000000000);
1099 print << "EOF";
1100     <form action=$ENV{'SCRIPT_NAME'} method=GET>
1101     <input type=hidden name=z3950queue value=1>
1102     <input type=hidden name=menu value=$menu>
1103     <p>
1104     <input type=hidden name=test value=testvalue>
1105     <input type=hidden name=rand value=$rand>
1106     <table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb colspan=2>Search for MARC records</th></tr>
1107     <tr><td>Query Term</td><td><input name=query></td></tr>
1108     <tr><td colspan=2 align=center><input type=radio name=type value=isbn checked>&nbsp;ISBN <input type=radio name=type value=lccn>&nbsp;LCCN<br><input type=radio name=type value=author>&nbsp;Author <input type=radio name=type value=title>&nbsp;Title <input type=radio name=type value=keyword>&nbsp;Keyword</td></tr>
1109     <tr><td colspan=2>
1110     $serverlist
1111     </td></tr>
1112     <tr><td colspan=2 align=center>
1113     <input type=submit>
1114     </td></tr>
1115     </table>
1116
1117     </form>
1118 EOF
1119 print "</td></tr></table>\n";
1120 }
1121
1122 sub uploadmarc {
1123     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
1124     my $sth=$dbh->prepare("select id,name from uploadedmarc");
1125     $sth->execute;
1126     print "<h2>Select a set of MARC records</h2>\n<ul>";
1127     while (my ($id, $name) = $sth->fetchrow) {
1128         print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$id&menu=$menu>$name</a><br>\n";
1129     }
1130     print "</ul>\n";
1131     print "<p>\n";
1132     print "<table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb
1133     colspan=2>Upload a set of MARC records</th></tr>\n";
1134     print "<tr><td>Upload a set of MARC records:</td><td>";
1135     print $input->start_multipart_form();
1136     print $input->filefield('uploadmarc');
1137     print << "EOF";
1138     </td></tr>
1139     <tr><td>
1140     <input type=hidden name=menu value=$menu>
1141     Name this set of MARC records:</td><td><input type=text
1142     name=name></td></tr>
1143     <tr><td colspan=2 align=center>
1144     <input type=submit>
1145     </td></tr>
1146     </table>
1147     </form>
1148 EOF
1149 }
1150
1151 sub manual {
1152 }
1153
1154
1155 sub mainmenu {
1156     print << "EOF";
1157 <h1>Main Menu</h1>
1158 <ul>
1159 <li><a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Z39.50 Search</a>
1160 <li><a href=$ENV{'SCRIPT_NAME'}?menu=uploadmarc>Upload MARC Records</a>
1161 </ul>
1162 EOF
1163 }
1164
1165 sub skip {
1166
1167     #opendir(D, "/home/$userid/");
1168     #my @dirlist=readdir D;
1169     #foreach $file (@dirlist) {
1170 #       (next) if ($file=~/^\./);
1171 #       (next) if ($file=~/^nsmail$/);
1172 #       (next) if ($file=~/^public_html$/);
1173 #       ($file=~/\.mrc/) || ($filelist.="$file<br>\n");
1174 #       (next) unless ($file=~/\.mrc$/);
1175 #       $file=~s/ /\%20/g;
1176 #       print "<a href=$ENV{'SCRIPT_NAME'}?file=/home/$userid/$file>$file</a><br>\n";
1177 #    }
1178
1179
1180     #<form action=$ENV{'SCRIPT_NAME'} method=POST enctype=multipart/form-data>
1181
1182 }
1183 print endmenu();
1184 print endpage();
1185
1186 sub parsemarcdata {
1187     my $data=shift;
1188     my $splitchar=chr(29);
1189     my @records;
1190     my $record;
1191     foreach $record (split(/$splitchar/, $data)) {
1192         my $leader=substr($record,0,24);
1193         #print "<tr><td>Leader:</td><td>$leader</td></tr>\n";
1194         $record=substr($record,24);
1195         my $splitchar2=chr(30);
1196         my $directory=0;
1197         my $tagcounter=0;
1198         my %tag;
1199         my @record;
1200         my $field;
1201         foreach $field (split(/$splitchar2/, $record)) {
1202             my %field;
1203             ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
1204             unless ($directory) {
1205                 $directory=$field;
1206                 my $itemcounter=1;
1207                 $counter=0;
1208                 while ($item=substr($directory,0,12)) {
1209                     $tag=substr($directory,0,3);
1210                     $length=substr($directory,3,4);
1211                     $start=substr($directory,7,6);
1212                     $directory=substr($directory,12);
1213                     $tag{$counter}=$tag;
1214                     $counter++;
1215                 }
1216                 $directory=1;
1217                 next;
1218             }
1219             $tag=$tag{$tagcounter};
1220             $tagcounter++;
1221             $field{'tag'}=$tag;
1222             $splitchar3=chr(31);
1223             my @subfields=split(/$splitchar3/, $field);
1224             $indicator=$subfields[0];
1225             $field{'indicator'}=$indicator;
1226             my $firstline=1;
1227             unless ($#subfields==0) {
1228                 my %subfields;
1229                 for ($i=1; $i<=$#subfields; $i++) {
1230                     my $text=$subfields[$i];
1231                     my $subfieldcode=substr($text,0,1);
1232                     my $subfield=substr($text,1);
1233                     if ($subfields{$subfieldcode}) {
1234                         my $subfieldlist=$subfields{$subfieldcode};
1235                         my @subfieldlist=@$subfieldlist;
1236                         if ($#subfieldlist>=0) {
1237 #                       print "$tag Adding to array $subfieldcode -- $subfield<br>\n";
1238                             push (@subfieldlist, $subfield);
1239                         } else {
1240 #                       print "$tag Arraying $subfieldcode -- $subfield<br>\n";
1241                             @subfieldlist=($subfields{$subfieldcode}, $subfield);
1242                         }
1243                         $subfields{$subfieldcode}=\@subfieldlist;
1244                     } else {
1245                         $subfields{$subfieldcode}=$subfield;
1246                     }
1247                 }
1248                 $field{'subfields'}=\%subfields;
1249             }
1250             push (@record, \%field);
1251         }
1252         push (@records, \@record);
1253         $counter++;
1254     }
1255     return @records;
1256 }
1257
1258 #---------------
1259 # Create an HTML option list for a <SELECT> form tag by using
1260 #    values from a DB file
1261 sub GetKeyTableSelectOptions {
1262         # inputs
1263         my (
1264                 $dbh,           # DBI handle
1265                 $tablename,     # name of table containing list of choices
1266                 $keyfieldname,  # column name of code to use in option list
1267                 $descfieldname, # column name of descriptive field
1268                 $showkey,       # flag to show key in description
1269         )=@_;
1270         my $selectclause;       # return value
1271
1272         my (
1273                 $sth, $query, 
1274                 $key, $desc, $orderfieldname,
1275         );
1276         my $debug=0;
1277
1278         if ( $showkey ) {
1279                 $orderfieldname=$keyfieldname;
1280         } else {
1281                 $orderfieldname=$descfieldname;
1282         }
1283         $query= "select $keyfieldname,$descfieldname
1284                 from $tablename
1285                 order by $orderfieldname ";
1286         print "<PRE>Query=$query </PRE>\n" if $debug; 
1287         $sth=$dbh->prepare($query);
1288         $sth->execute;
1289         while ( ($key, $desc) = $sth->fetchrow) {
1290             if ($showkey) { $desc="$key - $desc"; }
1291             $selectclause.="<option value='$key'>$desc\n";
1292             print "<PRE>Sel=$selectclause </PRE>\n" if $debug; 
1293         }
1294         return $selectclause;
1295 } # sub GetKeyTableSelectOptions