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 @EXPORT_OK);
45 @ISA = qw( Exporter );
63 &GetBiblioItemByBiblioNumber
64 &GetBiblioFromItemNumber
84 &GetAuthorisedValueDesc
88 &GetPublisherNameFromIsbn
100 # To delete something
105 # To link headings in a bib record
106 # to authority records.
108 &LinkBibHeadingsToAuthorities
112 # those functions are exported but should not be used
113 # they are usefull is few circumstances, so are exported.
114 # but don't use them unless you're a core developer ;-)
121 &TransformHtmlToMarc2
124 &PrepareItemrecordDisplay
131 C4::Biblio - cataloging management functions
135 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:
139 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
141 =item 2. as raw MARC in the Zebra index and storage engine
143 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
147 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
149 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.
153 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
155 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
159 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:
163 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
165 =item 2. _koha_* - low-level internal functions for managing the koha tables
167 =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.
169 =item 4. Zebra functions used to update the Zebra index
171 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
175 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 :
179 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
181 =item 2. add the biblionumber and biblioitemnumber into the MARC records
183 =item 3. save the marc record
187 When dealing with items, we must :
191 =item 1. save the item in items table, that gives us an itemnumber
193 =item 2. add the itemnumber to the item MARC field
195 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
197 When modifying a biblio or an item, the behaviour is quite similar.
201 =head1 EXPORTED FUNCTIONS
207 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
211 Exported function (core API) for adding a new biblio to koha.
213 The first argument is a C<MARC::Record> object containing the
214 bib to add, while the second argument is the desired MARC
217 This function also accepts a third, optional argument: a hashref
218 to additional options. The only defined option is C<defer_marc_save>,
219 which if present and mapped to a true value, causes C<AddBiblio>
220 to omit the call to save the MARC in C<bibilioitems.marc>
221 and C<biblioitems.marcxml> This option is provided B<only>
222 for the use of scripts such as C<bulkmarcimport.pl> that may need
223 to do some manipulation of the MARC record for item parsing before
224 saving it and which cannot afford the performance hit of saving
225 the MARC record twice. Consequently, do not use that option
226 unless you can guarantee that C<ModBiblioMarc> will be called.
231 my ( $record, $frameworkcode, $options ) = @_;
232 my $dbh = C4::Context->dbh;
233 # transform the data into koha-table style data
234 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
235 my ($biblionumber) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
236 $olddata->{'biblionumber'} = $biblionumber;
237 my ($biblioitemnumber) = _koha_add_biblioitem( $dbh, $olddata );
239 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
241 # update MARC subfield that stores biblioitems.cn_sort
242 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
245 ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $$options{defer_marc_save} ||= 0;
247 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
248 return ( $biblionumber, $biblioitemnumber );
255 ModBiblio( $record,$biblionumber,$frameworkcode);
259 Replace an existing bib record identified by C<$biblionumber>
260 with one supplied by the MARC::Record object C<$record>. The embedded
261 item, biblioitem, and biblionumber fields from the previous
262 version of the bib record replace any such fields of those tags that
263 are present in C<$record>. Consequently, ModBiblio() is not
264 to be used to try to modify item records.
266 C<$frameworkcode> specifies the MARC framework to use
267 when storing the modified bib record; among other things,
268 this controls how MARC fields get mapped to display columns
269 in the C<biblio> and C<biblioitems> tables, as well as
270 which fields are used to store embedded item, biblioitem,
271 and biblionumber data for indexing.
276 my ( $record, $biblionumber, $frameworkcode ) = @_;
277 if (C4::Context->preference("CataloguingLog")) {
278 my $newrecord = GetMarcBiblio($biblionumber);
279 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
282 my $dbh = C4::Context->dbh;
284 $frameworkcode = "" unless $frameworkcode;
286 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
287 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
288 my $oldRecord = GetMarcBiblio( $biblionumber );
290 # delete any item fields from incoming record to avoid
291 # duplication or incorrect data - use AddItem() or ModItem()
293 foreach my $field ($record->field($itemtag)) {
294 $record->delete_field($field);
297 # once all the items fields are removed, copy the old ones, in order to keep synchronize
298 $record->append_fields($oldRecord->field( $itemtag ));
300 # update biblionumber and biblioitemnumber in MARC
301 # FIXME - this is assuming a 1 to 1 relationship between
302 # biblios and biblioitems
303 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
304 $sth->execute($biblionumber);
305 my ($biblioitemnumber) = $sth->fetchrow;
307 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
309 # load the koha-table data object
310 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
312 # update MARC subfield that stores biblioitems.cn_sort
313 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
315 # update the MARC record (that now contains biblio and items) with the new record data
316 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
318 # modify the other koha tables
319 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
320 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
324 =head2 ModBiblioframework
326 ModBiblioframework($biblionumber,$frameworkcode);
327 Exported function to modify a biblio framework
331 sub ModBiblioframework {
332 my ( $biblionumber, $frameworkcode ) = @_;
333 my $dbh = C4::Context->dbh;
334 my $sth = $dbh->prepare(
335 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
337 $sth->execute($frameworkcode, $biblionumber);
345 my $error = &DelBiblio($dbh,$biblionumber);
346 Exported function (core API) for deleting a biblio in koha.
347 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
348 Also backs it up to deleted* tables
349 Checks to make sure there are not issues on any of the items
351 C<$error> : undef unless an error occurs
358 my ( $biblionumber ) = @_;
359 my $dbh = C4::Context->dbh;
360 my $error; # for error handling
362 # First make sure this biblio has no items attached
363 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
364 $sth->execute($biblionumber);
365 if (my $itemnumber = $sth->fetchrow){
366 # Fix this to use a status the template can understand
367 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
370 return $error if $error;
372 # We delete attached subscriptions
373 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
374 foreach my $subscription (@$subscriptions){
375 &C4::Serials::DelSubscription($subscription->{subscriptionid});
378 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
379 # for at least 2 reasons :
380 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
381 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
382 # 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)
384 if (C4::Context->preference("NoZebra")) {
385 # only NoZebra indexing needs to have
386 # the previous version of the record
387 $oldRecord = GetMarcBiblio($biblionumber);
389 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
391 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
394 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
395 $sth->execute($biblionumber);
396 while ( my $biblioitemnumber = $sth->fetchrow ) {
398 # delete this biblioitem
399 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
400 return $error if $error;
403 # delete biblio from Koha tables and save in deletedbiblio
404 # must do this *after* _koha_delete_biblioitems, otherwise
405 # delete cascade will prevent deletedbiblioitems rows
406 # from being generated by _koha_delete_biblioitems
407 $error = _koha_delete_biblio( $dbh, $biblionumber );
409 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
414 =head2 LinkBibHeadingsToAuthorities
418 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
422 Links bib headings to authority records by checking
423 each authority-controlled field in the C<MARC::Record>
424 object C<$marc>, looking for a matching authority record,
425 and setting the linking subfield $9 to the ID of that
428 If no matching authority exists, or if multiple
429 authorities match, no $9 will be added, and any
430 existing one inthe field will be deleted.
432 Returns the number of heading links changed in the
437 sub LinkBibHeadingsToAuthorities {
440 my $num_headings_changed = 0;
441 foreach my $field ($bib->fields()) {
442 my $heading = C4::Heading->new_from_bib_field($field);
443 next unless defined $heading;
446 my $current_link = $field->subfield('9');
448 # look for matching authorities
449 my $authorities = $heading->authorities();
451 # want only one exact match
452 if ($#{ $authorities } == 0) {
453 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
454 my $authid = $authority->field('001')->data();
455 next if defined $current_link and $current_link eq $authid;
457 $field->delete_subfield(code => '9') if defined $current_link;
458 $field->add_subfields('9', $authid);
459 $num_headings_changed++;
461 if (defined $current_link) {
462 $field->delete_subfield(code => '9');
463 $num_headings_changed++;
468 return $num_headings_changed;
471 =head2 GetRecordValue
475 my $values = GetRecordValue($field, $record, $frameworkcode);
479 Get MARC fields from a keyword defined in fieldmapping table.
484 my ($field, $record, $frameworkcode) = @_;
485 my $dbh = C4::Context->dbh;
487 my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?');
488 $sth->execute($frameworkcode, $field);
492 while(my $row = $sth->fetchrow_hashref){
493 foreach my $field ($record->field($row->{fieldcode})){
494 if( ($row->{subfieldcode} ne "" && $field->subfield($row->{subfieldcode}))){
495 foreach my $subfield ($field->subfield($row->{subfieldcode})){
496 push @result, { 'subfield' => $subfield };
499 }elsif($row->{subfieldcode} eq "") {
500 push @result, {'subfield' => $field->as_string()};
508 =head2 SetFieldMapping
512 SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
516 Set a Field to MARC mapping value, if it already exists we don't add a new one.
520 sub SetFieldMapping {
521 my ($framework, $field, $fieldcode, $subfieldcode) = @_;
522 my $dbh = C4::Context->dbh;
524 my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
525 $sth->execute($fieldcode, $subfieldcode, $framework, $field);
526 if(not $sth->fetchrow_hashref){
528 $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
530 $sth->execute($fieldcode, $subfieldcode, $framework, $field);
534 =head2 DeleteFieldMapping
538 DeleteFieldMapping($id);
542 Delete a field mapping from an $id.
546 sub DeleteFieldMapping{
548 my $dbh = C4::Context->dbh;
550 my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
554 =head2 GetFieldMapping
558 GetFieldMapping($frameworkcode);
562 Get all field mappings for a specified frameworkcode
566 sub GetFieldMapping {
567 my ($framework) = @_;
568 my $dbh = C4::Context->dbh;
570 my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
571 $sth->execute($framework);
574 while(my $row = $sth->fetchrow_hashref){
584 $data = &GetBiblioData($biblionumber);
585 Returns information about the book with the given biblionumber.
586 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
587 the C<biblio> and C<biblioitems> tables in the
589 In addition, C<$data-E<gt>{subject}> is the list of the book's
590 subjects, separated by C<" , "> (space, comma, space).
591 If there are multiple biblioitems with the given biblionumber, only
592 the first one is considered.
600 my $dbh = C4::Context->dbh;
602 # my $query = C4::Context->preference('item-level_itypes') ?
603 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
605 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
606 # WHERE biblio.biblionumber = ?
607 # AND biblioitems.biblionumber = biblio.biblionumber
610 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
612 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
613 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
614 WHERE biblio.biblionumber = ?
615 AND biblioitems.biblionumber = biblio.biblionumber ";
617 my $sth = $dbh->prepare($query);
618 $sth->execute($bibnum);
620 $data = $sth->fetchrow_hashref;
624 } # sub GetBiblioData
626 =head2 &GetBiblioItemData
630 $itemdata = &GetBiblioItemData($biblioitemnumber);
632 Looks up the biblioitem with the given biblioitemnumber. Returns a
633 reference-to-hash. The keys are the fields from the C<biblio>,
634 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
635 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
642 sub GetBiblioItemData {
643 my ($biblioitemnumber) = @_;
644 my $dbh = C4::Context->dbh;
645 my $query = "SELECT *,biblioitems.notes AS bnotes
646 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
647 unless(C4::Context->preference('item-level_itypes')) {
648 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
650 $query .= " WHERE biblioitemnumber = ? ";
651 my $sth = $dbh->prepare($query);
653 $sth->execute($biblioitemnumber);
654 $data = $sth->fetchrow_hashref;
657 } # sub &GetBiblioItemData
659 =head2 GetBiblioItemByBiblioNumber
663 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
669 sub GetBiblioItemByBiblioNumber {
670 my ($biblionumber) = @_;
671 my $dbh = C4::Context->dbh;
672 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
676 $sth->execute($biblionumber);
678 while ( my $data = $sth->fetchrow_hashref ) {
679 push @results, $data;
686 =head2 GetBiblioFromItemNumber
690 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
692 Looks up the item with the given itemnumber. if undef, try the barcode.
694 C<&itemnodata> returns a reference-to-hash whose keys are the fields
695 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
703 sub GetBiblioFromItemNumber {
704 my ( $itemnumber, $barcode ) = @_;
705 my $dbh = C4::Context->dbh;
708 $sth=$dbh->prepare( "SELECT * FROM items
709 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
710 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
711 WHERE items.itemnumber = ?") ;
712 $sth->execute($itemnumber);
714 $sth=$dbh->prepare( "SELECT * FROM items
715 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
716 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
717 WHERE items.barcode = ?") ;
718 $sth->execute($barcode);
720 my $data = $sth->fetchrow_hashref;
729 $isbd = &GetISBDView($biblionumber);
731 Return the ISBD view which can be included in opac and intranet
738 my ($biblionumber, $template) = @_;
739 my $record = GetMarcBiblio($biblionumber);
740 my $itemtype = &GetFrameworkCode($biblionumber);
741 my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
742 my $tagslib = &GetMarcStructure( 1, $itemtype );
744 my $ISBD = C4::Context->preference('ISBD');
745 if($template eq "opac"){
746 $ISBD = C4::Context->preference('OPACISBD');
753 foreach my $isbdfield ( split (/#/, $bloc) ) {
755 # $isbdfield= /(.?.?.?)/;
756 $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
757 my $fieldvalue = $1 || 0;
758 my $subfvalue = $2 || "";
760 my $analysestring = $4;
763 # warn "==> $1 / $2 / $3 / $4";
764 # my $fieldvalue=substr($isbdfield,0,3);
765 if ( $fieldvalue > 0 ) {
766 my $hasputtextbefore = 0;
767 my @fieldslist = $record->field($fieldvalue);
768 @fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
770 # warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
771 # warn "FV : $fieldvalue";
772 if ($subfvalue ne ""){
773 foreach my $field ( @fieldslist ) {
774 foreach my $subfield ($field->subfield($subfvalue)){
775 my $calculated = $analysestring;
776 my $tag = $field->tag();
781 GetAuthorisedValueDesc( $tag, $subfvalue,
782 $subfield, '', $tagslib );
783 my $tagsubf = $tag . $subfvalue;
785 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
787 # field builded, store the result
788 if ( $calculated && !$hasputtextbefore )
789 { # put textbefore if not done
790 $blocres .= $textbefore;
791 $hasputtextbefore = 1;
794 # remove punctuation at start
795 $calculated =~ s/^( |;|:|\.|-)*//g;
796 $blocres .= $calculated;
801 $blocres .= $textafter if $hasputtextbefore;
803 foreach my $field ( @fieldslist ) {
804 my $calculated = $analysestring;
805 my $tag = $field->tag();
809 my @subf = $field->subfields;
810 for my $i ( 0 .. $#subf ) {
811 my $valuecode = $subf[$i][1];
812 my $subfieldcode = $subf[$i][0];
814 GetAuthorisedValueDesc( $tag, $subf[$i][0],
815 $subf[$i][1], '', $tagslib );
816 my $tagsubf = $tag . $subfieldcode;
818 $calculated =~ s/\{\{$tagsubf\}\}/$valuecode/gx;
820 s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
823 # field builded, store the result
824 if ( $calculated && !$hasputtextbefore )
825 { # put textbefore if not done
826 $blocres .= $textbefore;
827 $hasputtextbefore = 1;
830 # remove punctuation at start
831 $calculated =~ s/^( |;|:|\.|-)*//g;
832 $blocres .= $calculated;
835 $blocres .= $textafter if $hasputtextbefore;
839 $blocres .= $isbdfield;
844 $res =~ s/\{(.*?)\}//g;
846 $res =~ s/\n/<br\/>/g;
858 ( $count, @results ) = &GetBiblio($biblionumber);
865 my ($biblionumber) = @_;
866 my $dbh = C4::Context->dbh;
867 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
870 $sth->execute($biblionumber);
871 while ( my $data = $sth->fetchrow_hashref ) {
872 $results[$count] = $data;
876 return ( $count, @results );
879 =head2 GetBiblioItemInfosOf
883 GetBiblioItemInfosOf(@biblioitemnumbers);
889 sub GetBiblioItemInfosOf {
890 my @biblioitemnumbers = @_;
893 SELECT biblioitemnumber,
897 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
899 return get_infos_of( $query, 'biblioitemnumber' );
902 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
904 =head2 GetMarcStructure
908 $res = GetMarcStructure($forlibrarian,$frameworkcode);
910 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
911 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
912 $frameworkcode : the framework code to read
918 # cache for results of GetMarcStructure -- needed
920 our $marc_structure_cache;
922 sub GetMarcStructure {
923 my ( $forlibrarian, $frameworkcode ) = @_;
924 my $dbh=C4::Context->dbh;
925 $frameworkcode = "" unless $frameworkcode;
927 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
928 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
931 my $sth = $dbh->prepare(
932 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
933 $sth->execute($frameworkcode);
934 my ($total) = $sth->fetchrow;
935 $frameworkcode = "" unless ( $total > 0 );
936 $sth = $dbh->prepare(
937 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
938 FROM marc_tag_structure
939 WHERE frameworkcode=?
942 $sth->execute($frameworkcode);
943 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
945 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
948 $res->{$tag}->{lib} =
949 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
950 $res->{$tag}->{tab} = "";
951 $res->{$tag}->{mandatory} = $mandatory;
952 $res->{$tag}->{repeatable} = $repeatable;
955 $sth = $dbh->prepare(
956 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
957 FROM marc_subfield_structure
958 WHERE frameworkcode=?
959 ORDER BY tagfield,tagsubfield
963 $sth->execute($frameworkcode);
966 my $authorised_value;
978 $tag, $subfield, $liblibrarian,
980 $mandatory, $repeatable, $authorised_value,
981 $authtypecode, $value_builder, $kohafield,
982 $seealso, $hidden, $isurl,
988 $res->{$tag}->{$subfield}->{lib} =
989 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
990 $res->{$tag}->{$subfield}->{tab} = $tab;
991 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
992 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
993 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
994 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
995 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
996 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
997 $res->{$tag}->{$subfield}->{seealso} = $seealso;
998 $res->{$tag}->{$subfield}->{hidden} = $hidden;
999 $res->{$tag}->{$subfield}->{isurl} = $isurl;
1000 $res->{$tag}->{$subfield}->{'link'} = $link;
1001 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
1004 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
1009 =head2 GetUsedMarcStructure
1011 the same function as GetMarcStructure except it just takes field
1012 in tab 0-9. (used field)
1014 my $results = GetUsedMarcStructure($frameworkcode);
1016 L<$results> is a ref to an array which each case containts a ref
1017 to a hash which each keys is the columns from marc_subfield_structure
1019 L<$frameworkcode> is the framework code.
1023 sub GetUsedMarcStructure($){
1024 my $frameworkcode = shift || '';
1027 FROM marc_subfield_structure
1029 AND frameworkcode = ?
1030 ORDER BY tagfield, tagsubfield
1032 my $sth = C4::Context->dbh->prepare($query);
1033 $sth->execute($frameworkcode);
1034 return $sth->fetchall_arrayref({});
1037 =head2 GetMarcFromKohaField
1041 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1042 Returns the MARC fields & subfields mapped to the koha field
1043 for the given frameworkcode
1049 sub GetMarcFromKohaField {
1050 my ( $kohafield, $frameworkcode ) = @_;
1051 return 0, 0 unless $kohafield and defined $frameworkcode;
1052 my $relations = C4::Context->marcfromkohafield;
1054 $relations->{$frameworkcode}->{$kohafield}->[0],
1055 $relations->{$frameworkcode}->{$kohafield}->[1]
1059 =head2 GetMarcBiblio
1063 my $record = GetMarcBiblio($biblionumber);
1067 Returns MARC::Record representing bib identified by
1068 C<$biblionumber>. If no bib exists, returns undef.
1069 The MARC record contains both biblio & item data.
1074 my $biblionumber = shift;
1075 my $dbh = C4::Context->dbh;
1077 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1078 $sth->execute($biblionumber);
1079 my $row = $sth->fetchrow_hashref;
1080 my $marcxml = StripNonXmlChars($row->{'marcxml'});
1081 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
1082 my $record = MARC::Record->new();
1084 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
1085 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
1086 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1097 my $marcxml = GetXmlBiblio($biblionumber);
1099 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1100 The XML contains both biblio & item datas
1107 my ( $biblionumber ) = @_;
1108 my $dbh = C4::Context->dbh;
1110 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1111 $sth->execute($biblionumber);
1112 my ($marcxml) = $sth->fetchrow;
1116 =head2 GetCOinSBiblio
1120 my $coins = GetCOinSBiblio($biblionumber);
1122 Returns the COinS(a span) which can be included in a biblio record
1128 sub GetCOinSBiblio {
1129 my ( $biblionumber ) = @_;
1130 my $record = GetMarcBiblio($biblionumber);
1132 if (defined $record){
1133 # get the coin format
1134 my $pos7 = substr $record->leader(), 7,1;
1135 my $pos6 = substr $record->leader(), 6,1;
1138 my ($aulast, $aufirst) = ('','');
1147 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
1152 'b' => 'manuscript',
1154 'd' => 'manuscript',
1158 'i' => 'audioRecording',
1159 'j' => 'audioRecording',
1162 'm' => 'computerProgram',
1167 'a' => 'journalArticle',
1171 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
1173 if( $genre eq 'book' ){
1174 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
1177 ##### We must transform mtx to a valable mtx and document type ####
1178 if( $genre eq 'book' ){
1180 }elsif( $genre eq 'journal' ){
1182 }elsif( $genre eq 'journalArticle' ){
1189 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
1192 $aulast = $record->subfield('700','a') || '';
1193 $aufirst = $record->subfield('700','b') || '';
1194 $oauthors = "&rft.au=$aufirst $aulast";
1196 if($record->field('200')){
1197 for my $au ($record->field('200')->subfield('g')){
1198 $oauthors .= "&rft.au=$au";
1201 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
1202 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
1203 $pubyear = $record->subfield('210','d') || '';
1204 $publisher = $record->subfield('210','c') || '';
1205 $isbn = $record->subfield('010','a') || '';
1206 $issn = $record->subfield('011','a') || '';
1208 # MARC21 need some improve
1211 $genre = "&rft.genre=book";
1214 if ($record->field('100')) {
1215 $oauthors .= "&rft.au=".$record->subfield('100','a');
1218 if($record->field('700')){
1219 for my $au ($record->field('700')->subfield('a')){
1220 $oauthors .= "&rft.au=$au";
1223 $title = "&rft.btitle=".$record->subfield('245','a');
1224 $subtitle = $record->subfield('245', 'b') || '';
1225 $title .= $subtitle;
1226 $pubyear = $record->subfield('260', 'c') || '';
1227 $publisher = $record->subfield('260', 'b') || '';
1228 $isbn = $record->subfield('020', 'a') || '';
1229 $issn = $record->subfield('022', 'a') || '';
1232 $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";
1233 $coins_value =~ s/\ /\+/g;
1234 #<!-- 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="
1236 return $coins_value;
1239 =head2 GetBiblioSummary
1243 $summary = GetBiblioSummary($marcrecord);
1245 Return the summary of a record.
1251 sub GetBiblioSummary {
1252 my $recorddata =shift @_;
1254 return unless $recorddata;
1255 my $marcflavour = C4::Context->preference("marcflavour");
1256 my $marc=MARC::Record::new_from_xml($recorddata,"utf-8",$marcflavour);
1257 return unless $marc;
1261 if($marcflavour eq "MARC21"){
1262 $str="<b>".$marc->subfield('245',"a")."</b>" if $marc->subfield('245','a');
1263 $str.= " <i>".$marc->subfield('245',"b")."</i> " if $marc->subfield('245','b');
1265 if ($marc->field('245')){
1267 foreach ($marc->field('100')->subfield("a")) {
1273 if ($marc->field('260')){
1275 $str.=$marc->subfield('260',"a")." " if $marc->subfield('260','a');
1276 $str.=" : ".$marc->subfield('260',"b")." " if $marc->subfield('260','b');
1277 $str.=", ".$marc->subfield('260',"c")." " if $marc->subfield('260','c');
1279 if ($marc->field('300')){
1281 $str.=$marc->subfield('300','a') if ($marc->subfield(300,'a'));
1282 $str.=" ; ".$marc->subfield('300','b') if $marc->subfield('300','b');
1283 $str.=" ; ".$marc->subfield('300','c') if $marc->subfield('300','c');
1284 $str.=" ; ".$marc->subfield('300','e') if $marc->subfield('300','e');
1286 foreach ($marc->field('500')){
1288 foreach ($_->subfield("a")){
1292 my $itemtypes=GetItemTypes();
1293 $str.=" - <u>".$itemtypes->{$marc->subfield('942','c')}->{'description'}."</u> ";
1297 $str = "<b>".$marc->subfield('200','a')."</b>" if $marc->subfield('200','a');
1298 $str.= " <i>".$marc->subfield('200','e')."</i> " if $marc->subfield('200','e');
1299 if ($marc->field('200')){
1301 foreach ($marc->field('200')->subfield("f")) {
1307 if ($marc->subfield('200','g')){
1309 foreach ($marc->field('200')->subfield("g")){
1315 if ($marc->field('461')){
1317 $str.= $marc->subfield('461','t') if $marc->subfield('461','t');
1318 $str.=", ".$marc->subfield('461','d') if $marc->subfield('461','d');
1319 $str.=", ".$marc->subfield('461','v') if $marc->subfield('461','v');
1320 $str.=", ".$marc->subfield('461','h') if $marc->subfield('461','h');
1321 $str.=" ; ".$marc->subfield('461','x') if $marc->subfield('461','x');
1324 if ($marc->field('210')){
1326 $str.=$marc->subfield('210',"a")." " if $marc->subfield('210','a');
1327 $str.=" : ".$marc->subfield('210',"c")." " if $marc->subfield('210','c');
1328 $str.=", ".$marc->subfield('210',"d")." " if $marc->subfield('210','d');
1331 if ($marc->field('215')){
1333 $str.=$marc->subfield('215','a') if ($marc->subfield(215,'a'));
1334 $str.=" ; ".$marc->subfield('215','d') if $marc->subfield('215','d');
1335 $str.=" ; ".$marc->subfield('215','c') if $marc->subfield('215','c');
1336 $str.=" ; ".$marc->subfield('215','e') if $marc->subfield('215','e');
1338 foreach ($marc->field('300')){
1340 foreach ($_->subfield("a")){
1345 my $itemtypes=GetItemTypes;
1346 if($itemtypes->{$marc->subfield('200','b')}){
1347 $str.=" - <u>".$itemtypes->{$marc->subfield('200','b')}->{'description'}."</u> ";
1354 =head2 GetAuthorisedValueDesc
1358 my $subfieldvalue =get_authorised_value_desc(
1359 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1360 Retrieve the complete description for a given authorised value.
1362 Now takes $category and $value pair too.
1363 my $auth_value_desc =GetAuthorisedValueDesc(
1364 '','', 'DVD' ,'','','CCODE');
1370 sub GetAuthorisedValueDesc {
1371 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1372 my $dbh = C4::Context->dbh;
1376 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1379 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1380 return C4::Branch::GetBranchName($value);
1384 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1385 return getitemtypeinfo($value)->{description};
1388 #---- "true" authorized value
1389 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1392 if ( $category ne "" ) {
1395 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1397 $sth->execute( $category, $value );
1398 my $data = $sth->fetchrow_hashref;
1399 return $data->{'lib'};
1402 return $value; # if nothing is found return the original value
1410 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1411 Get all notes from the MARC record and returns them in an array.
1412 The note are stored in differents places depending on MARC flavour
1419 my ( $record, $marcflavour ) = @_;
1421 if ( $marcflavour eq "MARC21" ) {
1424 else { # assume unimarc if not marc21
1431 foreach my $field ( $record->field($scope) ) {
1432 my $value = $field->as_string();
1433 $value =~ s/\n/<br \/>/g ;
1435 if ( $note ne "" ) {
1436 $marcnote = { marcnote => $note, };
1437 push @marcnotes, $marcnote;
1440 if ( $note ne $value ) {
1441 $note = $note . " " . $value;
1446 $marcnote = { marcnote => $note };
1447 push @marcnotes, $marcnote; #load last tag into array
1450 } # end GetMarcNotes
1452 =head2 GetMarcSubjects
1456 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1457 Get all subjects from the MARC record and returns them in an array.
1458 The subjects are stored in differents places depending on MARC flavour
1464 sub GetMarcSubjects {
1465 my ( $record, $marcflavour ) = @_;
1466 my ( $mintag, $maxtag );
1467 if ( $marcflavour eq "MARC21" ) {
1471 else { # assume unimarc if not marc21
1481 foreach my $field ( $record->field('6..' )) {
1482 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1484 my @subfields = $field->subfields();
1487 # if there is an authority link, build the link with an= subfield9
1488 my $subfield9 = $field->subfield('9');
1489 for my $subject_subfield (@subfields ) {
1490 # don't load unimarc subfields 3,4,5
1491 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1492 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1493 next if (($marcflavour eq "MARC21") and ($subject_subfield->[0] =~ /2/ ) );
1494 my $code = $subject_subfield->[0];
1495 my $value = $subject_subfield->[1];
1496 my $linkvalue = $value;
1497 $linkvalue =~ s/(\(|\))//g;
1498 my $operator = " and " unless $counter==0;
1500 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1502 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1504 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1506 my @this_link_loop = @link_loop;
1507 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1511 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1514 return \@marcsubjects;
1515 } #end getMARCsubjects
1517 =head2 GetMarcAuthors
1521 authors = GetMarcAuthors($record,$marcflavour);
1522 Get all authors from the MARC record and returns them in an array.
1523 The authors are stored in differents places depending on MARC flavour
1529 sub GetMarcAuthors {
1530 my ( $record, $marcflavour ) = @_;
1531 my ( $mintag, $maxtag );
1532 # tagslib useful for UNIMARC author reponsabilities
1533 my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be buggy on some setups, will be usually correct.
1534 if ( $marcflavour eq "MARC21" ) {
1538 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1547 foreach my $field ( $record->fields ) {
1548 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1551 my @subfields = $field->subfields();
1553 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1554 my $subfield9 = $field->subfield('9');
1555 for my $authors_subfield (@subfields) {
1556 # don't load unimarc subfields 3, 5
1557 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1558 my $subfieldcode = $authors_subfield->[0];
1559 my $value = $authors_subfield->[1];
1560 my $linkvalue = $value;
1561 $linkvalue =~ s/(\(|\))//g;
1562 my $operator = " and " unless $count_auth==0;
1563 # if we have an authority link, use that as the link, otherwise use standard searching
1565 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1568 # reset $linkvalue if UNIMARC author responsibility
1569 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1570 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1572 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1574 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1575 my @this_link_loop = @link_loop;
1576 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1577 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1580 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1582 return \@marcauthors;
1589 $marcurls = GetMarcUrls($record,$marcflavour);
1590 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1591 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1598 my ( $record, $marcflavour ) = @_;
1601 for my $field ( $record->field('856') ) {
1603 for my $note ( $field->subfield('z') ) {
1604 push @notes, { note => $note };
1606 my @urls = $field->subfield('u');
1607 foreach my $url (@urls) {
1609 if ( $marcflavour eq 'MARC21' ) {
1610 my $s3 = $field->subfield('3');
1611 my $link = $field->subfield('y');
1612 unless ( $url =~ /^\w+:/ ) {
1613 if ( $field->indicator(1) eq '7' ) {
1614 $url = $field->subfield('2') . "://" . $url;
1615 } elsif ( $field->indicator(1) eq '1' ) {
1616 $url = 'ftp://' . $url;
1618 # properly, this should be if ind1=4,
1619 # however we will assume http protocol since we're building a link.
1620 $url = 'http://' . $url;
1623 # TODO handle ind 2 (relationship)
1628 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1629 $marcurl->{'part'} = $s3 if ($link);
1630 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^table/i );
1632 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1633 $marcurl->{'MARCURL'} = $url;
1635 push @marcurls, $marcurl;
1641 =head2 GetMarcSeries
1645 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1646 Get all series from the MARC record and returns them in an array.
1647 The series are stored in differents places depending on MARC flavour
1654 my ($record, $marcflavour) = @_;
1655 my ($mintag, $maxtag);
1656 if ($marcflavour eq "MARC21") {
1659 } else { # assume unimarc if not marc21
1669 foreach my $field ($record->field('440'), $record->field('490')) {
1671 #my $value = $field->subfield('a');
1672 #$marcsubjct = {MARCSUBJCT => $value,};
1673 my @subfields = $field->subfields();
1674 #warn "subfields:".join " ", @$subfields;
1677 for my $series_subfield (@subfields) {
1679 undef $volume_number;
1680 # see if this is an instance of a volume
1681 if ($series_subfield->[0] eq 'v') {
1685 my $code = $series_subfield->[0];
1686 my $value = $series_subfield->[1];
1687 my $linkvalue = $value;
1688 $linkvalue =~ s/(\(|\))//g;
1689 my $operator = " and " unless $counter==0;
1690 push @link_loop, {link => $linkvalue, operator => $operator };
1691 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1692 if ($volume_number) {
1693 push @subfields_loop, {volumenum => $value};
1696 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1700 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1701 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1702 #push @marcsubjcts, $marcsubjct;
1706 my $marcseriessarray=\@marcseries;
1707 return $marcseriessarray;
1708 } #end getMARCseriess
1710 =head2 GetFrameworkCode
1714 $frameworkcode = GetFrameworkCode( $biblionumber )
1720 sub GetFrameworkCode {
1721 my ( $biblionumber ) = @_;
1722 my $dbh = C4::Context->dbh;
1723 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1724 $sth->execute($biblionumber);
1725 my ($frameworkcode) = $sth->fetchrow;
1726 return $frameworkcode;
1729 =head2 GetPublisherNameFromIsbn
1731 $name = GetPublishercodeFromIsbn($isbn);
1738 sub GetPublisherNameFromIsbn($){
1740 $isbn =~ s/[- _]//g;
1742 my @codes = (split '-', DisplayISBN($isbn));
1743 my $code = $codes[0].$codes[1].$codes[2];
1744 my $dbh = C4::Context->dbh;
1746 SELECT distinct publishercode
1749 AND publishercode IS NOT NULL
1752 my $sth = $dbh->prepare($query);
1753 $sth->execute("$code%");
1754 my $name = $sth->fetchrow;
1755 return $name if length $name;
1759 =head2 TransformKohaToMarc
1763 $record = TransformKohaToMarc( $hash )
1764 This function builds partial MARC::Record from a hash
1765 Hash entries can be from biblio or biblioitems.
1766 This function is called in acquisition module, to create a basic catalogue entry from user entry
1772 sub TransformKohaToMarc {
1774 my $sth = C4::Context->dbh->prepare(
1775 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1777 my $record = MARC::Record->new();
1778 SetMarcUnicodeFlag($record, C4::Context->preference("marcflavour"));
1779 foreach (keys %{$hash}) {
1780 &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
1785 =head2 TransformKohaToMarcOneField
1789 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1795 sub TransformKohaToMarcOneField {
1796 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1797 $frameworkcode='' unless $frameworkcode;
1801 if ( !defined $sth ) {
1802 my $dbh = C4::Context->dbh;
1803 $sth = $dbh->prepare(
1804 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1807 $sth->execute( $frameworkcode, $kohafieldname );
1808 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1809 my @values = split(/\s?\|\s?/, $value, -1);
1811 foreach my $itemvalue (@values){
1812 my $tag = $record->field($tagfield);
1814 $tag->add_subfields( $tagsubfield => $itemvalue );
1815 $record->delete_field($tag);
1816 $record->insert_fields_ordered($tag);
1819 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
1826 =head2 TransformHtmlToXml
1830 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1832 $auth_type contains :
1833 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1834 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1835 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1841 sub TransformHtmlToXml {
1842 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1843 my $xml = MARC::File::XML::header('UTF-8');
1844 $xml .= "<record>\n";
1845 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1846 MARC::File::XML->default_record_format($auth_type);
1847 # in UNIMARC, field 100 contains the encoding
1848 # check that there is one, otherwise the
1849 # MARC::Record->new_from_xml will fail (and Koha will die)
1850 my $unimarc_and_100_exist=0;
1851 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1856 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1857 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1858 # if we have a 100 field and it's values are not correct, skip them.
1859 # if we don't have any valid 100 field, we will create a default one at the end
1860 my $enc = substr( @$values[$i], 26, 2 );
1861 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1862 $unimarc_and_100_exist=1;
1867 @$values[$i] =~ s/&/&/g;
1868 @$values[$i] =~ s/</</g;
1869 @$values[$i] =~ s/>/>/g;
1870 @$values[$i] =~ s/"/"/g;
1871 @$values[$i] =~ s/'/'/g;
1872 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1873 # utf8::decode( @$values[$i] );
1875 if ( ( @$tags[$i] ne $prevtag ) ) {
1876 $j++ unless ( @$tags[$i] eq "" );
1877 my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
1878 my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
1879 my $ind1 = _default_ind_to_space($indicator1);
1881 if ( @$indicator[$j] ) {
1882 $ind2 = _default_ind_to_space($indicator2);
1885 warn "Indicator in @$tags[$i] is empty";
1889 $xml .= "</datafield>\n";
1890 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1891 && ( @$values[$i] ne "" ) )
1893 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1894 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1902 if ( @$values[$i] ne "" ) {
1905 if ( @$tags[$i] eq "000" ) {
1906 $xml .= "<leader>@$values[$i]</leader>\n";
1909 # rest of the fixed fields
1911 elsif ( @$tags[$i] < 10 ) {
1912 $xml .= "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1916 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1917 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1923 else { # @$tags[$i] eq $prevtag
1924 my $indicator1=eval{substr( @$indicator[$j], 0, 1 )};
1925 my $indicator2=eval{substr( @$indicator[$j], 1, 1 )};
1926 my $ind1 = _default_ind_to_space($indicator1);
1928 if ( @$indicator[$j] ) {
1929 $ind2 = _default_ind_to_space($indicator2);
1932 warn "Indicator in @$tags[$i] is empty";
1935 if ( @$values[$i] eq "" ) {
1939 $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1942 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1945 $prevtag = @$tags[$i];
1947 $xml .= "</datafield>\n" if @$tags > 0;
1948 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1949 # warn "SETTING 100 for $auth_type";
1950 my $string = strftime( "%Y%m%d", localtime(time) );
1951 # set 50 to position 26 is biblios, 13 if authorities
1953 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1954 $string = sprintf( "%-*s", 35, $string );
1955 substr( $string, $pos , 6, "50" );
1956 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1957 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1958 $xml .= "</datafield>\n";
1960 $xml .= "</record>\n";
1961 $xml .= MARC::File::XML::footer();
1965 =head2 _default_ind_to_space
1967 Passed what should be an indicator returns a space
1968 if its undefined or zero length
1972 sub _default_ind_to_space {
1974 if (!defined $s || $s eq q{}) {
1980 =head2 TransformHtmlToMarc
1982 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1983 L<$params> is a ref to an array as below:
1985 'tag_010_indicator1_531951' ,
1986 'tag_010_indicator2_531951' ,
1987 'tag_010_code_a_531951_145735' ,
1988 'tag_010_subfield_a_531951_145735' ,
1989 'tag_200_indicator1_873510' ,
1990 'tag_200_indicator2_873510' ,
1991 'tag_200_code_a_873510_673465' ,
1992 'tag_200_subfield_a_873510_673465' ,
1993 'tag_200_code_b_873510_704318' ,
1994 'tag_200_subfield_b_873510_704318' ,
1995 'tag_200_code_e_873510_280822' ,
1996 'tag_200_subfield_e_873510_280822' ,
1997 'tag_200_code_f_873510_110730' ,
1998 'tag_200_subfield_f_873510_110730' ,
2000 L<$cgi> is the CGI object which containts the value.
2001 L<$record> is the MARC::Record object.
2005 sub TransformHtmlToMarc {
2009 # explicitly turn on the UTF-8 flag for all
2010 # 'tag_' parameters to avoid incorrect character
2011 # conversion later on
2012 my $cgi_params = $cgi->Vars;
2013 foreach my $param_name (keys %$cgi_params) {
2014 if ($param_name =~ /^tag_/) {
2015 my $param_value = $cgi_params->{$param_name};
2016 if (utf8::decode($param_value)) {
2017 $cgi_params->{$param_name} = $param_value;
2019 # FIXME - need to do something if string is not valid UTF-8
2023 # creating a new record
2024 my $record = MARC::Record->new();
2027 while ($params->[$i]){ # browse all CGI params
2028 my $param = $params->[$i];
2030 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
2031 if ($param eq 'biblionumber') {
2032 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
2033 &GetMarcFromKohaField( "biblio.biblionumber", '' );
2034 if ($biblionumbertagfield < 10) {
2035 $newfield = MARC::Field->new(
2036 $biblionumbertagfield,
2037 $cgi->param($param),
2040 $newfield = MARC::Field->new(
2041 $biblionumbertagfield,
2044 "$biblionumbertagsubfield" => $cgi->param($param),
2047 push @fields,$newfield if($newfield);
2049 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
2052 my $ind1 = _default_ind_to_space(substr($cgi->param($param), 0, 1));
2053 my $ind2 = _default_ind_to_space(substr($cgi->param($params->[$i+1]), 0, 1));
2057 if($tag < 10){ # no code for theses fields
2058 # in MARC editor, 000 contains the leader.
2059 if ($tag eq '000' ) {
2060 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
2061 # between 001 and 009 (included)
2062 } elsif ($cgi->param($params->[$j+1]) ne '') {
2063 $newfield = MARC::Field->new(
2065 $cgi->param($params->[$j+1]),
2068 # > 009, deal with subfields
2070 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
2071 my $inner_param = $params->[$j];
2073 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
2074 $newfield->add_subfields(
2075 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
2079 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
2080 $newfield = MARC::Field->new(
2084 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
2091 push @fields,$newfield if($newfield);
2096 $record->append_fields(@fields);
2100 # cache inverted MARC field map
2101 our $inverted_field_map;
2103 =head2 TransformMarcToKoha
2107 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2111 Extract data from a MARC bib record into a hashref representing
2112 Koha biblio, biblioitems, and items fields.
2115 sub TransformMarcToKoha {
2116 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
2119 $limit_table=$limit_table||0;
2120 $frameworkcode = '' unless defined $frameworkcode;
2122 unless (defined $inverted_field_map) {
2123 $inverted_field_map = _get_inverted_marc_field_map();
2127 if ( defined $limit_table && $limit_table eq 'items') {
2128 $tables{'items'} = 1;
2130 $tables{'items'} = 1;
2131 $tables{'biblio'} = 1;
2132 $tables{'biblioitems'} = 1;
2135 # traverse through record
2136 MARCFIELD: foreach my $field ($record->fields()) {
2137 my $tag = $field->tag();
2138 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2139 if ($field->is_control_field()) {
2140 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
2141 ENTRY: foreach my $entry (@{ $kohafields }) {
2142 my ($subfield, $table, $column) = @{ $entry };
2143 next ENTRY unless exists $tables{$table};
2144 my $key = _disambiguate($table, $column);
2145 if ($result->{$key}) {
2146 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
2147 $result->{$key} .= " | " . $field->data();
2150 $result->{$key} = $field->data();
2154 # deal with subfields
2155 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
2156 my $code = $sf->[0];
2157 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
2158 my $value = $sf->[1];
2159 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
2160 my ($table, $column) = @{ $entry };
2161 next SFENTRY unless exists $tables{$table};
2162 my $key = _disambiguate($table, $column);
2163 if ($result->{$key}) {
2164 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
2165 $result->{$key} .= " | " . $value;
2168 $result->{$key} = $value;
2175 # modify copyrightdate to keep only the 1st year found
2176 if (exists $result->{'copyrightdate'}) {
2177 my $temp = $result->{'copyrightdate'};
2178 $temp =~ m/c(\d\d\d\d)/;
2179 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2180 $result->{'copyrightdate'} = $1;
2182 else { # if no cYYYY, get the 1st date.
2183 $temp =~ m/(\d\d\d\d)/;
2184 $result->{'copyrightdate'} = $1;
2188 # modify publicationyear to keep only the 1st year found
2189 if (exists $result->{'publicationyear'}) {
2190 my $temp = $result->{'publicationyear'};
2191 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
2192 $result->{'publicationyear'} = $1;
2194 else { # if no cYYYY, get the 1st date.
2195 $temp =~ m/(\d\d\d\d)/;
2196 $result->{'publicationyear'} = $1;
2203 sub _get_inverted_marc_field_map {
2205 my $relations = C4::Context->marcfromkohafield;
2207 foreach my $frameworkcode (keys %{ $relations }) {
2208 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
2209 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
2210 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
2211 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2212 my ($table, $column) = split /[.]/, $kohafield, 2;
2213 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2214 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2220 =head2 _disambiguate
2224 $newkey = _disambiguate($table, $field);
2226 This is a temporary hack to distinguish between the
2227 following sets of columns when using TransformMarcToKoha.
2229 items.cn_source & biblioitems.cn_source
2230 items.cn_sort & biblioitems.cn_sort
2232 Columns that are currently NOT distinguished (FIXME
2233 due to lack of time to fully test) are:
2235 biblio.notes and biblioitems.notes
2240 FIXME - this is necessary because prefixing each column
2241 name with the table name would require changing lots
2242 of code and templates, and exposing more of the DB
2243 structure than is good to the UI templates, particularly
2244 since biblio and bibloitems may well merge in a future
2245 version. In the future, it would also be good to
2246 separate DB access and UI presentation field names
2253 sub CountItemsIssued {
2254 my ( $biblionumber ) = @_;
2255 my $dbh = C4::Context->dbh;
2256 my $sth = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2257 $sth->execute( $biblionumber );
2258 my $row = $sth->fetchrow_hashref();
2259 return $row->{'issuedCount'};
2263 my ($table, $column) = @_;
2264 if ($column eq "cn_sort" or $column eq "cn_source") {
2265 return $table . '.' . $column;
2272 =head2 get_koha_field_from_marc
2276 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2278 Internal function to map data from the MARC record to a specific non-MARC field.
2279 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2285 sub get_koha_field_from_marc {
2286 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
2287 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
2289 foreach my $field ( $record->field($tagfield) ) {
2290 if ( $field->tag() < 10 ) {
2292 $kohafield .= " | " . $field->data();
2295 $kohafield = $field->data();
2299 if ( $field->subfields ) {
2300 my @subfields = $field->subfields();
2301 foreach my $subfieldcount ( 0 .. $#subfields ) {
2302 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2305 " | " . $subfields[$subfieldcount][1];
2309 $subfields[$subfieldcount][1];
2320 =head2 TransformMarcToKohaOneField
2324 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2330 sub TransformMarcToKohaOneField {
2332 # FIXME ? if a field has a repeatable subfield that is used in old-db,
2333 # only the 1st will be retrieved...
2334 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2336 my ( $tagfield, $subfield ) =
2337 GetMarcFromKohaField( $kohatable . "." . $kohafield,
2339 foreach my $field ( $record->field($tagfield) ) {
2340 if ( $field->tag() < 10 ) {
2341 if ( $result->{$kohafield} ) {
2342 $result->{$kohafield} .= " | " . $field->data();
2345 $result->{$kohafield} = $field->data();
2349 if ( $field->subfields ) {
2350 my @subfields = $field->subfields();
2351 foreach my $subfieldcount ( 0 .. $#subfields ) {
2352 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2353 if ( $result->{$kohafield} ) {
2354 $result->{$kohafield} .=
2355 " | " . $subfields[$subfieldcount][1];
2358 $result->{$kohafield} =
2359 $subfields[$subfieldcount][1];
2369 =head1 OTHER FUNCTIONS
2372 =head2 PrepareItemrecordDisplay
2376 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
2378 Returns a hash with all the fields for Display a given item data in a template
2384 sub PrepareItemrecordDisplay {
2386 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2388 my $dbh = C4::Context->dbh;
2389 my $today_iso = C4::Dates->today('iso');
2390 my $frameworkcode = &GetFrameworkCode( $bibnum );
2391 my ( $itemtagfield, $itemtagsubfield ) =
2392 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2393 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2394 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2395 # FIXME : I'd rather have GetMarcBiblio called out of this.
2396 # Since it gets the whole Biblio record for each item
2397 my $marcrecord = GetMarcBiblio( $bibnum) if ($bibnum);
2399 my $authorised_values_sth =
2401 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2403 foreach my $tag ( sort keys %{$tagslib} ) {
2404 my $previous_tag = '';
2406 # loop through each subfield
2408 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2409 next if ( subfield_is_koha_internal_p($subfield) );
2410 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2412 $subfield_data{tag} = $tag;
2413 $subfield_data{subfield} = $subfield;
2414 $subfield_data{countsubfield} = $cntsubf++;
2415 $subfield_data{kohafield} =
2416 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2418 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2419 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2420 $subfield_data{mandatory} =
2421 $tagslib->{$tag}->{$subfield}->{mandatory};
2422 $subfield_data{repeatable} =
2423 $tagslib->{$tag}->{$subfield}->{repeatable};
2424 $subfield_data{hidden} = "display:none"
2425 if $tagslib->{$tag}->{$subfield}->{hidden};
2428 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord );
2431 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
2432 $value ||= $defaultvalues->{$tagslib->{$tag}->{$subfield}->{'kohafield'}};
2433 # get today date & replace YYYY, MM, DD if provided in the default value
2434 my ( $year, $month, $day ) = split ',', $today_iso; # FIXME: iso dates don't have commas!
2435 $value =~ s/YYYY/$year/g;
2436 $value =~ s/MM/$month/g;
2437 $value =~ s/DD/$day/g;
2439 $value =~ s/"/"/g;
2441 # search for itemcallnumber if applicable
2442 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2443 'items.itemcallnumber'
2444 && C4::Context->preference('itemcallnumber') )
2447 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2449 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2450 my $temp = $marcrecord->field($CNtag) if ($marcrecord);
2452 $value = $temp->subfield($CNsubfield);
2455 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2456 'items.itemcallnumber'
2457 && $defaultvalues->{'callnumber'} )
2459 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2461 $value = $defaultvalues->{'callnumber'};
2464 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2465 'items.holdingbranch' ||
2466 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2468 && $defaultvalues->{'branchcode'} )
2470 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2472 $value = $defaultvalues->{branchcode};
2475 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2476 my @authorised_values;
2479 # builds list, depending on authorised value...
2481 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2484 if ( ( C4::Context->preference("IndependantBranches") )
2485 && ( C4::Context->userenv->{flags} != 1 ) )
2489 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2491 $sth->execute( C4::Context->userenv->{branch} );
2492 push @authorised_values, ""
2494 $tagslib->{$tag}->{$subfield}->{mandatory} );
2495 while ( my ( $branchcode, $branchname ) =
2496 $sth->fetchrow_array )
2498 push @authorised_values, $branchcode;
2499 $authorised_lib{$branchcode} = $branchname;
2505 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2508 push @authorised_values, ""
2510 $tagslib->{$tag}->{$subfield}->{mandatory} );
2511 while ( my ( $branchcode, $branchname ) =
2512 $sth->fetchrow_array )
2514 push @authorised_values, $branchcode;
2515 $authorised_lib{$branchcode} = $branchname;
2521 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2526 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2529 push @authorised_values, ""
2530 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2531 while ( my ( $itemtype, $description ) =
2532 $sth->fetchrow_array )
2534 push @authorised_values, $itemtype;
2535 $authorised_lib{$itemtype} = $description;
2538 #---- "true" authorised value
2541 $authorised_values_sth->execute(
2542 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2543 push @authorised_values, ""
2544 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2545 while ( my ( $value, $lib ) =
2546 $authorised_values_sth->fetchrow_array )
2548 push @authorised_values, $value;
2549 $authorised_lib{$value} = $lib;
2552 $subfield_data{marc_value} = CGI::scrolling_list(
2553 -name => 'field_value',
2554 -values => \@authorised_values,
2555 -default => "$value",
2556 -labels => \%authorised_lib,
2563 $subfield_data{marc_value} =
2564 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2566 push( @loop_data, \%subfield_data );
2570 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2571 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2573 'itemtagfield' => $itemtagfield,
2574 'itemtagsubfield' => $itemtagsubfield,
2575 'itemnumber' => $itemnumber,
2576 'iteminformation' => \@loop_data
2582 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2584 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2585 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2586 # =head2 ModZebrafiles
2588 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2592 # sub ModZebrafiles {
2594 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2598 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2599 # unless ( opendir( DIR, "$zebradir" ) ) {
2600 # warn "$zebradir not found";
2604 # my $filename = $zebradir . $biblionumber;
2607 # open( OUTPUT, ">", $filename . ".xml" );
2608 # print OUTPUT $record;
2617 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2619 $biblionumber is the biblionumber we want to index
2620 $op is specialUpdate or delete, and is used to know what we want to do
2621 $server is the server that we want to update
2622 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2623 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2625 $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.
2632 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2633 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2634 my $dbh=C4::Context->dbh;
2636 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2638 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2639 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2641 if (C4::Context->preference("NoZebra")) {
2642 # lock the nozebra table : we will read index lines, update them in Perl process
2643 # and write everything in 1 transaction.
2644 # lock the table to avoid someone else overwriting what we are doing
2645 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2646 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2647 if ($op eq 'specialUpdate') {
2648 # OK, we have to add or update the record
2649 # 1st delete (virtually, in indexes), if record actually exists
2651 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2653 # ... add the record
2654 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2656 # it's a deletion, delete the record...
2657 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2658 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2660 # ok, now update the database...
2661 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2662 foreach my $key (keys %result) {
2663 foreach my $index (keys %{$result{$key}}) {
2664 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2667 $dbh->do('UNLOCK TABLES');
2670 # we use zebra, just fill zebraqueue table
2672 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2674 AND biblio_auth_number = ?
2677 my $check_sth = $dbh->prepare_cached($check_sql);
2678 $check_sth->execute($server, $biblionumber, $op);
2679 my ($count) = $check_sth->fetchrow_array;
2680 $check_sth->finish();
2682 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2683 $sth->execute($biblionumber,$server,$op);
2689 =head2 GetNoZebraIndexes
2691 %indexes = GetNoZebraIndexes;
2693 return the data from NoZebraIndexes syspref.
2697 sub GetNoZebraIndexes {
2698 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2700 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2701 $line =~ /(.*)=>(.*)/;
2702 my $index = $1; # initial ' or " is removed afterwards
2704 $index =~ s/'|"|\s//g;
2705 $fields =~ s/'|"|\s//g;
2706 $indexes{$index}=$fields;
2711 =head1 INTERNAL FUNCTIONS
2713 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2715 function to delete a biblio in NoZebra indexes
2716 This function does NOT delete anything in database : it reads all the indexes entries
2717 that have to be deleted & delete them in the hash
2718 The SQL part is done either :
2719 - after the Add if we are modifying a biblio (delete + add again)
2720 - immediatly after this sub if we are doing a true deletion.
2721 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2726 sub _DelBiblioNoZebra {
2727 my ($biblionumber, $record, $server)=@_;
2730 my $dbh = C4::Context->dbh;
2734 if ($server eq 'biblioserver') {
2735 %index=GetNoZebraIndexes;
2736 # get title of the record (to store the 10 first letters with the index)
2737 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2738 $title = lc($record->subfield($titletag,$titlesubfield));
2740 # for authorities, the "title" is the $a mainentry
2741 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2742 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2743 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2744 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2745 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2746 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2747 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2751 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2752 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2753 # limit to 10 char, should be enough, and limit the DB size
2754 $title = substr($title,0,10);
2756 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2757 foreach my $field ($record->fields()) {
2758 #parse each subfield
2759 next if $field->tag <10;
2760 foreach my $subfield ($field->subfields()) {
2761 my $tag = $field->tag();
2762 my $subfieldcode = $subfield->[0];
2764 # check each index to see if the subfield is stored somewhere
2765 # otherwise, store it in __RAW__ index
2766 foreach my $key (keys %index) {
2767 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2768 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2770 my $line= lc $subfield->[1];
2771 # remove meaningless value in the field...
2772 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2773 # ... and split in words
2774 foreach (split / /,$line) {
2775 next unless $_; # skip empty values (multiple spaces)
2776 # if the entry is already here, do nothing, the biblionumber has already be removed
2777 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2778 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2779 $sth2->execute($server,$key,$_);
2780 my $existing_biblionumbers = $sth2->fetchrow;
2782 if ($existing_biblionumbers) {
2783 # warn " existing for $key $_: $existing_biblionumbers";
2784 $result{$key}->{$_} =$existing_biblionumbers;
2785 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2791 # the subfield is not indexed, store it in __RAW__ index anyway
2793 my $line= lc $subfield->[1];
2794 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2795 # ... and split in words
2796 foreach (split / /,$line) {
2797 next unless $_; # skip empty values (multiple spaces)
2798 # if the entry is already here, do nothing, the biblionumber has already be removed
2799 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2800 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2801 $sth2->execute($server,'__RAW__',$_);
2802 my $existing_biblionumbers = $sth2->fetchrow;
2804 if ($existing_biblionumbers) {
2805 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2806 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2816 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2818 function to add a biblio in NoZebra indexes
2822 sub _AddBiblioNoZebra {
2823 my ($biblionumber, $record, $server, %result)=@_;
2824 my $dbh = C4::Context->dbh;
2828 if ($server eq 'biblioserver') {
2829 %index=GetNoZebraIndexes;
2830 # get title of the record (to store the 10 first letters with the index)
2831 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2832 $title = lc($record->subfield($titletag,$titlesubfield));
2834 # warn "server : $server";
2835 # for authorities, the "title" is the $a mainentry
2836 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2837 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2838 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2839 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2840 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2841 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2842 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2845 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2846 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2847 # limit to 10 char, should be enough, and limit the DB size
2848 $title = substr($title,0,10);
2850 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2851 foreach my $field ($record->fields()) {
2852 #parse each subfield
2853 ###FIXME: impossible to index a 001-009 value with NoZebra
2854 next if $field->tag <10;
2855 foreach my $subfield ($field->subfields()) {
2856 my $tag = $field->tag();
2857 my $subfieldcode = $subfield->[0];
2859 # warn "INDEXING :".$subfield->[1];
2860 # check each index to see if the subfield is stored somewhere
2861 # otherwise, store it in __RAW__ index
2862 foreach my $key (keys %index) {
2863 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2864 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2866 my $line= lc $subfield->[1];
2867 # remove meaningless value in the field...
2868 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2869 # ... and split in words
2870 foreach (split / /,$line) {
2871 next unless $_; # skip empty values (multiple spaces)
2872 # if the entry is already here, improve weight
2873 # warn "managing $_";
2874 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2875 my $weight = $1 + 1;
2876 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2877 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2879 # get the value if it exist in the nozebra table, otherwise, create it
2880 $sth2->execute($server,$key,$_);
2881 my $existing_biblionumbers = $sth2->fetchrow;
2883 if ($existing_biblionumbers) {
2884 $result{$key}->{"$_"} =$existing_biblionumbers;
2885 my $weight = defined $1 ? $1 + 1 : 1;
2886 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2887 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2888 # create a new ligne for this entry
2890 # warn "INSERT : $server / $key / $_";
2891 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2892 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2898 # the subfield is not indexed, store it in __RAW__ index anyway
2900 my $line= lc $subfield->[1];
2901 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2902 # ... and split in words
2903 foreach (split / /,$line) {
2904 next unless $_; # skip empty values (multiple spaces)
2905 # if the entry is already here, improve weight
2906 my $tmpstr = $result{'__RAW__'}->{"$_"} || "";
2907 if ($tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2909 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2910 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2912 # get the value if it exist in the nozebra table, otherwise, create it
2913 $sth2->execute($server,'__RAW__',$_);
2914 my $existing_biblionumbers = $sth2->fetchrow;
2916 if ($existing_biblionumbers) {
2917 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2918 my $weight = ($1 ? $1 : 0) + 1;
2919 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2920 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2921 # create a new ligne for this entry
2923 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2924 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2939 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2941 Find the given $subfield in the given $tag in the given
2942 MARC::Record $record. If the subfield is found, returns
2943 the (indicators, value) pair; otherwise, (undef, undef) is
2947 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2948 I suggest we export it from this module.
2955 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2958 if ( $tagfield < 10 ) {
2959 if ( $record->field($tagfield) ) {
2960 push @result, $record->field($tagfield)->data();
2967 foreach my $field ( $record->field($tagfield) ) {
2968 my @subfields = $field->subfields();
2969 foreach my $subfield (@subfields) {
2970 if ( @$subfield[0] eq $insubfield ) {
2971 push @result, @$subfield[1];
2972 $indicator = $field->indicator(1) . $field->indicator(2);
2977 return ( $indicator, @result );
2980 =head2 _koha_marc_update_bib_ids
2984 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2986 Internal function to add or update biblionumber and biblioitemnumber to
2993 sub _koha_marc_update_bib_ids {
2994 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2996 # we must add bibnum and bibitemnum in MARC::Record...
2997 # we build the new field with biblionumber and biblioitemnumber
2998 # we drop the original field
2999 # we add the new builded field.
3000 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
3001 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
3003 if ($biblio_tag != $biblioitem_tag) {
3004 # biblionumber & biblioitemnumber are in different fields
3006 # deal with biblionumber
3007 my ($new_field, $old_field);
3008 if ($biblio_tag < 10) {
3009 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
3012 MARC::Field->new( $biblio_tag, '', '',
3013 "$biblio_subfield" => $biblionumber );
3016 # drop old field and create new one...
3017 $old_field = $record->field($biblio_tag);
3018 $record->delete_field($old_field) if $old_field;
3019 $record->append_fields($new_field);
3021 # deal with biblioitemnumber
3022 if ($biblioitem_tag < 10) {
3023 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
3026 MARC::Field->new( $biblioitem_tag, '', '',
3027 "$biblioitem_subfield" => $biblioitemnumber, );
3029 # drop old field and create new one...
3030 $old_field = $record->field($biblioitem_tag);
3031 $record->delete_field($old_field) if $old_field;
3032 $record->insert_fields_ordered($new_field);
3035 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
3036 my $new_field = MARC::Field->new(
3037 $biblio_tag, '', '',
3038 "$biblio_subfield" => $biblionumber,
3039 "$biblioitem_subfield" => $biblioitemnumber
3042 # drop old field and create new one...
3043 my $old_field = $record->field($biblio_tag);
3044 $record->delete_field($old_field) if $old_field;
3045 $record->insert_fields_ordered($new_field);
3049 =head2 _koha_marc_update_biblioitem_cn_sort
3053 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
3057 Given a MARC bib record and the biblioitem hash, update the
3058 subfield that contains a copy of the value of biblioitems.cn_sort.
3062 sub _koha_marc_update_biblioitem_cn_sort {
3064 my $biblioitem = shift;
3065 my $frameworkcode= shift;
3067 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
3068 return unless $biblioitem_tag;
3070 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3072 if (my $field = $marc->field($biblioitem_tag)) {
3073 $field->delete_subfield(code => $biblioitem_subfield);
3074 if ($cn_sort ne '') {
3075 $field->add_subfields($biblioitem_subfield => $cn_sort);
3078 # if we get here, no biblioitem tag is present in the MARC record, so
3079 # we'll create it if $cn_sort is not empty -- this would be
3080 # an odd combination of events, however
3082 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
3087 =head2 _koha_add_biblio
3091 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
3093 Internal function to add a biblio ($biblio is a hash with the values)
3099 sub _koha_add_biblio {
3100 my ( $dbh, $biblio, $frameworkcode ) = @_;
3104 # set the series flag
3106 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
3110 SET frameworkcode = ?,
3121 my $sth = $dbh->prepare($query);
3124 $biblio->{'author'},
3126 $biblio->{'unititle'},
3129 $biblio->{'seriestitle'},
3130 $biblio->{'copyrightdate'},
3131 $biblio->{'abstract'}
3134 my $biblionumber = $dbh->{'mysql_insertid'};
3135 if ( $dbh->errstr ) {
3136 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
3141 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
3142 return ($biblionumber,$error);
3145 =head2 _koha_modify_biblio
3149 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
3151 Internal function for updating the biblio table
3157 sub _koha_modify_biblio {
3158 my ( $dbh, $biblio, $frameworkcode ) = @_;
3163 SET frameworkcode = ?,
3172 WHERE biblionumber = ?
3175 my $sth = $dbh->prepare($query);
3179 $biblio->{'author'},
3181 $biblio->{'unititle'},
3183 $biblio->{'serial'},
3184 $biblio->{'seriestitle'},
3185 $biblio->{'copyrightdate'},
3186 $biblio->{'abstract'},
3187 $biblio->{'biblionumber'}
3188 ) if $biblio->{'biblionumber'};
3190 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
3191 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
3194 return ( $biblio->{'biblionumber'},$error );
3197 =head2 _koha_modify_biblioitem_nonmarc
3201 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
3203 Updates biblioitems row except for marc and marcxml, which should be changed
3210 sub _koha_modify_biblioitem_nonmarc {
3211 my ( $dbh, $biblioitem ) = @_;
3214 # re-calculate the cn_sort, it may have changed
3215 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3219 SET biblionumber = ?,
3225 publicationyear = ?,
3229 collectiontitle = ?,
3231 collectionvolume= ?,
3232 editionstatement= ?,
3233 editionresponsibility = ?,
3247 where biblioitemnumber = ?
3249 my $sth = $dbh->prepare($query);
3251 $biblioitem->{'biblionumber'},
3252 $biblioitem->{'volume'},
3253 $biblioitem->{'number'},
3254 $biblioitem->{'itemtype'},
3255 $biblioitem->{'isbn'},
3256 $biblioitem->{'issn'},
3257 $biblioitem->{'publicationyear'},
3258 $biblioitem->{'publishercode'},
3259 $biblioitem->{'volumedate'},
3260 $biblioitem->{'volumedesc'},
3261 $biblioitem->{'collectiontitle'},
3262 $biblioitem->{'collectionissn'},
3263 $biblioitem->{'collectionvolume'},
3264 $biblioitem->{'editionstatement'},
3265 $biblioitem->{'editionresponsibility'},
3266 $biblioitem->{'illus'},
3267 $biblioitem->{'pages'},
3268 $biblioitem->{'bnotes'},
3269 $biblioitem->{'size'},
3270 $biblioitem->{'place'},
3271 $biblioitem->{'lccn'},
3272 $biblioitem->{'url'},
3273 $biblioitem->{'biblioitems.cn_source'},
3274 $biblioitem->{'cn_class'},
3275 $biblioitem->{'cn_item'},
3276 $biblioitem->{'cn_suffix'},
3278 $biblioitem->{'totalissues'},
3279 $biblioitem->{'biblioitemnumber'}
3281 if ( $dbh->errstr ) {
3282 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
3285 return ($biblioitem->{'biblioitemnumber'},$error);
3288 =head2 _koha_add_biblioitem
3292 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
3294 Internal function to add a biblioitem
3300 sub _koha_add_biblioitem {
3301 my ( $dbh, $biblioitem ) = @_;
3302 my @fields = qw/ biblionumber
3303 cn_class cn_item cn_sort cn_source cn_suffix
3304 collectionissn collectiontitle collectionvolume
3305 editionresponsibility editionstatement
3306 illus isbn issn itemtype lccn marc
3307 notes number pages place
3308 publicationyear publishercode size
3310 volume volumedate volumedesc
3313 ($$biblioitem{cn_sort}) = GetClassSort( @$biblioitem{qw/ biblioitems.cn_source cn_class cn_item /} );
3315 my $query = 'INSERT INTO biblioitems SET '
3316 . join ( ',', map { "$_ =?" } @fields )
3320 my $sth = $dbh->prepare($query);
3321 $sth->execute( @$biblioitem{@fields} );
3322 my $bibitemnum = $dbh->{'mysql_insertid'};
3324 $dbh->errstr and warn $error .=
3325 'ERROR in _koha_add_biblioitem '
3330 return ($bibitemnum,$error);
3333 =head2 _koha_delete_biblio
3337 $error = _koha_delete_biblio($dbh,$biblionumber);
3339 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3341 C<$dbh> - the database handle
3342 C<$biblionumber> - the biblionumber of the biblio to be deleted
3348 # FIXME: add error handling
3350 sub _koha_delete_biblio {
3351 my ( $dbh, $biblionumber ) = @_;
3353 # get all the data for this biblio
3354 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3355 $sth->execute($biblionumber);
3357 if ( my $data = $sth->fetchrow_hashref ) {
3359 # save the record in deletedbiblio
3360 # find the fields to save
3361 my $query = "INSERT INTO deletedbiblio SET ";
3363 foreach my $temp ( keys %$data ) {
3364 $query .= "$temp = ?,";
3365 push( @bind, $data->{$temp} );
3368 # replace the last , by ",?)"
3370 my $bkup_sth = $dbh->prepare($query);
3371 $bkup_sth->execute(@bind);
3375 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3376 $del_sth->execute($biblionumber);
3383 =head2 _koha_delete_biblioitems
3387 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3389 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3391 C<$dbh> - the database handle
3392 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3398 # FIXME: add error handling
3400 sub _koha_delete_biblioitems {
3401 my ( $dbh, $biblioitemnumber ) = @_;
3403 # get all the data for this biblioitem
3405 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3406 $sth->execute($biblioitemnumber);
3408 if ( my $data = $sth->fetchrow_hashref ) {
3410 # save the record in deletedbiblioitems
3411 # find the fields to save
3412 my $query = "INSERT INTO deletedbiblioitems SET ";
3414 foreach my $temp ( keys %$data ) {
3415 $query .= "$temp = ?,";
3416 push( @bind, $data->{$temp} );
3419 # replace the last , by ",?)"
3421 my $bkup_sth = $dbh->prepare($query);
3422 $bkup_sth->execute(@bind);
3425 # delete the biblioitem
3427 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3428 $del_sth->execute($biblioitemnumber);
3435 =head1 UNEXPORTED FUNCTIONS
3437 =head2 ModBiblioMarc
3439 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3441 Add MARC data for a biblio to koha
3443 Function exported, but should NOT be used, unless you really know what you're doing
3449 # pass the MARC::Record to this function, and it will create the records in the marc field
3450 my ( $record, $biblionumber, $frameworkcode ) = @_;
3451 my $dbh = C4::Context->dbh;
3452 my @fields = $record->fields();
3453 if ( !$frameworkcode ) {
3454 $frameworkcode = "";
3457 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3458 $sth->execute( $frameworkcode, $biblionumber );
3460 my $encoding = C4::Context->preference("marcflavour");
3462 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3463 if ( $encoding eq "UNIMARC" ) {
3464 my $string = $record->subfield( 100, "a" );
3465 if ( ($string) && ( length($record->subfield( 100, "a" )) == 35 ) ) {
3466 my $f100 = $record->field(100);
3467 $record->delete_field($f100);
3470 $string = POSIX::strftime( "%Y%m%d", localtime );
3472 $string = sprintf( "%-*s", 35, $string );
3474 substr( $string, 22, 6, "frey50" );
3475 unless ( $record->subfield( 100, "a" ) ) {
3476 $record->insert_grouped_field(
3477 MARC::Field->new( 100, "", "", "a" => $string ) );
3481 if (C4::Context->preference("NoZebra")) {
3482 # only NoZebra indexing needs to have
3483 # the previous version of the record
3484 $oldRecord = GetMarcBiblio($biblionumber);
3488 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3489 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3492 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3493 return $biblionumber;
3496 =head2 z3950_extended_services
3498 z3950_extended_services($serviceType,$serviceOptions,$record);
3500 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.
3502 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3504 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3506 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3510 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3511 syntax => the record syntax (transfer syntax)
3512 databaseName = Database from connection object
3514 To set serviceOptions, call set_service_options($serviceType)
3516 C<$record> the record, if one is needed for the service type
3518 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3522 sub z3950_extended_services {
3523 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3525 # get our connection object
3526 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3528 # create a new package object
3529 my $Zpackage = $Zconn->package();
3532 $Zpackage->option( action => $action );
3534 if ( $serviceOptions->{'databaseName'} ) {
3535 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3537 if ( $serviceOptions->{'recordIdNumber'} ) {
3539 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3541 if ( $serviceOptions->{'recordIdOpaque'} ) {
3543 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3546 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3547 #if ($serviceType eq 'itemorder') {
3548 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3549 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3550 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3551 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3554 if ( $serviceOptions->{record} ) {
3555 $Zpackage->option( record => $serviceOptions->{record} );
3557 # can be xml or marc
3558 if ( $serviceOptions->{'syntax'} ) {
3559 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3563 # send the request, handle any exception encountered
3564 eval { $Zpackage->send($serviceType) };
3565 if ( $@ && $@->isa("ZOOM::Exception") ) {
3566 return "error: " . $@->code() . " " . $@->message() . "\n";
3569 # free up package resources
3570 $Zpackage->destroy();
3573 =head2 set_service_options
3575 my $serviceOptions = set_service_options($serviceType);
3577 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3579 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3583 sub set_service_options {
3584 my ($serviceType) = @_;
3587 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3588 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3590 if ( $serviceType eq 'commit' ) {
3594 if ( $serviceType eq 'create' ) {
3598 if ( $serviceType eq 'drop' ) {
3599 die "ERROR: 'drop' not currently supported (by Zebra)";
3601 return $serviceOptions;
3604 =head3 get_biblio_authorised_values
3606 find the types and values for all authorised values assigned to this biblio.
3610 MARC::Record of the bib
3612 returns: a hashref mapping the authorised value to the value set for this biblionumber
3614 $authorised_values = {
3615 'Scent' => 'flowery',
3616 'Audience' => 'Young Adult',
3617 'itemtypes' => 'SER',
3620 Notes: forlibrarian should probably be passed in, and called something different.
3625 sub get_biblio_authorised_values {
3626 my $biblionumber = shift;
3629 my $forlibrarian = 1; # are we in staff or opac?
3630 my $frameworkcode = GetFrameworkCode( $biblionumber );
3632 my $authorised_values;
3634 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3635 or return $authorised_values;
3637 # assume that these entries in the authorised_value table are bibliolevel.
3638 # ones that start with 'item%' are item level.
3639 my $query = q(SELECT distinct authorised_value, kohafield
3640 FROM marc_subfield_structure
3641 WHERE authorised_value !=''
3642 AND (kohafield like 'biblio%'
3643 OR kohafield like '') );
3644 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3646 foreach my $tag ( keys( %$tagslib ) ) {
3647 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3648 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3649 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3650 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3651 if ( defined $record->field( $tag ) ) {
3652 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3653 if ( defined $this_subfield_value ) {
3654 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3661 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3662 return $authorised_values;
3672 Koha Developement team <info@koha.org>
3674 Paul POULAIN paul.poulain@free.fr
3676 Joshua Ferraro jmf@liblime.com