3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use MARC
::File
::USMARC
;
28 use POSIX
qw(strftime);
31 use C4
::Dates qw
/format_date/;
32 use C4
::Log
; # logaction
38 use vars
qw($VERSION @ISA @EXPORT);
44 @ISA = qw( Exporter );
59 &GetBiblioItemByBiblioNumber
60 &GetBiblioFromItemNumber
61 &GetBiblionumberFromItemnumber
80 &GetAuthorisedValueDesc
84 &GetPublisherNameFromIsbn
102 # To link headings in a bib record
103 # to authority records.
105 &LinkBibHeadingsToAuthorities
109 # those functions are exported but should not be used
110 # they are usefull is few circumstances, so are exported.
111 # but don't use them unless you're a core developer ;-)
119 &TransformHtmlToMarc2
122 &PrepareItemrecordDisplay
128 my $servers = C4
::Context
->config('memcached_servers');
130 require Memoize
::Memcached
;
131 import Memoize
::Memcached
qw(memoize_memcached);
134 servers
=> [$servers],
135 key_prefix
=> C4
::Context
->config('memcached_namespace') || 'koha',
137 memoize_memcached
( 'GetMarcStructure', memcached
=> $memcached, expire_time
=> 600 ); #cache for 10 minutes
143 C4::Biblio - cataloging management functions
147 Biblio.pm contains functions for managing storage and editing of bibliographic data within Koha. Most of the functions in this module are used for cataloging records: adding, editing, or removing biblios, biblioitems, or items. Koha's stores bibliographic information in three places:
151 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
153 =item 2. as raw MARC in the Zebra index and storage engine
155 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
159 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
161 Because the data isn't completely normalized there's a chance for information to get out of sync. The design choice to go with a un-normalized schema was driven by performance and stability concerns. However, if this occur, it can be considered as a bug : The API is (or should be) complete & the only entry point for all biblio/items managements.
165 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
167 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
171 Because of this design choice, the process of managing storage and editing is a bit convoluted. Historically, Biblio.pm's grown to an unmanagable size and as a result we have several types of functions currently:
175 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
177 =item 2. _koha_* - low-level internal functions for managing the koha tables
179 =item 3. Marc management function : as the MARC record is stored in biblioitems.marc(xml), some subs dedicated to it's management are in this package. They should be used only internally by Biblio.pm, the only official entry points being AddBiblio, AddItem, ModBiblio, ModItem.
181 =item 4. Zebra functions used to update the Zebra index
183 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
187 The MARC record (in biblioitems.marcxml) contains the complete marc record, including items. It also contains the biblionumber. That is the reason why it is not stored directly by AddBiblio, with all other fields . To save a biblio, we need to :
191 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
193 =item 2. add the biblionumber and biblioitemnumber into the MARC records
195 =item 3. save the marc record
199 When dealing with items, we must :
203 =item 1. save the item in items table, that gives us an itemnumber
205 =item 2. add the itemnumber to the item MARC field
207 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
209 When modifying a biblio or an item, the behaviour is quite similar.
213 =head1 EXPORTED FUNCTIONS
219 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
223 Exported function (core API) for adding a new biblio to koha.
225 The first argument is a C<MARC::Record> object containing the
226 bib to add, while the second argument is the desired MARC
229 This function also accepts a third, optional argument: a hashref
230 to additional options. The only defined option is C<defer_marc_save>,
231 which if present and mapped to a true value, causes C<AddBiblio>
232 to omit the call to save the MARC in C<bibilioitems.marc>
233 and C<biblioitems.marcxml> This option is provided B<only>
234 for the use of scripts such as C<bulkmarcimport.pl> that may need
235 to do some manipulation of the MARC record for item parsing before
236 saving it and which cannot afford the performance hit of saving
237 the MARC record twice. Consequently, do not use that option
238 unless you can guarantee that C<ModBiblioMarc> will be called.
244 my $frameworkcode = shift;
245 my $options = @_ ?
shift : undef;
246 my $defer_marc_save = 0;
247 if ( defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'} ) {
248 $defer_marc_save = 1;
251 my ( $biblionumber, $biblioitemnumber, $error );
252 my $dbh = C4
::Context
->dbh;
254 # transform the data into koha-table style data
255 my $olddata = TransformMarcToKoha
( $dbh, $record, $frameworkcode );
256 ( $biblionumber, $error ) = _koha_add_biblio
( $dbh, $olddata, $frameworkcode );
257 $olddata->{'biblionumber'} = $biblionumber;
258 ( $biblioitemnumber, $error ) = _koha_add_biblioitem
( $dbh, $olddata );
260 _koha_marc_update_bib_ids
( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
262 # update MARC subfield that stores biblioitems.cn_sort
263 _koha_marc_update_biblioitem_cn_sort
( $record, $olddata, $frameworkcode );
266 ModBiblioMarc
( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
268 logaction
( "CATALOGUING", "ADD", $biblionumber, "biblio" ) if C4
::Context
->preference("CataloguingLog");
269 return ( $biblionumber, $biblioitemnumber );
276 ModBiblio( $record,$biblionumber,$frameworkcode);
280 Replace an existing bib record identified by C<$biblionumber>
281 with one supplied by the MARC::Record object C<$record>. The embedded
282 item, biblioitem, and biblionumber fields from the previous
283 version of the bib record replace any such fields of those tags that
284 are present in C<$record>. Consequently, ModBiblio() is not
285 to be used to try to modify item records.
287 C<$frameworkcode> specifies the MARC framework to use
288 when storing the modified bib record; among other things,
289 this controls how MARC fields get mapped to display columns
290 in the C<biblio> and C<biblioitems> tables, as well as
291 which fields are used to store embedded item, biblioitem,
292 and biblionumber data for indexing.
297 my ( $record, $biblionumber, $frameworkcode ) = @_;
298 if ( C4
::Context
->preference("CataloguingLog") ) {
299 my $newrecord = GetMarcBiblio
($biblionumber);
300 logaction
( "CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>" . $newrecord->as_formatted );
303 my $dbh = C4
::Context
->dbh;
305 $frameworkcode = "" unless $frameworkcode;
307 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
308 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField
( "items.itemnumber", $frameworkcode );
309 my $oldRecord = GetMarcBiblio
($biblionumber);
311 # delete any item fields from incoming record to avoid
312 # duplication or incorrect data - use AddItem() or ModItem()
314 foreach my $field ( $record->field($itemtag) ) {
315 $record->delete_field($field);
318 # parse each item, and, for an unknown reason, re-encode each subfield
319 # if you don't do that, the record will have encoding mixed
320 # and the biblio will be re-encoded.
321 # strange, I (Paul P.) searched more than 1 day to understand what happends
322 # but could only solve the problem this way...
323 my @fields = $oldRecord->field($itemtag);
324 foreach my $fielditem (@fields) {
326 foreach ( $fielditem->subfields() ) {
328 $field->add_subfields( Encode
::encode
( 'utf-8', $_->[0] ) => Encode
::encode
( 'utf-8', $_->[1] ) );
330 $field = MARC
::Field
->new( "$itemtag", '', '', Encode
::encode
( 'utf-8', $_->[0] ) => Encode
::encode
( 'utf-8', $_->[1] ) );
333 $record->append_fields($field);
336 # update biblionumber and biblioitemnumber in MARC
337 # FIXME - this is assuming a 1 to 1 relationship between
338 # biblios and biblioitems
339 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
340 $sth->execute($biblionumber);
341 my ($biblioitemnumber) = $sth->fetchrow;
343 _koha_marc_update_bib_ids
( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
345 # load the koha-table data object
346 my $oldbiblio = TransformMarcToKoha
( $dbh, $record, $frameworkcode );
348 # update MARC subfield that stores biblioitems.cn_sort
349 _koha_marc_update_biblioitem_cn_sort
( $record, $oldbiblio, $frameworkcode );
351 # update the MARC record (that now contains biblio and items) with the new record data
352 &ModBiblioMarc
( $record, $biblionumber, $frameworkcode );
354 # modify the other koha tables
355 _koha_modify_biblio
( $dbh, $oldbiblio, $frameworkcode );
356 _koha_modify_biblioitem_nonmarc
( $dbh, $oldbiblio );
360 =head2 ModBiblioframework
362 ModBiblioframework($biblionumber,$frameworkcode);
363 Exported function to modify a biblio framework
367 sub ModBiblioframework
{
368 my ( $biblionumber, $frameworkcode ) = @_;
369 my $dbh = C4
::Context
->dbh;
370 my $sth = $dbh->prepare( "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?" );
371 $sth->execute( $frameworkcode, $biblionumber );
379 my $error = &DelBiblio($dbh,$biblionumber);
380 Exported function (core API) for deleting a biblio in koha.
381 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
382 Also backs it up to deleted* tables
383 Checks to make sure there are not issues on any of the items
385 C<$error> : undef unless an error occurs
392 my ($biblionumber) = @_;
393 my $dbh = C4
::Context
->dbh;
394 my $error; # for error handling
396 # First make sure this biblio has no items attached
397 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
398 $sth->execute($biblionumber);
399 if ( my $itemnumber = $sth->fetchrow ) {
401 # Fix this to use a status the template can understand
402 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
405 return $error if $error;
407 # We delete attached subscriptions
408 my $subscriptions = &C4
::Serials
::GetFullSubscriptionsFromBiblionumber
($biblionumber);
409 foreach my $subscription (@
$subscriptions) {
410 &C4
::Serials
::DelSubscription
( $subscription->{subscriptionid
} );
413 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
414 # for at least 2 reasons :
415 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
416 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
417 # and we would have no way to remove it (except manually in zebra, but I bet it would be very hard to handle the problem)
419 if ( C4
::Context
->preference("NoZebra") ) {
421 # only NoZebra indexing needs to have
422 # the previous version of the record
423 $oldRecord = GetMarcBiblio
($biblionumber);
425 ModZebra
( $biblionumber, "recordDelete", "biblioserver", $oldRecord, undef );
427 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
428 $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
429 $sth->execute($biblionumber);
430 while ( my $biblioitemnumber = $sth->fetchrow ) {
432 # delete this biblioitem
433 $error = _koha_delete_biblioitems
( $dbh, $biblioitemnumber );
434 return $error if $error;
437 # delete biblio from Koha tables and save in deletedbiblio
438 # must do this *after* _koha_delete_biblioitems, otherwise
439 # delete cascade will prevent deletedbiblioitems rows
440 # from being generated by _koha_delete_biblioitems
441 $error = _koha_delete_biblio
( $dbh, $biblionumber );
443 logaction
( "CATALOGUING", "DELETE", $biblionumber, "" ) if C4
::Context
->preference("CataloguingLog");
448 =head2 LinkBibHeadingsToAuthorities
452 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
456 Links bib headings to authority records by checking
457 each authority-controlled field in the C<MARC::Record>
458 object C<$marc>, looking for a matching authority record,
459 and setting the linking subfield $9 to the ID of that
462 If no matching authority exists, or if multiple
463 authorities match, no $9 will be added, and any
464 existing one inthe field will be deleted.
466 Returns the number of heading links changed in the
471 sub LinkBibHeadingsToAuthorities
{
474 my $num_headings_changed = 0;
475 foreach my $field ( $bib->fields() ) {
476 my $heading = C4
::Heading
->new_from_bib_field($field);
477 next unless defined $heading;
480 my $current_link = $field->subfield('9');
482 # look for matching authorities
483 my $authorities = $heading->authorities();
485 # want only one exact match
486 if ( $#{$authorities} == 0 ) {
487 my $authority = MARC
::Record
->new_from_usmarc( $authorities->[0] );
488 my $authid = $authority->field('001')->data();
489 next if defined $current_link and $current_link eq $authid;
491 $field->delete_subfield( code
=> '9' ) if defined $current_link;
492 $field->add_subfields( '9', $authid );
493 $num_headings_changed++;
495 if ( defined $current_link ) {
496 $field->delete_subfield( code
=> '9' );
497 $num_headings_changed++;
502 return $num_headings_changed;
505 =head2 GetRecordValue
509 my $values = GetRecordValue($field, $record, $frameworkcode);
513 Get MARC fields from a keyword defined in fieldmapping table.
518 my ( $field, $record, $frameworkcode ) = @_;
519 my $dbh = C4
::Context
->dbh;
521 my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?');
522 $sth->execute( $frameworkcode, $field );
526 while ( my $row = $sth->fetchrow_hashref ) {
527 foreach my $field ( $record->field( $row->{fieldcode
} ) ) {
528 if ( ( $row->{subfieldcode
} ne "" && $field->subfield( $row->{subfieldcode
} ) ) ) {
529 foreach my $subfield ( $field->subfield( $row->{subfieldcode
} ) ) {
530 push @result, { 'subfield' => $subfield };
533 } elsif ( $row->{subfieldcode
} eq "" ) {
534 push @result, { 'subfield' => $field->as_string() };
542 =head2 SetFieldMapping
546 SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
550 Set a Field to MARC mapping value, if it already exists we don't add a new one.
554 sub SetFieldMapping
{
555 my ( $framework, $field, $fieldcode, $subfieldcode ) = @_;
556 my $dbh = C4
::Context
->dbh;
558 my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
559 $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
560 if ( not $sth->fetchrow_hashref ) {
562 $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
564 $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
568 =head2 DeleteFieldMapping
572 DeleteFieldMapping($id);
576 Delete a field mapping from an $id.
580 sub DeleteFieldMapping
{
582 my $dbh = C4
::Context
->dbh;
584 my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
588 =head2 GetFieldMapping
592 GetFieldMapping($frameworkcode);
596 Get all field mappings for a specified frameworkcode
600 sub GetFieldMapping
{
601 my ($framework) = @_;
602 my $dbh = C4
::Context
->dbh;
604 my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
605 $sth->execute($framework);
608 while ( my $row = $sth->fetchrow_hashref ) {
618 $data = &GetBiblioData($biblionumber);
619 Returns information about the book with the given biblionumber.
620 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
621 the C<biblio> and C<biblioitems> tables in the
623 In addition, C<$data-E<gt>{subject}> is the list of the book's
624 subjects, separated by C<" , "> (space, comma, space).
625 If there are multiple biblioitems with the given biblionumber, only
626 the first one is considered.
634 my $dbh = C4
::Context
->dbh;
636 # my $query = C4::Context->preference('item-level_itypes') ?
637 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
639 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
640 # WHERE biblio.biblionumber = ?
641 # AND biblioitems.biblionumber = biblio.biblionumber
644 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
646 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
647 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
648 WHERE biblio.biblionumber = ?
649 AND biblioitems.biblionumber = biblio.biblionumber ";
651 my $sth = $dbh->prepare($query);
652 $sth->execute($bibnum);
654 $data = $sth->fetchrow_hashref;
658 } # sub GetBiblioData
660 =head2 &GetBiblioItemData
664 $itemdata = &GetBiblioItemData($biblioitemnumber);
666 Looks up the biblioitem with the given biblioitemnumber. Returns a
667 reference-to-hash. The keys are the fields from the C<biblio>,
668 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
669 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
676 sub GetBiblioItemData
{
677 my ($biblioitemnumber) = @_;
678 my $dbh = C4
::Context
->dbh;
679 my $query = "SELECT *,biblioitems.notes AS bnotes
680 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
681 unless ( C4
::Context
->preference('item-level_itypes') ) {
682 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
684 $query .= " WHERE biblioitemnumber = ? ";
685 my $sth = $dbh->prepare($query);
687 $sth->execute($biblioitemnumber);
688 $data = $sth->fetchrow_hashref;
691 } # sub &GetBiblioItemData
693 =head2 GetBiblioItemByBiblioNumber
697 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
703 sub GetBiblioItemByBiblioNumber
{
704 my ($biblionumber) = @_;
705 my $dbh = C4
::Context
->dbh;
706 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
710 $sth->execute($biblionumber);
712 while ( my $data = $sth->fetchrow_hashref ) {
713 push @results, $data;
720 =head2 GetBiblionumberFromItemnumber
728 sub GetBiblionumberFromItemnumber
{
729 my ($itemnumber) = @_;
730 my $dbh = C4
::Context
->dbh;
731 my $sth = $dbh->prepare("Select biblionumber FROM items WHERE itemnumber = ?");
733 $sth->execute($itemnumber);
734 my ($result) = $sth->fetchrow;
738 =head2 GetBiblioFromItemNumber
742 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
744 Looks up the item with the given itemnumber. if undef, try the barcode.
746 C<&itemnodata> returns a reference-to-hash whose keys are the fields
747 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
755 sub GetBiblioFromItemNumber
{
756 my ( $itemnumber, $barcode ) = @_;
757 my $dbh = C4
::Context
->dbh;
760 $sth = $dbh->prepare(
762 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
763 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
764 WHERE items.itemnumber = ?"
766 $sth->execute($itemnumber);
768 $sth = $dbh->prepare(
770 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
771 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
772 WHERE items.barcode = ?"
774 $sth->execute($barcode);
776 my $data = $sth->fetchrow_hashref;
785 $isbd = &GetISBDView($biblionumber);
787 Return the ISBD view which can be included in opac and intranet
794 my ( $biblionumber, $template ) = @_;
795 my $record = GetMarcBiblio
($biblionumber);
796 my $itemtype = &GetFrameworkCode
($biblionumber);
797 my ( $holdingbrtagf, $holdingbrtagsubf ) = &GetMarcFromKohaField
( "items.holdingbranch", $itemtype );
798 my $tagslib = &GetMarcStructure
( 1, $itemtype );
800 my $ISBD = C4
::Context
->preference('ISBD');
805 foreach my $isbdfield ( split( /#/, $bloc ) ) {
807 # $isbdfield= /(.?.?.?)/;
808 $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
809 my $fieldvalue = $1 || 0;
810 my $subfvalue = $2 || "";
812 my $analysestring = $4;
815 # warn "==> $1 / $2 / $3 / $4";
816 # my $fieldvalue=substr($isbdfield,0,3);
817 if ( $fieldvalue > 0 ) {
818 my $hasputtextbefore = 0;
819 my @fieldslist = $record->field($fieldvalue);
820 @fieldslist = sort { $a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf) } @fieldslist if ( $fieldvalue eq $holdingbrtagf );
822 # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
823 # warn "FV : $fieldvalue";
824 if ( $subfvalue ne "" ) {
825 foreach my $field (@fieldslist) {
826 foreach my $subfield ( $field->subfield($subfvalue) ) {
827 my $calculated = $analysestring;
828 my $tag = $field->tag();
831 my $subfieldvalue = GetAuthorisedValueDesc
( $tag, $subfvalue, $subfield, '', $tagslib );
832 my $tagsubf = $tag . $subfvalue;
833 $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
834 if ( $template eq "opac" ) { $calculated =~ s
#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
836 # field builded, store the result
837 if ( $calculated && !$hasputtextbefore ) { # put textbefore if not done
838 $blocres .= $textbefore;
839 $hasputtextbefore = 1;
842 # remove punctuation at start
843 $calculated =~ s/^( |;|:|\.|-)*//g;
844 $blocres .= $calculated;
849 $blocres .= $textafter if $hasputtextbefore;
851 foreach my $field (@fieldslist) {
852 my $calculated = $analysestring;
853 my $tag = $field->tag();
856 my @subf = $field->subfields;
857 for my $i ( 0 .. $#subf ) {
858 my $valuecode = $subf[$i][1];
859 my $subfieldcode = $subf[$i][0];
860 my $subfieldvalue = GetAuthorisedValueDesc
( $tag, $subf[$i][0], $subf[$i][1], '', $tagslib );
861 my $tagsubf = $tag . $subfieldcode;
863 $calculated =~ s
/ # replace all {{}} codes by the value code.
864 \
{\
{$tagsubf\
}\
} # catch the {{actualcode}}
866 $valuecode # replace by the value code
869 $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
870 if ( $template eq "opac" ) { $calculated =~ s
#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
873 # field builded, store the result
874 if ( $calculated && !$hasputtextbefore ) { # put textbefore if not done
875 $blocres .= $textbefore;
876 $hasputtextbefore = 1;
879 # remove punctuation at start
880 $calculated =~ s/^( |;|:|\.|-)*//g;
881 $blocres .= $calculated;
884 $blocres .= $textafter if $hasputtextbefore;
887 $blocres .= $isbdfield;
892 $res =~ s/\{(.*?)\}//g;
894 $res =~ s/\n/<br\/>/g
;
906 ( $count, @results ) = &GetBiblio($biblionumber);
913 my ($biblionumber) = @_;
914 my $dbh = C4
::Context
->dbh;
915 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
918 $sth->execute($biblionumber);
919 while ( my $data = $sth->fetchrow_hashref ) {
920 $results[$count] = $data;
924 return ( $count, @results );
927 =head2 GetBiblioItemInfosOf
931 GetBiblioItemInfosOf(@biblioitemnumbers);
937 sub GetBiblioItemInfosOf
{
938 my @biblioitemnumbers = @_;
941 SELECT biblioitemnumber,
945 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
947 return get_infos_of
( $query, 'biblioitemnumber' );
950 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
952 =head2 GetMarcStructure
956 $res = GetMarcStructure($forlibrarian,$frameworkcode);
958 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
959 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
960 $frameworkcode : the framework code to read
966 # cache for results of GetMarcStructure -- needed
968 our $marc_structure_cache;
970 sub GetMarcStructure
{
971 my ( $forlibrarian, $frameworkcode ) = @_;
972 my $dbh = C4
::Context
->dbh;
973 $frameworkcode = "" unless $frameworkcode;
975 if ( defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode} ) {
976 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
979 # my $sth = $dbh->prepare(
980 # "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
981 # $sth->execute($frameworkcode);
982 # my ($total) = $sth->fetchrow;
983 # $frameworkcode = "" unless ( $total > 0 );
984 my $sth = $dbh->prepare(
985 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
986 FROM marc_tag_structure
987 WHERE frameworkcode=?
990 $sth->execute($frameworkcode);
991 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
993 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
994 $res->{$tag}->{lib
} = ( $forlibrarian or !$libopac ) ?
$liblibrarian : $libopac;
995 $res->{$tag}->{tab
} = "";
996 $res->{$tag}->{mandatory
} = $mandatory;
997 $res->{$tag}->{repeatable
} = $repeatable;
1000 $sth = $dbh->prepare(
1001 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
1002 FROM marc_subfield_structure
1003 WHERE frameworkcode=?
1004 ORDER BY tagfield,tagsubfield
1008 $sth->execute($frameworkcode);
1011 my $authorised_value;
1022 ( $tag, $subfield, $liblibrarian, $libopac, $tab, $mandatory, $repeatable, $authorised_value,
1023 $authtypecode, $value_builder, $kohafield, $seealso, $hidden, $isurl, $link, $defaultvalue
1027 $res->{$tag}->{$subfield}->{lib
} = ( $forlibrarian or !$libopac ) ?
$liblibrarian : $libopac;
1028 $res->{$tag}->{$subfield}->{tab
} = $tab;
1029 $res->{$tag}->{$subfield}->{mandatory
} = $mandatory;
1030 $res->{$tag}->{$subfield}->{repeatable
} = $repeatable;
1031 $res->{$tag}->{$subfield}->{authorised_value
} = $authorised_value;
1032 $res->{$tag}->{$subfield}->{authtypecode
} = $authtypecode;
1033 $res->{$tag}->{$subfield}->{value_builder
} = $value_builder;
1034 $res->{$tag}->{$subfield}->{kohafield
} = $kohafield;
1035 $res->{$tag}->{$subfield}->{seealso
} = $seealso;
1036 $res->{$tag}->{$subfield}->{hidden
} = $hidden;
1037 $res->{$tag}->{$subfield}->{isurl
} = $isurl;
1038 $res->{$tag}->{$subfield}->{'link'} = $link;
1039 $res->{$tag}->{$subfield}->{defaultvalue
} = $defaultvalue;
1042 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
1047 =head2 GetUsedMarcStructure
1049 the same function as GetMarcStructure except it just takes field
1050 in tab 0-9. (used field)
1052 my $results = GetUsedMarcStructure($frameworkcode);
1054 L<$results> is a ref to an array which each case containts a ref
1055 to a hash which each keys is the columns from marc_subfield_structure
1057 L<$frameworkcode> is the framework code.
1061 sub GetUsedMarcStructure
($) {
1062 my $frameworkcode = shift || '';
1065 FROM marc_subfield_structure
1067 AND frameworkcode
= ?
1068 ORDER BY tagfield
, tagsubfield
1070 my $sth = C4
::Context
->dbh->prepare($query);
1071 $sth->execute($frameworkcode);
1072 return $sth->fetchall_arrayref( {} );
1075 =head2 GetMarcFromKohaField
1079 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1080 Returns the MARC fields & subfields mapped to the koha field
1081 for the given frameworkcode
1087 sub GetMarcFromKohaField
{
1088 my ( $kohafield, $frameworkcode ) = @_;
1089 return 0, 0 unless $kohafield and defined $frameworkcode;
1090 my $relations = C4
::Context
->marcfromkohafield;
1091 return ( $relations->{$frameworkcode}->{$kohafield}->[0], $relations->{$frameworkcode}->{$kohafield}->[1] );
1094 =head2 GetMarcBiblio
1098 my $record = GetMarcBiblio($biblionumber);
1102 Returns MARC::Record representing bib identified by
1103 C<$biblionumber>. If no bib exists, returns undef.
1104 The MARC record contains both biblio & item data.
1109 my $biblionumber = shift;
1110 my $dbh = C4
::Context
->dbh;
1111 my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1112 $sth->execute($biblionumber);
1113 my $row = $sth->fetchrow_hashref;
1114 my $marcxml = StripNonXmlChars
( $row->{'marcxml'} );
1115 MARC
::File
::XML
->default_record_format( C4
::Context
->preference('marcflavour') );
1116 my $record = MARC
::Record
->new();
1119 $record = eval { MARC
::Record
::new_from_xml
( $marcxml, "utf8", C4
::Context
->preference('marcflavour') ) };
1120 if ($@
) { warn " problem with :$biblionumber : $@ \n$marcxml"; }
1122 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1133 my $marcxml = GetXmlBiblio($biblionumber);
1135 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1136 The XML contains both biblio & item datas
1143 my ($biblionumber) = @_;
1144 my $dbh = C4
::Context
->dbh;
1145 my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1146 $sth->execute($biblionumber);
1147 my ($marcxml) = $sth->fetchrow;
1151 =head2 GetCOinSBiblio
1155 my $coins = GetCOinSBiblio($biblionumber);
1157 Returns the COinS(a span) which can be included in a biblio record
1163 sub GetCOinSBiblio
{
1164 my ($biblionumber) = @_;
1165 my $record = GetMarcBiblio
($biblionumber);
1167 # get the coin format
1169 # can't get a valid MARC::Record object, bail out at this point
1170 warn "We called GetMarcBiblio with a biblionumber that doesn't exist biblionumber=$biblionumber";
1173 my $pos7 = substr $record->leader(), 7, 1;
1174 my $pos6 = substr $record->leader(), 6, 1;
1177 my ( $aulast, $aufirst ) = ( '', '' );
1186 if ( C4
::Context
->preference("marcflavour") eq "UNIMARC" ) {
1191 'b' => 'manuscript',
1193 'd' => 'manuscript',
1197 'i' => 'audioRecording',
1198 'j' => 'audioRecording',
1201 'm' => 'computerProgram',
1206 'a' => 'journalArticle',
1210 $genre = $fmts6->{$pos6} ?
$fmts6->{$pos6} : 'book';
1212 if ( $genre eq 'book' ) {
1213 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
1216 ##### We must transform mtx to a valable mtx and document type ####
1217 if ( $genre eq 'book' ) {
1219 } elsif ( $genre eq 'journal' ) {
1221 } elsif ( $genre eq 'journalArticle' ) {
1228 $genre = ( $mtx eq 'dc' ) ?
"&rft.type=$genre" : "&rft.genre=$genre";
1231 $aulast = $record->subfield( '700', 'a' );
1232 $aufirst = $record->subfield( '700', 'b' );
1233 $oauthors = "&rft.au=$aufirst $aulast";
1236 if ( $record->field('200') ) {
1237 for my $au ( $record->field('200')->subfield('g') ) {
1238 $oauthors .= "&rft.au=$au";
1243 ?
"&rft.title=" . $record->subfield( '200', 'a' )
1244 : "&rft.title=" . $record->subfield( '200', 'a' ) . "&rft.btitle=" . $record->subfield( '200', 'a' );
1245 $pubyear = $record->subfield( '210', 'd' );
1246 $publisher = $record->subfield( '210', 'c' );
1247 $isbn = $record->subfield( '010', 'a' );
1248 $issn = $record->subfield( '011', 'a' );
1251 # MARC21 need some improve
1254 $genre = "&rft.genre=book";
1257 if ( $record->field('100') ) {
1258 $oauthors .= "&rft.au=" . $record->subfield( '100', 'a' );
1262 if ( $record->field('700') ) {
1263 for my $au ( $record->field('700')->subfield('a') ) {
1264 $oauthors .= "&rft.au=$au";
1267 $title = "&rft.btitle=" . $record->subfield( '245', 'a' );
1268 $subtitle = $record->subfield( '245', 'b' ) || '';
1269 $title .= $subtitle;
1270 $pubyear = $record->subfield( '260', 'c' ) || '';
1271 $publisher = $record->subfield( '260', 'b' ) || '';
1272 $isbn = $record->subfield( '020', 'a' ) || '';
1273 $issn = $record->subfield( '022', 'a' ) || '';
1277 "ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear";
1278 $coins_value =~ s/(\ |&[^a])/\+/g;
1280 #<!-- TMPL_VAR NAME="ocoins_format" -->&rft.au=<!-- TMPL_VAR NAME="author" -->&rft.btitle=<!-- TMPL_VAR NAME="title" -->&rft.date=<!-- TMPL_VAR NAME="publicationyear" -->&rft.pages=<!-- TMPL_VAR NAME="pages" -->&rft.isbn=<!-- TMPL_VAR NAME=amazonisbn -->&rft.aucorp=&rft.place=<!-- TMPL_VAR NAME="place" -->&rft.pub=<!-- TMPL_VAR NAME="publishercode" -->&rft.edition=<!-- TMPL_VAR NAME="edition" -->&rft.series=<!-- TMPL_VAR NAME="series" -->&rft.genre="
1282 return $coins_value;
1285 =head2 GetAuthorisedValueDesc
1289 my $subfieldvalue =get_authorised_value_desc(
1290 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category, $opac);
1291 Retrieve the complete description for a given authorised value.
1293 Now takes $category and $value pair too.
1294 my $auth_value_desc =GetAuthorisedValueDesc(
1295 '','', 'DVD' ,'','','CCODE');
1297 If the optional $opac parameter is set to a true value, displays OPAC descriptions rather than normal ones when they exist.
1304 sub GetAuthorisedValueDesc
{
1305 my ( $tag, $subfield, $value, $framework, $tagslib, $category, $opac ) = @_;
1306 my $dbh = C4
::Context
->dbh;
1310 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1313 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1314 return C4
::Branch
::GetBranchName
($value);
1318 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1319 return getitemtypeinfo
($value)->{description
};
1322 #---- "true" authorized value
1323 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1326 if ( $category ne "" ) {
1327 my $sth = $dbh->prepare( "SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ?" );
1328 $sth->execute( $category, $value );
1329 my $data = $sth->fetchrow_hashref;
1330 return ( $opac && $data->{'lib_opac'} ) ?
$data->{'lib_opac'} : $data->{'lib'};
1332 return $value; # if nothing is found return the original value
1340 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1341 Get all notes from the MARC record and returns them in an array.
1342 The note are stored in differents places depending on MARC flavour
1349 my ( $record, $marcflavour ) = @_;
1351 if ( $marcflavour eq "MARC21" ) {
1353 } else { # assume unimarc if not marc21
1360 foreach my $field ( $record->field($scope) ) {
1361 my $value = $field->as_string();
1362 if ( $note ne "" ) {
1363 $marcnote = { marcnote
=> $note, };
1364 push @marcnotes, $marcnote;
1367 if ( $note ne $value ) {
1368 $note = $note . " " . $value;
1373 $marcnote = { marcnote
=> $note };
1374 push @marcnotes, $marcnote; #load last tag into array
1377 } # end GetMarcNotes
1379 =head2 GetMarcSubjects
1383 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1384 Get all subjects from the MARC record and returns them in an array.
1385 The subjects are stored in differents places depending on MARC flavour
1391 sub GetMarcSubjects
{
1392 my ( $record, $marcflavour ) = @_;
1393 my ( $mintag, $maxtag );
1394 if ( $marcflavour eq "MARC21" ) {
1397 } else { # assume unimarc if not marc21
1407 foreach my $field ( $record->field('6..') ) {
1408 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1410 my @subfields = $field->subfields();
1414 # if there is an authority link, build the link with an= subfield9
1416 for my $subject_subfield (@subfields) {
1418 # don't load unimarc subfields 3,4,5
1419 next if ( ( $marcflavour eq "UNIMARC" ) and ( $subject_subfield->[0] =~ /2|3|4|5/ ) );
1421 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1422 next if ( ( $marcflavour eq "MARC21" ) and ( $subject_subfield->[0] =~ /2/ ) );
1423 my $code = $subject_subfield->[0];
1424 my $value = $subject_subfield->[1];
1425 my $linkvalue = $value;
1426 $linkvalue =~ s/(\(|\))//g;
1427 my $operator = " and " unless $counter == 0;
1430 @link_loop = ( { 'limit' => 'an', link => "$linkvalue" } );
1432 if ( not $found9 ) {
1433 push @link_loop, { 'limit' => 'su', link => $linkvalue, operator
=> $operator };
1435 my $separator = C4
::Context
->preference("authoritysep") unless $counter == 0;
1438 my @this_link_loop = @link_loop;
1439 push @subfields_loop, { code
=> $code, value
=> $value, link_loop
=> \
@this_link_loop, separator
=> $separator } unless ( $subject_subfield->[0] eq 9 );
1443 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP
=> \
@subfields_loop };
1446 return \
@marcsubjects;
1447 } #end getMARCsubjects
1449 =head2 GetMarcAuthors
1453 authors = GetMarcAuthors($record,$marcflavour);
1454 Get all authors from the MARC record and returns them in an array.
1455 The authors are stored in differents places depending on MARC flavour
1461 sub GetMarcAuthors
{
1462 my ( $record, $marcflavour ) = @_;
1463 my ( $mintag, $maxtag );
1465 # tagslib useful for UNIMARC author reponsabilities
1467 &GetMarcStructure
( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be buggy on some setups, will be usually correct.
1468 if ( $marcflavour eq "MARC21" ) {
1471 } elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1479 foreach my $field ( $record->fields ) {
1480 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1483 my @subfields = $field->subfields();
1486 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1487 my $subfield9 = $field->subfield('9');
1488 for my $authors_subfield (@subfields) {
1490 # don't load unimarc subfields 3, 5
1491 next if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /3|5/ ) );
1492 my $subfieldcode = $authors_subfield->[0];
1493 my $value = $authors_subfield->[1];
1494 my $linkvalue = $value;
1495 $linkvalue =~ s/(\(|\))//g;
1496 my $operator = " and " unless $count_auth == 0;
1498 # if we have an authority link, use that as the link, otherwise use standard searching
1500 @link_loop = ( { 'limit' => 'an', link => "$subfield9" } );
1503 # reset $linkvalue if UNIMARC author responsibility
1504 if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] eq "4" ) ) {
1505 $linkvalue = "(" . GetAuthorisedValueDesc
( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) . ")";
1507 push @link_loop, { 'limit' => 'au', link => $linkvalue, operator
=> $operator };
1509 $value = GetAuthorisedValueDesc
( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib )
1510 if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /4/ ) );
1511 my @this_link_loop = @link_loop;
1512 my $separator = C4
::Context
->preference("authoritysep") unless $count_auth == 0;
1513 push @subfields_loop, { code
=> $subfieldcode, value
=> $value, link_loop
=> \
@this_link_loop, separator
=> $separator } unless ( $authors_subfield->[0] eq '9' );
1516 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP
=> \
@subfields_loop };
1518 return \
@marcauthors;
1525 $marcurls = GetMarcUrls($record,$marcflavour);
1526 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1527 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1534 my ( $record, $marcflavour ) = @_;
1537 for my $field ( $record->field('856') ) {
1539 for my $note ( $field->subfield('z') ) {
1540 push @notes, { note
=> $note };
1542 my @urls = $field->subfield('u');
1543 foreach my $url (@urls) {
1545 if ( $marcflavour eq 'MARC21' ) {
1546 my $s3 = $field->subfield('3');
1547 my $link = $field->subfield('y');
1548 unless ( $url =~ /^\w+:/ ) {
1549 if ( $field->indicator(1) eq '7' ) {
1550 $url = $field->subfield('2') . "://" . $url;
1551 } elsif ( $field->indicator(1) eq '1' ) {
1552 $url = 'ftp://' . $url;
1555 # properly, this should be if ind1=4,
1556 # however we will assume http protocol since we're building a link.
1557 $url = 'http://' . $url;
1561 # TODO handle ind 2 (relationship)
1566 $marcurl->{'linktext'} = $link || $s3 || C4
::Context
->preference('URLLinkText') || $url;
1567 $marcurl->{'part'} = $s3 if ($link);
1568 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^[Tt]able/ );
1570 $marcurl->{'linktext'} = $field->subfield('2') || C4
::Context
->preference('URLLinkText') || $url;
1571 $marcurl->{'MARCURL'} = $url;
1573 push @marcurls, $marcurl;
1579 =head2 GetMarcSeries
1583 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1584 Get all series from the MARC record and returns them in an array.
1585 The series are stored in differents places depending on MARC flavour
1592 my ( $record, $marcflavour ) = @_;
1593 my ( $mintag, $maxtag );
1594 if ( $marcflavour eq "MARC21" ) {
1597 } else { # assume unimarc if not marc21
1607 foreach my $field ( $record->field('440'), $record->field('490') ) {
1610 #my $value = $field->subfield('a');
1611 #$marcsubjct = {MARCSUBJCT => $value,};
1612 my @subfields = $field->subfields();
1614 #warn "subfields:".join " ", @$subfields;
1617 for my $series_subfield (@subfields) {
1619 undef $volume_number;
1621 # see if this is an instance of a volume
1622 if ( $series_subfield->[0] eq 'v' ) {
1626 my $code = $series_subfield->[0];
1627 my $value = $series_subfield->[1];
1628 my $linkvalue = $value;
1629 $linkvalue =~ s/(\(|\))//g;
1630 my $operator = " and " unless $counter == 0;
1631 push @link_loop, { link => $linkvalue, operator
=> $operator };
1632 my $separator = C4
::Context
->preference("authoritysep") unless $counter == 0;
1633 if ($volume_number) {
1634 push @subfields_loop, { volumenum
=> $value };
1636 push @subfields_loop, { code
=> $code, value
=> $value, link_loop
=> \
@link_loop, separator
=> $separator, volumenum
=> $volume_number };
1640 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP
=> \
@subfields_loop };
1642 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1643 #push @marcsubjcts, $marcsubjct;
1647 my $marcseriessarray = \
@marcseries;
1648 return $marcseriessarray;
1649 } #end getMARCseriess
1651 =head2 GetFrameworkCode
1655 $frameworkcode = GetFrameworkCode( $biblionumber )
1661 sub GetFrameworkCode
{
1662 my ($biblionumber) = @_;
1663 my $dbh = C4
::Context
->dbh;
1664 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1665 $sth->execute($biblionumber);
1666 my ($frameworkcode) = $sth->fetchrow;
1667 return $frameworkcode;
1670 =head2 GetPublisherNameFromIsbn
1672 $name = GetPublishercodeFromIsbn($isbn);
1679 sub GetPublisherNameFromIsbn
($) {
1681 $isbn =~ s/[- _]//g;
1683 my @codes = ( split '-', DisplayISBN
($isbn) );
1684 my $code = $codes[0] . $codes[1] . $codes[2];
1685 my $dbh = C4
::Context
->dbh;
1687 SELECT distinct publishercode
1690 AND publishercode IS NOT NULL
1693 my $sth = $dbh->prepare($query);
1694 $sth->execute("$code%");
1695 my $name = $sth->fetchrow;
1696 return $name if length $name;
1700 =head2 TransformKohaToMarc
1704 $record = TransformKohaToMarc( $hash )
1705 This function builds partial MARC::Record from a hash
1706 Hash entries can be from biblio or biblioitems.
1707 This function is called in acquisition module, to create a basic catalogue entry from user entry
1713 sub TransformKohaToMarc
{
1715 my $sth = C4
::Context
->dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1716 my $record = MARC
::Record
->new();
1717 SetMarcUnicodeFlag
( $record, C4
::Context
->preference("marcflavour") );
1718 foreach ( keys %{$hash} ) {
1719 &TransformKohaToMarcOneField
( $sth, $record, $_, $hash->{$_}, '' );
1724 =head2 TransformKohaToMarcOneField
1728 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1734 sub TransformKohaToMarcOneField
{
1735 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1736 $frameworkcode = '' unless $frameworkcode;
1740 if ( !defined $sth ) {
1741 my $dbh = C4
::Context
->dbh;
1742 $sth = $dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1744 $sth->execute( $frameworkcode, $kohafieldname );
1745 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1746 my $tag = $record->field($tagfield);
1748 $tag->update( $tagsubfield => $value );
1749 $record->delete_field($tag);
1750 $record->insert_fields_ordered($tag);
1752 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1758 =head2 TransformHtmlToXml
1762 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1764 $auth_type contains :
1765 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1766 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1767 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1773 sub TransformHtmlToXml
{
1774 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1775 my $xml = MARC
::File
::XML
::header
('UTF-8');
1776 $xml .= "<record>\n";
1777 $auth_type = C4
::Context
->preference('marcflavour') unless $auth_type;
1778 MARC
::File
::XML
->default_record_format($auth_type);
1780 # in UNIMARC, field 100 contains the encoding
1781 # check that there is one, otherwise the
1782 # MARC::Record->new_from_xml will fail (and Koha will die)
1783 my $unimarc_and_100_exist = 0;
1784 $unimarc_and_100_exist = 1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1789 for ( my $i = 0 ; $i < @
$tags ; $i++ ) {
1791 if ( C4
::Context
->preference('marcflavour') eq 'UNIMARC' and @
$tags[$i] eq "100" and @
$subfields[$i] eq "a" ) {
1793 # if we have a 100 field and it's values are not correct, skip them.
1794 # if we don't have any valid 100 field, we will create a default one at the end
1795 my $enc = substr( @
$values[$i], 26, 2 );
1796 if ( $enc eq '01' or $enc eq '50' or $enc eq '03' ) {
1797 $unimarc_and_100_exist = 1;
1802 @
$values[$i] =~ s/&/&/g;
1803 @
$values[$i] =~ s/</</g;
1804 @
$values[$i] =~ s/>/>/g;
1805 @
$values[$i] =~ s/"/"/g;
1806 @
$values[$i] =~ s/'/'/g;
1808 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1809 # utf8::decode( @$values[$i] );
1811 if ( ( @
$tags[$i] ne $prevtag ) ) {
1812 $j++ unless ( @
$tags[$i] eq "" );
1813 my $indicator1 = eval { substr( @
$indicator[$j], 0, 1 ) };
1814 my $indicator2 = eval { substr( @
$indicator[$j], 1, 1 ) };
1815 my $ind1 = _default_ind_to_space
($indicator1);
1817 if ( @
$indicator[$j] ) {
1818 $ind2 = _default_ind_to_space
($indicator2);
1820 warn "Indicator in @$tags[$i] is empty";
1824 $xml .= "</datafield>\n";
1825 if ( ( @
$tags[$i] && @
$tags[$i] > 10 )
1826 && ( @
$values[$i] ne "" ) ) {
1827 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1828 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1834 if ( @
$values[$i] ne "" ) {
1837 if ( @
$tags[$i] eq "000" ) {
1838 $xml .= "<leader>@$values[$i]</leader>\n";
1841 # rest of the fixed fields
1842 } elsif ( @
$tags[$i] < 10 ) {
1843 $xml .= "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1846 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1847 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1852 } else { # @$tags[$i] eq $prevtag
1853 my $indicator1 = eval { substr( @
$indicator[$j], 0, 1 ) };
1854 my $indicator2 = eval { substr( @
$indicator[$j], 1, 1 ) };
1855 my $ind1 = _default_ind_to_space
($indicator1);
1857 if ( @
$indicator[$j] ) {
1858 $ind2 = _default_ind_to_space
($indicator2);
1860 warn "Indicator in @$tags[$i] is empty";
1863 if ( @
$values[$i] eq "" ) {
1866 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1869 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1872 $prevtag = @
$tags[$i];
1874 $xml .= "</datafield>\n" if @
$tags > 0;
1875 if ( C4
::Context
->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist ) {
1877 # warn "SETTING 100 for $auth_type";
1878 my $string = strftime
( "%Y%m%d", localtime(time) );
1880 # set 50 to position 26 is biblios, 13 if authorities
1882 $pos = 13 if $auth_type eq 'UNIMARCAUTH';
1883 $string = sprintf( "%-*s", 35, $string );
1884 substr( $string, $pos, 6, "50" );
1885 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1886 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1887 $xml .= "</datafield>\n";
1889 $xml .= "</record>\n";
1890 $xml .= MARC
::File
::XML
::footer
();
1894 =head2 _default_ind_to_space
1896 Passed what should be an indicator returns a space
1897 if its undefined or zero length
1901 sub _default_ind_to_space
{
1903 if ( !defined $s || $s eq q{} ) {
1909 =head2 TransformHtmlToMarc
1911 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1912 L<$params> is a ref to an array as below:
1914 'tag_010_indicator1_531951' ,
1915 'tag_010_indicator2_531951' ,
1916 'tag_010_code_a_531951_145735' ,
1917 'tag_010_subfield_a_531951_145735' ,
1918 'tag_200_indicator1_873510' ,
1919 'tag_200_indicator2_873510' ,
1920 'tag_200_code_a_873510_673465' ,
1921 'tag_200_subfield_a_873510_673465' ,
1922 'tag_200_code_b_873510_704318' ,
1923 'tag_200_subfield_b_873510_704318' ,
1924 'tag_200_code_e_873510_280822' ,
1925 'tag_200_subfield_e_873510_280822' ,
1926 'tag_200_code_f_873510_110730' ,
1927 'tag_200_subfield_f_873510_110730' ,
1929 L<$cgi> is the CGI object which containts the value.
1930 L<$record> is the MARC::Record object.
1934 sub TransformHtmlToMarc
{
1938 # explicitly turn on the UTF-8 flag for all
1939 # 'tag_' parameters to avoid incorrect character
1940 # conversion later on
1941 my $cgi_params = $cgi->Vars;
1942 foreach my $param_name ( keys %$cgi_params ) {
1943 if ( $param_name =~ /^tag_/ ) {
1944 my $param_value = $cgi_params->{$param_name};
1945 if ( utf8
::decode
($param_value) ) {
1946 $cgi_params->{$param_name} = $param_value;
1949 # FIXME - need to do something if string is not valid UTF-8
1953 # creating a new record
1954 my $record = MARC
::Record
->new();
1957 while ( $params->[$i] ) { # browse all CGI params
1958 my $param = $params->[$i];
1961 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1962 if ( $param eq 'biblionumber' ) {
1963 my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField
( "biblio.biblionumber", '' );
1964 if ( $biblionumbertagfield < 10 ) {
1965 $newfield = MARC
::Field
->new( $biblionumbertagfield, $cgi->param($param), );
1967 $newfield = MARC
::Field
->new( $biblionumbertagfield, '', '', "$biblionumbertagsubfield" => $cgi->param($param), );
1969 push @fields, $newfield if ($newfield);
1970 } elsif ( $param =~ /^tag_(\d*)_indicator1_/ ) { # new field start when having 'input name="..._indicator1_..."
1973 my $ind1 = _default_ind_to_space
( substr( $cgi->param($param), 0, 1 ) );
1974 my $ind2 = _default_ind_to_space
( substr( $cgi->param( $params->[ $i + 1 ] ), 0, 1 ) );
1978 if ( $tag < 10 ) { # no code for theses fields
1979 # in MARC editor, 000 contains the leader.
1980 if ( $tag eq '000' ) {
1981 $record->leader( $cgi->param( $params->[ $j + 1 ] ) ) if length( $cgi->param( $params->[ $j + 1 ] ) ) == 24;
1983 # between 001 and 009 (included)
1984 } elsif ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) {
1985 $newfield = MARC
::Field
->new( $tag, $cgi->param( $params->[ $j + 1 ] ), );
1988 # > 009, deal with subfields
1990 while ( defined $params->[$j] && $params->[$j] =~ /_code_/ ) { # browse all it's subfield
1991 my $inner_param = $params->[$j];
1993 if ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) { # only if there is a value (code => value)
1994 $newfield->add_subfields( $cgi->param($inner_param) => $cgi->param( $params->[ $j + 1 ] ) );
1997 if ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) { # creating only if there is a value (code => value)
1998 $newfield = MARC
::Field
->new( $tag, $ind1, $ind2, $cgi->param($inner_param) => $cgi->param( $params->[ $j + 1 ] ), );
2004 push @fields, $newfield if ($newfield);
2009 $record->append_fields(@fields);
2013 # cache inverted MARC field map
2014 our $inverted_field_map;
2016 =head2 TransformMarcToKoha
2020 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2024 Extract data from a MARC bib record into a hashref representing
2025 Koha biblio, biblioitems, and items fields.
2029 sub TransformMarcToKoha
{
2030 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
2033 $limit_table = $limit_table || 0;
2034 $frameworkcode = '' unless defined $frameworkcode;
2036 unless ( defined $inverted_field_map ) {
2037 $inverted_field_map = _get_inverted_marc_field_map
();
2041 if ( defined $limit_table && $limit_table eq 'items' ) {
2042 $tables{'items'} = 1;
2044 $tables{'items'} = 1;
2045 $tables{'biblio'} = 1;
2046 $tables{'biblioitems'} = 1;
2049 # traverse through record
2050 MARCFIELD
: foreach my $field ( $record->fields() ) {
2051 my $tag = $field->tag();
2052 next MARCFIELD
unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2053 if ( $field->is_control_field() ) {
2054 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list
};
2055 ENTRY
: foreach my $entry ( @
{$kohafields} ) {
2056 my ( $subfield, $table, $column ) = @
{$entry};
2057 next ENTRY
unless exists $tables{$table};
2058 my $key = _disambiguate
( $table, $column );
2059 if ( $result->{$key} ) {
2060 unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $field->data() eq "" ) ) {
2061 $result->{$key} .= " | " . $field->data();
2064 $result->{$key} = $field->data();
2069 # deal with subfields
2070 MARCSUBFIELD
: foreach my $sf ( $field->subfields() ) {
2071 my $code = $sf->[0];
2072 next MARCSUBFIELD
unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs
}->{$code};
2073 my $value = $sf->[1];
2074 SFENTRY
: foreach my $entry ( @
{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs
}->{$code} } ) {
2075 my ( $table, $column ) = @
{$entry};
2076 next SFENTRY
unless exists $tables{$table};
2077 my $key = _disambiguate
( $table, $column );
2078 if ( $result->{$key} ) {
2079 unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $value eq "" ) ) {
2080 $result->{$key} .= " | " . $value;
2083 $result->{$key} = $value;
2090 # modify copyrightdate to keep only the 1st year found
2091 if ( exists $result->{'copyrightdate'} ) {
2092 my $temp = $result->{'copyrightdate'};
2093 $temp =~ m/c(\d\d\d\d)/;
2094 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2095 $result->{'copyrightdate'} = $1;
2096 } else { # if no cYYYY, get the 1st date.
2097 $temp =~ m/(\d\d\d\d)/;
2098 $result->{'copyrightdate'} = $1;
2102 # modify publicationyear to keep only the 1st year found
2103 if ( exists $result->{'publicationyear'} ) {
2104 my $temp = $result->{'publicationyear'};
2105 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2106 $result->{'publicationyear'} = $1;
2107 } else { # if no cYYYY, get the 1st date.
2108 $temp =~ m/(\d\d\d\d)/;
2109 $result->{'publicationyear'} = $1;
2116 sub _get_inverted_marc_field_map
{
2118 my $relations = C4
::Context
->marcfromkohafield;
2120 foreach my $frameworkcode ( keys %{$relations} ) {
2121 foreach my $kohafield ( keys %{ $relations->{$frameworkcode} } ) {
2122 next unless @
{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
2123 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
2124 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2125 my ( $table, $column ) = split /[.]/, $kohafield, 2;
2126 push @
{ $field_map->{$frameworkcode}->{$tag}->{list
} }, [ $subfield, $table, $column ];
2127 push @
{ $field_map->{$frameworkcode}->{$tag}->{sfs
}->{$subfield} }, [ $table, $column ];
2133 =head2 _disambiguate
2137 $newkey = _disambiguate($table, $field);
2139 This is a temporary hack to distinguish between the
2140 following sets of columns when using TransformMarcToKoha.
2142 items.cn_source & biblioitems.cn_source
2143 items.cn_sort & biblioitems.cn_sort
2145 Columns that are currently NOT distinguished (FIXME
2146 due to lack of time to fully test) are:
2148 biblio.notes and biblioitems.notes
2153 FIXME - this is necessary because prefixing each column
2154 name with the table name would require changing lots
2155 of code and templates, and exposing more of the DB
2156 structure than is good to the UI templates, particularly
2157 since biblio and bibloitems may well merge in a future
2158 version. In the future, it would also be good to
2159 separate DB access and UI presentation field names
2166 sub CountItemsIssued
{
2167 my ($biblionumber) = @_;
2168 my $dbh = C4
::Context
->dbh;
2169 my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2170 $sth->execute($biblionumber);
2171 my $row = $sth->fetchrow_hashref();
2172 return $row->{'issuedCount'};
2176 my ( $table, $column ) = @_;
2177 if ( $column eq "cn_sort" or $column eq "cn_source" ) {
2178 return $table . '.' . $column;
2185 =head2 get_koha_field_from_marc
2189 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2191 Internal function to map data from the MARC record to a specific non-MARC field.
2192 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2198 sub get_koha_field_from_marc
{
2199 my ( $koha_table, $koha_column, $record, $frameworkcode ) = @_;
2200 my ( $tagfield, $subfield ) = GetMarcFromKohaField
( $koha_table . '.' . $koha_column, $frameworkcode );
2202 foreach my $field ( $record->field($tagfield) ) {
2203 if ( $field->tag() < 10 ) {
2205 $kohafield .= " | " . $field->data();
2207 $kohafield = $field->data();
2210 if ( $field->subfields ) {
2211 my @subfields = $field->subfields();
2212 foreach my $subfieldcount ( 0 .. $#subfields ) {
2213 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2215 $kohafield .= " | " . $subfields[$subfieldcount][1];
2217 $kohafield = $subfields[$subfieldcount][1];
2227 =head2 TransformMarcToKohaOneField
2231 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2237 sub TransformMarcToKohaOneField
{
2239 # FIXME ? if a field has a repeatable subfield that is used in old-db,
2240 # only the 1st will be retrieved...
2241 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2243 my ( $tagfield, $subfield ) = GetMarcFromKohaField
( $kohatable . "." . $kohafield, $frameworkcode );
2244 foreach my $field ( $record->field($tagfield) ) {
2245 if ( $field->tag() < 10 ) {
2246 if ( $result->{$kohafield} ) {
2247 $result->{$kohafield} .= " | " . $field->data();
2249 $result->{$kohafield} = $field->data();
2252 if ( $field->subfields ) {
2253 my @subfields = $field->subfields();
2254 foreach my $subfieldcount ( 0 .. $#subfields ) {
2255 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2256 if ( $result->{$kohafield} ) {
2257 $result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
2259 $result->{$kohafield} = $subfields[$subfieldcount][1];
2269 =head1 OTHER FUNCTIONS
2272 =head2 PrepareItemrecordDisplay
2276 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber,$frameworkcode);
2278 Returns a hash with all the fields for Display a given item data in a template
2280 The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
2286 sub PrepareItemrecordDisplay
{
2288 my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
2290 my $dbh = C4
::Context
->dbh;
2291 $frameworkcode = &GetFrameworkCode
($bibnum) if $bibnum;
2292 my ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField
( "items.itemnumber", $frameworkcode );
2293 my $tagslib = &GetMarcStructure
( 1, $frameworkcode );
2295 # return nothing if we don't have found an existing framework.
2296 return "" unless $tagslib;
2297 my $itemrecord = C4
::Items
::GetMarcItem
( $bibnum, $itemnum ) if ($itemnum);
2299 my $authorised_values_sth = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib" );
2300 foreach my $tag ( sort keys %{$tagslib} ) {
2301 my $previous_tag = '';
2304 # loop through each subfield
2306 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2307 next if ( subfield_is_koha_internal_p
($subfield) );
2308 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2310 $subfield_data{tag
} = $tag;
2311 $subfield_data{subfield
} = $subfield;
2312 $subfield_data{countsubfield
} = $cntsubf++;
2313 $subfield_data{kohafield
} = $tagslib->{$tag}->{$subfield}->{'kohafield'};
2315 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2316 $subfield_data{marc_lib
} = $tagslib->{$tag}->{$subfield}->{lib
};
2317 $subfield_data{mandatory
} = $tagslib->{$tag}->{$subfield}->{mandatory
};
2318 $subfield_data{repeatable
} = $tagslib->{$tag}->{$subfield}->{repeatable
};
2319 $subfield_data{hidden
} = "display:none"
2320 if $tagslib->{$tag}->{$subfield}->{hidden
};
2321 my ( $x, $defaultvalue );
2323 ( $x, $defaultvalue ) = _find_value
( $tag, $subfield, $itemrecord );
2325 $defaultvalue = $tagslib->{$tag}->{$subfield}->{defaultvalue
} unless $defaultvalue;
2326 if ( !defined $defaultvalue ) {
2327 $defaultvalue = q
||;
2329 $defaultvalue =~ s/"/"/g;
2331 # search for itemcallnumber if applicable
2332 if ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber'
2333 && C4
::Context
->preference('itemcallnumber') ) {
2334 my $CNtag = substr( C4
::Context
->preference('itemcallnumber'), 0, 3 );
2335 my $CNsubfield = substr( C4
::Context
->preference('itemcallnumber'), 3, 1 );
2336 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2338 $defaultvalue = $temp->subfield($CNsubfield);
2341 if ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.itemcallnumber'
2343 && $defaultvalues->{'callnumber'} ) {
2344 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2346 $defaultvalue = $defaultvalues->{'callnumber'} if $defaultvalues;
2349 if ( ( $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.holdingbranch' || $tagslib->{$tag}->{$subfield}->{kohafield
} eq 'items.homebranch' )
2351 && $defaultvalues->{'branchcode'} ) {
2352 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2354 $defaultvalue = $defaultvalues->{branchcode
} if $defaultvalues;
2357 if ( $tagslib->{$tag}->{$subfield}->{authorised_value
} ) {
2358 my @authorised_values;
2361 # builds list, depending on authorised value...
2363 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
2364 if ( ( C4
::Context
->preference("IndependantBranches") )
2365 && ( C4
::Context
->userenv->{flags
} % 2 != 1 ) ) {
2366 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" );
2367 $sth->execute( C4
::Context
->userenv->{branch
} );
2368 push @authorised_values, ""
2369 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2370 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2371 push @authorised_values, $branchcode;
2372 $authorised_lib{$branchcode} = $branchname;
2375 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches ORDER BY branchname" );
2377 push @authorised_values, ""
2378 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2379 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2380 push @authorised_values, $branchcode;
2381 $authorised_lib{$branchcode} = $branchname;
2386 } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value
} eq "itemtypes" ) {
2387 my $sth = $dbh->prepare( "SELECT itemtype,description FROM itemtypes ORDER BY description" );
2389 push @authorised_values, ""
2390 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2391 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
2392 push @authorised_values, $itemtype;
2393 $authorised_lib{$itemtype} = $description;
2396 #---- "true" authorised value
2398 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value
} );
2399 push @authorised_values, ""
2400 unless ( $tagslib->{$tag}->{$subfield}->{mandatory
} );
2401 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
2402 push @authorised_values, $value;
2403 $authorised_lib{$value} = $lib;
2406 $subfield_data{marc_value
} = CGI
::scrolling_list
(
2407 -name
=> 'field_value',
2408 -values => \
@authorised_values,
2409 -default => "$defaultvalue",
2410 -labels
=> \
%authorised_lib,
2416 $subfield_data{marc_value
} = "<input type=\"text\" name=\"field_value\" value=\"$defaultvalue\" size=\"50\" maxlength=\"255\" />";
2418 push( @loop_data, \
%subfield_data );
2422 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2423 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2425 'itemtagfield' => $itemtagfield,
2426 'itemtagsubfield' => $itemtagsubfield,
2427 'itemnumber' => $itemnumber,
2428 'iteminformation' => \
@loop_data
2435 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2437 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2438 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2439 # =head2 ModZebrafiles
2441 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2445 # sub ModZebrafiles {
2447 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2451 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2452 # unless ( opendir( DIR, "$zebradir" ) ) {
2453 # warn "$zebradir not found";
2457 # my $filename = $zebradir . $biblionumber;
2460 # open( OUTPUT, ">", $filename . ".xml" );
2461 # print OUTPUT $record;
2470 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2472 $biblionumber is the biblionumber we want to index
2473 $op is specialUpdate or delete, and is used to know what we want to do
2474 $server is the server that we want to update
2475 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2476 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2478 $newRecord is the MARC::Record containing the new record. It is usefull only when NoZebra=1, and is used to know what to add to the nozebra database. (the record in mySQL being, if it exist, the previous record, the one just before the modif. We need both : the previous and the new one.
2485 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2486 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2487 my $dbh = C4
::Context
->dbh;
2489 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2491 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2492 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2494 if ( C4
::Context
->preference("NoZebra") ) {
2496 # lock the nozebra table : we will read index lines, update them in Perl process
2497 # and write everything in 1 transaction.
2498 # lock the table to avoid someone else overwriting what we are doing
2499 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2500 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2501 if ( $op eq 'specialUpdate' ) {
2503 # OK, we have to add or update the record
2504 # 1st delete (virtually, in indexes), if record actually exists
2506 %result = _DelBiblioNoZebra
( $biblionumber, $oldRecord, $server );
2509 # ... add the record
2510 %result = _AddBiblioNoZebra
( $biblionumber, $newRecord, $server, %result );
2513 # it's a deletion, delete the record...
2514 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2515 %result = _DelBiblioNoZebra
( $biblionumber, $oldRecord, $server );
2518 # ok, now update the database...
2519 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2520 foreach my $key ( keys %result ) {
2521 foreach my $index ( keys %{ $result{$key} } ) {
2522 $sth->execute( $result{$key}->{$index}, $server, $key, $index );
2525 $dbh->do('UNLOCK TABLES');
2529 # we use zebra, just fill zebraqueue table
2531 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2533 AND biblio_auth_number = ?
2536 my $check_sth = $dbh->prepare_cached($check_sql);
2537 $check_sth->execute( $server, $biblionumber, $op );
2538 my ($count) = $check_sth->fetchrow_array;
2539 $check_sth->finish();
2540 if ( $count == 0 ) {
2541 my $sth = $dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2542 $sth->execute( $biblionumber, $server, $op );
2548 =head2 GetNoZebraIndexes
2550 %indexes = GetNoZebraIndexes;
2552 return the data from NoZebraIndexes syspref.
2556 sub GetNoZebraIndexes
{
2557 my $no_zebra_indexes = C4
::Context
->preference('NoZebraIndexes');
2559 INDEX
: foreach my $line ( split /['"],[\n\r]*/, $no_zebra_indexes ) {
2560 $line =~ /(.*)=>(.*)/;
2561 my $index = $1; # initial ' or " is removed afterwards
2563 $index =~ s/'|"|\s//g;
2564 $fields =~ s/'|"|\s//g;
2565 $indexes{$index} = $fields;
2570 =head1 INTERNAL FUNCTIONS
2572 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2574 function to delete a biblio in NoZebra indexes
2575 This function does NOT delete anything in database : it reads all the indexes entries
2576 that have to be deleted & delete them in the hash
2577 The SQL part is done either :
2578 - after the Add if we are modifying a biblio (delete + add again)
2579 - immediatly after this sub if we are doing a true deletion.
2580 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2584 sub _DelBiblioNoZebra
{
2585 my ( $biblionumber, $record, $server ) = @_;
2588 my $dbh = C4
::Context
->dbh;
2593 if ( $server eq 'biblioserver' ) {
2594 %index = GetNoZebraIndexes
;
2596 # get title of the record (to store the 10 first letters with the index)
2597 my ( $titletag, $titlesubfield ) = GetMarcFromKohaField
( 'biblio.title', '' ); # FIXME: should be GetFrameworkCode($biblionumber) ??
2598 $title = lc( $record->subfield( $titletag, $titlesubfield ) );
2601 # for authorities, the "title" is the $a mainentry
2602 my ( $auth_type_tag, $auth_type_sf ) = C4
::AuthoritiesMarc
::get_auth_type_location
();
2603 my $authref = C4
::AuthoritiesMarc
::GetAuthType
( $record->subfield( $auth_type_tag, $auth_type_sf ) );
2604 warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
2605 $title = $record->subfield( $authref->{auth_tag_to_report
}, 'a' );
2606 $index{'mainmainentry'} = $authref->{'auth_tag_to_report'} . 'a';
2607 $index{'mainentry'} = $authref->{'auth_tag_to_report'} . '*';
2608 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2613 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2614 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2616 # limit to 10 char, should be enough, and limit the DB size
2617 $title = substr( $title, 0, 10 );
2620 my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2621 foreach my $field ( $record->fields() ) {
2623 #parse each subfield
2624 next if $field->tag < 10;
2625 foreach my $subfield ( $field->subfields() ) {
2626 my $tag = $field->tag();
2627 my $subfieldcode = $subfield->[0];
2630 # check each index to see if the subfield is stored somewhere
2631 # otherwise, store it in __RAW__ index
2632 foreach my $key ( keys %index ) {
2634 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2635 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
2637 my $line = lc $subfield->[1];
2639 # remove meaningless value in the field...
2640 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g
;
2642 # ... and split in words
2643 foreach ( split / /, $line ) {
2644 next unless $_; # skip empty values (multiple spaces)
2645 # if the entry is already here, do nothing, the biblionumber has already be removed
2646 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/ ) ) {
2648 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2649 $sth2->execute( $server, $key, $_ );
2650 my $existing_biblionumbers = $sth2->fetchrow;
2653 if ($existing_biblionumbers) {
2655 # warn " existing for $key $_: $existing_biblionumbers";
2656 $result{$key}->{$_} = $existing_biblionumbers;
2657 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2664 # the subfield is not indexed, store it in __RAW__ index anyway
2666 my $line = lc $subfield->[1];
2667 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g
;
2669 # ... and split in words
2670 foreach ( split / /, $line ) {
2671 next unless $_; # skip empty values (multiple spaces)
2672 # if the entry is already here, do nothing, the biblionumber has already be removed
2673 unless ( $result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/ ) {
2675 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2676 $sth2->execute( $server, '__RAW__', $_ );
2677 my $existing_biblionumbers = $sth2->fetchrow;
2680 if ($existing_biblionumbers) {
2681 $result{'__RAW__'}->{$_} = $existing_biblionumbers;
2682 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2692 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2694 function to add a biblio in NoZebra indexes
2698 sub _AddBiblioNoZebra
{
2699 my ( $biblionumber, $record, $server, %result ) = @_;
2700 my $dbh = C4
::Context
->dbh;
2705 if ( $server eq 'biblioserver' ) {
2706 %index = GetNoZebraIndexes
;
2708 # get title of the record (to store the 10 first letters with the index)
2709 my ( $titletag, $titlesubfield ) = GetMarcFromKohaField
( 'biblio.title', '' ); # FIXME: should be GetFrameworkCode($biblionumber) ??
2710 $title = lc( $record->subfield( $titletag, $titlesubfield ) );
2713 # warn "server : $server";
2714 # for authorities, the "title" is the $a mainentry
2715 my ( $auth_type_tag, $auth_type_sf ) = C4
::AuthoritiesMarc
::get_auth_type_location
();
2716 my $authref = C4
::AuthoritiesMarc
::GetAuthType
( $record->subfield( $auth_type_tag, $auth_type_sf ) );
2717 warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
2718 $title = $record->subfield( $authref->{auth_tag_to_report
}, 'a' );
2719 $index{'mainmainentry'} = $authref->{auth_tag_to_report
} . 'a';
2720 $index{'mainentry'} = $authref->{auth_tag_to_report
} . '*';
2721 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2724 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2725 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2727 # limit to 10 char, should be enough, and limit the DB size
2728 $title = substr( $title, 0, 10 );
2731 my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2732 foreach my $field ( $record->fields() ) {
2734 #parse each subfield
2735 ###FIXME: impossible to index a 001-009 value with NoZebra
2736 next if $field->tag < 10;
2737 foreach my $subfield ( $field->subfields() ) {
2738 my $tag = $field->tag();
2739 my $subfieldcode = $subfield->[0];
2742 # warn "INDEXING :".$subfield->[1];
2743 # check each index to see if the subfield is stored somewhere
2744 # otherwise, store it in __RAW__ index
2745 foreach my $key ( keys %index ) {
2747 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2748 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
2750 my $line = lc $subfield->[1];
2752 # remove meaningless value in the field...
2753 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g
;
2755 # ... and split in words
2756 foreach ( split / /, $line ) {
2757 next unless $_; # skip empty values (multiple spaces)
2758 # if the entry is already here, improve weight
2760 # warn "managing $_";
2761 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
2762 my $weight = $1 + 1;
2763 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2764 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2767 # get the value if it exist in the nozebra table, otherwise, create it
2768 $sth2->execute( $server, $key, $_ );
2769 my $existing_biblionumbers = $sth2->fetchrow;
2772 if ($existing_biblionumbers) {
2773 $result{$key}->{"$_"} = $existing_biblionumbers;
2774 my $weight = defined $1 ?
$1 + 1 : 1;
2775 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2776 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2778 # create a new ligne for this entry
2781 # warn "INSERT : $server / $key / $_";
2782 $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ', indexname=' . $dbh->quote($key) . ',value=' . $dbh->quote($_) );
2783 $result{$key}->{"$_"} .= "$biblionumber,$title-1;";
2790 # the subfield is not indexed, store it in __RAW__ index anyway
2792 my $line = lc $subfield->[1];
2793 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g
;
2795 # ... and split in words
2796 foreach ( split / /, $line ) {
2797 next unless $_; # skip empty values (multiple spaces)
2798 # if the entry is already here, improve weight
2799 my $tmpstr = $result{'__RAW__'}->{"$_"} || "";
2800 if ( $tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
2801 my $weight = $1 + 1;
2802 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2803 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2806 # get the value if it exist in the nozebra table, otherwise, create it
2807 $sth2->execute( $server, '__RAW__', $_ );
2808 my $existing_biblionumbers = $sth2->fetchrow;
2811 if ($existing_biblionumbers) {
2812 $result{'__RAW__'}->{"$_"} = $existing_biblionumbers;
2813 my $weight = ( $1 ?
$1 : 0 ) + 1;
2814 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2815 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2817 # create a new ligne for this entry
2819 $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ', indexname="__RAW__",value=' . $dbh->quote($_) );
2820 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-1;";
2834 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2836 Find the given $subfield in the given $tag in the given
2837 MARC::Record $record. If the subfield is found, returns
2838 the (indicators, value) pair; otherwise, (undef, undef) is
2842 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2843 I suggest we export it from this module.
2850 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2853 if ( $tagfield < 10 ) {
2854 if ( $record->field($tagfield) ) {
2855 push @result, $record->field($tagfield)->data();
2860 foreach my $field ( $record->field($tagfield) ) {
2861 my @subfields = $field->subfields();
2862 foreach my $subfield (@subfields) {
2863 if ( @
$subfield[0] eq $insubfield ) {
2864 push @result, @
$subfield[1];
2865 $indicator = $field->indicator(1) . $field->indicator(2);
2870 return ( $indicator, @result );
2873 =head2 _koha_marc_update_bib_ids
2877 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2879 Internal function to add or update biblionumber and biblioitemnumber to
2886 sub _koha_marc_update_bib_ids
{
2887 my ( $record, $frameworkcode, $biblionumber, $biblioitemnumber ) = @_;
2889 # we must add bibnum and bibitemnum in MARC::Record...
2890 # we build the new field with biblionumber and biblioitemnumber
2891 # we drop the original field
2892 # we add the new builded field.
2893 my ( $biblio_tag, $biblio_subfield ) = GetMarcFromKohaField
( "biblio.biblionumber", $frameworkcode );
2894 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField
( "biblioitems.biblioitemnumber", $frameworkcode );
2896 if ( $biblio_tag != $biblioitem_tag ) {
2898 # biblionumber & biblioitemnumber are in different fields
2900 # deal with biblionumber
2901 my ( $new_field, $old_field );
2902 if ( $biblio_tag < 10 ) {
2903 $new_field = MARC
::Field
->new( $biblio_tag, $biblionumber );
2905 $new_field = MARC
::Field
->new( $biblio_tag, '', '', "$biblio_subfield" => $biblionumber );
2908 # drop old field and create new one...
2909 $old_field = $record->field($biblio_tag);
2910 $record->delete_field($old_field) if $old_field;
2911 $record->append_fields($new_field);
2913 # deal with biblioitemnumber
2914 if ( $biblioitem_tag < 10 ) {
2915 $new_field = MARC
::Field
->new( $biblioitem_tag, $biblioitemnumber, );
2917 $new_field = MARC
::Field
->new( $biblioitem_tag, '', '', "$biblioitem_subfield" => $biblioitemnumber, );
2920 # drop old field and create new one...
2921 $old_field = $record->field($biblioitem_tag);
2922 $record->delete_field($old_field) if $old_field;
2923 $record->insert_fields_ordered($new_field);
2927 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2928 my $new_field = MARC
::Field
->new(
2929 $biblio_tag, '', '',
2930 "$biblio_subfield" => $biblionumber,
2931 "$biblioitem_subfield" => $biblioitemnumber
2934 # drop old field and create new one...
2935 my $old_field = $record->field($biblio_tag);
2936 $record->delete_field($old_field) if $old_field;
2937 $record->insert_fields_ordered($new_field);
2941 =head2 _koha_marc_update_biblioitem_cn_sort
2945 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2949 Given a MARC bib record and the biblioitem hash, update the
2950 subfield that contains a copy of the value of biblioitems.cn_sort.
2954 sub _koha_marc_update_biblioitem_cn_sort
{
2956 my $biblioitem = shift;
2957 my $frameworkcode = shift;
2959 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField
( "biblioitems.cn_sort", $frameworkcode );
2960 return unless $biblioitem_tag;
2962 my ($cn_sort) = GetClassSort
( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2964 if ( my $field = $marc->field($biblioitem_tag) ) {
2965 $field->delete_subfield( code
=> $biblioitem_subfield );
2966 if ( $cn_sort ne '' ) {
2967 $field->add_subfields( $biblioitem_subfield => $cn_sort );
2971 # if we get here, no biblioitem tag is present in the MARC record, so
2972 # we'll create it if $cn_sort is not empty -- this would be
2973 # an odd combination of events, however
2975 $marc->insert_grouped_field( MARC
::Field
->new( $biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort ) );
2980 =head2 _koha_add_biblio
2984 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2986 Internal function to add a biblio ($biblio is a hash with the values)
2992 sub _koha_add_biblio
{
2993 my ( $dbh, $biblio, $frameworkcode ) = @_;
2997 # set the series flag
2998 unless (defined $biblio->{'serial'}){
2999 $biblio->{'serial'} = 0;
3000 if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
3003 my $query = "INSERT INTO biblio
3004 SET frameworkcode = ?,
3015 my $sth = $dbh->prepare($query);
3017 $frameworkcode, $biblio->{'author'}, $biblio->{'title'}, $biblio->{'unititle'}, $biblio->{'notes'},
3018 $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
3021 my $biblionumber = $dbh->{'mysql_insertid'};
3022 if ( $dbh->errstr ) {
3023 $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3029 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
3030 return ( $biblionumber, $error );
3033 =head2 _koha_modify_biblio
3037 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
3039 Internal function for updating the biblio table
3045 sub _koha_modify_biblio
{
3046 my ( $dbh, $biblio, $frameworkcode ) = @_;
3051 SET frameworkcode = ?,
3060 WHERE biblionumber = ?
3063 my $sth = $dbh->prepare($query);
3066 $frameworkcode, $biblio->{'author'}, $biblio->{'title'}, $biblio->{'unititle'}, $biblio->{'notes'},
3067 $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}, $biblio->{'biblionumber'}
3068 ) if $biblio->{'biblionumber'};
3070 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
3071 $error .= "ERROR in _koha_modify_biblio $query" . $dbh->errstr;
3074 return ( $biblio->{'biblionumber'}, $error );
3077 =head2 _koha_modify_biblioitem_nonmarc
3081 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
3083 Updates biblioitems row except for marc and marcxml, which should be changed
3090 sub _koha_modify_biblioitem_nonmarc
{
3091 my ( $dbh, $biblioitem ) = @_;
3094 # re-calculate the cn_sort, it may have changed
3095 my ($cn_sort) = GetClassSort
( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3097 my $query = "UPDATE biblioitems
3098 SET biblionumber = ?,
3104 publicationyear = ?,
3108 collectiontitle = ?,
3110 collectionvolume= ?,
3111 editionstatement= ?,
3112 editionresponsibility = ?,
3126 where biblioitemnumber = ?
3128 my $sth = $dbh->prepare($query);
3130 $biblioitem->{'biblionumber'}, $biblioitem->{'volume'}, $biblioitem->{'number'}, $biblioitem->{'itemtype'},
3131 $biblioitem->{'isbn'}, $biblioitem->{'issn'}, $biblioitem->{'publicationyear'}, $biblioitem->{'publishercode'},
3132 $biblioitem->{'volumedate'}, $biblioitem->{'volumedesc'}, $biblioitem->{'collectiontitle'}, $biblioitem->{'collectionissn'},
3133 $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3134 $biblioitem->{'pages'}, $biblioitem->{'bnotes'}, $biblioitem->{'size'}, $biblioitem->{'place'},
3135 $biblioitem->{'lccn'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'},
3136 $biblioitem->{'cn_item'}, $biblioitem->{'cn_suffix'}, $cn_sort, $biblioitem->{'totalissues'},
3137 $biblioitem->{'biblioitemnumber'}
3139 if ( $dbh->errstr ) {
3140 $error .= "ERROR in _koha_modify_biblioitem_nonmarc $query" . $dbh->errstr;
3143 return ( $biblioitem->{'biblioitemnumber'}, $error );
3146 =head2 _koha_add_biblioitem
3150 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
3152 Internal function to add a biblioitem
3158 sub _koha_add_biblioitem
{
3159 my ( $dbh, $biblioitem ) = @_;
3162 my ($cn_sort) = GetClassSort
( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3163 my $query = "INSERT INTO biblioitems SET
3170 publicationyear = ?,
3174 collectiontitle = ?,
3176 collectionvolume= ?,
3177 editionstatement= ?,
3178 editionresponsibility = ?,
3194 my $sth = $dbh->prepare($query);
3196 $biblioitem->{'biblionumber'}, $biblioitem->{'volume'}, $biblioitem->{'number'}, $biblioitem->{'itemtype'},
3197 $biblioitem->{'isbn'}, $biblioitem->{'issn'}, $biblioitem->{'publicationyear'}, $biblioitem->{'publishercode'},
3198 $biblioitem->{'volumedate'}, $biblioitem->{'volumedesc'}, $biblioitem->{'collectiontitle'}, $biblioitem->{'collectionissn'},
3199 $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3200 $biblioitem->{'pages'}, $biblioitem->{'bnotes'}, $biblioitem->{'size'}, $biblioitem->{'place'},
3201 $biblioitem->{'lccn'}, $biblioitem->{'marc'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'},
3202 $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'}, $biblioitem->{'cn_suffix'}, $cn_sort,
3203 $biblioitem->{'totalissues'}
3205 my $bibitemnum = $dbh->{'mysql_insertid'};
3207 if ( $dbh->errstr ) {
3208 $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3212 return ( $bibitemnum, $error );
3215 =head2 _koha_delete_biblio
3219 $error = _koha_delete_biblio($dbh,$biblionumber);
3221 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3223 C<$dbh> - the database handle
3224 C<$biblionumber> - the biblionumber of the biblio to be deleted
3230 # FIXME: add error handling
3232 sub _koha_delete_biblio
{
3233 my ( $dbh, $biblionumber ) = @_;
3235 # get all the data for this biblio
3236 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3237 $sth->execute($biblionumber);
3239 if ( my $data = $sth->fetchrow_hashref ) {
3241 # save the record in deletedbiblio
3242 # find the fields to save
3243 my $query = "INSERT INTO deletedbiblio SET ";
3245 foreach my $temp ( keys %$data ) {
3246 $query .= "$temp = ?,";
3247 push( @bind, $data->{$temp} );
3250 # replace the last , by ",?)"
3252 my $bkup_sth = $dbh->prepare($query);
3253 $bkup_sth->execute(@bind);
3257 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3258 $del_sth->execute($biblionumber);
3265 =head2 _koha_delete_biblioitems
3269 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3271 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3273 C<$dbh> - the database handle
3274 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3280 # FIXME: add error handling
3282 sub _koha_delete_biblioitems
{
3283 my ( $dbh, $biblioitemnumber ) = @_;
3285 # get all the data for this biblioitem
3286 my $sth = $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3287 $sth->execute($biblioitemnumber);
3289 if ( my $data = $sth->fetchrow_hashref ) {
3291 # save the record in deletedbiblioitems
3292 # find the fields to save
3293 my $query = "INSERT INTO deletedbiblioitems SET ";
3295 foreach my $temp ( keys %$data ) {
3296 $query .= "$temp = ?,";
3297 push( @bind, $data->{$temp} );
3300 # replace the last , by ",?)"
3302 my $bkup_sth = $dbh->prepare($query);
3303 $bkup_sth->execute(@bind);
3306 # delete the biblioitem
3307 my $del_sth = $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3308 $del_sth->execute($biblioitemnumber);
3315 =head1 UNEXPORTED FUNCTIONS
3317 =head2 ModBiblioMarc
3319 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3321 Add MARC data for a biblio to koha
3323 Function exported, but should NOT be used, unless you really know what you're doing
3329 # pass the MARC::Record to this function, and it will create the records in the marc field
3330 my ( $record, $biblionumber, $frameworkcode ) = @_;
3331 my $dbh = C4
::Context
->dbh;
3332 my @fields = $record->fields();
3333 if ( !$frameworkcode ) {
3334 $frameworkcode = "";
3336 my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3337 $sth->execute( $frameworkcode, $biblionumber );
3339 my $encoding = C4
::Context
->preference("marcflavour");
3341 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3342 if ( $encoding eq "UNIMARC" ) {
3343 my $string = $record->subfield( 100, "a" );
3344 if ( ($string) && ( length( $record->subfield( 100, "a" ) ) == 36 ) ) {
3345 my $f100 = $record->field(100);
3346 $record->delete_field($f100);
3348 $string = POSIX
::strftime
( "%Y%m%d", localtime );
3350 $string = sprintf( "%-*s", 35, $string );
3352 substr( $string, 22, 6, "frey50" );
3353 unless ( $record->subfield( 100, "a" ) ) {
3354 $record->insert_grouped_field( MARC
::Field
->new( 100, "", "", "a" => $string ) );
3358 if ( C4
::Context
->preference("NoZebra") ) {
3360 # only NoZebra indexing needs to have
3361 # the previous version of the record
3362 $oldRecord = GetMarcBiblio
($biblionumber);
3364 $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3365 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding), $biblionumber );
3367 ModZebra
( $biblionumber, "specialUpdate", "biblioserver", $oldRecord, $record );
3368 return $biblionumber;
3371 =head2 z3950_extended_services
3373 z3950_extended_services($serviceType,$serviceOptions,$record);
3375 z3950_extended_services is used to handle all interactions with Zebra's extended serices package, which is employed to perform all management of the MARC data stored in Zebra.
3377 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3379 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3381 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3385 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3386 syntax => the record syntax (transfer syntax)
3387 databaseName = Database from connection object
3389 To set serviceOptions, call set_service_options($serviceType)
3391 C<$record> the record, if one is needed for the service type
3393 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3397 sub z3950_extended_services
{
3398 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3400 # get our connection object
3401 my $Zconn = C4
::Context
->Zconn( $server, 0, 1 );
3403 # create a new package object
3404 my $Zpackage = $Zconn->package();
3407 $Zpackage->option( action
=> $action );
3409 if ( $serviceOptions->{'databaseName'} ) {
3410 $Zpackage->option( databaseName
=> $serviceOptions->{'databaseName'} );
3412 if ( $serviceOptions->{'recordIdNumber'} ) {
3413 $Zpackage->option( recordIdNumber
=> $serviceOptions->{'recordIdNumber'} );
3415 if ( $serviceOptions->{'recordIdOpaque'} ) {
3416 $Zpackage->option( recordIdOpaque
=> $serviceOptions->{'recordIdOpaque'} );
3419 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3420 #if ($serviceType eq 'itemorder') {
3421 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3422 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3423 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3424 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3427 if ( $serviceOptions->{record
} ) {
3428 $Zpackage->option( record
=> $serviceOptions->{record
} );
3430 # can be xml or marc
3431 if ( $serviceOptions->{'syntax'} ) {
3432 $Zpackage->option( syntax
=> $serviceOptions->{'syntax'} );
3436 # send the request, handle any exception encountered
3437 eval { $Zpackage->send($serviceType) };
3438 if ( $@
&& $@
->isa("ZOOM::Exception") ) {
3439 return "error: " . $@
->code() . " " . $@
->message() . "\n";
3442 # free up package resources
3443 $Zpackage->destroy();
3446 =head2 set_service_options
3448 my $serviceOptions = set_service_options($serviceType);
3450 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3452 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3456 sub set_service_options
{
3457 my ($serviceType) = @_;
3460 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3461 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3463 if ( $serviceType eq 'commit' ) {
3467 if ( $serviceType eq 'create' ) {
3471 if ( $serviceType eq 'drop' ) {
3472 die "ERROR: 'drop' not currently supported (by Zebra)";
3474 return $serviceOptions;
3477 =head3 get_biblio_authorised_values
3479 find the types and values for all authorised values assigned to this biblio.
3483 MARC::Record of the bib
3485 returns: a hashref mapping the authorised value to the value set for this biblionumber
3487 $authorised_values = {
3488 'Scent' => 'flowery',
3489 'Audience' => 'Young Adult',
3490 'itemtypes' => 'SER',
3493 Notes: forlibrarian should probably be passed in, and called something different.
3498 sub get_biblio_authorised_values
{
3499 my $biblionumber = shift;
3502 my $forlibrarian = 1; # are we in staff or opac?
3503 my $frameworkcode = GetFrameworkCode
($biblionumber);
3505 my $authorised_values;
3507 my $tagslib = GetMarcStructure
( $forlibrarian, $frameworkcode )
3508 or return $authorised_values;
3510 # assume that these entries in the authorised_value table are bibliolevel.
3511 # ones that start with 'item%' are item level.
3512 my $query = q
(SELECT distinct authorised_value
, kohafield
3513 FROM marc_subfield_structure
3514 WHERE authorised_value
!=''
3515 AND
(kohafield like
'biblio%'
3516 OR kohafield like
'') );
3517 my $bibliolevel_authorised_values = C4
::Context
->dbh->selectall_hashref( $query, 'authorised_value' );
3519 foreach my $tag ( keys(%$tagslib) ) {
3520 foreach my $subfield ( keys( %{ $tagslib->{$tag} } ) ) {
3522 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3523 if ( 'HASH' eq ref $tagslib->{$tag}{$subfield} ) {
3524 if ( defined $tagslib->{$tag}{$subfield}{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } ) {
3525 if ( defined $record->field($tag) ) {
3526 my $this_subfield_value = $record->field($tag)->subfield($subfield);
3527 if ( defined $this_subfield_value ) {
3528 $authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } = $this_subfield_value;
3536 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3537 return $authorised_values;
3546 Koha Developement team <info@koha.org>
3548 Paul POULAIN paul.poulain@free.fr
3550 Joshua Ferraro jmf@liblime.com