3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2011 Equinox Software, Inc.
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use MARC::File::USMARC;
31 use POSIX qw(strftime);
34 use C4::Dates qw/format_date/;
35 use C4::Log; # logaction
42 use vars qw($VERSION @ISA @EXPORT);
48 @ISA = qw( Exporter );
63 &GetBiblioItemByBiblioNumber
64 &GetBiblioFromItemNumber
65 &GetBiblionumberFromItemnumber
89 &GetAuthorisedValueDesc
102 # To modify something
109 # To delete something
114 # To link headings in a bib record
115 # to authority records.
117 &LinkBibHeadingsToAuthorities
121 # those functions are exported but should not be used
122 # they are usefull is few circumstances, so are exported.
123 # but don't use them unless you're a core developer ;-)
131 &TransformHtmlToMarc2
134 &PrepareItemrecordDisplay
140 my $servers = C4::Context->config('memcached_servers');
142 require Memoize::Memcached;
143 import Memoize::Memcached qw(memoize_memcached);
146 servers => [$servers],
147 key_prefix => C4::Context->config('memcached_namespace') || 'koha',
149 memoize_memcached( 'GetMarcStructure', memcached => $memcached, expire_time => 600 ); #cache for 10 minutes
154 memoize('GetMarcStructure');
158 C4::Biblio - cataloging management functions
162 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:
166 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
168 =item 2. as raw MARC in the Zebra index and storage engine
170 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
174 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
176 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.
180 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
182 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
186 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:
190 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
192 =item 2. _koha_* - low-level internal functions for managing the koha tables
194 =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.
196 =item 4. Zebra functions used to update the Zebra index
198 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
202 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 :
206 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
208 =item 2. add the biblionumber and biblioitemnumber into the MARC records
210 =item 3. save the marc record
214 When dealing with items, we must :
218 =item 1. save the item in items table, that gives us an itemnumber
220 =item 2. add the itemnumber to the item MARC field
222 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
224 When modifying a biblio or an item, the behaviour is quite similar.
228 =head1 EXPORTED FUNCTIONS
232 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
234 Exported function (core API) for adding a new biblio to koha.
236 The first argument is a C<MARC::Record> object containing the
237 bib to add, while the second argument is the desired MARC
240 This function also accepts a third, optional argument: a hashref
241 to additional options. The only defined option is C<defer_marc_save>,
242 which if present and mapped to a true value, causes C<AddBiblio>
243 to omit the call to save the MARC in C<bibilioitems.marc>
244 and C<biblioitems.marcxml> This option is provided B<only>
245 for the use of scripts such as C<bulkmarcimport.pl> that may need
246 to do some manipulation of the MARC record for item parsing before
247 saving it and which cannot afford the performance hit of saving
248 the MARC record twice. Consequently, do not use that option
249 unless you can guarantee that C<ModBiblioMarc> will be called.
255 my $frameworkcode = shift;
256 my $options = @_ ? shift : undef;
257 my $defer_marc_save = 0;
258 if ( defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'} ) {
259 $defer_marc_save = 1;
262 my ( $biblionumber, $biblioitemnumber, $error );
263 my $dbh = C4::Context->dbh;
265 # transform the data into koha-table style data
266 SetUTF8Flag($record);
267 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
268 ( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
269 $olddata->{'biblionumber'} = $biblionumber;
270 ( $biblioitemnumber, $error ) = _koha_add_biblioitem( $dbh, $olddata );
272 _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
274 # update MARC subfield that stores biblioitems.cn_sort
275 _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
278 ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
280 logaction( "CATALOGUING", "ADD", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
281 return ( $biblionumber, $biblioitemnumber );
286 ModBiblio( $record,$biblionumber,$frameworkcode);
288 Replace an existing bib record identified by C<$biblionumber>
289 with one supplied by the MARC::Record object C<$record>. The embedded
290 item, biblioitem, and biblionumber fields from the previous
291 version of the bib record replace any such fields of those tags that
292 are present in C<$record>. Consequently, ModBiblio() is not
293 to be used to try to modify item records.
295 C<$frameworkcode> specifies the MARC framework to use
296 when storing the modified bib record; among other things,
297 this controls how MARC fields get mapped to display columns
298 in the C<biblio> and C<biblioitems> tables, as well as
299 which fields are used to store embedded item, biblioitem,
300 and biblionumber data for indexing.
305 my ( $record, $biblionumber, $frameworkcode ) = @_;
306 croak "No record" unless $record;
308 if ( C4::Context->preference("CataloguingLog") ) {
309 my $newrecord = GetMarcBiblio($biblionumber);
310 logaction( "CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>" . $newrecord->as_formatted );
313 # Cleaning up invalid fields must be done early or SetUTF8Flag is liable to
314 # throw an exception which probably won't be handled.
315 foreach my $field ($record->fields()) {
316 if (! $field->is_control_field()) {
317 if (scalar($field->subfields()) == 0 || (scalar($field->subfields()) == 1 && $field->subfield('9'))) {
318 $record->delete_field($field);
323 SetUTF8Flag($record);
324 my $dbh = C4::Context->dbh;
326 $frameworkcode = "" unless $frameworkcode;
328 _strip_item_fields($record, $frameworkcode);
330 # update biblionumber and biblioitemnumber in MARC
331 # FIXME - this is assuming a 1 to 1 relationship between
332 # biblios and biblioitems
333 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
334 $sth->execute($biblionumber);
335 my ($biblioitemnumber) = $sth->fetchrow;
337 _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
339 # load the koha-table data object
340 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
342 # update MARC subfield that stores biblioitems.cn_sort
343 _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
345 # update the MARC record (that now contains biblio and items) with the new record data
346 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
348 # modify the other koha tables
349 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
350 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
354 =head2 _strip_item_fields
356 _strip_item_fields($record, $frameworkcode)
358 Utility routine to remove item tags from a
363 sub _strip_item_fields {
365 my $frameworkcode = shift;
366 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
367 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
369 # delete any item fields from incoming record to avoid
370 # duplication or incorrect data - use AddItem() or ModItem()
372 foreach my $field ( $record->field($itemtag) ) {
373 $record->delete_field($field);
377 =head2 ModBiblioframework
379 ModBiblioframework($biblionumber,$frameworkcode);
381 Exported function to modify a biblio framework
385 sub ModBiblioframework {
386 my ( $biblionumber, $frameworkcode ) = @_;
387 my $dbh = C4::Context->dbh;
388 my $sth = $dbh->prepare( "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?" );
389 $sth->execute( $frameworkcode, $biblionumber );
395 my $error = &DelBiblio($dbh,$biblionumber);
397 Exported function (core API) for deleting a biblio in koha.
398 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
399 Also backs it up to deleted* tables
400 Checks to make sure there are not issues on any of the items
402 C<$error> : undef unless an error occurs
407 my ($biblionumber) = @_;
408 my $dbh = C4::Context->dbh;
409 my $error; # for error handling
411 # First make sure this biblio has no items attached
412 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
413 $sth->execute($biblionumber);
414 if ( my $itemnumber = $sth->fetchrow ) {
416 # Fix this to use a status the template can understand
417 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
420 return $error if $error;
422 # We delete attached subscriptions
423 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
424 foreach my $subscription (@$subscriptions) {
425 &C4::Serials::DelSubscription( $subscription->{subscriptionid} );
428 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
429 # for at least 2 reasons :
430 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
431 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
432 # 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)
434 if ( C4::Context->preference("NoZebra") ) {
436 # only NoZebra indexing needs to have
437 # the previous version of the record
438 $oldRecord = GetMarcBiblio($biblionumber);
440 ModZebra( $biblionumber, "recordDelete", "biblioserver", $oldRecord, undef );
442 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
443 $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
444 $sth->execute($biblionumber);
445 while ( my $biblioitemnumber = $sth->fetchrow ) {
447 # delete this biblioitem
448 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
449 return $error if $error;
452 # delete biblio from Koha tables and save in deletedbiblio
453 # must do this *after* _koha_delete_biblioitems, otherwise
454 # delete cascade will prevent deletedbiblioitems rows
455 # from being generated by _koha_delete_biblioitems
456 $error = _koha_delete_biblio( $dbh, $biblionumber );
458 logaction( "CATALOGUING", "DELETE", $biblionumber, "" ) if C4::Context->preference("CataloguingLog");
463 =head2 LinkBibHeadingsToAuthorities
465 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
467 Links bib headings to authority records by checking
468 each authority-controlled field in the C<MARC::Record>
469 object C<$marc>, looking for a matching authority record,
470 and setting the linking subfield $9 to the ID of that
473 If no matching authority exists, or if multiple
474 authorities match, no $9 will be added, and any
475 existing one inthe field will be deleted.
477 Returns the number of heading links changed in the
482 sub LinkBibHeadingsToAuthorities {
485 my $num_headings_changed = 0;
486 foreach my $field ( $bib->fields() ) {
487 my $heading = C4::Heading->new_from_bib_field($field);
488 next unless defined $heading;
491 my $current_link = $field->subfield('9');
493 # look for matching authorities
494 my $authorities = $heading->authorities();
496 # want only one exact match
497 if ( $#{$authorities} == 0 ) {
498 my $authority = MARC::Record->new_from_usmarc( $authorities->[0] );
499 my $authid = $authority->field('001')->data();
500 next if defined $current_link and $current_link eq $authid;
502 $field->delete_subfield( code => '9' ) if defined $current_link;
503 $field->add_subfields( '9', $authid );
504 $num_headings_changed++;
506 if ( defined $current_link ) {
507 $field->delete_subfield( code => '9' );
508 $num_headings_changed++;
513 return $num_headings_changed;
516 =head2 GetRecordValue
518 my $values = GetRecordValue($field, $record, $frameworkcode);
520 Get MARC fields from a keyword defined in fieldmapping table.
525 my ( $field, $record, $frameworkcode ) = @_;
526 my $dbh = C4::Context->dbh;
528 my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?');
529 $sth->execute( $frameworkcode, $field );
533 while ( my $row = $sth->fetchrow_hashref ) {
534 foreach my $field ( $record->field( $row->{fieldcode} ) ) {
535 if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) {
536 foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) {
537 push @result, { 'subfield' => $subfield };
540 } elsif ( $row->{subfieldcode} eq "" ) {
541 push @result, { 'subfield' => $field->as_string() };
549 =head2 SetFieldMapping
551 SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
553 Set a Field to MARC mapping value, if it already exists we don't add a new one.
557 sub SetFieldMapping {
558 my ( $framework, $field, $fieldcode, $subfieldcode ) = @_;
559 my $dbh = C4::Context->dbh;
561 my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
562 $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
563 if ( not $sth->fetchrow_hashref ) {
565 $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
567 $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
571 =head2 DeleteFieldMapping
573 DeleteFieldMapping($id);
575 Delete a field mapping from an $id.
579 sub DeleteFieldMapping {
581 my $dbh = C4::Context->dbh;
583 my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
587 =head2 GetFieldMapping
589 GetFieldMapping($frameworkcode);
591 Get all field mappings for a specified frameworkcode
595 sub GetFieldMapping {
596 my ($framework) = @_;
597 my $dbh = C4::Context->dbh;
599 my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
600 $sth->execute($framework);
603 while ( my $row = $sth->fetchrow_hashref ) {
611 $data = &GetBiblioData($biblionumber);
613 Returns information about the book with the given biblionumber.
614 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
615 the C<biblio> and C<biblioitems> tables in the
618 In addition, C<$data-E<gt>{subject}> is the list of the book's
619 subjects, separated by C<" , "> (space, comma, space).
620 If there are multiple biblioitems with the given biblionumber, only
621 the first one is considered.
627 my $dbh = C4::Context->dbh;
629 # my $query = C4::Context->preference('item-level_itypes') ?
630 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
632 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
633 # WHERE biblio.biblionumber = ?
634 # AND biblioitems.biblionumber = biblio.biblionumber
637 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
639 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
640 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
641 WHERE biblio.biblionumber = ?
642 AND biblioitems.biblionumber = biblio.biblionumber ";
644 my $sth = $dbh->prepare($query);
645 $sth->execute($bibnum);
647 $data = $sth->fetchrow_hashref;
651 } # sub GetBiblioData
653 =head2 &GetBiblioItemData
655 $itemdata = &GetBiblioItemData($biblioitemnumber);
657 Looks up the biblioitem with the given biblioitemnumber. Returns a
658 reference-to-hash. The keys are the fields from the C<biblio>,
659 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
660 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
665 sub GetBiblioItemData {
666 my ($biblioitemnumber) = @_;
667 my $dbh = C4::Context->dbh;
668 my $query = "SELECT *,biblioitems.notes AS bnotes
669 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
670 unless ( C4::Context->preference('item-level_itypes') ) {
671 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
673 $query .= " WHERE biblioitemnumber = ? ";
674 my $sth = $dbh->prepare($query);
676 $sth->execute($biblioitemnumber);
677 $data = $sth->fetchrow_hashref;
680 } # sub &GetBiblioItemData
682 =head2 GetBiblioItemByBiblioNumber
684 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
688 sub GetBiblioItemByBiblioNumber {
689 my ($biblionumber) = @_;
690 my $dbh = C4::Context->dbh;
691 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
695 $sth->execute($biblionumber);
697 while ( my $data = $sth->fetchrow_hashref ) {
698 push @results, $data;
705 =head2 GetBiblionumberFromItemnumber
710 sub GetBiblionumberFromItemnumber {
711 my ($itemnumber) = @_;
712 my $dbh = C4::Context->dbh;
713 my $sth = $dbh->prepare("Select biblionumber FROM items WHERE itemnumber = ?");
715 $sth->execute($itemnumber);
716 my ($result) = $sth->fetchrow;
720 =head2 GetBiblioFromItemNumber
722 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
724 Looks up the item with the given itemnumber. if undef, try the barcode.
726 C<&itemnodata> returns a reference-to-hash whose keys are the fields
727 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
733 sub GetBiblioFromItemNumber {
734 my ( $itemnumber, $barcode ) = @_;
735 my $dbh = C4::Context->dbh;
738 $sth = $dbh->prepare(
740 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
741 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
742 WHERE items.itemnumber = ?"
744 $sth->execute($itemnumber);
746 $sth = $dbh->prepare(
748 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
749 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
750 WHERE items.barcode = ?"
752 $sth->execute($barcode);
754 my $data = $sth->fetchrow_hashref;
761 $isbd = &GetISBDView($biblionumber);
763 Return the ISBD view which can be included in opac and intranet
768 my ( $biblionumber, $template ) = @_;
769 my $record = GetMarcBiblio($biblionumber, 1);
770 return undef unless defined $record;
771 my $itemtype = &GetFrameworkCode($biblionumber);
772 my ( $holdingbrtagf, $holdingbrtagsubf ) = &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
773 my $tagslib = &GetMarcStructure( 1, $itemtype );
775 my $ISBD = C4::Context->preference('isbd');
780 foreach my $isbdfield ( split( /#/, $bloc ) ) {
782 # $isbdfield= /(.?.?.?)/;
783 $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
784 my $fieldvalue = $1 || 0;
785 my $subfvalue = $2 || "";
787 my $analysestring = $4;
790 # warn "==> $1 / $2 / $3 / $4";
791 # my $fieldvalue=substr($isbdfield,0,3);
792 if ( $fieldvalue > 0 ) {
793 my $hasputtextbefore = 0;
794 my @fieldslist = $record->field($fieldvalue);
795 @fieldslist = sort { $a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf) } @fieldslist if ( $fieldvalue eq $holdingbrtagf );
797 # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
798 # warn "FV : $fieldvalue";
799 if ( $subfvalue ne "" ) {
800 foreach my $field (@fieldslist) {
801 foreach my $subfield ( $field->subfield($subfvalue) ) {
802 my $calculated = $analysestring;
803 my $tag = $field->tag();
806 my $subfieldvalue = GetAuthorisedValueDesc( $tag, $subfvalue, $subfield, '', $tagslib );
807 my $tagsubf = $tag . $subfvalue;
808 $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
809 if ( $template eq "opac" ) { $calculated =~ s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
811 # field builded, store the result
812 if ( $calculated && !$hasputtextbefore ) { # put textbefore if not done
813 $blocres .= $textbefore;
814 $hasputtextbefore = 1;
817 # remove punctuation at start
818 $calculated =~ s/^( |;|:|\.|-)*//g;
819 $blocres .= $calculated;
824 $blocres .= $textafter if $hasputtextbefore;
826 foreach my $field (@fieldslist) {
827 my $calculated = $analysestring;
828 my $tag = $field->tag();
831 my @subf = $field->subfields;
832 for my $i ( 0 .. $#subf ) {
833 my $valuecode = $subf[$i][1];
834 my $subfieldcode = $subf[$i][0];
835 my $subfieldvalue = GetAuthorisedValueDesc( $tag, $subf[$i][0], $subf[$i][1], '', $tagslib );
836 my $tagsubf = $tag . $subfieldcode;
838 $calculated =~ s/ # replace all {{}} codes by the value code.
839 \{\{$tagsubf\}\} # catch the {{actualcode}}
841 $valuecode # replace by the value code
844 $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
845 if ( $template eq "opac" ) { $calculated =~ s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
848 # field builded, store the result
849 if ( $calculated && !$hasputtextbefore ) { # put textbefore if not done
850 $blocres .= $textbefore;
851 $hasputtextbefore = 1;
854 # remove punctuation at start
855 $calculated =~ s/^( |;|:|\.|-)*//g;
856 $blocres .= $calculated;
859 $blocres .= $textafter if $hasputtextbefore;
862 $blocres .= $isbdfield;
867 $res =~ s/\{(.*?)\}//g;
869 $res =~ s/\n/<br\/>/g;
879 ( $count, @results ) = &GetBiblio($biblionumber);
884 my ($biblionumber) = @_;
885 my $dbh = C4::Context->dbh;
886 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
889 $sth->execute($biblionumber);
890 while ( my $data = $sth->fetchrow_hashref ) {
891 $results[$count] = $data;
895 return ( $count, @results );
898 =head2 GetBiblioItemInfosOf
900 GetBiblioItemInfosOf(@biblioitemnumbers);
904 sub GetBiblioItemInfosOf {
905 my @biblioitemnumbers = @_;
908 SELECT biblioitemnumber,
912 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
914 return get_infos_of( $query, 'biblioitemnumber' );
917 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
919 =head2 GetMarcStructure
921 $res = GetMarcStructure($forlibrarian,$frameworkcode);
923 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
924 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
925 $frameworkcode : the framework code to read
929 # cache for results of GetMarcStructure -- needed
931 our $marc_structure_cache;
933 sub GetMarcStructure {
934 my ( $forlibrarian, $frameworkcode ) = @_;
935 my $dbh = C4::Context->dbh;
936 $frameworkcode = "" unless $frameworkcode;
938 if ( defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode} ) {
939 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
942 # my $sth = $dbh->prepare(
943 # "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
944 # $sth->execute($frameworkcode);
945 # my ($total) = $sth->fetchrow;
946 # $frameworkcode = "" unless ( $total > 0 );
947 my $sth = $dbh->prepare(
948 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
949 FROM marc_tag_structure
950 WHERE frameworkcode=?
953 $sth->execute($frameworkcode);
954 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
956 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
957 $res->{$tag}->{lib} = ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
958 $res->{$tag}->{tab} = "";
959 $res->{$tag}->{mandatory} = $mandatory;
960 $res->{$tag}->{repeatable} = $repeatable;
963 $sth = $dbh->prepare(
964 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
965 FROM marc_subfield_structure
966 WHERE frameworkcode=?
967 ORDER BY tagfield,tagsubfield
971 $sth->execute($frameworkcode);
974 my $authorised_value;
985 ( $tag, $subfield, $liblibrarian, $libopac, $tab, $mandatory, $repeatable, $authorised_value,
986 $authtypecode, $value_builder, $kohafield, $seealso, $hidden, $isurl, $link, $defaultvalue
990 $res->{$tag}->{$subfield}->{lib} = ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
991 $res->{$tag}->{$subfield}->{tab} = $tab;
992 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
993 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
994 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
995 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
996 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
997 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
998 $res->{$tag}->{$subfield}->{seealso} = $seealso;
999 $res->{$tag}->{$subfield}->{hidden} = $hidden;
1000 $res->{$tag}->{$subfield}->{isurl} = $isurl;
1001 $res->{$tag}->{$subfield}->{'link'} = $link;
1002 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
1005 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
1010 =head2 GetUsedMarcStructure
1012 The same function as GetMarcStructure except it just takes field
1013 in tab 0-9. (used field)
1015 my $results = GetUsedMarcStructure($frameworkcode);
1017 C<$results> is a ref to an array which each case containts a ref
1018 to a hash which each keys is the columns from marc_subfield_structure
1020 C<$frameworkcode> is the framework code.
1024 sub GetUsedMarcStructure($) {
1025 my $frameworkcode = shift || '';
1028 FROM marc_subfield_structure
1030 AND frameworkcode = ?
1031 ORDER BY tagfield, tagsubfield
1033 my $sth = C4::Context->dbh->prepare($query);
1034 $sth->execute($frameworkcode);
1035 return $sth->fetchall_arrayref( {} );
1038 =head2 GetMarcFromKohaField
1040 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1042 Returns the MARC fields & subfields mapped to the koha field
1043 for the given frameworkcode
1047 sub GetMarcFromKohaField {
1048 my ( $kohafield, $frameworkcode ) = @_;
1049 return 0, 0 unless $kohafield and defined $frameworkcode;
1050 my $relations = C4::Context->marcfromkohafield;
1051 return ( $relations->{$frameworkcode}->{$kohafield}->[0], $relations->{$frameworkcode}->{$kohafield}->[1] );
1054 =head2 GetMarcBiblio
1056 my $record = GetMarcBiblio($biblionumber, [$embeditems]);
1058 Returns MARC::Record representing bib identified by
1059 C<$biblionumber>. If no bib exists, returns undef.
1060 C<$embeditems>. If set to true, items data are included.
1061 The MARC record contains biblio data, and items data if $embeditems is set to true.
1066 my $biblionumber = shift;
1067 my $embeditems = shift || 0;
1068 my $dbh = C4::Context->dbh;
1069 my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1070 $sth->execute($biblionumber);
1071 my $row = $sth->fetchrow_hashref;
1072 my $marcxml = StripNonXmlChars( $row->{'marcxml'} );
1073 MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') );
1074 my $record = MARC::Record->new();
1077 $record = eval { MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour') ) };
1078 if ($@) { warn " problem with :$biblionumber : $@ \n$marcxml"; }
1079 return unless $record;
1081 C4::Biblio::_koha_marc_update_bib_ids($record, '', $biblionumber, $biblionumber);
1082 C4::Biblio::EmbedItemsInMarcBiblio($record, $biblionumber) if ($embeditems);
1092 my $marcxml = GetXmlBiblio($biblionumber);
1094 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1095 The XML contains both biblio & item datas
1100 my ($biblionumber) = @_;
1101 my $dbh = C4::Context->dbh;
1102 my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1103 $sth->execute($biblionumber);
1104 my ($marcxml) = $sth->fetchrow;
1108 =head2 GetCOinSBiblio
1110 my $coins = GetCOinSBiblio($biblionumber);
1112 Returns the COinS(a span) which can be included in a biblio record
1116 sub GetCOinSBiblio {
1117 my ($biblionumber) = @_;
1118 my $record = GetMarcBiblio($biblionumber);
1120 # get the coin format
1122 # can't get a valid MARC::Record object, bail out at this point
1123 warn "We called GetMarcBiblio with a biblionumber that doesn't exist biblionumber=$biblionumber";
1126 my $pos7 = substr $record->leader(), 7, 1;
1127 my $pos6 = substr $record->leader(), 6, 1;
1130 my ( $aulast, $aufirst ) = ( '', '' );
1139 my $titletype = 'b';
1141 # For the purposes of generating COinS metadata, LDR/06-07 can be
1142 # considered the same for UNIMARC and MARC21
1147 'b' => 'manuscript',
1149 'd' => 'manuscript',
1153 'i' => 'audioRecording',
1154 'j' => 'audioRecording',
1157 'm' => 'computerProgram',
1162 'a' => 'journalArticle',
1166 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book';
1168 if ( $genre eq 'book' ) {
1169 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
1172 ##### We must transform mtx to a valable mtx and document type ####
1173 if ( $genre eq 'book' ) {
1175 } elsif ( $genre eq 'journal' ) {
1178 } elsif ( $genre eq 'journalArticle' ) {
1186 $genre = ( $mtx eq 'dc' ) ? "&rft.type=$genre" : "&rft.genre=$genre";
1188 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
1191 $aulast = $record->subfield( '700', 'a' ) || '';
1192 $aufirst = $record->subfield( '700', 'b' ) || '';
1193 $oauthors = "&rft.au=$aufirst $aulast";
1196 if ( $record->field('200') ) {
1197 for my $au ( $record->field('200')->subfield('g') ) {
1198 $oauthors .= "&rft.au=$au";
1203 ? "&rft.title=" . $record->subfield( '200', 'a' )
1204 : "&rft.title=" . $record->subfield( '200', 'a' ) . "&rft.btitle=" . $record->subfield( '200', 'a' );
1205 $pubyear = $record->subfield( '210', 'd' ) || '';
1206 $publisher = $record->subfield( '210', 'c' ) || '';
1207 $isbn = $record->subfield( '010', 'a' ) || '';
1208 $issn = $record->subfield( '011', 'a' ) || '';
1211 # MARC21 need some improve
1214 if ( $record->field('100') ) {
1215 $oauthors .= "&rft.au=" . $record->subfield( '100', 'a' );
1219 if ( $record->field('700') ) {
1220 for my $au ( $record->field('700')->subfield('a') ) {
1221 $oauthors .= "&rft.au=$au";
1224 $title = "&rft." . $titletype . "title=" . $record->subfield( '245', 'a' );
1225 $subtitle = $record->subfield( '245', 'b' ) || '';
1226 $title .= $subtitle;
1227 if ($titletype eq 'a') {
1228 $pubyear = $record->field('008') || '';
1229 $pubyear = substr($pubyear->data(), 7, 4) if $pubyear;
1230 $isbn = $record->subfield( '773', 'z' ) || '';
1231 $issn = $record->subfield( '773', 'x' ) || '';
1232 if ($mtx eq 'journal') {
1233 $title .= "&rft.title=" . (($record->subfield( '773', 't' ) || $record->subfield( '773', 'a')));
1235 $title .= "&rft.btitle=" . (($record->subfield( '773', 't' ) || $record->subfield( '773', 'a')) || '');
1237 foreach my $rel ($record->subfield( '773', 'g' )) {
1244 $pubyear = $record->subfield( '260', 'c' ) || '';
1245 $publisher = $record->subfield( '260', 'b' ) || '';
1246 $isbn = $record->subfield( '020', 'a' ) || '';
1247 $issn = $record->subfield( '022', 'a' ) || '';
1252 "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&rft.pages=$pages";
1253 $coins_value =~ s/(\ |&[^a])/\+/g;
1254 $coins_value =~ s/\"/\"\;/g;
1256 #<!-- 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="
1258 return $coins_value;
1264 return the prices in accordance with the Marc format.
1268 my ( $record, $marcflavour ) = @_;
1272 if ( $marcflavour eq "MARC21" ) {
1273 @listtags = ('345', '020');
1275 } elsif ( $marcflavour eq "UNIMARC" ) {
1276 @listtags = ('345', '010');
1282 for my $field ( $record->field(@listtags) ) {
1283 for my $subfield_value ($field->subfield($subfield)){
1285 return $subfield_value if ($subfield_value);
1288 return 0; # no price found
1291 =head2 GetMarcQuantity
1293 return the quantity of a book. Used in acquisition only, when importing a file an iso2709 from a bookseller
1294 Warning : this is not really in the marc standard. In Unimarc, Electre (the most widely used bookseller) use the 969$a
1298 sub GetMarcQuantity {
1299 my ( $record, $marcflavour ) = @_;
1303 if ( $marcflavour eq "MARC21" ) {
1305 } elsif ( $marcflavour eq "UNIMARC" ) {
1306 @listtags = ('969');
1312 for my $field ( $record->field(@listtags) ) {
1313 for my $subfield_value ($field->subfield($subfield)){
1315 if ($subfield_value) {
1316 # in France, the cents separator is the , but sometimes, ppl use a .
1317 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
1318 $subfield_value =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
1319 return $subfield_value;
1323 return 0; # no price found
1327 =head2 GetAuthorisedValueDesc
1329 my $subfieldvalue =get_authorised_value_desc(
1330 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category, $opac);
1332 Retrieve the complete description for a given authorised value.
1334 Now takes $category and $value pair too.
1336 my $auth_value_desc =GetAuthorisedValueDesc(
1337 '','', 'DVD' ,'','','CCODE');
1339 If the optional $opac parameter is set to a true value, displays OPAC
1340 descriptions rather than normal ones when they exist.
1346 sub GetAuthorisedValueDesc {
1347 my ( $tag, $subfield, $value, $framework, $tagslib, $category, $opac ) = @_;
1348 my $dbh = C4::Context->dbh;
1352 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1355 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1356 return C4::Branch::GetBranchName($value);
1360 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1361 return getitemtypeinfo($value)->{description};
1364 #---- "true" authorized value
1365 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1368 if ( $category ne "" ) {
1372 if ( $data = $_auth_val->{$category}->{$value} ) {
1373 warn "XXX auth_val hit: $category $value\n";
1375 my $sth = $dbh->prepare( "SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ?" );
1376 $sth->execute( $category, $value );
1377 $_auth_val->{$category}->{$value} = $data = $sth->fetchrow_hashref;
1379 return ( $opac && $data->{'lib_opac'} ) ? $data->{'lib_opac'} : $data->{'lib'};
1381 return $value; # if nothing is found return the original value
1385 =head2 GetMarcControlnumber
1387 $marccontrolnumber = GetMarcControlnumber($record,$marcflavour);
1389 Get the control number / record Identifier from the MARC record and return it.
1393 sub GetMarcControlnumber {
1394 my ( $record, $marcflavour ) = @_;
1395 my $controlnumber = "";
1396 # Control number or Record identifier are the same field in MARC21 and UNIMARC
1397 # Keep $marcflavour for possible later use
1398 if ($marcflavour eq "MARC21" || $marcflavour eq "UNIMARC") {
1399 my $controlnumberField = $record->field('001');
1400 if ($controlnumberField) {
1401 $controlnumber = $controlnumberField->data();
1404 return $controlnumber;
1409 $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
1411 Get all ISBNs from the MARC record and returns them in an array.
1412 ISBNs stored in differents places depending on MARC flavour
1417 my ( $record, $marcflavour ) = @_;
1419 if ( $marcflavour eq "UNIMARC" ) {
1421 } else { # assume marc21 if not unimarc
1428 foreach my $field ( $record->field($scope) ) {
1429 my $value = $field->as_string();
1430 if ( $isbn ne "" ) {
1431 $marcisbn = { marcisbn => $isbn, };
1432 push @marcisbns, $marcisbn;
1435 if ( $isbn ne $value ) {
1436 $isbn = $isbn . " " . $value;
1441 $marcisbn = { marcisbn => $isbn };
1442 push @marcisbns, $marcisbn; #load last tag into array
1449 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1451 Get all notes from the MARC record and returns them in an array.
1452 The note are stored in differents places depending on MARC flavour
1457 my ( $record, $marcflavour ) = @_;
1459 if ( $marcflavour eq "UNIMARC" ) {
1461 } else { # assume marc21 if not unimarc
1468 foreach my $field ( $record->field($scope) ) {
1469 my $value = $field->as_string();
1470 if ( $note ne "" ) {
1471 $marcnote = { marcnote => $note, };
1472 push @marcnotes, $marcnote;
1475 if ( $note ne $value ) {
1476 $note = $note . " " . $value;
1481 $marcnote = { marcnote => $note };
1482 push @marcnotes, $marcnote; #load last tag into array
1485 } # end GetMarcNotes
1487 =head2 GetMarcSubjects
1489 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1491 Get all subjects from the MARC record and returns them in an array.
1492 The subjects are stored in differents places depending on MARC flavour
1496 sub GetMarcSubjects {
1497 my ( $record, $marcflavour ) = @_;
1498 my ( $mintag, $maxtag );
1499 if ( $marcflavour eq "UNIMARC" ) {
1502 } else { # assume marc21 if not unimarc
1512 my $subject_limit = C4::Context->preference("TraceCompleteSubfields") ? 'su,complete-subfield' : 'su';
1514 foreach my $field ( $record->field('6..') ) {
1515 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1517 my @subfields = $field->subfields();
1521 # if there is an authority link, build the link with an= subfield9
1523 for my $subject_subfield (@subfields) {
1525 # don't load unimarc subfields 3,4,5
1526 next if ( ( $marcflavour eq "UNIMARC" ) and ( $subject_subfield->[0] =~ /2|3|4|5/ ) );
1528 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1529 next if ( ( $marcflavour eq "MARC21" ) and ( $subject_subfield->[0] =~ /2/ ) );
1530 my $code = $subject_subfield->[0];
1531 my $value = $subject_subfield->[1];
1532 my $linkvalue = $value;
1533 $linkvalue =~ s/(\(|\))//g;
1535 if ( $counter != 0 ) {
1536 $operator = ' and ';
1540 @link_loop = ( { 'limit' => 'an', link => "$linkvalue" } );
1542 if ( not $found9 ) {
1543 push @link_loop, { 'limit' => $subject_limit, link => $linkvalue, operator => $operator };
1546 if ( $counter != 0 ) {
1547 $separator = C4::Context->preference('authoritysep');
1551 my @this_link_loop = @link_loop;
1552 push @subfields_loop, { code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator } unless ( $subject_subfield->[0] eq 9 );
1556 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1559 return \@marcsubjects;
1560 } #end getMARCsubjects
1562 =head2 GetMarcAuthors
1564 authors = GetMarcAuthors($record,$marcflavour);
1566 Get all authors from the MARC record and returns them in an array.
1567 The authors are stored in differents places depending on MARC flavour
1571 sub GetMarcAuthors {
1572 my ( $record, $marcflavour ) = @_;
1573 my ( $mintag, $maxtag );
1575 # tagslib useful for UNIMARC author reponsabilities
1577 &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.
1578 if ( $marcflavour eq "UNIMARC" ) {
1581 } elsif ( $marcflavour eq "MARC21" || $marcflavour eq "NORMARC" ) { # assume marc21 or normarc if not unimarc
1589 foreach my $field ( $record->fields ) {
1590 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1593 my @subfields = $field->subfields();
1596 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1597 my $subfield9 = $field->subfield('9');
1598 for my $authors_subfield (@subfields) {
1600 # don't load unimarc subfields 3, 5
1601 next if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /3|5/ ) );
1602 my $subfieldcode = $authors_subfield->[0];
1603 my $value = $authors_subfield->[1];
1604 my $linkvalue = $value;
1605 $linkvalue =~ s/(\(|\))//g;
1607 if ( $count_auth != 0 ) {
1608 $operator = ' and ';
1611 # if we have an authority link, use that as the link, otherwise use standard searching
1613 @link_loop = ( { 'limit' => 'an', link => "$subfield9" } );
1616 # reset $linkvalue if UNIMARC author responsibility
1617 if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] eq "4" ) ) {
1618 $linkvalue = "(" . GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) . ")";
1620 push @link_loop, { 'limit' => 'au', link => $linkvalue, operator => $operator };
1622 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib )
1623 if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /4/ ) );
1624 my @this_link_loop = @link_loop;
1626 if ( $count_auth != 0 ) {
1627 $separator = C4::Context->preference('authoritysep');
1629 push @subfields_loop,
1630 { code => $subfieldcode,
1632 link_loop => \@this_link_loop,
1633 separator => $separator
1635 unless ( $authors_subfield->[0] eq '9' );
1638 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1640 return \@marcauthors;
1645 $marcurls = GetMarcUrls($record,$marcflavour);
1647 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1648 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1653 my ( $record, $marcflavour ) = @_;
1656 for my $field ( $record->field('856') ) {
1658 for my $note ( $field->subfield('z') ) {
1659 push @notes, { note => $note };
1661 my @urls = $field->subfield('u');
1662 foreach my $url (@urls) {
1664 if ( $marcflavour eq 'MARC21' ) {
1665 my $s3 = $field->subfield('3');
1666 my $link = $field->subfield('y');
1667 unless ( $url =~ /^\w+:/ ) {
1668 if ( $field->indicator(1) eq '7' ) {
1669 $url = $field->subfield('2') . "://" . $url;
1670 } elsif ( $field->indicator(1) eq '1' ) {
1671 $url = 'ftp://' . $url;
1674 # properly, this should be if ind1=4,
1675 # however we will assume http protocol since we're building a link.
1676 $url = 'http://' . $url;
1680 # TODO handle ind 2 (relationship)
1685 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1686 $marcurl->{'part'} = $s3 if ($link);
1687 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^[Tt]able/ );
1689 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1690 $marcurl->{'MARCURL'} = $url;
1692 push @marcurls, $marcurl;
1698 =head2 GetMarcSeries
1700 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1702 Get all series from the MARC record and returns them in an array.
1703 The series are stored in differents places depending on MARC flavour
1708 my ( $record, $marcflavour ) = @_;
1709 my ( $mintag, $maxtag );
1710 if ( $marcflavour eq "UNIMARC" ) {
1713 } else { # assume marc21 if not unimarc
1723 foreach my $field ( $record->field('440'), $record->field('490') ) {
1726 #my $value = $field->subfield('a');
1727 #$marcsubjct = {MARCSUBJCT => $value,};
1728 my @subfields = $field->subfields();
1730 #warn "subfields:".join " ", @$subfields;
1733 for my $series_subfield (@subfields) {
1735 undef $volume_number;
1737 # see if this is an instance of a volume
1738 if ( $series_subfield->[0] eq 'v' ) {
1742 my $code = $series_subfield->[0];
1743 my $value = $series_subfield->[1];
1744 my $linkvalue = $value;
1745 $linkvalue =~ s/(\(|\))//g;
1746 if ( $counter != 0 ) {
1747 push @link_loop, { link => $linkvalue, operator => ' and ', };
1749 push @link_loop, { link => $linkvalue, operator => undef, };
1752 if ( $counter != 0 ) {
1753 $separator = C4::Context->preference('authoritysep');
1755 if ($volume_number) {
1756 push @subfields_loop, { volumenum => $value };
1758 if ( $series_subfield->[0] ne '9' ) {
1759 push @subfields_loop, {
1762 link_loop => \@link_loop,
1763 separator => $separator,
1764 volumenum => $volume_number,
1770 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1772 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1773 #push @marcsubjcts, $marcsubjct;
1777 my $marcseriessarray = \@marcseries;
1778 return $marcseriessarray;
1779 } #end getMARCseriess
1783 $marchostsarray = GetMarcHosts($record,$marcflavour);
1785 Get all host records (773s MARC21, 461 UNIMARC) from the MARC record and returns them in an array.
1790 my ( $record, $marcflavour ) = @_;
1791 my ( $tag,$title_subf,$bibnumber_subf,$itemnumber_subf);
1792 $marcflavour ||="MARC21";
1793 if ( $marcflavour eq "MARC21" || $marcflavour eq "NORMARC" ) {
1796 $bibnumber_subf ="0";
1797 $itemnumber_subf='9';
1799 elsif ($marcflavour eq "UNIMARC") {
1802 $bibnumber_subf ="0";
1803 $itemnumber_subf='9';
1808 foreach my $field ( $record->field($tag)) {
1812 my $hostbiblionumber = $field->subfield("$bibnumber_subf");
1813 my $hosttitle = $field->subfield($title_subf);
1814 my $hostitemnumber=$field->subfield($itemnumber_subf);
1815 push @fields_loop, { hostbiblionumber => $hostbiblionumber, hosttitle => $hosttitle, hostitemnumber => $hostitemnumber};
1816 push @marchosts, { MARCHOSTS_FIELDS_LOOP => \@fields_loop };
1819 my $marchostsarray = \@marchosts;
1820 return $marchostsarray;
1823 =head2 GetFrameworkCode
1825 $frameworkcode = GetFrameworkCode( $biblionumber )
1829 our $_frameworkcode;
1831 sub GetFrameworkCode {
1832 my ($biblionumber) = @_;
1835 if ( $frameworkcode = $_frameworkcode->{$biblionumber} ) {
1836 warn "# _frameworkcode hit $biblionumber\n";
1837 return $frameworkcode;
1840 my $dbh = C4::Context->dbh;
1841 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1842 $sth->execute($biblionumber);
1843 my ($frameworkcode) = $sth->fetchrow;
1844 return $_frameworkcode->{$biblionumber} = $frameworkcode;
1847 =head2 TransformKohaToMarc
1849 $record = TransformKohaToMarc( $hash )
1851 This function builds partial MARC::Record from a hash
1852 Hash entries can be from biblio or biblioitems.
1854 This function is called in acquisition module, to create a basic catalogue entry from user entry
1858 sub TransformKohaToMarc {
1860 my $sth = C4::Context->dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1861 my $record = MARC::Record->new();
1862 SetMarcUnicodeFlag( $record, C4::Context->preference("marcflavour") );
1863 foreach ( keys %{$hash} ) {
1864 &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
1869 =head2 PrepHostMarcField
1871 $hostfield = PrepHostMarcField ( $hostbiblionumber,$hostitemnumber,$marcflavour )
1873 This function returns a host field populated with data from the host record, the field can then be added to an analytical record
1877 sub PrepHostMarcField {
1878 my ($hostbiblionumber,$hostitemnumber, $marcflavour) = @_;
1879 $marcflavour ||="MARC21";
1881 my $hostrecord = GetMarcBiblio($hostbiblionumber);
1882 my $item = C4::Items::GetItem($hostitemnumber);
1885 if ( $marcflavour eq "MARC21" || $marcflavour eq "NORMARC" ) {
1889 if ($hostrecord->subfield('100','a')){
1890 $mainentry = $hostrecord->subfield('100','a');
1891 } elsif ($hostrecord->subfield('110','a')){
1892 $mainentry = $hostrecord->subfield('110','a');
1894 $mainentry = $hostrecord->subfield('111','a');
1897 # qualification info
1899 if (my $field260 = $hostrecord->field('260')){
1900 $qualinfo = $field260->as_string( 'abc' );
1905 my $ed = $hostrecord->subfield('250','a');
1906 my $barcode = $item->{'barcode'};
1907 my $title = $hostrecord->subfield('245','a');
1909 # record control number, 001 with 003 and prefix
1911 if ($hostrecord->field('001')){
1912 $recctrlno = $hostrecord->field('001')->data();
1913 if ($hostrecord->field('003')){
1914 $recctrlno = '('.$hostrecord->field('003')->data().')'.$recctrlno;
1919 my $issn = $hostrecord->subfield('022','a');
1920 my $isbn = $hostrecord->subfield('020','a');
1923 $hostmarcfield = MARC::Field->new(
1925 '0' => $hostbiblionumber,
1926 '9' => $hostitemnumber,
1936 } elsif ($marcflavour eq "UNIMARC") {
1937 $hostmarcfield = MARC::Field->new(
1939 '0' => $hostbiblionumber,
1940 't' => $hostrecord->subfield('200','a'),
1941 '9' => $hostitemnumber
1945 return $hostmarcfield;
1949 =head2 TransformKohaToMarcOneField
1951 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1955 sub TransformKohaToMarcOneField {
1956 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1957 $frameworkcode = '' unless $frameworkcode;
1961 if ( !defined $sth ) {
1962 my $dbh = C4::Context->dbh;
1963 $sth = $dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1965 $sth->execute( $frameworkcode, $kohafieldname );
1966 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1967 my @values = split(/\s?\|\s?/, $value, -1);
1969 foreach my $itemvalue (@values){
1970 my $tag = $record->field($tagfield);
1972 $tag->add_subfields( $tagsubfield => $itemvalue );
1973 $record->delete_field($tag);
1974 $record->insert_fields_ordered($tag);
1977 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
1984 =head2 TransformHtmlToXml
1986 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator,
1987 $ind_tag, $auth_type )
1989 $auth_type contains :
1993 =item - nothing : rebuild a biblio. In UNIMARC the encoding is in 100$a pos 26/27
1995 =item - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1997 =item - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
2003 sub TransformHtmlToXml {
2004 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
2005 my $xml = MARC::File::XML::header('UTF-8');
2006 $xml .= "<record>\n";
2007 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
2008 MARC::File::XML->default_record_format($auth_type);
2010 # in UNIMARC, field 100 contains the encoding
2011 # check that there is one, otherwise the
2012 # MARC::Record->new_from_xml will fail (and Koha will die)
2013 my $unimarc_and_100_exist = 0;
2014 $unimarc_and_100_exist = 1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
2019 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
2021 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a" ) {
2023 # if we have a 100 field and it's values are not correct, skip them.
2024 # if we don't have any valid 100 field, we will create a default one at the end
2025 my $enc = substr( @$values[$i], 26, 2 );
2026 if ( $enc eq '01' or $enc eq '50' or $enc eq '03' ) {
2027 $unimarc_and_100_exist = 1;
2032 @$values[$i] =~ s/&/&/g;
2033 @$values[$i] =~ s/</</g;
2034 @$values[$i] =~ s/>/>/g;
2035 @$values[$i] =~ s/"/"/g;
2036 @$values[$i] =~ s/'/'/g;
2038 # if ( !utf8::is_utf8( @$values[$i] ) ) {
2039 # utf8::decode( @$values[$i] );
2041 if ( ( @$tags[$i] ne $prevtag ) ) {
2042 $j++ unless ( @$tags[$i] eq "" );
2043 my $indicator1 = eval { substr( @$indicator[$j], 0, 1 ) };
2044 my $indicator2 = eval { substr( @$indicator[$j], 1, 1 ) };
2045 my $ind1 = _default_ind_to_space($indicator1);
2047 if ( @$indicator[$j] ) {
2048 $ind2 = _default_ind_to_space($indicator2);
2050 warn "Indicator in @$tags[$i] is empty";
2054 $xml .= "</datafield>\n";
2055 if ( ( @$tags[$i] && @$tags[$i] > 10 )
2056 && ( @$values[$i] ne "" ) ) {
2057 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2058 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2064 if ( @$values[$i] ne "" ) {
2067 if ( @$tags[$i] eq "000" ) {
2068 $xml .= "<leader>@$values[$i]</leader>\n";
2071 # rest of the fixed fields
2072 } elsif ( @$tags[$i] < 10 ) {
2073 $xml .= "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
2076 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2077 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2082 } else { # @$tags[$i] eq $prevtag
2083 my $indicator1 = eval { substr( @$indicator[$j], 0, 1 ) };
2084 my $indicator2 = eval { substr( @$indicator[$j], 1, 1 ) };
2085 my $ind1 = _default_ind_to_space($indicator1);
2087 if ( @$indicator[$j] ) {
2088 $ind2 = _default_ind_to_space($indicator2);
2090 warn "Indicator in @$tags[$i] is empty";
2093 if ( @$values[$i] eq "" ) {
2096 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2099 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2102 $prevtag = @$tags[$i];
2104 $xml .= "</datafield>\n" if @$tags > 0;
2105 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist ) {
2107 # warn "SETTING 100 for $auth_type";
2108 my $string = strftime( "%Y%m%d", localtime(time) );
2110 # set 50 to position 26 is biblios, 13 if authorities
2112 $pos = 13 if $auth_type eq 'UNIMARCAUTH';
2113 $string = sprintf( "%-*s", 35, $string );
2114 substr( $string, $pos, 6, "50" );
2115 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
2116 $xml .= "<subfield code=\"a\">$string</subfield>\n";
2117 $xml .= "</datafield>\n";
2119 $xml .= "</record>\n";
2120 $xml .= MARC::File::XML::footer();
2124 =head2 _default_ind_to_space
2126 Passed what should be an indicator returns a space
2127 if its undefined or zero length
2131 sub _default_ind_to_space {
2133 if ( !defined $s || $s eq q{} ) {
2139 =head2 TransformHtmlToMarc
2141 L<$record> = TransformHtmlToMarc(L<$cgi>)
2142 L<$cgi> is the CGI object which containts the values for subfields
2144 'tag_010_indicator1_531951' ,
2145 'tag_010_indicator2_531951' ,
2146 'tag_010_code_a_531951_145735' ,
2147 'tag_010_subfield_a_531951_145735' ,
2148 'tag_200_indicator1_873510' ,
2149 'tag_200_indicator2_873510' ,
2150 'tag_200_code_a_873510_673465' ,
2151 'tag_200_subfield_a_873510_673465' ,
2152 'tag_200_code_b_873510_704318' ,
2153 'tag_200_subfield_b_873510_704318' ,
2154 'tag_200_code_e_873510_280822' ,
2155 'tag_200_subfield_e_873510_280822' ,
2156 'tag_200_code_f_873510_110730' ,
2157 'tag_200_subfield_f_873510_110730' ,
2159 L<$record> is the MARC::Record object.
2163 sub TransformHtmlToMarc {
2166 my @params = $cgi->param();
2168 # explicitly turn on the UTF-8 flag for all
2169 # 'tag_' parameters to avoid incorrect character
2170 # conversion later on
2171 my $cgi_params = $cgi->Vars;
2172 foreach my $param_name ( keys %$cgi_params ) {
2173 if ( $param_name =~ /^tag_/ ) {
2174 my $param_value = $cgi_params->{$param_name};
2175 if ( utf8::decode($param_value) ) {
2176 $cgi_params->{$param_name} = $param_value;
2179 # FIXME - need to do something if string is not valid UTF-8
2183 # creating a new record
2184 my $record = MARC::Record->new();
2187 while ( $params[$i] ) { # browse all CGI params
2188 my $param = $params[$i];
2191 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
2192 if ( $param eq 'biblionumber' ) {
2193 my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
2194 if ( $biblionumbertagfield < 10 ) {
2195 $newfield = MARC::Field->new( $biblionumbertagfield, $cgi->param($param), );
2197 $newfield = MARC::Field->new( $biblionumbertagfield, '', '', "$biblionumbertagsubfield" => $cgi->param($param), );
2199 push @fields, $newfield if ($newfield);
2200 } elsif ( $param =~ /^tag_(\d*)_indicator1_/ ) { # new field start when having 'input name="..._indicator1_..."
2203 my $ind1 = _default_ind_to_space( substr( $cgi->param($param), 0, 1 ) );
2204 my $ind2 = _default_ind_to_space( substr( $cgi->param( $params[ $i + 1 ] ), 0, 1 ) );
2208 if ( $tag < 10 ) { # no code for theses fields
2209 # in MARC editor, 000 contains the leader.
2210 if ( $tag eq '000' ) {
2211 # Force a fake leader even if not provided to avoid crashing
2212 # during decoding MARC record containing UTF-8 characters
2214 length( $cgi->param($params[$j+1]) ) == 24
2215 ? $cgi->param( $params[ $j + 1 ] )
2219 # between 001 and 009 (included)
2220 } elsif ( $cgi->param( $params[ $j + 1 ] ) ne '' ) {
2221 $newfield = MARC::Field->new( $tag, $cgi->param( $params[ $j + 1 ] ), );
2224 # > 009, deal with subfields
2226 while ( defined $params[$j] && $params[$j] =~ /_code_/ ) { # browse all it's subfield
2227 my $inner_param = $params[$j];
2229 if ( $cgi->param( $params[ $j + 1 ] ) ne '' ) { # only if there is a value (code => value)
2230 $newfield->add_subfields( $cgi->param($inner_param) => $cgi->param( $params[ $j + 1 ] ) );
2233 if ( $cgi->param( $params[ $j + 1 ] ) ne '' ) { # creating only if there is a value (code => value)
2234 $newfield = MARC::Field->new( $tag, $ind1, $ind2, $cgi->param($inner_param) => $cgi->param( $params[ $j + 1 ] ), );
2240 push @fields, $newfield if ($newfield);
2245 $record->append_fields(@fields);
2249 # cache inverted MARC field map
2250 our $inverted_field_map;
2252 =head2 TransformMarcToKoha
2254 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2256 Extract data from a MARC bib record into a hashref representing
2257 Koha biblio, biblioitems, and items fields.
2261 sub TransformMarcToKoha {
2262 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
2265 $limit_table = $limit_table || 0;
2266 $frameworkcode = '' unless defined $frameworkcode;
2268 unless ( defined $inverted_field_map ) {
2269 $inverted_field_map = _get_inverted_marc_field_map();
2273 if ( defined $limit_table && $limit_table eq 'items' ) {
2274 $tables{'items'} = 1;
2276 $tables{'items'} = 1;
2277 $tables{'biblio'} = 1;
2278 $tables{'biblioitems'} = 1;
2281 # traverse through record
2282 MARCFIELD: foreach my $field ( $record->fields() ) {
2283 my $tag = $field->tag();
2284 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2285 if ( $field->is_control_field() ) {
2286 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
2287 ENTRY: foreach my $entry ( @{$kohafields} ) {
2288 my ( $subfield, $table, $column ) = @{$entry};
2289 next ENTRY unless exists $tables{$table};
2290 my $key = _disambiguate( $table, $column );
2291 if ( $result->{$key} ) {
2292 unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $field->data() eq "" ) ) {
2293 $result->{$key} .= " | " . $field->data();
2296 $result->{$key} = $field->data();
2301 # deal with subfields
2302 MARCSUBFIELD: foreach my $sf ( $field->subfields() ) {
2303 my $code = $sf->[0];
2304 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
2305 my $value = $sf->[1];
2306 SFENTRY: foreach my $entry ( @{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} } ) {
2307 my ( $table, $column ) = @{$entry};
2308 next SFENTRY unless exists $tables{$table};
2309 my $key = _disambiguate( $table, $column );
2310 if ( $result->{$key} ) {
2311 unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $value eq "" ) ) {
2312 $result->{$key} .= " | " . $value;
2315 $result->{$key} = $value;
2322 # modify copyrightdate to keep only the 1st year found
2323 if ( exists $result->{'copyrightdate'} ) {
2324 my $temp = $result->{'copyrightdate'};
2325 $temp =~ m/c(\d\d\d\d)/;
2326 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2327 $result->{'copyrightdate'} = $1;
2328 } else { # if no cYYYY, get the 1st date.
2329 $temp =~ m/(\d\d\d\d)/;
2330 $result->{'copyrightdate'} = $1;
2334 # modify publicationyear to keep only the 1st year found
2335 if ( exists $result->{'publicationyear'} ) {
2336 my $temp = $result->{'publicationyear'};
2337 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2338 $result->{'publicationyear'} = $1;
2339 } else { # if no cYYYY, get the 1st date.
2340 $temp =~ m/(\d\d\d\d)/;
2341 $result->{'publicationyear'} = $1;
2348 sub _get_inverted_marc_field_map {
2350 my $relations = C4::Context->marcfromkohafield;
2352 foreach my $frameworkcode ( keys %{$relations} ) {
2353 foreach my $kohafield ( keys %{ $relations->{$frameworkcode} } ) {
2354 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
2355 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
2356 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2357 my ( $table, $column ) = split /[.]/, $kohafield, 2;
2358 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2359 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2365 =head2 _disambiguate
2367 $newkey = _disambiguate($table, $field);
2369 This is a temporary hack to distinguish between the
2370 following sets of columns when using TransformMarcToKoha.
2372 items.cn_source & biblioitems.cn_source
2373 items.cn_sort & biblioitems.cn_sort
2375 Columns that are currently NOT distinguished (FIXME
2376 due to lack of time to fully test) are:
2378 biblio.notes and biblioitems.notes
2383 FIXME - this is necessary because prefixing each column
2384 name with the table name would require changing lots
2385 of code and templates, and exposing more of the DB
2386 structure than is good to the UI templates, particularly
2387 since biblio and bibloitems may well merge in a future
2388 version. In the future, it would also be good to
2389 separate DB access and UI presentation field names
2394 sub CountItemsIssued {
2395 my ($biblionumber) = @_;
2396 my $dbh = C4::Context->dbh;
2397 my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2398 $sth->execute($biblionumber);
2399 my $row = $sth->fetchrow_hashref();
2400 return $row->{'issuedCount'};
2404 my ( $table, $column ) = @_;
2405 if ( $column eq "cn_sort" or $column eq "cn_source" ) {
2406 return $table . '.' . $column;
2413 =head2 get_koha_field_from_marc
2415 $result->{_disambiguate($table, $field)} =
2416 get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2418 Internal function to map data from the MARC record to a specific non-MARC field.
2419 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2423 sub get_koha_field_from_marc {
2424 my ( $koha_table, $koha_column, $record, $frameworkcode ) = @_;
2425 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table . '.' . $koha_column, $frameworkcode );
2427 foreach my $field ( $record->field($tagfield) ) {
2428 if ( $field->tag() < 10 ) {
2430 $kohafield .= " | " . $field->data();
2432 $kohafield = $field->data();
2435 if ( $field->subfields ) {
2436 my @subfields = $field->subfields();
2437 foreach my $subfieldcount ( 0 .. $#subfields ) {
2438 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2440 $kohafield .= " | " . $subfields[$subfieldcount][1];
2442 $kohafield = $subfields[$subfieldcount][1];
2452 =head2 TransformMarcToKohaOneField
2454 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2458 sub TransformMarcToKohaOneField {
2460 # FIXME ? if a field has a repeatable subfield that is used in old-db,
2461 # only the 1st will be retrieved...
2462 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2464 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $kohatable . "." . $kohafield, $frameworkcode );
2465 foreach my $field ( $record->field($tagfield) ) {
2466 if ( $field->tag() < 10 ) {
2467 if ( $result->{$kohafield} ) {
2468 $result->{$kohafield} .= " | " . $field->data();
2470 $result->{$kohafield} = $field->data();
2473 if ( $field->subfields ) {
2474 my @subfields = $field->subfields();
2475 foreach my $subfieldcount ( 0 .. $#subfields ) {
2476 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2477 if ( $result->{$kohafield} ) {
2478 $result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
2480 $result->{$kohafield} = $subfields[$subfieldcount][1];
2490 =head1 OTHER FUNCTIONS
2493 =head2 PrepareItemrecordDisplay
2495 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber,$frameworkcode);
2497 Returns a hash with all the fields for Display a given item data in a template
2499 The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
2503 sub PrepareItemrecordDisplay {
2505 my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
2507 my $dbh = C4::Context->dbh;
2508 $frameworkcode = &GetFrameworkCode($bibnum) if $bibnum;
2509 my ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2510 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2512 # return nothing if we don't have found an existing framework.
2513 return q{} unless $tagslib;
2516 $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum );
2519 my $authorised_values_sth = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib" );
2520 foreach my $tag ( sort keys %{$tagslib} ) {
2521 my $previous_tag = '';
2524 # loop through each subfield
2526 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2527 next if ( subfield_is_koha_internal_p($subfield) );
2528 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2530 $subfield_data{tag} = $tag;
2531 $subfield_data{subfield} = $subfield;
2532 $subfield_data{countsubfield} = $cntsubf++;
2533 $subfield_data{kohafield} = $tagslib->{$tag}->{$subfield}->{'kohafield'};
2535 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2536 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2537 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
2538 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
2539 $subfield_data{hidden} = "display:none"
2540 if $tagslib->{$tag}->{$subfield}->{hidden};
2541 my ( $x, $defaultvalue );
2543 ( $x, $defaultvalue ) = _find_value( $tag, $subfield, $itemrecord );
2545 $defaultvalue = $tagslib->{$tag}->{$subfield}->{defaultvalue} unless $defaultvalue;
2546 if ( !defined $defaultvalue ) {
2547 $defaultvalue = q||;
2549 $defaultvalue =~ s/"/"/g;
2551 # search for itemcallnumber if applicable
2552 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber'
2553 && C4::Context->preference('itemcallnumber') ) {
2554 my $CNtag = substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2555 my $CNsubfield = substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2557 my $temp = $itemrecord->field($CNtag);
2559 $defaultvalue = $temp->subfield($CNsubfield);
2563 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber'
2565 && $defaultvalues->{'callnumber'} ) {
2568 $temp = $itemrecord->field($subfield);
2571 $defaultvalue = $defaultvalues->{'callnumber'} if $defaultvalues;
2574 if ( ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.holdingbranch' || $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.homebranch' )
2576 && $defaultvalues->{'branchcode'} ) {
2579 $temp = $itemrecord->field($subfield);
2582 $defaultvalue = $defaultvalues->{branchcode} if $defaultvalues;
2585 if ( ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.location' )
2587 && $defaultvalues->{'location'} ) {
2588 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2590 $defaultvalue = $defaultvalues->{location} if $defaultvalues;
2593 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2594 my @authorised_values;
2597 # builds list, depending on authorised value...
2599 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
2600 if ( ( C4::Context->preference("IndependantBranches") )
2601 && ( C4::Context->userenv->{flags} % 2 != 1 ) ) {
2602 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" );
2603 $sth->execute( C4::Context->userenv->{branch} );
2604 push @authorised_values, ""
2605 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2606 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2607 push @authorised_values, $branchcode;
2608 $authorised_lib{$branchcode} = $branchname;
2611 my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches ORDER BY branchname" );
2613 push @authorised_values, ""
2614 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2615 while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2616 push @authorised_values, $branchcode;
2617 $authorised_lib{$branchcode} = $branchname;
2622 } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
2623 my $sth = $dbh->prepare( "SELECT itemtype,description FROM itemtypes ORDER BY description" );
2625 push @authorised_values, ""
2626 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2627 while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
2628 push @authorised_values, $itemtype;
2629 $authorised_lib{$itemtype} = $description;
2632 } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
2633 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2635 my $class_sources = GetClassSources();
2636 my $default_source = C4::Context->preference("DefaultClassificationSource");
2638 foreach my $class_source (sort keys %$class_sources) {
2639 next unless $class_sources->{$class_source}->{'used'} or
2640 ($class_source eq $default_source);
2641 push @authorised_values, $class_source;
2642 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
2645 #---- "true" authorised value
2647 $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
2648 push @authorised_values, ""
2649 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2650 while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
2651 push @authorised_values, $value;
2652 $authorised_lib{$value} = $lib;
2655 $subfield_data{marc_value} = CGI::scrolling_list(
2656 -name => 'field_value',
2657 -values => \@authorised_values,
2658 -default => "$defaultvalue",
2659 -labels => \%authorised_lib,
2664 } elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
2666 my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
2669 my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
2670 my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
2671 $subfield_data{random} = int(rand(1000000)); # why do we need 2 different randoms?
2672 my $index_subfield = int(rand(1000000));
2673 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
2674 $subfield_data{marc_value} = qq[<input tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255"
2675 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
2676 onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
2677 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
2680 warn "Plugin Failed: $plugin";
2681 $subfield_data{marc_value} = qq(<input tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" />); # supply default input form
2684 elsif ( $tag eq '' ) { # it's an hidden field
2685 $subfield_data{marc_value} = qq(<input type="hidden" tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" value="$defaultvalue" />);
2687 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
2688 $subfield_data{marc_value} = qq(<input type="text" tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" value="$defaultvalue" />);
2690 elsif ( length($defaultvalue) > 100
2691 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
2692 300 <= $tag && $tag < 400 && $subfield eq 'a' )
2693 or (C4::Context->preference("marcflavour") eq "MARC21" and
2694 500 <= $tag && $tag < 600 )
2696 # oversize field (textarea)
2697 $subfield_data{marc_value} = qq(<textarea tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255">$defaultvalue</textarea>\n");
2699 $subfield_data{marc_value} = "<input type=\"text\" name=\"field_value\" value=\"$defaultvalue\" size=\"50\" maxlength=\"255\" />";
2701 push( @loop_data, \%subfield_data );
2706 if ( $itemrecord && $itemrecord->field($itemtagfield) ) {
2707 $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield );
2710 'itemtagfield' => $itemtagfield,
2711 'itemtagsubfield' => $itemtagsubfield,
2712 'itemnumber' => $itemnumber,
2713 'iteminformation' => \@loop_data
2720 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2722 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2723 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2724 # =head2 ModZebrafiles
2726 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2730 # sub ModZebrafiles {
2732 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2736 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2737 # unless ( opendir( DIR, "$zebradir" ) ) {
2738 # warn "$zebradir not found";
2742 # my $filename = $zebradir . $biblionumber;
2745 # open( OUTPUT, ">", $filename . ".xml" );
2746 # print OUTPUT $record;
2753 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2755 $biblionumber is the biblionumber we want to index
2757 $op is specialUpdate or delete, and is used to know what we want to do
2759 $server is the server that we want to update
2761 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2762 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2765 $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.
2770 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2771 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2772 my $dbh = C4::Context->dbh;
2774 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2776 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2777 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2779 if ( C4::Context->preference("NoZebra") ) {
2781 # lock the nozebra table : we will read index lines, update them in Perl process
2782 # and write everything in 1 transaction.
2783 # lock the table to avoid someone else overwriting what we are doing
2784 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2785 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2786 if ( $op eq 'specialUpdate' ) {
2788 # OK, we have to add or update the record
2789 # 1st delete (virtually, in indexes), if record actually exists
2791 %result = _DelBiblioNoZebra( $biblionumber, $oldRecord, $server );
2794 # ... add the record
2795 %result = _AddBiblioNoZebra( $biblionumber, $newRecord, $server, %result );
2798 # it's a deletion, delete the record...
2799 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2800 %result = _DelBiblioNoZebra( $biblionumber, $oldRecord, $server );
2803 # ok, now update the database...
2804 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2805 foreach my $key ( keys %result ) {
2806 foreach my $index ( keys %{ $result{$key} } ) {
2807 $sth->execute( $result{$key}->{$index}, $server, $key, $index );
2810 $dbh->do('UNLOCK TABLES');
2814 # we use zebra, just fill zebraqueue table
2816 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2818 AND biblio_auth_number = ?
2821 my $check_sth = $dbh->prepare_cached($check_sql);
2822 $check_sth->execute( $server, $biblionumber, $op );
2823 my ($count) = $check_sth->fetchrow_array;
2824 $check_sth->finish();
2825 if ( $count == 0 ) {
2826 my $sth = $dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2827 $sth->execute( $biblionumber, $server, $op );
2833 =head2 GetNoZebraIndexes
2835 %indexes = GetNoZebraIndexes;
2837 return the data from NoZebraIndexes syspref.
2841 sub GetNoZebraIndexes {
2842 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2844 INDEX: foreach my $line ( split /['"],[\n\r]*/, $no_zebra_indexes ) {
2845 $line =~ /(.*)=>(.*)/;
2846 my $index = $1; # initial ' or " is removed afterwards
2848 $index =~ s/'|"|\s//g;
2849 $fields =~ s/'|"|\s//g;
2850 $indexes{$index} = $fields;
2855 =head2 EmbedItemsInMarcBiblio
2857 EmbedItemsInMarcBiblio($marc, $biblionumber);
2859 Given a MARC::Record object containing a bib record,
2860 modify it to include the items attached to it as 9XX
2861 per the bib's MARC framework.
2865 sub EmbedItemsInMarcBiblio {
2866 my ($marc, $biblionumber) = @_;
2867 croak "No MARC record" unless $marc;
2869 my $frameworkcode = GetFrameworkCode($biblionumber);
2870 _strip_item_fields($marc, $frameworkcode);
2872 # ... and embed the current items
2873 my $dbh = C4::Context->dbh;
2874 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
2875 $sth->execute($biblionumber);
2877 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2878 while (my ($itemnumber) = $sth->fetchrow_array) {
2879 my $item_marc = C4::Items::GetMarcItem($biblionumber, $itemnumber);
2880 push @item_fields, $item_marc->field($itemtag);
2882 $marc->append_fields(@item_fields);
2885 =head1 INTERNAL FUNCTIONS
2887 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2889 function to delete a biblio in NoZebra indexes
2890 This function does NOT delete anything in database : it reads all the indexes entries
2891 that have to be deleted & delete them in the hash
2893 The SQL part is done either :
2894 - after the Add if we are modifying a biblio (delete + add again)
2895 - immediatly after this sub if we are doing a true deletion.
2897 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2901 sub _DelBiblioNoZebra {
2902 my ( $biblionumber, $record, $server ) = @_;
2905 my $dbh = C4::Context->dbh;
2910 if ( $server eq 'biblioserver' ) {
2911 %index = GetNoZebraIndexes;
2913 # get title of the record (to store the 10 first letters with the index)
2914 my ( $titletag, $titlesubfield ) = GetMarcFromKohaField( 'biblio.title', '' ); # FIXME: should be GetFrameworkCode($biblionumber) ??
2915 $title = lc( $record->subfield( $titletag, $titlesubfield ) );
2918 # for authorities, the "title" is the $a mainentry
2919 my ( $auth_type_tag, $auth_type_sf ) = C4::AuthoritiesMarc::get_auth_type_location();
2920 my $authref = C4::AuthoritiesMarc::GetAuthType( $record->subfield( $auth_type_tag, $auth_type_sf ) );
2921 warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
2922 $title = $record->subfield( $authref->{auth_tag_to_report}, 'a' );
2923 $index{'mainmainentry'} = $authref->{'auth_tag_to_report'} . 'a';
2924 $index{'mainentry'} = $authref->{'auth_tag_to_report'} . '*';
2925 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2930 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2931 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2933 # limit to 10 char, should be enough, and limit the DB size
2934 $title = substr( $title, 0, 10 );
2937 my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2938 foreach my $field ( $record->fields() ) {
2940 #parse each subfield
2941 next if $field->tag < 10;
2942 foreach my $subfield ( $field->subfields() ) {
2943 my $tag = $field->tag();
2944 my $subfieldcode = $subfield->[0];
2947 # check each index to see if the subfield is stored somewhere
2948 # otherwise, store it in __RAW__ index
2949 foreach my $key ( keys %index ) {
2951 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2952 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
2954 my $line = lc $subfield->[1];
2956 # remove meaningless value in the field...
2957 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2959 # ... and split in words
2960 foreach ( split / /, $line ) {
2961 next unless $_; # skip empty values (multiple spaces)
2962 # if the entry is already here, do nothing, the biblionumber has already be removed
2963 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/ ) ) {
2965 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2966 $sth2->execute( $server, $key, $_ );
2967 my $existing_biblionumbers = $sth2->fetchrow;
2970 if ($existing_biblionumbers) {
2972 # warn " existing for $key $_: $existing_biblionumbers";
2973 $result{$key}->{$_} = $existing_biblionumbers;
2974 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2981 # the subfield is not indexed, store it in __RAW__ index anyway
2983 my $line = lc $subfield->[1];
2984 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2986 # ... and split in words
2987 foreach ( split / /, $line ) {
2988 next unless $_; # skip empty values (multiple spaces)
2989 # if the entry is already here, do nothing, the biblionumber has already be removed
2990 unless ( $result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/ ) {
2992 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2993 $sth2->execute( $server, '__RAW__', $_ );
2994 my $existing_biblionumbers = $sth2->fetchrow;
2997 if ($existing_biblionumbers) {
2998 $result{'__RAW__'}->{$_} = $existing_biblionumbers;
2999 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
3009 =head2 _AddBiblioNoZebra
3011 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
3013 function to add a biblio in NoZebra indexes
3017 sub _AddBiblioNoZebra {
3018 my ( $biblionumber, $record, $server, %result ) = @_;
3019 my $dbh = C4::Context->dbh;
3024 if ( $server eq 'biblioserver' ) {
3025 %index = GetNoZebraIndexes;
3027 # get title of the record (to store the 10 first letters with the index)
3028 my ( $titletag, $titlesubfield ) = GetMarcFromKohaField( 'biblio.title', '' ); # FIXME: should be GetFrameworkCode($biblionumber) ??
3029 $title = lc( $record->subfield( $titletag, $titlesubfield ) );
3032 # warn "server : $server";
3033 # for authorities, the "title" is the $a mainentry
3034 my ( $auth_type_tag, $auth_type_sf ) = C4::AuthoritiesMarc::get_auth_type_location();
3035 my $authref = C4::AuthoritiesMarc::GetAuthType( $record->subfield( $auth_type_tag, $auth_type_sf ) );
3036 warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
3037 $title = $record->subfield( $authref->{auth_tag_to_report}, 'a' );
3038 $index{'mainmainentry'} = $authref->{auth_tag_to_report} . 'a';
3039 $index{'mainentry'} = $authref->{auth_tag_to_report} . '*';
3040 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
3043 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
3044 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
3046 # limit to 10 char, should be enough, and limit the DB size
3047 $title = substr( $title, 0, 10 );
3050 my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
3051 foreach my $field ( $record->fields() ) {
3053 #parse each subfield
3054 ###FIXME: impossible to index a 001-009 value with NoZebra
3055 next if $field->tag < 10;
3056 foreach my $subfield ( $field->subfields() ) {
3057 my $tag = $field->tag();
3058 my $subfieldcode = $subfield->[0];
3061 # warn "INDEXING :".$subfield->[1];
3062 # check each index to see if the subfield is stored somewhere
3063 # otherwise, store it in __RAW__ index
3064 foreach my $key ( keys %index ) {
3066 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
3067 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
3069 my $line = lc $subfield->[1];
3071 # remove meaningless value in the field...
3072 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
3074 # ... and split in words
3075 foreach ( split / /, $line ) {
3076 next unless $_; # skip empty values (multiple spaces)
3077 # if the entry is already here, improve weight
3079 # warn "managing $_";
3080 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
3081 my $weight = $1 + 1;
3082 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
3083 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
3086 # get the value if it exist in the nozebra table, otherwise, create it
3087 $sth2->execute( $server, $key, $_ );
3088 my $existing_biblionumbers = $sth2->fetchrow;
3091 if ($existing_biblionumbers) {
3092 $result{$key}->{"$_"} = $existing_biblionumbers;
3093 my $weight = defined $1 ? $1 + 1 : 1;
3094 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
3095 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
3097 # create a new ligne for this entry
3100 # warn "INSERT : $server / $key / $_";
3101 $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ', indexname=' . $dbh->quote($key) . ',value=' . $dbh->quote($_) );
3102 $result{$key}->{"$_"} .= "$biblionumber,$title-1;";
3109 # the subfield is not indexed, store it in __RAW__ index anyway
3111 my $line = lc $subfield->[1];
3112 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
3114 # ... and split in words
3115 foreach ( split / /, $line ) {
3116 next unless $_; # skip empty values (multiple spaces)
3117 # if the entry is already here, improve weight
3118 my $tmpstr = $result{'__RAW__'}->{"$_"} || "";
3119 if ( $tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
3120 my $weight = $1 + 1;
3121 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
3122 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
3125 # get the value if it exist in the nozebra table, otherwise, create it
3126 $sth2->execute( $server, '__RAW__', $_ );
3127 my $existing_biblionumbers = $sth2->fetchrow;
3130 if ($existing_biblionumbers) {
3131 $result{'__RAW__'}->{"$_"} = $existing_biblionumbers;
3132 my $weight = ( $1 ? $1 : 0 ) + 1;
3133 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
3134 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
3136 # create a new ligne for this entry
3138 $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ', indexname="__RAW__",value=' . $dbh->quote($_) );
3139 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-1;";
3151 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
3153 Find the given $subfield in the given $tag in the given
3154 MARC::Record $record. If the subfield is found, returns
3155 the (indicators, value) pair; otherwise, (undef, undef) is
3159 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
3160 I suggest we export it from this module.
3165 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
3168 if ( $tagfield < 10 ) {
3169 if ( $record->field($tagfield) ) {
3170 push @result, $record->field($tagfield)->data();
3175 foreach my $field ( $record->field($tagfield) ) {
3176 my @subfields = $field->subfields();
3177 foreach my $subfield (@subfields) {
3178 if ( @$subfield[0] eq $insubfield ) {
3179 push @result, @$subfield[1];
3180 $indicator = $field->indicator(1) . $field->indicator(2);
3185 return ( $indicator, @result );
3188 =head2 _koha_marc_update_bib_ids
3191 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
3193 Internal function to add or update biblionumber and biblioitemnumber to
3198 sub _koha_marc_update_bib_ids {
3199 my ( $record, $frameworkcode, $biblionumber, $biblioitemnumber ) = @_;
3201 # we must add bibnum and bibitemnum in MARC::Record...
3202 # we build the new field with biblionumber and biblioitemnumber
3203 # we drop the original field
3204 # we add the new builded field.
3205 my ( $biblio_tag, $biblio_subfield ) = GetMarcFromKohaField( "biblio.biblionumber", $frameworkcode );
3206 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
3208 if ( $biblio_tag != $biblioitem_tag ) {
3210 # biblionumber & biblioitemnumber are in different fields
3212 # deal with biblionumber
3213 my ( $new_field, $old_field );
3214 if ( $biblio_tag < 10 ) {
3215 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
3217 $new_field = MARC::Field->new( $biblio_tag, '', '', "$biblio_subfield" => $biblionumber );
3220 # drop old field and create new one...
3221 $old_field = $record->field($biblio_tag);
3222 $record->delete_field($old_field) if $old_field;
3223 $record->insert_fields_ordered($new_field);
3225 # deal with biblioitemnumber
3226 if ( $biblioitem_tag < 10 ) {
3227 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
3229 $new_field = MARC::Field->new( $biblioitem_tag, '', '', "$biblioitem_subfield" => $biblioitemnumber, );
3232 # drop old field and create new one...
3233 $old_field = $record->field($biblioitem_tag);
3234 $record->delete_field($old_field) if $old_field;
3235 $record->insert_fields_ordered($new_field);
3239 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
3240 my $new_field = MARC::Field->new(
3241 $biblio_tag, '', '',
3242 "$biblio_subfield" => $biblionumber,
3243 "$biblioitem_subfield" => $biblioitemnumber
3246 # drop old field and create new one...
3247 my $old_field = $record->field($biblio_tag);
3248 $record->delete_field($old_field) if $old_field;
3249 $record->insert_fields_ordered($new_field);
3253 =head2 _koha_marc_update_biblioitem_cn_sort
3255 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
3257 Given a MARC bib record and the biblioitem hash, update the
3258 subfield that contains a copy of the value of biblioitems.cn_sort.
3262 sub _koha_marc_update_biblioitem_cn_sort {
3264 my $biblioitem = shift;
3265 my $frameworkcode = shift;
3267 my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.cn_sort", $frameworkcode );
3268 return unless $biblioitem_tag;
3270 my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3272 if ( my $field = $marc->field($biblioitem_tag) ) {
3273 $field->delete_subfield( code => $biblioitem_subfield );
3274 if ( $cn_sort ne '' ) {
3275 $field->add_subfields( $biblioitem_subfield => $cn_sort );
3279 # if we get here, no biblioitem tag is present in the MARC record, so
3280 # we'll create it if $cn_sort is not empty -- this would be
3281 # an odd combination of events, however
3283 $marc->insert_grouped_field( MARC::Field->new( $biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort ) );
3288 =head2 _koha_add_biblio
3290 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
3292 Internal function to add a biblio ($biblio is a hash with the values)
3296 sub _koha_add_biblio {
3297 my ( $dbh, $biblio, $frameworkcode ) = @_;
3301 # set the series flag
3302 unless (defined $biblio->{'serial'}){
3303 $biblio->{'serial'} = 0;
3304 if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
3307 my $query = "INSERT INTO biblio
3308 SET frameworkcode = ?,
3319 my $sth = $dbh->prepare($query);
3321 $frameworkcode, $biblio->{'author'}, $biblio->{'title'}, $biblio->{'unititle'}, $biblio->{'notes'},
3322 $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
3325 my $biblionumber = $dbh->{'mysql_insertid'};
3326 if ( $dbh->errstr ) {
3327 $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3333 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
3334 return ( $biblionumber, $error );
3337 =head2 _koha_modify_biblio
3339 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
3341 Internal function for updating the biblio table
3345 sub _koha_modify_biblio {
3346 my ( $dbh, $biblio, $frameworkcode ) = @_;
3351 SET frameworkcode = ?,
3360 WHERE biblionumber = ?
3363 my $sth = $dbh->prepare($query);
3366 $frameworkcode, $biblio->{'author'}, $biblio->{'title'}, $biblio->{'unititle'}, $biblio->{'notes'},
3367 $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}, $biblio->{'biblionumber'}
3368 ) if $biblio->{'biblionumber'};
3370 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
3371 $error .= "ERROR in _koha_modify_biblio $query" . $dbh->errstr;
3374 return ( $biblio->{'biblionumber'}, $error );
3377 =head2 _koha_modify_biblioitem_nonmarc
3379 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
3381 Updates biblioitems row except for marc and marcxml, which should be changed
3386 sub _koha_modify_biblioitem_nonmarc {
3387 my ( $dbh, $biblioitem ) = @_;
3390 # re-calculate the cn_sort, it may have changed
3391 my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3393 my $query = "UPDATE biblioitems
3394 SET biblionumber = ?,
3400 publicationyear = ?,
3404 collectiontitle = ?,
3406 collectionvolume= ?,
3407 editionstatement= ?,
3408 editionresponsibility = ?,
3422 where biblioitemnumber = ?
3424 my $sth = $dbh->prepare($query);
3426 $biblioitem->{'biblionumber'}, $biblioitem->{'volume'}, $biblioitem->{'number'}, $biblioitem->{'itemtype'},
3427 $biblioitem->{'isbn'}, $biblioitem->{'issn'}, $biblioitem->{'publicationyear'}, $biblioitem->{'publishercode'},
3428 $biblioitem->{'volumedate'}, $biblioitem->{'volumedesc'}, $biblioitem->{'collectiontitle'}, $biblioitem->{'collectionissn'},
3429 $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3430 $biblioitem->{'pages'}, $biblioitem->{'bnotes'}, $biblioitem->{'size'}, $biblioitem->{'place'},
3431 $biblioitem->{'lccn'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'},
3432 $biblioitem->{'cn_item'}, $biblioitem->{'cn_suffix'}, $cn_sort, $biblioitem->{'totalissues'},
3433 $biblioitem->{'biblioitemnumber'}
3435 if ( $dbh->errstr ) {
3436 $error .= "ERROR in _koha_modify_biblioitem_nonmarc $query" . $dbh->errstr;
3439 return ( $biblioitem->{'biblioitemnumber'}, $error );
3442 =head2 _koha_add_biblioitem
3444 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
3446 Internal function to add a biblioitem
3450 sub _koha_add_biblioitem {
3451 my ( $dbh, $biblioitem ) = @_;
3454 my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3455 my $query = "INSERT INTO biblioitems SET
3462 publicationyear = ?,
3466 collectiontitle = ?,
3468 collectionvolume= ?,
3469 editionstatement= ?,
3470 editionresponsibility = ?,
3486 my $sth = $dbh->prepare($query);
3488 $biblioitem->{'biblionumber'}, $biblioitem->{'volume'}, $biblioitem->{'number'}, $biblioitem->{'itemtype'},
3489 $biblioitem->{'isbn'}, $biblioitem->{'issn'}, $biblioitem->{'publicationyear'}, $biblioitem->{'publishercode'},
3490 $biblioitem->{'volumedate'}, $biblioitem->{'volumedesc'}, $biblioitem->{'collectiontitle'}, $biblioitem->{'collectionissn'},
3491 $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3492 $biblioitem->{'pages'}, $biblioitem->{'bnotes'}, $biblioitem->{'size'}, $biblioitem->{'place'},
3493 $biblioitem->{'lccn'}, $biblioitem->{'marc'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'},
3494 $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'}, $biblioitem->{'cn_suffix'}, $cn_sort,
3495 $biblioitem->{'totalissues'}
3497 my $bibitemnum = $dbh->{'mysql_insertid'};
3499 if ( $dbh->errstr ) {
3500 $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3504 return ( $bibitemnum, $error );
3507 =head2 _koha_delete_biblio
3509 $error = _koha_delete_biblio($dbh,$biblionumber);
3511 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3513 C<$dbh> - the database handle
3515 C<$biblionumber> - the biblionumber of the biblio to be deleted
3519 # FIXME: add error handling
3521 sub _koha_delete_biblio {
3522 my ( $dbh, $biblionumber ) = @_;
3524 # get all the data for this biblio
3525 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3526 $sth->execute($biblionumber);
3528 if ( my $data = $sth->fetchrow_hashref ) {
3530 # save the record in deletedbiblio
3531 # find the fields to save
3532 my $query = "INSERT INTO deletedbiblio SET ";
3534 foreach my $temp ( keys %$data ) {
3535 $query .= "$temp = ?,";
3536 push( @bind, $data->{$temp} );
3539 # replace the last , by ",?)"
3541 my $bkup_sth = $dbh->prepare($query);
3542 $bkup_sth->execute(@bind);
3546 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3547 $del_sth->execute($biblionumber);
3554 =head2 _koha_delete_biblioitems
3556 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3558 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3560 C<$dbh> - the database handle
3561 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3565 # FIXME: add error handling
3567 sub _koha_delete_biblioitems {
3568 my ( $dbh, $biblioitemnumber ) = @_;
3570 # get all the data for this biblioitem
3571 my $sth = $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3572 $sth->execute($biblioitemnumber);
3574 if ( my $data = $sth->fetchrow_hashref ) {
3576 # save the record in deletedbiblioitems
3577 # find the fields to save
3578 my $query = "INSERT INTO deletedbiblioitems SET ";
3580 foreach my $temp ( keys %$data ) {
3581 $query .= "$temp = ?,";
3582 push( @bind, $data->{$temp} );
3585 # replace the last , by ",?)"
3587 my $bkup_sth = $dbh->prepare($query);
3588 $bkup_sth->execute(@bind);
3591 # delete the biblioitem
3592 my $del_sth = $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3593 $del_sth->execute($biblioitemnumber);
3600 =head1 UNEXPORTED FUNCTIONS
3602 =head2 ModBiblioMarc
3604 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3606 Add MARC data for a biblio to koha
3608 Function exported, but should NOT be used, unless you really know what you're doing
3614 # pass the MARC::Record to this function, and it will create the records in the marc field
3615 my ( $record, $biblionumber, $frameworkcode ) = @_;
3616 my $dbh = C4::Context->dbh;
3617 my @fields = $record->fields();
3618 if ( !$frameworkcode ) {
3619 $frameworkcode = "";
3621 my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3622 $sth->execute( $frameworkcode, $biblionumber );
3624 my $encoding = C4::Context->preference("marcflavour");
3626 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3627 if ( $encoding eq "UNIMARC" ) {
3628 my $string = $record->subfield( 100, "a" );
3629 if ( ($string) && ( length( $record->subfield( 100, "a" ) ) == 36 ) ) {
3630 my $f100 = $record->field(100);
3631 $record->delete_field($f100);
3633 $string = POSIX::strftime( "%Y%m%d", localtime );
3635 $string = sprintf( "%-*s", 35, $string );
3637 substr( $string, 22, 6, "frey50" );
3638 unless ( $record->subfield( 100, "a" ) ) {
3639 $record->insert_fields_ordered( MARC::Field->new( 100, "", "", "a" => $string ) );
3643 #enhancement 5374: update transaction date (005) for marc21/unimarc
3644 if($encoding =~ /MARC21|UNIMARC/) {
3645 my @a= (localtime) [5,4,3,2,1,0]; $a[0]+=1900; $a[1]++;
3646 # YY MM DD HH MM SS (update year and month)
3647 my $f005= $record->field('005');
3648 $f005->update(sprintf("%4d%02d%02d%02d%02d%04.1f",@a)) if $f005;
3652 if ( C4::Context->preference("NoZebra") ) {
3654 # only NoZebra indexing needs to have
3655 # the previous version of the record
3656 $oldRecord = GetMarcBiblio($biblionumber);
3658 $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3659 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding), $biblionumber );
3661 ModZebra( $biblionumber, "specialUpdate", "biblioserver", $oldRecord, $record );
3662 return $biblionumber;
3665 =head2 z3950_extended_services
3667 z3950_extended_services($serviceType,$serviceOptions,$record);
3669 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.
3671 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3673 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3675 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3679 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3680 syntax => the record syntax (transfer syntax)
3681 databaseName = Database from connection object
3683 To set serviceOptions, call set_service_options($serviceType)
3685 C<$record> the record, if one is needed for the service type
3687 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3691 sub z3950_extended_services {
3692 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3694 # get our connection object
3695 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3697 # create a new package object
3698 my $Zpackage = $Zconn->package();
3701 $Zpackage->option( action => $action );
3703 if ( $serviceOptions->{'databaseName'} ) {
3704 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3706 if ( $serviceOptions->{'recordIdNumber'} ) {
3707 $Zpackage->option( recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3709 if ( $serviceOptions->{'recordIdOpaque'} ) {
3710 $Zpackage->option( recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3713 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3714 #if ($serviceType eq 'itemorder') {
3715 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3716 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3717 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3718 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3721 if ( $serviceOptions->{record} ) {
3722 $Zpackage->option( record => $serviceOptions->{record} );
3724 # can be xml or marc
3725 if ( $serviceOptions->{'syntax'} ) {
3726 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3730 # send the request, handle any exception encountered
3731 eval { $Zpackage->send($serviceType) };
3732 if ( $@ && $@->isa("ZOOM::Exception") ) {
3733 return "error: " . $@->code() . " " . $@->message() . "\n";
3736 # free up package resources
3737 $Zpackage->destroy();
3740 =head2 set_service_options
3742 my $serviceOptions = set_service_options($serviceType);
3744 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3746 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3750 sub set_service_options {
3751 my ($serviceType) = @_;
3754 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3755 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3757 if ( $serviceType eq 'commit' ) {
3761 if ( $serviceType eq 'create' ) {
3765 if ( $serviceType eq 'drop' ) {
3766 die "ERROR: 'drop' not currently supported (by Zebra)";
3768 return $serviceOptions;
3771 =head2 get_biblio_authorised_values
3773 find the types and values for all authorised values assigned to this biblio.
3777 MARC::Record of the bib
3779 returns: a hashref mapping the authorised value to the value set for this biblionumber
3781 $authorised_values = {
3782 'Scent' => 'flowery',
3783 'Audience' => 'Young Adult',
3784 'itemtypes' => 'SER',
3787 Notes: forlibrarian should probably be passed in, and called something different.
3791 sub get_biblio_authorised_values {
3792 my $biblionumber = shift;
3795 my $forlibrarian = 1; # are we in staff or opac?
3796 my $frameworkcode = GetFrameworkCode($biblionumber);
3798 my $authorised_values;
3800 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3801 or return $authorised_values;
3803 # assume that these entries in the authorised_value table are bibliolevel.
3804 # ones that start with 'item%' are item level.
3805 my $query = q(SELECT distinct authorised_value, kohafield
3806 FROM marc_subfield_structure
3807 WHERE authorised_value !=''
3808 AND (kohafield like 'biblio%'
3809 OR kohafield like '') );
3810 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3812 foreach my $tag ( keys(%$tagslib) ) {
3813 foreach my $subfield ( keys( %{ $tagslib->{$tag} } ) ) {
3815 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3816 if ( 'HASH' eq ref $tagslib->{$tag}{$subfield} ) {
3817 if ( defined $tagslib->{$tag}{$subfield}{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } ) {
3818 if ( defined $record->field($tag) ) {
3819 my $this_subfield_value = $record->field($tag)->subfield($subfield);
3820 if ( defined $this_subfield_value ) {
3821 $authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } = $this_subfield_value;
3829 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3830 return $authorised_values;
3833 =head2 CountBiblioInOrders
3836 $count = &CountBiblioInOrders( $biblionumber);
3840 This function return count of biblios in orders with $biblionumber
3844 sub CountBiblioInOrders {
3845 my ($biblionumber) = @_;
3846 my $dbh = C4::Context->dbh;
3847 my $query = "SELECT count(*)
3849 WHERE biblionumber=? AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')";
3850 my $sth = $dbh->prepare($query);
3851 $sth->execute($biblionumber);
3852 my $count = $sth->fetchrow;
3856 =head2 GetSubscriptionsId
3859 $subscriptions = &GetSubscriptionsId($biblionumber);
3863 This function return an array of subscriptionid with $biblionumber
3867 sub GetSubscriptionsId {
3868 my ($biblionumber) = @_;
3869 my $dbh = C4::Context->dbh;
3870 my $query = "SELECT subscriptionid
3872 WHERE biblionumber=?";
3873 my $sth = $dbh->prepare($query);
3874 $sth->execute($biblionumber);
3875 my @subscriptions = $sth->fetchrow_array;
3876 return (@subscriptions);
3882 $holds = &GetHolds($biblionumber);
3886 This function return the count of holds with $biblionumber
3891 my ($biblionumber) = @_;
3892 my $dbh = C4::Context->dbh;
3893 my $query = "SELECT count(*)
3895 WHERE biblionumber=?";
3896 my $sth = $dbh->prepare($query);
3897 $sth->execute($biblionumber);
3898 my $holds = $sth->fetchrow;
3909 Koha Development Team <http://koha-community.org/>
3911 Paul POULAIN paul.poulain@free.fr
3913 Joshua Ferraro jmf@liblime.com