Add item using newitem() from C4::Acquisitions.pm
[koha_fer] / 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     use strict;
399     my $error;
400     my $barcode=$input->param('barcode');
401     my $replacementprice=($input->param('replacementprice') || 0);
402
403     my $sth=$dbh->prepare("select barcode 
404         from items 
405         where barcode=?");
406     $sth->execute($barcode);
407     if ($sth->rows) {
408         print "<font color=red>Barcode '$barcode' has already been assigned.</font><p>\n";
409     } else {
410            # Insert new item into database
411            $error=&newitems(
412                 { biblionumber=> $input->param('biblionumber'),
413                   biblioitemnumber=> $input->param('biblioitemnumber'),
414                   itemnotes=> $input->param('notes'),
415                   homebranch=> $input->param('homebranch'),
416                   replacementprice=> $replacementprice,
417                 },
418                 $barcode
419             );
420             if ( $error ) {
421                 print "<font color=red>Error: $error </font><p>\n";
422             } # if error
423     } # if barcode exists
424 }
425
426
427 my $menu = $input->param('menu');
428 if ($file) {
429     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
430     my $qisbn=$input->param('isbn');
431     my $qissn=$input->param('issn');
432     my $qlccn=$input->param('lccn');
433     my $qcontrolnumber=$input->param('controlnumber');
434     if ($qisbn || $qissn || $qlccn || $qcontrolnumber) {
435         print "<a href=$ENV{'SCRIPT_NAME'}>New File</a><hr>\n";
436         #open (F, "$file");
437         #my $data=<F>;
438         my $data;
439         if ($file=~/Z-(\d+)/) {
440             my $id=$1;
441             my $resultsid=$input->param('resultsid');
442             my $sth=$dbh->prepare("select results from z3950results where id=$resultsid");
443             $sth->execute;
444             ($data) = $sth->fetchrow;
445         } else {
446             my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file");
447             $sth->execute;
448             ($data) = $sth->fetchrow;
449         }
450
451         $splitchar=chr(29);
452         my @records;
453         foreach $record (split(/$splitchar/, $data)) {
454             my $marctext="<table border=0 cellspacing=0>\n";
455             $marctext.="<tr><th colspan=3 bgcolor=black><font color=white>MARC RECORD</font></th></tr>\n";
456             $leader=substr($record,0,24);
457             $marctext.="<tr><td>Leader:</td><td colspan=2>$leader</td></tr>\n";
458             $record=substr($record,24);
459             $splitchar2=chr(30);
460             my $directory=0;
461             my $tagcounter=0;
462             my %tag;
463             my @record;
464             foreach $field (split(/$splitchar2/, $record)) {
465                 my %field;
466                 ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
467                 unless ($directory) {
468                     $directory=$field;
469                     my $itemcounter=1;
470                     $counter=0;
471                     while ($item=substr($directory,0,12)) {
472                         $tag=substr($directory,0,3);
473                         $length=substr($directory,3,4);
474                         $start=substr($directory,7,6);
475                         $directory=substr($directory,12);
476                         $tag{$counter}=$tag;
477                         $counter++;
478                     }
479                     $directory=1;
480                     next;
481                 }
482                 $tag=$tag{$tagcounter};
483                 $tagcounter++;
484                 $field{'tag'}=$tag;
485                 $marctext.="<tr><td bgcolor=$color valign=top>$tagtext{$tag}</td><td bgcolor=$color valign=top>$tag</td>";
486                 $splitchar3=chr(31);
487                 my @subfields=split(/$splitchar3/, $field);
488                 $indicator=$subfields[0];
489                 $field{'indicator'}=$indicator;
490                 my $firstline=1;
491                 if ($#subfields==0) {
492                     $marctext.="<td bgcolor=$color valign=top>$indicator</td></tr>";
493                 } else {
494                     my %subfields;
495                     $marctext.="<td bgcolor=$color valign=top><table border=0 cellspacing=0>\n";
496                     my $color2=$color;
497                     for ($i=1; $i<=$#subfields; $i++) {
498                         ($color2 eq $lc1) ? ($color2=$lc2) : ($color2=$lc1);
499                         my $text=$subfields[$i];
500                         my $subfieldcode=substr($text,0,1);
501                         my $subfield=substr($text,1);
502                         $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";
503                         if ($subfields{$subfieldcode}) {
504                             my $subfieldlist=$subfields{$subfieldcode};
505                             my @subfieldlist=@$subfieldlist;
506                             if ($#subfieldlist>=0) {
507                                 push (@subfieldlist, $subfield);
508                             } else {
509                                 @subfieldlist=($subfields{$subfieldcode}, $subfield);
510                             }
511                             $subfields{$subfieldcode}=\@subfieldlist;
512                         } else {
513                             $subfields{$subfieldcode}=$subfield;
514                         }
515                     }
516                     $marctext.="</table></td></tr>\n";
517                     $field{'subfields'}=\%subfields;
518                 }
519                 push (@record, \%field);
520             }
521             $marctext.="</table>\n";
522             $marctext{\@record}=$marctext;
523             $marc{\@record}=$record;
524             push (@records, \@record);
525             $counter++;
526         }
527 RECORD:
528         foreach $record (@records) {
529             my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $additionalauthors, $illustrator, $copyrightdate, $seriestitle);
530             my $marctext=$marctext{$record};
531             my $marc=$marc{$record};
532             foreach $field (@$record) {
533                 if ($field->{'tag'} eq '001') {
534                     $controlnumber=$field->{'indicator'};
535                 }
536                 if ($field->{'tag'} eq '010') {
537                     $lccn=$field->{'subfields'}->{'a'};
538                     $lccn=~s/^\s*//;
539                     ($lccn) = (split(/\s+/, $lccn))[0];
540                 }
541                 if ($field->{'tag'} eq '015') {
542                     $lccn=$field->{'subfields'}->{'a'};
543                     $lccn=~s/^\s*//;
544                     $lccn=~s/^C//;
545                     ($lccn) = (split(/\s+/, $lccn))[0];
546                 }
547                 if ($field->{'tag'} eq '020') {
548                     $isbn=$field->{'subfields'}->{'a'};
549                     ($isbn=~/^ARRAY/) && ($isbn=$$isbn[0]);
550                     $isbn=~s/[^\d]*//g;
551                 }
552                 if ($field->{'tag'} eq '022') {
553                     $issn=$field->{'subfields'}->{'a'};
554                     $issn=~s/^\s*//;
555                     ($issn) = (split(/\s+/, $issn))[0];
556                 }
557                 if ($field->{'tag'} eq '082') {
558                     $dewey=$field->{'subfields'}->{'a'};
559                     $dewey=~s/\///g;
560                     if (@$dewey) {
561                         $dewey=$$dewey[0];
562                     }
563                     #$dewey=~s/\///g;
564                 }
565                 if ($field->{'tag'} eq '100') {
566                     $author=$field->{'subfields'}->{'a'};
567                 }
568                 if ($field->{'tag'} eq '245') {
569                     $title=$field->{'subfields'}->{'a'};
570                     $title=~s/ \/$//;
571                     $subtitle=$field->{'subfields'}->{'b'};
572                     $subtitle=~s/ \/$//;
573                 }
574                 if ($field->{'tag'} eq '260') {
575                     $place=$field->{'subfields'}->{'a'};
576                     if (@$place) {
577                         $place=$$place[0];
578                     }
579                     $place=~s/\s*:$//g;
580                     $publisher=$field->{'subfields'}->{'b'};
581                     if (@$publisher) {
582                         $publisher=$$publisher[0];
583                     }
584                     $publisher=~s/\s*:$//g;
585                     $publicationyear=$field->{'subfields'}->{'c'};
586                     if ($publicationyear=~/c(\d\d\d\d)/) {
587                         $copyrightdate=$1;
588                     }
589                     if ($publicationyear=~/[^c](\d\d\d\d)/) {
590                         $publicationyear=$1;
591                     } elsif ($copyrightdate) {
592                         $publicationyear=$copyrightdate;
593                     } else {
594                         $publicationyear=~/(\d\d\d\d)/;
595                         $publicationyear=$1;
596                     }
597                 }
598                 if ($field->{'tag'} eq '300') {
599                     $pages=$field->{'subfields'}->{'a'};
600                     $pages=~s/ \;$//;
601                     $size=$field->{'subfields'}->{'c'};
602                     $pages=~s/\s*:$//g;
603                     $size=~s/\s*:$//g;
604                 }
605                 if ($field->{'tag'} eq '362') {
606                     if ($field->{'subfields'}->{'a'}=~/(\d+).*(\d+)/) {
607                         $volume=$1;
608                         $number=$2;
609                     }
610                 }
611                 if ($field->{'tag'} eq '440') {
612                     $seriestitle=$field->{'subfields'}->{'a'};
613                     if ($field->{'subfields'}->{'v'}=~/(\d+).*(\d+)/) {
614                         $volume=$1;
615                         $number=$2;
616                     }
617                 }
618                 if ($field->{'tag'} eq '700') {
619                     my $name=$field->{'subfields'}->{'a'};
620                     if ($field->{'subfields'}->{'c'}=~/ill/) {
621                         $additionalauthors.="$name\n";
622                     } else {
623                         $illustrator=$name;
624                     }
625                 }
626                 if ($field->{'tag'} =~/^5/) {
627                     $note.="$field->{'subfields'}->{'a'}\n";
628                 }
629                 if ($field->{'tag'} =~/65\d/) {
630                     my $subject=$field->{'subfields'}->{'a'};
631                     $subject=~s/\.$//;
632                     if ($gensubdivision=$field->{'subfields'}->{'x'}) {
633                         my @sub=@$gensubdivision;
634                         if ($#sub>=0) {
635                             foreach $s (@sub) {
636                                 $s=~s/\.$//;
637                                 $subject.=" -- $s";
638                             }
639                         } else {
640                             $gensubdivision=~s/\.$//;
641                             $subject.=" -- $gensubdivision";
642                         }
643                     }
644                     if ($chronsubdivision=$field->{'subfields'}->{'y'}) {
645                         my @sub=@$chronsubdivision;
646                         if ($#sub>=0) {
647                             foreach $s (@sub) {
648                                 $s=~s/\.$//;
649                                 $subject.=" -- $s";
650                             }
651                         } else {
652                             $chronsubdivision=~s/\.$//;
653                             $subject.=" -- $chronsubdivision";
654                         }
655                     }
656                     if ($geosubdivision=$field->{'subfields'}->{'z'}) {
657                         my @sub=@$geosubdivision;
658                         if ($#sub>=0) {
659                             foreach $s (@sub) {
660                                 $s=~s/\.$//;
661                                 $subject.=" -- $s";
662                             }
663                         } else {
664                             $geosubdivision=~s/\.$//;
665                             $subject.=" -- $geosubdivision";
666                         }
667                     }
668                     push @subjects, $subject;
669                 }
670             }
671             $titleinput=$input->textfield(-name=>'title', -default=>$title, -size=>40);
672             $marcinput=$input->hidden(-name=>'marc', -default=>$marc);
673             $subtitleinput=$input->textfield(-name=>'subtitle', -default=>$subtitle, -size=>40);
674             $authorinput=$input->textfield(-name=>'author', -default=>$author);
675             $illustratorinput=$input->textfield(-name=>'illustrator', -default=>$illustrator);
676             $additionalauthorsinput=$input->textarea(-name=>'additionalauthors', -default=>$additionalauthors, -rows=>4, -cols=>20);
677             my $subject='';
678             foreach (@subjects) {
679                 $subject.="$_\n";
680             }
681             $subjectinput=$input->textarea(-name=>'subject', -default=>$subject, -rows=>4, -cols=>40);
682             $noteinput=$input->textarea(-name=>'note', -default=>$note, -rows=>4, -cols=>40, -wrap=>'physical');
683             $copyrightinput=$input->textfield(-name=>'copyrightdate', -default=>$copyrightdate);
684             $seriestitleinput=$input->textfield(-name=>'seriestitle', -default=>$seriestitle);
685             $volumeinput=$input->textfield(-name=>'volume', -default=>$volume);
686             $volumedateinput=$input->textfield(-name=>'volumedate', -default=>$volumedate);
687             $volumeddescinput=$input->textfield(-name=>'volumeddesc', -default=>$volumeddesc);
688             $numberinput=$input->textfield(-name=>'number', -default=>$number);
689             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
690             $issninput=$input->textfield(-name=>'issn', -default=>$issn);
691             $lccninput=$input->textfield(-name=>'lccn', -default=>$lccn);
692             $isbninput=$input->textfield(-name=>'isbn', -default=>$isbn);
693             $deweyinput=$input->textfield(-name=>'dewey', -default=>$dewey);
694             $cleanauthor=$author;
695             $cleanauthor=~s/[^A-Za-z]//g;
696             $subclassinput=$input->textfield(-name=>'subclass', -default=>uc(substr($cleanauthor,0,3)));
697             $publisherinput=$input->textfield(-name=>'publishercode', -default=>$publisher);
698             $pubyearinput=$input->textfield(-name=>'publicationyear', -default=>$publicationyear);
699             $placeinput=$input->textfield(-name=>'place', -default=>$place);
700             $pagesinput=$input->textfield(-name=>'pages', -default=>$pages);
701             $sizeinput=$input->textfield(-name=>'size', -default=>$size);
702             $fileinput=$input->hidden(-name=>'file', -default=>$file);
703             $origisbn=$input->hidden(-name=>'origisbn', -default=>$isbn);
704             $origissn=$input->hidden(-name=>'origissn', -default=>$issn);
705             $origlccn=$input->hidden(-name=>'origlccn', -default=>$lccn);
706             $origcontrolnumber=$input->hidden(-name=>'origcontrolnumber', -default=>$controlnumber);
707
708             #print "<PRE>getting itemtypeselect</PRE>\n";
709             $itemtypeselect=&GetKeyTableSelectOptions(
710                 $dbh, 'itemtypes', 'itemtype', 'description', 1);
711             #print "<PRE>it=$itemtypeselect</PRE>\n";
712
713             ($qissn) || ($qissn='NIL');
714             ($qlccn) || ($qlccn='NIL');
715             ($qisbn) || ($qisbn='NIL');
716             ($qcontrolnumber) || ($qcontrolnumber='NIL');
717             $controlnumber=~s/\s+//g;
718
719             unless (($isbn eq $qisbn) || ($issn eq $qissn) || ($lccn eq $qlccn) || ($controlnumber eq $qcontrolnumber)) {
720                 #print "<PRE>Skip record $isbn $issn $lccn </PRE>\n";
721                 next RECORD;
722             }
723
724             print << "EOF";
725             <center>
726             <h1>New Record</h1>
727             Full MARC Record available at bottom
728             <form method=post>
729             <table border=1>
730             <tr><td>Title</td><td>$titleinput</td></tr>
731             <tr><td>Subtitle</td><td>$subtitleinput</td></tr>
732             <tr><td>Author</td><td>$authorinput</td></tr>
733             <tr><td>Additional Authors</td><td>$additionalauthorsinput</td></tr>
734             <tr><td>Illustrator</td><td>$illustratorinput</td></tr>
735             <tr><td>Copyright</td><td>$copyrightinput</td></tr>
736             <tr><td>Series Title</td><td>$seriestitleinput</td></tr>
737             <tr><td>Volume</td><td>$volumeinput</td></tr>
738             <tr><td>Number</td><td>$numberinput</td></tr>
739             <tr><td>Volume Date</td><td>$volumedateinput</td></tr>
740             <tr><td>Volume Description</td><td>$volumeddescinput</td></tr>
741             <tr><td>Subject</td><td>$subjectinput</td></tr>
742             <tr><td>Notes</td><td>$noteinput</td></tr>
743             <tr><td>Item Type</td><td><select name=itemtype>$itemtypeselect</select></td></tr>
744             <tr><td>ISBN</td><td>$isbninput</td></tr>
745             <tr><td>ISSN</td><td>$issninput</td></tr>
746             <tr><td>LCCN</td><td>$lccninput</td></tr>
747             <tr><td>Dewey</td><td>$deweyinput</td></tr>
748             <tr><td>Subclass</td><td>$subclassinput</td></tr>
749             <tr><td>Publication Year</td><td>$pubyearinput</td></tr>
750             <tr><td>Publisher</td><td>$publisherinput</td></tr>
751             <tr><td>Place</td><td>$placeinput</td></tr>
752             <tr><td>Pages</td><td>$pagesinput</td></tr>
753             <tr><td>Size</td><td>$sizeinput</td></tr>
754             </table>
755             <input type=submit>
756             <input type=hidden name=insertnewrecord value=1>
757             $fileinput
758             $marcinput
759             $origisbn
760             $origissn
761             $origlccn
762             $origcontrolnumber
763             </form>
764             $marctext
765 EOF
766         }
767     } else {
768         #open (F, "$file");
769         #my $data=<F>;
770         my $data;
771         my $name;
772         my $z3950=0;
773         if ($file=~/Z-(\d+)/) {
774             print << "EOF";
775 <center>
776 <p>
777 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
778 <p>
779 <table border=0 cellpadding=10 cellspacing=0>
780 <tr><th bgcolor=black><font color=white>Select a Record to Import</font></th></tr>
781 <tr><td bgcolor=#dddddd>
782 EOF
783             my $id=$1;
784             my $sth=$dbh->prepare("select servers from z3950queue where id=$id");
785             $sth->execute;
786             my ($servers) = $sth->fetchrow;
787             my $serverstring;
788             my $starttimer=time();
789             foreach $serverstring (split(/\s+/, $servers)) {
790                 my ($name, $server, $database, $auth) = split(/\//, $serverstring, 4);
791                 if ($name eq 'MAN') {
792                     print "$server/$database<br>\n";
793                 } else {
794                     my $sti=$dbh->prepare("select name from
795                     z3950servers where id=$name");
796                     $sti->execute;
797                     my ($longname)=$sti->fetchrow;
798                     print "<a name=SERVER-$name></a>\n";
799                     if ($longname) {
800                         print "$longname \n";
801                     } else {
802                         print "$server/$database \n";
803                     }
804                 }
805                 my $q_server=$dbh->quote($serverstring);
806                 my $startrecord=$input->param("ST-$name");
807                 ($startrecord) || ($startrecord='0');
808                 my $sti=$dbh->prepare("select numrecords,id,results,startdate,enddate from z3950results where queryid=$id and server=$q_server");
809                 $sti->execute;
810                 ($numrecords,$resultsid,$data,$startdate,$enddate) = $sti->fetchrow;
811                 my $serverplaceholder='';
812                 foreach ($input->param) {
813                     (next) unless (/ST-(.+)/);
814                     my $serverid=$1;
815                     (next) if ($serverid eq $name);
816                     my $place=$input->param("ST-$serverid");
817                     $serverplaceholder.="\&ST-$serverid=$place";
818                 }
819                 if ($numrecords) {
820                     my $previous='';
821                     my $next='';
822                     if ($startrecord>0) {
823                         $previous="<a href=".$ENV{'SCRIPT_NAME'}."?file=Z-$id&menu=z3950$serverplaceholder\&ST-$name=".($startrecord-10)."#SERVER-$name>Previous</a>";
824                     }
825                     my $highest;
826                     $highest=$startrecord+10;
827                     ($highest>$numrecords) && ($highest=$numrecords);
828                     if ($numrecords>$startrecord+10) {
829                         $next="<a href=".$ENV{'SCRIPT_NAME'}."?file=Z-$id&menu=z3950$serverplaceholder\&ST-$name=$highest#SERVER-$name>Next</a>";
830                     }
831                     print "<font size=-1>[Viewing ".($startrecord+1)." to ".$highest." of $numrecords records]  $previous | $next </font><br>\n";
832                 } else {
833                     print "<br>\n";
834                 }
835                 print "<ul>\n";
836                 my $stj=$dbh->prepare("update z3950results set highestseen=".($startrecord+10)." where id=$resultsid");
837                 $stj->execute;
838                 if ($sti->rows == 0) {
839                     print "pending...";
840                 } elsif ($enddate == 0) {
841                     my $now=time();
842                     my $elapsed=$now-$startdate;
843                     my $elapsedtime='';
844                     if ($elapsed>60) {
845                         $elapsedtime=sprintf "%d minutes",($elapsed/60);
846                     } else {
847                         $elapsedtime=sprintf "%d seconds",$elapsed;
848                     }
849                     print "<font color=red>processing... ($elapsedtime)</font>";
850                 } elsif ($numrecords) {
851                     my $splitchar=chr(29);
852                     my @records=split(/$splitchar/, $data);
853                     $data='';
854                     for ($i=$startrecord; $i<$startrecord+10; $i++) {
855                         $data.=$records[$i].$splitchar;
856                     }
857                     @records=parsemarcdata($data);
858                     my $counter=0;
859                     foreach $record (@records) {
860                         $counter++;
861                         #(next) unless ($counter>=$startrecord && $counter<=$startrecord+10);
862                         my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
863                         foreach $field (@$record) {
864                             if ($field->{'tag'} eq '001') {
865                                 $controlnumber=$field->{'indicator'};
866                             }
867                             if ($field->{'tag'} eq '010') {
868                                 $lccn=$field->{'subfields'}->{'a'};
869                                 $lccn=~s/^\s*//;
870                                 ($lccn) = (split(/\s+/, $lccn))[0];
871                             }
872                             if ($field->{'tag'} eq '015') {
873                                 $lccn=$field->{'subfields'}->{'a'};
874                                 $lccn=~s/^\s*//;
875                                 $lccn=~s/^C//;
876                                 ($lccn) = (split(/\s+/, $lccn))[0];
877                             }
878                             if ($field->{'tag'} eq '020') {
879                                 $isbn=$field->{'subfields'}->{'a'};
880                                 ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
881                                 $isbn=~s/[^\d]*//g;
882                             }
883                             if ($field->{'tag'} eq '022') {
884                                 $issn=$field->{'subfields'}->{'a'};
885                                 $issn=~s/^\s*//;
886                                 ($issn) = (split(/\s+/, $issn))[0];
887                             }
888                             if ($field->{'tag'} eq '100') {
889                                 $author=$field->{'subfields'}->{'a'};
890                             }
891                             if ($field->{'tag'} eq '245') {
892                                 $title=$field->{'subfields'}->{'a'};
893                                 $title=~s/ \/$//;
894                                 $subtitle=$field->{'subfields'}->{'b'};
895                                 $subtitle=~s/ \/$//;
896                             }
897                         }
898                         my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
899                         my $q_issn=$dbh->quote((($issn) || ('NIL')));
900                         my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
901                         my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
902                         my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
903                         $sth->execute;
904                         my $donetext='';
905                         if ($sth->rows) {
906                             $donetext="DONE";
907                         }
908                         $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
909                         $sth->execute;
910                         if ($sth->rows) {
911                             $donetext="DONE";
912                         }
913                         ($author) && ($author="by $author");
914                         if ($isbn) {
915                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&isbn=$isbn>$title $subtitle $author</a> $donetext<br>\n";
916                         } elsif ($lccn) {
917                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&lccn=$lccn>$title $subtitle $author</a> $donetext<br>\n";
918                         } elsif ($issn) {
919                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&issn=$issn>$title $subtitle $author</a><br> $donetext\n";
920                         } elsif ($controlnumber) {
921                             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$file&resultsid=$resultsid&controlnumber=$controlnumber>$title $subtitle $author</a><br> $donetext\n";
922                         } else {
923                             print "Error: Contact steve regarding $title by $author<br>\n";
924                         }
925                     }
926                     print "<p>\n";
927                 } else {
928                     print "No records returned.<p>\n";
929                 }
930                 print "</ul>\n";
931             }
932             my $elapsed=time()-$starttimer;
933             print "<hr>It took $elapsed seconds to process this page.\n";
934         } else {
935             my $sth=$dbh->prepare("select marc,name from uploadedmarc where id=$file");
936             $sth->execute;
937             ($data, $name) = $sth->fetchrow;
938             print << "EOF";
939 <center>
940 <p>
941 <a href=$ENV{'SCRIPT_NAME'}?menu=$menu>Select a New File</a>
942 <p>
943 <table border=0 cellpadding=10 cellspacing=0>
944 <tr><th bgcolor=black><font color=white>Select a Record to Import<br>from $name</font></th></tr>
945 <tr><td bgcolor=#dddddd>
946 EOF
947             
948             my @records=parsemarcdata($data);
949             foreach $record (@records) {
950                 my ($lccn, $isbn, $issn, $dewey, $author, $title, $place, $publisher, $publicationyear, $volume, $number, @subjects, $note, $controlnumber);
951                 foreach $field (@$record) {
952                     if ($field->{'tag'} eq '001') {
953                         $controlnumber=$field->{'indicator'};
954                     }
955                     if ($field->{'tag'} eq '010') {
956                         $lccn=$field->{'subfields'}->{'a'};
957                         $lccn=~s/^\s*//;
958                         ($lccn) = (split(/\s+/, $lccn))[0];
959                     }
960                     if ($field->{'tag'} eq '015') {
961                         $lccn=$field->{'subfields'}->{'a'};
962                         $lccn=~s/^\s*//;
963                         $lccn=~s/^C//;
964                         ($lccn) = (split(/\s+/, $lccn))[0];
965                     }
966                     if ($field->{'tag'} eq '020') {
967                         $isbn=$field->{'subfields'}->{'a'};
968                         ($isbn=~/ARRAY/) && ($isbn=$$isbn[0]);
969                         $isbn=~s/[^\d]*//g;
970                     }
971                     if ($field->{'tag'} eq '022') {
972                         $issn=$field->{'subfields'}->{'a'};
973                         $issn=~s/^\s*//;
974                         ($issn) = (split(/\s+/, $issn))[0];
975                     }
976                     if ($field->{'tag'} eq '100') {
977                         $author=$field->{'subfields'}->{'a'};
978                     }
979                     if ($field->{'tag'} eq '245') {
980                         $title=$field->{'subfields'}->{'a'};
981                         $title=~s/ \/$//;
982                         $subtitle=$field->{'subfields'}->{'b'};
983                         $subtitle=~s/ \/$//;
984                     }
985                 }
986                 my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
987                 my $q_issn=$dbh->quote((($issn) || ('NIL')));
988                 my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
989                 my $q_controlnumber=$dbh->quote((($controlnumber) || ('NIL')));
990                 my $sth=$dbh->prepare("select * from marcrecorddone where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn or controlnumber=$q_controlnumber");
991                 $sth->execute;
992                 my $donetext='';
993                 if ($sth->rows) {
994                     $donetext="DONE";
995                 }
996                 $sth=$dbh->prepare("select * from biblioitems where isbn=$q_isbn or issn=$q_issn or lccn=$q_lccn");
997                 $sth->execute;
998                 if ($sth->rows) {
999                     $donetext="DONE";
1000                 }
1001                 ($author) && ($author="by $author");
1002                 if ($isbn) {
1003                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&isbn=$isbn>$title$subtitle $author</a> $donetext<br>\n";
1004                 } elsif ($lccn) {
1005                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&lccn=$lccn>$title$subtitle $author</a> $donetext<br>\n";
1006                 } elsif ($issn) {
1007                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&issn=$issn>$title$subtitle $author</a><br> $donetext\n";
1008                 } elsif ($controlnumber) {
1009                     print "<a href=$ENV{'SCRIPT_NAME'}?file=$file&controlnumber=$controlnumber>$title by $author</a><br> $donetext\n";
1010                 } else {
1011                     print "Error: Contact steve regarding $title by $author<br>\n";
1012                 }
1013             }
1014         }
1015         print "</td></tr></table>\n";
1016     }
1017 } else {
1018
1019 SWITCH:
1020     {
1021         if ($menu eq 'z3950') { z3950(); last SWITCH; }
1022         if ($menu eq 'uploadmarc') { uploadmarc(); last SWITCH; }
1023         if ($menu eq 'manual') { manual(); last SWITCH; }
1024         mainmenu();
1025     }
1026
1027 }
1028
1029
1030 sub z3950 {
1031     my $sth=$dbh->prepare("select id,term,type,done,numrecords,length(results),startdate,enddate,servers from z3950queue order by id desc limit 20");
1032     $sth->execute;
1033     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
1034     print "<table border=0><tr><td valign=top>\n";
1035     print "<h2>Results of Z39.50 searches</h2>\n";
1036     print "<a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Refresh</a><br>\n<ul>\n";
1037     while (my ($id, $term, $type, $done, $numrecords, $length, $startdate, $enddate, $servers) = $sth->fetchrow) {
1038         $type=uc($type);
1039         $term=~s/</&lt;/g;
1040         $term=~s/>/&gt;/g;
1041         my $sti=$dbh->prepare("select id,server,startdate,enddate,numrecords from z3950results where queryid=$id");
1042         $sti->execute;
1043         if ($sti->rows) {
1044             my $processing=0;
1045             my $realenddate=0;
1046             my $totalrecords=0;
1047             while (my ($r_id,$r_server,$r_startdate,$r_enddate,$r_numrecords) = $sti->fetchrow) {
1048                 if ($r_enddate==0) {
1049                     $processing=1;
1050                 } else {
1051                     if ($r_enddate>$realenddate) {
1052                         $realenddate=$r_enddate;
1053                     }
1054                 }
1055
1056                 $totalrecords+=$r_numrecords;
1057             }
1058             if ($processing) {
1059                 my $elapsed=time()-$startdate;
1060                 my $elapsedtime='';
1061                 if ($elapsed>60) {
1062                     $elapsedtime=sprintf "%d minutes",($elapsed/60);
1063                 } else {
1064                     $elapsedtime=sprintf "%d seconds",$elapsed;
1065                 }
1066                 if ($totalrecords) {
1067                     $totalrecords="$totalrecords found.";
1068                 } else {
1069                     $totalrecords='';
1070                 }
1071                 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";
1072             } else {
1073                 my $elapsed=$realenddate-$startdate;
1074                 my $elapsedtime='';
1075                 if ($elapsed>60) {
1076                     $elapsedtime=sprintf "%d minutes",($elapsed/60);
1077                 } else {
1078                     $elapsedtime=sprintf "%d seconds",$elapsed;
1079                 }
1080                 if ($totalrecords) {
1081                     $totalrecords="$totalrecords found.";
1082                 } else {
1083                     $totalrecords='';
1084                 }
1085                 print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Done. $totalrecords ($elapsedtime)</font><br>\n";
1086             }
1087         } else {
1088             print "<li><a href=$ENV{'SCRIPT_NAME'}?file=Z-$id&menu=$menu>$type=$term</a> <font size=-1>Pending</font><br>\n";
1089         }
1090     }
1091     print "</ul>\n";
1092     print "</td><td valign=top width=30%>\n";
1093     my $sth=$dbh->prepare("select id,name,checked from z3950servers order by rank");
1094     $sth->execute;
1095     my $serverlist='';
1096     while (my ($id, $name, $checked) = $sth->fetchrow) {
1097         ($checked) ? ($checked='checked') : ($checked='');
1098         $serverlist.="<input type=checkbox name=S-$id $checked> $name<br>\n";
1099     }
1100     $serverlist.="<input type=checkbox name=S-MAN> <input name=manualz3950server size=25 value=otherserver:210/DATABASE>\n";
1101     
1102     my $rand=rand(1000000000);
1103 print << "EOF";
1104     <form action=$ENV{'SCRIPT_NAME'} method=GET>
1105     <input type=hidden name=z3950queue value=1>
1106     <input type=hidden name=menu value=$menu>
1107     <p>
1108     <input type=hidden name=test value=testvalue>
1109     <input type=hidden name=rand value=$rand>
1110     <table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb colspan=2>Search for MARC records</th></tr>
1111     <tr><td>Query Term</td><td><input name=query></td></tr>
1112     <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>
1113     <tr><td colspan=2>
1114     $serverlist
1115     </td></tr>
1116     <tr><td colspan=2 align=center>
1117     <input type=submit>
1118     </td></tr>
1119     </table>
1120
1121     </form>
1122 EOF
1123 print "</td></tr></table>\n";
1124 }
1125
1126 sub uploadmarc {
1127     print "<a href=$ENV{'SCRIPT_NAME'}>Main Menu</a><hr>\n";
1128     my $sth=$dbh->prepare("select id,name from uploadedmarc");
1129     $sth->execute;
1130     print "<h2>Select a set of MARC records</h2>\n<ul>";
1131     while (my ($id, $name) = $sth->fetchrow) {
1132         print "<li><a href=$ENV{'SCRIPT_NAME'}?file=$id&menu=$menu>$name</a><br>\n";
1133     }
1134     print "</ul>\n";
1135     print "<p>\n";
1136     print "<table border=1 bgcolor=#dddddd><tr><th bgcolor=#bbbbbb
1137     colspan=2>Upload a set of MARC records</th></tr>\n";
1138     print "<tr><td>Upload a set of MARC records:</td><td>";
1139     print $input->start_multipart_form();
1140     print $input->filefield('uploadmarc');
1141     print << "EOF";
1142     </td></tr>
1143     <tr><td>
1144     <input type=hidden name=menu value=$menu>
1145     Name this set of MARC records:</td><td><input type=text
1146     name=name></td></tr>
1147     <tr><td colspan=2 align=center>
1148     <input type=submit>
1149     </td></tr>
1150     </table>
1151     </form>
1152 EOF
1153 }
1154
1155 sub manual {
1156 }
1157
1158
1159 sub mainmenu {
1160     print << "EOF";
1161 <h1>Main Menu</h1>
1162 <ul>
1163 <li><a href=$ENV{'SCRIPT_NAME'}?menu=z3950>Z39.50 Search</a>
1164 <li><a href=$ENV{'SCRIPT_NAME'}?menu=uploadmarc>Upload MARC Records</a>
1165 </ul>
1166 EOF
1167 }
1168
1169 sub skip {
1170
1171     #opendir(D, "/home/$userid/");
1172     #my @dirlist=readdir D;
1173     #foreach $file (@dirlist) {
1174 #       (next) if ($file=~/^\./);
1175 #       (next) if ($file=~/^nsmail$/);
1176 #       (next) if ($file=~/^public_html$/);
1177 #       ($file=~/\.mrc/) || ($filelist.="$file<br>\n");
1178 #       (next) unless ($file=~/\.mrc$/);
1179 #       $file=~s/ /\%20/g;
1180 #       print "<a href=$ENV{'SCRIPT_NAME'}?file=/home/$userid/$file>$file</a><br>\n";
1181 #    }
1182
1183
1184     #<form action=$ENV{'SCRIPT_NAME'} method=POST enctype=multipart/form-data>
1185
1186 }
1187 print endmenu();
1188 print endpage();
1189
1190 sub parsemarcdata {
1191     my $data=shift;
1192     my $splitchar=chr(29);
1193     my @records;
1194     my $record;
1195     foreach $record (split(/$splitchar/, $data)) {
1196         my $leader=substr($record,0,24);
1197         #print "<tr><td>Leader:</td><td>$leader</td></tr>\n";
1198         $record=substr($record,24);
1199         my $splitchar2=chr(30);
1200         my $directory=0;
1201         my $tagcounter=0;
1202         my %tag;
1203         my @record;
1204         my $field;
1205         foreach $field (split(/$splitchar2/, $record)) {
1206             my %field;
1207             ($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
1208             unless ($directory) {
1209                 $directory=$field;
1210                 my $itemcounter=1;
1211                 $counter=0;
1212                 while ($item=substr($directory,0,12)) {
1213                     $tag=substr($directory,0,3);
1214                     $length=substr($directory,3,4);
1215                     $start=substr($directory,7,6);
1216                     $directory=substr($directory,12);
1217                     $tag{$counter}=$tag;
1218                     $counter++;
1219                 }
1220                 $directory=1;
1221                 next;
1222             }
1223             $tag=$tag{$tagcounter};
1224             $tagcounter++;
1225             $field{'tag'}=$tag;
1226             $splitchar3=chr(31);
1227             my @subfields=split(/$splitchar3/, $field);
1228             $indicator=$subfields[0];
1229             $field{'indicator'}=$indicator;
1230             my $firstline=1;
1231             unless ($#subfields==0) {
1232                 my %subfields;
1233                 for ($i=1; $i<=$#subfields; $i++) {
1234                     my $text=$subfields[$i];
1235                     my $subfieldcode=substr($text,0,1);
1236                     my $subfield=substr($text,1);
1237                     if ($subfields{$subfieldcode}) {
1238                         my $subfieldlist=$subfields{$subfieldcode};
1239                         my @subfieldlist=@$subfieldlist;
1240                         if ($#subfieldlist>=0) {
1241 #                       print "$tag Adding to array $subfieldcode -- $subfield<br>\n";
1242                             push (@subfieldlist, $subfield);
1243                         } else {
1244 #                       print "$tag Arraying $subfieldcode -- $subfield<br>\n";
1245                             @subfieldlist=($subfields{$subfieldcode}, $subfield);
1246                         }
1247                         $subfields{$subfieldcode}=\@subfieldlist;
1248                     } else {
1249                         $subfields{$subfieldcode}=$subfield;
1250                     }
1251                 }
1252                 $field{'subfields'}=\%subfields;
1253             }
1254             push (@record, \%field);
1255         }
1256         push (@records, \@record);
1257         $counter++;
1258     }
1259     return @records;
1260 }
1261
1262 #---------------
1263 # Create an HTML option list for a <SELECT> form tag by using
1264 #    values from a DB file
1265 sub GetKeyTableSelectOptions {
1266         # inputs
1267         my (
1268                 $dbh,           # DBI handle
1269                 $tablename,     # name of table containing list of choices
1270                 $keyfieldname,  # column name of code to use in option list
1271                 $descfieldname, # column name of descriptive field
1272                 $showkey,       # flag to show key in description
1273         )=@_;
1274         my $selectclause;       # return value
1275
1276         my (
1277                 $sth, $query, 
1278                 $key, $desc, $orderfieldname,
1279         );
1280         my $debug=0;
1281
1282         if ( $showkey ) {
1283                 $orderfieldname=$keyfieldname;
1284         } else {
1285                 $orderfieldname=$descfieldname;
1286         }
1287         $query= "select $keyfieldname,$descfieldname
1288                 from $tablename
1289                 order by $orderfieldname ";
1290         print "<PRE>Query=$query </PRE>\n" if $debug; 
1291         $sth=$dbh->prepare($query);
1292         $sth->execute;
1293         while ( ($key, $desc) = $sth->fetchrow) {
1294             if ($showkey) { $desc="$key - $desc"; }
1295             $selectclause.="<option value='$key'>$desc\n";
1296             print "<PRE>Sel=$selectclause </PRE>\n" if $debug; 
1297         }
1298         return $selectclause;
1299 } # sub GetKeyTableSelectOptions