3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
24 use MARC::File::USMARC;
25 # Force MARC::File::XML to use LibXML SAX Parser
26 #$XML::SAX::ParserPackage = "XML::LibXML::SAX";
31 use C4::Dates qw/format_date/;
32 use C4::Log; # logaction
36 use vars qw($VERSION @ISA @EXPORT);
42 @ISA = qw( Exporter );
56 &GetBiblioItemByBiblioNumber
57 &GetBiblioFromItemNumber
69 &GetAuthorisedValueDesc
73 &GetPublisherNameFromIsbn
88 # To link headings in a bib record
89 # to authority records.
91 &LinkBibHeadingsToAuthorities
95 # those functions are exported but should not be used
96 # they are usefull is few circumstances, so are exported.
97 # but don't use them unless you're a core developer ;-)
104 &TransformHtmlToMarc2
107 &PrepareItemrecordDisplay
114 C4::Biblio - cataloging management functions
118 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:
122 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
124 =item 2. as raw MARC in the Zebra index and storage engine
126 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
130 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
132 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.
136 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
138 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
142 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:
146 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
148 =item 2. _koha_* - low-level internal functions for managing the koha tables
150 =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.
152 =item 4. Zebra functions used to update the Zebra index
154 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
158 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 :
162 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
164 =item 2. add the biblionumber and biblioitemnumber into the MARC records
166 =item 3. save the marc record
170 When dealing with items, we must :
174 =item 1. save the item in items table, that gives us an itemnumber
176 =item 2. add the itemnumber to the item MARC field
178 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
180 When modifying a biblio or an item, the behaviour is quite similar.
184 =head1 EXPORTED FUNCTIONS
190 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
194 Exported function (core API) for adding a new biblio to koha.
196 The first argument is a C<MARC::Record> object containing the
197 bib to add, while the second argument is the desired MARC
200 This function also accepts a third, optional argument: a hashref
201 to additional options. The only defined option is C<defer_marc_save>,
202 which if present and mapped to a true value, causes C<AddBiblio>
203 to omit the call to save the MARC in C<bibilioitems.marc>
204 and C<biblioitems.marcxml> This option is provided B<only>
205 for the use of scripts such as C<bulkmarcimport.pl> that may need
206 to do some manipulation of the MARC record for item parsing before
207 saving it and which cannot afford the performance hit of saving
208 the MARC record twice. Consequently, do not use that option
209 unless you can guarantee that C<ModBiblioMarc> will be called.
215 my $frameworkcode = shift;
216 my $options = @_ ? shift : undef;
217 my $defer_marc_save = 0;
218 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
219 $defer_marc_save = 1;
222 my ($biblionumber,$biblioitemnumber,$error);
223 my $dbh = C4::Context->dbh;
224 # transform the data into koha-table style data
225 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
226 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
227 $olddata->{'biblionumber'} = $biblionumber;
228 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
230 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
232 # update MARC subfield that stores biblioitems.cn_sort
233 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
236 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
238 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
240 return ( $biblionumber, $biblioitemnumber );
247 ModBiblio( $record,$biblionumber,$frameworkcode);
251 Replace an existing bib record identified by C<$biblionumber>
252 with one supplied by the MARC::Record object C<$record>. The embedded
253 item, biblioitem, and biblionumber fields from the previous
254 version of the bib record replace any such fields of those tags that
255 are present in C<$record>. Consequently, ModBiblio() is not
256 to be used to try to modify item records.
258 C<$frameworkcode> specifies the MARC framework to use
259 when storing the modified bib record; among other things,
260 this controls how MARC fields get mapped to display columns
261 in the C<biblio> and C<biblioitems> tables, as well as
262 which fields are used to store embedded item, biblioitem,
263 and biblionumber data for indexing.
268 my ( $record, $biblionumber, $frameworkcode ) = @_;
269 if (C4::Context->preference("CataloguingLog")) {
270 my $newrecord = GetMarcBiblio($biblionumber);
271 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
274 my $dbh = C4::Context->dbh;
276 $frameworkcode = "" unless $frameworkcode;
278 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
279 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
280 my $oldRecord = GetMarcBiblio( $biblionumber );
282 # delete any item fields from incoming record to avoid
283 # duplication or incorrect data - use AddItem() or ModItem()
285 foreach my $field ($record->field($itemtag)) {
286 $record->delete_field($field);
289 # parse each item, and, for an unknown reason, re-encode each subfield
290 # if you don't do that, the record will have encoding mixed
291 # and the biblio will be re-encoded.
292 # strange, I (Paul P.) searched more than 1 day to understand what happends
293 # but could only solve the problem this way...
294 my @fields = $oldRecord->field( $itemtag );
295 foreach my $fielditem ( @fields ){
297 foreach ($fielditem->subfields()) {
299 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
301 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
304 $record->append_fields($field);
307 # update biblionumber and biblioitemnumber in MARC
308 # FIXME - this is assuming a 1 to 1 relationship between
309 # biblios and biblioitems
310 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
311 $sth->execute($biblionumber);
312 my ($biblioitemnumber) = $sth->fetchrow;
314 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
316 # load the koha-table data object
317 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
319 # update MARC subfield that stores biblioitems.cn_sort
320 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
322 # update the MARC record (that now contains biblio and items) with the new record data
323 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
325 # modify the other koha tables
326 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
327 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
331 =head2 ModBiblioframework
333 ModBiblioframework($biblionumber,$frameworkcode);
334 Exported function to modify a biblio framework
338 sub ModBiblioframework {
339 my ( $biblionumber, $frameworkcode ) = @_;
340 my $dbh = C4::Context->dbh;
341 my $sth = $dbh->prepare(
342 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
344 $sth->execute($frameworkcode, $biblionumber);
352 my $error = &DelBiblio($dbh,$biblionumber);
353 Exported function (core API) for deleting a biblio in koha.
354 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
355 Also backs it up to deleted* tables
356 Checks to make sure there are not issues on any of the items
358 C<$error> : undef unless an error occurs
365 my ( $biblionumber ) = @_;
366 my $dbh = C4::Context->dbh;
367 my $error; # for error handling
369 # First make sure this biblio has no items attached
370 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
371 $sth->execute($biblionumber);
372 if (my $itemnumber = $sth->fetchrow){
373 # Fix this to use a status the template can understand
374 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
377 return $error if $error;
379 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
380 # for at least 2 reasons :
381 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
382 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
383 # 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)
385 if (C4::Context->preference("NoZebra")) {
386 # only NoZebra indexing needs to have
387 # the previous version of the record
388 $oldRecord = GetMarcBiblio($biblionumber);
390 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
392 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
395 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
396 $sth->execute($biblionumber);
397 while ( my $biblioitemnumber = $sth->fetchrow ) {
399 # delete this biblioitem
400 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
401 return $error if $error;
404 # delete biblio from Koha tables and save in deletedbiblio
405 # must do this *after* _koha_delete_biblioitems, otherwise
406 # delete cascade will prevent deletedbiblioitems rows
407 # from being generated by _koha_delete_biblioitems
408 $error = _koha_delete_biblio( $dbh, $biblionumber );
410 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
415 =head2 LinkBibHeadingsToAuthorities
419 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
423 Links bib headings to authority records by checking
424 each authority-controlled field in the C<MARC::Record>
425 object C<$marc>, looking for a matching authority record,
426 and setting the linking subfield $9 to the ID of that
429 If no matching authority exists, or if multiple
430 authorities match, no $9 will be added, and any
431 existing one inthe field will be deleted.
433 Returns the number of heading links changed in the
438 sub LinkBibHeadingsToAuthorities {
441 my $num_headings_changed = 0;
442 foreach my $field ($bib->fields()) {
443 my $heading = C4::Heading->new_from_bib_field($field);
444 next unless defined $heading;
447 my $current_link = $field->subfield('9');
449 # look for matching authorities
450 my $authorities = $heading->authorities();
452 # want only one exact match
453 if ($#{ $authorities } == 0) {
454 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
455 my $authid = $authority->field('001')->data();
456 next if defined $current_link and $current_link eq $authid;
458 $field->delete_subfield(code => '9') if defined $current_link;
459 $field->add_subfields('9', $authid);
460 $num_headings_changed++;
462 if (defined $current_link) {
463 $field->delete_subfield(code => '9');
464 $num_headings_changed++;
469 return $num_headings_changed;
476 $data = &GetBiblioData($biblionumber);
477 Returns information about the book with the given biblionumber.
478 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
479 the C<biblio> and C<biblioitems> tables in the
481 In addition, C<$data-E<gt>{subject}> is the list of the book's
482 subjects, separated by C<" , "> (space, comma, space).
483 If there are multiple biblioitems with the given biblionumber, only
484 the first one is considered.
492 my $dbh = C4::Context->dbh;
494 # my $query = C4::Context->preference('item-level_itypes') ?
495 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
497 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
498 # WHERE biblio.biblionumber = ?
499 # AND biblioitems.biblionumber = biblio.biblionumber
502 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
504 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
505 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
506 WHERE biblio.biblionumber = ?
507 AND biblioitems.biblionumber = biblio.biblionumber ";
509 my $sth = $dbh->prepare($query);
510 $sth->execute($bibnum);
512 $data = $sth->fetchrow_hashref;
516 } # sub GetBiblioData
518 =head2 &GetBiblioItemData
522 $itemdata = &GetBiblioItemData($biblioitemnumber);
524 Looks up the biblioitem with the given biblioitemnumber. Returns a
525 reference-to-hash. The keys are the fields from the C<biblio>,
526 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
527 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
534 sub GetBiblioItemData {
535 my ($biblioitemnumber) = @_;
536 my $dbh = C4::Context->dbh;
537 my $query = "SELECT *,biblioitems.notes AS bnotes
538 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
539 unless(C4::Context->preference('item-level_itypes')) {
540 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
542 $query .= " WHERE biblioitemnumber = ? ";
543 my $sth = $dbh->prepare($query);
545 $sth->execute($biblioitemnumber);
546 $data = $sth->fetchrow_hashref;
549 } # sub &GetBiblioItemData
551 =head2 GetBiblioItemByBiblioNumber
555 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
561 sub GetBiblioItemByBiblioNumber {
562 my ($biblionumber) = @_;
563 my $dbh = C4::Context->dbh;
564 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
568 $sth->execute($biblionumber);
570 while ( my $data = $sth->fetchrow_hashref ) {
571 push @results, $data;
578 =head2 GetBiblioFromItemNumber
582 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
584 Looks up the item with the given itemnumber. if undef, try the barcode.
586 C<&itemnodata> returns a reference-to-hash whose keys are the fields
587 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
595 sub GetBiblioFromItemNumber {
596 my ( $itemnumber, $barcode ) = @_;
597 my $dbh = C4::Context->dbh;
600 $sth=$dbh->prepare( "SELECT * FROM items
601 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
602 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
603 WHERE items.itemnumber = ?") ;
604 $sth->execute($itemnumber);
606 $sth=$dbh->prepare( "SELECT * FROM items
607 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
608 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
609 WHERE items.barcode = ?") ;
610 $sth->execute($barcode);
612 my $data = $sth->fetchrow_hashref;
621 ( $count, @results ) = &GetBiblio($biblionumber);
628 my ($biblionumber) = @_;
629 my $dbh = C4::Context->dbh;
630 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
633 $sth->execute($biblionumber);
634 while ( my $data = $sth->fetchrow_hashref ) {
635 $results[$count] = $data;
639 return ( $count, @results );
642 =head2 GetBiblioItemInfosOf
646 GetBiblioItemInfosOf(@biblioitemnumbers);
652 sub GetBiblioItemInfosOf {
653 my @biblioitemnumbers = @_;
656 SELECT biblioitemnumber,
660 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
662 return get_infos_of( $query, 'biblioitemnumber' );
665 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
667 =head2 GetMarcStructure
671 $res = GetMarcStructure($forlibrarian,$frameworkcode);
673 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
674 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
675 $frameworkcode : the framework code to read
681 # cache for results of GetMarcStructure -- needed
683 our $marc_structure_cache;
685 sub GetMarcStructure {
686 my ( $forlibrarian, $frameworkcode ) = @_;
687 my $dbh=C4::Context->dbh;
688 $frameworkcode = "" unless $frameworkcode;
690 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
691 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
695 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
697 # check that framework exists
700 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
701 $sth->execute($frameworkcode);
702 my ($total) = $sth->fetchrow;
703 $frameworkcode = "" unless ( $total > 0 );
706 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
707 FROM marc_tag_structure
708 WHERE frameworkcode=?
711 $sth->execute($frameworkcode);
712 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
714 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
717 $res->{$tag}->{lib} =
718 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
719 $res->{$tag}->{tab} = "";
720 $res->{$tag}->{mandatory} = $mandatory;
721 $res->{$tag}->{repeatable} = $repeatable;
726 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
727 FROM marc_subfield_structure
728 WHERE frameworkcode=?
729 ORDER BY tagfield,tagsubfield
733 $sth->execute($frameworkcode);
736 my $authorised_value;
748 $tag, $subfield, $liblibrarian,
750 $mandatory, $repeatable, $authorised_value,
751 $authtypecode, $value_builder, $kohafield,
752 $seealso, $hidden, $isurl,
758 $res->{$tag}->{$subfield}->{lib} =
759 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
760 $res->{$tag}->{$subfield}->{tab} = $tab;
761 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
762 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
763 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
764 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
765 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
766 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
767 $res->{$tag}->{$subfield}->{seealso} = $seealso;
768 $res->{$tag}->{$subfield}->{hidden} = $hidden;
769 $res->{$tag}->{$subfield}->{isurl} = $isurl;
770 $res->{$tag}->{$subfield}->{'link'} = $link;
771 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
774 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
779 =head2 GetUsedMarcStructure
781 the same function as GetMarcStructure expcet it just take field
782 in tab 0-9. (used field)
784 my $results = GetUsedMarcStructure($frameworkcode);
786 L<$results> is a ref to an array which each case containts a ref
787 to a hash which each keys is the columns from marc_subfield_structure
789 L<$frameworkcode> is the framework code.
793 sub GetUsedMarcStructure($){
794 my $frameworkcode = shift || '';
795 my $dbh = C4::Context->dbh;
798 FROM marc_subfield_structure
800 AND frameworkcode = ?
803 my $sth = $dbh->prepare($query);
804 $sth->execute($frameworkcode);
805 while (my $row = $sth->fetchrow_hashref){
811 =head2 GetMarcFromKohaField
815 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
816 Returns the MARC fields & subfields mapped to the koha field
817 for the given frameworkcode
823 sub GetMarcFromKohaField {
824 my ( $kohafield, $frameworkcode ) = @_;
825 return 0, 0 unless $kohafield and defined $frameworkcode;
826 my $relations = C4::Context->marcfromkohafield;
828 $relations->{$frameworkcode}->{$kohafield}->[0],
829 $relations->{$frameworkcode}->{$kohafield}->[1]
837 my $record = GetMarcBiblio($biblionumber);
841 Returns MARC::Record representing bib identified by
842 C<$biblionumber>. If no bib exists, returns undef.
843 The MARC record contains both biblio & item data.
848 my $biblionumber = shift;
849 my $dbh = C4::Context->dbh;
851 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
852 $sth->execute($biblionumber);
853 my $row = $sth->fetchrow_hashref;
854 my $marcxml = StripNonXmlChars($row->{'marcxml'});
855 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
856 my $record = MARC::Record->new();
858 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
859 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
860 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
871 my $marcxml = GetXmlBiblio($biblionumber);
873 Returns biblioitems.marcxml of the biblionumber passed in parameter.
874 The XML contains both biblio & item datas
881 my ( $biblionumber ) = @_;
882 my $dbh = C4::Context->dbh;
884 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
885 $sth->execute($biblionumber);
886 my ($marcxml) = $sth->fetchrow;
890 =head2 GetCOinSBiblio
894 my $coins = GetCOinSBiblio($biblionumber);
896 Returns the COinS(a span) which can be included in a biblio record
903 my ( $biblionumber ) = @_;
904 my $record = GetMarcBiblio($biblionumber);
906 # get the coin format
907 my $pos7 = substr $record->leader(), 7,1;
908 my $pos6 = substr $record->leader(), 6,1;
911 my ($aulast, $aufirst);
919 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
930 'i' => 'audioRecording',
931 'j' => 'audioRecording',
934 'm' => 'computerProgram',
939 'a' => 'journalArticle',
943 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
945 if( $genre eq 'book' ){
946 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
949 ##### We must transform mtx to a valable mtx and document type ####
950 if( $genre eq 'book' ){
952 }elsif( $genre eq 'journal' ){
954 }elsif( $genre eq 'journalArticle' ){
961 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
964 $aulast = $record->subfield('700','a');
965 $aufirst = $record->subfield('700','b');
966 $oauthors = "&rft.au=$aufirst $aulast";
968 if($record->field('200')){
969 for my $au ($record->field('200')->subfield('g')){
970 $oauthors .= "&rft.au=$au";
973 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
974 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
975 $pubyear = $record->subfield('210','d');
976 $publisher = $record->subfield('210','c');
977 $isbn = $record->subfield('010','a');
978 $issn = $record->subfield('011','a');
980 # MARC21 need some improve
983 $genre = "&rft.genre=book";
986 $oauthors .= "&rft.au=".$record->subfield('100','a');
988 if($record->field('700')){
989 for my $au ($record->field('700')->subfield('a')){
990 $oauthors .= "&rft.au=$au";
993 $title = "&rft.btitle=".$record->subfield('245','a');
994 $pubyear = $record->subfield('260','c');
995 $publisher = $record->subfield('260','b');
996 $isbn = $record->subfield('020','a');
997 $issn = $record->subfield('022','a');
1000 my $coins_value = "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";
1001 $coins_value =~ s/\ /\+/g;
1002 #<!-- 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="
1004 return $coins_value;
1007 =head2 GetAuthorisedValueDesc
1011 my $subfieldvalue =get_authorised_value_desc(
1012 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1013 Retrieve the complete description for a given authorised value.
1015 Now takes $category and $value pair too.
1016 my $auth_value_desc =GetAuthorisedValueDesc(
1017 '','', 'DVD' ,'','','CCODE');
1023 sub GetAuthorisedValueDesc {
1024 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1025 my $dbh = C4::Context->dbh;
1029 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1032 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1033 return C4::Branch::GetBranchName($value);
1037 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1038 return getitemtypeinfo($value)->{description};
1041 #---- "true" authorized value
1042 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1045 if ( $category ne "" ) {
1048 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1050 $sth->execute( $category, $value );
1051 my $data = $sth->fetchrow_hashref;
1052 return $data->{'lib'};
1055 return $value; # if nothing is found return the original value
1063 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1064 Get all notes from the MARC record and returns them in an array.
1065 The note are stored in differents places depending on MARC flavour
1072 my ( $record, $marcflavour ) = @_;
1074 if ( $marcflavour eq "MARC21" ) {
1077 else { # assume unimarc if not marc21
1084 foreach my $field ( $record->field($scope) ) {
1085 my $value = $field->as_string();
1086 $value =~ s/\n/<br \/>/g ;
1088 if ( $note ne "" ) {
1089 $marcnote = { marcnote => $note, };
1090 push @marcnotes, $marcnote;
1093 if ( $note ne $value ) {
1094 $note = $note . " " . $value;
1099 $marcnote = { marcnote => $note };
1100 push @marcnotes, $marcnote; #load last tag into array
1103 } # end GetMarcNotes
1105 =head2 GetMarcSubjects
1109 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1110 Get all subjects from the MARC record and returns them in an array.
1111 The subjects are stored in differents places depending on MARC flavour
1117 sub GetMarcSubjects {
1118 my ( $record, $marcflavour ) = @_;
1119 my ( $mintag, $maxtag );
1120 if ( $marcflavour eq "MARC21" ) {
1124 else { # assume unimarc if not marc21
1134 foreach my $field ( $record->field('6..' )) {
1135 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1137 my @subfields = $field->subfields();
1140 # if there is an authority link, build the link with an= subfield9
1141 my $subfield9 = $field->subfield('9');
1142 for my $subject_subfield (@subfields ) {
1143 # don't load unimarc subfields 3,4,5
1144 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1145 my $code = $subject_subfield->[0];
1146 my $value = $subject_subfield->[1];
1147 my $linkvalue = $value;
1148 $linkvalue =~ s/(\(|\))//g;
1149 my $operator = " and " unless $counter==0;
1151 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1153 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1155 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1157 my @this_link_loop = @link_loop;
1158 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1162 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1165 return \@marcsubjects;
1166 } #end getMARCsubjects
1168 =head2 GetMarcAuthors
1172 authors = GetMarcAuthors($record,$marcflavour);
1173 Get all authors from the MARC record and returns them in an array.
1174 The authors are stored in differents places depending on MARC flavour
1180 sub GetMarcAuthors {
1181 my ( $record, $marcflavour ) = @_;
1182 my ( $mintag, $maxtag );
1183 # tagslib useful for UNIMARC author reponsabilities
1184 my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
1185 if ( $marcflavour eq "MARC21" ) {
1189 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1198 foreach my $field ( $record->fields ) {
1199 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1202 my @subfields = $field->subfields();
1204 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1205 my $subfield9 = $field->subfield('9');
1206 for my $authors_subfield (@subfields) {
1207 # don't load unimarc subfields 3, 5
1208 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1209 my $subfieldcode = $authors_subfield->[0];
1210 my $value = $authors_subfield->[1];
1211 my $linkvalue = $value;
1212 $linkvalue =~ s/(\(|\))//g;
1213 my $operator = " and " unless $count_auth==0;
1214 # if we have an authority link, use that as the link, otherwise use standard searching
1216 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1219 # reset $linkvalue if UNIMARC author responsibility
1220 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1221 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1223 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1225 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1226 my @this_link_loop = @link_loop;
1227 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1228 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1231 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1233 return \@marcauthors;
1240 $marcurls = GetMarcUrls($record,$marcflavour);
1241 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1242 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1249 my ($record, $marcflavour) = @_;
1251 for my $field ($record->field('856')) {
1253 my $url = $field->subfield('u');
1255 for my $note ( $field->subfield('z')) {
1256 push @notes , {note => $note};
1258 if($marcflavour eq 'MARC21') {
1259 my $s3 = $field->subfield('3');
1260 my $link = $field->subfield('y');
1261 unless($url =~ /^\w+:/) {
1262 if($field->indicator(1) eq '7') {
1263 $url = $field->subfield('2') . "://" . $url;
1264 } elsif ($field->indicator(1) eq '1') {
1265 $url = 'ftp://' . $url;
1267 # properly, this should be if ind1=4,
1268 # however we will assume http protocol since we're building a link.
1269 $url = 'http://' . $url;
1272 # TODO handle ind 2 (relationship)
1273 $marcurl = { MARCURL => $url,
1276 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url ;;
1277 $marcurl->{'part'} = $s3 if($link);
1278 $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
1280 $marcurl->{'linktext'} = $field->subfield('z') || C4::Context->preference('URLLinkText') || $url;
1281 $marcurl->{'MARCURL'} = $url ;
1283 push @marcurls, $marcurl;
1288 =head2 GetMarcSeries
1292 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1293 Get all series from the MARC record and returns them in an array.
1294 The series are stored in differents places depending on MARC flavour
1301 my ($record, $marcflavour) = @_;
1302 my ($mintag, $maxtag);
1303 if ($marcflavour eq "MARC21") {
1306 } else { # assume unimarc if not marc21
1316 foreach my $field ($record->field('440'), $record->field('490')) {
1318 #my $value = $field->subfield('a');
1319 #$marcsubjct = {MARCSUBJCT => $value,};
1320 my @subfields = $field->subfields();
1321 #warn "subfields:".join " ", @$subfields;
1324 for my $series_subfield (@subfields) {
1326 undef $volume_number;
1327 # see if this is an instance of a volume
1328 if ($series_subfield->[0] eq 'v') {
1332 my $code = $series_subfield->[0];
1333 my $value = $series_subfield->[1];
1334 my $linkvalue = $value;
1335 $linkvalue =~ s/(\(|\))//g;
1336 my $operator = " and " unless $counter==0;
1337 push @link_loop, {link => $linkvalue, operator => $operator };
1338 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1339 if ($volume_number) {
1340 push @subfields_loop, {volumenum => $value};
1343 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1347 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1348 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1349 #push @marcsubjcts, $marcsubjct;
1353 my $marcseriessarray=\@marcseries;
1354 return $marcseriessarray;
1355 } #end getMARCseriess
1357 =head2 GetFrameworkCode
1361 $frameworkcode = GetFrameworkCode( $biblionumber )
1367 sub GetFrameworkCode {
1368 my ( $biblionumber ) = @_;
1369 my $dbh = C4::Context->dbh;
1370 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1371 $sth->execute($biblionumber);
1372 my ($frameworkcode) = $sth->fetchrow;
1373 return $frameworkcode;
1376 =head2 GetPublisherNameFromIsbn
1378 $name = GetPublishercodeFromIsbn($isbn);
1385 sub GetPublisherNameFromIsbn($){
1387 $isbn =~ s/[- _]//g;
1389 my @codes = (split '-', DisplayISBN($isbn));
1390 my $code = $codes[0].$codes[1].$codes[2];
1391 my $dbh = C4::Context->dbh;
1393 SELECT distinct publishercode
1396 AND publishercode IS NOT NULL
1399 my $sth = $dbh->prepare($query);
1400 $sth->execute("$code%");
1401 my $name = $sth->fetchrow;
1402 return $name if length $name;
1406 =head2 TransformKohaToMarc
1410 $record = TransformKohaToMarc( $hash )
1411 This function builds partial MARC::Record from a hash
1412 Hash entries can be from biblio or biblioitems.
1413 This function is called in acquisition module, to create a basic catalogue entry from user entry
1419 sub TransformKohaToMarc {
1422 my $dbh = C4::Context->dbh;
1425 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1427 my $record = MARC::Record->new();
1428 foreach (keys %{$hash}) {
1429 &TransformKohaToMarcOneField( $sth, $record, $_,
1435 =head2 TransformKohaToMarcOneField
1439 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1445 sub TransformKohaToMarcOneField {
1446 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1447 $frameworkcode='' unless $frameworkcode;
1451 if ( !defined $sth ) {
1452 my $dbh = C4::Context->dbh;
1453 $sth = $dbh->prepare(
1454 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1457 $sth->execute( $frameworkcode, $kohafieldname );
1458 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1459 my $tag = $record->field($tagfield);
1461 $tag->update( $tagsubfield => $value );
1462 $record->delete_field($tag);
1463 $record->insert_fields_ordered($tag);
1466 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1472 =head2 TransformHtmlToXml
1476 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1478 $auth_type contains :
1479 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1480 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1481 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1487 sub TransformHtmlToXml {
1488 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1489 my $xml = MARC::File::XML::header('UTF-8');
1490 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1491 MARC::File::XML->default_record_format($auth_type);
1492 # in UNIMARC, field 100 contains the encoding
1493 # check that there is one, otherwise the
1494 # MARC::Record->new_from_xml will fail (and Koha will die)
1495 my $unimarc_and_100_exist=0;
1496 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1501 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1502 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1503 # if we have a 100 field and it's values are not correct, skip them.
1504 # if we don't have any valid 100 field, we will create a default one at the end
1505 my $enc = substr( @$values[$i], 26, 2 );
1506 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1507 $unimarc_and_100_exist=1;
1512 @$values[$i] =~ s/&/&/g;
1513 @$values[$i] =~ s/</</g;
1514 @$values[$i] =~ s/>/>/g;
1515 @$values[$i] =~ s/"/"/g;
1516 @$values[$i] =~ s/'/'/g;
1517 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1518 # utf8::decode( @$values[$i] );
1520 if ( ( @$tags[$i] ne $prevtag ) ) {
1521 $j++ unless ( @$tags[$i] eq "" );
1523 $xml .= "</datafield>\n";
1524 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1525 && ( @$values[$i] ne "" ) )
1527 my $ind1 = substr( @$indicator[$j], 0, 1 );
1529 if ( @$indicator[$j] ) {
1530 $ind2 = substr( @$indicator[$j], 1, 1 );
1533 warn "Indicator in @$tags[$i] is empty";
1537 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1539 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1547 if ( @$values[$i] ne "" ) {
1550 if ( @$tags[$i] eq "000" ) {
1551 $xml .= "<leader>@$values[$i]</leader>\n";
1554 # rest of the fixed fields
1556 elsif ( @$tags[$i] < 10 ) {
1558 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1562 my $ind1 = substr( @$indicator[$j], 0, 1 );
1563 my $ind2 = substr( @$indicator[$j], 1, 1 );
1565 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1567 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1573 else { # @$tags[$i] eq $prevtag
1574 if ( @$values[$i] eq "" ) {
1578 my $ind1 = substr( @$indicator[$j], 0, 1 );
1579 my $ind2 = substr( @$indicator[$j], 1, 1 );
1581 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1585 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1588 $prevtag = @$tags[$i];
1590 $xml .= "</datafield>\n" if @$tags > 0;
1591 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1592 # warn "SETTING 100 for $auth_type";
1593 use POSIX qw(strftime);
1594 my $string = strftime( "%Y%m%d", localtime(time) );
1595 # set 50 to position 26 is biblios, 13 if authorities
1597 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1598 $string = sprintf( "%-*s", 35, $string );
1599 substr( $string, $pos , 6, "50" );
1600 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1601 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1602 $xml .= "</datafield>\n";
1604 $xml .= MARC::File::XML::footer();
1608 =head2 TransformHtmlToMarc
1610 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1611 L<$params> is a ref to an array as below:
1613 'tag_010_indicator1_531951' ,
1614 'tag_010_indicator2_531951' ,
1615 'tag_010_code_a_531951_145735' ,
1616 'tag_010_subfield_a_531951_145735' ,
1617 'tag_200_indicator1_873510' ,
1618 'tag_200_indicator2_873510' ,
1619 'tag_200_code_a_873510_673465' ,
1620 'tag_200_subfield_a_873510_673465' ,
1621 'tag_200_code_b_873510_704318' ,
1622 'tag_200_subfield_b_873510_704318' ,
1623 'tag_200_code_e_873510_280822' ,
1624 'tag_200_subfield_e_873510_280822' ,
1625 'tag_200_code_f_873510_110730' ,
1626 'tag_200_subfield_f_873510_110730' ,
1628 L<$cgi> is the CGI object which containts the value.
1629 L<$record> is the MARC::Record object.
1633 sub TransformHtmlToMarc {
1637 # explicitly turn on the UTF-8 flag for all
1638 # 'tag_' parameters to avoid incorrect character
1639 # conversion later on
1640 my $cgi_params = $cgi->Vars;
1641 foreach my $param_name (keys %$cgi_params) {
1642 if ($param_name =~ /^tag_/) {
1643 my $param_value = $cgi_params->{$param_name};
1644 if (utf8::decode($param_value)) {
1645 $cgi_params->{$param_name} = $param_value;
1647 # FIXME - need to do something if string is not valid UTF-8
1651 # creating a new record
1652 my $record = MARC::Record->new();
1655 while ($params->[$i]){ # browse all CGI params
1656 my $param = $params->[$i];
1658 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1659 if ($param eq 'biblionumber') {
1660 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1661 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1662 if ($biblionumbertagfield < 10) {
1663 $newfield = MARC::Field->new(
1664 $biblionumbertagfield,
1665 $cgi->param($param),
1668 $newfield = MARC::Field->new(
1669 $biblionumbertagfield,
1672 "$biblionumbertagsubfield" => $cgi->param($param),
1675 push @fields,$newfield if($newfield);
1677 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1680 my $ind1 = substr($cgi->param($param),0,1);
1681 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1685 if($tag < 10){ # no code for theses fields
1686 # in MARC editor, 000 contains the leader.
1687 if ($tag eq '000' ) {
1688 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1689 # between 001 and 009 (included)
1690 } elsif ($cgi->param($params->[$j+1]) ne '') {
1691 $newfield = MARC::Field->new(
1693 $cgi->param($params->[$j+1]),
1696 # > 009, deal with subfields
1698 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1699 my $inner_param = $params->[$j];
1701 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1702 $newfield->add_subfields(
1703 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1707 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1708 $newfield = MARC::Field->new(
1712 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1719 push @fields,$newfield if($newfield);
1724 $record->append_fields(@fields);
1728 # cache inverted MARC field map
1729 our $inverted_field_map;
1731 =head2 TransformMarcToKoha
1735 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1739 Extract data from a MARC bib record into a hashref representing
1740 Koha biblio, biblioitems, and items fields.
1743 sub TransformMarcToKoha {
1744 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1747 $limit_table=$limit_table||0;
1748 $frameworkcode = '' unless defined $frameworkcode;
1750 unless (defined $inverted_field_map) {
1751 $inverted_field_map = _get_inverted_marc_field_map();
1755 if ( defined $limit_table && $limit_table eq 'items') {
1756 $tables{'items'} = 1;
1758 $tables{'items'} = 1;
1759 $tables{'biblio'} = 1;
1760 $tables{'biblioitems'} = 1;
1763 # traverse through record
1764 MARCFIELD: foreach my $field ($record->fields()) {
1765 my $tag = $field->tag();
1766 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1767 if ($field->is_control_field()) {
1768 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1769 ENTRY: foreach my $entry (@{ $kohafields }) {
1770 my ($subfield, $table, $column) = @{ $entry };
1771 next ENTRY unless exists $tables{$table};
1772 my $key = _disambiguate($table, $column);
1773 if ($result->{$key}) {
1774 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1775 $result->{$key} .= " | " . $field->data();
1778 $result->{$key} = $field->data();
1782 # deal with subfields
1783 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1784 my $code = $sf->[0];
1785 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1786 my $value = $sf->[1];
1787 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1788 my ($table, $column) = @{ $entry };
1789 next SFENTRY unless exists $tables{$table};
1790 my $key = _disambiguate($table, $column);
1791 if ($result->{$key}) {
1792 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1793 $result->{$key} .= " | " . $value;
1796 $result->{$key} = $value;
1803 # modify copyrightdate to keep only the 1st year found
1804 if (exists $result->{'copyrightdate'}) {
1805 my $temp = $result->{'copyrightdate'};
1806 $temp =~ m/c(\d\d\d\d)/;
1807 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1808 $result->{'copyrightdate'} = $1;
1810 else { # if no cYYYY, get the 1st date.
1811 $temp =~ m/(\d\d\d\d)/;
1812 $result->{'copyrightdate'} = $1;
1816 # modify publicationyear to keep only the 1st year found
1817 if (exists $result->{'publicationyear'}) {
1818 my $temp = $result->{'publicationyear'};
1819 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1820 $result->{'publicationyear'} = $1;
1822 else { # if no cYYYY, get the 1st date.
1823 $temp =~ m/(\d\d\d\d)/;
1824 $result->{'publicationyear'} = $1;
1831 sub _get_inverted_marc_field_map {
1833 my $relations = C4::Context->marcfromkohafield;
1835 foreach my $frameworkcode (keys %{ $relations }) {
1836 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1837 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1838 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1839 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1840 my ($table, $column) = split /[.]/, $kohafield, 2;
1841 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1842 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1848 =head2 _disambiguate
1852 $newkey = _disambiguate($table, $field);
1854 This is a temporary hack to distinguish between the
1855 following sets of columns when using TransformMarcToKoha.
1857 items.cn_source & biblioitems.cn_source
1858 items.cn_sort & biblioitems.cn_sort
1860 Columns that are currently NOT distinguished (FIXME
1861 due to lack of time to fully test) are:
1863 biblio.notes and biblioitems.notes
1868 FIXME - this is necessary because prefixing each column
1869 name with the table name would require changing lots
1870 of code and templates, and exposing more of the DB
1871 structure than is good to the UI templates, particularly
1872 since biblio and bibloitems may well merge in a future
1873 version. In the future, it would also be good to
1874 separate DB access and UI presentation field names
1882 my ($table, $column) = @_;
1883 if ($column eq "cn_sort" or $column eq "cn_source") {
1884 return $table . '.' . $column;
1891 =head2 get_koha_field_from_marc
1895 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1897 Internal function to map data from the MARC record to a specific non-MARC field.
1898 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1904 sub get_koha_field_from_marc {
1905 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1906 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1908 foreach my $field ( $record->field($tagfield) ) {
1909 if ( $field->tag() < 10 ) {
1911 $kohafield .= " | " . $field->data();
1914 $kohafield = $field->data();
1918 if ( $field->subfields ) {
1919 my @subfields = $field->subfields();
1920 foreach my $subfieldcount ( 0 .. $#subfields ) {
1921 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1924 " | " . $subfields[$subfieldcount][1];
1928 $subfields[$subfieldcount][1];
1939 =head2 TransformMarcToKohaOneField
1943 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1949 sub TransformMarcToKohaOneField {
1951 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1952 # only the 1st will be retrieved...
1953 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1955 my ( $tagfield, $subfield ) =
1956 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1958 foreach my $field ( $record->field($tagfield) ) {
1959 if ( $field->tag() < 10 ) {
1960 if ( $result->{$kohafield} ) {
1961 $result->{$kohafield} .= " | " . $field->data();
1964 $result->{$kohafield} = $field->data();
1968 if ( $field->subfields ) {
1969 my @subfields = $field->subfields();
1970 foreach my $subfieldcount ( 0 .. $#subfields ) {
1971 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1972 if ( $result->{$kohafield} ) {
1973 $result->{$kohafield} .=
1974 " | " . $subfields[$subfieldcount][1];
1977 $result->{$kohafield} =
1978 $subfields[$subfieldcount][1];
1988 =head1 OTHER FUNCTIONS
1991 =head2 PrepareItemrecordDisplay
1995 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1997 Returns a hash with all the fields for Display a given item data in a template
2003 sub PrepareItemrecordDisplay {
2005 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2007 my $dbh = C4::Context->dbh;
2008 my $frameworkcode = &GetFrameworkCode( $bibnum );
2009 my ( $itemtagfield, $itemtagsubfield ) =
2010 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2011 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2012 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2014 my $authorised_values_sth =
2016 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2018 foreach my $tag ( sort keys %{$tagslib} ) {
2019 my $previous_tag = '';
2021 # loop through each subfield
2023 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2024 next if ( subfield_is_koha_internal_p($subfield) );
2025 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2027 $subfield_data{tag} = $tag;
2028 $subfield_data{subfield} = $subfield;
2029 $subfield_data{countsubfield} = $cntsubf++;
2030 $subfield_data{kohafield} =
2031 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2033 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2034 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2035 $subfield_data{mandatory} =
2036 $tagslib->{$tag}->{$subfield}->{mandatory};
2037 $subfield_data{repeatable} =
2038 $tagslib->{$tag}->{$subfield}->{repeatable};
2039 $subfield_data{hidden} = "display:none"
2040 if $tagslib->{$tag}->{$subfield}->{hidden};
2042 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2044 $value =~ s/"/"/g;
2046 # search for itemcallnumber if applicable
2047 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2048 'items.itemcallnumber'
2049 && C4::Context->preference('itemcallnumber') )
2052 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2054 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2055 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2057 $value = $temp->subfield($CNsubfield);
2060 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2061 'items.itemcallnumber'
2062 && $defaultvalues->{'callnumber'} )
2064 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2066 $value = $defaultvalues->{'callnumber'};
2069 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2070 'items.holdingbranch' ||
2071 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2073 && $defaultvalues->{'branchcode'} )
2075 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2077 $value = $defaultvalues->{branchcode};
2080 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2081 my @authorised_values;
2084 # builds list, depending on authorised value...
2086 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2089 if ( ( C4::Context->preference("IndependantBranches") )
2090 && ( C4::Context->userenv->{flags} != 1 ) )
2094 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2096 $sth->execute( C4::Context->userenv->{branch} );
2097 push @authorised_values, ""
2099 $tagslib->{$tag}->{$subfield}->{mandatory} );
2100 while ( my ( $branchcode, $branchname ) =
2101 $sth->fetchrow_array )
2103 push @authorised_values, $branchcode;
2104 $authorised_lib{$branchcode} = $branchname;
2110 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2113 push @authorised_values, ""
2115 $tagslib->{$tag}->{$subfield}->{mandatory} );
2116 while ( my ( $branchcode, $branchname ) =
2117 $sth->fetchrow_array )
2119 push @authorised_values, $branchcode;
2120 $authorised_lib{$branchcode} = $branchname;
2126 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2131 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2134 push @authorised_values, ""
2135 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2136 while ( my ( $itemtype, $description ) =
2137 $sth->fetchrow_array )
2139 push @authorised_values, $itemtype;
2140 $authorised_lib{$itemtype} = $description;
2143 #---- "true" authorised value
2146 $authorised_values_sth->execute(
2147 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2148 push @authorised_values, ""
2149 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2150 while ( my ( $value, $lib ) =
2151 $authorised_values_sth->fetchrow_array )
2153 push @authorised_values, $value;
2154 $authorised_lib{$value} = $lib;
2157 $subfield_data{marc_value} = CGI::scrolling_list(
2158 -name => 'field_value',
2159 -values => \@authorised_values,
2160 -default => "$value",
2161 -labels => \%authorised_lib,
2167 elsif ( $tagslib->{$tag}->{$subfield}->{thesaurus_category} ) {
2168 $subfield_data{marc_value} =
2169 "<input type=\"text\" name=\"field_value\" size=\"47\" maxlength=\"255\" /> <a href=\"javascript:Dopop('cataloguing/thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=',)\">...</a>";
2172 # COMMENTED OUT because No $i is provided with this API.
2173 # And thus, no value_builder can be activated.
2174 # BUT could be thought over.
2175 # } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
2176 # my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
2178 # my $extended_param = plugin_parameters($dbh,$itemrecord,$tagslib,$i,0);
2179 # my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
2180 # $subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name()\" OnBlur=\"javascript:Blur$function_name()\"> <a href=\"javascript:Clic$function_name()\">...</a> $javascript";
2183 $subfield_data{marc_value} =
2184 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2186 push( @loop_data, \%subfield_data );
2190 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2191 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2193 'itemtagfield' => $itemtagfield,
2194 'itemtagsubfield' => $itemtagsubfield,
2195 'itemnumber' => $itemnumber,
2196 'iteminformation' => \@loop_data
2202 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2204 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2205 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2206 # =head2 ModZebrafiles
2208 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2212 # sub ModZebrafiles {
2214 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2218 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2219 # unless ( opendir( DIR, "$zebradir" ) ) {
2220 # warn "$zebradir not found";
2224 # my $filename = $zebradir . $biblionumber;
2227 # open( OUTPUT, ">", $filename . ".xml" );
2228 # print OUTPUT $record;
2237 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2239 $biblionumber is the biblionumber we want to index
2240 $op is specialUpdate or delete, and is used to know what we want to do
2241 $server is the server that we want to update
2242 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2243 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2245 $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.
2252 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2253 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2254 my $dbh=C4::Context->dbh;
2256 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2258 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2259 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2261 if (C4::Context->preference("NoZebra")) {
2262 # lock the nozebra table : we will read index lines, update them in Perl process
2263 # and write everything in 1 transaction.
2264 # lock the table to avoid someone else overwriting what we are doing
2265 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2266 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2267 if ($op eq 'specialUpdate') {
2268 # OK, we have to add or update the record
2269 # 1st delete (virtually, in indexes), if record actually exists
2271 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2273 # ... add the record
2274 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2276 # it's a deletion, delete the record...
2277 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2278 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2280 # ok, now update the database...
2281 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2282 foreach my $key (keys %result) {
2283 foreach my $index (keys %{$result{$key}}) {
2284 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2287 $dbh->do('UNLOCK TABLES');
2290 # we use zebra, just fill zebraqueue table
2292 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2294 AND biblio_auth_number = ?
2297 my $check_sth = $dbh->prepare_cached($check_sql);
2298 $check_sth->execute($server, $biblionumber, $op);
2299 my ($count) = $check_sth->fetchrow_array;
2300 $check_sth->finish();
2302 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2303 $sth->execute($biblionumber,$server,$op);
2309 =head2 GetNoZebraIndexes
2311 %indexes = GetNoZebraIndexes;
2313 return the data from NoZebraIndexes syspref.
2317 sub GetNoZebraIndexes {
2318 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2320 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2321 $line =~ /(.*)=>(.*)/;
2322 my $index = $1; # initial ' or " is removed afterwards
2324 $index =~ s/'|"|\s//g;
2325 $fields =~ s/'|"|\s//g;
2326 $indexes{$index}=$fields;
2331 =head1 INTERNAL FUNCTIONS
2333 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2335 function to delete a biblio in NoZebra indexes
2336 This function does NOT delete anything in database : it reads all the indexes entries
2337 that have to be deleted & delete them in the hash
2338 The SQL part is done either :
2339 - after the Add if we are modifying a biblio (delete + add again)
2340 - immediatly after this sub if we are doing a true deletion.
2341 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2346 sub _DelBiblioNoZebra {
2347 my ($biblionumber, $record, $server)=@_;
2350 my $dbh = C4::Context->dbh;
2354 if ($server eq 'biblioserver') {
2355 %index=GetNoZebraIndexes;
2356 # get title of the record (to store the 10 first letters with the index)
2357 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2358 $title = lc($record->subfield($titletag,$titlesubfield));
2360 # for authorities, the "title" is the $a mainentry
2361 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2362 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2363 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2364 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2365 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2366 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2367 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2371 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2372 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2373 # limit to 10 char, should be enough, and limit the DB size
2374 $title = substr($title,0,10);
2376 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2377 foreach my $field ($record->fields()) {
2378 #parse each subfield
2379 next if $field->tag <10;
2380 foreach my $subfield ($field->subfields()) {
2381 my $tag = $field->tag();
2382 my $subfieldcode = $subfield->[0];
2384 # check each index to see if the subfield is stored somewhere
2385 # otherwise, store it in __RAW__ index
2386 foreach my $key (keys %index) {
2387 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2388 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2390 my $line= lc $subfield->[1];
2391 # remove meaningless value in the field...
2392 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2393 # ... and split in words
2394 foreach (split / /,$line) {
2395 next unless $_; # skip empty values (multiple spaces)
2396 # if the entry is already here, do nothing, the biblionumber has already be removed
2397 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2398 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2399 $sth2->execute($server,$key,$_);
2400 my $existing_biblionumbers = $sth2->fetchrow;
2402 if ($existing_biblionumbers) {
2403 # warn " existing for $key $_: $existing_biblionumbers";
2404 $result{$key}->{$_} =$existing_biblionumbers;
2405 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2411 # the subfield is not indexed, store it in __RAW__ index anyway
2413 my $line= lc $subfield->[1];
2414 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2415 # ... and split in words
2416 foreach (split / /,$line) {
2417 next unless $_; # skip empty values (multiple spaces)
2418 # if the entry is already here, do nothing, the biblionumber has already be removed
2419 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2420 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2421 $sth2->execute($server,'__RAW__',$_);
2422 my $existing_biblionumbers = $sth2->fetchrow;
2424 if ($existing_biblionumbers) {
2425 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2426 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2436 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2438 function to add a biblio in NoZebra indexes
2442 sub _AddBiblioNoZebra {
2443 my ($biblionumber, $record, $server, %result)=@_;
2444 my $dbh = C4::Context->dbh;
2448 if ($server eq 'biblioserver') {
2449 %index=GetNoZebraIndexes;
2450 # get title of the record (to store the 10 first letters with the index)
2451 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2452 $title = lc($record->subfield($titletag,$titlesubfield));
2454 # warn "server : $server";
2455 # for authorities, the "title" is the $a mainentry
2456 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2457 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2458 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2459 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2460 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2461 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2462 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2465 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2466 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2467 # limit to 10 char, should be enough, and limit the DB size
2468 $title = substr($title,0,10);
2470 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2471 foreach my $field ($record->fields()) {
2472 #parse each subfield
2473 ###FIXME: impossible to index a 001-009 value with NoZebra
2474 next if $field->tag <10;
2475 foreach my $subfield ($field->subfields()) {
2476 my $tag = $field->tag();
2477 my $subfieldcode = $subfield->[0];
2479 # warn "INDEXING :".$subfield->[1];
2480 # check each index to see if the subfield is stored somewhere
2481 # otherwise, store it in __RAW__ index
2482 foreach my $key (keys %index) {
2483 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2484 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2486 my $line= lc $subfield->[1];
2487 # remove meaningless value in the field...
2488 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2489 # ... and split in words
2490 foreach (split / /,$line) {
2491 next unless $_; # skip empty values (multiple spaces)
2492 # if the entry is already here, improve weight
2493 # warn "managing $_";
2494 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2495 my $weight = $1 + 1;
2496 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2497 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2499 # get the value if it exist in the nozebra table, otherwise, create it
2500 $sth2->execute($server,$key,$_);
2501 my $existing_biblionumbers = $sth2->fetchrow;
2503 if ($existing_biblionumbers) {
2504 $result{$key}->{"$_"} =$existing_biblionumbers;
2505 my $weight = defined $1 ? $1 + 1 : 1;
2506 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2507 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2508 # create a new ligne for this entry
2510 # warn "INSERT : $server / $key / $_";
2511 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2512 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2518 # the subfield is not indexed, store it in __RAW__ index anyway
2520 my $line= lc $subfield->[1];
2521 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2522 # ... and split in words
2523 foreach (split / /,$line) {
2524 next unless $_; # skip empty values (multiple spaces)
2525 # if the entry is already here, improve weight
2526 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2528 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2529 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2531 # get the value if it exist in the nozebra table, otherwise, create it
2532 $sth2->execute($server,'__RAW__',$_);
2533 my $existing_biblionumbers = $sth2->fetchrow;
2535 if ($existing_biblionumbers) {
2536 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2538 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2539 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2540 # create a new ligne for this entry
2542 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2543 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2558 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2560 Find the given $subfield in the given $tag in the given
2561 MARC::Record $record. If the subfield is found, returns
2562 the (indicators, value) pair; otherwise, (undef, undef) is
2566 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2567 I suggest we export it from this module.
2574 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2577 if ( $tagfield < 10 ) {
2578 if ( $record->field($tagfield) ) {
2579 push @result, $record->field($tagfield)->data();
2586 foreach my $field ( $record->field($tagfield) ) {
2587 my @subfields = $field->subfields();
2588 foreach my $subfield (@subfields) {
2589 if ( @$subfield[0] eq $insubfield ) {
2590 push @result, @$subfield[1];
2591 $indicator = $field->indicator(1) . $field->indicator(2);
2596 return ( $indicator, @result );
2599 =head2 _koha_marc_update_bib_ids
2603 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2605 Internal function to add or update biblionumber and biblioitemnumber to
2612 sub _koha_marc_update_bib_ids {
2613 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2615 # we must add bibnum and bibitemnum in MARC::Record...
2616 # we build the new field with biblionumber and biblioitemnumber
2617 # we drop the original field
2618 # we add the new builded field.
2619 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2620 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2622 if ($biblio_tag != $biblioitem_tag) {
2623 # biblionumber & biblioitemnumber are in different fields
2625 # deal with biblionumber
2626 my ($new_field, $old_field);
2627 if ($biblio_tag < 10) {
2628 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2631 MARC::Field->new( $biblio_tag, '', '',
2632 "$biblio_subfield" => $biblionumber );
2635 # drop old field and create new one...
2636 $old_field = $record->field($biblio_tag);
2637 $record->delete_field($old_field) if $old_field;
2638 $record->append_fields($new_field);
2640 # deal with biblioitemnumber
2641 if ($biblioitem_tag < 10) {
2642 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2645 MARC::Field->new( $biblioitem_tag, '', '',
2646 "$biblioitem_subfield" => $biblioitemnumber, );
2648 # drop old field and create new one...
2649 $old_field = $record->field($biblioitem_tag);
2650 $record->delete_field($old_field) if $old_field;
2651 $record->insert_fields_ordered($new_field);
2654 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2655 my $new_field = MARC::Field->new(
2656 $biblio_tag, '', '',
2657 "$biblio_subfield" => $biblionumber,
2658 "$biblioitem_subfield" => $biblioitemnumber
2661 # drop old field and create new one...
2662 my $old_field = $record->field($biblio_tag);
2663 $record->delete_field($old_field) if $old_field;
2664 $record->insert_fields_ordered($new_field);
2668 =head2 _koha_marc_update_biblioitem_cn_sort
2672 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2676 Given a MARC bib record and the biblioitem hash, update the
2677 subfield that contains a copy of the value of biblioitems.cn_sort.
2681 sub _koha_marc_update_biblioitem_cn_sort {
2683 my $biblioitem = shift;
2684 my $frameworkcode= shift;
2686 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2687 return unless $biblioitem_tag;
2689 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2691 if (my $field = $marc->field($biblioitem_tag)) {
2692 $field->delete_subfield(code => $biblioitem_subfield);
2693 if ($cn_sort ne '') {
2694 $field->add_subfields($biblioitem_subfield => $cn_sort);
2697 # if we get here, no biblioitem tag is present in the MARC record, so
2698 # we'll create it if $cn_sort is not empty -- this would be
2699 # an odd combination of events, however
2701 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2706 =head2 _koha_add_biblio
2710 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2712 Internal function to add a biblio ($biblio is a hash with the values)
2718 sub _koha_add_biblio {
2719 my ( $dbh, $biblio, $frameworkcode ) = @_;
2723 # set the series flag
2725 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2729 SET frameworkcode = ?,
2740 my $sth = $dbh->prepare($query);
2743 $biblio->{'author'},
2745 $biblio->{'unititle'},
2748 $biblio->{'seriestitle'},
2749 $biblio->{'copyrightdate'},
2750 $biblio->{'abstract'}
2753 my $biblionumber = $dbh->{'mysql_insertid'};
2754 if ( $dbh->errstr ) {
2755 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2760 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2761 return ($biblionumber,$error);
2764 =head2 _koha_modify_biblio
2768 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2770 Internal function for updating the biblio table
2776 sub _koha_modify_biblio {
2777 my ( $dbh, $biblio, $frameworkcode ) = @_;
2782 SET frameworkcode = ?,
2791 WHERE biblionumber = ?
2794 my $sth = $dbh->prepare($query);
2798 $biblio->{'author'},
2800 $biblio->{'unititle'},
2802 $biblio->{'serial'},
2803 $biblio->{'seriestitle'},
2804 $biblio->{'copyrightdate'},
2805 $biblio->{'abstract'},
2806 $biblio->{'biblionumber'}
2807 ) if $biblio->{'biblionumber'};
2809 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2810 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2813 return ( $biblio->{'biblionumber'},$error );
2816 =head2 _koha_modify_biblioitem_nonmarc
2820 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2822 Updates biblioitems row except for marc and marcxml, which should be changed
2829 sub _koha_modify_biblioitem_nonmarc {
2830 my ( $dbh, $biblioitem ) = @_;
2833 # re-calculate the cn_sort, it may have changed
2834 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2838 SET biblionumber = ?,
2844 publicationyear = ?,
2848 collectiontitle = ?,
2850 collectionvolume= ?,
2851 editionstatement= ?,
2852 editionresponsibility = ?,
2866 where biblioitemnumber = ?
2868 my $sth = $dbh->prepare($query);
2870 $biblioitem->{'biblionumber'},
2871 $biblioitem->{'volume'},
2872 $biblioitem->{'number'},
2873 $biblioitem->{'itemtype'},
2874 $biblioitem->{'isbn'},
2875 $biblioitem->{'issn'},
2876 $biblioitem->{'publicationyear'},
2877 $biblioitem->{'publishercode'},
2878 $biblioitem->{'volumedate'},
2879 $biblioitem->{'volumedesc'},
2880 $biblioitem->{'collectiontitle'},
2881 $biblioitem->{'collectionissn'},
2882 $biblioitem->{'collectionvolume'},
2883 $biblioitem->{'editionstatement'},
2884 $biblioitem->{'editionresponsibility'},
2885 $biblioitem->{'illus'},
2886 $biblioitem->{'pages'},
2887 $biblioitem->{'bnotes'},
2888 $biblioitem->{'size'},
2889 $biblioitem->{'place'},
2890 $biblioitem->{'lccn'},
2891 $biblioitem->{'url'},
2892 $biblioitem->{'biblioitems.cn_source'},
2893 $biblioitem->{'cn_class'},
2894 $biblioitem->{'cn_item'},
2895 $biblioitem->{'cn_suffix'},
2897 $biblioitem->{'totalissues'},
2898 $biblioitem->{'biblioitemnumber'}
2900 if ( $dbh->errstr ) {
2901 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2904 return ($biblioitem->{'biblioitemnumber'},$error);
2907 =head2 _koha_add_biblioitem
2911 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2913 Internal function to add a biblioitem
2919 sub _koha_add_biblioitem {
2920 my ( $dbh, $biblioitem ) = @_;
2923 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2925 "INSERT INTO biblioitems SET
2932 publicationyear = ?,
2936 collectiontitle = ?,
2938 collectionvolume= ?,
2939 editionstatement= ?,
2940 editionresponsibility = ?,
2956 my $sth = $dbh->prepare($query);
2958 $biblioitem->{'biblionumber'},
2959 $biblioitem->{'volume'},
2960 $biblioitem->{'number'},
2961 $biblioitem->{'itemtype'},
2962 $biblioitem->{'isbn'},
2963 $biblioitem->{'issn'},
2964 $biblioitem->{'publicationyear'},
2965 $biblioitem->{'publishercode'},
2966 $biblioitem->{'volumedate'},
2967 $biblioitem->{'volumedesc'},
2968 $biblioitem->{'collectiontitle'},
2969 $biblioitem->{'collectionissn'},
2970 $biblioitem->{'collectionvolume'},
2971 $biblioitem->{'editionstatement'},
2972 $biblioitem->{'editionresponsibility'},
2973 $biblioitem->{'illus'},
2974 $biblioitem->{'pages'},
2975 $biblioitem->{'bnotes'},
2976 $biblioitem->{'size'},
2977 $biblioitem->{'place'},
2978 $biblioitem->{'lccn'},
2979 $biblioitem->{'marc'},
2980 $biblioitem->{'url'},
2981 $biblioitem->{'biblioitems.cn_source'},
2982 $biblioitem->{'cn_class'},
2983 $biblioitem->{'cn_item'},
2984 $biblioitem->{'cn_suffix'},
2986 $biblioitem->{'totalissues'}
2988 my $bibitemnum = $dbh->{'mysql_insertid'};
2989 if ( $dbh->errstr ) {
2990 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2994 return ($bibitemnum,$error);
2997 =head2 _koha_delete_biblio
3001 $error = _koha_delete_biblio($dbh,$biblionumber);
3003 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3005 C<$dbh> - the database handle
3006 C<$biblionumber> - the biblionumber of the biblio to be deleted
3012 # FIXME: add error handling
3014 sub _koha_delete_biblio {
3015 my ( $dbh, $biblionumber ) = @_;
3017 # get all the data for this biblio
3018 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3019 $sth->execute($biblionumber);
3021 if ( my $data = $sth->fetchrow_hashref ) {
3023 # save the record in deletedbiblio
3024 # find the fields to save
3025 my $query = "INSERT INTO deletedbiblio SET ";
3027 foreach my $temp ( keys %$data ) {
3028 $query .= "$temp = ?,";
3029 push( @bind, $data->{$temp} );
3032 # replace the last , by ",?)"
3034 my $bkup_sth = $dbh->prepare($query);
3035 $bkup_sth->execute(@bind);
3039 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3040 $del_sth->execute($biblionumber);
3047 =head2 _koha_delete_biblioitems
3051 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3053 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3055 C<$dbh> - the database handle
3056 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3062 # FIXME: add error handling
3064 sub _koha_delete_biblioitems {
3065 my ( $dbh, $biblioitemnumber ) = @_;
3067 # get all the data for this biblioitem
3069 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3070 $sth->execute($biblioitemnumber);
3072 if ( my $data = $sth->fetchrow_hashref ) {
3074 # save the record in deletedbiblioitems
3075 # find the fields to save
3076 my $query = "INSERT INTO deletedbiblioitems SET ";
3078 foreach my $temp ( keys %$data ) {
3079 $query .= "$temp = ?,";
3080 push( @bind, $data->{$temp} );
3083 # replace the last , by ",?)"
3085 my $bkup_sth = $dbh->prepare($query);
3086 $bkup_sth->execute(@bind);
3089 # delete the biblioitem
3091 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3092 $del_sth->execute($biblioitemnumber);
3099 =head1 UNEXPORTED FUNCTIONS
3101 =head2 ModBiblioMarc
3103 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3105 Add MARC data for a biblio to koha
3107 Function exported, but should NOT be used, unless you really know what you're doing
3113 # pass the MARC::Record to this function, and it will create the records in the marc field
3114 my ( $record, $biblionumber, $frameworkcode ) = @_;
3115 my $dbh = C4::Context->dbh;
3116 my @fields = $record->fields();
3117 if ( !$frameworkcode ) {
3118 $frameworkcode = "";
3121 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3122 $sth->execute( $frameworkcode, $biblionumber );
3124 my $encoding = C4::Context->preference("marcflavour");
3126 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3127 if ( $encoding eq "UNIMARC" ) {
3129 if ( length($record->subfield( 100, "a" )) == 35 ) {
3130 $string = $record->subfield( 100, "a" );
3131 my $f100 = $record->field(100);
3132 $record->delete_field($f100);
3135 $string = POSIX::strftime( "%Y%m%d", localtime );
3137 $string = sprintf( "%-*s", 35, $string );
3139 substr( $string, 22, 6, "frey50" );
3140 unless ( $record->subfield( 100, "a" ) ) {
3141 $record->insert_grouped_field(
3142 MARC::Field->new( 100, "", "", "a" => $string ) );
3146 if (C4::Context->preference("NoZebra")) {
3147 # only NoZebra indexing needs to have
3148 # the previous version of the record
3149 $oldRecord = GetMarcBiblio($biblionumber);
3153 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3154 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3157 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3158 return $biblionumber;
3161 =head2 z3950_extended_services
3163 z3950_extended_services($serviceType,$serviceOptions,$record);
3165 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.
3167 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3169 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3171 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3175 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3176 syntax => the record syntax (transfer syntax)
3177 databaseName = Database from connection object
3179 To set serviceOptions, call set_service_options($serviceType)
3181 C<$record> the record, if one is needed for the service type
3183 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3187 sub z3950_extended_services {
3188 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3190 # get our connection object
3191 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3193 # create a new package object
3194 my $Zpackage = $Zconn->package();
3197 $Zpackage->option( action => $action );
3199 if ( $serviceOptions->{'databaseName'} ) {
3200 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3202 if ( $serviceOptions->{'recordIdNumber'} ) {
3204 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3206 if ( $serviceOptions->{'recordIdOpaque'} ) {
3208 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3211 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3212 #if ($serviceType eq 'itemorder') {
3213 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3214 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3215 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3216 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3219 if ( $serviceOptions->{record} ) {
3220 $Zpackage->option( record => $serviceOptions->{record} );
3222 # can be xml or marc
3223 if ( $serviceOptions->{'syntax'} ) {
3224 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3228 # send the request, handle any exception encountered
3229 eval { $Zpackage->send($serviceType) };
3230 if ( $@ && $@->isa("ZOOM::Exception") ) {
3231 return "error: " . $@->code() . " " . $@->message() . "\n";
3234 # free up package resources
3235 $Zpackage->destroy();
3238 =head2 set_service_options
3240 my $serviceOptions = set_service_options($serviceType);
3242 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3244 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3248 sub set_service_options {
3249 my ($serviceType) = @_;
3252 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3253 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3255 if ( $serviceType eq 'commit' ) {
3259 if ( $serviceType eq 'create' ) {
3263 if ( $serviceType eq 'drop' ) {
3264 die "ERROR: 'drop' not currently supported (by Zebra)";
3266 return $serviceOptions;
3269 =head3 get_biblio_authorised_values
3271 find the types and values for all authorised values assigned to this biblio.
3276 returns: a hashref malling the authorised value to the value set for this biblionumber
3278 $authorised_values = {
3279 'Scent' => 'flowery',
3280 'Audience' => 'Young Adult',
3281 'itemtypes' => 'SER',
3284 Notes: forlibrarian should probably be passed in, and called something different.
3289 sub get_biblio_authorised_values {
3290 my $biblionumber = shift;
3292 my $forlibrarian = 1; # are we in staff or opac?
3293 my $frameworkcode = GetFrameworkCode( $biblionumber );
3295 my $authorised_values;
3297 my $record = GetMarcBiblio( $biblionumber )
3298 or return $authorised_values;
3299 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3300 or return $authorised_values;
3302 # assume that these entries in the authorised_value table are bibliolevel.
3303 # ones that start with 'item%' are item level.
3304 my $query = q(SELECT distinct authorised_value, kohafield
3305 FROM marc_subfield_structure
3306 WHERE authorised_value !=''
3307 AND (kohafield like 'biblio%'
3308 OR kohafield like '') );
3309 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3311 foreach my $tag ( keys( %$tagslib ) ) {
3312 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3313 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3314 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3315 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3316 if ( defined $record->field( $tag ) ) {
3317 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3318 if ( defined $this_subfield_value ) {
3319 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3326 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3327 return $authorised_values;
3337 Koha Developement team <info@koha.org>
3339 Paul POULAIN paul.poulain@free.fr
3341 Joshua Ferraro jmf@liblime.com