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
26 use MARC::File::USMARC;
31 use C4::Dates qw/format_date/;
32 use C4::Log; # logaction
35 use vars qw($VERSION @ISA @EXPORT);
40 @ISA = qw( Exporter );
44 # to add biblios or items
45 push @EXPORT, qw( &AddBiblio &AddItem &AddBiblioAndItems );
53 &GetBiblioItemByBiblioNumber
54 &GetBiblioFromItemNumber
74 &GetItemsByBiblioitemnumber
75 &GetItemnumberFromBarcode
79 &GetAuthorisedValueDesc
83 &GetPublisherNameFromIsbn
95 &ModItemInMarconefield
106 # those functions are exported but should not be used
107 # they are usefull is few circumstances, so are exported.
108 # but don't use them unless you're a core developer ;-)
117 &TransformHtmlToMarc2
120 &PrepareItemrecordDisplay
127 C4::Biblio - cataloging management functions
131 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:
135 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
137 =item 2. as raw MARC in the Zebra index and storage engine
139 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
143 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
145 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.
149 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
151 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
155 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:
159 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
161 =item 2. _koha_* - low-level internal functions for managing the koha tables
163 =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.
165 =item 4. Zebra functions used to update the Zebra index
167 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
171 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 :
175 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
177 =item 2. add the biblionumber and biblioitemnumber into the MARC records
179 =item 3. save the marc record
183 When dealing with items, we must :
187 =item 1. save the item in items table, that gives us an itemnumber
189 =item 2. add the itemnumber to the item MARC field
191 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
193 When modifying a biblio or an item, the behaviour is quite similar.
197 =head1 EXPORTED FUNCTIONS
203 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
204 Exported function (core API) for adding a new biblio to koha.
211 my ( $record, $frameworkcode ) = @_;
212 my ($biblionumber,$biblioitemnumber,$error);
213 my $dbh = C4::Context->dbh;
214 # transform the data into koha-table style data
215 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
216 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
217 $olddata->{'biblionumber'} = $biblionumber;
218 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
220 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
223 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
225 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$biblionumber,"biblio")
226 if C4::Context->preference("CataloguingLog");
228 return ( $biblionumber, $biblioitemnumber );
231 =head2 AddBiblioAndItems
235 ($biblionumber,$biblioitemnumber, $itemnumber_ref, $error_ref) = AddBiblioAndItems($record, $frameworkcode);
239 Efficiently add a biblio record and create item records from its
240 embedded item fields. This routine is suitable for batch jobs.
242 The goal of this API is to have a similar effect to using AddBiblio
243 and AddItems in succession, but without inefficient repeated
244 parsing of the MARC XML bib record.
246 One functional difference is that the duplicate item barcode
247 check is implemented in this API, instead of relying on
248 the caller to do it, like AddItem does.
250 This function returns the biblionumber and biblioitemnumber of the
251 new bib, an arrayref of new itemsnumbers, and an arrayref of item
252 errors encountered during the processing. Each entry in the errors
253 list is a hashref containing the following keys:
259 Sequence number of original item tag in the MARC record.
263 Item barcode, provide to assist in the construction of
264 useful error messages.
266 =item error_condition
268 Code representing the error condition. Can be 'duplicate_barcode',
269 'invalid_homebranch', or 'invalid_holdingbranch'.
271 =item error_information
273 Additional information appropriate to the error condition.
279 sub AddBiblioAndItems {
280 my ( $record, $frameworkcode ) = @_;
281 my ($biblionumber,$biblioitemnumber,$error);
282 my @itemnumbers = ();
284 my $dbh = C4::Context->dbh;
286 # transform the data into koha-table style data
287 # FIXME - this paragraph copied from AddBiblio
288 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
289 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
290 $olddata->{'biblionumber'} = $biblionumber;
291 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
293 # FIXME - this paragraph copied from AddBiblio
294 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
296 # now we loop through the item tags and start creating items
297 my @bad_item_fields = ();
298 my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
299 my $item_sequence_num = 0;
300 ITEMFIELD: foreach my $item_field ($record->field($itemtag)) {
301 $item_sequence_num++;
302 # we take the item field and stick it into a new
303 # MARC record -- this is required so far because (FIXME)
304 # TransformMarcToKoha requires a MARC::Record, not a MARC::Field
305 # and there is no TransformMarcFieldToKoha
306 my $temp_item_marc = MARC::Record->new();
307 $temp_item_marc->append_fields($item_field);
309 # add biblionumber and biblioitemnumber
310 my $item = TransformMarcToKoha( $dbh, $temp_item_marc, $frameworkcode, 'items' );
311 $item->{'biblionumber'} = $biblionumber;
312 $item->{'biblioitemnumber'} = $biblioitemnumber;
314 # check for duplicate barcode
315 my %item_errors = CheckItemPreSave($item);
317 push @errors, _repack_item_errors($item_sequence_num, $item, \%item_errors);
318 push @bad_item_fields, $item_field;
321 my $duplicate_barcode = exists($item->{'barcode'}) && GetItemnumberFromBarcode($item->{'barcode'});
322 if ($duplicate_barcode) {
323 warn "ERROR: cannot add item $item->{'barcode'} for biblio $biblionumber: duplicate barcode\n";
326 # figure out what item type to use -- biblioitem-level or item-level
328 if (C4::Context->preference('item-level_itypes')) {
329 $itemtype = $item->{'itype'};
331 $itemtype = $olddata->{'itemtype'};
334 # FIXME - notforloan stuff copied from AddItem
335 my $sth = $dbh->prepare("SELECT notforloan FROM itemtypes WHERE itemtype=?");
336 $sth->execute($itemtype);
337 my $notforloan = $sth->fetchrow;
338 ##Change the notforloan field if $notforloan found
339 if ( $notforloan > 0 ) {
340 $item->{'notforloan'} = $notforloan;
341 &MARCitemchange( $temp_item_marc, "items.notforloan", $notforloan );
344 # FIXME - dateaccessioned stuff copied from AddItem
345 if ( !$item->{'dateaccessioned'} || $item->{'dateaccessioned'} eq '' ) {
348 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
353 "$year-" . sprintf( "%0.2d", $mon ) . "-" . sprintf( "%0.2d", $mday );
354 $item->{'dateaccessioned'} = $date;
355 &MARCitemchange( $temp_item_marc, "items.dateaccessioned", $date );
358 my ( $itemnumber, $error ) = &_koha_new_items( $dbh, $item, $item->{barcode} );
359 warn $error if $error;
360 push @itemnumbers, $itemnumber; # FIXME not checking error
362 # FIXME - not copied from AddItem
363 # FIXME - AddItems equiv code about passing $sth to TransformKohaToMarcOneField is stupid
364 &MARCitemchange( $temp_item_marc, "items.itemnumber", $itemnumber );
366 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
367 if C4::Context->preference("CataloguingLog");
369 $item_field->replace_with($temp_item_marc->field($itemtag));
372 # remove any MARC item fields for rejected items
373 foreach my $item_field (@bad_item_fields) {
374 $record->delete_field($item_field);
378 # FIXME - this paragraph copied from AddBiblio -- however, moved since
379 # since we need to create the items row and plug in the itemnumbers in the
381 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode );
383 # FIXME - when using this API, do we log both bib and item add, or just
385 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$biblionumber,"biblio")
386 if C4::Context->preference("CataloguingLog");
388 return ( $biblionumber, $biblioitemnumber, \@itemnumbers, \@errors);
392 sub _repack_item_errors {
393 my $item_sequence_num = shift;
394 my $item_ref = shift;
395 my $error_ref = shift;
397 my @repacked_errors = ();
399 foreach my $error_code (sort keys %{ $error_ref }) {
400 my $repacked_error = {};
401 $repacked_error->{'item_sequence'} = $item_sequence_num;
402 $repacked_error->{'item_barcode'} = exists($item_ref->{'barcode'}) ? $item_ref->{'barcode'} : '';
403 $repacked_error->{'error_code'} = $error_code;
404 $repacked_error->{'error_information'} = $error_ref->{$error_code};
405 push @repacked_errors, $repacked_error;
408 return @repacked_errors;
415 $biblionumber = AddItem( $record, $biblionumber)
416 Exported function (core API) for adding a new item to Koha
423 my ( $record, $biblionumber ) = @_;
424 my $dbh = C4::Context->dbh;
427 my $frameworkcode = GetFrameworkCode( $biblionumber );
428 my $item = &TransformMarcToKoha( $dbh, $record, $frameworkcode );
430 # needs old biblionumber and biblioitemnumber
431 $item->{'biblionumber'} = $biblionumber;
434 "SELECT biblioitemnumber,itemtype FROM biblioitems WHERE biblionumber=?"
436 $sth->execute( $item->{'biblionumber'} );
438 ( $item->{'biblioitemnumber'}, $itemtype ) = $sth->fetchrow;
441 "SELECT notforloan FROM itemtypes WHERE itemtype=?");
442 $sth->execute( C4::Context->preference('item-level_itypes') ? $item->{'itype'} : $itemtype );
443 my $notforloan = $sth->fetchrow;
444 ##Change the notforloan field if $notforloan found
445 if ( $notforloan > 0 ) {
446 $item->{'notforloan'} = $notforloan;
447 &MARCitemchange( $record, "items.notforloan", $notforloan );
449 if ( !$item->{'dateaccessioned'} || $item->{'dateaccessioned'} eq '' ) {
452 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
457 "$year-" . sprintf( "%0.2d", $mon ) . "-" . sprintf( "%0.2d", $mday );
458 $item->{'dateaccessioned'} = $date;
459 &MARCitemchange( $record, "items.dateaccessioned", $date );
461 my ( $itemnumber, $error ) = &_koha_new_items( $dbh, $item, $item->{barcode} );
462 # add itemnumber to MARC::Record before adding the item.
463 $sth = $dbh->prepare(
464 "SELECT tagfield,tagsubfield
465 FROM marc_subfield_structure
466 WHERE frameworkcode=?
469 &TransformKohaToMarcOneField( $sth, $record, "items.itemnumber", $itemnumber,
473 &AddItemInMarc( $record, $item->{'biblionumber'},$frameworkcode );
475 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
476 if C4::Context->preference("CataloguingLog");
478 return ($item->{biblionumber}, $item->{biblioitemnumber},$itemnumber);
483 ModBiblio( $record,$biblionumber,$frameworkcode);
484 Exported function (core API) to modify a biblio
489 my ( $record, $biblionumber, $frameworkcode ) = @_;
490 if (C4::Context->preference("CataloguingLog")) {
491 my $newrecord = GetMarcBiblio($biblionumber);
492 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$biblionumber,"BEFORE=>".$newrecord->as_formatted);
495 my $dbh = C4::Context->dbh;
497 $frameworkcode = "" unless $frameworkcode;
499 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
500 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
501 my $oldRecord = GetMarcBiblio( $biblionumber );
503 # parse each item, and, for an unknown reason, re-encode each subfield
504 # if you don't do that, the record will have encoding mixed
505 # and the biblio will be re-encoded.
506 # strange, I (Paul P.) searched more than 1 day to understand what happends
507 # but could only solve the problem this way...
508 my @fields = $oldRecord->field( $itemtag );
509 foreach my $fielditem ( @fields ){
511 foreach ($fielditem->subfields()) {
513 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
515 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
518 $record->append_fields($field);
521 # update biblionumber and biblioitemnumber in MARC
522 # FIXME - this is assuming a 1 to 1 relationship between
523 # biblios and biblioitems
524 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
525 $sth->execute($biblionumber);
526 my ($biblioitemnumber) = $sth->fetchrow;
528 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
530 # update the MARC record (that now contains biblio and items) with the new record data
531 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
533 # load the koha-table data object
534 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
536 # modify the other koha tables
537 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
538 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
546 Exported function (core API) for modifying an item in Koha.
553 my ( $record, $biblionumber, $itemnumber, $delete, $new_item_hashref )
557 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$itemnumber,$record->as_formatted)
558 if C4::Context->preference("CataloguingLog");
560 my $dbh = C4::Context->dbh;
562 # if we have a MARC record, we're coming from cataloging and so
563 # we do the whole routine: update the MARC and zebra, then update the koha
566 my $frameworkcode = GetFrameworkCode( $biblionumber );
567 ModItemInMarc( $record, $biblionumber, $itemnumber, $frameworkcode );
568 my $olditem = TransformMarcToKoha( $dbh, $record, $frameworkcode,'items');
569 $olditem->{'biblionumber'} = $biblionumber;
570 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
571 $sth->execute($biblionumber);
572 my ($biblioitemnumber) = $sth->fetchrow;
574 $olditem->{'biblioitemnumber'} = $biblioitemnumber;
575 _koha_modify_item( $dbh, $olditem );
576 return $biblionumber;
579 # otherwise, we're just looking to modify something quickly
580 # (like a status) so we just update the koha tables
581 elsif ($new_item_hashref) {
582 _koha_modify_item( $dbh, $new_item_hashref );
586 sub ModItemTransfer {
587 my ( $itemnumber, $frombranch, $tobranch ) = @_;
589 my $dbh = C4::Context->dbh;
591 #new entry in branchtransfers....
592 my $sth = $dbh->prepare(
593 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
594 VALUES (?, ?, NOW(), ?)");
595 $sth->execute($itemnumber, $frombranch, $tobranch);
596 #update holdingbranch in items .....
598 "UPDATE items SET holdingbranch = ? WHERE items.itemnumber = ?");
599 $sth->execute($tobranch,$itemnumber);
600 &ModDateLastSeen($itemnumber);
601 $sth = $dbh->prepare(
602 "SELECT biblionumber FROM items WHERE itemnumber=?"
604 $sth->execute($itemnumber);
605 while ( my ( $biblionumber ) = $sth->fetchrow ) {
606 &ModItemInMarconefield( $biblionumber, $itemnumber,
607 'items.holdingbranch', $tobranch );
612 =head2 ModBiblioframework
614 ModBiblioframework($biblionumber,$frameworkcode);
615 Exported function to modify a biblio framework
619 sub ModBiblioframework {
620 my ( $biblionumber, $frameworkcode ) = @_;
621 my $dbh = C4::Context->dbh;
622 my $sth = $dbh->prepare(
623 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
625 $sth->execute($frameworkcode, $biblionumber);
629 =head2 ModItemInMarconefield
633 modify only 1 field in a MARC item (mainly used for holdingbranch, but could also be used for status modif - moving a book to "lost" on a long overdu for example)
634 &ModItemInMarconefield( $biblionumber, $itemnumber, $itemfield, $newvalue )
640 sub ModItemInMarconefield {
641 my ( $biblionumber, $itemnumber, $itemfield, $newvalue ) = @_;
642 my $dbh = C4::Context->dbh;
643 if ( !defined $newvalue ) {
647 my $record = GetMarcItem( $biblionumber, $itemnumber );
648 my ($tagfield, $tagsubfield) = GetMarcFromKohaField( $itemfield,'');
649 # FIXME - the condition is done this way because GetMarcFromKohaField
650 # returns (0, 0) if it can't field a MARC tag for the kohafield. However,
651 # some fields like items.wthdrawn are mapped to subfield $0, making the
652 # customary test of "if ($tagfield && $tagsubfield)" incorrect.
653 # GetMarcFromKohaField should probably be returning (undef, undef), making
654 # the correct test "if (defined $tagfield && defined $tagsubfield)", but
655 # this would be a large change and consequently deferred for after 3.0.
656 if (not(int($tagfield) == 0 && int($tagsubfield) == 0)) {
657 my $tag = $record->field($tagfield);
659 # my $tagsubs = $record->field($tagfield)->subfield($tagsubfield);
660 $tag->update( $tagsubfield => $newvalue );
661 $record->delete_field($tag);
662 $record->insert_fields_ordered($tag);
663 my $frameworkcode = GetFrameworkCode( $biblionumber );
664 &ModItemInMarc( $record, $biblionumber, $itemnumber, $frameworkcode );
673 &ModItemInMarc( $record, $biblionumber, $itemnumber, $frameworkcode )
680 my ( $ItemRecord, $biblionumber, $itemnumber, $frameworkcode) = @_;
681 my $dbh = C4::Context->dbh;
683 # get complete MARC record & replace the item field by the new one
684 my $completeRecord = GetMarcBiblio($biblionumber);
685 my ($itemtag,$itemsubfield) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
686 my $itemField = $ItemRecord->field($itemtag);
687 my @items = $completeRecord->field($itemtag);
689 if ($_->subfield($itemsubfield) eq $itemnumber) {
690 # $completeRecord->delete_field($_);
691 $_->replace_with($itemField);
695 my $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
696 $sth->execute( $completeRecord->as_usmarc(), $completeRecord->as_xml_record(),$biblionumber );
698 ModZebra($biblionumber,"specialUpdate","biblioserver",$completeRecord);
701 =head2 ModDateLastSeen
703 &ModDateLastSeen($itemnum)
704 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking
705 C<$itemnum> is the item number
709 sub ModDateLastSeen {
711 my $dbh = C4::Context->dbh;
714 "UPDATE items SET itemlost=0,datelastseen = NOW() WHERE items.itemnumber = ?"
716 $sth->execute($itemnum);
723 my $error = &DelBiblio($dbh,$biblionumber);
724 Exported function (core API) for deleting a biblio in koha.
725 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
726 Also backs it up to deleted* tables
727 Checks to make sure there are not issues on any of the items
729 C<$error> : undef unless an error occurs
736 my ( $biblionumber ) = @_;
737 my $dbh = C4::Context->dbh;
738 my $error; # for error handling
740 # First make sure this biblio has no items attached
741 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
742 $sth->execute($biblionumber);
743 if (my $itemnumber = $sth->fetchrow){
744 # Fix this to use a status the template can understand
745 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
748 return $error if $error;
750 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
751 # for at least 2 reasons :
752 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
753 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
754 # 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)
755 ModZebra($biblionumber, "recordDelete", "biblioserver", undef);
757 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
760 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
761 $sth->execute($biblionumber);
762 while ( my $biblioitemnumber = $sth->fetchrow ) {
764 # delete this biblioitem
765 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
766 return $error if $error;
769 # delete biblio from Koha tables and save in deletedbiblio
770 # must do this *after* _koha_delete_biblioitems, otherwise
771 # delete cascade will prevent deletedbiblioitems rows
772 # from being generated by _koha_delete_biblioitems
773 $error = _koha_delete_biblio( $dbh, $biblionumber );
775 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$biblionumber,"")
776 if C4::Context->preference("CataloguingLog");
784 DelItem( $biblionumber, $itemnumber );
785 Exported function (core API) for deleting an item record in Koha.
792 my ( $dbh, $biblionumber, $itemnumber ) = @_;
794 # check the item has no current issues
797 &_koha_delete_item( $dbh, $itemnumber );
799 # get the MARC record
800 my $record = GetMarcBiblio($biblionumber);
801 my $frameworkcode = GetFrameworkCode($biblionumber);
804 my $copy2deleted = $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
805 $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
807 #search item field code
808 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
809 my @fields = $record->field($itemtag);
811 # delete the item specified
812 foreach my $field (@fields) {
813 if ( $field->subfield($itemsubfield) eq $itemnumber ) {
814 $record->delete_field($field);
817 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
818 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$itemnumber,"item")
819 if C4::Context->preference("CataloguingLog");
822 =head2 CheckItemPreSave
826 my $item_ref = TransformMarcToKoha($marc, 'items');
828 my %errors = CheckItemPreSave($item_ref);
829 if (exists $errors{'duplicate_barcode'}) {
830 print "item has duplicate barcode: ", $errors{'duplicate_barcode'}, "\n";
831 } elsif (exists $errors{'invalid_homebranch'}) {
832 print "item has invalid home branch: ", $errors{'invalid_homebranch'}, "\n";
833 } elsif (exists $errors{'invalid_holdingbranch'}) {
834 print "item has invalid holding branch: ", $errors{'invalid_holdingbranch'}, "\n";
841 Given a hashref containing item fields, determine if it can be
842 inserted or updated in the database. Specifically, checks for
843 database integrity issues, and returns a hash containing any
844 of the following keys, if applicable.
848 =item duplicate_barcode
850 Barcode, if it duplicates one already found in the database.
852 =item invalid_homebranch
854 Home branch, if not defined in branches table.
856 =item invalid_holdingbranch
858 Holding branch, if not defined in branches table.
862 This function does NOT implement any policy-related checks,
863 e.g., whether current operator is allowed to save an
864 item that has a given branch code.
868 sub CheckItemPreSave {
869 my $item_ref = shift;
873 # check for duplicate barcode
874 if (exists $item_ref->{'barcode'} and defined $item_ref->{'barcode'}) {
875 my $existing_itemnumber = GetItemnumberFromBarcode($item_ref->{'barcode'});
876 if ($existing_itemnumber) {
877 if (!exists $item_ref->{'itemnumber'} # new item
878 or $item_ref->{'itemnumber'} != $existing_itemnumber) { # existing item
879 $errors{'duplicate_barcode'} = $item_ref->{'barcode'};
884 # check for valid home branch
885 if (exists $item_ref->{'homebranch'} and defined $item_ref->{'homebranch'}) {
886 my $branch_name = GetBranchName($item_ref->{'homebranch'});
887 unless (defined $branch_name) {
888 # relies on fact that branches.branchname is a non-NULL column,
889 # so GetBranchName returns undef only if branch does not exist
890 $errors{'invalid_homebranch'} = $item_ref->{'homebranch'};
894 # check for valid holding branch
895 if (exists $item_ref->{'holdingbranch'} and defined $item_ref->{'holdingbranch'}) {
896 my $branch_name = GetBranchName($item_ref->{'holdingbranch'});
897 unless (defined $branch_name) {
898 # relies on fact that branches.branchname is a non-NULL column,
899 # so GetBranchName returns undef only if branch does not exist
900 $errors{'invalid_holdingbranch'} = $item_ref->{'holdingbranch'};
912 $data = &GetBiblioData($biblionumber);
913 Returns information about the book with the given biblionumber.
914 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
915 the C<biblio> and C<biblioitems> tables in the
917 In addition, C<$data-E<gt>{subject}> is the list of the book's
918 subjects, separated by C<" , "> (space, comma, space).
919 If there are multiple biblioitems with the given biblionumber, only
920 the first one is considered.
928 my $dbh = C4::Context->dbh;
930 # my $query = C4::Context->preference('item-level_itypes') ?
931 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
933 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
934 # WHERE biblio.biblionumber = ?
935 # AND biblioitems.biblionumber = biblio.biblionumber
938 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
940 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
941 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
942 WHERE biblio.biblionumber = ?
943 AND biblioitems.biblionumber = biblio.biblionumber ";
945 my $sth = $dbh->prepare($query);
946 $sth->execute($bibnum);
948 $data = $sth->fetchrow_hashref;
952 } # sub GetBiblioData
959 @results = &GetItemsInfo($biblionumber, $type);
961 Returns information about books with the given biblionumber.
963 C<$type> may be either C<intra> or anything else. If it is not set to
964 C<intra>, then the search will exclude lost, very overdue, and
967 C<&GetItemsInfo> returns a list of references-to-hash. Each element
968 contains a number of keys. Most of them are table items from the
969 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
970 Koha database. Other keys include:
974 =item C<$data-E<gt>{branchname}>
976 The name (not the code) of the branch to which the book belongs.
978 =item C<$data-E<gt>{datelastseen}>
980 This is simply C<items.datelastseen>, except that while the date is
981 stored in YYYY-MM-DD format in the database, here it is converted to
982 DD/MM/YYYY format. A NULL date is returned as C<//>.
984 =item C<$data-E<gt>{datedue}>
986 =item C<$data-E<gt>{class}>
988 This is the concatenation of C<biblioitems.classification>, the book's
989 Dewey code, and C<biblioitems.subclass>.
991 =item C<$data-E<gt>{ocount}>
993 I think this is the number of copies of the book available.
995 =item C<$data-E<gt>{order}>
997 If this is set, it is set to C<One Order>.
1006 my ( $biblionumber, $type ) = @_;
1007 my $dbh = C4::Context->dbh;
1008 my $query = "SELECT *,items.notforloan as itemnotforloan
1010 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1011 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber";
1012 $query .= (C4::Context->preference('item-level_itypes')) ?
1013 " LEFT JOIN itemtypes on items.itype = itemtypes.itemtype "
1014 : " LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype ";
1015 $query .= "WHERE items.biblionumber = ? ORDER BY items.dateaccessioned desc" ;
1016 my $sth = $dbh->prepare($query);
1017 $sth->execute($biblionumber);
1020 my ( $date_due, $count_reserves );
1022 my $isth = $dbh->prepare(
1023 "SELECT issues.*,borrowers.cardnumber,borrowers.surname,borrowers.firstname,borrowers.branchcode as bcode
1024 FROM issues LEFT JOIN borrowers ON issues.borrowernumber=borrowers.borrowernumber
1025 WHERE itemnumber = ?
1026 AND returndate IS NULL"
1028 while ( my $data = $sth->fetchrow_hashref ) {
1030 $isth->execute( $data->{'itemnumber'} );
1031 if ( my $idata = $isth->fetchrow_hashref ) {
1032 $data->{borrowernumber} = $idata->{borrowernumber};
1033 $data->{cardnumber} = $idata->{cardnumber};
1034 $data->{surname} = $idata->{surname};
1035 $data->{firstname} = $idata->{firstname};
1036 $datedue = $idata->{'date_due'};
1037 if (C4::Context->preference("IndependantBranches")){
1038 my $userenv = C4::Context->userenv;
1039 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1040 $data->{'NOTSAMEBRANCH'} = 1 if ($idata->{'bcode'} ne $userenv->{branch});
1044 if ( $datedue eq '' ) {
1045 my ( $restype, $reserves ) =
1046 C4::Reserves::CheckReserves( $data->{'itemnumber'} );
1048 $count_reserves = $restype;
1053 #get branch information.....
1054 my $bsth = $dbh->prepare(
1055 "SELECT * FROM branches WHERE branchcode = ?
1058 $bsth->execute( $data->{'holdingbranch'} );
1059 if ( my $bdata = $bsth->fetchrow_hashref ) {
1060 $data->{'branchname'} = $bdata->{'branchname'};
1062 $data->{'datedue'} = $datedue;
1063 $data->{'count_reserves'} = $count_reserves;
1065 # get notforloan complete status if applicable
1066 my $sthnflstatus = $dbh->prepare(
1067 'SELECT authorised_value
1068 FROM marc_subfield_structure
1069 WHERE kohafield="items.notforloan"
1073 $sthnflstatus->execute;
1074 my ($authorised_valuecode) = $sthnflstatus->fetchrow;
1075 if ($authorised_valuecode) {
1076 $sthnflstatus = $dbh->prepare(
1077 "SELECT lib FROM authorised_values
1079 AND authorised_value=?"
1081 $sthnflstatus->execute( $authorised_valuecode,
1082 $data->{itemnotforloan} );
1083 my ($lib) = $sthnflstatus->fetchrow;
1084 $data->{notforloan} = $lib;
1087 # my stack procedures
1088 my $stackstatus = $dbh->prepare(
1089 'SELECT authorised_value
1090 FROM marc_subfield_structure
1091 WHERE kohafield="items.stack"
1094 $stackstatus->execute;
1096 ($authorised_valuecode) = $stackstatus->fetchrow;
1097 if ($authorised_valuecode) {
1098 $stackstatus = $dbh->prepare(
1100 FROM authorised_values
1102 AND authorised_value=?
1105 $stackstatus->execute( $authorised_valuecode, $data->{stack} );
1106 my ($lib) = $stackstatus->fetchrow;
1107 $data->{stack} = $lib;
1109 # Find the last 3 people who borrowed this item.
1110 my $sth2 = $dbh->prepare("SELECT * FROM issues,borrowers
1111 WHERE itemnumber = ?
1112 AND issues.borrowernumber = borrowers.borrowernumber
1113 AND returndate IS NOT NULL LIMIT 3");
1114 $sth2->execute($data->{'itemnumber'});
1116 while (my $data2 = $sth2->fetchrow_hashref()) {
1117 $data->{"timestamp$ii"} = $data2->{'timestamp'} if $data2->{'timestamp'};
1118 $data->{"card$ii"} = $data2->{'cardnumber'} if $data2->{'cardnumber'};
1119 $data->{"borrower$ii"} = $data2->{'borrowernumber'} if $data2->{'borrowernumber'};
1123 $results[$i] = $data;
1131 =head2 getitemstatus
1135 $itemstatushash = &getitemstatus($fwkcode);
1136 returns information about status.
1137 Can be MARC dependant.
1138 fwkcode is optional.
1139 But basically could be can be loan or not
1140 Create a status selector with the following code
1142 =head3 in PERL SCRIPT
1144 my $itemstatushash = getitemstatus;
1146 foreach my $thisstatus (keys %$itemstatushash) {
1147 my %row =(value => $thisstatus,
1148 statusname => $itemstatushash->{$thisstatus}->{'statusname'},
1150 push @itemstatusloop, \%row;
1152 $template->param(statusloop=>\@itemstatusloop);
1157 <select name="statusloop">
1158 <option value="">Default</option>
1159 <!-- TMPL_LOOP name="statusloop" -->
1160 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
1168 # returns a reference to a hash of references to status...
1171 my $dbh = C4::Context->dbh;
1173 $fwk = '' unless ($fwk);
1174 my ( $tag, $subfield ) =
1175 GetMarcFromKohaField( "items.notforloan", $fwk );
1176 if ( $tag and $subfield ) {
1179 "SELECT authorised_value
1180 FROM marc_subfield_structure
1186 $sth->execute( $tag, $subfield, $fwk );
1187 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
1190 "SELECT authorised_value,lib
1191 FROM authorised_values
1196 $authvalsth->execute($authorisedvaluecat);
1197 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
1198 $itemstatus{$authorisedvalue} = $lib;
1200 $authvalsth->finish;
1201 return \%itemstatus;
1214 $itemstatus{"1"} = "Not For Loan";
1215 return \%itemstatus;
1218 =head2 getitemlocation
1222 $itemlochash = &getitemlocation($fwk);
1223 returns informations about location.
1224 where fwk stands for an optional framework code.
1225 Create a location selector with the following code
1227 =head3 in PERL SCRIPT
1229 my $itemlochash = getitemlocation;
1231 foreach my $thisloc (keys %$itemlochash) {
1232 my $selected = 1 if $thisbranch eq $branch;
1233 my %row =(locval => $thisloc,
1234 selected => $selected,
1235 locname => $itemlochash->{$thisloc},
1237 push @itemlocloop, \%row;
1239 $template->param(itemlocationloop => \@itemlocloop);
1243 <select name="location">
1244 <option value="">Default</option>
1245 <!-- TMPL_LOOP name="itemlocationloop" -->
1246 <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
1254 sub GetItemLocation {
1256 # returns a reference to a hash of references to location...
1259 my $dbh = C4::Context->dbh;
1261 $fwk = '' unless ($fwk);
1262 my ( $tag, $subfield ) =
1263 GetMarcFromKohaField( "items.location", $fwk );
1264 if ( $tag and $subfield ) {
1267 "SELECT authorised_value
1268 FROM marc_subfield_structure
1271 AND frameworkcode=?"
1273 $sth->execute( $tag, $subfield, $fwk );
1274 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
1277 "SELECT authorised_value,lib
1278 FROM authorised_values
1282 $authvalsth->execute($authorisedvaluecat);
1283 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
1284 $itemlocation{$authorisedvalue} = $lib;
1286 $authvalsth->finish;
1287 return \%itemlocation;
1300 $itemlocation{"1"} = "Not For Loan";
1301 return \%itemlocation;
1306 $items = GetLostItems($where,$orderby);
1308 This function get the items lost into C<$items>.
1313 C<$where> is a hashref. it containts a field of the items table as key
1314 and the value to match as value.
1315 C<$orderby> is a field of the items table.
1318 C<$items> is a reference to an array full of hasref which keys are items' table column.
1320 =item usage in the perl script:
1323 $where{barcode} = 0001548;
1324 my $items = GetLostItems( \%where, "homebranch" );
1325 $template->param(itemsloop => $items);
1332 # Getting input args.
1334 my $orderby = shift;
1335 my $dbh = C4::Context->dbh;
1340 WHERE itemlost IS NOT NULL
1343 foreach my $key (keys %$where) {
1344 $query .= " AND " . $key . " LIKE '%" . $where->{$key} . "%'";
1346 $query .= " ORDER BY ".$orderby if defined $orderby;
1348 my $sth = $dbh->prepare($query);
1351 while ( my $row = $sth->fetchrow_hashref ){
1357 =head2 GetItemsForInventory
1359 $itemlist = GetItemsForInventory($minlocation,$maxlocation,$datelastseen,$offset,$size)
1361 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
1363 The sub returns a list of hashes, containing itemnumber, author, title, barcode & item callnumber.
1364 It is ordered by callnumber,title.
1366 The minlocation & maxlocation parameters are used to specify a range of item callnumbers
1367 the datelastseen can be used to specify that you want to see items not seen since a past date only.
1368 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
1372 sub GetItemsForInventory {
1373 my ( $minlocation, $maxlocation,$location, $datelastseen, $branch, $offset, $size ) = @_;
1374 my $dbh = C4::Context->dbh;
1376 if ($datelastseen) {
1377 $datelastseen=format_date_in_iso($datelastseen);
1379 "SELECT itemnumber,barcode,itemcallnumber,title,author,biblio.biblionumber,datelastseen
1381 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1382 WHERE itemcallnumber>= ?
1383 AND itemcallnumber <=?
1384 AND (datelastseen< ? OR datelastseen IS NULL)";
1385 $query.= " AND items.location=".$dbh->quote($location) if $location;
1386 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
1387 $query .= " ORDER BY itemcallnumber,title";
1388 $sth = $dbh->prepare($query);
1389 $sth->execute( $minlocation, $maxlocation, $datelastseen );
1393 SELECT itemnumber,barcode,itemcallnumber,biblio.biblionumber,title,author,datelastseen
1395 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1396 WHERE itemcallnumber>= ?
1397 AND itemcallnumber <=?";
1398 $query.= " AND items.location=".$dbh->quote($location) if $location;
1399 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
1400 $query .= " ORDER BY itemcallnumber,title";
1401 $sth = $dbh->prepare($query);
1402 $sth->execute( $minlocation, $maxlocation );
1405 while ( my $row = $sth->fetchrow_hashref ) {
1406 $offset-- if ($offset);
1407 $row->{datelastseen}=format_date($row->{datelastseen});
1408 if ( ( !$offset ) && $size ) {
1409 push @results, $row;
1416 =head2 &GetBiblioItemData
1420 $itemdata = &GetBiblioItemData($biblioitemnumber);
1422 Looks up the biblioitem with the given biblioitemnumber. Returns a
1423 reference-to-hash. The keys are the fields from the C<biblio>,
1424 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1425 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1432 sub GetBiblioItemData {
1433 my ($biblioitemnumber) = @_;
1434 my $dbh = C4::Context->dbh;
1435 my $query = "SELECT *,biblioitems.notes AS bnotes
1436 FROM biblio, biblioitems ";
1437 unless(C4::Context->preference('item-level_itypes')) {
1438 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
1440 $query .= " WHERE biblio.biblionumber = biblioitems.biblionumber
1441 AND biblioitemnumber = ? ";
1442 my $sth = $dbh->prepare($query);
1444 $sth->execute($biblioitemnumber);
1445 $data = $sth->fetchrow_hashref;
1448 } # sub &GetBiblioItemData
1450 =head2 GetItemnumberFromBarcode
1454 $result = GetItemnumberFromBarcode($barcode);
1460 sub GetItemnumberFromBarcode {
1462 my $dbh = C4::Context->dbh;
1465 $dbh->prepare("SELECT itemnumber FROM items WHERE items.barcode=?");
1466 $rq->execute($barcode);
1467 my ($result) = $rq->fetchrow;
1471 =head2 GetBiblioItemByBiblioNumber
1475 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
1481 sub GetBiblioItemByBiblioNumber {
1482 my ($biblionumber) = @_;
1483 my $dbh = C4::Context->dbh;
1484 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
1488 $sth->execute($biblionumber);
1490 while ( my $data = $sth->fetchrow_hashref ) {
1491 push @results, $data;
1498 =head2 GetBiblioFromItemNumber
1502 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
1504 Looks up the item with the given itemnumber. if undef, try the barcode.
1506 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1507 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1515 sub GetBiblioFromItemNumber {
1516 my ( $itemnumber, $barcode ) = @_;
1517 my $dbh = C4::Context->dbh;
1520 $sth=$dbh->prepare( "SELECT * FROM items
1521 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1522 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1523 WHERE items.itemnumber = ?") ;
1524 $sth->execute($itemnumber);
1526 $sth=$dbh->prepare( "SELECT * FROM items
1527 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
1528 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
1529 WHERE items.barcode = ?") ;
1530 $sth->execute($barcode);
1532 my $data = $sth->fetchrow_hashref;
1541 ( $count, @results ) = &GetBiblio($biblionumber);
1548 my ($biblionumber) = @_;
1549 my $dbh = C4::Context->dbh;
1550 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
1553 $sth->execute($biblionumber);
1554 while ( my $data = $sth->fetchrow_hashref ) {
1555 $results[$count] = $data;
1559 return ( $count, @results );
1566 $data = &GetItem($itemnumber,$barcode);
1568 return Item information, for a given itemnumber or barcode
1575 my ($itemnumber,$barcode) = @_;
1576 my $dbh = C4::Context->dbh;
1578 my $sth = $dbh->prepare("
1580 WHERE itemnumber = ?");
1581 $sth->execute($itemnumber);
1582 my $data = $sth->fetchrow_hashref;
1585 my $sth = $dbh->prepare("
1589 $sth->execute($barcode);
1590 my $data = $sth->fetchrow_hashref;
1595 =head2 get_itemnumbers_of
1599 my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1601 Given a list of biblionumbers, return the list of corresponding itemnumbers
1602 for each biblionumber.
1604 Return a reference on a hash where keys are biblionumbers and values are
1605 references on array of itemnumbers.
1611 sub get_itemnumbers_of {
1612 my @biblionumbers = @_;
1614 my $dbh = C4::Context->dbh;
1620 WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
1622 my $sth = $dbh->prepare($query);
1623 $sth->execute(@biblionumbers);
1627 while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1628 push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1631 return \%itemnumbers_of;
1634 =head2 GetItemInfosOf
1638 GetItemInfosOf(@itemnumbers);
1644 sub GetItemInfosOf {
1645 my @itemnumbers = @_;
1650 WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
1652 return get_infos_of( $query, 'itemnumber' );
1655 =head2 GetItemsByBiblioitemnumber
1659 GetItemsByBiblioitemnumber($biblioitemnumber);
1661 Returns an arrayref of hashrefs suitable for use in a TMPL_LOOP
1662 Called by moredetail.pl
1668 sub GetItemsByBiblioitemnumber {
1669 my ( $bibitem ) = @_;
1670 my $dbh = C4::Context->dbh;
1671 my $sth = $dbh->prepare("SELECT * FROM items WHERE items.biblioitemnumber = ?") || die $dbh->errstr;
1672 # Get all items attached to a biblioitem
1675 $sth->execute($bibitem) || die $sth->errstr;
1676 while ( my $data = $sth->fetchrow_hashref ) {
1677 # Foreach item, get circulation information
1678 my $sth2 = $dbh->prepare( "SELECT * FROM issues,borrowers
1679 WHERE itemnumber = ?
1680 AND returndate is NULL
1681 AND issues.borrowernumber = borrowers.borrowernumber"
1683 $sth2->execute( $data->{'itemnumber'} );
1684 if ( my $data2 = $sth2->fetchrow_hashref ) {
1685 # if item is out, set the due date and who it is out too
1686 $data->{'date_due'} = $data2->{'date_due'};
1687 $data->{'cardnumber'} = $data2->{'cardnumber'};
1688 $data->{'borrowernumber'} = $data2->{'borrowernumber'};
1691 # set date_due to blank, so in the template we check itemlost, and wthdrawn
1692 $data->{'date_due'} = '';
1695 # Find the last 3 people who borrowed this item.
1696 my $query2 = "SELECT * FROM issues, borrowers WHERE itemnumber = ?
1697 AND issues.borrowernumber = borrowers.borrowernumber
1698 AND returndate is not NULL
1699 ORDER BY returndate desc,timestamp desc LIMIT 3";
1700 $sth2 = $dbh->prepare($query2) || die $dbh->errstr;
1701 $sth2->execute( $data->{'itemnumber'} ) || die $sth2->errstr;
1703 while ( my $data2 = $sth2->fetchrow_hashref ) {
1704 $data->{"timestamp$i2"} = $data2->{'timestamp'};
1705 $data->{"card$i2"} = $data2->{'cardnumber'};
1706 $data->{"borrower$i2"} = $data2->{'borrowernumber'};
1710 push(@results,$data);
1717 =head2 GetBiblioItemInfosOf
1721 GetBiblioItemInfosOf(@biblioitemnumbers);
1727 sub GetBiblioItemInfosOf {
1728 my @biblioitemnumbers = @_;
1731 SELECT biblioitemnumber,
1735 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
1737 return get_infos_of( $query, 'biblioitemnumber' );
1740 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
1742 =head2 GetMarcStructure
1746 $res = GetMarcStructure($forlibrarian,$frameworkcode);
1748 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
1749 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
1750 $frameworkcode : the framework code to read
1756 sub GetMarcStructure {
1757 my ( $forlibrarian, $frameworkcode ) = @_;
1758 my $dbh=C4::Context->dbh;
1759 $frameworkcode = "" unless $frameworkcode;
1761 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
1763 # check that framework exists
1766 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
1767 $sth->execute($frameworkcode);
1768 my ($total) = $sth->fetchrow;
1769 $frameworkcode = "" unless ( $total > 0 );
1772 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
1773 FROM marc_tag_structure
1774 WHERE frameworkcode=?
1777 $sth->execute($frameworkcode);
1778 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
1780 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
1783 $res->{$tag}->{lib} =
1784 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
1785 $res->{$tab}->{tab} = "";
1786 $res->{$tag}->{mandatory} = $mandatory;
1787 $res->{$tag}->{repeatable} = $repeatable;
1792 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
1793 FROM marc_subfield_structure
1794 WHERE frameworkcode=?
1795 ORDER BY tagfield,tagsubfield
1799 $sth->execute($frameworkcode);
1802 my $authorised_value;
1814 $tag, $subfield, $liblibrarian,
1816 $mandatory, $repeatable, $authorised_value,
1817 $authtypecode, $value_builder, $kohafield,
1818 $seealso, $hidden, $isurl,
1824 $res->{$tag}->{$subfield}->{lib} =
1825 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
1826 $res->{$tag}->{$subfield}->{tab} = $tab;
1827 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
1828 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
1829 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
1830 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
1831 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
1832 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
1833 $res->{$tag}->{$subfield}->{seealso} = $seealso;
1834 $res->{$tag}->{$subfield}->{hidden} = $hidden;
1835 $res->{$tag}->{$subfield}->{isurl} = $isurl;
1836 $res->{$tag}->{$subfield}->{'link'} = $link;
1837 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
1842 =head2 GetUsedMarcStructure
1844 the same function as GetMarcStructure expcet it just take field
1845 in tab 0-9. (used field)
1847 my $results = GetUsedMarcStructure($frameworkcode);
1849 L<$results> is a ref to an array which each case containts a ref
1850 to a hash which each keys is the columns from marc_subfield_structure
1852 L<$frameworkcode> is the framework code.
1856 sub GetUsedMarcStructure($){
1857 my $frameworkcode = shift || '';
1858 my $dbh = C4::Context->dbh;
1861 FROM marc_subfield_structure
1863 AND frameworkcode = ?
1866 my $sth = $dbh->prepare($query);
1867 $sth->execute($frameworkcode);
1868 while (my $row = $sth->fetchrow_hashref){
1874 =head2 GetMarcFromKohaField
1878 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1879 Returns the MARC fields & subfields mapped to the koha field
1880 for the given frameworkcode
1886 sub GetMarcFromKohaField {
1887 my ( $kohafield, $frameworkcode ) = @_;
1888 return 0, 0 unless $kohafield;
1889 my $relations = C4::Context->marcfromkohafield;
1891 $relations->{$frameworkcode}->{$kohafield}->[0],
1892 $relations->{$frameworkcode}->{$kohafield}->[1]
1896 =head2 GetMarcBiblio
1900 Returns MARC::Record of the biblionumber passed in parameter.
1901 the marc record contains both biblio & item datas
1908 my $biblionumber = shift;
1909 my $dbh = C4::Context->dbh;
1911 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1912 $sth->execute($biblionumber);
1913 my ($marcxml) = $sth->fetchrow;
1914 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
1915 $marcxml =~ s/\x1e//g;
1916 $marcxml =~ s/\x1f//g;
1917 $marcxml =~ s/\x1d//g;
1918 $marcxml =~ s/\x0f//g;
1919 $marcxml =~ s/\x0c//g;
1921 my $record = MARC::Record->new();
1923 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
1925 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1936 my $marcxml = GetXmlBiblio($biblionumber);
1938 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1939 The XML contains both biblio & item datas
1946 my ( $biblionumber ) = @_;
1947 my $dbh = C4::Context->dbh;
1949 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1950 $sth->execute($biblionumber);
1951 my ($marcxml) = $sth->fetchrow;
1955 =head2 GetAuthorisedValueDesc
1959 my $subfieldvalue =get_authorised_value_desc(
1960 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1961 Retrieve the complete description for a given authorised value.
1963 Now takes $category and $value pair too.
1964 my $auth_value_desc =GetAuthorisedValueDesc(
1965 '','', 'DVD' ,'','','CCODE');
1971 sub GetAuthorisedValueDesc {
1972 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1973 my $dbh = C4::Context->dbh;
1977 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1978 return C4::Branch::GetBranchName($value);
1982 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1983 return getitemtypeinfo($value)->{description};
1986 #---- "true" authorized value
1987 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1990 if ( $category ne "" ) {
1993 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1995 $sth->execute( $category, $value );
1996 my $data = $sth->fetchrow_hashref;
1997 return $data->{'lib'};
2000 return $value; # if nothing is found return the original value
2008 Returns MARC::Record of the item passed in parameter.
2015 my ( $biblionumber, $itemnumber ) = @_;
2017 # GetMarcItem has been revised so that it does the following:
2018 # 1. Gets the item information from the items table.
2019 # 2. Converts it to a MARC field for storage in the bib record.
2021 # The previous behavior was:
2022 # 1. Get the bib record.
2023 # 2. Return the MARC tag corresponding to the item record.
2025 # The difference is that one treats the items row as authoritative,
2026 # while the other treats the MARC representation as authoritative
2027 # under certain circumstances.
2031 # As of 2007-11-27, this change hopefully does not introduce
2032 # any bugs. However, it does mean that for code that uses
2033 # ModItemInMarconefield to update one subfield (corresponding to
2034 # an items column) is now less efficient.
2036 # The API needs to be shifted to the following:
2037 # 1. User updates items record.
2038 # 2. Linked bib is sent for indexing.
2040 # The missing step 1.5 is updating the item tag in the bib MARC record
2041 # so that the indexes are updated. Depending on performance considerations,
2042 # this may ultimately mean of of the following:
2043 # a. MARC field for item is updated right away.
2044 # b. MARC field for item is updated only as part of indexing.
2045 # c. MARC field for item is never actually stored in bib record; instead
2046 # it is generated only when needed for indexing, item export, and
2047 # (maybe) OPAC display.
2050 my $itemrecord = GetItem($itemnumber);
2052 # Tack on 'items.' prefix to column names so that TransformKohaToMarc will work.
2053 # Also, don't emit a subfield if the underlying field is blank.
2054 my $mungeditem = { map { $itemrecord->{$_} ne '' ? ("items.$_" => $itemrecord->{$_}) : () } keys %{ $itemrecord } };
2056 my $itemmarc = TransformKohaToMarc($mungeditem);
2067 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
2068 Get all notes from the MARC record and returns them in an array.
2069 The note are stored in differents places depending on MARC flavour
2076 my ( $record, $marcflavour ) = @_;
2078 if ( $marcflavour eq "MARC21" ) {
2081 else { # assume unimarc if not marc21
2088 foreach my $field ( $record->field($scope) ) {
2089 my $value = $field->as_string();
2090 if ( $note ne "" ) {
2091 $marcnote = { marcnote => $note, };
2092 push @marcnotes, $marcnote;
2095 if ( $note ne $value ) {
2096 $note = $note . " " . $value;
2101 $marcnote = { marcnote => $note };
2102 push @marcnotes, $marcnote; #load last tag into array
2105 } # end GetMarcNotes
2107 =head2 GetMarcSubjects
2111 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
2112 Get all subjects from the MARC record and returns them in an array.
2113 The subjects are stored in differents places depending on MARC flavour
2119 sub GetMarcSubjects {
2120 my ( $record, $marcflavour ) = @_;
2121 my ( $mintag, $maxtag );
2122 if ( $marcflavour eq "MARC21" ) {
2126 else { # assume unimarc if not marc21
2136 foreach my $field ( $record->field('6..' )) {
2137 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
2139 my @subfields = $field->subfields();
2142 # if there is an authority link, build the link with an= subfield9
2143 my $subfield9 = $field->subfield('9');
2144 for my $subject_subfield (@subfields ) {
2145 # don't load unimarc subfields 3,4,5
2146 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ (3|4|5) ) );
2147 my $code = $subject_subfield->[0];
2148 my $value = $subject_subfield->[1];
2149 my $linkvalue = $value;
2150 $linkvalue =~ s/(\(|\))//g;
2151 my $operator = " and " unless $counter==0;
2153 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
2155 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
2157 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
2159 my @this_link_loop = @link_loop;
2160 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] == 9 );
2164 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
2167 return \@marcsubjects;
2168 } #end getMARCsubjects
2170 =head2 GetMarcAuthors
2174 authors = GetMarcAuthors($record,$marcflavour);
2175 Get all authors from the MARC record and returns them in an array.
2176 The authors are stored in differents places depending on MARC flavour
2182 sub GetMarcAuthors {
2183 my ( $record, $marcflavour ) = @_;
2184 my ( $mintag, $maxtag );
2185 # tagslib useful for UNIMARC author reponsabilities
2186 my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
2187 if ( $marcflavour eq "MARC21" ) {
2191 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
2200 foreach my $field ( $record->fields ) {
2201 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
2204 my @subfields = $field->subfields();
2206 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
2207 my $subfield9 = $field->subfield('9');
2208 for my $authors_subfield (@subfields) {
2209 # don't load unimarc subfields 3, 5
2210 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ (3|5) ) );
2211 my $subfieldcode = $authors_subfield->[0];
2212 my $value = $authors_subfield->[1];
2213 my $linkvalue = $value;
2214 $linkvalue =~ s/(\(|\))//g;
2215 my $operator = " and " unless $count_auth==0;
2216 # if we have an authority link, use that as the link, otherwise use standard searching
2218 @link_loop = ({'limit' => 'Koha-Auth-Number' ,link => "$subfield9" });
2221 # reset $linkvalue if UNIMARC author responsibility
2222 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq '4')) {
2223 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
2225 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
2227 my @this_link_loop = @link_loop;
2228 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
2229 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] == 9 );
2232 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
2234 return \@marcauthors;
2241 $marcurls = GetMarcUrls($record,$marcflavour);
2242 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
2243 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
2250 my ($record, $marcflavour) = @_;
2253 for my $field ($record->field('856')) {
2254 my $url = $field->subfield('u');
2256 for my $note ( $field->subfield('z')) {
2257 push @notes , {note => $note};
2259 $marcurl = { MARCURL => $url,
2262 if($marcflavour eq 'MARC21') {
2263 my $s3 = $field->subfield('3');
2264 my $link = $field->subfield('y');
2265 $marcurl->{'linktext'} = $link || $s3 || $url ;;
2266 $marcurl->{'part'} = $s3 if($link);
2267 $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
2269 $marcurl->{'linktext'} = $url;
2271 push @marcurls, $marcurl;
2276 =head2 GetMarcSeries
2280 $marcseriesarray = GetMarcSeries($record,$marcflavour);
2281 Get all series from the MARC record and returns them in an array.
2282 The series are stored in differents places depending on MARC flavour
2289 my ($record, $marcflavour) = @_;
2290 my ($mintag, $maxtag);
2291 if ($marcflavour eq "MARC21") {
2294 } else { # assume unimarc if not marc21
2304 foreach my $field ($record->field('440'), $record->field('490')) {
2306 #my $value = $field->subfield('a');
2307 #$marcsubjct = {MARCSUBJCT => $value,};
2308 my @subfields = $field->subfields();
2309 #warn "subfields:".join " ", @$subfields;
2312 for my $series_subfield (@subfields) {
2314 undef $volume_number;
2315 # see if this is an instance of a volume
2316 if ($series_subfield->[0] eq 'v') {
2320 my $code = $series_subfield->[0];
2321 my $value = $series_subfield->[1];
2322 my $linkvalue = $value;
2323 $linkvalue =~ s/(\(|\))//g;
2324 my $operator = " and " unless $counter==0;
2325 push @link_loop, {link => $linkvalue, operator => $operator };
2326 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
2327 if ($volume_number) {
2328 push @subfields_loop, {volumenum => $value};
2331 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
2335 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
2336 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
2337 #push @marcsubjcts, $marcsubjct;
2341 my $marcseriessarray=\@marcseries;
2342 return $marcseriessarray;
2343 } #end getMARCseriess
2345 =head2 GetFrameworkCode
2349 $frameworkcode = GetFrameworkCode( $biblionumber )
2355 sub GetFrameworkCode {
2356 my ( $biblionumber ) = @_;
2357 my $dbh = C4::Context->dbh;
2358 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
2359 $sth->execute($biblionumber);
2360 my ($frameworkcode) = $sth->fetchrow;
2361 return $frameworkcode;
2364 =head2 GetPublisherNameFromIsbn
2366 $name = GetPublishercodeFromIsbn($isbn);
2373 sub GetPublisherNameFromIsbn($){
2375 $isbn =~ s/[- _]//g;
2377 my @codes = (split '-', DisplayISBN($isbn));
2378 my $code = $codes[0].$codes[1].$codes[2];
2379 my $dbh = C4::Context->dbh;
2381 SELECT distinct publishercode
2384 AND publishercode IS NOT NULL
2387 my $sth = $dbh->prepare($query);
2388 $sth->execute("$code%");
2389 my $name = $sth->fetchrow;
2390 return $name if length $name;
2394 =head2 TransformKohaToMarc
2398 $record = TransformKohaToMarc( $hash )
2399 This function builds partial MARC::Record from a hash
2400 Hash entries can be from biblio or biblioitems.
2401 This function is called in acquisition module, to create a basic catalogue entry from user entry
2407 sub TransformKohaToMarc {
2410 my $dbh = C4::Context->dbh;
2413 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
2415 my $record = MARC::Record->new();
2416 foreach (keys %{$hash}) {
2417 &TransformKohaToMarcOneField( $sth, $record, $_,
2423 =head2 TransformKohaToMarcOneField
2427 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
2433 sub TransformKohaToMarcOneField {
2434 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
2435 $frameworkcode='' unless $frameworkcode;
2439 if ( !defined $sth ) {
2440 my $dbh = C4::Context->dbh;
2441 $sth = $dbh->prepare(
2442 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
2445 $sth->execute( $frameworkcode, $kohafieldname );
2446 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
2447 my $tag = $record->field($tagfield);
2449 $tag->update( $tagsubfield => $value );
2450 $record->delete_field($tag);
2451 $record->insert_fields_ordered($tag);
2454 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
2460 =head2 TransformHtmlToXml
2464 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
2466 $auth_type contains :
2467 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
2468 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
2469 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
2475 sub TransformHtmlToXml {
2476 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
2477 my $xml = MARC::File::XML::header('UTF-8');
2478 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
2479 MARC::File::XML->default_record_format($auth_type);
2480 # in UNIMARC, field 100 contains the encoding
2481 # check that there is one, otherwise the
2482 # MARC::Record->new_from_xml will fail (and Koha will die)
2483 my $unimarc_and_100_exist=0;
2484 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
2489 for ( my $i = 0 ; $i <= @$tags ; $i++ ) {
2490 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
2491 # if we have a 100 field and it's values are not correct, skip them.
2492 # if we don't have any valid 100 field, we will create a default one at the end
2493 my $enc = substr( @$values[$i], 26, 2 );
2494 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
2495 $unimarc_and_100_exist=1;
2500 @$values[$i] =~ s/&/&/g;
2501 @$values[$i] =~ s/</</g;
2502 @$values[$i] =~ s/>/>/g;
2503 @$values[$i] =~ s/"/"/g;
2504 @$values[$i] =~ s/'/'/g;
2505 # if ( !utf8::is_utf8( @$values[$i] ) ) {
2506 # utf8::decode( @$values[$i] );
2508 if ( ( @$tags[$i] ne $prevtag ) ) {
2509 $j++ unless ( @$tags[$i] eq "" );
2511 $xml .= "</datafield>\n";
2512 if ( ( @$tags[$i] && @$tags[$i] > 10 )
2513 && ( @$values[$i] ne "" ) )
2515 my $ind1 = substr( @$indicator[$j], 0, 1 );
2517 if ( @$indicator[$j] ) {
2518 $ind2 = substr( @$indicator[$j], 1, 1 );
2521 warn "Indicator in @$tags[$i] is empty";
2525 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2527 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2535 if ( @$values[$i] ne "" ) {
2538 if ( @$tags[$i] eq "000" ) {
2539 $xml .= "<leader>@$values[$i]</leader>\n";
2542 # rest of the fixed fields
2544 elsif ( @$tags[$i] < 10 ) {
2546 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
2550 my $ind1 = substr( @$indicator[$j], 0, 1 );
2551 my $ind2 = substr( @$indicator[$j], 1, 1 );
2553 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2555 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2561 else { # @$tags[$i] eq $prevtag
2562 if ( @$values[$i] eq "" ) {
2566 my $ind1 = substr( @$indicator[$j], 0, 1 );
2567 my $ind2 = substr( @$indicator[$j], 1, 1 );
2569 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2573 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2576 $prevtag = @$tags[$i];
2578 if (C4::Context->preference('marcflavour') and !$unimarc_and_100_exist) {
2579 # warn "SETTING 100 for $auth_type";
2580 use POSIX qw(strftime);
2581 my $string = strftime( "%Y%m%d", localtime(time) );
2582 # set 50 to position 26 is biblios, 13 if authorities
2584 $pos=13 if $auth_type eq 'UNIMARCAUTH';
2585 $string = sprintf( "%-*s", 35, $string );
2586 substr( $string, $pos , 6, "50" );
2587 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
2588 $xml .= "<subfield code=\"a\">$string</subfield>\n";
2589 $xml .= "</datafield>\n";
2591 $xml .= MARC::File::XML::footer();
2595 =head2 TransformHtmlToMarc
2597 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
2598 L<$params> is a ref to an array as below:
2600 'tag_010_indicator_531951' ,
2601 'tag_010_code_a_531951_145735' ,
2602 'tag_010_subfield_a_531951_145735' ,
2603 'tag_200_indicator_873510' ,
2604 'tag_200_code_a_873510_673465' ,
2605 'tag_200_subfield_a_873510_673465' ,
2606 'tag_200_code_b_873510_704318' ,
2607 'tag_200_subfield_b_873510_704318' ,
2608 'tag_200_code_e_873510_280822' ,
2609 'tag_200_subfield_e_873510_280822' ,
2610 'tag_200_code_f_873510_110730' ,
2611 'tag_200_subfield_f_873510_110730' ,
2613 L<$cgi> is the CGI object which containts the value.
2614 L<$record> is the MARC::Record object.
2618 sub TransformHtmlToMarc {
2622 # creating a new record
2623 my $record = MARC::Record->new();
2626 while ($params->[$i]){ # browse all CGI params
2627 my $param = $params->[$i];
2629 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
2630 if ($param eq 'biblionumber') {
2631 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
2632 &GetMarcFromKohaField( "biblio.biblionumber", '' );
2633 if ($biblionumbertagfield < 10) {
2634 $newfield = MARC::Field->new(
2635 $biblionumbertagfield,
2636 $cgi->param($param),
2639 $newfield = MARC::Field->new(
2640 $biblionumbertagfield,
2643 "$biblionumbertagsubfield" => $cgi->param($param),
2646 push @fields,$newfield if($newfield);
2648 elsif ($param =~ /^tag_(\d*)_indicator_/){ # new field start when having 'input name="..._indicator_..."
2651 my $ind1 = substr($cgi->param($param),0,1);
2652 my $ind2 = substr($cgi->param($param),1,1);
2656 if($tag < 10){ # no code for theses fields
2657 # in MARC editor, 000 contains the leader.
2658 if ($tag eq '000' ) {
2659 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
2660 # between 001 and 009 (included)
2662 $newfield = MARC::Field->new(
2664 $cgi->param($params->[$j+1]),
2667 # > 009, deal with subfields
2669 while($params->[$j] =~ /_code_/){ # browse all it's subfield
2670 my $inner_param = $params->[$j];
2672 if($cgi->param($params->[$j+1])){ # only if there is a value (code => value)
2673 $newfield->add_subfields(
2674 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
2678 if ( $cgi->param($params->[$j+1]) ) { # creating only if there is a value (code => value)
2679 $newfield = MARC::Field->new(
2683 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
2690 push @fields,$newfield if($newfield);
2695 $record->append_fields(@fields);
2699 # cache inverted MARC field map
2700 our $inverted_field_map;
2702 =head2 TransformMarcToKoha
2706 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2710 Extract data from a MARC bib record into a hashref representing
2711 Koha biblio, biblioitems, and items fields.
2714 sub TransformMarcToKoha {
2715 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
2719 unless (defined $inverted_field_map) {
2720 $inverted_field_map = _get_inverted_marc_field_map();
2724 if ($limit_table eq 'items') {
2725 $tables{'items'} = 1;
2727 $tables{'items'} = 1;
2728 $tables{'biblio'} = 1;
2729 $tables{'biblioitems'} = 1;
2732 # traverse through record
2733 MARCFIELD: foreach my $field ($record->fields()) {
2734 my $tag = $field->tag();
2735 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2736 if ($field->is_control_field()) {
2737 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
2738 ENTRY: foreach my $entry (@{ $kohafields }) {
2739 my ($subfield, $table, $column) = @{ $entry };
2740 next ENTRY unless exists $tables{$table};
2741 my $key = _disambiguate($table, $column);
2742 if ($result->{$key}) {
2743 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
2744 $result->{$key} .= " | " . $field->data();
2747 $result->{$key} = $field->data();
2751 # deal with subfields
2752 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
2753 my $code = $sf->[0];
2754 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
2755 my $value = $sf->[1];
2756 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
2757 my ($table, $column) = @{ $entry };
2758 next SFENTRY unless exists $tables{$table};
2759 my $key = _disambiguate($table, $column);
2760 if ($result->{$key}) {
2761 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
2762 $result->{$key} .= " | " . $value;
2765 $result->{$key} = $value;
2772 # modify copyrightdate to keep only the 1st year found
2773 if (exists $result->{'copyrightdate'}) {
2774 my $temp = $result->{'copyrightdate'};
2775 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
2777 $result->{'copyrightdate'} = $1;
2779 else { # if no cYYYY, get the 1st date.
2780 $temp =~ m/(\d\d\d\d)/;
2781 $result->{'copyrightdate'} = $1;
2785 # modify publicationyear to keep only the 1st year found
2786 if (exists $result->{'publicationyear'}) {
2787 my $temp = $result->{'publicationyear'};
2788 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
2790 $result->{'publicationyear'} = $1;
2792 else { # if no cYYYY, get the 1st date.
2793 $temp =~ m/(\d\d\d\d)/;
2794 $result->{'publicationyear'} = $1;
2801 sub _get_inverted_marc_field_map {
2802 my $relations = C4::Context->marcfromkohafield;
2805 my $relations = C4::Context->marcfromkohafield;
2807 foreach my $frameworkcode (keys %{ $relations }) {
2808 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
2809 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
2810 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2811 my ($table, $column) = split /[.]/, $kohafield, 2;
2812 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2813 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2819 =head2 _disambiguate
2823 $newkey = _disambiguate($table, $field);
2825 This is a temporary hack to distinguish between the
2826 following sets of columns when using TransformMarcToKoha.
2828 items.cn_source & biblioitems.cn_source
2829 items.cn_sort & biblioitems.cn_sort
2831 Columns that are currently NOT distinguished (FIXME
2832 due to lack of time to fully test) are:
2834 biblio.notes and biblioitems.notes
2839 FIXME - this is necessary because prefixing each column
2840 name with the table name would require changing lots
2841 of code and templates, and exposing more of the DB
2842 structure than is good to the UI templates, particularly
2843 since biblio and bibloitems may well merge in a future
2844 version. In the future, it would also be good to
2845 separate DB access and UI presentation field names
2853 my ($table, $column) = @_;
2854 if ($column eq "cn_sort" or $column eq "cn_source") {
2855 return $table . '.' . $column;
2862 =head2 get_koha_field_from_marc
2866 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2868 Internal function to map data from the MARC record to a specific non-MARC field.
2869 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2875 sub get_koha_field_from_marc {
2876 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
2877 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
2879 foreach my $field ( $record->field($tagfield) ) {
2880 if ( $field->tag() < 10 ) {
2882 $kohafield .= " | " . $field->data();
2885 $kohafield = $field->data();
2889 if ( $field->subfields ) {
2890 my @subfields = $field->subfields();
2891 foreach my $subfieldcount ( 0 .. $#subfields ) {
2892 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2895 " | " . $subfields[$subfieldcount][1];
2899 $subfields[$subfieldcount][1];
2910 =head2 TransformMarcToKohaOneField
2914 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2920 sub TransformMarcToKohaOneField {
2922 # FIXME ? if a field has a repeatable subfield that is used in old-db,
2923 # only the 1st will be retrieved...
2924 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2926 my ( $tagfield, $subfield ) =
2927 GetMarcFromKohaField( $kohatable . "." . $kohafield,
2929 foreach my $field ( $record->field($tagfield) ) {
2930 if ( $field->tag() < 10 ) {
2931 if ( $result->{$kohafield} ) {
2932 $result->{$kohafield} .= " | " . $field->data();
2935 $result->{$kohafield} = $field->data();
2939 if ( $field->subfields ) {
2940 my @subfields = $field->subfields();
2941 foreach my $subfieldcount ( 0 .. $#subfields ) {
2942 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2943 if ( $result->{$kohafield} ) {
2944 $result->{$kohafield} .=
2945 " | " . $subfields[$subfieldcount][1];
2948 $result->{$kohafield} =
2949 $subfields[$subfieldcount][1];
2959 =head1 OTHER FUNCTIONS
2965 my $string = char_decode( $string, $encoding );
2967 converts ISO 5426 coded string to UTF-8
2968 sloppy code : should be improved in next issue
2975 my ( $string, $encoding ) = @_;
2978 $encoding = C4::Context->preference("marcflavour") unless $encoding;
2979 if ( $encoding eq "UNIMARC" ) {
3049 # this handles non-sorting blocks (if implementation requires this)
3050 $string = nsb_clean($_);
3052 elsif ( $encoding eq "USMARC" || $encoding eq "MARC21" ) {
3111 #Additional Turkish characters
3114 s/(\xf0)s/\xc5\x9f/gm;
3115 s/(\xf0)S/\xc5\x9e/gm;
3118 s/\xe7\x49/\\xc4\xb0/gm;
3119 s/(\xe6)G/\xc4\x9e/gm;
3120 s/(\xe6)g/ğ\xc4\x9f/gm;
3123 s/(\xe8|\xc8)o/ö/gm;
3124 s/(\xe8|\xc8)O/Ö/gm;
3125 s/(\xe8|\xc8)u/ü/gm;
3126 s/(\xe8|\xc8)U/Ü/gm;
3127 s/\xc2\xb8/\xc4\xb1/gm;
3130 # this handles non-sorting blocks (if implementation requires this)
3131 $string = nsb_clean($_);
3140 my $string = nsb_clean( $string, $encoding );
3147 my $NSB = '\x88'; # NSB : begin Non Sorting Block
3148 my $NSE = '\x89'; # NSE : Non Sorting Block end
3149 # handles non sorting blocks
3153 s/[ ]{0,1}$NSE/) /gm;
3158 =head2 PrepareItemrecordDisplay
3162 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
3164 Returns a hash with all the fields for Display a given item data in a template
3170 sub PrepareItemrecordDisplay {
3172 my ( $bibnum, $itemnum ) = @_;
3174 my $dbh = C4::Context->dbh;
3175 my $frameworkcode = &GetFrameworkCode( $bibnum );
3176 my ( $itemtagfield, $itemtagsubfield ) =
3177 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
3178 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
3179 my $itemrecord = GetMarcItem( $bibnum, $itemnum) if ($itemnum);
3181 my $authorised_values_sth =
3183 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
3185 foreach my $tag ( sort keys %{$tagslib} ) {
3186 my $previous_tag = '';
3188 # loop through each subfield
3190 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
3191 next if ( subfield_is_koha_internal_p($subfield) );
3192 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
3194 $subfield_data{tag} = $tag;
3195 $subfield_data{subfield} = $subfield;
3196 $subfield_data{countsubfield} = $cntsubf++;
3197 $subfield_data{kohafield} =
3198 $tagslib->{$tag}->{$subfield}->{'kohafield'};
3200 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
3201 $subfield_data{marc_lib} =
3202 "<span id=\"error\" title=\""
3203 . $tagslib->{$tag}->{$subfield}->{lib} . "\">"
3204 . substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 12 )
3206 $subfield_data{mandatory} =
3207 $tagslib->{$tag}->{$subfield}->{mandatory};
3208 $subfield_data{repeatable} =
3209 $tagslib->{$tag}->{$subfield}->{repeatable};
3210 $subfield_data{hidden} = "display:none"
3211 if $tagslib->{$tag}->{$subfield}->{hidden};
3213 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
3215 $value =~ s/"/"/g;
3217 # search for itemcallnumber if applicable
3218 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
3219 'items.itemcallnumber'
3220 && C4::Context->preference('itemcallnumber') )
3223 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
3225 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
3226 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
3228 $value = $temp->subfield($CNsubfield);
3231 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
3232 my @authorised_values;
3235 # builds list, depending on authorised value...
3237 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
3240 if ( ( C4::Context->preference("IndependantBranches") )
3241 && ( C4::Context->userenv->{flags} != 1 ) )
3245 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
3247 $sth->execute( C4::Context->userenv->{branch} );
3248 push @authorised_values, ""
3250 $tagslib->{$tag}->{$subfield}->{mandatory} );
3251 while ( my ( $branchcode, $branchname ) =
3252 $sth->fetchrow_array )
3254 push @authorised_values, $branchcode;
3255 $authorised_lib{$branchcode} = $branchname;
3261 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
3264 push @authorised_values, ""
3266 $tagslib->{$tag}->{$subfield}->{mandatory} );
3267 while ( my ( $branchcode, $branchname ) =
3268 $sth->fetchrow_array )
3270 push @authorised_values, $branchcode;
3271 $authorised_lib{$branchcode} = $branchname;
3277 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
3282 "SELECT itemtype,description FROM itemtypes ORDER BY description"
3285 push @authorised_values, ""
3286 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
3287 while ( my ( $itemtype, $description ) =
3288 $sth->fetchrow_array )
3290 push @authorised_values, $itemtype;
3291 $authorised_lib{$itemtype} = $description;
3294 #---- "true" authorised value
3297 $authorised_values_sth->execute(
3298 $tagslib->{$tag}->{$subfield}->{authorised_value} );
3299 push @authorised_values, ""
3300 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
3301 while ( my ( $value, $lib ) =
3302 $authorised_values_sth->fetchrow_array )
3304 push @authorised_values, $value;
3305 $authorised_lib{$value} = $lib;
3308 $subfield_data{marc_value} = CGI::scrolling_list(
3309 -name => 'field_value',
3310 -values => \@authorised_values,
3311 -default => "$value",
3312 -labels => \%authorised_lib,
3318 elsif ( $tagslib->{$tag}->{$subfield}->{thesaurus_category} ) {
3319 $subfield_data{marc_value} =
3320 "<input type=\"text\" name=\"field_value\" size=47 maxlength=255> <a href=\"javascript:Dopop('cataloguing/thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=',)\">...</a>";
3323 # COMMENTED OUT because No $i is provided with this API.
3324 # And thus, no value_builder can be activated.
3325 # BUT could be thought over.
3326 # } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
3327 # my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
3329 # my $extended_param = plugin_parameters($dbh,$itemrecord,$tagslib,$i,0);
3330 # my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
3331 # $subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name()\" OnBlur=\"javascript:Blur$function_name()\"> <a href=\"javascript:Clic$function_name()\">...</a> $javascript";
3334 $subfield_data{marc_value} =
3335 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
3337 push( @loop_data, \%subfield_data );
3341 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
3342 if ( $itemrecord && $itemrecord->field($itemtagfield) );
3344 'itemtagfield' => $itemtagfield,
3345 'itemtagsubfield' => $itemtagsubfield,
3346 'itemnumber' => $itemnumber,
3347 'iteminformation' => \@loop_data
3353 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
3355 # replaced by a zebraqueue table, that is filled with ModZebra to run.
3356 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
3357 # =head2 ModZebrafiles
3359 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
3363 # sub ModZebrafiles {
3365 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
3369 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
3370 # unless ( opendir( DIR, "$zebradir" ) ) {
3371 # warn "$zebradir not found";
3375 # my $filename = $zebradir . $biblionumber;
3378 # open( OUTPUT, ">", $filename . ".xml" );
3379 # print OUTPUT $record;
3388 ModZebra( $biblionumber, $op, $server, $newRecord );
3390 $biblionumber is the biblionumber we want to index
3391 $op is specialUpdate or delete, and is used to know what we want to do
3392 $server is the server that we want to update
3393 $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.
3400 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
3401 my ( $biblionumber, $op, $server, $newRecord ) = @_;
3402 my $dbh=C4::Context->dbh;
3404 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
3406 # replaced by a zebraqueue table, that is filled with ModZebra to run.
3407 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
3409 if (C4::Context->preference("NoZebra")) {
3410 # lock the nozebra table : we will read index lines, update them in Perl process
3411 # and write everything in 1 transaction.
3412 # lock the table to avoid someone else overwriting what we are doing
3413 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE');
3414 my %result; # the result hash that will be builded by deletion / add, and written on mySQL at the end, to improve speed
3416 if ($server eq 'biblioserver') {
3417 $record= GetMarcBiblio($biblionumber);
3419 $record= C4::AuthoritiesMarc::GetAuthority($biblionumber);
3421 if ($op eq 'specialUpdate') {
3422 # OK, we have to add or update the record
3423 # 1st delete (virtually, in indexes), if record actually exists
3425 %result = _DelBiblioNoZebra($biblionumber,$record,$server);
3427 # ... add the record
3428 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
3430 # it's a deletion, delete the record...
3431 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
3432 %result=_DelBiblioNoZebra($biblionumber,$record,$server);
3434 # ok, now update the database...
3435 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
3436 foreach my $key (keys %result) {
3437 foreach my $index (keys %{$result{$key}}) {
3438 $sth->execute($result{$key}->{$index}, $server, $key, $index);
3441 $dbh->do('UNLOCK TABLES');
3445 # we use zebra, just fill zebraqueue table
3447 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
3448 $sth->execute($biblionumber,$server,$op);
3453 =head2 GetNoZebraIndexes
3455 %indexes = GetNoZebraIndexes;
3457 return the data from NoZebraIndexes syspref.
3461 sub GetNoZebraIndexes {
3462 my $index = C4::Context->preference('NoZebraIndexes');
3464 foreach my $line (split /('|"),/,$index) {
3465 $line =~ /(.*)=>(.*)/;
3466 my $index = substr($1,1); # get the index, don't forget to remove initial ' or "
3468 $index =~ s/'|"|\s//g;
3471 $fields =~ s/'|"|\s//g;
3472 $indexes{$index}=$fields;
3477 =head1 INTERNAL FUNCTIONS
3479 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
3481 function to delete a biblio in NoZebra indexes
3482 This function does NOT delete anything in database : it reads all the indexes entries
3483 that have to be deleted & delete them in the hash
3484 The SQL part is done either :
3485 - after the Add if we are modifying a biblio (delete + add again)
3486 - immediatly after this sub if we are doing a true deletion.
3487 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
3492 sub _DelBiblioNoZebra {
3493 my ($biblionumber, $record, $server)=@_;
3496 my $dbh = C4::Context->dbh;
3500 if ($server eq 'biblioserver') {
3501 %index=GetNoZebraIndexes;
3502 # get title of the record (to store the 10 first letters with the index)
3503 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
3504 $title = lc($record->subfield($titletag,$titlesubfield));
3506 # for authorities, the "title" is the $a mainentry
3507 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
3508 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
3509 $title = $record->subfield($authref->{auth_tag_to_report},'a');
3510 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
3511 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
3512 $index{'auth_type'} = '152b';
3516 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
3517 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
3518 # limit to 10 char, should be enough, and limit the DB size
3519 $title = substr($title,0,10);
3521 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
3522 foreach my $field ($record->fields()) {
3523 #parse each subfield
3524 next if $field->tag <10;
3525 foreach my $subfield ($field->subfields()) {
3526 my $tag = $field->tag();
3527 my $subfieldcode = $subfield->[0];
3529 # check each index to see if the subfield is stored somewhere
3530 # otherwise, store it in __RAW__ index
3531 foreach my $key (keys %index) {
3532 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
3533 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
3535 my $line= lc $subfield->[1];
3536 # remove meaningless value in the field...
3537 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
3538 # ... and split in words
3539 foreach (split / /,$line) {
3540 next unless $_; # skip empty values (multiple spaces)
3541 # if the entry is already here, do nothing, the biblionumber has already be removed
3542 unless ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
3543 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
3544 $sth2->execute($server,$key,$_);
3545 my $existing_biblionumbers = $sth2->fetchrow;
3547 if ($existing_biblionumbers) {
3548 # warn " existing for $key $_: $existing_biblionumbers";
3549 $result{$key}->{$_} =$existing_biblionumbers;
3550 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
3556 # the subfield is not indexed, store it in __RAW__ index anyway
3558 my $line= lc $subfield->[1];
3559 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
3560 # ... and split in words
3561 foreach (split / /,$line) {
3562 next unless $_; # skip empty values (multiple spaces)
3563 # if the entry is already here, do nothing, the biblionumber has already be removed
3564 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
3565 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
3566 $sth2->execute($server,'__RAW__',$_);
3567 my $existing_biblionumbers = $sth2->fetchrow;
3569 if ($existing_biblionumbers) {
3570 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
3571 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
3581 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
3583 function to add a biblio in NoZebra indexes
3587 sub _AddBiblioNoZebra {
3588 my ($biblionumber, $record, $server, %result)=@_;
3589 my $dbh = C4::Context->dbh;
3593 if ($server eq 'biblioserver') {
3594 %index=GetNoZebraIndexes;
3595 # get title of the record (to store the 10 first letters with the index)
3596 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
3597 $title = lc($record->subfield($titletag,$titlesubfield));
3599 # warn "server : $server";
3600 # for authorities, the "title" is the $a mainentry
3601 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
3602 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
3603 $title = $record->subfield($authref->{auth_tag_to_report},'a');
3604 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
3605 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
3606 $index{'auth_type'} = '152b';
3609 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
3610 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
3611 # limit to 10 char, should be enough, and limit the DB size
3612 $title = substr($title,0,10);
3614 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
3615 foreach my $field ($record->fields()) {
3616 #parse each subfield
3617 next if $field->tag <10;
3618 foreach my $subfield ($field->subfields()) {
3619 my $tag = $field->tag();
3620 my $subfieldcode = $subfield->[0];
3622 # check each index to see if the subfield is stored somewhere
3623 # otherwise, store it in __RAW__ index
3624 foreach my $key (keys %index) {
3625 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
3626 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
3628 my $line= lc $subfield->[1];
3629 # remove meaningless value in the field...
3630 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
3631 # ... and split in words
3632 foreach (split / /,$line) {
3633 next unless $_; # skip empty values (multiple spaces)
3634 # if the entry is already here, improve weight
3635 # warn "managing $_";
3636 if ($result{$key}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
3638 $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
3639 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
3641 # get the value if it exist in the nozebra table, otherwise, create it
3642 $sth2->execute($server,$key,$_);
3643 my $existing_biblionumbers = $sth2->fetchrow;
3645 if ($existing_biblionumbers) {
3646 $result{$key}->{"$_"} =$existing_biblionumbers;
3648 $result{$key}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
3649 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
3650 # create a new ligne for this entry
3652 # warn "INSERT : $server / $key / $_";
3653 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
3654 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
3660 # the subfield is not indexed, store it in __RAW__ index anyway
3662 my $line= lc $subfield->[1];
3663 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
3664 # ... and split in words
3665 foreach (split / /,$line) {
3666 next unless $_; # skip empty values (multiple spaces)
3667 # if the entry is already here, improve weight
3668 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,$title\-(\d);/) {
3670 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
3671 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
3673 # get the value if it exist in the nozebra table, otherwise, create it
3674 $sth2->execute($server,'__RAW__',$_);
3675 my $existing_biblionumbers = $sth2->fetchrow;
3677 if ($existing_biblionumbers) {
3678 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
3680 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,$title\-(\d);//;
3681 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
3682 # create a new ligne for this entry
3684 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
3685 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
3696 =head2 MARCitemchange
3700 &MARCitemchange( $record, $itemfield, $newvalue )
3702 Function to update a single value in an item field.
3703 Used twice, could probably be replaced by something else, but works well...
3711 sub MARCitemchange {
3712 my ( $record, $itemfield, $newvalue ) = @_;
3713 my $dbh = C4::Context->dbh;
3715 my ( $tagfield, $tagsubfield ) =
3716 GetMarcFromKohaField( $itemfield, "" );
3717 if ( ($tagfield) && ($tagsubfield) ) {
3718 my $tag = $record->field($tagfield);
3720 $tag->update( $tagsubfield => $newvalue );
3721 $record->delete_field($tag);
3722 $record->insert_fields_ordered($tag);
3730 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
3732 Find the given $subfield in the given $tag in the given
3733 MARC::Record $record. If the subfield is found, returns
3734 the (indicators, value) pair; otherwise, (undef, undef) is
3738 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
3739 I suggest we export it from this module.
3746 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
3749 if ( $tagfield < 10 ) {
3750 if ( $record->field($tagfield) ) {
3751 push @result, $record->field($tagfield)->data();
3758 foreach my $field ( $record->field($tagfield) ) {
3759 my @subfields = $field->subfields();
3760 foreach my $subfield (@subfields) {
3761 if ( @$subfield[0] eq $insubfield ) {
3762 push @result, @$subfield[1];
3763 $indicator = $field->indicator(1) . $field->indicator(2);
3768 return ( $indicator, @result );
3771 =head2 _koha_marc_update_bib_ids
3775 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
3777 Internal function to add or update biblionumber and biblioitemnumber to
3784 sub _koha_marc_update_bib_ids {
3785 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
3787 # we must add bibnum and bibitemnum in MARC::Record...
3788 # we build the new field with biblionumber and biblioitemnumber
3789 # we drop the original field
3790 # we add the new builded field.
3791 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
3792 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
3794 if ($biblio_tag != $biblioitem_tag) {
3795 # biblionumber & biblioitemnumber are in different fields
3797 # deal with biblionumber
3798 my ($new_field, $old_field);
3799 if ($biblio_tag < 10) {
3800 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
3803 MARC::Field->new( $biblio_tag, '', '',
3804 "$biblio_subfield" => $biblionumber );
3807 # drop old field and create new one...
3808 $old_field = $record->field($biblio_tag);
3809 $record->delete_field($old_field);
3810 $record->append_fields($new_field);
3812 # deal with biblioitemnumber
3813 if ($biblioitem_tag < 10) {
3814 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
3817 MARC::Field->new( $biblioitem_tag, '', '',
3818 "$biblioitem_subfield" => $biblioitemnumber, );
3820 # drop old field and create new one...
3821 $old_field = $record->field($biblioitem_tag);
3822 $record->delete_field($old_field);
3823 $record->insert_fields_ordered($new_field);
3826 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
3827 my $new_field = MARC::Field->new(
3828 $biblio_tag, '', '',
3829 "$biblio_subfield" => $biblionumber,
3830 "$biblioitem_subfield" => $biblioitemnumber
3833 # drop old field and create new one...
3834 my $old_field = $record->field($biblio_tag);
3835 $record->delete_field($old_field);
3836 $record->insert_fields_ordered($new_field);
3840 =head2 _koha_add_biblio
3844 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
3846 Internal function to add a biblio ($biblio is a hash with the values)
3852 sub _koha_add_biblio {
3853 my ( $dbh, $biblio, $frameworkcode ) = @_;
3857 # set the series flag
3859 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
3863 SET frameworkcode = ?,
3874 my $sth = $dbh->prepare($query);
3877 $biblio->{'author'},
3879 $biblio->{'unititle'},
3882 $biblio->{'seriestitle'},
3883 $biblio->{'copyrightdate'},
3884 $biblio->{'abstract'}
3887 my $biblionumber = $dbh->{'mysql_insertid'};
3888 if ( $dbh->errstr ) {
3889 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
3894 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
3895 return ($biblionumber,$error);
3898 =head2 _koha_modify_biblio
3902 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
3904 Internal function for updating the biblio table
3910 sub _koha_modify_biblio {
3911 my ( $dbh, $biblio, $frameworkcode ) = @_;
3916 SET frameworkcode = ?,
3925 WHERE biblionumber = ?
3928 my $sth = $dbh->prepare($query);
3932 $biblio->{'author'},
3934 $biblio->{'unititle'},
3936 $biblio->{'serial'},
3937 $biblio->{'seriestitle'},
3938 $biblio->{'copyrightdate'},
3939 $biblio->{'abstract'},
3940 $biblio->{'biblionumber'}
3941 ) if $biblio->{'biblionumber'};
3943 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
3944 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
3947 return ( $biblio->{'biblionumber'},$error );
3950 =head2 _koha_modify_biblioitem_nonmarc
3954 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
3956 Updates biblioitems row except for marc and marcxml, which should be changed
3963 sub _koha_modify_biblioitem_nonmarc {
3964 my ( $dbh, $biblioitem ) = @_;
3967 # re-calculate the cn_sort, it may have changed
3968 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3972 SET biblionumber = ?,
3978 publicationyear = ?,
3982 collectiontitle = ?,
3984 collectionvolume= ?,
3985 editionstatement= ?,
3986 editionresponsibility = ?,
4000 where biblioitemnumber = ?
4002 my $sth = $dbh->prepare($query);
4004 $biblioitem->{'biblionumber'},
4005 $biblioitem->{'volume'},
4006 $biblioitem->{'number'},
4007 $biblioitem->{'itemtype'},
4008 $biblioitem->{'isbn'},
4009 $biblioitem->{'issn'},
4010 $biblioitem->{'publicationyear'},
4011 $biblioitem->{'publishercode'},
4012 $biblioitem->{'volumedate'},
4013 $biblioitem->{'volumedesc'},
4014 $biblioitem->{'collectiontitle'},
4015 $biblioitem->{'collectionissn'},
4016 $biblioitem->{'collectionvolume'},
4017 $biblioitem->{'editionstatement'},
4018 $biblioitem->{'editionresponsibility'},
4019 $biblioitem->{'illus'},
4020 $biblioitem->{'pages'},
4021 $biblioitem->{'bnotes'},
4022 $biblioitem->{'size'},
4023 $biblioitem->{'place'},
4024 $biblioitem->{'lccn'},
4025 $biblioitem->{'url'},
4026 $biblioitem->{'biblioitems.cn_source'},
4027 $biblioitem->{'cn_class'},
4028 $biblioitem->{'cn_item'},
4029 $biblioitem->{'cn_suffix'},
4031 $biblioitem->{'totalissues'},
4032 $biblioitem->{'biblioitemnumber'}
4034 if ( $dbh->errstr ) {
4035 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
4038 return ($biblioitem->{'biblioitemnumber'},$error);
4041 =head2 _koha_add_biblioitem
4045 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
4047 Internal function to add a biblioitem
4053 sub _koha_add_biblioitem {
4054 my ( $dbh, $biblioitem ) = @_;
4057 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
4059 "INSERT INTO biblioitems SET
4066 publicationyear = ?,
4070 collectiontitle = ?,
4072 collectionvolume= ?,
4073 editionstatement= ?,
4074 editionresponsibility = ?,
4090 my $sth = $dbh->prepare($query);
4092 $biblioitem->{'biblionumber'},
4093 $biblioitem->{'volume'},
4094 $biblioitem->{'number'},
4095 $biblioitem->{'itemtype'},
4096 $biblioitem->{'isbn'},
4097 $biblioitem->{'issn'},
4098 $biblioitem->{'publicationyear'},
4099 $biblioitem->{'publishercode'},
4100 $biblioitem->{'volumedate'},
4101 $biblioitem->{'volumedesc'},
4102 $biblioitem->{'collectiontitle'},
4103 $biblioitem->{'collectionissn'},
4104 $biblioitem->{'collectionvolume'},
4105 $biblioitem->{'editionstatement'},
4106 $biblioitem->{'editionresponsibility'},
4107 $biblioitem->{'illus'},
4108 $biblioitem->{'pages'},
4109 $biblioitem->{'bnotes'},
4110 $biblioitem->{'size'},
4111 $biblioitem->{'place'},
4112 $biblioitem->{'lccn'},
4113 $biblioitem->{'marc'},
4114 $biblioitem->{'url'},
4115 $biblioitem->{'biblioitems.cn_source'},
4116 $biblioitem->{'cn_class'},
4117 $biblioitem->{'cn_item'},
4118 $biblioitem->{'cn_suffix'},
4120 $biblioitem->{'totalissues'}
4122 my $bibitemnum = $dbh->{'mysql_insertid'};
4123 if ( $dbh->errstr ) {
4124 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
4128 return ($bibitemnum,$error);
4131 =head2 _koha_new_items
4135 my ($itemnumber,$error) = _koha_new_items( $dbh, $item, $barcode );
4141 sub _koha_new_items {
4142 my ( $dbh, $item, $barcode ) = @_;
4145 my ($items_cn_sort) = GetClassSort($item->{'items.cn_source'}, $item->{'itemcallnumber'}, "");
4147 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
4148 if ( $item->{'dateaccessioned'} eq '' || !$item->{'dateaccessioned'} ) {
4149 my $today = C4::Dates->new();
4150 $item->{'dateaccessioned'} = $today->output("iso"); #TODO: check time issues
4153 "INSERT INTO items SET
4155 biblioitemnumber = ?,
4157 dateaccessioned = ?,
4161 replacementprice = ?,
4162 replacementpricedate = NOW(),
4163 datelastborrowed = ?,
4164 datelastseen = NOW(),
4187 my $sth = $dbh->prepare($query);
4189 $item->{'biblionumber'},
4190 $item->{'biblioitemnumber'},
4192 $item->{'dateaccessioned'},
4193 $item->{'booksellerid'},
4194 $item->{'homebranch'},
4196 $item->{'replacementprice'},
4197 $item->{datelastborrowed},
4199 $item->{'notforloan'},
4201 $item->{'itemlost'},
4202 $item->{'wthdrawn'},
4203 $item->{'itemcallnumber'},
4204 $item->{'restricted'},
4205 $item->{'itemnotes'},
4206 $item->{'holdingbranch'},
4208 $item->{'location'},
4211 $item->{'renewals'},
4212 $item->{'reserves'},
4213 $item->{'items.cn_source'},
4217 $item->{'materials'},
4220 my $itemnumber = $dbh->{'mysql_insertid'};
4221 if ( defined $sth->errstr ) {
4222 $error.="ERROR in _koha_new_items $query".$sth->errstr;
4225 return ( $itemnumber, $error );
4228 =head2 _koha_modify_item
4232 my ($itemnumber,$error) =_koha_modify_item( $dbh, $item, $op );
4238 sub _koha_modify_item {
4239 my ( $dbh, $item ) = @_;
4242 # calculate items.cn_sort
4243 if($item->{'itemcallnumber'}) {
4244 # This works, even when user is setting the call number blank (in which case
4245 # how would we get here to calculate new (blank) of items.cn_sort?).
4247 # Why? Because at present the only way to update itemcallnumber is via
4248 # additem.pl; since it uses a MARC data-entry form, TransformMarcToKoha
4249 # already has created $item->{'items.cn_sort'} and set it to undef because the
4250 # subfield for items.cn_sort in the framework is specified as ignored, meaning
4251 # that it is not supplied or passed to the form. Thus, if the user has
4252 # blanked itemcallnumber, there is already a undef value for $item->{'items.cn_sort'}.
4254 # This is subtle; it is also fragile.
4255 $item->{'items.cn_sort'} = GetClassSort($item->{'items.cn_source'}, $item->{'itemcallnumber'}, "");
4257 my $query = "UPDATE items SET ";
4259 for my $key ( keys %$item ) {
4261 push @bind, $item->{$key};
4264 $query .= " WHERE itemnumber=?";
4265 push @bind, $item->{'itemnumber'};
4266 my $sth = $dbh->prepare($query);
4267 $sth->execute(@bind);
4268 if ( $dbh->errstr ) {
4269 $error.="ERROR in _koha_modify_item $query".$dbh->errstr;
4273 return ($item->{'itemnumber'},$error);
4276 =head2 _koha_delete_biblio
4280 $error = _koha_delete_biblio($dbh,$biblionumber);
4282 Internal sub for deleting from biblio table -- also saves to deletedbiblio
4284 C<$dbh> - the database handle
4285 C<$biblionumber> - the biblionumber of the biblio to be deleted
4291 # FIXME: add error handling
4293 sub _koha_delete_biblio {
4294 my ( $dbh, $biblionumber ) = @_;
4296 # get all the data for this biblio
4297 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
4298 $sth->execute($biblionumber);
4300 if ( my $data = $sth->fetchrow_hashref ) {
4302 # save the record in deletedbiblio
4303 # find the fields to save
4304 my $query = "INSERT INTO deletedbiblio SET ";
4306 foreach my $temp ( keys %$data ) {
4307 $query .= "$temp = ?,";
4308 push( @bind, $data->{$temp} );
4311 # replace the last , by ",?)"
4313 my $bkup_sth = $dbh->prepare($query);
4314 $bkup_sth->execute(@bind);
4318 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
4319 $del_sth->execute($biblionumber);
4326 =head2 _koha_delete_biblioitems
4330 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
4332 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
4334 C<$dbh> - the database handle
4335 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
4341 # FIXME: add error handling
4343 sub _koha_delete_biblioitems {
4344 my ( $dbh, $biblioitemnumber ) = @_;
4346 # get all the data for this biblioitem
4348 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
4349 $sth->execute($biblioitemnumber);
4351 if ( my $data = $sth->fetchrow_hashref ) {
4353 # save the record in deletedbiblioitems
4354 # find the fields to save
4355 my $query = "INSERT INTO deletedbiblioitems SET ";
4357 foreach my $temp ( keys %$data ) {
4358 $query .= "$temp = ?,";
4359 push( @bind, $data->{$temp} );
4362 # replace the last , by ",?)"
4364 my $bkup_sth = $dbh->prepare($query);
4365 $bkup_sth->execute(@bind);
4368 # delete the biblioitem
4370 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
4371 $del_sth->execute($biblioitemnumber);
4378 =head2 _koha_delete_item
4382 _koha_delete_item( $dbh, $itemnum );
4384 Internal function to delete an item record from the koha tables
4390 sub _koha_delete_item {
4391 my ( $dbh, $itemnum ) = @_;
4393 # save the deleted item to deleteditems table
4394 my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?");
4395 $sth->execute($itemnum);
4396 my $data = $sth->fetchrow_hashref();
4398 my $query = "INSERT INTO deleteditems SET ";
4400 foreach my $key ( keys %$data ) {
4401 $query .= "$key = ?,";
4402 push( @bind, $data->{$key} );
4405 $sth = $dbh->prepare($query);
4406 $sth->execute(@bind);
4409 # delete from items table
4410 $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?");
4411 $sth->execute($itemnum);
4416 =head1 UNEXPORTED FUNCTIONS
4418 =head2 ModBiblioMarc
4420 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
4422 Add MARC data for a biblio to koha
4424 Function exported, but should NOT be used, unless you really know what you're doing
4430 # pass the MARC::Record to this function, and it will create the records in the marc field
4431 my ( $record, $biblionumber, $frameworkcode ) = @_;
4432 my $dbh = C4::Context->dbh;
4433 my @fields = $record->fields();
4434 if ( !$frameworkcode ) {
4435 $frameworkcode = "";
4438 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
4439 $sth->execute( $frameworkcode, $biblionumber );
4441 my $encoding = C4::Context->preference("marcflavour");
4443 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
4444 if ( $encoding eq "UNIMARC" ) {
4446 if ( length($record->subfield( 100, "a" )) == 35 ) {
4447 $string = $record->subfield( 100, "a" );
4448 my $f100 = $record->field(100);
4449 $record->delete_field($f100);
4452 $string = POSIX::strftime( "%Y%m%d", localtime );
4454 $string = sprintf( "%-*s", 35, $string );
4456 substr( $string, 22, 6, "frey50" );
4457 unless ( $record->subfield( 100, "a" ) ) {
4458 $record->insert_grouped_field(
4459 MARC::Field->new( 100, "", "", "a" => $string ) );
4462 ModZebra($biblionumber,"specialUpdate","biblioserver",$record);
4465 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
4466 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
4469 return $biblionumber;
4472 =head2 AddItemInMarc
4476 $newbiblionumber = AddItemInMarc( $record, $biblionumber, $frameworkcode );
4478 Add an item in a MARC record and save the MARC record
4480 Function exported, but should NOT be used, unless you really know what you're doing
4488 # pass the MARC::Record to this function, and it will create the records in the marc tables
4489 my ( $record, $biblionumber, $frameworkcode ) = @_;
4490 my $newrec = &GetMarcBiblio($biblionumber);
4493 my @fields = $record->fields();
4494 foreach my $field (@fields) {
4495 $newrec->append_fields($field);
4498 # FIXME: should we be making sure the biblionumbers are the same?
4499 my $newbiblionumber =
4500 &ModBiblioMarc( $newrec, $biblionumber, $frameworkcode );
4501 return $newbiblionumber;
4504 =head2 z3950_extended_services
4506 z3950_extended_services($serviceType,$serviceOptions,$record);
4508 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.
4510 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
4512 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
4514 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
4518 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
4519 syntax => the record syntax (transfer syntax)
4520 databaseName = Database from connection object
4522 To set serviceOptions, call set_service_options($serviceType)
4524 C<$record> the record, if one is needed for the service type
4526 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
4530 sub z3950_extended_services {
4531 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
4533 # get our connection object
4534 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
4536 # create a new package object
4537 my $Zpackage = $Zconn->package();
4540 $Zpackage->option( action => $action );
4542 if ( $serviceOptions->{'databaseName'} ) {
4543 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
4545 if ( $serviceOptions->{'recordIdNumber'} ) {
4547 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
4549 if ( $serviceOptions->{'recordIdOpaque'} ) {
4551 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
4554 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
4555 #if ($serviceType eq 'itemorder') {
4556 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
4557 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
4558 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
4559 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
4562 if ( $serviceOptions->{record} ) {
4563 $Zpackage->option( record => $serviceOptions->{record} );
4565 # can be xml or marc
4566 if ( $serviceOptions->{'syntax'} ) {
4567 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
4571 # send the request, handle any exception encountered
4572 eval { $Zpackage->send($serviceType) };
4573 if ( $@ && $@->isa("ZOOM::Exception") ) {
4574 return "error: " . $@->code() . " " . $@->message() . "\n";
4577 # free up package resources
4578 $Zpackage->destroy();
4581 =head2 set_service_options
4583 my $serviceOptions = set_service_options($serviceType);
4585 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
4587 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
4591 sub set_service_options {
4592 my ($serviceType) = @_;
4595 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
4596 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
4598 if ( $serviceType eq 'commit' ) {
4602 if ( $serviceType eq 'create' ) {
4606 if ( $serviceType eq 'drop' ) {
4607 die "ERROR: 'drop' not currently supported (by Zebra)";
4609 return $serviceOptions;
4612 =head2 GetItemsCount
4614 $count = &GetItemsCount( $biblionumber);
4615 this function return count of item with $biblionumber
4619 my ( $biblionumber ) = @_;
4620 my $dbh = C4::Context->dbh;
4621 my $query = "SELECT count(*)
4623 WHERE biblionumber=?";
4624 my $sth = $dbh->prepare($query);
4625 $sth->execute($biblionumber);
4626 my $count = $sth->fetchrow;
4631 END { } # module clean-up code here (global destructor)
4639 Koha Developement team <info@koha.org>
4641 Paul POULAIN paul.poulain@free.fr
4643 Joshua Ferraro jmf@liblime.com