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
38 use vars qw($VERSION @ISA @EXPORT);
44 @ISA = qw( Exporter );
58 &GetBiblioItemByBiblioNumber
59 &GetBiblioFromItemNumber
71 &GetAuthorisedValueDesc
75 &GetPublisherNameFromIsbn
90 # To link headings in a bib record
91 # to authority records.
93 &LinkBibHeadingsToAuthorities
97 # those functions are exported but should not be used
98 # they are usefull is few circumstances, so are exported.
99 # but don't use them unless you're a core developer ;-)
106 &TransformHtmlToMarc2
109 &PrepareItemrecordDisplay
116 C4::Biblio - cataloging management functions
120 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:
124 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
126 =item 2. as raw MARC in the Zebra index and storage engine
128 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
132 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
134 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.
138 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
140 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
144 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:
148 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
150 =item 2. _koha_* - low-level internal functions for managing the koha tables
152 =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.
154 =item 4. Zebra functions used to update the Zebra index
156 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
160 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 :
164 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
166 =item 2. add the biblionumber and biblioitemnumber into the MARC records
168 =item 3. save the marc record
172 When dealing with items, we must :
176 =item 1. save the item in items table, that gives us an itemnumber
178 =item 2. add the itemnumber to the item MARC field
180 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
182 When modifying a biblio or an item, the behaviour is quite similar.
186 =head1 EXPORTED FUNCTIONS
192 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
196 Exported function (core API) for adding a new biblio to koha.
198 The first argument is a C<MARC::Record> object containing the
199 bib to add, while the second argument is the desired MARC
202 This function also accepts a third, optional argument: a hashref
203 to additional options. The only defined option is C<defer_marc_save>,
204 which if present and mapped to a true value, causes C<AddBiblio>
205 to omit the call to save the MARC in C<bibilioitems.marc>
206 and C<biblioitems.marcxml> This option is provided B<only>
207 for the use of scripts such as C<bulkmarcimport.pl> that may need
208 to do some manipulation of the MARC record for item parsing before
209 saving it and which cannot afford the performance hit of saving
210 the MARC record twice. Consequently, do not use that option
211 unless you can guarantee that C<ModBiblioMarc> will be called.
217 my $frameworkcode = shift;
218 my $options = @_ ? shift : undef;
219 my $defer_marc_save = 0;
220 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
221 $defer_marc_save = 1;
224 my ($biblionumber,$biblioitemnumber,$error);
225 my $dbh = C4::Context->dbh;
226 # transform the data into koha-table style data
227 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
228 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
229 $olddata->{'biblionumber'} = $biblionumber;
230 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
232 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
234 # update MARC subfield that stores biblioitems.cn_sort
235 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
238 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
240 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
242 return ( $biblionumber, $biblioitemnumber );
249 ModBiblio( $record,$biblionumber,$frameworkcode);
253 Replace an existing bib record identified by C<$biblionumber>
254 with one supplied by the MARC::Record object C<$record>. The embedded
255 item, biblioitem, and biblionumber fields from the previous
256 version of the bib record replace any such fields of those tags that
257 are present in C<$record>. Consequently, ModBiblio() is not
258 to be used to try to modify item records.
260 C<$frameworkcode> specifies the MARC framework to use
261 when storing the modified bib record; among other things,
262 this controls how MARC fields get mapped to display columns
263 in the C<biblio> and C<biblioitems> tables, as well as
264 which fields are used to store embedded item, biblioitem,
265 and biblionumber data for indexing.
270 my ( $record, $biblionumber, $frameworkcode ) = @_;
271 if (C4::Context->preference("CataloguingLog")) {
272 my $newrecord = GetMarcBiblio($biblionumber);
273 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
276 my $dbh = C4::Context->dbh;
278 $frameworkcode = "" unless $frameworkcode;
280 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
281 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
282 my $oldRecord = GetMarcBiblio( $biblionumber );
284 # delete any item fields from incoming record to avoid
285 # duplication or incorrect data - use AddItem() or ModItem()
287 foreach my $field ($record->field($itemtag)) {
288 $record->delete_field($field);
291 # once all the items fields are removed, copy the old ones, in order to keep synchronize
292 $record->append_fields($oldRecord->field( $itemtag ));
294 # update biblionumber and biblioitemnumber in MARC
295 # FIXME - this is assuming a 1 to 1 relationship between
296 # biblios and biblioitems
297 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
298 $sth->execute($biblionumber);
299 my ($biblioitemnumber) = $sth->fetchrow;
301 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
303 # load the koha-table data object
304 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
306 # update MARC subfield that stores biblioitems.cn_sort
307 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
309 # update the MARC record (that now contains biblio and items) with the new record data
310 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
312 # modify the other koha tables
313 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
314 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
318 =head2 ModBiblioframework
320 ModBiblioframework($biblionumber,$frameworkcode);
321 Exported function to modify a biblio framework
325 sub ModBiblioframework {
326 my ( $biblionumber, $frameworkcode ) = @_;
327 my $dbh = C4::Context->dbh;
328 my $sth = $dbh->prepare(
329 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
331 $sth->execute($frameworkcode, $biblionumber);
339 my $error = &DelBiblio($dbh,$biblionumber);
340 Exported function (core API) for deleting a biblio in koha.
341 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
342 Also backs it up to deleted* tables
343 Checks to make sure there are not issues on any of the items
345 C<$error> : undef unless an error occurs
352 my ( $biblionumber ) = @_;
353 my $dbh = C4::Context->dbh;
354 my $error; # for error handling
356 # First make sure this biblio has no items attached
357 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
358 $sth->execute($biblionumber);
359 if (my $itemnumber = $sth->fetchrow){
360 # Fix this to use a status the template can understand
361 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
364 return $error if $error;
366 # We delete attached subscriptions
367 if(C4::Serials::CountSubscriptionFromBiblionumber($biblionumber) != 0){
368 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
369 foreach my $subscription (@$subscriptions){
370 &C4::Serials::DelSubscription($subscription->{subscriptionid});
374 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
375 # for at least 2 reasons :
376 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
377 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
378 # 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)
380 if (C4::Context->preference("NoZebra")) {
381 # only NoZebra indexing needs to have
382 # the previous version of the record
383 $oldRecord = GetMarcBiblio($biblionumber);
385 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
387 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
390 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
391 $sth->execute($biblionumber);
392 while ( my $biblioitemnumber = $sth->fetchrow ) {
394 # delete this biblioitem
395 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
396 return $error if $error;
399 # delete biblio from Koha tables and save in deletedbiblio
400 # must do this *after* _koha_delete_biblioitems, otherwise
401 # delete cascade will prevent deletedbiblioitems rows
402 # from being generated by _koha_delete_biblioitems
403 $error = _koha_delete_biblio( $dbh, $biblionumber );
405 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
410 =head2 LinkBibHeadingsToAuthorities
414 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
418 Links bib headings to authority records by checking
419 each authority-controlled field in the C<MARC::Record>
420 object C<$marc>, looking for a matching authority record,
421 and setting the linking subfield $9 to the ID of that
424 If no matching authority exists, or if multiple
425 authorities match, no $9 will be added, and any
426 existing one inthe field will be deleted.
428 Returns the number of heading links changed in the
433 sub LinkBibHeadingsToAuthorities {
436 my $num_headings_changed = 0;
437 foreach my $field ($bib->fields()) {
438 my $heading = C4::Heading->new_from_bib_field($field);
439 next unless defined $heading;
442 my $current_link = $field->subfield('9');
444 # look for matching authorities
445 my $authorities = $heading->authorities();
447 # want only one exact match
448 if ($#{ $authorities } == 0) {
449 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
450 my $authid = $authority->field('001')->data();
451 next if defined $current_link and $current_link eq $authid;
453 $field->delete_subfield(code => '9') if defined $current_link;
454 $field->add_subfields('9', $authid);
455 $num_headings_changed++;
457 if (defined $current_link) {
458 $field->delete_subfield(code => '9');
459 $num_headings_changed++;
464 return $num_headings_changed;
471 $data = &GetBiblioData($biblionumber);
472 Returns information about the book with the given biblionumber.
473 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
474 the C<biblio> and C<biblioitems> tables in the
476 In addition, C<$data-E<gt>{subject}> is the list of the book's
477 subjects, separated by C<" , "> (space, comma, space).
478 If there are multiple biblioitems with the given biblionumber, only
479 the first one is considered.
487 my $dbh = C4::Context->dbh;
489 # my $query = C4::Context->preference('item-level_itypes') ?
490 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
492 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
493 # WHERE biblio.biblionumber = ?
494 # AND biblioitems.biblionumber = biblio.biblionumber
497 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
499 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
500 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
501 WHERE biblio.biblionumber = ?
502 AND biblioitems.biblionumber = biblio.biblionumber ";
504 my $sth = $dbh->prepare($query);
505 $sth->execute($bibnum);
507 $data = $sth->fetchrow_hashref;
511 } # sub GetBiblioData
513 =head2 &GetBiblioItemData
517 $itemdata = &GetBiblioItemData($biblioitemnumber);
519 Looks up the biblioitem with the given biblioitemnumber. Returns a
520 reference-to-hash. The keys are the fields from the C<biblio>,
521 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
522 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
529 sub GetBiblioItemData {
530 my ($biblioitemnumber) = @_;
531 my $dbh = C4::Context->dbh;
532 my $query = "SELECT *,biblioitems.notes AS bnotes
533 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
534 unless(C4::Context->preference('item-level_itypes')) {
535 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
537 $query .= " WHERE biblioitemnumber = ? ";
538 my $sth = $dbh->prepare($query);
540 $sth->execute($biblioitemnumber);
541 $data = $sth->fetchrow_hashref;
544 } # sub &GetBiblioItemData
546 =head2 GetBiblioItemByBiblioNumber
550 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
556 sub GetBiblioItemByBiblioNumber {
557 my ($biblionumber) = @_;
558 my $dbh = C4::Context->dbh;
559 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
563 $sth->execute($biblionumber);
565 while ( my $data = $sth->fetchrow_hashref ) {
566 push @results, $data;
573 =head2 GetBiblioFromItemNumber
577 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
579 Looks up the item with the given itemnumber. if undef, try the barcode.
581 C<&itemnodata> returns a reference-to-hash whose keys are the fields
582 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
590 sub GetBiblioFromItemNumber {
591 my ( $itemnumber, $barcode ) = @_;
592 my $dbh = C4::Context->dbh;
595 $sth=$dbh->prepare( "SELECT * FROM items
596 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
597 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
598 WHERE items.itemnumber = ?") ;
599 $sth->execute($itemnumber);
601 $sth=$dbh->prepare( "SELECT * FROM items
602 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
603 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
604 WHERE items.barcode = ?") ;
605 $sth->execute($barcode);
607 my $data = $sth->fetchrow_hashref;
616 ( $count, @results ) = &GetBiblio($biblionumber);
623 my ($biblionumber) = @_;
624 my $dbh = C4::Context->dbh;
625 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
628 $sth->execute($biblionumber);
629 while ( my $data = $sth->fetchrow_hashref ) {
630 $results[$count] = $data;
634 return ( $count, @results );
637 =head2 GetBiblioItemInfosOf
641 GetBiblioItemInfosOf(@biblioitemnumbers);
647 sub GetBiblioItemInfosOf {
648 my @biblioitemnumbers = @_;
651 SELECT biblioitemnumber,
655 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
657 return get_infos_of( $query, 'biblioitemnumber' );
660 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
662 =head2 GetMarcStructure
666 $res = GetMarcStructure($forlibrarian,$frameworkcode);
668 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
669 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
670 $frameworkcode : the framework code to read
676 # cache for results of GetMarcStructure -- needed
678 our $marc_structure_cache;
680 sub GetMarcStructure {
681 my ( $forlibrarian, $frameworkcode ) = @_;
682 my $dbh=C4::Context->dbh;
683 $frameworkcode = "" unless $frameworkcode;
685 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
686 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
690 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
692 # check that framework exists
695 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
696 $sth->execute($frameworkcode);
697 my ($total) = $sth->fetchrow;
698 $frameworkcode = "" unless ( $total > 0 );
701 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
702 FROM marc_tag_structure
703 WHERE frameworkcode=?
706 $sth->execute($frameworkcode);
707 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
709 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
712 $res->{$tag}->{lib} =
713 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
714 $res->{$tag}->{tab} = "";
715 $res->{$tag}->{mandatory} = $mandatory;
716 $res->{$tag}->{repeatable} = $repeatable;
721 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
722 FROM marc_subfield_structure
723 WHERE frameworkcode=?
724 ORDER BY tagfield,tagsubfield
728 $sth->execute($frameworkcode);
731 my $authorised_value;
743 $tag, $subfield, $liblibrarian,
745 $mandatory, $repeatable, $authorised_value,
746 $authtypecode, $value_builder, $kohafield,
747 $seealso, $hidden, $isurl,
753 $res->{$tag}->{$subfield}->{lib} =
754 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
755 $res->{$tag}->{$subfield}->{tab} = $tab;
756 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
757 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
758 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
759 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
760 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
761 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
762 $res->{$tag}->{$subfield}->{seealso} = $seealso;
763 $res->{$tag}->{$subfield}->{hidden} = $hidden;
764 $res->{$tag}->{$subfield}->{isurl} = $isurl;
765 $res->{$tag}->{$subfield}->{'link'} = $link;
766 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
769 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
774 =head2 GetUsedMarcStructure
776 the same function as GetMarcStructure expcet it just take field
777 in tab 0-9. (used field)
779 my $results = GetUsedMarcStructure($frameworkcode);
781 L<$results> is a ref to an array which each case containts a ref
782 to a hash which each keys is the columns from marc_subfield_structure
784 L<$frameworkcode> is the framework code.
788 sub GetUsedMarcStructure($){
789 my $frameworkcode = shift || '';
790 my $dbh = C4::Context->dbh;
793 FROM marc_subfield_structure
795 AND frameworkcode = ?
798 my $sth = $dbh->prepare($query);
799 $sth->execute($frameworkcode);
800 while (my $row = $sth->fetchrow_hashref){
806 =head2 GetMarcFromKohaField
810 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
811 Returns the MARC fields & subfields mapped to the koha field
812 for the given frameworkcode
818 sub GetMarcFromKohaField {
819 my ( $kohafield, $frameworkcode ) = @_;
820 return 0, 0 unless $kohafield and defined $frameworkcode;
821 my $relations = C4::Context->marcfromkohafield;
823 $relations->{$frameworkcode}->{$kohafield}->[0],
824 $relations->{$frameworkcode}->{$kohafield}->[1]
832 my $record = GetMarcBiblio($biblionumber);
836 Returns MARC::Record representing bib identified by
837 C<$biblionumber>. If no bib exists, returns undef.
838 The MARC record contains both biblio & item data.
843 my $biblionumber = shift;
844 my $dbh = C4::Context->dbh;
846 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
847 $sth->execute($biblionumber);
848 my $row = $sth->fetchrow_hashref;
849 my $marcxml = StripNonXmlChars($row->{'marcxml'});
850 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
851 my $record = MARC::Record->new();
853 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
854 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
855 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
866 my $marcxml = GetXmlBiblio($biblionumber);
868 Returns biblioitems.marcxml of the biblionumber passed in parameter.
869 The XML contains both biblio & item datas
876 my ( $biblionumber ) = @_;
877 my $dbh = C4::Context->dbh;
879 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
880 $sth->execute($biblionumber);
881 my ($marcxml) = $sth->fetchrow;
885 =head2 GetCOinSBiblio
889 my $coins = GetCOinSBiblio($biblionumber);
891 Returns the COinS(a span) which can be included in a biblio record
898 my ( $biblionumber ) = @_;
899 my $record = GetMarcBiblio($biblionumber);
901 if (defined $record){
902 # get the coin format
903 my $pos7 = substr $record->leader(), 7,1;
904 my $pos6 = substr $record->leader(), 6,1;
907 my ($aulast, $aufirst);
915 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
926 'i' => 'audioRecording',
927 'j' => 'audioRecording',
930 'm' => 'computerProgram',
935 'a' => 'journalArticle',
939 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
941 if( $genre eq 'book' ){
942 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
945 ##### We must transform mtx to a valable mtx and document type ####
946 if( $genre eq 'book' ){
948 }elsif( $genre eq 'journal' ){
950 }elsif( $genre eq 'journalArticle' ){
957 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
960 $aulast = $record->subfield('700','a');
961 $aufirst = $record->subfield('700','b');
962 $oauthors = "&rft.au=$aufirst $aulast";
964 if($record->field('200')){
965 for my $au ($record->field('200')->subfield('g')){
966 $oauthors .= "&rft.au=$au";
969 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
970 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
971 $pubyear = $record->subfield('210','d');
972 $publisher = $record->subfield('210','c');
973 $isbn = $record->subfield('010','a');
974 $issn = $record->subfield('011','a');
976 # MARC21 need some improve
979 $genre = "&rft.genre=book";
982 $oauthors .= "&rft.au=".$record->subfield('100','a');
984 if($record->field('700')){
985 for my $au ($record->field('700')->subfield('a')){
986 $oauthors .= "&rft.au=$au";
989 $title = "&rft.btitle=".$record->subfield('245','a');
990 $pubyear = $record->subfield('260','c');
991 $publisher = $record->subfield('260','b');
992 $isbn = $record->subfield('020','a');
993 $issn = $record->subfield('022','a');
996 $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";
997 $coins_value =~ s/\ /\+/g;
998 #<!-- 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="
1000 return $coins_value;
1003 =head2 GetAuthorisedValueDesc
1007 my $subfieldvalue =get_authorised_value_desc(
1008 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1009 Retrieve the complete description for a given authorised value.
1011 Now takes $category and $value pair too.
1012 my $auth_value_desc =GetAuthorisedValueDesc(
1013 '','', 'DVD' ,'','','CCODE');
1019 sub GetAuthorisedValueDesc {
1020 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1021 my $dbh = C4::Context->dbh;
1025 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1028 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1029 return C4::Branch::GetBranchName($value);
1033 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1034 return getitemtypeinfo($value)->{description};
1037 #---- "true" authorized value
1038 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1041 if ( $category ne "" ) {
1044 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1046 $sth->execute( $category, $value );
1047 my $data = $sth->fetchrow_hashref;
1048 return $data->{'lib'};
1051 return $value; # if nothing is found return the original value
1059 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1060 Get all notes from the MARC record and returns them in an array.
1061 The note are stored in differents places depending on MARC flavour
1068 my ( $record, $marcflavour ) = @_;
1070 if ( $marcflavour eq "MARC21" ) {
1073 else { # assume unimarc if not marc21
1080 foreach my $field ( $record->field($scope) ) {
1081 my $value = $field->as_string();
1082 $value =~ s/\n/<br \/>/g ;
1084 if ( $note ne "" ) {
1085 $marcnote = { marcnote => $note, };
1086 push @marcnotes, $marcnote;
1089 if ( $note ne $value ) {
1090 $note = $note . " " . $value;
1095 $marcnote = { marcnote => $note };
1096 push @marcnotes, $marcnote; #load last tag into array
1099 } # end GetMarcNotes
1101 =head2 GetMarcSubjects
1105 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1106 Get all subjects from the MARC record and returns them in an array.
1107 The subjects are stored in differents places depending on MARC flavour
1113 sub GetMarcSubjects {
1114 my ( $record, $marcflavour ) = @_;
1115 my ( $mintag, $maxtag );
1116 if ( $marcflavour eq "MARC21" ) {
1120 else { # assume unimarc if not marc21
1130 foreach my $field ( $record->field('6..' )) {
1131 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1133 my @subfields = $field->subfields();
1136 # if there is an authority link, build the link with an= subfield9
1137 my $subfield9 = $field->subfield('9');
1138 for my $subject_subfield (@subfields ) {
1139 # don't load unimarc subfields 3,4,5
1140 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1141 my $code = $subject_subfield->[0];
1142 my $value = $subject_subfield->[1];
1143 my $linkvalue = $value;
1144 $linkvalue =~ s/(\(|\))//g;
1145 my $operator = " and " unless $counter==0;
1147 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1149 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1151 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1153 my @this_link_loop = @link_loop;
1154 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1158 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1161 return \@marcsubjects;
1162 } #end getMARCsubjects
1164 =head2 GetMarcAuthors
1168 authors = GetMarcAuthors($record,$marcflavour);
1169 Get all authors from the MARC record and returns them in an array.
1170 The authors are stored in differents places depending on MARC flavour
1176 sub GetMarcAuthors {
1177 my ( $record, $marcflavour ) = @_;
1178 my ( $mintag, $maxtag );
1179 # tagslib useful for UNIMARC author reponsabilities
1180 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.
1181 if ( $marcflavour eq "MARC21" ) {
1185 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1194 foreach my $field ( $record->fields ) {
1195 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1198 my @subfields = $field->subfields();
1200 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1201 my $subfield9 = $field->subfield('9');
1202 for my $authors_subfield (@subfields) {
1203 # don't load unimarc subfields 3, 5
1204 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1205 my $subfieldcode = $authors_subfield->[0];
1206 my $value = $authors_subfield->[1];
1207 my $linkvalue = $value;
1208 $linkvalue =~ s/(\(|\))//g;
1209 my $operator = " and " unless $count_auth==0;
1210 # if we have an authority link, use that as the link, otherwise use standard searching
1212 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1215 # reset $linkvalue if UNIMARC author responsibility
1216 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1217 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1219 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1221 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1222 my @this_link_loop = @link_loop;
1223 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1224 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1227 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1229 return \@marcauthors;
1236 $marcurls = GetMarcUrls($record,$marcflavour);
1237 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1238 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1245 my ($record, $marcflavour) = @_;
1247 for my $field ($record->field('856')) {
1249 my $url = $field->subfield('u');
1251 for my $note ( $field->subfield('z')) {
1252 push @notes , {note => $note};
1254 if($marcflavour eq 'MARC21') {
1255 my $s3 = $field->subfield('3');
1256 my $link = $field->subfield('y');
1257 unless($url =~ /^\w+:/) {
1258 if($field->indicator(1) eq '7') {
1259 $url = $field->subfield('2') . "://" . $url;
1260 } elsif ($field->indicator(1) eq '1') {
1261 $url = 'ftp://' . $url;
1263 # properly, this should be if ind1=4,
1264 # however we will assume http protocol since we're building a link.
1265 $url = 'http://' . $url;
1268 # TODO handle ind 2 (relationship)
1269 $marcurl = { MARCURL => $url,
1272 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url ;
1273 $marcurl->{'part'} = $s3 if($link);
1274 $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
1276 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1277 $marcurl->{'MARCURL'} = $url ;
1279 push @marcurls, $marcurl;
1284 =head2 GetMarcSeries
1288 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1289 Get all series from the MARC record and returns them in an array.
1290 The series are stored in differents places depending on MARC flavour
1297 my ($record, $marcflavour) = @_;
1298 my ($mintag, $maxtag);
1299 if ($marcflavour eq "MARC21") {
1302 } else { # assume unimarc if not marc21
1312 foreach my $field ($record->field('440'), $record->field('490')) {
1314 #my $value = $field->subfield('a');
1315 #$marcsubjct = {MARCSUBJCT => $value,};
1316 my @subfields = $field->subfields();
1317 #warn "subfields:".join " ", @$subfields;
1320 for my $series_subfield (@subfields) {
1322 undef $volume_number;
1323 # see if this is an instance of a volume
1324 if ($series_subfield->[0] eq 'v') {
1328 my $code = $series_subfield->[0];
1329 my $value = $series_subfield->[1];
1330 my $linkvalue = $value;
1331 $linkvalue =~ s/(\(|\))//g;
1332 my $operator = " and " unless $counter==0;
1333 push @link_loop, {link => $linkvalue, operator => $operator };
1334 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1335 if ($volume_number) {
1336 push @subfields_loop, {volumenum => $value};
1339 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1343 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1344 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1345 #push @marcsubjcts, $marcsubjct;
1349 my $marcseriessarray=\@marcseries;
1350 return $marcseriessarray;
1351 } #end getMARCseriess
1353 =head2 GetFrameworkCode
1357 $frameworkcode = GetFrameworkCode( $biblionumber )
1363 sub GetFrameworkCode {
1364 my ( $biblionumber ) = @_;
1365 my $dbh = C4::Context->dbh;
1366 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1367 $sth->execute($biblionumber);
1368 my ($frameworkcode) = $sth->fetchrow;
1369 return $frameworkcode;
1372 =head2 GetPublisherNameFromIsbn
1374 $name = GetPublishercodeFromIsbn($isbn);
1381 sub GetPublisherNameFromIsbn($){
1383 $isbn =~ s/[- _]//g;
1385 my @codes = (split '-', DisplayISBN($isbn));
1386 my $code = $codes[0].$codes[1].$codes[2];
1387 my $dbh = C4::Context->dbh;
1389 SELECT distinct publishercode
1392 AND publishercode IS NOT NULL
1395 my $sth = $dbh->prepare($query);
1396 $sth->execute("$code%");
1397 my $name = $sth->fetchrow;
1398 return $name if length $name;
1402 =head2 TransformKohaToMarc
1406 $record = TransformKohaToMarc( $hash )
1407 This function builds partial MARC::Record from a hash
1408 Hash entries can be from biblio or biblioitems.
1409 This function is called in acquisition module, to create a basic catalogue entry from user entry
1415 sub TransformKohaToMarc {
1418 my $dbh = C4::Context->dbh;
1421 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1423 my $record = MARC::Record->new();
1424 foreach (keys %{$hash}) {
1425 &TransformKohaToMarcOneField( $sth, $record, $_,
1431 =head2 TransformKohaToMarcOneField
1435 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1441 sub TransformKohaToMarcOneField {
1442 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1443 $frameworkcode='' unless $frameworkcode;
1447 if ( !defined $sth ) {
1448 my $dbh = C4::Context->dbh;
1449 $sth = $dbh->prepare(
1450 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1453 $sth->execute( $frameworkcode, $kohafieldname );
1454 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1455 my $tag = $record->field($tagfield);
1457 $tag->update( $tagsubfield => $value );
1458 $record->delete_field($tag);
1459 $record->insert_fields_ordered($tag);
1462 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1468 =head2 TransformHtmlToXml
1472 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1474 $auth_type contains :
1475 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1476 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1477 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1483 sub TransformHtmlToXml {
1484 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1485 my $xml = MARC::File::XML::header('UTF-8');
1486 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1487 MARC::File::XML->default_record_format($auth_type);
1488 # in UNIMARC, field 100 contains the encoding
1489 # check that there is one, otherwise the
1490 # MARC::Record->new_from_xml will fail (and Koha will die)
1491 my $unimarc_and_100_exist=0;
1492 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1497 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1498 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1499 # if we have a 100 field and it's values are not correct, skip them.
1500 # if we don't have any valid 100 field, we will create a default one at the end
1501 my $enc = substr( @$values[$i], 26, 2 );
1502 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1503 $unimarc_and_100_exist=1;
1508 @$values[$i] =~ s/&/&/g;
1509 @$values[$i] =~ s/</</g;
1510 @$values[$i] =~ s/>/>/g;
1511 @$values[$i] =~ s/"/"/g;
1512 @$values[$i] =~ s/'/'/g;
1513 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1514 # utf8::decode( @$values[$i] );
1516 if ( ( @$tags[$i] ne $prevtag ) ) {
1517 $j++ unless ( @$tags[$i] eq "" );
1519 $xml .= "</datafield>\n";
1520 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1521 && ( @$values[$i] ne "" ) )
1523 my $ind1 = substr( @$indicator[$j], 0, 1 );
1525 if ( @$indicator[$j] ) {
1526 $ind2 = substr( @$indicator[$j], 1, 1 );
1529 warn "Indicator in @$tags[$i] is empty";
1533 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1535 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1543 if ( @$values[$i] ne "" ) {
1546 if ( @$tags[$i] eq "000" ) {
1547 $xml .= "<leader>@$values[$i]</leader>\n";
1550 # rest of the fixed fields
1552 elsif ( @$tags[$i] < 10 ) {
1554 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1558 my $ind1 = substr( @$indicator[$j], 0, 1 );
1559 my $ind2 = substr( @$indicator[$j], 1, 1 );
1561 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1563 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1569 else { # @$tags[$i] eq $prevtag
1570 if ( @$values[$i] eq "" ) {
1574 my $ind1 = substr( @$indicator[$j], 0, 1 );
1575 my $ind2 = substr( @$indicator[$j], 1, 1 );
1577 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1581 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1584 $prevtag = @$tags[$i];
1586 $xml .= "</datafield>\n" if @$tags > 0;
1587 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1588 # warn "SETTING 100 for $auth_type";
1589 use POSIX qw(strftime);
1590 my $string = strftime( "%Y%m%d", localtime(time) );
1591 # set 50 to position 26 is biblios, 13 if authorities
1593 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1594 $string = sprintf( "%-*s", 35, $string );
1595 substr( $string, $pos , 6, "50" );
1596 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1597 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1598 $xml .= "</datafield>\n";
1600 $xml .= MARC::File::XML::footer();
1604 =head2 TransformHtmlToMarc
1606 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1607 L<$params> is a ref to an array as below:
1609 'tag_010_indicator1_531951' ,
1610 'tag_010_indicator2_531951' ,
1611 'tag_010_code_a_531951_145735' ,
1612 'tag_010_subfield_a_531951_145735' ,
1613 'tag_200_indicator1_873510' ,
1614 'tag_200_indicator2_873510' ,
1615 'tag_200_code_a_873510_673465' ,
1616 'tag_200_subfield_a_873510_673465' ,
1617 'tag_200_code_b_873510_704318' ,
1618 'tag_200_subfield_b_873510_704318' ,
1619 'tag_200_code_e_873510_280822' ,
1620 'tag_200_subfield_e_873510_280822' ,
1621 'tag_200_code_f_873510_110730' ,
1622 'tag_200_subfield_f_873510_110730' ,
1624 L<$cgi> is the CGI object which containts the value.
1625 L<$record> is the MARC::Record object.
1629 sub TransformHtmlToMarc {
1633 # explicitly turn on the UTF-8 flag for all
1634 # 'tag_' parameters to avoid incorrect character
1635 # conversion later on
1636 my $cgi_params = $cgi->Vars;
1637 foreach my $param_name (keys %$cgi_params) {
1638 if ($param_name =~ /^tag_/) {
1639 my $param_value = $cgi_params->{$param_name};
1640 if (utf8::decode($param_value)) {
1641 $cgi_params->{$param_name} = $param_value;
1643 # FIXME - need to do something if string is not valid UTF-8
1647 # creating a new record
1648 my $record = MARC::Record->new();
1651 while ($params->[$i]){ # browse all CGI params
1652 my $param = $params->[$i];
1654 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1655 if ($param eq 'biblionumber') {
1656 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1657 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1658 if ($biblionumbertagfield < 10) {
1659 $newfield = MARC::Field->new(
1660 $biblionumbertagfield,
1661 $cgi->param($param),
1664 $newfield = MARC::Field->new(
1665 $biblionumbertagfield,
1668 "$biblionumbertagsubfield" => $cgi->param($param),
1671 push @fields,$newfield if($newfield);
1673 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1676 my $ind1 = substr($cgi->param($param),0,1);
1677 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1681 if($tag < 10){ # no code for theses fields
1682 # in MARC editor, 000 contains the leader.
1683 if ($tag eq '000' ) {
1684 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1685 # between 001 and 009 (included)
1686 } elsif ($cgi->param($params->[$j+1]) ne '') {
1687 $newfield = MARC::Field->new(
1689 $cgi->param($params->[$j+1]),
1692 # > 009, deal with subfields
1694 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1695 my $inner_param = $params->[$j];
1697 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1698 $newfield->add_subfields(
1699 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1703 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1704 $newfield = MARC::Field->new(
1708 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1715 push @fields,$newfield if($newfield);
1720 $record->append_fields(@fields);
1724 # cache inverted MARC field map
1725 our $inverted_field_map;
1727 =head2 TransformMarcToKoha
1731 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1735 Extract data from a MARC bib record into a hashref representing
1736 Koha biblio, biblioitems, and items fields.
1739 sub TransformMarcToKoha {
1740 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1743 $limit_table=$limit_table||0;
1744 $frameworkcode = '' unless defined $frameworkcode;
1746 unless (defined $inverted_field_map) {
1747 $inverted_field_map = _get_inverted_marc_field_map();
1751 if ( defined $limit_table && $limit_table eq 'items') {
1752 $tables{'items'} = 1;
1754 $tables{'items'} = 1;
1755 $tables{'biblio'} = 1;
1756 $tables{'biblioitems'} = 1;
1759 # traverse through record
1760 MARCFIELD: foreach my $field ($record->fields()) {
1761 my $tag = $field->tag();
1762 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1763 if ($field->is_control_field()) {
1764 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1765 ENTRY: foreach my $entry (@{ $kohafields }) {
1766 my ($subfield, $table, $column) = @{ $entry };
1767 next ENTRY unless exists $tables{$table};
1768 my $key = _disambiguate($table, $column);
1769 if ($result->{$key}) {
1770 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1771 $result->{$key} .= " | " . $field->data();
1774 $result->{$key} = $field->data();
1778 # deal with subfields
1779 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1780 my $code = $sf->[0];
1781 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1782 my $value = $sf->[1];
1783 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1784 my ($table, $column) = @{ $entry };
1785 next SFENTRY unless exists $tables{$table};
1786 my $key = _disambiguate($table, $column);
1787 if ($result->{$key}) {
1788 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1789 $result->{$key} .= " | " . $value;
1792 $result->{$key} = $value;
1799 # modify copyrightdate to keep only the 1st year found
1800 if (exists $result->{'copyrightdate'}) {
1801 my $temp = $result->{'copyrightdate'};
1802 $temp =~ m/c(\d\d\d\d)/;
1803 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1804 $result->{'copyrightdate'} = $1;
1806 else { # if no cYYYY, get the 1st date.
1807 $temp =~ m/(\d\d\d\d)/;
1808 $result->{'copyrightdate'} = $1;
1812 # modify publicationyear to keep only the 1st year found
1813 if (exists $result->{'publicationyear'}) {
1814 my $temp = $result->{'publicationyear'};
1815 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1816 $result->{'publicationyear'} = $1;
1818 else { # if no cYYYY, get the 1st date.
1819 $temp =~ m/(\d\d\d\d)/;
1820 $result->{'publicationyear'} = $1;
1827 sub _get_inverted_marc_field_map {
1829 my $relations = C4::Context->marcfromkohafield;
1831 foreach my $frameworkcode (keys %{ $relations }) {
1832 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1833 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1834 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1835 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1836 my ($table, $column) = split /[.]/, $kohafield, 2;
1837 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1838 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1844 =head2 _disambiguate
1848 $newkey = _disambiguate($table, $field);
1850 This is a temporary hack to distinguish between the
1851 following sets of columns when using TransformMarcToKoha.
1853 items.cn_source & biblioitems.cn_source
1854 items.cn_sort & biblioitems.cn_sort
1856 Columns that are currently NOT distinguished (FIXME
1857 due to lack of time to fully test) are:
1859 biblio.notes and biblioitems.notes
1864 FIXME - this is necessary because prefixing each column
1865 name with the table name would require changing lots
1866 of code and templates, and exposing more of the DB
1867 structure than is good to the UI templates, particularly
1868 since biblio and bibloitems may well merge in a future
1869 version. In the future, it would also be good to
1870 separate DB access and UI presentation field names
1878 my ($table, $column) = @_;
1879 if ($column eq "cn_sort" or $column eq "cn_source") {
1880 return $table . '.' . $column;
1887 =head2 get_koha_field_from_marc
1891 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1893 Internal function to map data from the MARC record to a specific non-MARC field.
1894 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1900 sub get_koha_field_from_marc {
1901 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1902 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1904 foreach my $field ( $record->field($tagfield) ) {
1905 if ( $field->tag() < 10 ) {
1907 $kohafield .= " | " . $field->data();
1910 $kohafield = $field->data();
1914 if ( $field->subfields ) {
1915 my @subfields = $field->subfields();
1916 foreach my $subfieldcount ( 0 .. $#subfields ) {
1917 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1920 " | " . $subfields[$subfieldcount][1];
1924 $subfields[$subfieldcount][1];
1935 =head2 TransformMarcToKohaOneField
1939 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1945 sub TransformMarcToKohaOneField {
1947 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1948 # only the 1st will be retrieved...
1949 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1951 my ( $tagfield, $subfield ) =
1952 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1954 foreach my $field ( $record->field($tagfield) ) {
1955 if ( $field->tag() < 10 ) {
1956 if ( $result->{$kohafield} ) {
1957 $result->{$kohafield} .= " | " . $field->data();
1960 $result->{$kohafield} = $field->data();
1964 if ( $field->subfields ) {
1965 my @subfields = $field->subfields();
1966 foreach my $subfieldcount ( 0 .. $#subfields ) {
1967 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1968 if ( $result->{$kohafield} ) {
1969 $result->{$kohafield} .=
1970 " | " . $subfields[$subfieldcount][1];
1973 $result->{$kohafield} =
1974 $subfields[$subfieldcount][1];
1984 =head1 OTHER FUNCTIONS
1987 =head2 PrepareItemrecordDisplay
1991 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1993 Returns a hash with all the fields for Display a given item data in a template
1999 sub PrepareItemrecordDisplay {
2001 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2003 my $dbh = C4::Context->dbh;
2004 my $frameworkcode = &GetFrameworkCode( $bibnum );
2005 my ( $itemtagfield, $itemtagsubfield ) =
2006 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2007 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2008 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2010 my $authorised_values_sth =
2012 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2014 foreach my $tag ( sort keys %{$tagslib} ) {
2015 my $previous_tag = '';
2017 # loop through each subfield
2019 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2020 next if ( subfield_is_koha_internal_p($subfield) );
2021 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2023 $subfield_data{tag} = $tag;
2024 $subfield_data{subfield} = $subfield;
2025 $subfield_data{countsubfield} = $cntsubf++;
2026 $subfield_data{kohafield} =
2027 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2029 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2030 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2031 $subfield_data{mandatory} =
2032 $tagslib->{$tag}->{$subfield}->{mandatory};
2033 $subfield_data{repeatable} =
2034 $tagslib->{$tag}->{$subfield}->{repeatable};
2035 $subfield_data{hidden} = "display:none"
2036 if $tagslib->{$tag}->{$subfield}->{hidden};
2038 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2040 $value =~ s/"/"/g;
2042 # search for itemcallnumber if applicable
2043 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2044 'items.itemcallnumber'
2045 && C4::Context->preference('itemcallnumber') )
2048 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2050 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2051 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2053 $value = $temp->subfield($CNsubfield);
2056 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2057 'items.itemcallnumber'
2058 && $defaultvalues->{'callnumber'} )
2060 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2062 $value = $defaultvalues->{'callnumber'};
2065 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2066 'items.holdingbranch' ||
2067 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2069 && $defaultvalues->{'branchcode'} )
2071 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2073 $value = $defaultvalues->{branchcode};
2076 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2077 my @authorised_values;
2080 # builds list, depending on authorised value...
2082 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2085 if ( ( C4::Context->preference("IndependantBranches") )
2086 && ( C4::Context->userenv->{flags} != 1 ) )
2090 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2092 $sth->execute( C4::Context->userenv->{branch} );
2093 push @authorised_values, ""
2095 $tagslib->{$tag}->{$subfield}->{mandatory} );
2096 while ( my ( $branchcode, $branchname ) =
2097 $sth->fetchrow_array )
2099 push @authorised_values, $branchcode;
2100 $authorised_lib{$branchcode} = $branchname;
2106 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2109 push @authorised_values, ""
2111 $tagslib->{$tag}->{$subfield}->{mandatory} );
2112 while ( my ( $branchcode, $branchname ) =
2113 $sth->fetchrow_array )
2115 push @authorised_values, $branchcode;
2116 $authorised_lib{$branchcode} = $branchname;
2122 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2127 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2130 push @authorised_values, ""
2131 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2132 while ( my ( $itemtype, $description ) =
2133 $sth->fetchrow_array )
2135 push @authorised_values, $itemtype;
2136 $authorised_lib{$itemtype} = $description;
2139 #---- "true" authorised value
2142 $authorised_values_sth->execute(
2143 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2144 push @authorised_values, ""
2145 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2146 while ( my ( $value, $lib ) =
2147 $authorised_values_sth->fetchrow_array )
2149 push @authorised_values, $value;
2150 $authorised_lib{$value} = $lib;
2153 $subfield_data{marc_value} = CGI::scrolling_list(
2154 -name => 'field_value',
2155 -values => \@authorised_values,
2156 -default => "$value",
2157 -labels => \%authorised_lib,
2163 elsif ( $tagslib->{$tag}->{$subfield}->{thesaurus_category} ) {
2164 $subfield_data{marc_value} =
2165 "<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>";
2168 # COMMENTED OUT because No $i is provided with this API.
2169 # And thus, no value_builder can be activated.
2170 # BUT could be thought over.
2171 # } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
2172 # my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
2174 # my $extended_param = plugin_parameters($dbh,$itemrecord,$tagslib,$i,0);
2175 # my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
2176 # $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";
2179 $subfield_data{marc_value} =
2180 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2182 push( @loop_data, \%subfield_data );
2186 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2187 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2189 'itemtagfield' => $itemtagfield,
2190 'itemtagsubfield' => $itemtagsubfield,
2191 'itemnumber' => $itemnumber,
2192 'iteminformation' => \@loop_data
2198 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2200 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2201 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2202 # =head2 ModZebrafiles
2204 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2208 # sub ModZebrafiles {
2210 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2214 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2215 # unless ( opendir( DIR, "$zebradir" ) ) {
2216 # warn "$zebradir not found";
2220 # my $filename = $zebradir . $biblionumber;
2223 # open( OUTPUT, ">", $filename . ".xml" );
2224 # print OUTPUT $record;
2233 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2235 $biblionumber is the biblionumber we want to index
2236 $op is specialUpdate or delete, and is used to know what we want to do
2237 $server is the server that we want to update
2238 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2239 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2241 $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.
2248 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2249 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2250 my $dbh=C4::Context->dbh;
2252 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2254 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2255 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2257 if (C4::Context->preference("NoZebra")) {
2258 # lock the nozebra table : we will read index lines, update them in Perl process
2259 # and write everything in 1 transaction.
2260 # lock the table to avoid someone else overwriting what we are doing
2261 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2262 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2263 if ($op eq 'specialUpdate') {
2264 # OK, we have to add or update the record
2265 # 1st delete (virtually, in indexes), if record actually exists
2267 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2269 # ... add the record
2270 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2272 # it's a deletion, delete the record...
2273 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2274 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2276 # ok, now update the database...
2277 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2278 foreach my $key (keys %result) {
2279 foreach my $index (keys %{$result{$key}}) {
2280 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2283 $dbh->do('UNLOCK TABLES');
2286 # we use zebra, just fill zebraqueue table
2288 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2290 AND biblio_auth_number = ?
2293 my $check_sth = $dbh->prepare_cached($check_sql);
2294 $check_sth->execute($server, $biblionumber, $op);
2295 my ($count) = $check_sth->fetchrow_array;
2296 $check_sth->finish();
2298 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2299 $sth->execute($biblionumber,$server,$op);
2305 =head2 GetNoZebraIndexes
2307 %indexes = GetNoZebraIndexes;
2309 return the data from NoZebraIndexes syspref.
2313 sub GetNoZebraIndexes {
2314 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2316 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2317 $line =~ /(.*)=>(.*)/;
2318 my $index = $1; # initial ' or " is removed afterwards
2320 $index =~ s/'|"|\s//g;
2321 $fields =~ s/'|"|\s//g;
2322 $indexes{$index}=$fields;
2327 =head1 INTERNAL FUNCTIONS
2329 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2331 function to delete a biblio in NoZebra indexes
2332 This function does NOT delete anything in database : it reads all the indexes entries
2333 that have to be deleted & delete them in the hash
2334 The SQL part is done either :
2335 - after the Add if we are modifying a biblio (delete + add again)
2336 - immediatly after this sub if we are doing a true deletion.
2337 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2342 sub _DelBiblioNoZebra {
2343 my ($biblionumber, $record, $server)=@_;
2346 my $dbh = C4::Context->dbh;
2350 if ($server eq 'biblioserver') {
2351 %index=GetNoZebraIndexes;
2352 # get title of the record (to store the 10 first letters with the index)
2353 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2354 $title = lc($record->subfield($titletag,$titlesubfield));
2356 # for authorities, the "title" is the $a mainentry
2357 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2358 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2359 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2360 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2361 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2362 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2363 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2367 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2368 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2369 # limit to 10 char, should be enough, and limit the DB size
2370 $title = substr($title,0,10);
2372 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2373 foreach my $field ($record->fields()) {
2374 #parse each subfield
2375 next if $field->tag <10;
2376 foreach my $subfield ($field->subfields()) {
2377 my $tag = $field->tag();
2378 my $subfieldcode = $subfield->[0];
2380 # check each index to see if the subfield is stored somewhere
2381 # otherwise, store it in __RAW__ index
2382 foreach my $key (keys %index) {
2383 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2384 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2386 my $line= lc $subfield->[1];
2387 # remove meaningless value in the field...
2388 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2389 # ... and split in words
2390 foreach (split / /,$line) {
2391 next unless $_; # skip empty values (multiple spaces)
2392 # if the entry is already here, do nothing, the biblionumber has already be removed
2393 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2394 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2395 $sth2->execute($server,$key,$_);
2396 my $existing_biblionumbers = $sth2->fetchrow;
2398 if ($existing_biblionumbers) {
2399 # warn " existing for $key $_: $existing_biblionumbers";
2400 $result{$key}->{$_} =$existing_biblionumbers;
2401 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2407 # the subfield is not indexed, store it in __RAW__ index anyway
2409 my $line= lc $subfield->[1];
2410 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2411 # ... and split in words
2412 foreach (split / /,$line) {
2413 next unless $_; # skip empty values (multiple spaces)
2414 # if the entry is already here, do nothing, the biblionumber has already be removed
2415 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2416 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2417 $sth2->execute($server,'__RAW__',$_);
2418 my $existing_biblionumbers = $sth2->fetchrow;
2420 if ($existing_biblionumbers) {
2421 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2422 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2432 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2434 function to add a biblio in NoZebra indexes
2438 sub _AddBiblioNoZebra {
2439 my ($biblionumber, $record, $server, %result)=@_;
2440 my $dbh = C4::Context->dbh;
2444 if ($server eq 'biblioserver') {
2445 %index=GetNoZebraIndexes;
2446 # get title of the record (to store the 10 first letters with the index)
2447 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2448 $title = lc($record->subfield($titletag,$titlesubfield));
2450 # warn "server : $server";
2451 # for authorities, the "title" is the $a mainentry
2452 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2453 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2454 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2455 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2456 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2457 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2458 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2461 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2462 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2463 # limit to 10 char, should be enough, and limit the DB size
2464 $title = substr($title,0,10);
2466 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2467 foreach my $field ($record->fields()) {
2468 #parse each subfield
2469 ###FIXME: impossible to index a 001-009 value with NoZebra
2470 next if $field->tag <10;
2471 foreach my $subfield ($field->subfields()) {
2472 my $tag = $field->tag();
2473 my $subfieldcode = $subfield->[0];
2475 # warn "INDEXING :".$subfield->[1];
2476 # check each index to see if the subfield is stored somewhere
2477 # otherwise, store it in __RAW__ index
2478 foreach my $key (keys %index) {
2479 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2480 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2482 my $line= lc $subfield->[1];
2483 # remove meaningless value in the field...
2484 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2485 # ... and split in words
2486 foreach (split / /,$line) {
2487 next unless $_; # skip empty values (multiple spaces)
2488 # if the entry is already here, improve weight
2489 # warn "managing $_";
2490 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2491 my $weight = $1 + 1;
2492 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2493 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2495 # get the value if it exist in the nozebra table, otherwise, create it
2496 $sth2->execute($server,$key,$_);
2497 my $existing_biblionumbers = $sth2->fetchrow;
2499 if ($existing_biblionumbers) {
2500 $result{$key}->{"$_"} =$existing_biblionumbers;
2501 my $weight = defined $1 ? $1 + 1 : 1;
2502 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2503 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2504 # create a new ligne for this entry
2506 # warn "INSERT : $server / $key / $_";
2507 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2508 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2514 # the subfield is not indexed, store it in __RAW__ index anyway
2516 my $line= lc $subfield->[1];
2517 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2518 # ... and split in words
2519 foreach (split / /,$line) {
2520 next unless $_; # skip empty values (multiple spaces)
2521 # if the entry is already here, improve weight
2522 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2524 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2525 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2527 # get the value if it exist in the nozebra table, otherwise, create it
2528 $sth2->execute($server,'__RAW__',$_);
2529 my $existing_biblionumbers = $sth2->fetchrow;
2531 if ($existing_biblionumbers) {
2532 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2534 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2535 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2536 # create a new ligne for this entry
2538 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2539 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2554 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2556 Find the given $subfield in the given $tag in the given
2557 MARC::Record $record. If the subfield is found, returns
2558 the (indicators, value) pair; otherwise, (undef, undef) is
2562 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2563 I suggest we export it from this module.
2570 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2573 if ( $tagfield < 10 ) {
2574 if ( $record->field($tagfield) ) {
2575 push @result, $record->field($tagfield)->data();
2582 foreach my $field ( $record->field($tagfield) ) {
2583 my @subfields = $field->subfields();
2584 foreach my $subfield (@subfields) {
2585 if ( @$subfield[0] eq $insubfield ) {
2586 push @result, @$subfield[1];
2587 $indicator = $field->indicator(1) . $field->indicator(2);
2592 return ( $indicator, @result );
2595 =head2 _koha_marc_update_bib_ids
2599 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2601 Internal function to add or update biblionumber and biblioitemnumber to
2608 sub _koha_marc_update_bib_ids {
2609 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2611 # we must add bibnum and bibitemnum in MARC::Record...
2612 # we build the new field with biblionumber and biblioitemnumber
2613 # we drop the original field
2614 # we add the new builded field.
2615 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2616 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2618 if ($biblio_tag != $biblioitem_tag) {
2619 # biblionumber & biblioitemnumber are in different fields
2621 # deal with biblionumber
2622 my ($new_field, $old_field);
2623 if ($biblio_tag < 10) {
2624 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2627 MARC::Field->new( $biblio_tag, '', '',
2628 "$biblio_subfield" => $biblionumber );
2631 # drop old field and create new one...
2632 $old_field = $record->field($biblio_tag);
2633 $record->delete_field($old_field) if $old_field;
2634 $record->append_fields($new_field);
2636 # deal with biblioitemnumber
2637 if ($biblioitem_tag < 10) {
2638 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2641 MARC::Field->new( $biblioitem_tag, '', '',
2642 "$biblioitem_subfield" => $biblioitemnumber, );
2644 # drop old field and create new one...
2645 $old_field = $record->field($biblioitem_tag);
2646 $record->delete_field($old_field) if $old_field;
2647 $record->insert_fields_ordered($new_field);
2650 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2651 my $new_field = MARC::Field->new(
2652 $biblio_tag, '', '',
2653 "$biblio_subfield" => $biblionumber,
2654 "$biblioitem_subfield" => $biblioitemnumber
2657 # drop old field and create new one...
2658 my $old_field = $record->field($biblio_tag);
2659 $record->delete_field($old_field) if $old_field;
2660 $record->insert_fields_ordered($new_field);
2664 =head2 _koha_marc_update_biblioitem_cn_sort
2668 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2672 Given a MARC bib record and the biblioitem hash, update the
2673 subfield that contains a copy of the value of biblioitems.cn_sort.
2677 sub _koha_marc_update_biblioitem_cn_sort {
2679 my $biblioitem = shift;
2680 my $frameworkcode= shift;
2682 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2683 return unless $biblioitem_tag;
2685 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2687 if (my $field = $marc->field($biblioitem_tag)) {
2688 $field->delete_subfield(code => $biblioitem_subfield);
2689 if ($cn_sort ne '') {
2690 $field->add_subfields($biblioitem_subfield => $cn_sort);
2693 # if we get here, no biblioitem tag is present in the MARC record, so
2694 # we'll create it if $cn_sort is not empty -- this would be
2695 # an odd combination of events, however
2697 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2702 =head2 _koha_add_biblio
2706 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2708 Internal function to add a biblio ($biblio is a hash with the values)
2714 sub _koha_add_biblio {
2715 my ( $dbh, $biblio, $frameworkcode ) = @_;
2719 # set the series flag
2721 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2725 SET frameworkcode = ?,
2736 my $sth = $dbh->prepare($query);
2739 $biblio->{'author'},
2741 $biblio->{'unititle'},
2744 $biblio->{'seriestitle'},
2745 $biblio->{'copyrightdate'},
2746 $biblio->{'abstract'}
2749 my $biblionumber = $dbh->{'mysql_insertid'};
2750 if ( $dbh->errstr ) {
2751 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2756 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2757 return ($biblionumber,$error);
2760 =head2 _koha_modify_biblio
2764 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2766 Internal function for updating the biblio table
2772 sub _koha_modify_biblio {
2773 my ( $dbh, $biblio, $frameworkcode ) = @_;
2778 SET frameworkcode = ?,
2787 WHERE biblionumber = ?
2790 my $sth = $dbh->prepare($query);
2794 $biblio->{'author'},
2796 $biblio->{'unititle'},
2798 $biblio->{'serial'},
2799 $biblio->{'seriestitle'},
2800 $biblio->{'copyrightdate'},
2801 $biblio->{'abstract'},
2802 $biblio->{'biblionumber'}
2803 ) if $biblio->{'biblionumber'};
2805 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2806 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2809 return ( $biblio->{'biblionumber'},$error );
2812 =head2 _koha_modify_biblioitem_nonmarc
2816 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2818 Updates biblioitems row except for marc and marcxml, which should be changed
2825 sub _koha_modify_biblioitem_nonmarc {
2826 my ( $dbh, $biblioitem ) = @_;
2829 # re-calculate the cn_sort, it may have changed
2830 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2834 SET biblionumber = ?,
2840 publicationyear = ?,
2844 collectiontitle = ?,
2846 collectionvolume= ?,
2847 editionstatement= ?,
2848 editionresponsibility = ?,
2862 where biblioitemnumber = ?
2864 my $sth = $dbh->prepare($query);
2866 $biblioitem->{'biblionumber'},
2867 $biblioitem->{'volume'},
2868 $biblioitem->{'number'},
2869 $biblioitem->{'itemtype'},
2870 $biblioitem->{'isbn'},
2871 $biblioitem->{'issn'},
2872 $biblioitem->{'publicationyear'},
2873 $biblioitem->{'publishercode'},
2874 $biblioitem->{'volumedate'},
2875 $biblioitem->{'volumedesc'},
2876 $biblioitem->{'collectiontitle'},
2877 $biblioitem->{'collectionissn'},
2878 $biblioitem->{'collectionvolume'},
2879 $biblioitem->{'editionstatement'},
2880 $biblioitem->{'editionresponsibility'},
2881 $biblioitem->{'illus'},
2882 $biblioitem->{'pages'},
2883 $biblioitem->{'bnotes'},
2884 $biblioitem->{'size'},
2885 $biblioitem->{'place'},
2886 $biblioitem->{'lccn'},
2887 $biblioitem->{'url'},
2888 $biblioitem->{'biblioitems.cn_source'},
2889 $biblioitem->{'cn_class'},
2890 $biblioitem->{'cn_item'},
2891 $biblioitem->{'cn_suffix'},
2893 $biblioitem->{'totalissues'},
2894 $biblioitem->{'biblioitemnumber'}
2896 if ( $dbh->errstr ) {
2897 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2900 return ($biblioitem->{'biblioitemnumber'},$error);
2903 =head2 _koha_add_biblioitem
2907 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2909 Internal function to add a biblioitem
2915 sub _koha_add_biblioitem {
2916 my ( $dbh, $biblioitem ) = @_;
2919 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2921 "INSERT INTO biblioitems SET
2928 publicationyear = ?,
2932 collectiontitle = ?,
2934 collectionvolume= ?,
2935 editionstatement= ?,
2936 editionresponsibility = ?,
2952 my $sth = $dbh->prepare($query);
2954 $biblioitem->{'biblionumber'},
2955 $biblioitem->{'volume'},
2956 $biblioitem->{'number'},
2957 $biblioitem->{'itemtype'},
2958 $biblioitem->{'isbn'},
2959 $biblioitem->{'issn'},
2960 $biblioitem->{'publicationyear'},
2961 $biblioitem->{'publishercode'},
2962 $biblioitem->{'volumedate'},
2963 $biblioitem->{'volumedesc'},
2964 $biblioitem->{'collectiontitle'},
2965 $biblioitem->{'collectionissn'},
2966 $biblioitem->{'collectionvolume'},
2967 $biblioitem->{'editionstatement'},
2968 $biblioitem->{'editionresponsibility'},
2969 $biblioitem->{'illus'},
2970 $biblioitem->{'pages'},
2971 $biblioitem->{'bnotes'},
2972 $biblioitem->{'size'},
2973 $biblioitem->{'place'},
2974 $biblioitem->{'lccn'},
2975 $biblioitem->{'marc'},
2976 $biblioitem->{'url'},
2977 $biblioitem->{'biblioitems.cn_source'},
2978 $biblioitem->{'cn_class'},
2979 $biblioitem->{'cn_item'},
2980 $biblioitem->{'cn_suffix'},
2982 $biblioitem->{'totalissues'}
2984 my $bibitemnum = $dbh->{'mysql_insertid'};
2985 if ( $dbh->errstr ) {
2986 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2990 return ($bibitemnum,$error);
2993 =head2 _koha_delete_biblio
2997 $error = _koha_delete_biblio($dbh,$biblionumber);
2999 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3001 C<$dbh> - the database handle
3002 C<$biblionumber> - the biblionumber of the biblio to be deleted
3008 # FIXME: add error handling
3010 sub _koha_delete_biblio {
3011 my ( $dbh, $biblionumber ) = @_;
3013 # get all the data for this biblio
3014 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3015 $sth->execute($biblionumber);
3017 if ( my $data = $sth->fetchrow_hashref ) {
3019 # save the record in deletedbiblio
3020 # find the fields to save
3021 my $query = "INSERT INTO deletedbiblio SET ";
3023 foreach my $temp ( keys %$data ) {
3024 $query .= "$temp = ?,";
3025 push( @bind, $data->{$temp} );
3028 # replace the last , by ",?)"
3030 my $bkup_sth = $dbh->prepare($query);
3031 $bkup_sth->execute(@bind);
3035 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3036 $del_sth->execute($biblionumber);
3043 =head2 _koha_delete_biblioitems
3047 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3049 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3051 C<$dbh> - the database handle
3052 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3058 # FIXME: add error handling
3060 sub _koha_delete_biblioitems {
3061 my ( $dbh, $biblioitemnumber ) = @_;
3063 # get all the data for this biblioitem
3065 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3066 $sth->execute($biblioitemnumber);
3068 if ( my $data = $sth->fetchrow_hashref ) {
3070 # save the record in deletedbiblioitems
3071 # find the fields to save
3072 my $query = "INSERT INTO deletedbiblioitems SET ";
3074 foreach my $temp ( keys %$data ) {
3075 $query .= "$temp = ?,";
3076 push( @bind, $data->{$temp} );
3079 # replace the last , by ",?)"
3081 my $bkup_sth = $dbh->prepare($query);
3082 $bkup_sth->execute(@bind);
3085 # delete the biblioitem
3087 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3088 $del_sth->execute($biblioitemnumber);
3095 =head1 UNEXPORTED FUNCTIONS
3097 =head2 ModBiblioMarc
3099 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3101 Add MARC data for a biblio to koha
3103 Function exported, but should NOT be used, unless you really know what you're doing
3109 # pass the MARC::Record to this function, and it will create the records in the marc field
3110 my ( $record, $biblionumber, $frameworkcode ) = @_;
3111 my $dbh = C4::Context->dbh;
3112 my @fields = $record->fields();
3113 if ( !$frameworkcode ) {
3114 $frameworkcode = "";
3117 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3118 $sth->execute( $frameworkcode, $biblionumber );
3120 my $encoding = C4::Context->preference("marcflavour");
3122 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3123 if ( $encoding eq "UNIMARC" ) {
3125 if ( length($record->subfield( 100, "a" )) == 35 ) {
3126 $string = $record->subfield( 100, "a" );
3127 my $f100 = $record->field(100);
3128 $record->delete_field($f100);
3131 $string = POSIX::strftime( "%Y%m%d", localtime );
3133 $string = sprintf( "%-*s", 35, $string );
3135 substr( $string, 22, 6, "frey50" );
3136 unless ( $record->subfield( 100, "a" ) ) {
3137 $record->insert_grouped_field(
3138 MARC::Field->new( 100, "", "", "a" => $string ) );
3142 if (C4::Context->preference("NoZebra")) {
3143 # only NoZebra indexing needs to have
3144 # the previous version of the record
3145 $oldRecord = GetMarcBiblio($biblionumber);
3149 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3150 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3153 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3154 return $biblionumber;
3157 =head2 z3950_extended_services
3159 z3950_extended_services($serviceType,$serviceOptions,$record);
3161 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.
3163 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3165 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3167 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3171 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3172 syntax => the record syntax (transfer syntax)
3173 databaseName = Database from connection object
3175 To set serviceOptions, call set_service_options($serviceType)
3177 C<$record> the record, if one is needed for the service type
3179 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3183 sub z3950_extended_services {
3184 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3186 # get our connection object
3187 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3189 # create a new package object
3190 my $Zpackage = $Zconn->package();
3193 $Zpackage->option( action => $action );
3195 if ( $serviceOptions->{'databaseName'} ) {
3196 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3198 if ( $serviceOptions->{'recordIdNumber'} ) {
3200 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3202 if ( $serviceOptions->{'recordIdOpaque'} ) {
3204 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3207 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3208 #if ($serviceType eq 'itemorder') {
3209 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3210 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3211 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3212 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3215 if ( $serviceOptions->{record} ) {
3216 $Zpackage->option( record => $serviceOptions->{record} );
3218 # can be xml or marc
3219 if ( $serviceOptions->{'syntax'} ) {
3220 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3224 # send the request, handle any exception encountered
3225 eval { $Zpackage->send($serviceType) };
3226 if ( $@ && $@->isa("ZOOM::Exception") ) {
3227 return "error: " . $@->code() . " " . $@->message() . "\n";
3230 # free up package resources
3231 $Zpackage->destroy();
3234 =head2 set_service_options
3236 my $serviceOptions = set_service_options($serviceType);
3238 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3240 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3244 sub set_service_options {
3245 my ($serviceType) = @_;
3248 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3249 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3251 if ( $serviceType eq 'commit' ) {
3255 if ( $serviceType eq 'create' ) {
3259 if ( $serviceType eq 'drop' ) {
3260 die "ERROR: 'drop' not currently supported (by Zebra)";
3262 return $serviceOptions;
3265 =head3 get_biblio_authorised_values
3267 find the types and values for all authorised values assigned to this biblio.
3272 returns: a hashref malling the authorised value to the value set for this biblionumber
3274 $authorised_values = {
3275 'Scent' => 'flowery',
3276 'Audience' => 'Young Adult',
3277 'itemtypes' => 'SER',
3280 Notes: forlibrarian should probably be passed in, and called something different.
3285 sub get_biblio_authorised_values {
3286 my $biblionumber = shift;
3288 my $forlibrarian = 1; # are we in staff or opac?
3289 my $frameworkcode = GetFrameworkCode( $biblionumber );
3291 my $authorised_values;
3293 my $record = GetMarcBiblio( $biblionumber )
3294 or return $authorised_values;
3295 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3296 or return $authorised_values;
3298 # assume that these entries in the authorised_value table are bibliolevel.
3299 # ones that start with 'item%' are item level.
3300 my $query = q(SELECT distinct authorised_value, kohafield
3301 FROM marc_subfield_structure
3302 WHERE authorised_value !=''
3303 AND (kohafield like 'biblio%'
3304 OR kohafield like '') );
3305 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3307 foreach my $tag ( keys( %$tagslib ) ) {
3308 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3309 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3310 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3311 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3312 if ( defined $record->field( $tag ) ) {
3313 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3314 if ( defined $this_subfield_value ) {
3315 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3322 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3323 return $authorised_values;
3333 Koha Developement team <info@koha.org>
3335 Paul POULAIN paul.poulain@free.fr
3337 Joshua Ferraro jmf@liblime.com