#!/usr/bin/perl
my $lc1='#dddddd';
my $lc2='#ddaaaa';
use C4::Database;
use CGI;
use DBI;
#use strict;
use C4::Acquisitions;
use C4::Output;
my $dbh=C4Connect;
my $userid=$ENV{'REMOTE_USER'};
%tagtext = (
'001' => 'Control number',
'003' => 'Control number identifier',
'005' => 'Date and time of latest transaction',
'006' => 'Fixed-length data elements -- additional material characteristics',
'007' => 'Physical description fixed field',
'008' => 'Fixed length data elements',
'010' => 'LCCN',
'015' => 'LCCN Cdn',
'020' => 'ISBN',
'022' => 'ISSN',
'037' => 'Source of acquisition',
'040' => 'Cataloging source',
'041' => 'Language code',
'043' => 'Geographic area code',
'050' => 'Library of Congress call number',
'060' => 'National Library of Medicine call number',
'082' => 'Dewey decimal call number',
'100' => 'Main entry -- Personal name',
'110' => 'Main entry -- Corporate name',
'130' => 'Main entry -- Uniform title',
'240' => 'Uniform title',
'245' => 'Title statement',
'246' => 'Varying form of title',
'250' => 'Edition statement',
'256' => 'Computer file characteristics',
'260' => 'Publication, distribution, etc.',
'263' => 'Projected publication date',
'300' => 'Physical description',
'306' => 'Playing time',
'440' => 'Series statement / Added entry -- Title',
'490' => 'Series statement',
'500' => 'General note',
'504' => 'Bibliography, etc. note',
'505' => 'Formatted contents note',
'508' => 'Creation/production credits note',
'510' => 'Citation/references note',
'511' => 'Participant or performer note',
'520' => 'Summary, etc. note',
'521' => 'Target audience note (ie age)',
'530' => 'Additional physical form available note',
'538' => 'System details note',
'586' => 'Awards note',
'600' => 'Subject added entry -- Personal name',
'610' => 'Subject added entry -- Corporate name',
'650' => 'Subject added entry -- Topical term',
'651' => 'Subject added entry -- Geographic name',
'656' => 'Index term -- Occupation',
'700' => 'Added entry -- Personal name',
'710' => 'Added entry -- Corporate name',
'730' => 'Added entry -- Uniform title',
'740' => 'Added entry -- Uncontrolled related/analytical title',
'800' => 'Series added entry -- Personal name',
'830' => 'Series added entry -- Uniform title',
'852' => 'Location',
'856' => 'Electronic location and access',
);
my $input = new CGI;
my $dbh=C4Connect;
print $input->header;
print startpage();
print startmenu('acquisitions');
my $file=$input->param('file');
if ($input->param('z3950queue')) {
my $query=$input->param('query');
my $type=$input->param('type');
my @serverlist;
foreach ($input->param) {
if (/S-(.*)/) {
my $server=$1;
if ($server eq 'MAN') {
push @serverlist, "MAN/".$input->param('manualz3950server')."//";
} else {
my $sth=$dbh->prepare("select host,port,db,userid,password from z3950servers where id=$server");
$sth->execute;
my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
push @serverlist, "$server/$host\:$port/$db/$userid/$password";
}
}
}
my $isbnfailed=0;
if ($type eq 'isbn') {
my $q=$query;
$q=~s/[^X\d]//g;
$q=~s/X.//g;
if (length($q)==10) {
my $checksum=substr($q,9,1);
my $isbn=substr($q,0,9);
my $i;
my $c=0;
for ($i=0; $i<9; $i++) {
my $digit=substr($q,$i,1);
$c+=$digit*(10-$i);
}
$c=int(11-($c/11-int($c/11))*11+.1);
($c==10) && ($c='X');
if ($c eq $checksum) {
} else {
print "$query is not a valid ISBN
Number
\n";
$isbnfailed=1;
}
} else {
print "$query is not a valid ISBN
Number
\n";
$isbnfailed=1;
}
}
unless ($isbnfailed) {
my $q_term=$dbh->quote($query);
my $serverlist='';
foreach (@serverlist) {
$serverlist.="$_ ";
}
chop $serverlist;
my $q_serverlist=$dbh->quote($serverlist);
my $rand=$input->param('rand');
my $sth=$dbh->prepare("select identifier from z3950queue where
identifier=$rand");
$sth->execute;
unless ($sth->rows) {
$sth=$dbh->prepare("insert into z3950queue (term,type,servers, identifier) values ($q_term, '$type', $q_serverlist, '$rand')");
$sth->execute;
}
}
}
if (my $data=$input->param('uploadmarc')) {
my $name=$input->param('name');
($name) || ($name=$data);
my $marcrecord='';
if (length($data)>0) {
while (<$data>) {
$marcrecord.=$_;
}
}
my $q_marcrecord=$dbh->quote($marcrecord);
my $q_name=$dbh->quote($name);
my $sth=$dbh->prepare("insert into uploadedmarc (marc,name) values ($q_marcrecord, $q_name)");
$sth->execute;
}
if ($input->param('insertnewrecord')) {
my $isbn=$input->param('isbn');
my $issn=$input->param('issn');
my $lccn=$input->param('lccn');
my $q_origisbn=$dbh->quote($input->param('origisbn'));
my $q_origissn=$dbh->quote($input->param('origissn'));
my $q_origlccn=$dbh->quote($input->param('origlccn'));
my $q_origcontrolnumber=$dbh->quote($input->param('origcontrolnumber'));
my $q_isbn=$dbh->quote((($isbn) || ('NIL')));
my $q_issn=$dbh->quote((($issn) || ('NIL')));
my $q_lccn=$dbh->quote((($lccn) || ('NIL')));
$sth=$dbh->prepare("insert into marcrecorddone values ($q_origisbn, $q_origissn, $q_origlccn, $q_origcontrolnumber)");
$sth->execute;
my $sth=$dbh->prepare("select biblionumber,biblioitemnumber from biblioitems where issn=$q_issn or isbn=$q_isbn or lccn=$q_lccn");
$sth->execute;
my $biblionumber=0;
my $biblioitemnumber=0;
print "
\n";
print "New Record | New File \n";
if ($sth->rows) {
($biblionumber, $biblioitemnumber) = $sth->fetchrow;
my $title=$input->param('title');
print << "EOF";
Record already in database
$title is already in the database with biblionumber $biblionumber and biblioitemnumber $biblioitemnumber
EOF
} else {
my $q_title=$dbh->quote($input->param('title'));
my $q_subtitle=$dbh->quote($input->param('subtitle'));
my $q_author=$dbh->quote($input->param('author'));
my $q_copyrightdate=$dbh->quote($input->param('copyrightdate'));
my $q_seriestitle=$dbh->quote($input->param('seriestitle'));
$sth=$dbh->prepare("select biblionumber from biblio where title=$q_title and author=$q_author and copyrightdate=$q_copyrightdate and seriestitle=$q_seriestitle");
$sth->execute;
if ($sth->rows) {
($biblionumber) = $sth->fetchrow;
} else {
$sth=$dbh->prepare("select max(biblionumber) from biblio");
$sth->execute;
($biblionumber) = $sth->fetchrow;
$biblionumber++;
$sth=$dbh->prepare("insert into biblio (biblionumber, title, author, copyrightdate, seriestitle) values ($biblionumber, $q_title, $q_author, $q_copyrightdate, $q_seriestitle)");
$sth->execute;
$sth=$dbh->prepare("insert into bibliosubtitle (biblionumber,subtitle) values ($biblionumber, $q_subtitle)");
$sth->execute;
}
$sth=$dbh->prepare("select max(biblioitemnumber) from biblioitems");
$sth->execute;
($biblioitemnumber) = $sth->fetchrow;
$biblioitemnumber++;
my $q_isbn=$dbh->quote($isbn);
my $q_issn=$dbh->quote($issn);
my $q_lccn=$dbh->quote($lccn);
my $q_volume=$dbh->quote($input->param('volume'));
my $q_number=$dbh->quote($input->param('number'));
my $q_itemtype=$dbh->quote($input->param('itemtype'));
my $q_dewey=$dbh->quote($input->param('dewey'));
my $q_subclass=$dbh->quote($input->param('subclass'));
my $q_publicationyear=$dbh->quote($input->param('publicationyear'));
my $q_publishercode=$dbh->quote($input->param('publishercode'));
my $q_volumedate=$dbh->quote($input->param('volumedate'));
my $q_volumeddesc=$dbh->quote($input->param('volumeddesc'));
my $q_illus=$dbh->quote($input->param('illustrator'));
my $q_pages=$dbh->quote($input->param('pages'));
my $q_notes=$dbh->quote($input->param('note'));
my $q_size=$dbh->quote($input->param('size'));
my $q_place=$dbh->quote($input->param('place'));
my $q_marc=$dbh->quote($input->param('marc'));
$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)");
$sth->execute;
my $subjectheadings=$input->param('subject');
my $additionalauthors=$input->param('additionalauthors');
my @subjectheadings=split(/\n/,$subjectheadings);
my $subjectheading;
foreach $subjectheading (@subjectheadings) {
# remove any line ending characters (Ctrl-J or M)
$subjectheading=~s/\013//g;
$subjectheading=~s/\010//g;
# convert to upper case
$subjectheading=uc($subjectheading);
chomp ($subjectheading);
while (ord(substr($subjectheading, length($subjectheading)-1, 1))<14) {
chop $subjectheading;
}
# quote value
my $q_subjectheading=$dbh->quote($subjectheading);
$sth=$dbh->prepare("insert into bibliosubject (biblionumber,subject)
values ($biblionumber, $q_subjectheading)");
$sth->execute;
}
my @additionalauthors=split(/\n/,$additionalauthors);
my $additionalauthor;
foreach $additionalauthor (@additionalauthors) {
# remove any line ending characters (Ctrl-L or Ctrl-M)
$additionalauthor=~s/\013//g;
$additionalauthor=~s/\010//g;
# convert to upper case
$additionalauthor=uc($additionalauthor);
# quote value
my $q_additionalauthor=$dbh->quote($additionalauthor);
$sth=$dbh->prepare("insert into additionalauthors (biblionumber,author) values ($biblionumber, $q_additionalauthor)");
$sth->execute;
}
my $title=$input->param('title');
print << "EOF";
Record entered into database
$title has been entered into the database with biblionumber
$biblionumber and biblioitemnumber $biblioitemnumber
EOF
}
my $title=$input->param('title');
$sth=$dbh->prepare("select max(barcode) from items");
$sth->execute;
my ($barcode) = $sth->fetchrow;
$barcode++;
if ($barcode==1) {
$barcode=int(rand()*1000000);
}
print << "EOF";
Add a New Item for $title
EOF
print endmenu();
print endpage();
exit;
}
if ($input->param('newitem')) {
my $barcode=$input->param('barcode');
my $q_barcode=$dbh->quote($barcode);
my $q_notes=$dbh->quote($input->param('notes'));
my $q_homebranch=$dbh->quote($input->param('homebranch'));
my $biblionumber=$input->param('biblionumber');
my $biblioitemnumber=$input->param('biblioitemnumber');
my $replacementprice=($input->param('replacementprice') || 0);
my $sth=$dbh->prepare("select barcode from items where
barcode=$q_barcode");
$sth->execute;
if ($sth->rows) {
print "Barcode '$barcode' has already been assigned.
\n";
} else {
$sth=$dbh->prepare("select max(itemnumber) from items");
$sth->execute;
my ($itemnumber) = $sth->fetchrow;
$itemnumber++;
my @datearr=localtime(time);
my $date=(1900+$datearr[5])."-".($datearr[4]+1)."-".$datearr[3];
$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)");
$sth->execute;
}
}
my $menu = $input->param('menu');
if ($file) {
print "Main Menu
\n";
my $qisbn=$input->param('isbn');
my $qissn=$input->param('issn');
my $qlccn=$input->param('lccn');
my $qcontrolnumber=$input->param('controlnumber');
if ($qisbn || $qissn || $qlccn || $qcontrolnumber) {
print "New File\n";
#open (F, "$file");
#my $data=;
my $data;
if ($file=~/Z-(\d+)/) {
my $id=$1;
my $resultsid=$input->param('resultsid');
my $sth=$dbh->prepare("select results from z3950results where id=$resultsid");
$sth->execute;
($data) = $sth->fetchrow;
} else {
my $sth=$dbh->prepare("select marc from uploadedmarc where id=$file");
$sth->execute;
($data) = $sth->fetchrow;
}
$splitchar=chr(29);
my @records;
foreach $record (split(/$splitchar/, $data)) {
my $marctext="
\n";
$marctext.="
MARC RECORD
\n";
$leader=substr($record,0,24);
$marctext.="
Leader:
$leader
\n";
$record=substr($record,24);
$splitchar2=chr(30);
my $directory=0;
my $tagcounter=0;
my %tag;
my @record;
foreach $field (split(/$splitchar2/, $record)) {
my %field;
($color eq $lc1) ? ($color=$lc2) : ($color=$lc1);
unless ($directory) {
$directory=$field;
my $itemcounter=1;
$counter=0;
while ($item=substr($directory,0,12)) {
$tag=substr($directory,0,3);
$length=substr($directory,3,4);
$start=substr($directory,7,6);
$directory=substr($directory,12);
$tag{$counter}=$tag;
$counter++;
}
$directory=1;
next;
}
$tag=$tag{$tagcounter};
$tagcounter++;
$field{'tag'}=$tag;
$marctext.="
$tagtext{$tag}
$tag
";
$splitchar3=chr(31);
my @subfields=split(/$splitchar3/, $field);
$indicator=$subfields[0];
$field{'indicator'}=$indicator;
my $firstline=1;
if ($#subfields==0) {
$marctext.="
$indicator
";
} else {
my %subfields;
$marctext.="
\n";
my $color2=$color;
for ($i=1; $i<=$#subfields; $i++) {
($color2 eq $lc1) ? ($color2=$lc2) : ($color2=$lc1);
my $text=$subfields[$i];
my $subfieldcode=substr($text,0,1);
my $subfield=substr($text,1);
$marctext.="
$subfieldcode
$subfield
\n";
if ($subfields{$subfieldcode}) {
my $subfieldlist=$subfields{$subfieldcode};
my @subfieldlist=@$subfieldlist;
if ($#subfieldlist>=0) {
push (@subfieldlist, $subfield);
} else {
@subfieldlist=($subfields{$subfieldcode}, $subfield);
}
$subfields{$subfieldcode}=\@subfieldlist;
} else {
$subfields{$subfieldcode}=$subfield;
}
}
$marctext.="