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;
29 use C4::Dates qw/format_date/;
30 use C4::Log; # logaction
35 use vars qw($VERSION @ISA @EXPORT);
41 @ISA = qw( Exporter );
55 &GetBiblioItemByBiblioNumber
56 &GetBiblioFromItemNumber
69 &GetAuthorisedValueDesc
73 &GetPublisherNameFromIsbn
88 # To link headings in a bib record
89 # to authority records.
91 &LinkBibHeadingsToAuthorities
95 # those functions are exported but should not be used
96 # they are usefull is few circumstances, so are exported.
97 # but don't use them unless you're a core developer ;-)
104 &TransformHtmlToMarc2
107 &PrepareItemrecordDisplay
114 C4::Biblio - cataloging management functions
118 Biblio.pm contains functions for managing storage and editing of bibliographic data within Koha. Most of the functions in this module are used for cataloging records: adding, editing, or removing biblios, biblioitems, or items. Koha's stores bibliographic information in three places:
122 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
124 =item 2. as raw MARC in the Zebra index and storage engine
126 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
130 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
132 Because the data isn't completely normalized there's a chance for information to get out of sync. The design choice to go with a un-normalized schema was driven by performance and stability concerns. However, if this occur, it can be considered as a bug : The API is (or should be) complete & the only entry point for all biblio/items managements.
136 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
138 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
142 Because of this design choice, the process of managing storage and editing is a bit convoluted. Historically, Biblio.pm's grown to an unmanagable size and as a result we have several types of functions currently:
146 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
148 =item 2. _koha_* - low-level internal functions for managing the koha tables
150 =item 3. Marc management function : as the MARC record is stored in biblioitems.marc(xml), some subs dedicated to it's management are in this package. They should be used only internally by Biblio.pm, the only official entry points being AddBiblio, AddItem, ModBiblio, ModItem.
152 =item 4. Zebra functions used to update the Zebra index
154 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
158 The MARC record (in biblioitems.marcxml) contains the complete marc record, including items. It also contains the biblionumber. That is the reason why it is not stored directly by AddBiblio, with all other fields . To save a biblio, we need to :
162 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
164 =item 2. add the biblionumber and biblioitemnumber into the MARC records
166 =item 3. save the marc record
170 When dealing with items, we must :
174 =item 1. save the item in items table, that gives us an itemnumber
176 =item 2. add the itemnumber to the item MARC field
178 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
180 When modifying a biblio or an item, the behaviour is quite similar.
184 =head1 EXPORTED FUNCTIONS
190 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
194 Exported function (core API) for adding a new biblio to koha.
196 The first argument is a C<MARC::Record> object containing the
197 bib to add, while the second argument is the desired MARC
200 This function also accepts a third, optional argument: a hashref
201 to additional options. The only defined option is C<defer_marc_save>,
202 which if present and mapped to a true value, causes C<AddBiblio>
203 to omit the call to save the MARC in C<bibilioitems.marc>
204 and C<biblioitems.marcxml> This option is provided B<only>
205 for the use of scripts such as C<bulkmarcimport.pl> that may need
206 to do some manipulation of the MARC record for item parsing before
207 saving it and which cannot afford the performance hit of saving
208 the MARC record twice. Consequently, do not use that option
209 unless you can guarantee that C<ModBiblioMarc> will be called.
215 my $frameworkcode = shift;
216 my $options = @_ ? shift : undef;
217 my $defer_marc_save = 0;
218 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
219 $defer_marc_save = 1;
222 my ($biblionumber,$biblioitemnumber,$error);
223 my $dbh = C4::Context->dbh;
224 # transform the data into koha-table style data
225 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
226 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
227 $olddata->{'biblionumber'} = $biblionumber;
228 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
230 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
232 # update MARC subfield that stores biblioitems.cn_sort
233 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
236 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
238 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
240 return ( $biblionumber, $biblioitemnumber );
247 ModBiblio( $record,$biblionumber,$frameworkcode);
251 Replace an existing bib record identified by C<$biblionumber>
252 with one supplied by the MARC::Record object C<$record>. The embedded
253 item, biblioitem, and biblionumber fields from the previous
254 version of the bib record replace any such fields of those tags that
255 are present in C<$record>. Consequently, ModBiblio() is not
256 to be used to try to modify item records.
258 C<$frameworkcode> specifies the MARC framework to use
259 when storing the modified bib record; among other things,
260 this controls how MARC fields get mapped to display columns
261 in the C<biblio> and C<biblioitems> tables, as well as
262 which fields are used to store embedded item, biblioitem,
263 and biblionumber data for indexing.
268 my ( $record, $biblionumber, $frameworkcode ) = @_;
269 if (C4::Context->preference("CataloguingLog")) {
270 my $newrecord = GetMarcBiblio($biblionumber);
271 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
274 my $dbh = C4::Context->dbh;
276 $frameworkcode = "" unless $frameworkcode;
278 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
279 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
280 my $oldRecord = GetMarcBiblio( $biblionumber );
282 # delete any item fields from incoming record to avoid
283 # duplication or incorrect data - use AddItem() or ModItem()
285 foreach my $field ($record->field($itemtag)) {
286 $record->delete_field($field);
289 # parse each item, and, for an unknown reason, re-encode each subfield
290 # if you don't do that, the record will have encoding mixed
291 # and the biblio will be re-encoded.
292 # strange, I (Paul P.) searched more than 1 day to understand what happends
293 # but could only solve the problem this way...
294 my @fields = $oldRecord->field( $itemtag );
295 foreach my $fielditem ( @fields ){
297 foreach ($fielditem->subfields()) {
299 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
301 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
304 $record->append_fields($field);
307 # update biblionumber and biblioitemnumber in MARC
308 # FIXME - this is assuming a 1 to 1 relationship between
309 # biblios and biblioitems
310 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
311 $sth->execute($biblionumber);
312 my ($biblioitemnumber) = $sth->fetchrow;
314 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
316 # load the koha-table data object
317 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
319 # update MARC subfield that stores biblioitems.cn_sort
320 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
322 # update the MARC record (that now contains biblio and items) with the new record data
323 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
325 # modify the other koha tables
326 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
327 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
331 =head2 ModBiblioframework
333 ModBiblioframework($biblionumber,$frameworkcode);
334 Exported function to modify a biblio framework
338 sub ModBiblioframework {
339 my ( $biblionumber, $frameworkcode ) = @_;
340 my $dbh = C4::Context->dbh;
341 my $sth = $dbh->prepare(
342 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
344 $sth->execute($frameworkcode, $biblionumber);
352 my $error = &DelBiblio($dbh,$biblionumber);
353 Exported function (core API) for deleting a biblio in koha.
354 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
355 Also backs it up to deleted* tables
356 Checks to make sure there are not issues on any of the items
358 C<$error> : undef unless an error occurs
365 my ( $biblionumber ) = @_;
366 my $dbh = C4::Context->dbh;
367 my $error; # for error handling
369 # First make sure this biblio has no items attached
370 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
371 $sth->execute($biblionumber);
372 if (my $itemnumber = $sth->fetchrow){
373 # Fix this to use a status the template can understand
374 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
377 return $error if $error;
379 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
380 # for at least 2 reasons :
381 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
382 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
383 # and we would have no way to remove it (except manually in zebra, but I bet it would be very hard to handle the problem)
385 if (C4::Context->preference("NoZebra")) {
386 # only NoZebra indexing needs to have
387 # the previous version of the record
388 $oldRecord = GetMarcBiblio($biblionumber);
390 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
392 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
395 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
396 $sth->execute($biblionumber);
397 while ( my $biblioitemnumber = $sth->fetchrow ) {
399 # delete this biblioitem
400 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
401 return $error if $error;
404 # delete biblio from Koha tables and save in deletedbiblio
405 # must do this *after* _koha_delete_biblioitems, otherwise
406 # delete cascade will prevent deletedbiblioitems rows
407 # from being generated by _koha_delete_biblioitems
408 $error = _koha_delete_biblio( $dbh, $biblionumber );
410 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
415 =head2 LinkBibHeadingsToAuthorities
419 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
423 Links bib headings to authority records by checking
424 each authority-controlled field in the C<MARC::Record>
425 object C<$marc>, looking for a matching authority record,
426 and setting the linking subfield $9 to the ID of that
429 If no matching authority exists, or if multiple
430 authorities match, no $9 will be added, and any
431 existing one inthe field will be deleted.
433 Returns the number of heading links changed in the
438 sub LinkBibHeadingsToAuthorities {
441 my $num_headings_changed = 0;
442 foreach my $field ($bib->fields()) {
443 my $heading = C4::Heading->new_from_bib_field($field);
444 next unless defined $heading;
447 my $current_link = $field->subfield('9');
449 # look for matching authorities
450 my $authorities = $heading->authorities();
452 # want only one exact match
453 if ($#{ $authorities } == 0) {
454 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
455 my $authid = $authority->field('001')->data();
456 next if defined $current_link and $current_link eq $authid;
458 $field->delete_subfield(code => '9') if defined $current_link;
459 $field->add_subfields('9', $authid);
460 $num_headings_changed++;
462 if (defined $current_link) {
463 $field->delete_subfield(code => '9');
464 $num_headings_changed++;
469 return $num_headings_changed;
476 $data = &GetBiblioData($biblionumber);
477 Returns information about the book with the given biblionumber.
478 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
479 the C<biblio> and C<biblioitems> tables in the
481 In addition, C<$data-E<gt>{subject}> is the list of the book's
482 subjects, separated by C<" , "> (space, comma, space).
483 If there are multiple biblioitems with the given biblionumber, only
484 the first one is considered.
492 my $dbh = C4::Context->dbh;
494 # my $query = C4::Context->preference('item-level_itypes') ?
495 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
497 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
498 # WHERE biblio.biblionumber = ?
499 # AND biblioitems.biblionumber = biblio.biblionumber
502 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
504 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
505 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
506 WHERE biblio.biblionumber = ?
507 AND biblioitems.biblionumber = biblio.biblionumber ";
509 my $sth = $dbh->prepare($query);
510 $sth->execute($bibnum);
512 $data = $sth->fetchrow_hashref;
516 } # sub GetBiblioData
518 =head2 &GetBiblioItemData
522 $itemdata = &GetBiblioItemData($biblioitemnumber);
524 Looks up the biblioitem with the given biblioitemnumber. Returns a
525 reference-to-hash. The keys are the fields from the C<biblio>,
526 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
527 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
534 sub GetBiblioItemData {
535 my ($biblioitemnumber) = @_;
536 my $dbh = C4::Context->dbh;
537 my $query = "SELECT *,biblioitems.notes AS bnotes
538 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
539 unless(C4::Context->preference('item-level_itypes')) {
540 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
542 $query .= " WHERE biblioitemnumber = ? ";
543 my $sth = $dbh->prepare($query);
545 $sth->execute($biblioitemnumber);
546 $data = $sth->fetchrow_hashref;
549 } # sub &GetBiblioItemData
551 =head2 GetBiblioItemByBiblioNumber
555 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
561 sub GetBiblioItemByBiblioNumber {
562 my ($biblionumber) = @_;
563 my $dbh = C4::Context->dbh;
564 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
568 $sth->execute($biblionumber);
570 while ( my $data = $sth->fetchrow_hashref ) {
571 push @results, $data;
578 =head2 GetBiblioFromItemNumber
582 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
584 Looks up the item with the given itemnumber. if undef, try the barcode.
586 C<&itemnodata> returns a reference-to-hash whose keys are the fields
587 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
595 sub GetBiblioFromItemNumber {
596 my ( $itemnumber, $barcode ) = @_;
597 my $dbh = C4::Context->dbh;
600 $sth=$dbh->prepare( "SELECT * FROM items
601 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
602 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
603 WHERE items.itemnumber = ?") ;
604 $sth->execute($itemnumber);
606 $sth=$dbh->prepare( "SELECT * FROM items
607 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
608 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
609 WHERE items.barcode = ?") ;
610 $sth->execute($barcode);
612 my $data = $sth->fetchrow_hashref;
621 $isbd = &GetISBDView($biblionumber);
623 Return the ISBD view which can be included in opac and intranet
630 my $biblionumber = shift;
631 my $record = GetMarcBiblio($biblionumber);
632 my $itemtype = &GetFrameworkCode($biblionumber);
633 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
634 my $tagslib = &GetMarcStructure( 1, $itemtype );
636 my $ISBD = C4::Context->preference('ISBD');
641 foreach my $isbdfield ( split (/#/, $bloc) ) {
643 # $isbdfield= /(.?.?.?)/;
644 $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
645 my $fieldvalue = $1 || 0;
646 my $subfvalue = $2 || "";
648 my $analysestring = $4;
651 # warn "==> $1 / $2 / $3 / $4";
652 # my $fieldvalue=substr($isbdfield,0,3);
653 if ( $fieldvalue > 0 ) {
654 my $hasputtextbefore = 0;
655 my @fieldslist = $record->field($fieldvalue);
656 @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
658 # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
659 # warn "FV : $fieldvalue";
660 if ($subfvalue ne ""){
661 foreach my $field ( @fieldslist ) {
662 foreach my $subfield ($field->subfield($subfvalue)){
663 my $calculated = $analysestring;
664 my $tag = $field->tag();
669 GetAuthorisedValueDesc( $tag, $subfvalue,
670 $subfield, '', $tagslib );
671 my $tagsubf = $tag . $subfvalue;
673 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
674 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
676 # field builded, store the result
677 if ( $calculated && !$hasputtextbefore )
678 { # put textbefore if not done
679 $blocres .= $textbefore;
680 $hasputtextbefore = 1;
683 # remove punctuation at start
684 $calculated =~ s/^( |;|:|\.|-)*//g;
685 $blocres .= $calculated;
690 $blocres .= $textafter if $hasputtextbefore;
692 foreach my $field ( @fieldslist ) {
693 my $calculated = $analysestring;
694 my $tag = $field->tag();
698 my @subf = $field->subfields;
699 for my $i ( 0 .. $#subf ) {
700 my $valuecode = $subf[$i][1];
701 my $subfieldcode = $subf[$i][0];
703 GetAuthorisedValueDesc( $tag, $subf[$i][0],
704 $subf[$i][1], '', $tagslib );
705 my $tagsubf = $tag . $subfieldcode;
707 $calculated =~ s/ # replace all {{}} codes by the value code.
708 \{\{$tagsubf\}\} # catch the {{actualcode}}
710 $valuecode # replace by the value code
714 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
715 $calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
718 # field builded, store the result
719 if ( $calculated && !$hasputtextbefore )
720 { # put textbefore if not done
721 $blocres .= $textbefore;
722 $hasputtextbefore = 1;
725 # remove punctuation at start
726 $calculated =~ s/^( |;|:|\.|-)*//g;
727 $blocres .= $calculated;
730 $blocres .= $textafter if $hasputtextbefore;
734 $blocres .= $isbdfield;
739 $res =~ s/\{(.*?)\}//g;
741 $res =~ s/\n/<br\/>/g;
753 ( $count, @results ) = &GetBiblio($biblionumber);
760 my ($biblionumber) = @_;
761 my $dbh = C4::Context->dbh;
762 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
765 $sth->execute($biblionumber);
766 while ( my $data = $sth->fetchrow_hashref ) {
767 $results[$count] = $data;
771 return ( $count, @results );
774 =head2 GetBiblioItemInfosOf
778 GetBiblioItemInfosOf(@biblioitemnumbers);
784 sub GetBiblioItemInfosOf {
785 my @biblioitemnumbers = @_;
788 SELECT biblioitemnumber,
792 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
794 return get_infos_of( $query, 'biblioitemnumber' );
797 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
799 =head2 GetMarcStructure
803 $res = GetMarcStructure($forlibrarian,$frameworkcode);
805 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
806 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
807 $frameworkcode : the framework code to read
813 # cache for results of GetMarcStructure -- needed
815 our $marc_structure_cache;
817 sub GetMarcStructure {
818 my ( $forlibrarian, $frameworkcode ) = @_;
819 my $dbh=C4::Context->dbh;
820 $frameworkcode = "" unless $frameworkcode;
822 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
823 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
827 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
829 # check that framework exists
832 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
833 $sth->execute($frameworkcode);
834 my ($total) = $sth->fetchrow;
835 $frameworkcode = "" unless ( $total > 0 );
838 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
839 FROM marc_tag_structure
840 WHERE frameworkcode=?
843 $sth->execute($frameworkcode);
844 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
846 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
849 $res->{$tag}->{lib} =
850 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
851 $res->{$tag}->{tab} = "";
852 $res->{$tag}->{mandatory} = $mandatory;
853 $res->{$tag}->{repeatable} = $repeatable;
858 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
859 FROM marc_subfield_structure
860 WHERE frameworkcode=?
861 ORDER BY tagfield,tagsubfield
865 $sth->execute($frameworkcode);
868 my $authorised_value;
880 $tag, $subfield, $liblibrarian,
882 $mandatory, $repeatable, $authorised_value,
883 $authtypecode, $value_builder, $kohafield,
884 $seealso, $hidden, $isurl,
890 $res->{$tag}->{$subfield}->{lib} =
891 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
892 $res->{$tag}->{$subfield}->{tab} = $tab;
893 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
894 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
895 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
896 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
897 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
898 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
899 $res->{$tag}->{$subfield}->{seealso} = $seealso;
900 $res->{$tag}->{$subfield}->{hidden} = $hidden;
901 $res->{$tag}->{$subfield}->{isurl} = $isurl;
902 $res->{$tag}->{$subfield}->{'link'} = $link;
903 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
906 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
911 =head2 GetUsedMarcStructure
913 the same function as GetMarcStructure expcet it just take field
914 in tab 0-9. (used field)
916 my $results = GetUsedMarcStructure($frameworkcode);
918 L<$results> is a ref to an array which each case containts a ref
919 to a hash which each keys is the columns from marc_subfield_structure
921 L<$frameworkcode> is the framework code.
925 sub GetUsedMarcStructure($){
926 my $frameworkcode = shift || '';
927 my $dbh = C4::Context->dbh;
930 FROM marc_subfield_structure
932 AND frameworkcode = ?
935 my $sth = $dbh->prepare($query);
936 $sth->execute($frameworkcode);
937 while (my $row = $sth->fetchrow_hashref){
943 =head2 GetMarcFromKohaField
947 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
948 Returns the MARC fields & subfields mapped to the koha field
949 for the given frameworkcode
955 sub GetMarcFromKohaField {
956 my ( $kohafield, $frameworkcode ) = @_;
957 return 0, 0 unless $kohafield and defined $frameworkcode;
958 my $relations = C4::Context->marcfromkohafield;
960 $relations->{$frameworkcode}->{$kohafield}->[0],
961 $relations->{$frameworkcode}->{$kohafield}->[1]
969 my $record = GetMarcBiblio($biblionumber);
973 Returns MARC::Record representing bib identified by
974 C<$biblionumber>. If no bib exists, returns undef.
975 The MARC record contains both biblio & item data.
980 my $biblionumber = shift;
981 my $dbh = C4::Context->dbh;
983 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
984 $sth->execute($biblionumber);
985 my $row = $sth->fetchrow_hashref;
986 my $marcxml = StripNonXmlChars($row->{'marcxml'});
987 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
988 my $record = MARC::Record->new();
990 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
991 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
992 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1003 my $marcxml = GetXmlBiblio($biblionumber);
1005 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1006 The XML contains both biblio & item datas
1013 my ( $biblionumber ) = @_;
1014 my $dbh = C4::Context->dbh;
1016 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1017 $sth->execute($biblionumber);
1018 my ($marcxml) = $sth->fetchrow;
1022 =head2 GetAuthorisedValueDesc
1026 my $subfieldvalue =get_authorised_value_desc(
1027 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1028 Retrieve the complete description for a given authorised value.
1030 Now takes $category and $value pair too.
1031 my $auth_value_desc =GetAuthorisedValueDesc(
1032 '','', 'DVD' ,'','','CCODE');
1038 sub GetAuthorisedValueDesc {
1039 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1040 my $dbh = C4::Context->dbh;
1044 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1047 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1048 return C4::Branch::GetBranchName($value);
1052 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1053 return getitemtypeinfo($value)->{description};
1056 #---- "true" authorized value
1057 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1060 if ( $category ne "" ) {
1063 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1065 $sth->execute( $category, $value );
1066 my $data = $sth->fetchrow_hashref;
1067 return $data->{'lib'};
1070 return $value; # if nothing is found return the original value
1078 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1079 Get all notes from the MARC record and returns them in an array.
1080 The note are stored in differents places depending on MARC flavour
1087 my ( $record, $marcflavour ) = @_;
1089 if ( $marcflavour eq "MARC21" ) {
1092 else { # assume unimarc if not marc21
1099 foreach my $field ( $record->field($scope) ) {
1100 my $value = $field->as_string();
1101 if ( $note ne "" ) {
1102 $marcnote = { marcnote => $note, };
1103 push @marcnotes, $marcnote;
1106 if ( $note ne $value ) {
1107 $note = $note . " " . $value;
1112 $marcnote = { marcnote => $note };
1113 push @marcnotes, $marcnote; #load last tag into array
1116 } # end GetMarcNotes
1118 =head2 GetMarcSubjects
1122 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1123 Get all subjects from the MARC record and returns them in an array.
1124 The subjects are stored in differents places depending on MARC flavour
1130 sub GetMarcSubjects {
1131 my ( $record, $marcflavour ) = @_;
1132 my ( $mintag, $maxtag );
1133 if ( $marcflavour eq "MARC21" ) {
1137 else { # assume unimarc if not marc21
1147 foreach my $field ( $record->field('6..' )) {
1148 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1150 my @subfields = $field->subfields();
1153 # if there is an authority link, build the link with an= subfield9
1154 my $subfield9 = $field->subfield('9');
1155 for my $subject_subfield (@subfields ) {
1156 # don't load unimarc subfields 3,4,5
1157 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1158 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1159 next if (($marcflavour eq "MARC21") and ($subject_subfield->[0] =~ /2/ ) );
1160 my $code = $subject_subfield->[0];
1161 my $value = $subject_subfield->[1];
1162 my $linkvalue = $value;
1163 $linkvalue =~ s/(\(|\))//g;
1164 my $operator = " and " unless $counter==0;
1166 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1168 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1170 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1172 my @this_link_loop = @link_loop;
1173 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1177 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1180 return \@marcsubjects;
1181 } #end getMARCsubjects
1183 =head2 GetMarcAuthors
1187 authors = GetMarcAuthors($record,$marcflavour);
1188 Get all authors from the MARC record and returns them in an array.
1189 The authors are stored in differents places depending on MARC flavour
1195 sub GetMarcAuthors {
1196 my ( $record, $marcflavour ) = @_;
1197 my ( $mintag, $maxtag );
1198 # tagslib useful for UNIMARC author reponsabilities
1199 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.
1200 if ( $marcflavour eq "MARC21" ) {
1204 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1213 foreach my $field ( $record->fields ) {
1214 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1217 my @subfields = $field->subfields();
1219 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1220 my $subfield9 = $field->subfield('9');
1221 for my $authors_subfield (@subfields) {
1222 # don't load unimarc subfields 3, 5
1223 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1224 my $subfieldcode = $authors_subfield->[0];
1225 my $value = $authors_subfield->[1];
1226 my $linkvalue = $value;
1227 $linkvalue =~ s/(\(|\))//g;
1228 my $operator = " and " unless $count_auth==0;
1229 # if we have an authority link, use that as the link, otherwise use standard searching
1231 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1234 # reset $linkvalue if UNIMARC author responsibility
1235 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1236 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1238 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1240 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1241 my @this_link_loop = @link_loop;
1242 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1243 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1246 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1248 return \@marcauthors;
1255 $marcurls = GetMarcUrls($record,$marcflavour);
1256 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1257 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1264 my ( $record, $marcflavour ) = @_;
1267 for my $field ( $record->field('856') ) {
1270 for my $note ( $field->subfield('z') ) {
1271 push @notes, { note => $note };
1273 my @urls = $field->subfield('u');
1274 foreach my $url (@urls) {
1275 if ( $marcflavour eq 'MARC21' ) {
1276 my $s3 = $field->subfield('3');
1277 my $link = $field->subfield('y');
1278 unless ( $url =~ /^\w+:/ ) {
1279 if ( $field->indicator(1) eq '7' ) {
1280 $url = $field->subfield('2') . "://" . $url;
1281 } elsif ( $field->indicator(1) eq '1' ) {
1282 $url = 'ftp://' . $url;
1284 # properly, this should be if ind1=4,
1285 # however we will assume http protocol since we're building a link.
1286 $url = 'http://' . $url;
1289 # TODO handle ind 2 (relationship)
1294 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1295 $marcurl->{'part'} = $s3 if ($link);
1296 $marcurl->{'toc'} = 1 if ( $s3 =~ /^[Tt]able/ );
1298 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1299 $marcurl->{'MARCURL'} = $url;
1301 push @marcurls, $marcurl;
1307 =head2 GetMarcSeries
1311 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1312 Get all series from the MARC record and returns them in an array.
1313 The series are stored in differents places depending on MARC flavour
1320 my ($record, $marcflavour) = @_;
1321 my ($mintag, $maxtag);
1322 if ($marcflavour eq "MARC21") {
1325 } else { # assume unimarc if not marc21
1335 foreach my $field ($record->field('440'), $record->field('490')) {
1337 #my $value = $field->subfield('a');
1338 #$marcsubjct = {MARCSUBJCT => $value,};
1339 my @subfields = $field->subfields();
1340 #warn "subfields:".join " ", @$subfields;
1343 for my $series_subfield (@subfields) {
1345 undef $volume_number;
1346 # see if this is an instance of a volume
1347 if ($series_subfield->[0] eq 'v') {
1351 my $code = $series_subfield->[0];
1352 my $value = $series_subfield->[1];
1353 my $linkvalue = $value;
1354 $linkvalue =~ s/(\(|\))//g;
1355 my $operator = " and " unless $counter==0;
1356 push @link_loop, {link => $linkvalue, operator => $operator };
1357 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1358 if ($volume_number) {
1359 push @subfields_loop, {volumenum => $value};
1362 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1366 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1367 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1368 #push @marcsubjcts, $marcsubjct;
1372 my $marcseriessarray=\@marcseries;
1373 return $marcseriessarray;
1374 } #end getMARCseriess
1376 =head2 GetFrameworkCode
1380 $frameworkcode = GetFrameworkCode( $biblionumber )
1386 sub GetFrameworkCode {
1387 my ( $biblionumber ) = @_;
1388 my $dbh = C4::Context->dbh;
1389 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1390 $sth->execute($biblionumber);
1391 my ($frameworkcode) = $sth->fetchrow;
1392 return $frameworkcode;
1395 =head2 GetPublisherNameFromIsbn
1397 $name = GetPublishercodeFromIsbn($isbn);
1404 sub GetPublisherNameFromIsbn($){
1406 $isbn =~ s/[- _]//g;
1408 my @codes = (split '-', DisplayISBN($isbn));
1409 my $code = $codes[0].$codes[1].$codes[2];
1410 my $dbh = C4::Context->dbh;
1412 SELECT distinct publishercode
1415 AND publishercode IS NOT NULL
1418 my $sth = $dbh->prepare($query);
1419 $sth->execute("$code%");
1420 my $name = $sth->fetchrow;
1421 return $name if length $name;
1425 =head2 TransformKohaToMarc
1429 $record = TransformKohaToMarc( $hash )
1430 This function builds partial MARC::Record from a hash
1431 Hash entries can be from biblio or biblioitems.
1432 This function is called in acquisition module, to create a basic catalogue entry from user entry
1438 sub TransformKohaToMarc {
1441 my $dbh = C4::Context->dbh;
1444 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1446 my $record = MARC::Record->new();
1447 foreach (keys %{$hash}) {
1448 &TransformKohaToMarcOneField( $sth, $record, $_,
1454 =head2 TransformKohaToMarcOneField
1458 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1464 sub TransformKohaToMarcOneField {
1465 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1466 $frameworkcode='' unless $frameworkcode;
1470 if ( !defined $sth ) {
1471 my $dbh = C4::Context->dbh;
1472 $sth = $dbh->prepare(
1473 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1476 $sth->execute( $frameworkcode, $kohafieldname );
1477 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1478 my $tag = $record->field($tagfield);
1480 $tag->update( $tagsubfield => $value );
1481 $record->delete_field($tag);
1482 $record->insert_fields_ordered($tag);
1485 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1491 =head2 TransformHtmlToXml
1495 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1497 $auth_type contains :
1498 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1499 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1500 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1506 sub TransformHtmlToXml {
1507 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1508 my $xml = MARC::File::XML::header('UTF-8');
1509 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1510 MARC::File::XML->default_record_format($auth_type);
1511 # in UNIMARC, field 100 contains the encoding
1512 # check that there is one, otherwise the
1513 # MARC::Record->new_from_xml will fail (and Koha will die)
1514 my $unimarc_and_100_exist=0;
1515 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1520 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1521 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1522 # if we have a 100 field and it's values are not correct, skip them.
1523 # if we don't have any valid 100 field, we will create a default one at the end
1524 my $enc = substr( @$values[$i], 26, 2 );
1525 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1526 $unimarc_and_100_exist=1;
1531 @$values[$i] =~ s/&/&/g;
1532 @$values[$i] =~ s/</</g;
1533 @$values[$i] =~ s/>/>/g;
1534 @$values[$i] =~ s/"/"/g;
1535 @$values[$i] =~ s/'/'/g;
1536 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1537 # utf8::decode( @$values[$i] );
1539 if ( ( @$tags[$i] ne $prevtag ) ) {
1540 $j++ unless ( @$tags[$i] eq "" );
1542 $xml .= "</datafield>\n";
1543 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1544 && ( @$values[$i] ne "" ) )
1546 my $ind1 = substr( @$indicator[$j], 0, 1 );
1548 if ( @$indicator[$j] ) {
1549 $ind2 = substr( @$indicator[$j], 1, 1 );
1552 warn "Indicator in @$tags[$i] is empty";
1556 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1558 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1566 if ( @$values[$i] ne "" ) {
1569 if ( @$tags[$i] eq "000" ) {
1570 $xml .= "<leader>@$values[$i]</leader>\n";
1573 # rest of the fixed fields
1575 elsif ( @$tags[$i] < 10 ) {
1577 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1581 my $ind1 = substr( @$indicator[$j], 0, 1 );
1582 my $ind2 = substr( @$indicator[$j], 1, 1 );
1584 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1586 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1592 else { # @$tags[$i] eq $prevtag
1593 if ( @$values[$i] eq "" ) {
1597 my $ind1 = substr( @$indicator[$j], 0, 1 );
1598 my $ind2 = substr( @$indicator[$j], 1, 1 );
1600 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1604 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1607 $prevtag = @$tags[$i];
1609 $xml .= "</datafield>\n" if @$tags > 0;
1610 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1611 # warn "SETTING 100 for $auth_type";
1612 use POSIX qw(strftime);
1613 my $string = strftime( "%Y%m%d", localtime(time) );
1614 # set 50 to position 26 is biblios, 13 if authorities
1616 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1617 $string = sprintf( "%-*s", 35, $string );
1618 substr( $string, $pos , 6, "50" );
1619 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1620 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1621 $xml .= "</datafield>\n";
1623 $xml .= MARC::File::XML::footer();
1627 =head2 TransformHtmlToMarc
1629 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1630 L<$params> is a ref to an array as below:
1632 'tag_010_indicator1_531951' ,
1633 'tag_010_indicator2_531951' ,
1634 'tag_010_code_a_531951_145735' ,
1635 'tag_010_subfield_a_531951_145735' ,
1636 'tag_200_indicator1_873510' ,
1637 'tag_200_indicator2_873510' ,
1638 'tag_200_code_a_873510_673465' ,
1639 'tag_200_subfield_a_873510_673465' ,
1640 'tag_200_code_b_873510_704318' ,
1641 'tag_200_subfield_b_873510_704318' ,
1642 'tag_200_code_e_873510_280822' ,
1643 'tag_200_subfield_e_873510_280822' ,
1644 'tag_200_code_f_873510_110730' ,
1645 'tag_200_subfield_f_873510_110730' ,
1647 L<$cgi> is the CGI object which containts the value.
1648 L<$record> is the MARC::Record object.
1652 sub TransformHtmlToMarc {
1656 # explicitly turn on the UTF-8 flag for all
1657 # 'tag_' parameters to avoid incorrect character
1658 # conversion later on
1659 my $cgi_params = $cgi->Vars;
1660 foreach my $param_name (keys %$cgi_params) {
1661 if ($param_name =~ /^tag_/) {
1662 my $param_value = $cgi_params->{$param_name};
1663 if (utf8::decode($param_value)) {
1664 $cgi_params->{$param_name} = $param_value;
1666 # FIXME - need to do something if string is not valid UTF-8
1670 # creating a new record
1671 my $record = MARC::Record->new();
1674 while ($params->[$i]){ # browse all CGI params
1675 my $param = $params->[$i];
1677 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1678 if ($param eq 'biblionumber') {
1679 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1680 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1681 if ($biblionumbertagfield < 10) {
1682 $newfield = MARC::Field->new(
1683 $biblionumbertagfield,
1684 $cgi->param($param),
1687 $newfield = MARC::Field->new(
1688 $biblionumbertagfield,
1691 "$biblionumbertagsubfield" => $cgi->param($param),
1694 push @fields,$newfield if($newfield);
1696 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1699 my $ind1 = substr($cgi->param($param),0,1);
1700 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1704 if($tag < 10){ # no code for theses fields
1705 # in MARC editor, 000 contains the leader.
1706 if ($tag eq '000' ) {
1707 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1708 # between 001 and 009 (included)
1709 } elsif ($cgi->param($params->[$j+1]) ne '') {
1710 $newfield = MARC::Field->new(
1712 $cgi->param($params->[$j+1]),
1715 # > 009, deal with subfields
1717 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1718 my $inner_param = $params->[$j];
1720 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1721 $newfield->add_subfields(
1722 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1726 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1727 $newfield = MARC::Field->new(
1731 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1738 push @fields,$newfield if($newfield);
1743 $record->append_fields(@fields);
1747 # cache inverted MARC field map
1748 our $inverted_field_map;
1750 =head2 TransformMarcToKoha
1754 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1758 Extract data from a MARC bib record into a hashref representing
1759 Koha biblio, biblioitems, and items fields.
1762 sub TransformMarcToKoha {
1763 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1766 $limit_table=$limit_table||0;
1767 $frameworkcode = '' unless defined $frameworkcode;
1769 unless (defined $inverted_field_map) {
1770 $inverted_field_map = _get_inverted_marc_field_map();
1774 if ( defined $limit_table && $limit_table eq 'items') {
1775 $tables{'items'} = 1;
1777 $tables{'items'} = 1;
1778 $tables{'biblio'} = 1;
1779 $tables{'biblioitems'} = 1;
1782 # traverse through record
1783 MARCFIELD: foreach my $field ($record->fields()) {
1784 my $tag = $field->tag();
1785 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1786 if ($field->is_control_field()) {
1787 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1788 ENTRY: foreach my $entry (@{ $kohafields }) {
1789 my ($subfield, $table, $column) = @{ $entry };
1790 next ENTRY unless exists $tables{$table};
1791 my $key = _disambiguate($table, $column);
1792 if ($result->{$key}) {
1793 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1794 $result->{$key} .= " | " . $field->data();
1797 $result->{$key} = $field->data();
1801 # deal with subfields
1802 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1803 my $code = $sf->[0];
1804 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1805 my $value = $sf->[1];
1806 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1807 my ($table, $column) = @{ $entry };
1808 next SFENTRY unless exists $tables{$table};
1809 my $key = _disambiguate($table, $column);
1810 if ($result->{$key}) {
1811 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1812 $result->{$key} .= " | " . $value;
1815 $result->{$key} = $value;
1822 # modify copyrightdate to keep only the 1st year found
1823 if (exists $result->{'copyrightdate'}) {
1824 my $temp = $result->{'copyrightdate'};
1825 $temp =~ m/c(\d\d\d\d)/;
1826 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1827 $result->{'copyrightdate'} = $1;
1829 else { # if no cYYYY, get the 1st date.
1830 $temp =~ m/(\d\d\d\d)/;
1831 $result->{'copyrightdate'} = $1;
1835 # modify publicationyear to keep only the 1st year found
1836 if (exists $result->{'publicationyear'}) {
1837 my $temp = $result->{'publicationyear'};
1838 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1839 $result->{'publicationyear'} = $1;
1841 else { # if no cYYYY, get the 1st date.
1842 $temp =~ m/(\d\d\d\d)/;
1843 $result->{'publicationyear'} = $1;
1850 sub _get_inverted_marc_field_map {
1852 my $relations = C4::Context->marcfromkohafield;
1854 foreach my $frameworkcode (keys %{ $relations }) {
1855 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1856 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1857 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1858 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1859 my ($table, $column) = split /[.]/, $kohafield, 2;
1860 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1861 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1867 =head2 _disambiguate
1871 $newkey = _disambiguate($table, $field);
1873 This is a temporary hack to distinguish between the
1874 following sets of columns when using TransformMarcToKoha.
1876 items.cn_source & biblioitems.cn_source
1877 items.cn_sort & biblioitems.cn_sort
1879 Columns that are currently NOT distinguished (FIXME
1880 due to lack of time to fully test) are:
1882 biblio.notes and biblioitems.notes
1887 FIXME - this is necessary because prefixing each column
1888 name with the table name would require changing lots
1889 of code and templates, and exposing more of the DB
1890 structure than is good to the UI templates, particularly
1891 since biblio and bibloitems may well merge in a future
1892 version. In the future, it would also be good to
1893 separate DB access and UI presentation field names
1901 my ($table, $column) = @_;
1902 if ($column eq "cn_sort" or $column eq "cn_source") {
1903 return $table . '.' . $column;
1910 =head2 get_koha_field_from_marc
1914 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1916 Internal function to map data from the MARC record to a specific non-MARC field.
1917 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1923 sub get_koha_field_from_marc {
1924 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1925 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1927 foreach my $field ( $record->field($tagfield) ) {
1928 if ( $field->tag() < 10 ) {
1930 $kohafield .= " | " . $field->data();
1933 $kohafield = $field->data();
1937 if ( $field->subfields ) {
1938 my @subfields = $field->subfields();
1939 foreach my $subfieldcount ( 0 .. $#subfields ) {
1940 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1943 " | " . $subfields[$subfieldcount][1];
1947 $subfields[$subfieldcount][1];
1958 =head2 TransformMarcToKohaOneField
1962 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1968 sub TransformMarcToKohaOneField {
1970 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1971 # only the 1st will be retrieved...
1972 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1974 my ( $tagfield, $subfield ) =
1975 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1977 foreach my $field ( $record->field($tagfield) ) {
1978 if ( $field->tag() < 10 ) {
1979 if ( $result->{$kohafield} ) {
1980 $result->{$kohafield} .= " | " . $field->data();
1983 $result->{$kohafield} = $field->data();
1987 if ( $field->subfields ) {
1988 my @subfields = $field->subfields();
1989 foreach my $subfieldcount ( 0 .. $#subfields ) {
1990 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1991 if ( $result->{$kohafield} ) {
1992 $result->{$kohafield} .=
1993 " | " . $subfields[$subfieldcount][1];
1996 $result->{$kohafield} =
1997 $subfields[$subfieldcount][1];
2007 =head1 OTHER FUNCTIONS
2010 =head2 PrepareItemrecordDisplay
2014 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
2016 Returns a hash with all the fields for Display a given item data in a template
2022 sub PrepareItemrecordDisplay {
2024 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2026 my $dbh = C4::Context->dbh;
2027 my $frameworkcode = &GetFrameworkCode( $bibnum );
2028 my ( $itemtagfield, $itemtagsubfield ) =
2029 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2030 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2031 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2033 my $authorised_values_sth =
2035 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2037 foreach my $tag ( sort keys %{$tagslib} ) {
2038 my $previous_tag = '';
2040 # loop through each subfield
2042 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2043 next if ( subfield_is_koha_internal_p($subfield) );
2044 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2046 $subfield_data{tag} = $tag;
2047 $subfield_data{subfield} = $subfield;
2048 $subfield_data{countsubfield} = $cntsubf++;
2049 $subfield_data{kohafield} =
2050 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2052 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2053 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2054 $subfield_data{mandatory} =
2055 $tagslib->{$tag}->{$subfield}->{mandatory};
2056 $subfield_data{repeatable} =
2057 $tagslib->{$tag}->{$subfield}->{repeatable};
2058 $subfield_data{hidden} = "display:none"
2059 if $tagslib->{$tag}->{$subfield}->{hidden};
2061 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2063 $value =~ s/"/"/g;
2065 # search for itemcallnumber if applicable
2066 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2067 'items.itemcallnumber'
2068 && C4::Context->preference('itemcallnumber') )
2071 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2073 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2074 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2076 $value = $temp->subfield($CNsubfield);
2079 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2080 'items.itemcallnumber'
2081 && $defaultvalues->{'callnumber'} )
2083 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2085 $value = $defaultvalues->{'callnumber'};
2088 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2089 'items.holdingbranch' ||
2090 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2092 && $defaultvalues->{'branchcode'} )
2094 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2096 $value = $defaultvalues->{branchcode};
2099 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2100 my @authorised_values;
2103 # builds list, depending on authorised value...
2105 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2108 if ( ( C4::Context->preference("IndependantBranches") )
2109 && ( C4::Context->userenv->{flags} != 1 ) )
2113 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2115 $sth->execute( C4::Context->userenv->{branch} );
2116 push @authorised_values, ""
2118 $tagslib->{$tag}->{$subfield}->{mandatory} );
2119 while ( my ( $branchcode, $branchname ) =
2120 $sth->fetchrow_array )
2122 push @authorised_values, $branchcode;
2123 $authorised_lib{$branchcode} = $branchname;
2129 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2132 push @authorised_values, ""
2134 $tagslib->{$tag}->{$subfield}->{mandatory} );
2135 while ( my ( $branchcode, $branchname ) =
2136 $sth->fetchrow_array )
2138 push @authorised_values, $branchcode;
2139 $authorised_lib{$branchcode} = $branchname;
2145 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2150 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2153 push @authorised_values, ""
2154 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2155 while ( my ( $itemtype, $description ) =
2156 $sth->fetchrow_array )
2158 push @authorised_values, $itemtype;
2159 $authorised_lib{$itemtype} = $description;
2162 #---- "true" authorised value
2165 $authorised_values_sth->execute(
2166 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2167 push @authorised_values, ""
2168 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2169 while ( my ( $value, $lib ) =
2170 $authorised_values_sth->fetchrow_array )
2172 push @authorised_values, $value;
2173 $authorised_lib{$value} = $lib;
2176 $subfield_data{marc_value} = CGI::scrolling_list(
2177 -name => 'field_value',
2178 -values => \@authorised_values,
2179 -default => "$value",
2180 -labels => \%authorised_lib,
2187 $subfield_data{marc_value} =
2188 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2190 push( @loop_data, \%subfield_data );
2194 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2195 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2197 'itemtagfield' => $itemtagfield,
2198 'itemtagsubfield' => $itemtagsubfield,
2199 'itemnumber' => $itemnumber,
2200 'iteminformation' => \@loop_data
2206 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2208 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2209 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2210 # =head2 ModZebrafiles
2212 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2216 # sub ModZebrafiles {
2218 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2222 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2223 # unless ( opendir( DIR, "$zebradir" ) ) {
2224 # warn "$zebradir not found";
2228 # my $filename = $zebradir . $biblionumber;
2231 # open( OUTPUT, ">", $filename . ".xml" );
2232 # print OUTPUT $record;
2241 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2243 $biblionumber is the biblionumber we want to index
2244 $op is specialUpdate or delete, and is used to know what we want to do
2245 $server is the server that we want to update
2246 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2247 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2249 $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.
2256 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2257 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2258 my $dbh=C4::Context->dbh;
2260 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2262 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2263 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2265 if (C4::Context->preference("NoZebra")) {
2266 # lock the nozebra table : we will read index lines, update them in Perl process
2267 # and write everything in 1 transaction.
2268 # lock the table to avoid someone else overwriting what we are doing
2269 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2270 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2271 if ($op eq 'specialUpdate') {
2272 # OK, we have to add or update the record
2273 # 1st delete (virtually, in indexes), if record actually exists
2275 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2277 # ... add the record
2278 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2280 # it's a deletion, delete the record...
2281 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2282 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2284 # ok, now update the database...
2285 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2286 foreach my $key (keys %result) {
2287 foreach my $index (keys %{$result{$key}}) {
2288 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2291 $dbh->do('UNLOCK TABLES');
2294 # we use zebra, just fill zebraqueue table
2296 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2298 AND biblio_auth_number = ?
2301 my $check_sth = $dbh->prepare_cached($check_sql);
2302 $check_sth->execute($server, $biblionumber, $op);
2303 my ($count) = $check_sth->fetchrow_array;
2304 $check_sth->finish();
2306 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2307 $sth->execute($biblionumber,$server,$op);
2313 =head2 GetNoZebraIndexes
2315 %indexes = GetNoZebraIndexes;
2317 return the data from NoZebraIndexes syspref.
2321 sub GetNoZebraIndexes {
2322 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2324 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2325 $line =~ /(.*)=>(.*)/;
2326 my $index = $1; # initial ' or " is removed afterwards
2328 $index =~ s/'|"|\s//g;
2329 $fields =~ s/'|"|\s//g;
2330 $indexes{$index}=$fields;
2335 =head1 INTERNAL FUNCTIONS
2337 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2339 function to delete a biblio in NoZebra indexes
2340 This function does NOT delete anything in database : it reads all the indexes entries
2341 that have to be deleted & delete them in the hash
2342 The SQL part is done either :
2343 - after the Add if we are modifying a biblio (delete + add again)
2344 - immediatly after this sub if we are doing a true deletion.
2345 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2350 sub _DelBiblioNoZebra {
2351 my ($biblionumber, $record, $server)=@_;
2354 my $dbh = C4::Context->dbh;
2358 if ($server eq 'biblioserver') {
2359 %index=GetNoZebraIndexes;
2360 # get title of the record (to store the 10 first letters with the index)
2361 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title', ''); # FIXME: should be GetFrameworkCode($biblionumber) ??
2362 $title = lc($record->subfield($titletag,$titlesubfield));
2364 # for authorities, the "title" is the $a mainentry
2365 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2366 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2367 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2368 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2369 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2370 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2371 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2375 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2376 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2377 # limit to 10 char, should be enough, and limit the DB size
2378 $title = substr($title,0,10);
2380 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2381 foreach my $field ($record->fields()) {
2382 #parse each subfield
2383 next if $field->tag <10;
2384 foreach my $subfield ($field->subfields()) {
2385 my $tag = $field->tag();
2386 my $subfieldcode = $subfield->[0];
2388 # check each index to see if the subfield is stored somewhere
2389 # otherwise, store it in __RAW__ index
2390 foreach my $key (keys %index) {
2391 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2392 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2394 my $line= lc $subfield->[1];
2395 # remove meaningless value in the field...
2396 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2397 # ... and split in words
2398 foreach (split / /,$line) {
2399 next unless $_; # skip empty values (multiple spaces)
2400 # if the entry is already here, do nothing, the biblionumber has already be removed
2401 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2402 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2403 $sth2->execute($server,$key,$_);
2404 my $existing_biblionumbers = $sth2->fetchrow;
2406 if ($existing_biblionumbers) {
2407 # warn " existing for $key $_: $existing_biblionumbers";
2408 $result{$key}->{$_} =$existing_biblionumbers;
2409 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2415 # the subfield is not indexed, store it in __RAW__ index anyway
2417 my $line= lc $subfield->[1];
2418 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2419 # ... and split in words
2420 foreach (split / /,$line) {
2421 next unless $_; # skip empty values (multiple spaces)
2422 # if the entry is already here, do nothing, the biblionumber has already be removed
2423 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2424 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2425 $sth2->execute($server,'__RAW__',$_);
2426 my $existing_biblionumbers = $sth2->fetchrow;
2428 if ($existing_biblionumbers) {
2429 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2430 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2440 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2442 function to add a biblio in NoZebra indexes
2446 sub _AddBiblioNoZebra {
2447 my ($biblionumber, $record, $server, %result)=@_;
2448 my $dbh = C4::Context->dbh;
2452 if ($server eq 'biblioserver') {
2453 %index=GetNoZebraIndexes;
2454 # get title of the record (to store the 10 first letters with the index)
2455 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title', ''); # FIXME: should be GetFrameworkCode($biblionumber) ??
2456 $title = lc($record->subfield($titletag,$titlesubfield));
2458 # warn "server : $server";
2459 # for authorities, the "title" is the $a mainentry
2460 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2461 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2462 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2463 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2464 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2465 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2466 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2469 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2470 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2471 # limit to 10 char, should be enough, and limit the DB size
2472 $title = substr($title,0,10);
2474 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2475 foreach my $field ($record->fields()) {
2476 #parse each subfield
2477 ###FIXME: impossible to index a 001-009 value with NoZebra
2478 next if $field->tag <10;
2479 foreach my $subfield ($field->subfields()) {
2480 my $tag = $field->tag();
2481 my $subfieldcode = $subfield->[0];
2483 # warn "INDEXING :".$subfield->[1];
2484 # check each index to see if the subfield is stored somewhere
2485 # otherwise, store it in __RAW__ index
2486 foreach my $key (keys %index) {
2487 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2488 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2490 my $line= lc $subfield->[1];
2491 # remove meaningless value in the field...
2492 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2493 # ... and split in words
2494 foreach (split / /,$line) {
2495 next unless $_; # skip empty values (multiple spaces)
2496 # if the entry is already here, improve weight
2497 # warn "managing $_";
2498 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2499 my $weight = $1 + 1;
2500 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2501 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2503 # get the value if it exist in the nozebra table, otherwise, create it
2504 $sth2->execute($server,$key,$_);
2505 my $existing_biblionumbers = $sth2->fetchrow;
2507 if ($existing_biblionumbers) {
2508 $result{$key}->{"$_"} =$existing_biblionumbers;
2509 my $weight = defined $1 ? $1 + 1 : 1;
2510 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2511 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2512 # create a new ligne for this entry
2514 # warn "INSERT : $server / $key / $_";
2515 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2516 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2522 # the subfield is not indexed, store it in __RAW__ index anyway
2524 my $line= lc $subfield->[1];
2525 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2526 # ... and split in words
2527 foreach (split / /,$line) {
2528 next unless $_; # skip empty values (multiple spaces)
2529 # if the entry is already here, improve weight
2530 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2532 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2533 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2535 # get the value if it exist in the nozebra table, otherwise, create it
2536 $sth2->execute($server,'__RAW__',$_);
2537 my $existing_biblionumbers = $sth2->fetchrow;
2539 if ($existing_biblionumbers) {
2540 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2542 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2543 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2544 # create a new ligne for this entry
2546 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2547 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2562 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2564 Find the given $subfield in the given $tag in the given
2565 MARC::Record $record. If the subfield is found, returns
2566 the (indicators, value) pair; otherwise, (undef, undef) is
2570 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2571 I suggest we export it from this module.
2578 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2581 if ( $tagfield < 10 ) {
2582 if ( $record->field($tagfield) ) {
2583 push @result, $record->field($tagfield)->data();
2590 foreach my $field ( $record->field($tagfield) ) {
2591 my @subfields = $field->subfields();
2592 foreach my $subfield (@subfields) {
2593 if ( @$subfield[0] eq $insubfield ) {
2594 push @result, @$subfield[1];
2595 $indicator = $field->indicator(1) . $field->indicator(2);
2600 return ( $indicator, @result );
2603 =head2 _koha_marc_update_bib_ids
2607 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2609 Internal function to add or update biblionumber and biblioitemnumber to
2616 sub _koha_marc_update_bib_ids {
2617 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2619 # we must add bibnum and bibitemnum in MARC::Record...
2620 # we build the new field with biblionumber and biblioitemnumber
2621 # we drop the original field
2622 # we add the new builded field.
2623 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2624 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2626 if ($biblio_tag != $biblioitem_tag) {
2627 # biblionumber & biblioitemnumber are in different fields
2629 # deal with biblionumber
2630 my ($new_field, $old_field);
2631 if ($biblio_tag < 10) {
2632 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2635 MARC::Field->new( $biblio_tag, '', '',
2636 "$biblio_subfield" => $biblionumber );
2639 # drop old field and create new one...
2640 $old_field = $record->field($biblio_tag);
2641 $record->delete_field($old_field) if $old_field;
2642 $record->append_fields($new_field);
2644 # deal with biblioitemnumber
2645 if ($biblioitem_tag < 10) {
2646 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2649 MARC::Field->new( $biblioitem_tag, '', '',
2650 "$biblioitem_subfield" => $biblioitemnumber, );
2652 # drop old field and create new one...
2653 $old_field = $record->field($biblioitem_tag);
2654 $record->delete_field($old_field) if $old_field;
2655 $record->insert_fields_ordered($new_field);
2658 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2659 my $new_field = MARC::Field->new(
2660 $biblio_tag, '', '',
2661 "$biblio_subfield" => $biblionumber,
2662 "$biblioitem_subfield" => $biblioitemnumber
2665 # drop old field and create new one...
2666 my $old_field = $record->field($biblio_tag);
2667 $record->delete_field($old_field) if $old_field;
2668 $record->insert_fields_ordered($new_field);
2672 =head2 _koha_marc_update_biblioitem_cn_sort
2676 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2680 Given a MARC bib record and the biblioitem hash, update the
2681 subfield that contains a copy of the value of biblioitems.cn_sort.
2685 sub _koha_marc_update_biblioitem_cn_sort {
2687 my $biblioitem = shift;
2688 my $frameworkcode= shift;
2690 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2691 return unless $biblioitem_tag;
2693 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2695 if (my $field = $marc->field($biblioitem_tag)) {
2696 $field->delete_subfield(code => $biblioitem_subfield);
2697 if ($cn_sort ne '') {
2698 $field->add_subfields($biblioitem_subfield => $cn_sort);
2701 # if we get here, no biblioitem tag is present in the MARC record, so
2702 # we'll create it if $cn_sort is not empty -- this would be
2703 # an odd combination of events, however
2705 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2710 =head2 _koha_add_biblio
2714 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2716 Internal function to add a biblio ($biblio is a hash with the values)
2722 sub _koha_add_biblio {
2723 my ( $dbh, $biblio, $frameworkcode ) = @_;
2727 # set the series flag
2729 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2733 SET frameworkcode = ?,
2744 my $sth = $dbh->prepare($query);
2747 $biblio->{'author'},
2749 $biblio->{'unititle'},
2752 $biblio->{'seriestitle'},
2753 $biblio->{'copyrightdate'},
2754 $biblio->{'abstract'}
2757 my $biblionumber = $dbh->{'mysql_insertid'};
2758 if ( $dbh->errstr ) {
2759 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2764 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2765 return ($biblionumber,$error);
2768 =head2 _koha_modify_biblio
2772 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2774 Internal function for updating the biblio table
2780 sub _koha_modify_biblio {
2781 my ( $dbh, $biblio, $frameworkcode ) = @_;
2786 SET frameworkcode = ?,
2795 WHERE biblionumber = ?
2798 my $sth = $dbh->prepare($query);
2802 $biblio->{'author'},
2804 $biblio->{'unititle'},
2806 $biblio->{'serial'},
2807 $biblio->{'seriestitle'},
2808 $biblio->{'copyrightdate'},
2809 $biblio->{'abstract'},
2810 $biblio->{'biblionumber'}
2811 ) if $biblio->{'biblionumber'};
2813 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2814 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2817 return ( $biblio->{'biblionumber'},$error );
2820 =head2 _koha_modify_biblioitem_nonmarc
2824 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2826 Updates biblioitems row except for marc and marcxml, which should be changed
2833 sub _koha_modify_biblioitem_nonmarc {
2834 my ( $dbh, $biblioitem ) = @_;
2837 # re-calculate the cn_sort, it may have changed
2838 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2842 SET biblionumber = ?,
2848 publicationyear = ?,
2852 collectiontitle = ?,
2854 collectionvolume= ?,
2855 editionstatement= ?,
2856 editionresponsibility = ?,
2870 where biblioitemnumber = ?
2872 my $sth = $dbh->prepare($query);
2874 $biblioitem->{'biblionumber'},
2875 $biblioitem->{'volume'},
2876 $biblioitem->{'number'},
2877 $biblioitem->{'itemtype'},
2878 $biblioitem->{'isbn'},
2879 $biblioitem->{'issn'},
2880 $biblioitem->{'publicationyear'},
2881 $biblioitem->{'publishercode'},
2882 $biblioitem->{'volumedate'},
2883 $biblioitem->{'volumedesc'},
2884 $biblioitem->{'collectiontitle'},
2885 $biblioitem->{'collectionissn'},
2886 $biblioitem->{'collectionvolume'},
2887 $biblioitem->{'editionstatement'},
2888 $biblioitem->{'editionresponsibility'},
2889 $biblioitem->{'illus'},
2890 $biblioitem->{'pages'},
2891 $biblioitem->{'bnotes'},
2892 $biblioitem->{'size'},
2893 $biblioitem->{'place'},
2894 $biblioitem->{'lccn'},
2895 $biblioitem->{'url'},
2896 $biblioitem->{'biblioitems.cn_source'},
2897 $biblioitem->{'cn_class'},
2898 $biblioitem->{'cn_item'},
2899 $biblioitem->{'cn_suffix'},
2901 $biblioitem->{'totalissues'},
2902 $biblioitem->{'biblioitemnumber'}
2904 if ( $dbh->errstr ) {
2905 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2908 return ($biblioitem->{'biblioitemnumber'},$error);
2911 =head2 _koha_add_biblioitem
2915 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2917 Internal function to add a biblioitem
2923 sub _koha_add_biblioitem {
2924 my ( $dbh, $biblioitem ) = @_;
2927 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2929 "INSERT INTO biblioitems SET
2936 publicationyear = ?,
2940 collectiontitle = ?,
2942 collectionvolume= ?,
2943 editionstatement= ?,
2944 editionresponsibility = ?,
2960 my $sth = $dbh->prepare($query);
2962 $biblioitem->{'biblionumber'},
2963 $biblioitem->{'volume'},
2964 $biblioitem->{'number'},
2965 $biblioitem->{'itemtype'},
2966 $biblioitem->{'isbn'},
2967 $biblioitem->{'issn'},
2968 $biblioitem->{'publicationyear'},
2969 $biblioitem->{'publishercode'},
2970 $biblioitem->{'volumedate'},
2971 $biblioitem->{'volumedesc'},
2972 $biblioitem->{'collectiontitle'},
2973 $biblioitem->{'collectionissn'},
2974 $biblioitem->{'collectionvolume'},
2975 $biblioitem->{'editionstatement'},
2976 $biblioitem->{'editionresponsibility'},
2977 $biblioitem->{'illus'},
2978 $biblioitem->{'pages'},
2979 $biblioitem->{'bnotes'},
2980 $biblioitem->{'size'},
2981 $biblioitem->{'place'},
2982 $biblioitem->{'lccn'},
2983 $biblioitem->{'marc'},
2984 $biblioitem->{'url'},
2985 $biblioitem->{'biblioitems.cn_source'},
2986 $biblioitem->{'cn_class'},
2987 $biblioitem->{'cn_item'},
2988 $biblioitem->{'cn_suffix'},
2990 $biblioitem->{'totalissues'}
2992 my $bibitemnum = $dbh->{'mysql_insertid'};
2993 if ( $dbh->errstr ) {
2994 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2998 return ($bibitemnum,$error);
3001 =head2 _koha_delete_biblio
3005 $error = _koha_delete_biblio($dbh,$biblionumber);
3007 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3009 C<$dbh> - the database handle
3010 C<$biblionumber> - the biblionumber of the biblio to be deleted
3016 # FIXME: add error handling
3018 sub _koha_delete_biblio {
3019 my ( $dbh, $biblionumber ) = @_;
3021 # get all the data for this biblio
3022 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3023 $sth->execute($biblionumber);
3025 if ( my $data = $sth->fetchrow_hashref ) {
3027 # save the record in deletedbiblio
3028 # find the fields to save
3029 my $query = "INSERT INTO deletedbiblio SET ";
3031 foreach my $temp ( keys %$data ) {
3032 $query .= "$temp = ?,";
3033 push( @bind, $data->{$temp} );
3036 # replace the last , by ",?)"
3038 my $bkup_sth = $dbh->prepare($query);
3039 $bkup_sth->execute(@bind);
3043 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3044 $del_sth->execute($biblionumber);
3051 =head2 _koha_delete_biblioitems
3055 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3057 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3059 C<$dbh> - the database handle
3060 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3066 # FIXME: add error handling
3068 sub _koha_delete_biblioitems {
3069 my ( $dbh, $biblioitemnumber ) = @_;
3071 # get all the data for this biblioitem
3073 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3074 $sth->execute($biblioitemnumber);
3076 if ( my $data = $sth->fetchrow_hashref ) {
3078 # save the record in deletedbiblioitems
3079 # find the fields to save
3080 my $query = "INSERT INTO deletedbiblioitems SET ";
3082 foreach my $temp ( keys %$data ) {
3083 $query .= "$temp = ?,";
3084 push( @bind, $data->{$temp} );
3087 # replace the last , by ",?)"
3089 my $bkup_sth = $dbh->prepare($query);
3090 $bkup_sth->execute(@bind);
3093 # delete the biblioitem
3095 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3096 $del_sth->execute($biblioitemnumber);
3103 =head1 UNEXPORTED FUNCTIONS
3105 =head2 ModBiblioMarc
3107 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3109 Add MARC data for a biblio to koha
3111 Function exported, but should NOT be used, unless you really know what you're doing
3117 # pass the MARC::Record to this function, and it will create the records in the marc field
3118 my ( $record, $biblionumber, $frameworkcode ) = @_;
3119 my $dbh = C4::Context->dbh;
3120 my @fields = $record->fields();
3121 if ( !$frameworkcode ) {
3122 $frameworkcode = "";
3125 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3126 $sth->execute( $frameworkcode, $biblionumber );
3128 my $encoding = C4::Context->preference("marcflavour");
3130 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3131 if ( $encoding eq "UNIMARC" ) {
3133 if ( length($record->subfield( 100, "a" )) == 35 ) {
3134 $string = $record->subfield( 100, "a" );
3135 my $f100 = $record->field(100);
3136 $record->delete_field($f100);
3139 $string = POSIX::strftime( "%Y%m%d", localtime );
3141 $string = sprintf( "%-*s", 35, $string );
3143 substr( $string, 22, 6, "frey50" );
3144 unless ( $record->subfield( 100, "a" ) ) {
3145 $record->insert_grouped_field(
3146 MARC::Field->new( 100, "", "", "a" => $string ) );
3150 if (C4::Context->preference("NoZebra")) {
3151 # only NoZebra indexing needs to have
3152 # the previous version of the record
3153 $oldRecord = GetMarcBiblio($biblionumber);
3157 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3158 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3161 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3162 return $biblionumber;
3165 =head2 z3950_extended_services
3167 z3950_extended_services($serviceType,$serviceOptions,$record);
3169 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.
3171 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3173 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3175 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3179 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3180 syntax => the record syntax (transfer syntax)
3181 databaseName = Database from connection object
3183 To set serviceOptions, call set_service_options($serviceType)
3185 C<$record> the record, if one is needed for the service type
3187 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3191 sub z3950_extended_services {
3192 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3194 # get our connection object
3195 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3197 # create a new package object
3198 my $Zpackage = $Zconn->package();
3201 $Zpackage->option( action => $action );
3203 if ( $serviceOptions->{'databaseName'} ) {
3204 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3206 if ( $serviceOptions->{'recordIdNumber'} ) {
3208 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3210 if ( $serviceOptions->{'recordIdOpaque'} ) {
3212 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3215 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3216 #if ($serviceType eq 'itemorder') {
3217 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3218 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3219 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3220 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3223 if ( $serviceOptions->{record} ) {
3224 $Zpackage->option( record => $serviceOptions->{record} );
3226 # can be xml or marc
3227 if ( $serviceOptions->{'syntax'} ) {
3228 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3232 # send the request, handle any exception encountered
3233 eval { $Zpackage->send($serviceType) };
3234 if ( $@ && $@->isa("ZOOM::Exception") ) {
3235 return "error: " . $@->code() . " " . $@->message() . "\n";
3238 # free up package resources
3239 $Zpackage->destroy();
3242 =head2 set_service_options
3244 my $serviceOptions = set_service_options($serviceType);
3246 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3248 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3252 sub set_service_options {
3253 my ($serviceType) = @_;
3256 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3257 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3259 if ( $serviceType eq 'commit' ) {
3263 if ( $serviceType eq 'create' ) {
3267 if ( $serviceType eq 'drop' ) {
3268 die "ERROR: 'drop' not currently supported (by Zebra)";
3270 return $serviceOptions;
3273 =head3 get_biblio_authorised_values
3275 find the types and values for all authorised values assigned to this biblio.
3279 MARC::Record of the bib
3281 returns: a hashref malling the authorised value to the value set for this biblionumber
3283 $authorised_values = {
3284 'Scent' => 'flowery',
3285 'Audience' => 'Young Adult',
3286 'itemtypes' => 'SER',
3289 Notes: forlibrarian should probably be passed in, and called something different.
3294 sub get_biblio_authorised_values {
3295 my $biblionumber = shift;
3298 my $forlibrarian = 1; # are we in staff or opac?
3299 my $frameworkcode = GetFrameworkCode( $biblionumber );
3301 my $authorised_values;
3303 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3304 or return $authorised_values;
3306 # assume that these entries in the authorised_value table are bibliolevel.
3307 # ones that start with 'item%' are item level.
3308 my $query = q(SELECT distinct authorised_value, kohafield
3309 FROM marc_subfield_structure
3310 WHERE authorised_value !=''
3311 AND (kohafield like 'biblio%'
3312 OR kohafield like '') );
3313 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3315 foreach my $tag ( keys( %$tagslib ) ) {
3316 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3317 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3318 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3319 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3320 if ( defined $record->field( $tag ) ) {
3321 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3322 if ( defined $this_subfield_value ) {
3323 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3330 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3331 return $authorised_values;
3341 Koha Developement team <info@koha.org>
3343 Paul POULAIN paul.poulain@free.fr
3345 Joshua Ferraro jmf@liblime.com