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";
29 use POSIX qw(strftime);
32 use C4::Dates qw/format_date/;
33 use C4::Log; # logaction
39 use vars qw($VERSION @ISA @EXPORT);
45 @ISA = qw( Exporter );
59 &GetBiblioItemByBiblioNumber
60 &GetBiblioFromItemNumber
72 &GetAuthorisedValueDesc
76 &GetPublisherNameFromIsbn
91 # To link headings in a bib record
92 # to authority records.
94 &LinkBibHeadingsToAuthorities
98 # those functions are exported but should not be used
99 # they are usefull is few circumstances, so are exported.
100 # but don't use them unless you're a core developer ;-)
107 &TransformHtmlToMarc2
110 &PrepareItemrecordDisplay
117 C4::Biblio - cataloging management functions
121 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:
125 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
127 =item 2. as raw MARC in the Zebra index and storage engine
129 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
133 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
135 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.
139 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
141 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
145 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:
149 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
151 =item 2. _koha_* - low-level internal functions for managing the koha tables
153 =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.
155 =item 4. Zebra functions used to update the Zebra index
157 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
161 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 :
165 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
167 =item 2. add the biblionumber and biblioitemnumber into the MARC records
169 =item 3. save the marc record
173 When dealing with items, we must :
177 =item 1. save the item in items table, that gives us an itemnumber
179 =item 2. add the itemnumber to the item MARC field
181 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
183 When modifying a biblio or an item, the behaviour is quite similar.
187 =head1 EXPORTED FUNCTIONS
193 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
197 Exported function (core API) for adding a new biblio to koha.
199 The first argument is a C<MARC::Record> object containing the
200 bib to add, while the second argument is the desired MARC
203 This function also accepts a third, optional argument: a hashref
204 to additional options. The only defined option is C<defer_marc_save>,
205 which if present and mapped to a true value, causes C<AddBiblio>
206 to omit the call to save the MARC in C<bibilioitems.marc>
207 and C<biblioitems.marcxml> This option is provided B<only>
208 for the use of scripts such as C<bulkmarcimport.pl> that may need
209 to do some manipulation of the MARC record for item parsing before
210 saving it and which cannot afford the performance hit of saving
211 the MARC record twice. Consequently, do not use that option
212 unless you can guarantee that C<ModBiblioMarc> will be called.
218 my $frameworkcode = shift;
219 my $options = @_ ? shift : undef;
220 my $defer_marc_save = 0;
221 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
222 $defer_marc_save = 1;
225 my ($biblionumber,$biblioitemnumber,$error);
226 my $dbh = C4::Context->dbh;
227 # transform the data into koha-table style data
228 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
229 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
230 $olddata->{'biblionumber'} = $biblionumber;
231 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
233 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
235 # update MARC subfield that stores biblioitems.cn_sort
236 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
239 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
241 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
243 return ( $biblionumber, $biblioitemnumber );
250 ModBiblio( $record,$biblionumber,$frameworkcode);
254 Replace an existing bib record identified by C<$biblionumber>
255 with one supplied by the MARC::Record object C<$record>. The embedded
256 item, biblioitem, and biblionumber fields from the previous
257 version of the bib record replace any such fields of those tags that
258 are present in C<$record>. Consequently, ModBiblio() is not
259 to be used to try to modify item records.
261 C<$frameworkcode> specifies the MARC framework to use
262 when storing the modified bib record; among other things,
263 this controls how MARC fields get mapped to display columns
264 in the C<biblio> and C<biblioitems> tables, as well as
265 which fields are used to store embedded item, biblioitem,
266 and biblionumber data for indexing.
271 my ( $record, $biblionumber, $frameworkcode ) = @_;
272 if (C4::Context->preference("CataloguingLog")) {
273 my $newrecord = GetMarcBiblio($biblionumber);
274 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
277 my $dbh = C4::Context->dbh;
279 $frameworkcode = "" unless $frameworkcode;
281 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
282 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
283 my $oldRecord = GetMarcBiblio( $biblionumber );
285 # delete any item fields from incoming record to avoid
286 # duplication or incorrect data - use AddItem() or ModItem()
288 foreach my $field ($record->field($itemtag)) {
289 $record->delete_field($field);
292 # once all the items fields are removed, copy the old ones, in order to keep synchronize
293 $record->append_fields($oldRecord->field( $itemtag ));
295 # update biblionumber and biblioitemnumber in MARC
296 # FIXME - this is assuming a 1 to 1 relationship between
297 # biblios and biblioitems
298 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
299 $sth->execute($biblionumber);
300 my ($biblioitemnumber) = $sth->fetchrow;
302 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
304 # load the koha-table data object
305 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
307 # update MARC subfield that stores biblioitems.cn_sort
308 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
310 # update the MARC record (that now contains biblio and items) with the new record data
311 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
313 # modify the other koha tables
314 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
315 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
319 =head2 ModBiblioframework
321 ModBiblioframework($biblionumber,$frameworkcode);
322 Exported function to modify a biblio framework
326 sub ModBiblioframework {
327 my ( $biblionumber, $frameworkcode ) = @_;
328 my $dbh = C4::Context->dbh;
329 my $sth = $dbh->prepare(
330 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
332 $sth->execute($frameworkcode, $biblionumber);
340 my $error = &DelBiblio($dbh,$biblionumber);
341 Exported function (core API) for deleting a biblio in koha.
342 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
343 Also backs it up to deleted* tables
344 Checks to make sure there are not issues on any of the items
346 C<$error> : undef unless an error occurs
353 my ( $biblionumber ) = @_;
354 my $dbh = C4::Context->dbh;
355 my $error; # for error handling
357 # First make sure this biblio has no items attached
358 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
359 $sth->execute($biblionumber);
360 if (my $itemnumber = $sth->fetchrow){
361 # Fix this to use a status the template can understand
362 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
365 return $error if $error;
367 # We delete attached subscriptions
368 if(C4::Serials::CountSubscriptionFromBiblionumber($biblionumber) != 0){
369 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
370 foreach my $subscription (@$subscriptions){
371 &C4::Serials::DelSubscription($subscription->{subscriptionid});
375 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
376 # for at least 2 reasons :
377 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
378 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
379 # 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)
381 if (C4::Context->preference("NoZebra")) {
382 # only NoZebra indexing needs to have
383 # the previous version of the record
384 $oldRecord = GetMarcBiblio($biblionumber);
386 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
388 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
391 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
392 $sth->execute($biblionumber);
393 while ( my $biblioitemnumber = $sth->fetchrow ) {
395 # delete this biblioitem
396 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
397 return $error if $error;
400 # delete biblio from Koha tables and save in deletedbiblio
401 # must do this *after* _koha_delete_biblioitems, otherwise
402 # delete cascade will prevent deletedbiblioitems rows
403 # from being generated by _koha_delete_biblioitems
404 $error = _koha_delete_biblio( $dbh, $biblionumber );
406 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
411 =head2 LinkBibHeadingsToAuthorities
415 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
419 Links bib headings to authority records by checking
420 each authority-controlled field in the C<MARC::Record>
421 object C<$marc>, looking for a matching authority record,
422 and setting the linking subfield $9 to the ID of that
425 If no matching authority exists, or if multiple
426 authorities match, no $9 will be added, and any
427 existing one inthe field will be deleted.
429 Returns the number of heading links changed in the
434 sub LinkBibHeadingsToAuthorities {
437 my $num_headings_changed = 0;
438 foreach my $field ($bib->fields()) {
439 my $heading = C4::Heading->new_from_bib_field($field);
440 next unless defined $heading;
443 my $current_link = $field->subfield('9');
445 # look for matching authorities
446 my $authorities = $heading->authorities();
448 # want only one exact match
449 if ($#{ $authorities } == 0) {
450 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
451 my $authid = $authority->field('001')->data();
452 next if defined $current_link and $current_link eq $authid;
454 $field->delete_subfield(code => '9') if defined $current_link;
455 $field->add_subfields('9', $authid);
456 $num_headings_changed++;
458 if (defined $current_link) {
459 $field->delete_subfield(code => '9');
460 $num_headings_changed++;
465 return $num_headings_changed;
472 $data = &GetBiblioData($biblionumber);
473 Returns information about the book with the given biblionumber.
474 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
475 the C<biblio> and C<biblioitems> tables in the
477 In addition, C<$data-E<gt>{subject}> is the list of the book's
478 subjects, separated by C<" , "> (space, comma, space).
479 If there are multiple biblioitems with the given biblionumber, only
480 the first one is considered.
488 my $dbh = C4::Context->dbh;
490 # my $query = C4::Context->preference('item-level_itypes') ?
491 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
493 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
494 # WHERE biblio.biblionumber = ?
495 # AND biblioitems.biblionumber = biblio.biblionumber
498 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
500 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
501 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
502 WHERE biblio.biblionumber = ?
503 AND biblioitems.biblionumber = biblio.biblionumber ";
505 my $sth = $dbh->prepare($query);
506 $sth->execute($bibnum);
508 $data = $sth->fetchrow_hashref;
512 } # sub GetBiblioData
514 =head2 &GetBiblioItemData
518 $itemdata = &GetBiblioItemData($biblioitemnumber);
520 Looks up the biblioitem with the given biblioitemnumber. Returns a
521 reference-to-hash. The keys are the fields from the C<biblio>,
522 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
523 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
530 sub GetBiblioItemData {
531 my ($biblioitemnumber) = @_;
532 my $dbh = C4::Context->dbh;
533 my $query = "SELECT *,biblioitems.notes AS bnotes
534 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
535 unless(C4::Context->preference('item-level_itypes')) {
536 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
538 $query .= " WHERE biblioitemnumber = ? ";
539 my $sth = $dbh->prepare($query);
541 $sth->execute($biblioitemnumber);
542 $data = $sth->fetchrow_hashref;
545 } # sub &GetBiblioItemData
547 =head2 GetBiblioItemByBiblioNumber
551 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
557 sub GetBiblioItemByBiblioNumber {
558 my ($biblionumber) = @_;
559 my $dbh = C4::Context->dbh;
560 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
564 $sth->execute($biblionumber);
566 while ( my $data = $sth->fetchrow_hashref ) {
567 push @results, $data;
574 =head2 GetBiblioFromItemNumber
578 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
580 Looks up the item with the given itemnumber. if undef, try the barcode.
582 C<&itemnodata> returns a reference-to-hash whose keys are the fields
583 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
591 sub GetBiblioFromItemNumber {
592 my ( $itemnumber, $barcode ) = @_;
593 my $dbh = C4::Context->dbh;
596 $sth=$dbh->prepare( "SELECT * FROM items
597 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
598 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
599 WHERE items.itemnumber = ?") ;
600 $sth->execute($itemnumber);
602 $sth=$dbh->prepare( "SELECT * FROM items
603 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
604 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
605 WHERE items.barcode = ?") ;
606 $sth->execute($barcode);
608 my $data = $sth->fetchrow_hashref;
617 ( $count, @results ) = &GetBiblio($biblionumber);
624 my ($biblionumber) = @_;
625 my $dbh = C4::Context->dbh;
626 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
629 $sth->execute($biblionumber);
630 while ( my $data = $sth->fetchrow_hashref ) {
631 $results[$count] = $data;
635 return ( $count, @results );
638 =head2 GetBiblioItemInfosOf
642 GetBiblioItemInfosOf(@biblioitemnumbers);
648 sub GetBiblioItemInfosOf {
649 my @biblioitemnumbers = @_;
652 SELECT biblioitemnumber,
656 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
658 return get_infos_of( $query, 'biblioitemnumber' );
661 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
663 =head2 GetMarcStructure
667 $res = GetMarcStructure($forlibrarian,$frameworkcode);
669 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
670 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
671 $frameworkcode : the framework code to read
677 # cache for results of GetMarcStructure -- needed
679 our $marc_structure_cache;
681 sub GetMarcStructure {
682 my ( $forlibrarian, $frameworkcode ) = @_;
683 my $dbh=C4::Context->dbh;
684 $frameworkcode = "" unless $frameworkcode;
686 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
687 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
691 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
693 # check that framework exists
696 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
697 $sth->execute($frameworkcode);
698 my ($total) = $sth->fetchrow;
699 $frameworkcode = "" unless ( $total > 0 );
702 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
703 FROM marc_tag_structure
704 WHERE frameworkcode=?
707 $sth->execute($frameworkcode);
708 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
710 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
713 $res->{$tag}->{lib} =
714 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
715 $res->{$tag}->{tab} = "";
716 $res->{$tag}->{mandatory} = $mandatory;
717 $res->{$tag}->{repeatable} = $repeatable;
722 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
723 FROM marc_subfield_structure
724 WHERE frameworkcode=?
725 ORDER BY tagfield,tagsubfield
729 $sth->execute($frameworkcode);
732 my $authorised_value;
744 $tag, $subfield, $liblibrarian,
746 $mandatory, $repeatable, $authorised_value,
747 $authtypecode, $value_builder, $kohafield,
748 $seealso, $hidden, $isurl,
754 $res->{$tag}->{$subfield}->{lib} =
755 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
756 $res->{$tag}->{$subfield}->{tab} = $tab;
757 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
758 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
759 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
760 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
761 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
762 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
763 $res->{$tag}->{$subfield}->{seealso} = $seealso;
764 $res->{$tag}->{$subfield}->{hidden} = $hidden;
765 $res->{$tag}->{$subfield}->{isurl} = $isurl;
766 $res->{$tag}->{$subfield}->{'link'} = $link;
767 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
770 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
775 =head2 GetUsedMarcStructure
777 the same function as GetMarcStructure expcet it just take field
778 in tab 0-9. (used field)
780 my $results = GetUsedMarcStructure($frameworkcode);
782 L<$results> is a ref to an array which each case containts a ref
783 to a hash which each keys is the columns from marc_subfield_structure
785 L<$frameworkcode> is the framework code.
789 sub GetUsedMarcStructure($){
790 my $frameworkcode = shift || '';
791 my $dbh = C4::Context->dbh;
794 FROM marc_subfield_structure
796 AND frameworkcode = ?
799 my $sth = $dbh->prepare($query);
800 $sth->execute($frameworkcode);
801 while (my $row = $sth->fetchrow_hashref){
807 =head2 GetMarcFromKohaField
811 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
812 Returns the MARC fields & subfields mapped to the koha field
813 for the given frameworkcode
819 sub GetMarcFromKohaField {
820 my ( $kohafield, $frameworkcode ) = @_;
821 return 0, 0 unless $kohafield and defined $frameworkcode;
822 my $relations = C4::Context->marcfromkohafield;
824 $relations->{$frameworkcode}->{$kohafield}->[0],
825 $relations->{$frameworkcode}->{$kohafield}->[1]
833 my $record = GetMarcBiblio($biblionumber);
837 Returns MARC::Record representing bib identified by
838 C<$biblionumber>. If no bib exists, returns undef.
839 The MARC record contains both biblio & item data.
844 my $biblionumber = shift;
845 my $dbh = C4::Context->dbh;
847 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
848 $sth->execute($biblionumber);
849 my $row = $sth->fetchrow_hashref;
850 my $marcxml = StripNonXmlChars($row->{'marcxml'});
851 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
852 my $record = MARC::Record->new();
854 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
855 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
856 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
867 my $marcxml = GetXmlBiblio($biblionumber);
869 Returns biblioitems.marcxml of the biblionumber passed in parameter.
870 The XML contains both biblio & item datas
877 my ( $biblionumber ) = @_;
878 my $dbh = C4::Context->dbh;
880 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
881 $sth->execute($biblionumber);
882 my ($marcxml) = $sth->fetchrow;
886 =head2 GetCOinSBiblio
890 my $coins = GetCOinSBiblio($biblionumber);
892 Returns the COinS(a span) which can be included in a biblio record
899 my ( $biblionumber ) = @_;
900 my $record = GetMarcBiblio($biblionumber);
902 if (defined $record){
903 # get the coin format
904 my $pos7 = substr $record->leader(), 7,1;
905 my $pos6 = substr $record->leader(), 6,1;
908 my ($aulast, $aufirst);
916 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
927 'i' => 'audioRecording',
928 'j' => 'audioRecording',
931 'm' => 'computerProgram',
936 'a' => 'journalArticle',
940 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
942 if( $genre eq 'book' ){
943 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
946 ##### We must transform mtx to a valable mtx and document type ####
947 if( $genre eq 'book' ){
949 }elsif( $genre eq 'journal' ){
951 }elsif( $genre eq 'journalArticle' ){
958 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
961 $aulast = $record->subfield('700','a');
962 $aufirst = $record->subfield('700','b');
963 $oauthors = "&rft.au=$aufirst $aulast";
965 if($record->field('200')){
966 for my $au ($record->field('200')->subfield('g')){
967 $oauthors .= "&rft.au=$au";
970 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
971 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
972 $pubyear = $record->subfield('210','d');
973 $publisher = $record->subfield('210','c');
974 $isbn = $record->subfield('010','a');
975 $issn = $record->subfield('011','a');
977 # MARC21 need some improve
980 $genre = "&rft.genre=book";
983 $oauthors .= "&rft.au=".$record->subfield('100','a');
985 if($record->field('700')){
986 for my $au ($record->field('700')->subfield('a')){
987 $oauthors .= "&rft.au=$au";
990 $title = "&rft.btitle=".$record->subfield('245','a');
991 $pubyear = $record->subfield('260','c');
992 $publisher = $record->subfield('260','b');
993 $isbn = $record->subfield('020','a');
994 $issn = $record->subfield('022','a');
997 $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";
998 $coins_value =~ s/\ /\+/g;
999 #<!-- 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="
1001 return $coins_value;
1004 =head2 GetAuthorisedValueDesc
1008 my $subfieldvalue =get_authorised_value_desc(
1009 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1010 Retrieve the complete description for a given authorised value.
1012 Now takes $category and $value pair too.
1013 my $auth_value_desc =GetAuthorisedValueDesc(
1014 '','', 'DVD' ,'','','CCODE');
1020 sub GetAuthorisedValueDesc {
1021 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1022 my $dbh = C4::Context->dbh;
1026 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1029 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1030 return C4::Branch::GetBranchName($value);
1034 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1035 return getitemtypeinfo($value)->{description};
1038 #---- "true" authorized value
1039 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1042 if ( $category ne "" ) {
1045 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1047 $sth->execute( $category, $value );
1048 my $data = $sth->fetchrow_hashref;
1049 return $data->{'lib'};
1052 return $value; # if nothing is found return the original value
1060 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1061 Get all notes from the MARC record and returns them in an array.
1062 The note are stored in differents places depending on MARC flavour
1069 my ( $record, $marcflavour ) = @_;
1071 if ( $marcflavour eq "MARC21" ) {
1074 else { # assume unimarc if not marc21
1081 foreach my $field ( $record->field($scope) ) {
1082 my $value = $field->as_string();
1083 $value =~ s/\n/<br \/>/g ;
1085 if ( $note ne "" ) {
1086 $marcnote = { marcnote => $note, };
1087 push @marcnotes, $marcnote;
1090 if ( $note ne $value ) {
1091 $note = $note . " " . $value;
1096 $marcnote = { marcnote => $note };
1097 push @marcnotes, $marcnote; #load last tag into array
1100 } # end GetMarcNotes
1102 =head2 GetMarcSubjects
1106 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1107 Get all subjects from the MARC record and returns them in an array.
1108 The subjects are stored in differents places depending on MARC flavour
1114 sub GetMarcSubjects {
1115 my ( $record, $marcflavour ) = @_;
1116 my ( $mintag, $maxtag );
1117 if ( $marcflavour eq "MARC21" ) {
1121 else { # assume unimarc if not marc21
1131 foreach my $field ( $record->field('6..' )) {
1132 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1134 my @subfields = $field->subfields();
1137 # if there is an authority link, build the link with an= subfield9
1138 my $subfield9 = $field->subfield('9');
1139 for my $subject_subfield (@subfields ) {
1140 # don't load unimarc subfields 3,4,5
1141 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1142 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1143 next if (($marcflavour eq "MARC21") and ($subject_subfield->[0] =~ /2/ ) );
1144 my $code = $subject_subfield->[0];
1145 my $value = $subject_subfield->[1];
1146 my $linkvalue = $value;
1147 $linkvalue =~ s/(\(|\))//g;
1148 my $operator = " and " unless $counter==0;
1150 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1152 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1154 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1156 my @this_link_loop = @link_loop;
1157 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1161 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1164 return \@marcsubjects;
1165 } #end getMARCsubjects
1167 =head2 GetMarcAuthors
1171 authors = GetMarcAuthors($record,$marcflavour);
1172 Get all authors from the MARC record and returns them in an array.
1173 The authors are stored in differents places depending on MARC flavour
1179 sub GetMarcAuthors {
1180 my ( $record, $marcflavour ) = @_;
1181 my ( $mintag, $maxtag );
1182 # tagslib useful for UNIMARC author reponsabilities
1183 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.
1184 if ( $marcflavour eq "MARC21" ) {
1188 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1197 foreach my $field ( $record->fields ) {
1198 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1201 my @subfields = $field->subfields();
1203 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1204 my $subfield9 = $field->subfield('9');
1205 for my $authors_subfield (@subfields) {
1206 # don't load unimarc subfields 3, 5
1207 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1208 my $subfieldcode = $authors_subfield->[0];
1209 my $value = $authors_subfield->[1];
1210 my $linkvalue = $value;
1211 $linkvalue =~ s/(\(|\))//g;
1212 my $operator = " and " unless $count_auth==0;
1213 # if we have an authority link, use that as the link, otherwise use standard searching
1215 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1218 # reset $linkvalue if UNIMARC author responsibility
1219 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1220 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1222 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1224 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1225 my @this_link_loop = @link_loop;
1226 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1227 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1230 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1232 return \@marcauthors;
1239 $marcurls = GetMarcUrls($record,$marcflavour);
1240 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1241 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1248 my ( $record, $marcflavour ) = @_;
1251 for my $field ( $record->field('856') ) {
1254 for my $note ( $field->subfield('z') ) {
1255 push @notes, { note => $note };
1257 my @urls = $field->subfield('u');
1258 foreach my $url (@urls) {
1259 if ( $marcflavour eq 'MARC21' ) {
1260 my $s3 = $field->subfield('3');
1261 my $link = $field->subfield('y');
1262 unless ( $url =~ /^\w+:/ ) {
1263 if ( $field->indicator(1) eq '7' ) {
1264 $url = $field->subfield('2') . "://" . $url;
1265 } elsif ( $field->indicator(1) eq '1' ) {
1266 $url = 'ftp://' . $url;
1268 # properly, this should be if ind1=4,
1269 # however we will assume http protocol since we're building a link.
1270 $url = 'http://' . $url;
1273 # TODO handle ind 2 (relationship)
1278 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1279 $marcurl->{'part'} = $s3 if ($link);
1280 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^table/i );
1282 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1283 $marcurl->{'MARCURL'} = $url;
1285 push @marcurls, $marcurl;
1291 =head2 GetMarcSeries
1295 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1296 Get all series from the MARC record and returns them in an array.
1297 The series are stored in differents places depending on MARC flavour
1304 my ($record, $marcflavour) = @_;
1305 my ($mintag, $maxtag);
1306 if ($marcflavour eq "MARC21") {
1309 } else { # assume unimarc if not marc21
1319 foreach my $field ($record->field('440'), $record->field('490')) {
1321 #my $value = $field->subfield('a');
1322 #$marcsubjct = {MARCSUBJCT => $value,};
1323 my @subfields = $field->subfields();
1324 #warn "subfields:".join " ", @$subfields;
1327 for my $series_subfield (@subfields) {
1329 undef $volume_number;
1330 # see if this is an instance of a volume
1331 if ($series_subfield->[0] eq 'v') {
1335 my $code = $series_subfield->[0];
1336 my $value = $series_subfield->[1];
1337 my $linkvalue = $value;
1338 $linkvalue =~ s/(\(|\))//g;
1339 my $operator = " and " unless $counter==0;
1340 push @link_loop, {link => $linkvalue, operator => $operator };
1341 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1342 if ($volume_number) {
1343 push @subfields_loop, {volumenum => $value};
1346 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1350 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1351 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1352 #push @marcsubjcts, $marcsubjct;
1356 my $marcseriessarray=\@marcseries;
1357 return $marcseriessarray;
1358 } #end getMARCseriess
1360 =head2 GetFrameworkCode
1364 $frameworkcode = GetFrameworkCode( $biblionumber )
1370 sub GetFrameworkCode {
1371 my ( $biblionumber ) = @_;
1372 my $dbh = C4::Context->dbh;
1373 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1374 $sth->execute($biblionumber);
1375 my ($frameworkcode) = $sth->fetchrow;
1376 return $frameworkcode;
1379 =head2 GetPublisherNameFromIsbn
1381 $name = GetPublishercodeFromIsbn($isbn);
1388 sub GetPublisherNameFromIsbn($){
1390 $isbn =~ s/[- _]//g;
1392 my @codes = (split '-', DisplayISBN($isbn));
1393 my $code = $codes[0].$codes[1].$codes[2];
1394 my $dbh = C4::Context->dbh;
1396 SELECT distinct publishercode
1399 AND publishercode IS NOT NULL
1402 my $sth = $dbh->prepare($query);
1403 $sth->execute("$code%");
1404 my $name = $sth->fetchrow;
1405 return $name if length $name;
1409 =head2 TransformKohaToMarc
1413 $record = TransformKohaToMarc( $hash )
1414 This function builds partial MARC::Record from a hash
1415 Hash entries can be from biblio or biblioitems.
1416 This function is called in acquisition module, to create a basic catalogue entry from user entry
1422 sub TransformKohaToMarc {
1424 my $sth = C4::Context->dbh->prepare(
1425 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1427 my $record = MARC::Record->new();
1428 SetMarcUnicodeFlag($record, C4::Context->preference("marcflavour"));
1429 foreach (keys %{$hash}) {
1430 &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
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 my $string = strftime( "%Y%m%d", localtime(time) );
1594 # set 50 to position 26 is biblios, 13 if authorities
1596 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1597 $string = sprintf( "%-*s", 35, $string );
1598 substr( $string, $pos , 6, "50" );
1599 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1600 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1601 $xml .= "</datafield>\n";
1603 $xml .= MARC::File::XML::footer();
1607 =head2 TransformHtmlToMarc
1609 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1610 L<$params> is a ref to an array as below:
1612 'tag_010_indicator1_531951' ,
1613 'tag_010_indicator2_531951' ,
1614 'tag_010_code_a_531951_145735' ,
1615 'tag_010_subfield_a_531951_145735' ,
1616 'tag_200_indicator1_873510' ,
1617 'tag_200_indicator2_873510' ,
1618 'tag_200_code_a_873510_673465' ,
1619 'tag_200_subfield_a_873510_673465' ,
1620 'tag_200_code_b_873510_704318' ,
1621 'tag_200_subfield_b_873510_704318' ,
1622 'tag_200_code_e_873510_280822' ,
1623 'tag_200_subfield_e_873510_280822' ,
1624 'tag_200_code_f_873510_110730' ,
1625 'tag_200_subfield_f_873510_110730' ,
1627 L<$cgi> is the CGI object which containts the value.
1628 L<$record> is the MARC::Record object.
1632 sub TransformHtmlToMarc {
1636 # explicitly turn on the UTF-8 flag for all
1637 # 'tag_' parameters to avoid incorrect character
1638 # conversion later on
1639 my $cgi_params = $cgi->Vars;
1640 foreach my $param_name (keys %$cgi_params) {
1641 if ($param_name =~ /^tag_/) {
1642 my $param_value = $cgi_params->{$param_name};
1643 if (utf8::decode($param_value)) {
1644 $cgi_params->{$param_name} = $param_value;
1646 # FIXME - need to do something if string is not valid UTF-8
1650 # creating a new record
1651 my $record = MARC::Record->new();
1654 while ($params->[$i]){ # browse all CGI params
1655 my $param = $params->[$i];
1657 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1658 if ($param eq 'biblionumber') {
1659 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1660 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1661 if ($biblionumbertagfield < 10) {
1662 $newfield = MARC::Field->new(
1663 $biblionumbertagfield,
1664 $cgi->param($param),
1667 $newfield = MARC::Field->new(
1668 $biblionumbertagfield,
1671 "$biblionumbertagsubfield" => $cgi->param($param),
1674 push @fields,$newfield if($newfield);
1676 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1679 my $ind1 = substr($cgi->param($param),0,1);
1680 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1684 if($tag < 10){ # no code for theses fields
1685 # in MARC editor, 000 contains the leader.
1686 if ($tag eq '000' ) {
1687 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1688 # between 001 and 009 (included)
1689 } elsif ($cgi->param($params->[$j+1]) ne '') {
1690 $newfield = MARC::Field->new(
1692 $cgi->param($params->[$j+1]),
1695 # > 009, deal with subfields
1697 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1698 my $inner_param = $params->[$j];
1700 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1701 $newfield->add_subfields(
1702 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1706 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1707 $newfield = MARC::Field->new(
1711 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1718 push @fields,$newfield if($newfield);
1723 $record->append_fields(@fields);
1727 # cache inverted MARC field map
1728 our $inverted_field_map;
1730 =head2 TransformMarcToKoha
1734 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1738 Extract data from a MARC bib record into a hashref representing
1739 Koha biblio, biblioitems, and items fields.
1742 sub TransformMarcToKoha {
1743 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1746 $limit_table=$limit_table||0;
1747 $frameworkcode = '' unless defined $frameworkcode;
1749 unless (defined $inverted_field_map) {
1750 $inverted_field_map = _get_inverted_marc_field_map();
1754 if ( defined $limit_table && $limit_table eq 'items') {
1755 $tables{'items'} = 1;
1757 $tables{'items'} = 1;
1758 $tables{'biblio'} = 1;
1759 $tables{'biblioitems'} = 1;
1762 # traverse through record
1763 MARCFIELD: foreach my $field ($record->fields()) {
1764 my $tag = $field->tag();
1765 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1766 if ($field->is_control_field()) {
1767 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1768 ENTRY: foreach my $entry (@{ $kohafields }) {
1769 my ($subfield, $table, $column) = @{ $entry };
1770 next ENTRY unless exists $tables{$table};
1771 my $key = _disambiguate($table, $column);
1772 if ($result->{$key}) {
1773 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1774 $result->{$key} .= " | " . $field->data();
1777 $result->{$key} = $field->data();
1781 # deal with subfields
1782 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1783 my $code = $sf->[0];
1784 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1785 my $value = $sf->[1];
1786 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1787 my ($table, $column) = @{ $entry };
1788 next SFENTRY unless exists $tables{$table};
1789 my $key = _disambiguate($table, $column);
1790 if ($result->{$key}) {
1791 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1792 $result->{$key} .= " | " . $value;
1795 $result->{$key} = $value;
1802 # modify copyrightdate to keep only the 1st year found
1803 if (exists $result->{'copyrightdate'}) {
1804 my $temp = $result->{'copyrightdate'};
1805 $temp =~ m/c(\d\d\d\d)/;
1806 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1807 $result->{'copyrightdate'} = $1;
1809 else { # if no cYYYY, get the 1st date.
1810 $temp =~ m/(\d\d\d\d)/;
1811 $result->{'copyrightdate'} = $1;
1815 # modify publicationyear to keep only the 1st year found
1816 if (exists $result->{'publicationyear'}) {
1817 my $temp = $result->{'publicationyear'};
1818 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1819 $result->{'publicationyear'} = $1;
1821 else { # if no cYYYY, get the 1st date.
1822 $temp =~ m/(\d\d\d\d)/;
1823 $result->{'publicationyear'} = $1;
1830 sub _get_inverted_marc_field_map {
1832 my $relations = C4::Context->marcfromkohafield;
1834 foreach my $frameworkcode (keys %{ $relations }) {
1835 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1836 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1837 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1838 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1839 my ($table, $column) = split /[.]/, $kohafield, 2;
1840 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1841 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1847 =head2 _disambiguate
1851 $newkey = _disambiguate($table, $field);
1853 This is a temporary hack to distinguish between the
1854 following sets of columns when using TransformMarcToKoha.
1856 items.cn_source & biblioitems.cn_source
1857 items.cn_sort & biblioitems.cn_sort
1859 Columns that are currently NOT distinguished (FIXME
1860 due to lack of time to fully test) are:
1862 biblio.notes and biblioitems.notes
1867 FIXME - this is necessary because prefixing each column
1868 name with the table name would require changing lots
1869 of code and templates, and exposing more of the DB
1870 structure than is good to the UI templates, particularly
1871 since biblio and bibloitems may well merge in a future
1872 version. In the future, it would also be good to
1873 separate DB access and UI presentation field names
1881 my ($table, $column) = @_;
1882 if ($column eq "cn_sort" or $column eq "cn_source") {
1883 return $table . '.' . $column;
1890 =head2 get_koha_field_from_marc
1894 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1896 Internal function to map data from the MARC record to a specific non-MARC field.
1897 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1903 sub get_koha_field_from_marc {
1904 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1905 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1907 foreach my $field ( $record->field($tagfield) ) {
1908 if ( $field->tag() < 10 ) {
1910 $kohafield .= " | " . $field->data();
1913 $kohafield = $field->data();
1917 if ( $field->subfields ) {
1918 my @subfields = $field->subfields();
1919 foreach my $subfieldcount ( 0 .. $#subfields ) {
1920 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1923 " | " . $subfields[$subfieldcount][1];
1927 $subfields[$subfieldcount][1];
1938 =head2 TransformMarcToKohaOneField
1942 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1948 sub TransformMarcToKohaOneField {
1950 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1951 # only the 1st will be retrieved...
1952 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1954 my ( $tagfield, $subfield ) =
1955 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1957 foreach my $field ( $record->field($tagfield) ) {
1958 if ( $field->tag() < 10 ) {
1959 if ( $result->{$kohafield} ) {
1960 $result->{$kohafield} .= " | " . $field->data();
1963 $result->{$kohafield} = $field->data();
1967 if ( $field->subfields ) {
1968 my @subfields = $field->subfields();
1969 foreach my $subfieldcount ( 0 .. $#subfields ) {
1970 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1971 if ( $result->{$kohafield} ) {
1972 $result->{$kohafield} .=
1973 " | " . $subfields[$subfieldcount][1];
1976 $result->{$kohafield} =
1977 $subfields[$subfieldcount][1];
1987 =head1 OTHER FUNCTIONS
1990 =head2 PrepareItemrecordDisplay
1994 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1996 Returns a hash with all the fields for Display a given item data in a template
2002 sub PrepareItemrecordDisplay {
2004 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2006 my $dbh = C4::Context->dbh;
2007 my $frameworkcode = &GetFrameworkCode( $bibnum );
2008 my ( $itemtagfield, $itemtagsubfield ) =
2009 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2010 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2011 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2013 my $authorised_values_sth =
2015 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2017 foreach my $tag ( sort keys %{$tagslib} ) {
2018 my $previous_tag = '';
2020 # loop through each subfield
2022 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2023 next if ( subfield_is_koha_internal_p($subfield) );
2024 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2026 $subfield_data{tag} = $tag;
2027 $subfield_data{subfield} = $subfield;
2028 $subfield_data{countsubfield} = $cntsubf++;
2029 $subfield_data{kohafield} =
2030 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2032 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2033 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2034 $subfield_data{mandatory} =
2035 $tagslib->{$tag}->{$subfield}->{mandatory};
2036 $subfield_data{repeatable} =
2037 $tagslib->{$tag}->{$subfield}->{repeatable};
2038 $subfield_data{hidden} = "display:none"
2039 if $tagslib->{$tag}->{$subfield}->{hidden};
2041 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2043 $value =~ s/"/"/g;
2045 # search for itemcallnumber if applicable
2046 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2047 'items.itemcallnumber'
2048 && C4::Context->preference('itemcallnumber') )
2051 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2053 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2054 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2056 $value = $temp->subfield($CNsubfield);
2059 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2060 'items.itemcallnumber'
2061 && $defaultvalues->{'callnumber'} )
2063 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2065 $value = $defaultvalues->{'callnumber'};
2068 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2069 'items.holdingbranch' ||
2070 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2072 && $defaultvalues->{'branchcode'} )
2074 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2076 $value = $defaultvalues->{branchcode};
2079 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2080 my @authorised_values;
2083 # builds list, depending on authorised value...
2085 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2088 if ( ( C4::Context->preference("IndependantBranches") )
2089 && ( C4::Context->userenv->{flags} != 1 ) )
2093 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2095 $sth->execute( C4::Context->userenv->{branch} );
2096 push @authorised_values, ""
2098 $tagslib->{$tag}->{$subfield}->{mandatory} );
2099 while ( my ( $branchcode, $branchname ) =
2100 $sth->fetchrow_array )
2102 push @authorised_values, $branchcode;
2103 $authorised_lib{$branchcode} = $branchname;
2109 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2112 push @authorised_values, ""
2114 $tagslib->{$tag}->{$subfield}->{mandatory} );
2115 while ( my ( $branchcode, $branchname ) =
2116 $sth->fetchrow_array )
2118 push @authorised_values, $branchcode;
2119 $authorised_lib{$branchcode} = $branchname;
2125 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2130 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2133 push @authorised_values, ""
2134 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2135 while ( my ( $itemtype, $description ) =
2136 $sth->fetchrow_array )
2138 push @authorised_values, $itemtype;
2139 $authorised_lib{$itemtype} = $description;
2142 #---- "true" authorised value
2145 $authorised_values_sth->execute(
2146 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2147 push @authorised_values, ""
2148 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2149 while ( my ( $value, $lib ) =
2150 $authorised_values_sth->fetchrow_array )
2152 push @authorised_values, $value;
2153 $authorised_lib{$value} = $lib;
2156 $subfield_data{marc_value} = CGI::scrolling_list(
2157 -name => 'field_value',
2158 -values => \@authorised_values,
2159 -default => "$value",
2160 -labels => \%authorised_lib,
2167 $subfield_data{marc_value} =
2168 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2170 push( @loop_data, \%subfield_data );
2174 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2175 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2177 'itemtagfield' => $itemtagfield,
2178 'itemtagsubfield' => $itemtagsubfield,
2179 'itemnumber' => $itemnumber,
2180 'iteminformation' => \@loop_data
2186 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2188 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2189 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2190 # =head2 ModZebrafiles
2192 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2196 # sub ModZebrafiles {
2198 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2202 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2203 # unless ( opendir( DIR, "$zebradir" ) ) {
2204 # warn "$zebradir not found";
2208 # my $filename = $zebradir . $biblionumber;
2211 # open( OUTPUT, ">", $filename . ".xml" );
2212 # print OUTPUT $record;
2221 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2223 $biblionumber is the biblionumber we want to index
2224 $op is specialUpdate or delete, and is used to know what we want to do
2225 $server is the server that we want to update
2226 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2227 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2229 $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.
2236 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2237 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2238 my $dbh=C4::Context->dbh;
2240 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2242 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2243 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2245 if (C4::Context->preference("NoZebra")) {
2246 # lock the nozebra table : we will read index lines, update them in Perl process
2247 # and write everything in 1 transaction.
2248 # lock the table to avoid someone else overwriting what we are doing
2249 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2250 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2251 if ($op eq 'specialUpdate') {
2252 # OK, we have to add or update the record
2253 # 1st delete (virtually, in indexes), if record actually exists
2255 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2257 # ... add the record
2258 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2260 # it's a deletion, delete the record...
2261 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2262 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2264 # ok, now update the database...
2265 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2266 foreach my $key (keys %result) {
2267 foreach my $index (keys %{$result{$key}}) {
2268 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2271 $dbh->do('UNLOCK TABLES');
2274 # we use zebra, just fill zebraqueue table
2276 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2278 AND biblio_auth_number = ?
2281 my $check_sth = $dbh->prepare_cached($check_sql);
2282 $check_sth->execute($server, $biblionumber, $op);
2283 my ($count) = $check_sth->fetchrow_array;
2284 $check_sth->finish();
2286 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2287 $sth->execute($biblionumber,$server,$op);
2293 =head2 GetNoZebraIndexes
2295 %indexes = GetNoZebraIndexes;
2297 return the data from NoZebraIndexes syspref.
2301 sub GetNoZebraIndexes {
2302 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2304 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2305 $line =~ /(.*)=>(.*)/;
2306 my $index = $1; # initial ' or " is removed afterwards
2308 $index =~ s/'|"|\s//g;
2309 $fields =~ s/'|"|\s//g;
2310 $indexes{$index}=$fields;
2315 =head1 INTERNAL FUNCTIONS
2317 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2319 function to delete a biblio in NoZebra indexes
2320 This function does NOT delete anything in database : it reads all the indexes entries
2321 that have to be deleted & delete them in the hash
2322 The SQL part is done either :
2323 - after the Add if we are modifying a biblio (delete + add again)
2324 - immediatly after this sub if we are doing a true deletion.
2325 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2330 sub _DelBiblioNoZebra {
2331 my ($biblionumber, $record, $server)=@_;
2334 my $dbh = C4::Context->dbh;
2338 if ($server eq 'biblioserver') {
2339 %index=GetNoZebraIndexes;
2340 # get title of the record (to store the 10 first letters with the index)
2341 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2342 $title = lc($record->subfield($titletag,$titlesubfield));
2344 # for authorities, the "title" is the $a mainentry
2345 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2346 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2347 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2348 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2349 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2350 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2351 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2355 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2356 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2357 # limit to 10 char, should be enough, and limit the DB size
2358 $title = substr($title,0,10);
2360 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2361 foreach my $field ($record->fields()) {
2362 #parse each subfield
2363 next if $field->tag <10;
2364 foreach my $subfield ($field->subfields()) {
2365 my $tag = $field->tag();
2366 my $subfieldcode = $subfield->[0];
2368 # check each index to see if the subfield is stored somewhere
2369 # otherwise, store it in __RAW__ index
2370 foreach my $key (keys %index) {
2371 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2372 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2374 my $line= lc $subfield->[1];
2375 # remove meaningless value in the field...
2376 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2377 # ... and split in words
2378 foreach (split / /,$line) {
2379 next unless $_; # skip empty values (multiple spaces)
2380 # if the entry is already here, do nothing, the biblionumber has already be removed
2381 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2382 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2383 $sth2->execute($server,$key,$_);
2384 my $existing_biblionumbers = $sth2->fetchrow;
2386 if ($existing_biblionumbers) {
2387 # warn " existing for $key $_: $existing_biblionumbers";
2388 $result{$key}->{$_} =$existing_biblionumbers;
2389 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2395 # the subfield is not indexed, store it in __RAW__ index anyway
2397 my $line= lc $subfield->[1];
2398 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2399 # ... and split in words
2400 foreach (split / /,$line) {
2401 next unless $_; # skip empty values (multiple spaces)
2402 # if the entry is already here, do nothing, the biblionumber has already be removed
2403 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2404 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2405 $sth2->execute($server,'__RAW__',$_);
2406 my $existing_biblionumbers = $sth2->fetchrow;
2408 if ($existing_biblionumbers) {
2409 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2410 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2420 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2422 function to add a biblio in NoZebra indexes
2426 sub _AddBiblioNoZebra {
2427 my ($biblionumber, $record, $server, %result)=@_;
2428 my $dbh = C4::Context->dbh;
2432 if ($server eq 'biblioserver') {
2433 %index=GetNoZebraIndexes;
2434 # get title of the record (to store the 10 first letters with the index)
2435 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2436 $title = lc($record->subfield($titletag,$titlesubfield));
2438 # warn "server : $server";
2439 # for authorities, the "title" is the $a mainentry
2440 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2441 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2442 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2443 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2444 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2445 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2446 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2449 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2450 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2451 # limit to 10 char, should be enough, and limit the DB size
2452 $title = substr($title,0,10);
2454 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2455 foreach my $field ($record->fields()) {
2456 #parse each subfield
2457 ###FIXME: impossible to index a 001-009 value with NoZebra
2458 next if $field->tag <10;
2459 foreach my $subfield ($field->subfields()) {
2460 my $tag = $field->tag();
2461 my $subfieldcode = $subfield->[0];
2463 # warn "INDEXING :".$subfield->[1];
2464 # check each index to see if the subfield is stored somewhere
2465 # otherwise, store it in __RAW__ index
2466 foreach my $key (keys %index) {
2467 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2468 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2470 my $line= lc $subfield->[1];
2471 # remove meaningless value in the field...
2472 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2473 # ... and split in words
2474 foreach (split / /,$line) {
2475 next unless $_; # skip empty values (multiple spaces)
2476 # if the entry is already here, improve weight
2477 # warn "managing $_";
2478 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2479 my $weight = $1 + 1;
2480 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2481 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2483 # get the value if it exist in the nozebra table, otherwise, create it
2484 $sth2->execute($server,$key,$_);
2485 my $existing_biblionumbers = $sth2->fetchrow;
2487 if ($existing_biblionumbers) {
2488 $result{$key}->{"$_"} =$existing_biblionumbers;
2489 my $weight = defined $1 ? $1 + 1 : 1;
2490 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2491 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2492 # create a new ligne for this entry
2494 # warn "INSERT : $server / $key / $_";
2495 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2496 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2502 # the subfield is not indexed, store it in __RAW__ index anyway
2504 my $line= lc $subfield->[1];
2505 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2506 # ... and split in words
2507 foreach (split / /,$line) {
2508 next unless $_; # skip empty values (multiple spaces)
2509 # if the entry is already here, improve weight
2510 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2512 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2513 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2515 # get the value if it exist in the nozebra table, otherwise, create it
2516 $sth2->execute($server,'__RAW__',$_);
2517 my $existing_biblionumbers = $sth2->fetchrow;
2519 if ($existing_biblionumbers) {
2520 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2522 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2523 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2524 # create a new ligne for this entry
2526 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2527 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2542 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2544 Find the given $subfield in the given $tag in the given
2545 MARC::Record $record. If the subfield is found, returns
2546 the (indicators, value) pair; otherwise, (undef, undef) is
2550 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2551 I suggest we export it from this module.
2558 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2561 if ( $tagfield < 10 ) {
2562 if ( $record->field($tagfield) ) {
2563 push @result, $record->field($tagfield)->data();
2570 foreach my $field ( $record->field($tagfield) ) {
2571 my @subfields = $field->subfields();
2572 foreach my $subfield (@subfields) {
2573 if ( @$subfield[0] eq $insubfield ) {
2574 push @result, @$subfield[1];
2575 $indicator = $field->indicator(1) . $field->indicator(2);
2580 return ( $indicator, @result );
2583 =head2 _koha_marc_update_bib_ids
2587 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2589 Internal function to add or update biblionumber and biblioitemnumber to
2596 sub _koha_marc_update_bib_ids {
2597 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2599 # we must add bibnum and bibitemnum in MARC::Record...
2600 # we build the new field with biblionumber and biblioitemnumber
2601 # we drop the original field
2602 # we add the new builded field.
2603 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2604 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2606 if ($biblio_tag != $biblioitem_tag) {
2607 # biblionumber & biblioitemnumber are in different fields
2609 # deal with biblionumber
2610 my ($new_field, $old_field);
2611 if ($biblio_tag < 10) {
2612 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2615 MARC::Field->new( $biblio_tag, '', '',
2616 "$biblio_subfield" => $biblionumber );
2619 # drop old field and create new one...
2620 $old_field = $record->field($biblio_tag);
2621 $record->delete_field($old_field) if $old_field;
2622 $record->append_fields($new_field);
2624 # deal with biblioitemnumber
2625 if ($biblioitem_tag < 10) {
2626 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2629 MARC::Field->new( $biblioitem_tag, '', '',
2630 "$biblioitem_subfield" => $biblioitemnumber, );
2632 # drop old field and create new one...
2633 $old_field = $record->field($biblioitem_tag);
2634 $record->delete_field($old_field) if $old_field;
2635 $record->insert_fields_ordered($new_field);
2638 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2639 my $new_field = MARC::Field->new(
2640 $biblio_tag, '', '',
2641 "$biblio_subfield" => $biblionumber,
2642 "$biblioitem_subfield" => $biblioitemnumber
2645 # drop old field and create new one...
2646 my $old_field = $record->field($biblio_tag);
2647 $record->delete_field($old_field) if $old_field;
2648 $record->insert_fields_ordered($new_field);
2652 =head2 _koha_marc_update_biblioitem_cn_sort
2656 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2660 Given a MARC bib record and the biblioitem hash, update the
2661 subfield that contains a copy of the value of biblioitems.cn_sort.
2665 sub _koha_marc_update_biblioitem_cn_sort {
2667 my $biblioitem = shift;
2668 my $frameworkcode= shift;
2670 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2671 return unless $biblioitem_tag;
2673 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2675 if (my $field = $marc->field($biblioitem_tag)) {
2676 $field->delete_subfield(code => $biblioitem_subfield);
2677 if ($cn_sort ne '') {
2678 $field->add_subfields($biblioitem_subfield => $cn_sort);
2681 # if we get here, no biblioitem tag is present in the MARC record, so
2682 # we'll create it if $cn_sort is not empty -- this would be
2683 # an odd combination of events, however
2685 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2690 =head2 _koha_add_biblio
2694 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2696 Internal function to add a biblio ($biblio is a hash with the values)
2702 sub _koha_add_biblio {
2703 my ( $dbh, $biblio, $frameworkcode ) = @_;
2707 # set the series flag
2709 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2713 SET frameworkcode = ?,
2724 my $sth = $dbh->prepare($query);
2727 $biblio->{'author'},
2729 $biblio->{'unititle'},
2732 $biblio->{'seriestitle'},
2733 $biblio->{'copyrightdate'},
2734 $biblio->{'abstract'}
2737 my $biblionumber = $dbh->{'mysql_insertid'};
2738 if ( $dbh->errstr ) {
2739 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2744 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2745 return ($biblionumber,$error);
2748 =head2 _koha_modify_biblio
2752 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2754 Internal function for updating the biblio table
2760 sub _koha_modify_biblio {
2761 my ( $dbh, $biblio, $frameworkcode ) = @_;
2766 SET frameworkcode = ?,
2775 WHERE biblionumber = ?
2778 my $sth = $dbh->prepare($query);
2782 $biblio->{'author'},
2784 $biblio->{'unititle'},
2786 $biblio->{'serial'},
2787 $biblio->{'seriestitle'},
2788 $biblio->{'copyrightdate'},
2789 $biblio->{'abstract'},
2790 $biblio->{'biblionumber'}
2791 ) if $biblio->{'biblionumber'};
2793 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2794 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2797 return ( $biblio->{'biblionumber'},$error );
2800 =head2 _koha_modify_biblioitem_nonmarc
2804 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2806 Updates biblioitems row except for marc and marcxml, which should be changed
2813 sub _koha_modify_biblioitem_nonmarc {
2814 my ( $dbh, $biblioitem ) = @_;
2817 # re-calculate the cn_sort, it may have changed
2818 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2822 SET biblionumber = ?,
2828 publicationyear = ?,
2832 collectiontitle = ?,
2834 collectionvolume= ?,
2835 editionstatement= ?,
2836 editionresponsibility = ?,
2850 where biblioitemnumber = ?
2852 my $sth = $dbh->prepare($query);
2854 $biblioitem->{'biblionumber'},
2855 $biblioitem->{'volume'},
2856 $biblioitem->{'number'},
2857 $biblioitem->{'itemtype'},
2858 $biblioitem->{'isbn'},
2859 $biblioitem->{'issn'},
2860 $biblioitem->{'publicationyear'},
2861 $biblioitem->{'publishercode'},
2862 $biblioitem->{'volumedate'},
2863 $biblioitem->{'volumedesc'},
2864 $biblioitem->{'collectiontitle'},
2865 $biblioitem->{'collectionissn'},
2866 $biblioitem->{'collectionvolume'},
2867 $biblioitem->{'editionstatement'},
2868 $biblioitem->{'editionresponsibility'},
2869 $biblioitem->{'illus'},
2870 $biblioitem->{'pages'},
2871 $biblioitem->{'bnotes'},
2872 $biblioitem->{'size'},
2873 $biblioitem->{'place'},
2874 $biblioitem->{'lccn'},
2875 $biblioitem->{'url'},
2876 $biblioitem->{'biblioitems.cn_source'},
2877 $biblioitem->{'cn_class'},
2878 $biblioitem->{'cn_item'},
2879 $biblioitem->{'cn_suffix'},
2881 $biblioitem->{'totalissues'},
2882 $biblioitem->{'biblioitemnumber'}
2884 if ( $dbh->errstr ) {
2885 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2888 return ($biblioitem->{'biblioitemnumber'},$error);
2891 =head2 _koha_add_biblioitem
2895 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2897 Internal function to add a biblioitem
2903 sub _koha_add_biblioitem {
2904 my ( $dbh, $biblioitem ) = @_;
2907 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2909 "INSERT INTO biblioitems SET
2916 publicationyear = ?,
2920 collectiontitle = ?,
2922 collectionvolume= ?,
2923 editionstatement= ?,
2924 editionresponsibility = ?,
2940 my $sth = $dbh->prepare($query);
2942 $biblioitem->{'biblionumber'},
2943 $biblioitem->{'volume'},
2944 $biblioitem->{'number'},
2945 $biblioitem->{'itemtype'},
2946 $biblioitem->{'isbn'},
2947 $biblioitem->{'issn'},
2948 $biblioitem->{'publicationyear'},
2949 $biblioitem->{'publishercode'},
2950 $biblioitem->{'volumedate'},
2951 $biblioitem->{'volumedesc'},
2952 $biblioitem->{'collectiontitle'},
2953 $biblioitem->{'collectionissn'},
2954 $biblioitem->{'collectionvolume'},
2955 $biblioitem->{'editionstatement'},
2956 $biblioitem->{'editionresponsibility'},
2957 $biblioitem->{'illus'},
2958 $biblioitem->{'pages'},
2959 $biblioitem->{'bnotes'},
2960 $biblioitem->{'size'},
2961 $biblioitem->{'place'},
2962 $biblioitem->{'lccn'},
2963 $biblioitem->{'marc'},
2964 $biblioitem->{'url'},
2965 $biblioitem->{'biblioitems.cn_source'},
2966 $biblioitem->{'cn_class'},
2967 $biblioitem->{'cn_item'},
2968 $biblioitem->{'cn_suffix'},
2970 $biblioitem->{'totalissues'}
2972 my $bibitemnum = $dbh->{'mysql_insertid'};
2973 if ( $dbh->errstr ) {
2974 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2978 return ($bibitemnum,$error);
2981 =head2 _koha_delete_biblio
2985 $error = _koha_delete_biblio($dbh,$biblionumber);
2987 Internal sub for deleting from biblio table -- also saves to deletedbiblio
2989 C<$dbh> - the database handle
2990 C<$biblionumber> - the biblionumber of the biblio to be deleted
2996 # FIXME: add error handling
2998 sub _koha_delete_biblio {
2999 my ( $dbh, $biblionumber ) = @_;
3001 # get all the data for this biblio
3002 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3003 $sth->execute($biblionumber);
3005 if ( my $data = $sth->fetchrow_hashref ) {
3007 # save the record in deletedbiblio
3008 # find the fields to save
3009 my $query = "INSERT INTO deletedbiblio SET ";
3011 foreach my $temp ( keys %$data ) {
3012 $query .= "$temp = ?,";
3013 push( @bind, $data->{$temp} );
3016 # replace the last , by ",?)"
3018 my $bkup_sth = $dbh->prepare($query);
3019 $bkup_sth->execute(@bind);
3023 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3024 $del_sth->execute($biblionumber);
3031 =head2 _koha_delete_biblioitems
3035 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3037 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3039 C<$dbh> - the database handle
3040 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3046 # FIXME: add error handling
3048 sub _koha_delete_biblioitems {
3049 my ( $dbh, $biblioitemnumber ) = @_;
3051 # get all the data for this biblioitem
3053 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3054 $sth->execute($biblioitemnumber);
3056 if ( my $data = $sth->fetchrow_hashref ) {
3058 # save the record in deletedbiblioitems
3059 # find the fields to save
3060 my $query = "INSERT INTO deletedbiblioitems SET ";
3062 foreach my $temp ( keys %$data ) {
3063 $query .= "$temp = ?,";
3064 push( @bind, $data->{$temp} );
3067 # replace the last , by ",?)"
3069 my $bkup_sth = $dbh->prepare($query);
3070 $bkup_sth->execute(@bind);
3073 # delete the biblioitem
3075 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3076 $del_sth->execute($biblioitemnumber);
3083 =head1 UNEXPORTED FUNCTIONS
3085 =head2 ModBiblioMarc
3087 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3089 Add MARC data for a biblio to koha
3091 Function exported, but should NOT be used, unless you really know what you're doing
3097 # pass the MARC::Record to this function, and it will create the records in the marc field
3098 my ( $record, $biblionumber, $frameworkcode ) = @_;
3099 my $dbh = C4::Context->dbh;
3100 my @fields = $record->fields();
3101 if ( !$frameworkcode ) {
3102 $frameworkcode = "";
3105 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3106 $sth->execute( $frameworkcode, $biblionumber );
3108 my $encoding = C4::Context->preference("marcflavour");
3110 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3111 if ( $encoding eq "UNIMARC" ) {
3113 if ( length($record->subfield( 100, "a" )) == 35 ) {
3114 $string = $record->subfield( 100, "a" );
3115 my $f100 = $record->field(100);
3116 $record->delete_field($f100);
3119 $string = POSIX::strftime( "%Y%m%d", localtime );
3121 $string = sprintf( "%-*s", 35, $string );
3123 substr( $string, 22, 6, "frey50" );
3124 unless ( $record->subfield( 100, "a" ) ) {
3125 $record->insert_grouped_field(
3126 MARC::Field->new( 100, "", "", "a" => $string ) );
3130 if (C4::Context->preference("NoZebra")) {
3131 # only NoZebra indexing needs to have
3132 # the previous version of the record
3133 $oldRecord = GetMarcBiblio($biblionumber);
3137 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3138 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3141 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3142 return $biblionumber;
3145 =head2 z3950_extended_services
3147 z3950_extended_services($serviceType,$serviceOptions,$record);
3149 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.
3151 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3153 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3155 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3159 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3160 syntax => the record syntax (transfer syntax)
3161 databaseName = Database from connection object
3163 To set serviceOptions, call set_service_options($serviceType)
3165 C<$record> the record, if one is needed for the service type
3167 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3171 sub z3950_extended_services {
3172 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3174 # get our connection object
3175 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3177 # create a new package object
3178 my $Zpackage = $Zconn->package();
3181 $Zpackage->option( action => $action );
3183 if ( $serviceOptions->{'databaseName'} ) {
3184 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3186 if ( $serviceOptions->{'recordIdNumber'} ) {
3188 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3190 if ( $serviceOptions->{'recordIdOpaque'} ) {
3192 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3195 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3196 #if ($serviceType eq 'itemorder') {
3197 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3198 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3199 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3200 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3203 if ( $serviceOptions->{record} ) {
3204 $Zpackage->option( record => $serviceOptions->{record} );
3206 # can be xml or marc
3207 if ( $serviceOptions->{'syntax'} ) {
3208 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3212 # send the request, handle any exception encountered
3213 eval { $Zpackage->send($serviceType) };
3214 if ( $@ && $@->isa("ZOOM::Exception") ) {
3215 return "error: " . $@->code() . " " . $@->message() . "\n";
3218 # free up package resources
3219 $Zpackage->destroy();
3222 =head2 set_service_options
3224 my $serviceOptions = set_service_options($serviceType);
3226 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3228 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3232 sub set_service_options {
3233 my ($serviceType) = @_;
3236 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3237 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3239 if ( $serviceType eq 'commit' ) {
3243 if ( $serviceType eq 'create' ) {
3247 if ( $serviceType eq 'drop' ) {
3248 die "ERROR: 'drop' not currently supported (by Zebra)";
3250 return $serviceOptions;
3253 =head3 get_biblio_authorised_values
3255 find the types and values for all authorised values assigned to this biblio.
3259 MARC::Record of the bib
3261 returns: a hashref malling the authorised value to the value set for this biblionumber
3263 $authorised_values = {
3264 'Scent' => 'flowery',
3265 'Audience' => 'Young Adult',
3266 'itemtypes' => 'SER',
3269 Notes: forlibrarian should probably be passed in, and called something different.
3274 sub get_biblio_authorised_values {
3275 my $biblionumber = shift;
3278 my $forlibrarian = 1; # are we in staff or opac?
3279 my $frameworkcode = GetFrameworkCode( $biblionumber );
3281 my $authorised_values;
3283 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3284 or return $authorised_values;
3286 # assume that these entries in the authorised_value table are bibliolevel.
3287 # ones that start with 'item%' are item level.
3288 my $query = q(SELECT distinct authorised_value, kohafield
3289 FROM marc_subfield_structure
3290 WHERE authorised_value !=''
3291 AND (kohafield like 'biblio%'
3292 OR kohafield like '') );
3293 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3295 foreach my $tag ( keys( %$tagslib ) ) {
3296 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3297 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3298 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3299 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3300 if ( defined $record->field( $tag ) ) {
3301 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3302 if ( defined $this_subfield_value ) {
3303 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3310 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3311 return $authorised_values;
3321 Koha Developement team <info@koha.org>
3323 Paul POULAIN paul.poulain@free.fr
3325 Joshua Ferraro jmf@liblime.com