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
25 use MARC::File::USMARC;
31 use C4::Log; # logaction
33 use vars qw($VERSION @ISA @EXPORT);
35 # set the version for version checking
36 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); };
38 @ISA = qw( Exporter );
42 # to add biblios or items
43 push @EXPORT, qw( &AddBiblio &AddItem );
51 &GetBiblioItemByBiblioNumber
52 &GetBiblioFromItemNumber
69 &GetItemnumberFromBarcode
73 &GetAuthorisedValueDesc
88 &ModItemInMarconefield
99 # those functions are exported but should not be used
100 # they are usefull is few circumstances, so are exported.
101 # but don't use them unless you're a core developer ;-)
114 &PrepareItemrecordDisplay
121 C4::Biblio - cataloging management functions
125 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:
129 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
131 =item 2. as raw MARC in the Zebra index and storage engine
133 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
137 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
139 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.
143 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
145 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
149 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:
153 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
155 =item 2. _koha_* - low-level internal functions for managing the koha tables
157 =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.
159 =item 4. Zebra functions used to update the Zebra index
161 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
165 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 :
169 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
171 =item 2. add the biblionumber and biblioitemnumber into the MARC records
173 =item 3. save the marc record
177 When dealing with items, we must :
181 =item 1. save the item in items table, that gives us an itemnumber
183 =item 2. add the itemnumber to the item MARC field
185 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
187 When modifying a biblio or an item, the behaviour is quite similar.
191 =head1 EXPORTED FUNCTIONS
197 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
198 Exported function (core API) for adding a new biblio to koha.
205 my ( $record, $frameworkcode ) = @_;
207 my $biblioitemnumber;
208 my $dbh = C4::Context->dbh;
209 # transform the data into koha-table style data
210 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
211 $biblionumber = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
212 $olddata->{'biblionumber'} = $biblionumber;
213 $biblioitemnumber = _koha_add_biblioitem( $dbh, $olddata );
215 # we must add bibnum and bibitemnum in MARC::Record...
216 # we build the new field with biblionumber and biblioitemnumber
217 # we drop the original field
218 # we add the new builded field.
219 ( my $biblio_tag, my $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
220 ( my $biblioitem_tag, my $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
224 # biblionumber & biblioitemnumber are in different fields
225 if ( $biblio_tag != $biblioitem_tag ) {
227 # deal with biblionumber
228 if ( $biblio_tag < 10 ) {
229 $newfield = MARC::Field->new( $biblio_tag, $biblionumber );
233 MARC::Field->new( $biblio_tag, '', '',
234 "$biblio_subfield" => $biblionumber );
237 # drop old field and create new one...
238 my $old_field = $record->field($biblio_tag);
239 $record->delete_field($old_field);
240 $record->append_fields($newfield);
242 # deal with biblioitemnumber
243 if ( $biblioitem_tag < 10 ) {
244 $newfield = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
248 MARC::Field->new( $biblioitem_tag, '', '',
249 "$biblioitem_subfield" => $biblioitemnumber, );
251 # drop old field and create new one...
252 $old_field = $record->field($biblioitem_tag);
253 $record->delete_field($old_field);
254 $record->insert_fields_ordered($newfield);
256 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
259 my $newfield = MARC::Field->new(
261 "$biblio_subfield" => $biblionumber,
262 "$biblioitem_subfield" => $biblioitemnumber
265 # drop old field and create new one...
266 my $old_field = $record->field($biblio_tag);
267 $record->delete_field($old_field);
268 $record->insert_fields_ordered($newfield);
273 ModBiblioMarc( $record, $biblionumber, $frameworkcode );
275 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$biblionumber,"biblio")
276 if C4::Context->preference("CataloguingLog");
278 return ( $biblionumber, $biblioitemnumber );
285 $biblionumber = AddItem( $record, $biblionumber)
286 Exported function (core API) for adding a new item to Koha
293 my ( $record, $biblionumber ) = @_;
294 my $dbh = C4::Context->dbh;
297 my $frameworkcode = GetFrameworkCode( $biblionumber );
298 my $item = &TransformMarcToKoha( $dbh, $record, $frameworkcode );
300 # needs old biblionumber and biblioitemnumber
301 $item->{'biblionumber'} = $biblionumber;
304 "select biblioitemnumber,itemtype from biblioitems where biblionumber=?"
306 $sth->execute( $item->{'biblionumber'} );
308 ( $item->{'biblioitemnumber'}, $itemtype ) = $sth->fetchrow;
311 "select notforloan from itemtypes where itemtype='$itemtype'");
313 my $notforloan = $sth->fetchrow;
314 ##Change the notforloan field if $notforloan found
315 if ( $notforloan > 0 ) {
316 $item->{'notforloan'} = $notforloan;
317 &MARCitemchange( $record, "items.notforloan", $notforloan );
319 if ( !$item->{'dateaccessioned'} || $item->{'dateaccessioned'} eq '' ) {
322 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
327 "$year-" . sprintf( "%0.2d", $mon ) . "-" . sprintf( "%0.2d", $mday );
328 $item->{'dateaccessioned'} = $date;
329 &MARCitemchange( $record, "items.dateaccessioned", $date );
331 my ( $itemnumber, $error ) =
332 &_koha_new_items( $dbh, $item, $item->{barcode} );
334 # add itemnumber to MARC::Record before adding the item.
337 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
339 &TransformKohaToMarcOneField( $sth, $record, "items.itemnumber", $itemnumber,
343 &AddItemInMarc( $record, $item->{'biblionumber'},$frameworkcode );
345 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$itemnumber,"item")
346 if C4::Context->preference("CataloguingLog");
348 return ($item->{biblionumber}, $item->{biblioitemnumber},$itemnumber);
355 ModBiblio( $record,$biblionumber,$frameworkcode);
356 Exported function (core API) to modify a biblio
363 my ( $record, $biblionumber, $frameworkcode ) = @_;
365 if (C4::Context->preference("CataloguingLog")) {
366 my $newrecord = GetMarcBiblio($biblionumber);
367 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$biblionumber,$newrecord->as_formatted)
370 my $dbh = C4::Context->dbh;
372 $frameworkcode = "" unless $frameworkcode;
374 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
375 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
376 my $oldRecord = GetMarcBiblio($biblionumber);
377 my @fields = $oldRecord->field($itemtag);
378 $record->append_fields(@fields);
380 # update the MARC record (that now contains biblio and items) with the new record data
381 &ModBiblioMarc($record, $biblionumber, $frameworkcode );
383 # load the koha-table data object
384 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
386 # modify the other koha tables
387 my $biblionumber = _koha_modify_biblio( $dbh, $oldbiblio );
388 _koha_modify_biblioitem( $dbh, $oldbiblio );
397 Exported function (core API) for modifying an item in Koha.
404 my ( $record, $biblionumber, $itemnumber, $delete, $new_item_hashref )
408 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$itemnumber,$record->as_formatted)
409 if C4::Context->preference("CataloguingLog");
411 my $dbh = C4::Context->dbh;
413 # if we have a MARC record, we're coming from cataloging and so
414 # we do the whole routine: update the MARC and zebra, then update the koha
417 my $frameworkcode = GetFrameworkCode( $biblionumber );
418 ModItemInMarc( $record, $biblionumber, $itemnumber, $frameworkcode );
419 my $olditem = TransformMarcToKoha( $dbh, $record, $frameworkcode );
420 _koha_modify_item( $dbh, $olditem );
421 return $biblionumber;
424 # otherwise, we're just looking to modify something quickly
425 # (like a status) so we just update the koha tables
426 elsif ($new_item_hashref) {
427 _koha_modify_item( $dbh, $new_item_hashref );
431 sub ModItemTransfer {
432 my ( $itemnumber, $frombranch, $tobranch ) = @_;
434 my $dbh = C4::Context->dbh;
436 #new entry in branchtransfers....
437 my $sth = $dbh->prepare(
438 "INSERT INTO branchtransfers (itemnumber, frombranch, datesent, tobranch)
439 VALUES (?, ?, now(), $?)");
440 $sth->execute($itemnumber, $frombranch, $tobranch);
441 #update holdingbranch in items .....
443 "UPDATE items set holdingbranch = ? WHERE items.itemnumber = ?");
444 $sth->execute($tobranch,$itemnumber);
445 &ModDateLastSeen($itemnumber);
446 $sth = $dbh->prepare(
447 "SELECT biblionumber FROM items WHERE itemnumber=?"
449 $sth->execute($itemnumber);
450 while ( my ( $biblionumber ) = $sth->fetchrow ) {
451 &ModItemInMarconefield( $biblionumber, $itemnumber,
452 'items.holdingbranch', $tobranch );
457 ##New sub to dotransfer in marc tables as well. Not exported -TG 10/04/2006
458 # sub domarctransfer {
459 # my ( $dbh, $itemnumber ) = @_;
460 # $itemnumber =~ s /\'//g; ##itemnumber seems to come with quotes-TG
463 # "select biblionumber,holdingbranch from items where itemnumber=$itemnumber"
466 # while ( my ( $biblionumber, $holdingbranch ) = $sth->fetchrow ) {
467 # &ModItemInMarconefield( $biblionumber, $itemnumber,
468 # 'items.holdingbranch', $holdingbranch );
472 =head2 ModBiblioframework
476 ModBiblioframework($biblionumber,$frameworkcode);
477 Exported function to modify a biblio framework
483 sub ModBiblioframework {
484 my ( $biblionumber, $frameworkcode ) = @_;
485 my $dbh = C4::Context->dbh;
488 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=$biblionumber");
489 $sth->execute($frameworkcode);
493 =head2 ModItemInMarconefield
497 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)
498 &ModItemInMarconefield( $biblionumber, $itemnumber, $itemfield, $newvalue )
504 sub ModItemInMarconefield {
505 my ( $biblionumber, $itemnumber, $itemfield, $newvalue ) = @_;
506 my $dbh = C4::Context->dbh;
507 if ( !defined $newvalue ) {
511 my $record = GetMarcItem( $biblionumber, $itemnumber );
512 my ($tagfield, $tagsubfield) = GetMarcFromKohaField( $itemfield,'');
513 if ($tagfield && $tagsubfield) {
514 my $tag = $record->field($tagfield);
516 # my $tagsubs = $record->field($tagfield)->subfield($tagsubfield);
517 $tag->update( $tagsubfield => $newvalue );
518 $record->delete_field($tag);
519 $record->insert_fields_ordered($tag);
520 &ModItemInMarc( $record, $biblionumber, $itemnumber, 0 );
529 &ModItemInMarc( $record, $biblionumber, $itemnumber )
536 my ( $ItemRecord, $biblionumber, $itemnumber, $frameworkcode) = @_;
537 my $dbh = C4::Context->dbh;
539 # get complete MARC record & replace the item field by the new one
540 my $completeRecord = GetMarcBiblio($biblionumber);
541 my ($itemtag,$itemsubfield) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
542 my $itemField = $ItemRecord->field($itemtag);
543 my @items = $completeRecord->field($itemtag);
545 if ($_->subfield($itemsubfield) eq $itemnumber) {
546 # $completeRecord->delete_field($_);
547 $_->replace_with($itemField);
551 my $sth = $dbh->prepare("update biblioitems set marc=?,marcxml=? where biblionumber=?");
552 $sth->execute( $completeRecord->as_usmarc(), $completeRecord->as_xml_record(),$biblionumber );
554 ModZebra($biblionumber,"specialUpdate","biblioserver",$completeRecord);
557 =head2 ModDateLastSeen
559 &ModDateLastSeen($itemnum)
560 Mark item as seen. Is called when an item is issued, returned or manually marked during inventory/stocktaking
561 C<$itemnum> is the item number
565 sub ModDateLastSeen {
567 my $dbh = C4::Context->dbh;
570 "update items set itemlost=0, datelastseen = now() where items.itemnumber = ?"
572 $sth->execute($itemnum);
579 my $error = &DelBiblio($dbh,$biblionumber);
580 Exported function (core API) for deleting a biblio in koha.
581 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
582 Also backs it up to deleted* tables
583 Checks to make sure there are not issues on any of the items
585 C<$error> : undef unless an error occurs
592 my ( $biblionumber ) = @_;
593 my $dbh = C4::Context->dbh;
594 my $error; # for error handling
596 # First make sure there are no items with issues are still attached
599 "SELECT itemnumber FROM items WHERE biblionumber=?");
600 $sth->execute($biblionumber);
601 while ( my $itemnumber = $sth->fetchrow ) {
602 my $issues = GetItemIssues($itemnumber);
603 foreach my $issue (@$issues) {
604 if ( ( $issue->{date_due} )
605 && ( $issue->{date_due} ne "Available" ) )
608 #FIXME: we need a status system in Biblio like in Circ to return standard codes and messages
609 # instead of hard-coded strings
611 "Item is checked out to a patron -- you must return it before deleting the Biblio";
615 return $error if $error;
617 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
618 # for at least 2 reasons :
619 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
620 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
621 # 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)
622 ModZebra($biblionumber, "delete_record", "biblioserver", undef);
624 # delete biblio from Koha tables and save in deletedbiblio
625 $error = &_koha_delete_biblio( $dbh, $biblionumber );
627 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
630 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
631 $sth->execute($biblionumber);
632 while ( my $biblioitemnumber = $sth->fetchrow ) {
634 # delete this biblioitem
635 $error = &_koha_delete_biblioitems( $dbh, $biblioitemnumber );
636 return $error if $error;
641 "SELECT itemnumber FROM items WHERE biblioitemnumber=?");
642 $items_sth->execute($biblioitemnumber);
643 while ( my $itemnumber = $items_sth->fetchrow ) {
644 $error = &_koha_delete_item( $dbh, $itemnumber );
645 return $error if $error;
648 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$biblionumber,"")
649 if C4::Context->preference("CataloguingLog");
657 DelItem( $biblionumber, $itemnumber );
658 Exported function (core API) for deleting an item record in Koha.
665 my ( $biblionumber, $itemnumber ) = @_;
666 my $dbh = C4::Context->dbh;
667 &_koha_delete_item( $dbh, $itemnumber );
668 # get the MARC record
669 my $record = GetMarcBiblio($biblionumber);
670 my $frameworkcode = GetFrameworkCode($biblionumber);
674 $dbh->prepare("UPDATE deleteditems SET marc=? WHERE itemnumber=?");
675 $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
677 #search item field code
678 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
679 my @fields = $record->field($itemtag);
680 # delete the item specified
681 foreach my $field (@fields) {
682 if ( $field->subfield($itemsubfield) eq $itemnumber ) {
683 $record->delete_field($field);
686 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
687 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$itemnumber,"item")
688 if C4::Context->preference("CataloguingLog");
695 $data = &GetBiblioData($biblionumber);
696 Returns information about the book with the given biblionumber.
697 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
698 the C<biblio> and C<biblioitems> tables in the
700 In addition, C<$data-E<gt>{subject}> is the list of the book's
701 subjects, separated by C<" , "> (space, comma, space).
702 If there are multiple biblioitems with the given biblionumber, only
703 the first one is considered.
711 my $dbh = C4::Context->dbh;
714 SELECT * , biblioitems.notes AS bnotes, biblio.notes
716 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
717 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
718 WHERE biblio.biblionumber = ?
719 AND biblioitems.biblionumber = biblio.biblionumber
721 my $sth = $dbh->prepare($query);
722 $sth->execute($bibnum);
724 $data = $sth->fetchrow_hashref;
728 } # sub GetBiblioData
735 @results = &GetItemsInfo($biblionumber, $type);
737 Returns information about books with the given biblionumber.
739 C<$type> may be either C<intra> or anything else. If it is not set to
740 C<intra>, then the search will exclude lost, very overdue, and
743 C<&GetItemsInfo> returns a list of references-to-hash. Each element
744 contains a number of keys. Most of them are table items from the
745 C<biblio>, C<biblioitems>, C<items>, and C<itemtypes> tables in the
746 Koha database. Other keys include:
750 =item C<$data-E<gt>{branchname}>
752 The name (not the code) of the branch to which the book belongs.
754 =item C<$data-E<gt>{datelastseen}>
756 This is simply C<items.datelastseen>, except that while the date is
757 stored in YYYY-MM-DD format in the database, here it is converted to
758 DD/MM/YYYY format. A NULL date is returned as C<//>.
760 =item C<$data-E<gt>{datedue}>
762 =item C<$data-E<gt>{class}>
764 This is the concatenation of C<biblioitems.classification>, the book's
765 Dewey code, and C<biblioitems.subclass>.
767 =item C<$data-E<gt>{ocount}>
769 I think this is the number of copies of the book available.
771 =item C<$data-E<gt>{order}>
773 If this is set, it is set to C<One Order>.
782 my ( $biblionumber, $type ) = @_;
783 my $dbh = C4::Context->dbh;
784 my $query = "SELECT *,items.notforloan as itemnotforloan
785 FROM items, biblio, biblioitems
786 LEFT JOIN itemtypes on biblioitems.itemtype = itemtypes.itemtype
787 WHERE items.biblionumber = ?
788 AND biblioitems.biblioitemnumber = items.biblioitemnumber
789 AND biblio.biblionumber = items.biblionumber
790 ORDER BY items.dateaccessioned desc
792 my $sth = $dbh->prepare($query);
793 $sth->execute($biblionumber);
796 my ( $date_due, $count_reserves );
798 while ( my $data = $sth->fetchrow_hashref ) {
800 my $isth = $dbh->prepare(
801 "SELECT issues.*,borrowers.cardnumber
802 FROM issues, borrowers
804 AND returndate IS NULL
805 AND issues.borrowernumber=borrowers.borrowernumber"
807 $isth->execute( $data->{'itemnumber'} );
808 if ( my $idata = $isth->fetchrow_hashref ) {
809 $data->{borrowernumber} = $idata->{borrowernumber};
810 $data->{cardnumber} = $idata->{cardnumber};
811 $datedue = format_date( $idata->{'date_due'} );
813 if ( $datedue eq '' ) {
814 #$datedue="Available";
815 my ( $restype, $reserves ) =
816 C4::Reserves::CheckReserves( $data->{'itemnumber'} );
820 $count_reserves = $restype;
825 #get branch information.....
826 my $bsth = $dbh->prepare(
827 "SELECT * FROM branches WHERE branchcode = ?
830 $bsth->execute( $data->{'holdingbranch'} );
831 if ( my $bdata = $bsth->fetchrow_hashref ) {
832 $data->{'branchname'} = $bdata->{'branchname'};
834 my $date = format_date( $data->{'datelastseen'} );
835 $data->{'datelastseen'} = $date;
836 $data->{'datedue'} = $datedue;
837 $data->{'count_reserves'} = $count_reserves;
839 # get notforloan complete status if applicable
840 my $sthnflstatus = $dbh->prepare(
841 'SELECT authorised_value
842 FROM marc_subfield_structure
843 WHERE kohafield="items.notforloan"
847 $sthnflstatus->execute;
848 my ($authorised_valuecode) = $sthnflstatus->fetchrow;
849 if ($authorised_valuecode) {
850 $sthnflstatus = $dbh->prepare(
851 "SELECT lib FROM authorised_values
853 AND authorised_value=?"
855 $sthnflstatus->execute( $authorised_valuecode,
856 $data->{itemnotforloan} );
857 my ($lib) = $sthnflstatus->fetchrow;
858 $data->{notforloan} = $lib;
861 # my stack procedures
862 my $stackstatus = $dbh->prepare(
863 'SELECT authorised_value
864 FROM marc_subfield_structure
865 WHERE kohafield="items.stack"
868 $stackstatus->execute;
870 ($authorised_valuecode) = $stackstatus->fetchrow;
871 if ($authorised_valuecode) {
872 $stackstatus = $dbh->prepare(
874 FROM authorised_values
876 AND authorised_value=?
879 $stackstatus->execute( $authorised_valuecode, $data->{stack} );
880 my ($lib) = $stackstatus->fetchrow;
881 $data->{stack} = $lib;
883 $results[$i] = $data;
895 $itemstatushash = &getitemstatus($fwkcode);
896 returns information about status.
897 Can be MARC dependant.
899 But basically could be can be loan or not
900 Create a status selector with the following code
902 =head3 in PERL SCRIPT
904 my $itemstatushash = getitemstatus;
906 foreach my $thisstatus (keys %$itemstatushash) {
907 my %row =(value => $thisstatus,
908 statusname => $itemstatushash->{$thisstatus}->{'statusname'},
910 push @itemstatusloop, \%row;
912 $template->param(statusloop=>\@itemstatusloop);
917 <select name="statusloop">
918 <option value="">Default</option>
919 <!-- TMPL_LOOP name="statusloop" -->
920 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="statusname" --></option>
928 # returns a reference to a hash of references to status...
931 my $dbh = C4::Context->dbh;
933 $fwk = '' unless ($fwk);
934 my ( $tag, $subfield ) =
935 GetMarcFromKohaField( "items.notforloan", $fwk );
936 if ( $tag and $subfield ) {
939 "select authorised_value from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
941 $sth->execute( $tag, $subfield, $fwk );
942 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
945 "select authorised_value, lib from authorised_values where category=? order by lib"
947 $authvalsth->execute($authorisedvaluecat);
948 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
949 $itemstatus{$authorisedvalue} = $lib;
965 $itemstatus{"1"} = "Not For Loan";
969 =head2 getitemlocation
973 $itemlochash = &getitemlocation($fwk);
974 returns informations about location.
975 where fwk stands for an optional framework code.
976 Create a location selector with the following code
978 =head3 in PERL SCRIPT
980 my $itemlochash = getitemlocation;
982 foreach my $thisloc (keys %$itemlochash) {
983 my $selected = 1 if $thisbranch eq $branch;
984 my %row =(locval => $thisloc,
985 selected => $selected,
986 locname => $itemlochash->{$thisloc},
988 push @itemlocloop, \%row;
990 $template->param(itemlocationloop => \@itemlocloop);
994 <select name="location">
995 <option value="">Default</option>
996 <!-- TMPL_LOOP name="itemlocationloop" -->
997 <option value="<!-- TMPL_VAR name="locval" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="locname" --></option>
1005 sub GetItemLocation {
1007 # returns a reference to a hash of references to location...
1010 my $dbh = C4::Context->dbh;
1012 $fwk = '' unless ($fwk);
1013 my ( $tag, $subfield ) =
1014 GetMarcFromKohaField( "items.location", $fwk );
1015 if ( $tag and $subfield ) {
1018 "select authorised_value from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
1020 $sth->execute( $tag, $subfield, $fwk );
1021 if ( my ($authorisedvaluecat) = $sth->fetchrow ) {
1024 "select authorised_value, lib from authorised_values where category=? order by lib"
1026 $authvalsth->execute($authorisedvaluecat);
1027 while ( my ( $authorisedvalue, $lib ) = $authvalsth->fetchrow ) {
1028 $itemlocation{$authorisedvalue} = $lib;
1030 $authvalsth->finish;
1031 return \%itemlocation;
1044 $itemlocation{"1"} = "Not For Loan";
1045 return \%itemlocation;
1050 $items = GetLostItems($where,$orderby);
1052 This function get the items lost into C<$items>.
1057 C<$where> is a hashref. it containts a field of the items table as key
1058 and the value to match as value.
1059 C<$orderby> is a field of the items table.
1062 C<$items> is a reference to an array full of hasref which keys are items' table column.
1064 =item usage in the perl script:
1067 $where{barcode} = 0001548;
1068 my $items = GetLostItems( \%where, "homebranch" );
1069 $template->param(itemsloop => $items);
1076 # Getting input args.
1078 my $orderby = shift;
1079 my $dbh = C4::Context->dbh;
1084 WHERE itemlost IS NOT NULL
1087 foreach my $key (keys %$where) {
1088 $query .= " AND " . $key . " LIKE '%" . $where->{$key} . "%'";
1090 $query .= " ORDER BY ".$orderby if defined $orderby;
1092 my $sth = $dbh->prepare($query);
1095 while ( my $row = $sth->fetchrow_hashref ){
1101 =head2 GetItemsForInventory
1103 $itemlist = GetItemsForInventory($minlocation,$maxlocation,$datelastseen,$offset,$size)
1105 Retrieve a list of title/authors/barcode/callnumber, for biblio inventory.
1107 The sub returns a list of hashes, containing itemnumber, author, title, barcode & item callnumber.
1108 It is ordered by callnumber,title.
1110 The minlocation & maxlocation parameters are used to specify a range of item callnumbers
1111 the datelastseen can be used to specify that you want to see items not seen since a past date only.
1112 offset & size can be used to retrieve only a part of the whole listing (defaut behaviour)
1116 sub GetItemsForInventory {
1117 my ( $minlocation, $maxlocation, $datelastseen, $branch, $offset, $size ) = @_;
1118 my $dbh = C4::Context->dbh;
1120 if ($datelastseen) {
1122 "SELECT itemnumber,barcode,itemcallnumber,title,author,datelastseen
1124 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1125 WHERE itemcallnumber>= ?
1126 AND itemcallnumber <=?
1127 AND (datelastseen< ? OR datelastseen IS NULL)";
1128 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
1129 $query .= " ORDER BY itemcallnumber,title";
1130 $sth = $dbh->prepare($query);
1131 $sth->execute( $minlocation, $maxlocation, $datelastseen );
1135 SELECT itemnumber,barcode,itemcallnumber,title,author,datelastseen
1137 LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
1138 WHERE itemcallnumber>= ?
1139 AND itemcallnumber <=?";
1140 $query.= " AND items.homebranch=".$dbh->quote($branch) if $branch;
1141 $query .= " ORDER BY itemcallnumber,title";
1142 $sth = $dbh->prepare($query);
1143 $sth->execute( $minlocation, $maxlocation );
1146 while ( my $row = $sth->fetchrow_hashref ) {
1147 $offset-- if ($offset);
1148 if ( ( !$offset ) && $size ) {
1149 push @results, $row;
1156 =head2 &GetBiblioItemData
1160 $itemdata = &GetBiblioItemData($biblioitemnumber);
1162 Looks up the biblioitem with the given biblioitemnumber. Returns a
1163 reference-to-hash. The keys are the fields from the C<biblio>,
1164 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
1165 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
1172 sub GetBiblioItemData {
1174 my $dbh = C4::Context->dbh;
1177 "Select *,biblioitems.notes as bnotes from biblioitems, biblio,itemtypes where biblio.biblionumber = biblioitems.biblionumber and biblioitemnumber = ? and biblioitems.itemtype = itemtypes.itemtype"
1181 $sth->execute($bibitem);
1183 $data = $sth->fetchrow_hashref;
1187 } # sub &GetBiblioItemData
1189 =head2 GetItemnumberFromBarcode
1193 $result = GetItemnumberFromBarcode($barcode);
1199 sub GetItemnumberFromBarcode {
1201 my $dbh = C4::Context->dbh;
1204 $dbh->prepare("SELECT itemnumber from items where items.barcode=?");
1205 $rq->execute($barcode);
1206 my ($result) = $rq->fetchrow;
1210 =head2 GetBiblioItemByBiblioNumber
1214 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
1220 sub GetBiblioItemByBiblioNumber {
1221 my ($biblionumber) = @_;
1222 my $dbh = C4::Context->dbh;
1223 my $sth = $dbh->prepare("Select * from biblioitems where biblionumber = ?");
1227 $sth->execute($biblionumber);
1229 while ( my $data = $sth->fetchrow_hashref ) {
1230 push @results, $data;
1237 =head2 GetBiblioFromItemNumber
1241 $item = &GetBiblioFromItemNumber($itemnumber);
1243 Looks up the item with the given itemnumber.
1245 C<&itemnodata> returns a reference-to-hash whose keys are the fields
1246 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
1254 sub GetBiblioFromItemNumber {
1255 my ( $itemnumber ) = @_;
1256 my $dbh = C4::Context->dbh;
1257 my $sth = $dbh->prepare(
1258 "SELECT * FROM biblio,items,biblioitems
1259 WHERE items.itemnumber = ?
1260 AND biblio.biblionumber = items.biblionumber
1261 AND biblioitems.biblioitemnumber = items.biblioitemnumber"
1264 $sth->execute($itemnumber);
1265 my $data = $sth->fetchrow_hashref;
1274 ( $count, @results ) = &GetBiblio($biblionumber);
1281 my ($biblionumber) = @_;
1282 my $dbh = C4::Context->dbh;
1283 my $sth = $dbh->prepare("Select * from biblio where biblionumber = ?");
1286 $sth->execute($biblionumber);
1287 while ( my $data = $sth->fetchrow_hashref ) {
1288 $results[$count] = $data;
1292 return ( $count, @results );
1299 $data = &GetItem($itemnumber,$barcode);
1301 return Item information, for a given itemnumber or barcode
1308 my ($itemnumber,$barcode) = @_;
1309 my $dbh = C4::Context->dbh;
1311 my $sth = $dbh->prepare("
1313 WHERE itemnumber = ?");
1314 $sth->execute($itemnumber);
1315 my $data = $sth->fetchrow_hashref;
1318 my $sth = $dbh->prepare("
1322 $sth->execute($barcode);
1323 my $data = $sth->fetchrow_hashref;
1328 =head2 get_itemnumbers_of
1332 my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
1334 Given a list of biblionumbers, return the list of corresponding itemnumbers
1335 for each biblionumber.
1337 Return a reference on a hash where keys are biblionumbers and values are
1338 references on array of itemnumbers.
1344 sub get_itemnumbers_of {
1345 my @biblionumbers = @_;
1347 my $dbh = C4::Context->dbh;
1353 WHERE biblionumber IN (?' . ( ',?' x scalar @biblionumbers - 1 ) . ')
1355 my $sth = $dbh->prepare($query);
1356 $sth->execute(@biblionumbers);
1360 while ( my ( $itemnumber, $biblionumber ) = $sth->fetchrow_array ) {
1361 push @{ $itemnumbers_of{$biblionumber} }, $itemnumber;
1364 return \%itemnumbers_of;
1367 =head2 GetItemInfosOf
1371 GetItemInfosOf(@itemnumbers);
1377 sub GetItemInfosOf {
1378 my @itemnumbers = @_;
1383 WHERE itemnumber IN (' . join( ',', @itemnumbers ) . ')
1385 return get_infos_of( $query, 'itemnumber' );
1388 =head2 GetBiblioItemInfosOf
1392 GetBiblioItemInfosOf(@biblioitemnumbers);
1398 sub GetBiblioItemInfosOf {
1399 my @biblioitemnumbers = @_;
1402 SELECT biblioitemnumber,
1406 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
1408 return get_infos_of( $query, 'biblioitemnumber' );
1411 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
1413 =head2 GetMarcStructure
1417 $res = GetMarcStructure($dbh,$forlibrarian,$frameworkcode);
1419 Returns a reference to a big hash of hash, with the Marc structure fro the given frameworkcode
1421 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
1422 $frameworkcode : the framework code to read
1430 sub GetMarcStructure {
1431 my ( $dbh, $forlibrarian, $frameworkcode ) = @_;
1432 $frameworkcode = "" unless $frameworkcode;
1434 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
1436 # check that framework exists
1439 "select count(*) from marc_tag_structure where frameworkcode=?");
1440 $sth->execute($frameworkcode);
1441 my ($total) = $sth->fetchrow;
1442 $frameworkcode = "" unless ( $total > 0 );
1445 "select tagfield,liblibrarian,libopac,mandatory,repeatable from marc_tag_structure where frameworkcode=? order by tagfield"
1447 $sth->execute($frameworkcode);
1448 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
1450 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
1453 $res->{$tag}->{lib} =
1454 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
1455 # why the hell do we need to explicitly decode utf8 ?
1456 # that's a good question, but we must do it...
1458 utf8::decode($res->{$tag}->{lib});
1459 # warn "$liblibrarian";
1460 $res->{$tab}->{tab} = ""; # XXX
1461 $res->{$tag}->{mandatory} = $mandatory;
1462 $res->{$tag}->{repeatable} = $repeatable;
1467 "select tagfield,tagsubfield,liblibrarian,libopac,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue from marc_subfield_structure where frameworkcode=? order by tagfield,tagsubfield"
1469 $sth->execute($frameworkcode);
1472 my $authorised_value;
1484 $tag, $subfield, $liblibrarian,
1486 $mandatory, $repeatable, $authorised_value,
1487 $authtypecode, $value_builder, $kohafield,
1488 $seealso, $hidden, $isurl,
1494 $res->{$tag}->{$subfield}->{lib} =
1495 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
1496 $res->{$tag}->{$subfield}->{tab} = $tab;
1497 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
1498 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
1499 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
1500 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
1501 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
1502 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
1503 $res->{$tag}->{$subfield}->{seealso} = $seealso;
1504 $res->{$tag}->{$subfield}->{hidden} = $hidden;
1505 $res->{$tag}->{$subfield}->{isurl} = $isurl;
1506 $res->{$tag}->{$subfield}->{link} = $link;
1507 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
1512 =head2 GetMarcFromKohaField
1516 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1517 Returns the MARC fields & subfields mapped to the koha field
1518 for the given frameworkcode
1524 sub GetMarcFromKohaField {
1525 my ( $kohafield, $frameworkcode ) = @_;
1526 return 0, 0 unless $kohafield;
1527 my $relations = C4::Context->marcfromkohafield;
1529 $relations->{$frameworkcode}->{$kohafield}->[0],
1530 $relations->{$frameworkcode}->{$kohafield}->[1]
1534 =head2 GetMarcBiblio
1538 Returns MARC::Record of the biblionumber passed in parameter.
1539 the marc record contains both biblio & item datas
1546 my $biblionumber = shift;
1547 my $dbh = C4::Context->dbh;
1549 $dbh->prepare("select marcxml from biblioitems where biblionumber=? ");
1550 $sth->execute($biblionumber);
1551 my ($marcxml) = $sth->fetchrow;
1552 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
1553 $marcxml =~ s/\x1e//g;
1554 $marcxml =~ s/\x1f//g;
1555 $marcxml =~ s/\x1d//g;
1556 $marcxml =~ s/\x0f//g;
1557 $marcxml =~ s/\x0c//g;
1558 my $record = MARC::Record->new();
1559 $record = MARC::Record::new_from_xml( $marcxml, "utf8",C4::Context->preference('marcflavour')) if $marcxml;
1567 my $marcxml = GetXmlBiblio($biblionumber);
1569 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1570 The XML contains both biblio & item datas
1577 my ( $biblionumber ) = @_;
1578 my $dbh = C4::Context->dbh;
1580 $dbh->prepare("select marcxml from biblioitems where biblionumber=? ");
1581 $sth->execute($biblionumber);
1582 my ($marcxml) = $sth->fetchrow;
1586 =head2 GetAuthorisedValueDesc
1590 my $subfieldvalue =get_authorised_value_desc(
1591 $tag, $subf[$i][0],$subf[$i][1], '', $taglib);
1592 Retrieve the complete description for a given authorised value.
1598 sub GetAuthorisedValueDesc {
1599 my ( $tag, $subfield, $value, $framework, $tagslib ) = @_;
1600 my $dbh = C4::Context->dbh;
1603 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1604 return C4::Branch::GetBranchName($value);
1608 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1609 return getitemtypeinfo($value);
1612 #---- "true" authorized value
1613 my $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1615 if ( $category ne "" ) {
1618 "select lib from authorised_values where category = ? and authorised_value = ?"
1620 $sth->execute( $category, $value );
1621 my $data = $sth->fetchrow_hashref;
1622 return $data->{'lib'};
1625 return $value; # if nothing is found return the original value
1633 Returns MARC::Record of the item passed in parameter.
1640 my ( $biblionumber, $itemnumber ) = @_;
1641 my $dbh = C4::Context->dbh;
1642 my $newrecord = MARC::Record->new();
1643 my $marcflavour = C4::Context->preference('marcflavour');
1645 my $marcxml = GetXmlBiblio($biblionumber);
1646 my $record = MARC::Record->new();
1647 $record = MARC::Record::new_from_xml( $marcxml, "utf8", $marcflavour );
1648 # now, find where the itemnumber is stored & extract only the item
1649 my ( $itemnumberfield, $itemnumbersubfield ) =
1650 GetMarcFromKohaField( 'items.itemnumber', '' );
1651 my @fields = $record->field($itemnumberfield);
1652 foreach my $field (@fields) {
1653 if ( $field->subfield($itemnumbersubfield) eq $itemnumber ) {
1654 $newrecord->insert_fields_ordered($field);
1666 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1667 Get all notes from the MARC record and returns them in an array.
1668 The note are stored in differents places depending on MARC flavour
1675 my ( $record, $marcflavour ) = @_;
1677 if ( $marcflavour eq "MARC21" ) {
1680 else { # assume unimarc if not marc21
1687 foreach my $field ( $record->field($scope) ) {
1688 my $value = $field->as_string();
1689 if ( $note ne "" ) {
1690 $marcnote = { marcnote => $note, };
1691 push @marcnotes, $marcnote;
1694 if ( $note ne $value ) {
1695 $note = $note . " " . $value;
1700 $marcnote = { marcnote => $note };
1701 push @marcnotes, $marcnote; #load last tag into array
1704 } # end GetMarcNotes
1706 =head2 GetMarcSubjects
1710 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1711 Get all subjects from the MARC record and returns them in an array.
1712 The subjects are stored in differents places depending on MARC flavour
1718 sub GetMarcSubjects {
1719 my ( $record, $marcflavour ) = @_;
1720 my ( $mintag, $maxtag );
1721 if ( $marcflavour eq "MARC21" ) {
1725 else { # assume unimarc if not marc21
1732 foreach my $field ( $record->fields ) {
1733 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1734 my @subfields = $field->subfields();
1738 for my $subject_subfield ( @subfields ) {
1739 my $code = $subject_subfield->[0];
1740 $label .= $subject_subfield->[1] . " and su-to:" unless ( $code == 9 );
1742 $link = "Koha-Auth-Number:".$subject_subfield->[1];
1747 $link =~ s/ and\ssu-to:$//;
1760 return \@marcsubjcts;
1761 } #end GetMarcSubjects
1763 =head2 GetMarcAuthors
1767 authors = GetMarcAuthors($record,$marcflavour);
1768 Get all authors from the MARC record and returns them in an array.
1769 The authors are stored in differents places depending on MARC flavour
1775 sub GetMarcAuthors {
1776 my ( $record, $marcflavour ) = @_;
1777 my ( $mintag, $maxtag );
1778 if ( $marcflavour eq "MARC21" ) {
1782 else { # assume unimarc if not marc21
1789 foreach my $field ( $record->fields ) {
1790 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1792 my @subfields = $field->subfields();
1795 for my $authors_subfield (@subfields) {
1796 if ($count_auth ne '0'){
1800 my $subfieldcode = $authors_subfield->[0];
1801 my $value = $authors_subfield->[1];
1802 $hash{'tag'} = $field->tag;
1803 $hash{value} .= $value . " " if ($subfieldcode != 9) ;
1804 $hash{link} .= $value if ($subfieldcode eq 9);
1806 push @marcauthors, \%hash;
1808 return \@marcauthors;
1811 =head2 GetMarcSeries
1815 $marcseriessarray = GetMarcSeries($record,$marcflavour);
1816 Get all series from the MARC record and returns them in an array.
1817 The series are stored in differents places depending on MARC flavour
1824 my ($record, $marcflavour) = @_;
1825 my ($mintag, $maxtag);
1826 if ($marcflavour eq "MARC21") {
1829 } else { # assume unimarc if not marc21
1839 foreach my $field ($record->field('440'), $record->field('490')) {
1841 #my $value = $field->subfield('a');
1842 #$marcsubjct = {MARCSUBJCT => $value,};
1843 my @subfields = $field->subfields();
1844 #warn "subfields:".join " ", @$subfields;
1847 for my $series_subfield (@subfields) {
1849 undef $volume_number;
1850 # see if this is an instance of a volume
1851 if ($series_subfield->[0] eq 'v') {
1855 my $code = $series_subfield->[0];
1856 my $value = $series_subfield->[1];
1857 my $linkvalue = $value;
1858 $linkvalue =~ s/(\(|\))//g;
1859 my $operator = " and " unless $counter==0;
1860 push @link_loop, {link => $linkvalue, operator => $operator };
1861 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1862 if ($volume_number) {
1863 push @subfields_loop, {volumenum => $value};
1866 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1870 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1871 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1872 #push @marcsubjcts, $marcsubjct;
1876 my $marcseriessarray=\@marcseries;
1877 return $marcseriessarray;
1878 } #end getMARCseriess
1880 =head2 GetFrameworkCode
1884 $frameworkcode = GetFrameworkCode( $biblionumber )
1890 sub GetFrameworkCode {
1891 my ( $biblionumber ) = @_;
1892 my $dbh = C4::Context->dbh;
1894 $dbh->prepare("select frameworkcode from biblio where biblionumber=?");
1895 $sth->execute($biblionumber);
1896 my ($frameworkcode) = $sth->fetchrow;
1897 return $frameworkcode;
1900 =head2 TransformKohaToMarc
1904 $record = TransformKohaToMarc( $hash )
1905 This function builds partial MARC::Record from a hash
1906 Hash entries can be from biblio or biblioitems.
1907 This function is called in acquisition module, to create a basic catalogue entry from user entry
1913 sub TransformKohaToMarc {
1916 my $dbh = C4::Context->dbh;
1919 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
1921 my $record = MARC::Record->new();
1922 foreach (keys %{$hash}) {
1923 &TransformKohaToMarcOneField( $sth, $record, $_,
1929 =head2 TransformKohaToMarcOneField
1933 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1939 sub TransformKohaToMarcOneField {
1940 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1941 $frameworkcode='' unless $frameworkcode;
1945 if ( !defined $sth ) {
1946 my $dbh = C4::Context->dbh;
1949 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
1952 $sth->execute( $frameworkcode, $kohafieldname );
1953 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1954 my $tag = $record->field($tagfield);
1956 $tag->update( $tagsubfield => $value );
1957 $record->delete_field($tag);
1958 $record->insert_fields_ordered($tag);
1961 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1967 =head2 TransformHtmlToXml
1971 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag )
1977 sub TransformHtmlToXml {
1978 my ( $tags, $subfields, $values, $indicator, $ind_tag ) = @_;
1979 my $xml = MARC::File::XML::header('UTF-8');
1980 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
1981 MARC::File::XML->default_record_format('UNIMARC');
1982 use POSIX qw(strftime);
1983 my $string = strftime( "%Y%m%d", localtime(time) );
1984 $string = sprintf( "%-*s", 35, $string );
1985 substr( $string, 22, 6, "frey50" );
1986 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1987 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1988 $xml .= "</datafield>\n";
1994 for ( my $i = 0 ; $i <= @$tags ; $i++ ) {
1995 @$values[$i] =~ s/&/&/g;
1996 @$values[$i] =~ s/</</g;
1997 @$values[$i] =~ s/>/>/g;
1998 @$values[$i] =~ s/"/"/g;
1999 @$values[$i] =~ s/'/'/g;
2000 if ( !utf8::is_utf8( @$values[$i] ) ) {
2001 utf8::decode( @$values[$i] );
2003 if ( ( @$tags[$i] ne $prevtag ) ) {
2004 $j++ unless ( @$tags[$i] eq "" );
2006 $xml .= "</datafield>\n";
2007 if ( ( @$tags[$i] && @$tags[$i] > 10 )
2008 && ( @$values[$i] ne "" ) )
2010 my $ind1 = substr( @$indicator[$j], 0, 1 );
2012 if ( @$indicator[$j] ) {
2013 $ind2 = substr( @$indicator[$j], 1, 1 );
2016 warn "Indicator in @$tags[$i] is empty";
2020 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2022 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2030 if ( @$values[$i] ne "" ) {
2033 if ( @$tags[$i] eq "000" ) {
2034 $xml .= "<leader>@$values[$i]</leader>\n";
2037 # rest of the fixed fields
2039 elsif ( @$tags[$i] < 10 ) {
2041 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
2045 my $ind1 = substr( @$indicator[$j], 0, 1 );
2046 my $ind2 = substr( @$indicator[$j], 1, 1 );
2048 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2050 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2056 else { # @$tags[$i] eq $prevtag
2057 if ( @$values[$i] eq "" ) {
2061 my $ind1 = substr( @$indicator[$j], 0, 1 );
2062 my $ind2 = substr( @$indicator[$j], 1, 1 );
2064 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
2068 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
2071 $prevtag = @$tags[$i];
2073 $xml .= MARC::File::XML::footer();
2078 =head2 TransformHtmlToMarc
2082 $record = TransformHtmlToMarc( $dbh, $rtags, $rsubfields, $rvalues, %indicators )
2088 sub TransformHtmlToMarc {
2089 my ( $dbh, $rtags, $rsubfields, $rvalues, %indicators ) = @_;
2091 my $record = MARC::Record->new();
2093 # my %subfieldlist=();
2094 my $prevvalue; # if tag <10
2095 my $field; # if tag >=10
2096 for ( my $i = 0 ; $i < @$rtags ; $i++ ) {
2097 next unless @$rvalues[$i];
2099 # rebuild MARC::Record
2100 # warn "0=>".@$rtags[$i].@$rsubfields[$i]." = ".@$rvalues[$i].": ";
2101 if ( @$rtags[$i] ne $prevtag ) {
2102 if ( $prevtag < 10 ) {
2105 if ( $prevtag ne '000' ) {
2106 $record->insert_fields_ordered(
2107 ( sprintf "%03s", $prevtag ), $prevvalue );
2111 $record->leader($prevvalue);
2118 $record->insert_fields_ordered($field);
2121 $indicators{ @$rtags[$i] } .= ' ';
2122 if ( @$rtags[$i] < 10 ) {
2123 $prevvalue = @$rvalues[$i];
2128 $field = MARC::Field->new(
2129 ( sprintf "%03s", @$rtags[$i] ),
2130 substr( $indicators{ @$rtags[$i] }, 0, 1 ),
2131 substr( $indicators{ @$rtags[$i] }, 1, 1 ),
2132 @$rsubfields[$i] => @$rvalues[$i]
2135 $prevtag = @$rtags[$i];
2138 if ( @$rtags[$i] < 10 ) {
2139 $prevvalue = @$rvalues[$i];
2142 if ( length( @$rvalues[$i] ) > 0 ) {
2143 $field->add_subfields( @$rsubfields[$i] => @$rvalues[$i] );
2146 $prevtag = @$rtags[$i];
2150 # the last has not been included inside the loop... do it now !
2151 $record->insert_fields_ordered($field) if $field;
2153 $record->encoding('UTF-8');
2155 # $record->MARC::File::USMARC::update_leader();
2159 =head2 TransformMarcToKoha
2163 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2169 sub TransformMarcToKoha {
2170 my ( $dbh, $record, $frameworkcode ) = @_;
2173 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
2176 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
2179 while ( ($field) = $sth2->fetchrow ) {
2181 &TransformMarcToKohaOneField( "biblio", $field, $record, $result,
2184 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
2186 while ( ($field) = $sth2->fetchrow ) {
2187 if ( $field eq 'notes' ) { $field = 'bnotes'; }
2189 &TransformMarcToKohaOneField( "biblioitems", $field, $record, $result,
2192 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
2194 while ( ($field) = $sth2->fetchrow ) {
2196 &TransformMarcToKohaOneField( "items", $field, $record, $result,
2201 # modify copyrightdate to keep only the 1st year found
2202 my $temp = $result->{'copyrightdate'};
2203 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
2205 $result->{'copyrightdate'} = $1;
2207 else { # if no cYYYY, get the 1st date.
2208 $temp =~ m/(\d\d\d\d)/;
2209 $result->{'copyrightdate'} = $1;
2212 # modify publicationyear to keep only the 1st year found
2213 $temp = $result->{'publicationyear'};
2214 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
2216 $result->{'publicationyear'} = $1;
2218 else { # if no cYYYY, get the 1st date.
2219 $temp =~ m/(\d\d\d\d)/;
2220 $result->{'publicationyear'} = $1;
2225 =head2 TransformMarcToKohaOneField
2229 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2235 sub TransformMarcToKohaOneField {
2237 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
2238 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2241 my ( $tagfield, $subfield ) =
2242 GetMarcFromKohaField( $kohatable . "." . $kohafield,
2244 foreach my $field ( $record->field($tagfield) ) {
2245 if ( $field->tag() < 10 ) {
2246 if ( $result->{$kohafield} ) {
2247 $result->{$kohafield} .= " | " . $field->data();
2250 $result->{$kohafield} = $field->data();
2254 if ( $field->subfields ) {
2255 my @subfields = $field->subfields();
2256 foreach my $subfieldcount ( 0 .. $#subfields ) {
2257 if ( $subfields[$subfieldcount][0] eq $subfield ) {
2258 if ( $result->{$kohafield} ) {
2259 $result->{$kohafield} .=
2260 " | " . $subfields[$subfieldcount][1];
2263 $result->{$kohafield} =
2264 $subfields[$subfieldcount][1];
2273 =head1 OTHER FUNCTIONS
2279 my $string = char_decode( $string, $encoding );
2281 converts ISO 5426 coded string to UTF-8
2282 sloppy code : should be improved in next issue
2289 my ( $string, $encoding ) = @_;
2292 $encoding = C4::Context->preference("marcflavour") unless $encoding;
2293 if ( $encoding eq "UNIMARC" ) {
2363 # this handles non-sorting blocks (if implementation requires this)
2364 $string = nsb_clean($_);
2366 elsif ( $encoding eq "USMARC" || $encoding eq "MARC21" ) {
2425 #Additional Turkish characters
2428 s/(\xf0)s/\xc5\x9f/gm;
2429 s/(\xf0)S/\xc5\x9e/gm;
2432 s/\xe7\x49/\\xc4\xb0/gm;
2433 s/(\xe6)G/\xc4\x9e/gm;
2434 s/(\xe6)g/ğ\xc4\x9f/gm;
2437 s/(\xe8|\xc8)o/ö/gm;
2438 s/(\xe8|\xc8)O/Ö/gm;
2439 s/(\xe8|\xc8)u/ü/gm;
2440 s/(\xe8|\xc8)U/Ü/gm;
2441 s/\xc2\xb8/\xc4\xb1/gm;
2444 # this handles non-sorting blocks (if implementation requires this)
2445 $string = nsb_clean($_);
2454 my $string = nsb_clean( $string, $encoding );
2461 my $NSB = '\x88'; # NSB : begin Non Sorting Block
2462 my $NSE = '\x89'; # NSE : Non Sorting Block end
2463 # handles non sorting blocks
2467 s/[ ]{0,1}$NSE/) /gm;
2472 =head2 PrepareItemrecordDisplay
2476 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
2478 Returns a hash with all the fields for Display a given item data in a template
2484 sub PrepareItemrecordDisplay {
2486 my ( $bibnum, $itemnum ) = @_;
2488 my $dbh = C4::Context->dbh;
2489 my $frameworkcode = &GetFrameworkCode( $bibnum );
2490 my ( $itemtagfield, $itemtagsubfield ) =
2491 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2492 my $tagslib = &GetMarcStructure( $dbh, 1, $frameworkcode );
2493 my $itemrecord = GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2495 my $authorised_values_sth =
2497 "select authorised_value,lib from authorised_values where category=? order by lib"
2499 foreach my $tag ( sort keys %{$tagslib} ) {
2500 my $previous_tag = '';
2502 # loop through each subfield
2504 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2505 next if ( subfield_is_koha_internal_p($subfield) );
2506 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2508 $subfield_data{tag} = $tag;
2509 $subfield_data{subfield} = $subfield;
2510 $subfield_data{countsubfield} = $cntsubf++;
2511 $subfield_data{kohafield} =
2512 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2514 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2515 $subfield_data{marc_lib} =
2516 "<span id=\"error\" title=\""
2517 . $tagslib->{$tag}->{$subfield}->{lib} . "\">"
2518 . substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 12 )
2520 $subfield_data{mandatory} =
2521 $tagslib->{$tag}->{$subfield}->{mandatory};
2522 $subfield_data{repeatable} =
2523 $tagslib->{$tag}->{$subfield}->{repeatable};
2524 $subfield_data{hidden} = "display:none"
2525 if $tagslib->{$tag}->{$subfield}->{hidden};
2527 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2529 $value =~ s/"/"/g;
2531 # search for itemcallnumber if applicable
2532 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2533 'items.itemcallnumber'
2534 && C4::Context->preference('itemcallnumber') )
2537 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2539 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2540 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2542 $value = $temp->subfield($CNsubfield);
2545 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2546 my @authorised_values;
2549 # builds list, depending on authorised value...
2551 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2554 if ( ( C4::Context->preference("IndependantBranches") )
2555 && ( C4::Context->userenv->{flags} != 1 ) )
2559 "select branchcode,branchname from branches where branchcode = ? order by branchname"
2561 $sth->execute( C4::Context->userenv->{branch} );
2562 push @authorised_values, ""
2564 $tagslib->{$tag}->{$subfield}->{mandatory} );
2565 while ( my ( $branchcode, $branchname ) =
2566 $sth->fetchrow_array )
2568 push @authorised_values, $branchcode;
2569 $authorised_lib{$branchcode} = $branchname;
2575 "select branchcode,branchname from branches order by branchname"
2578 push @authorised_values, ""
2580 $tagslib->{$tag}->{$subfield}->{mandatory} );
2581 while ( my ( $branchcode, $branchname ) =
2582 $sth->fetchrow_array )
2584 push @authorised_values, $branchcode;
2585 $authorised_lib{$branchcode} = $branchname;
2591 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2596 "select itemtype,description from itemtypes order by description"
2599 push @authorised_values, ""
2600 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2601 while ( my ( $itemtype, $description ) =
2602 $sth->fetchrow_array )
2604 push @authorised_values, $itemtype;
2605 $authorised_lib{$itemtype} = $description;
2608 #---- "true" authorised value
2611 $authorised_values_sth->execute(
2612 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2613 push @authorised_values, ""
2614 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2615 while ( my ( $value, $lib ) =
2616 $authorised_values_sth->fetchrow_array )
2618 push @authorised_values, $value;
2619 $authorised_lib{$value} = $lib;
2622 $subfield_data{marc_value} = CGI::scrolling_list(
2623 -name => 'field_value',
2624 -values => \@authorised_values,
2625 -default => "$value",
2626 -labels => \%authorised_lib,
2632 elsif ( $tagslib->{$tag}->{$subfield}->{thesaurus_category} ) {
2633 $subfield_data{marc_value} =
2634 "<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>";
2637 # COMMENTED OUT because No $i is provided with this API.
2638 # And thus, no value_builder can be activated.
2639 # BUT could be thought over.
2640 # } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
2641 # my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
2643 # my $extended_param = plugin_parameters($dbh,$itemrecord,$tagslib,$i,0);
2644 # my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
2645 # $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";
2648 $subfield_data{marc_value} =
2649 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
2651 push( @loop_data, \%subfield_data );
2655 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2656 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2658 'itemtagfield' => $itemtagfield,
2659 'itemtagsubfield' => $itemtagsubfield,
2660 'itemnumber' => $itemnumber,
2661 'iteminformation' => \@loop_data
2667 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2669 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2670 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2671 # =head2 ModZebrafiles
2673 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2677 # sub ModZebrafiles {
2679 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2683 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2684 # unless ( opendir( DIR, "$zebradir" ) ) {
2685 # warn "$zebradir not found";
2689 # my $filename = $zebradir . $biblionumber;
2692 # open( OUTPUT, ">", $filename . ".xml" );
2693 # print OUTPUT $record;
2702 ModZebra( $biblionumber, $op, $server, $newRecord );
2704 $biblionumber is the biblionumber we want to index
2705 $op is specialUpdate or delete, and is used to know what we want to do
2706 $server is the server that we want to update
2707 $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.
2713 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2714 my ( $biblionumber, $op, $server, $newRecord ) = @_;
2715 # warn "ModZebra with : ( $biblionumber, $op, $server, $newRecord )";
2716 my $dbh=C4::Context->dbh;
2717 #warn "SERVER:".$server;
2719 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2721 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2722 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2724 if (C4::Context->preference("NoZebra")) {
2725 # lock the nozebra table : we will read index lines, update them in Perl process
2726 # and write everything in 1 transaction.
2727 # lock the table to avoid someone else overwriting what we are doing
2728 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE');
2729 my %result; # the result hash that will be builded by deletion / add, and written on mySQL at the end, to improve speed
2731 if ($server eq 'biblioserver') {
2732 $record= GetMarcBiblio($biblionumber);
2734 $record= C4::AuthoritiesMarc::GetAuthority($biblionumber);
2736 if ($op eq 'specialUpdate') {
2737 # OK, we have to add or update the record
2738 # 1st delete (virtually, in indexes) ...
2739 %result = _DelBiblioNoZebra($biblionumber,$record,$server);
2740 # ... add the record
2741 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2743 # it's a deletion, delete the record...
2744 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2745 %result=_DelBiblioNoZebra($biblionumber,$record,$server);
2747 # ok, now update the database...
2748 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2749 foreach my $key (keys %result) {
2750 foreach my $index (keys %{$result{$key}}) {
2751 # warn "UPDATING : $server $key , $index with :".$result{$key}->{$index};
2752 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2755 $dbh->do('UNLOCK TABLES');
2759 # we use zebra, just fill zebraqueue table
2761 my $sth=$dbh->prepare("insert into zebraqueue (biblio_auth_number ,server,operation) values(?,?,?)");
2762 $sth->execute($biblionumber,$server,$op);
2767 =head2 GetNoZebraIndexes
2771 sub GetNoZebraIndexes {
2772 my $index = C4::Context->preference('NoZebraIndexes');
2774 foreach my $line (split /('|"),/,$index) {
2775 $line =~ /(.*)=>(.*)/;
2776 my $index = substr($1,1); # get the index, don't forget to remove initial ' or "
2778 $index =~ s/'|"| //g;
2779 $fields =~ s/'|"| //g;
2780 $indexes{$index}=$fields;
2785 =head1 INTERNAL FUNCTIONS
2787 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2789 function to delete a biblio in NoZebra indexes
2790 This function does NOT delete anything in database : it reads all the indexes entries
2791 that have to be deleted & delete them in the hash
2792 The SQL part is done either :
2793 - after the Add if we are modifying a biblio (delete + add again)
2794 - immediatly after this sub if we are doing a true deletion.
2795 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2800 sub _DelBiblioNoZebra {
2801 my ($biblionumber, $record, $server)=@_;
2804 my $dbh = C4::Context->dbh;
2808 if ($server eq 'biblioserver') {
2809 %index=GetNoZebraIndexes;
2810 # get title of the record (to store the 10 first letters with the index)
2811 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2812 $title = lc($record->subfield($titletag,$titlesubfield));
2814 # for authorities, the "title" is the $a mainentry
2815 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
2816 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2817 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2818 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2819 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2820 $index{'auth_type'} = '152b';
2824 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2825 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2826 # limit to 10 char, should be enough, and limit the DB size
2827 $title = substr($title,0,10);
2829 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2830 foreach my $field ($record->fields()) {
2831 #parse each subfield
2832 next if $field->tag <10;
2833 foreach my $subfield ($field->subfields()) {
2834 my $tag = $field->tag();
2835 my $subfieldcode = $subfield->[0];
2837 # check each index to see if the subfield is stored somewhere
2838 # otherwise, store it in __RAW__ index
2839 foreach my $key (keys %index) {
2840 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2841 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2843 my $line= lc $subfield->[1];
2844 # remove meaningless value in the field...
2845 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2846 # ... and split in words
2847 foreach (split / /,$line) {
2848 next unless $_; # skip empty values (multiple spaces)
2849 # if the entry is already here, do nothing, the biblionumber has already be removed
2850 unless ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2851 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2852 $sth2->execute($server,$key,$_);
2853 my $existing_biblionumbers = $sth2->fetchrow;
2855 if ($existing_biblionumbers) {
2856 # warn " existing for $key $_: $existing_biblionumbers";
2857 $result{$key}->{$_} =$existing_biblionumbers;
2858 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2864 # the subfield is not indexed, store it in __RAW__ index anyway
2866 my $line= lc $subfield->[1];
2867 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2868 # ... and split in words
2869 foreach (split / /,$line) {
2870 next unless $_; # skip empty values (multiple spaces)
2871 # if the entry is already here, do nothing, the biblionumber has already be removed
2872 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2873 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2874 $sth2->execute($server,'__RAW__',$_);
2875 my $existing_biblionumbers = $sth2->fetchrow;
2877 if ($existing_biblionumbers) {
2878 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2879 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2889 =head2 _DelBiblioNoZebra($biblionumber,$record);
2891 function to delete a biblio in NoZebra indexes
2896 sub _AddBiblioNoZebra {
2897 my ($biblionumber, $record, $server, %result)=@_;
2898 my $dbh = C4::Context->dbh;
2902 if ($server eq 'biblioserver') {
2903 %index=GetNoZebraIndexes;
2904 # get title of the record (to store the 10 first letters with the index)
2905 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2906 $title = lc($record->subfield($titletag,$titlesubfield));
2908 # warn "server : $server";
2909 # for authorities, the "title" is the $a mainentry
2910 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
2911 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2912 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2913 $index{'mainmainentry'}=$authref->{auth_tag_to_report}.'a';
2914 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2915 $index{'auth_type'} = '152b';
2918 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2919 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2920 # limit to 10 char, should be enough, and limit the DB size
2921 $title = substr($title,0,10);
2923 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2924 foreach my $field ($record->fields()) {
2925 #parse each subfield
2926 next if $field->tag <10;
2927 foreach my $subfield ($field->subfields()) {
2928 my $tag = $field->tag();
2929 my $subfieldcode = $subfield->[0];
2931 # check each index to see if the subfield is stored somewhere
2932 # otherwise, store it in __RAW__ index
2933 foreach my $key (keys %index) {
2934 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2935 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2937 my $line= lc $subfield->[1];
2938 # remove meaningless value in the field...
2939 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2940 # ... and split in words
2941 foreach (split / /,$line) {
2942 next unless $_; # skip empty values (multiple spaces)
2943 # if the entry is already here, improve weight
2944 # warn "managing $_";
2945 if ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2947 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2948 $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
2950 # get the value if it exist in the nozebra table, otherwise, create it
2951 $sth2->execute($server,$key,$_);
2952 my $existing_biblionumbers = $sth2->fetchrow;
2954 if ($existing_biblionumbers) {
2955 $result{$key}->{$_} =$existing_biblionumbers;
2957 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2958 $result{$key}->{$_} .= "$biblionumber,$title-$weight;";
2959 # create a new ligne for this entry
2961 # warn "INSERT : $server / $key / $_";
2962 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2963 $result{$key}->{$_}.="$biblionumber,$title-1;";
2969 # the subfield is not indexed, store it in __RAW__ index anyway
2971 my $line= lc $subfield->[1];
2972 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2973 # ... and split in words
2974 foreach (split / /,$line) {
2975 next unless $_; # skip empty values (multiple spaces)
2976 # if the entry is already here, improve weight
2977 if ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2979 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2980 $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
2982 # get the value if it exist in the nozebra table, otherwise, create it
2983 $sth2->execute($server,'__RAW__',$_);
2984 my $existing_biblionumbers = $sth2->fetchrow;
2986 if ($existing_biblionumbers) {
2987 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2989 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2990 $result{'__RAW__'}->{$_} .= "$biblionumber,$title-$weight;";
2991 # create a new ligne for this entry
2993 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2994 $result{'__RAW__'}->{$_}.="$biblionumber,$title-1;";
3005 =head2 MARCitemchange
3009 &MARCitemchange( $record, $itemfield, $newvalue )
3011 Function to update a single value in an item field.
3012 Used twice, could probably be replaced by something else, but works well...
3020 sub MARCitemchange {
3021 my ( $record, $itemfield, $newvalue ) = @_;
3022 my $dbh = C4::Context->dbh;
3024 my ( $tagfield, $tagsubfield ) =
3025 GetMarcFromKohaField( $itemfield, "" );
3026 if ( ($tagfield) && ($tagsubfield) ) {
3027 my $tag = $record->field($tagfield);
3029 $tag->update( $tagsubfield => $newvalue );
3030 $record->delete_field($tag);
3031 $record->insert_fields_ordered($tag);
3036 =head2 _koha_add_biblio
3040 _koha_add_biblio($dbh,$biblioitem);
3042 Internal function to add a biblio ($biblio is a hash with the values)
3048 sub _koha_add_biblio {
3049 my ( $dbh, $biblio, $frameworkcode ) = @_;
3050 my $sth = $dbh->prepare("Select max(biblionumber) from biblio");
3052 my $data = $sth->fetchrow_arrayref;
3053 my $biblionumber = $$data[0] + 1;
3056 if ( $biblio->{'seriestitle'} ) { $series = 1 }
3058 $sth = $dbh->prepare(
3060 SET biblionumber = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?, unititle = ?, frameworkcode = ? "
3063 $biblionumber, $biblio->{'title'},
3064 $biblio->{'author'}, $biblio->{'copyrightdate'},
3065 $biblio->{'serial'}, $biblio->{'seriestitle'},
3066 $biblio->{'notes'}, $biblio->{'abstract'},
3067 $biblio->{'unititle'}, $frameworkcode
3071 return ($biblionumber);
3078 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
3080 Find the given $subfield in the given $tag in the given
3081 MARC::Record $record. If the subfield is found, returns
3082 the (indicators, value) pair; otherwise, (undef, undef) is
3086 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
3087 I suggest we export it from this module.
3094 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
3097 if ( $tagfield < 10 ) {
3098 if ( $record->field($tagfield) ) {
3099 push @result, $record->field($tagfield)->data();
3106 foreach my $field ( $record->field($tagfield) ) {
3107 my @subfields = $field->subfields();
3108 foreach my $subfield (@subfields) {
3109 if ( @$subfield[0] eq $insubfield ) {
3110 push @result, @$subfield[1];
3111 $indicator = $field->indicator(1) . $field->indicator(2);
3116 return ( $indicator, @result );
3119 =head2 _koha_modify_biblio
3123 Internal function for updating the biblio table
3129 sub _koha_modify_biblio {
3130 my ( $dbh, $biblio ) = @_;
3132 # FIXME: this code could be made more portable by not hard-coding the values that are supposed to be in biblio table
3135 "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?, seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?"
3138 $biblio->{'title'}, $biblio->{'author'},
3139 $biblio->{'abstract'}, $biblio->{'copyrightdate'},
3140 $biblio->{'seriestitle'}, $biblio->{'serial'},
3141 $biblio->{'unititle'}, $biblio->{'notes'},
3142 $biblio->{'biblionumber'}
3145 return ( $biblio->{'biblionumber'} );
3148 =head2 _koha_modify_biblioitem
3152 _koha_modify_biblioitem( $dbh, $biblioitem );
3158 sub _koha_modify_biblioitem {
3159 my ( $dbh, $biblioitem ) = @_;
3161 ##Recalculate LC in case it changed --TG
3163 $biblioitem->{'itemtype'} = $dbh->quote( $biblioitem->{'itemtype'} );
3164 $biblioitem->{'url'} = $dbh->quote( $biblioitem->{'url'} );
3165 $biblioitem->{'isbn'} = $dbh->quote( $biblioitem->{'isbn'} );
3166 $biblioitem->{'issn'} = $dbh->quote( $biblioitem->{'issn'} );
3167 $biblioitem->{'publishercode'} =
3168 $dbh->quote( $biblioitem->{'publishercode'} );
3169 $biblioitem->{'publicationyear'} =
3170 $dbh->quote( $biblioitem->{'publicationyear'} );
3171 $biblioitem->{'classification'} =
3172 $dbh->quote( $biblioitem->{'classification'} );
3173 $biblioitem->{'dewey'} = $dbh->quote( $biblioitem->{'dewey'} );
3174 $biblioitem->{'subclass'} = $dbh->quote( $biblioitem->{'subclass'} );
3175 $biblioitem->{'illus'} = $dbh->quote( $biblioitem->{'illus'} );
3176 $biblioitem->{'pages'} = $dbh->quote( $biblioitem->{'pages'} );
3177 $biblioitem->{'volumeddesc'} = $dbh->quote( $biblioitem->{'volumeddesc'} );
3178 $biblioitem->{'bnotes'} = $dbh->quote( $biblioitem->{'bnotes'} );
3179 $biblioitem->{'size'} = $dbh->quote( $biblioitem->{'size'} );
3180 $biblioitem->{'place'} = $dbh->quote( $biblioitem->{'place'} );
3181 $biblioitem->{'ccode'} = $dbh->quote( $biblioitem->{'ccode'} );
3182 $biblioitem->{'biblionumber'} =
3183 $dbh->quote( $biblioitem->{'biblionumber'} );
3185 $query = "Update biblioitems set
3186 itemtype = $biblioitem->{'itemtype'},
3187 url = $biblioitem->{'url'},
3188 isbn = $biblioitem->{'isbn'},
3189 issn = $biblioitem->{'issn'},
3190 publishercode = $biblioitem->{'publishercode'},
3191 publicationyear = $biblioitem->{'publicationyear'},
3192 classification = $biblioitem->{'classification'},
3193 dewey = $biblioitem->{'dewey'},
3194 subclass = $biblioitem->{'subclass'},
3195 illus = $biblioitem->{'illus'},
3196 pages = $biblioitem->{'pages'},
3197 volumeddesc = $biblioitem->{'volumeddesc'},
3198 notes = $biblioitem->{'bnotes'},
3199 size = $biblioitem->{'size'},
3200 place = $biblioitem->{'place'},
3201 ccode = $biblioitem->{'ccode'}
3202 where biblionumber = $biblioitem->{'biblionumber'}";
3205 if ( $dbh->errstr ) {
3206 warn "ERROR in _koha_modify_biblioitem $query";
3210 =head2 _koha_add_biblioitem
3214 _koha_add_biblioitem( $dbh, $biblioitem );
3216 Internal function to add a biblioitem
3222 sub _koha_add_biblioitem {
3223 my ( $dbh, $biblioitem ) = @_;
3225 # my $dbh = C4Connect;
3226 my $sth = $dbh->prepare("SELECT max(biblioitemnumber) FROM biblioitems");
3231 $data = $sth->fetchrow_arrayref;
3232 $bibitemnum = $$data[0] + 1;
3236 $sth = $dbh->prepare(
3237 "INSERT INTO biblioitems SET
3238 biblioitemnumber = ?, biblionumber = ?,
3239 volume = ?, number = ?,
3240 classification = ?, itemtype = ?,
3242 issn = ?, dewey = ?,
3243 subclass = ?, publicationyear = ?,
3244 publishercode = ?, volumedate = ?,
3245 volumeddesc = ?, illus = ?,
3246 pages = ?, notes = ?,
3248 marc = ?, lcsort =?,
3249 place = ?, ccode = ?
3253 calculatelc( $biblioitem->{'classification'} )
3254 . $biblioitem->{'subclass'};
3256 $bibitemnum, $biblioitem->{'biblionumber'},
3257 $biblioitem->{'volume'}, $biblioitem->{'number'},
3258 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
3259 $biblioitem->{'url'}, $biblioitem->{'isbn'},
3260 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
3261 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
3262 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
3263 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
3264 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
3265 $biblioitem->{'size'}, $biblioitem->{'lccn'},
3266 $biblioitem->{'marc'}, $biblioitem->{'place'},
3267 $lcsort, $biblioitem->{'ccode'}
3270 return ($bibitemnum);
3273 =head2 _koha_new_items
3277 _koha_new_items( $dbh, $item, $barcode );
3283 sub _koha_new_items {
3284 my ( $dbh, $item, $barcode ) = @_;
3286 # my $dbh = C4Connect;
3287 my $sth = $dbh->prepare("Select max(itemnumber) from items");
3293 $data = $sth->fetchrow_hashref;
3294 $itemnumber = $data->{'max(itemnumber)'} + 1;
3296 ## Now calculate lccalnumber
3297 my ($cutterextra) = itemcalculator(
3299 $item->{'biblioitemnumber'},
3300 $item->{'itemcallnumber'}
3303 # FIXME the "notforloan" field seems to be named "loan" in some places. workaround bugfix.
3304 if ( $item->{'loan'} ) {
3305 $item->{'notforloan'} = $item->{'loan'};
3308 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
3309 if ( $item->{'dateaccessioned'} eq '' || !$item->{'dateaccessioned'} ) {
3311 $sth = $dbh->prepare(
3312 "Insert into items set
3313 itemnumber = ?, biblionumber = ?,
3314 multivolumepart = ?,
3315 biblioitemnumber = ?, barcode = ?,
3316 booksellerid = ?, dateaccessioned = NOW(),
3317 homebranch = ?, holdingbranch = ?,
3318 price = ?, replacementprice = ?,
3319 replacementpricedate = NOW(), datelastseen = NOW(),
3320 multivolume = ?, stack = ?,
3321 itemlost = ?, wthdrawn = ?,
3322 paidfor = ?, itemnotes = ?,
3323 itemcallnumber =?, notforloan = ?,
3324 location = ?, Cutterextra = ?
3328 $itemnumber, $item->{'biblionumber'},
3329 $item->{'multivolumepart'}, $item->{'biblioitemnumber'},
3330 $barcode, $item->{'booksellerid'},
3331 $item->{'homebranch'}, $item->{'holdingbranch'},
3332 $item->{'price'}, $item->{'replacementprice'},
3333 $item->{multivolume}, $item->{stack},
3334 $item->{itemlost}, $item->{wthdrawn},
3335 $item->{paidfor}, $item->{'itemnotes'},
3336 $item->{'itemcallnumber'}, $item->{'notforloan'},
3337 $item->{'location'}, $cutterextra
3341 $sth = $dbh->prepare(
3342 "INSERT INTO items SET
3343 itemnumber = ?, biblionumber = ?,
3344 multivolumepart = ?,
3345 biblioitemnumber = ?, barcode = ?,
3346 booksellerid = ?, dateaccessioned = ?,
3347 homebranch = ?, holdingbranch = ?,
3348 price = ?, replacementprice = ?,
3349 replacementpricedate = NOW(), datelastseen = NOW(),
3350 multivolume = ?, stack = ?,
3351 itemlost = ?, wthdrawn = ?,
3352 paidfor = ?, itemnotes = ?,
3353 itemcallnumber = ?, notforloan = ?,
3359 $itemnumber, $item->{'biblionumber'},
3360 $item->{'multivolumepart'}, $item->{'biblioitemnumber'},
3361 $barcode, $item->{'booksellerid'},
3362 $item->{'dateaccessioned'}, $item->{'homebranch'},
3363 $item->{'holdingbranch'}, $item->{'price'},
3364 $item->{'replacementprice'}, $item->{multivolume},
3365 $item->{stack}, $item->{itemlost},
3366 $item->{wthdrawn}, $item->{paidfor},
3367 $item->{'itemnotes'}, $item->{'itemcallnumber'},
3368 $item->{'notforloan'}, $item->{'location'},
3372 if ( defined $sth->errstr ) {
3373 $error .= $sth->errstr;
3375 return ( $itemnumber, $error );
3378 =head2 _koha_modify_item
3382 _koha_modify_item( $dbh, $item, $op );
3388 sub _koha_modify_item {
3389 my ( $dbh, $item, $op ) = @_;
3390 $item->{'itemnum'} = $item->{'itemnumber'} unless $item->{'itemnum'};
3392 # if all we're doing is setting statuses, just update those and get out
3393 if ( $op eq "setstatus" ) {
3395 "UPDATE items SET itemlost=?,wthdrawn=?,binding=? WHERE itemnumber=?";
3397 $item->{'itemlost'}, $item->{'wthdrawn'},
3398 $item->{'binding'}, $item->{'itemnumber'}
3400 my $sth = $dbh->prepare($query);
3401 $sth->execute(@bind);
3405 ## Now calculate lccalnumber
3407 itemcalculator( $dbh, $item->{'bibitemnum'}, $item->{'itemcallnumber'} );
3409 my $query = "UPDATE items SET
3410 barcode=?,itemnotes=?,itemcallnumber=?,notforloan=?,location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,homebranch=?,cutterextra=?, onloan=?, binding=?";
3413 $item->{'barcode'}, $item->{'notes'},
3414 $item->{'itemcallnumber'}, $item->{'notforloan'},
3415 $item->{'location'}, $item->{multivolumepart},
3416 $item->{multivolume}, $item->{stack},
3417 $item->{wthdrawn}, $item->{holdingbranch},
3418 $item->{homebranch}, $cutterextra,
3419 $item->{onloan}, $item->{binding}
3421 if ( $item->{'lost'} ne '' ) {
3423 "update items set biblioitemnumber=?,barcode=?,itemnotes=?,homebranch=?,
3424 itemlost=?,wthdrawn=?,itemcallnumber=?,notforloan=?,
3425 location=?,multivolumepart=?,multivolume=?,stack=?,wthdrawn=?,holdingbranch=?,cutterextra=?,onloan=?, binding=?";
3427 $item->{'bibitemnum'}, $item->{'barcode'},
3428 $item->{'notes'}, $item->{'homebranch'},
3429 $item->{'lost'}, $item->{'wthdrawn'},
3430 $item->{'itemcallnumber'}, $item->{'notforloan'},
3431 $item->{'location'}, $item->{multivolumepart},
3432 $item->{multivolume}, $item->{stack},
3433 $item->{wthdrawn}, $item->{holdingbranch},
3434 $cutterextra, $item->{onloan},
3437 if ( $item->{homebranch} ) {
3438 $query .= ",homebranch=?";
3439 push @bind, $item->{homebranch};
3441 if ( $item->{holdingbranch} ) {
3442 $query .= ",holdingbranch=?";
3443 push @bind, $item->{holdingbranch};
3446 $query .= " where itemnumber=?";
3447 push @bind, $item->{'itemnum'};
3448 if ( $item->{'replacement'} ne '' ) {
3449 $query =~ s/ where/,replacementprice='$item->{'replacement'}' where/;
3451 my $sth = $dbh->prepare($query);
3452 $sth->execute(@bind);
3456 =head2 _koha_delete_biblio
3460 $error = _koha_delete_biblio($dbh,$biblionumber);
3462 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3464 C<$dbh> - the database handle
3465 C<$biblionumber> - the biblionumber of the biblio to be deleted
3471 # FIXME: add error handling
3473 sub _koha_delete_biblio {
3474 my ( $dbh, $biblionumber ) = @_;
3476 # get all the data for this biblio
3477 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3478 $sth->execute($biblionumber);
3480 if ( my $data = $sth->fetchrow_hashref ) {
3482 # save the record in deletedbiblio
3483 # find the fields to save
3484 my $query = "INSERT INTO deletedbiblio SET ";
3486 foreach my $temp ( keys %$data ) {
3487 $query .= "$temp = ?,";
3488 push( @bind, $data->{$temp} );
3491 # replace the last , by ",?)"
3493 my $bkup_sth = $dbh->prepare($query);
3494 $bkup_sth->execute(@bind);
3498 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3499 $del_sth->execute($biblionumber);
3506 =head2 _koha_delete_biblioitems
3510 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3512 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3514 C<$dbh> - the database handle
3515 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3521 # FIXME: add error handling
3523 sub _koha_delete_biblioitems {
3524 my ( $dbh, $biblioitemnumber ) = @_;
3526 # get all the data for this biblioitem
3528 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3529 $sth->execute($biblioitemnumber);
3531 if ( my $data = $sth->fetchrow_hashref ) {
3533 # save the record in deletedbiblioitems
3534 # find the fields to save
3535 my $query = "INSERT INTO deletedbiblioitems SET ";
3537 foreach my $temp ( keys %$data ) {
3538 $query .= "$temp = ?,";
3539 push( @bind, $data->{$temp} );
3542 # replace the last , by ",?)"
3544 my $bkup_sth = $dbh->prepare($query);
3545 $bkup_sth->execute(@bind);
3548 # delete the biblioitem
3550 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3551 $del_sth->execute($biblioitemnumber);
3558 =head2 _koha_delete_item
3562 _koha_delete_item( $dbh, $itemnum );
3564 Internal function to delete an item record from the koha tables
3570 sub _koha_delete_item {
3571 my ( $dbh, $itemnum ) = @_;
3573 my $sth = $dbh->prepare("select * from items where itemnumber=?");
3574 $sth->execute($itemnum);
3575 my $data = $sth->fetchrow_hashref;
3577 my $query = "Insert into deleteditems set ";
3579 foreach my $temp ( keys %$data ) {
3580 $query .= "$temp = ?,";
3581 push( @bind, $data->{$temp} );
3586 $sth = $dbh->prepare($query);
3587 $sth->execute(@bind);
3589 $sth = $dbh->prepare("Delete from items where itemnumber=?");
3590 $sth->execute($itemnum);
3594 =head1 UNEXPORTED FUNCTIONS
3600 $lc = calculatelc($classification);
3607 my ($classification) = @_;
3608 $classification =~ s/^\s+|\s+$//g;
3613 for ( $i = 0 ; $i < length($classification) ; $i++ ) {
3614 my $c = ( substr( $classification, $i, 1 ) );
3615 if ( $c ge '0' && $c le '9' ) {
3617 $lc2 = substr( $classification, $i );
3621 $lc1 .= substr( $classification, $i, 1 );
3626 my $other = length($lc1);
3633 for ( 1 .. ( 4 - $other ) ) {
3642 ##Find the decimal part of $lc2
3643 my $pos = index( $lc2, "." );
3644 if ( $pos < 0 ) { $pos = length($lc2); }
3645 if ( $pos >= 0 && $pos < 5 ) {
3646 ##Pad lc2 with zeros to create a 5digit decimal needed in marc record to sort as numeric
3648 for ( 1 .. ( 5 - $pos ) ) {
3652 $lc2 = $extras . $lc2;
3653 return ( $lc1 . $lc2 );
3656 =head2 itemcalculator
3660 $cutterextra = itemcalculator( $dbh, $biblioitem, $callnumber );
3666 sub itemcalculator {
3667 my ( $dbh, $biblioitem, $callnumber ) = @_;
3670 "select classification, subclass from biblioitems where biblioitemnumber=?"
3673 $sth->execute($biblioitem);
3674 my ( $classification, $subclass ) = $sth->fetchrow;
3675 my $all = $classification . " " . $subclass;
3676 my $total = length($all);
3677 my $cutterextra = substr( $callnumber, $total - 1 );
3679 return $cutterextra;
3682 =head2 ModBiblioMarc
3686 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3688 Add MARC data for a biblio to koha
3690 Function exported, but should NOT be used, unless you really know what you're doing
3698 # pass the MARC::Record to this function, and it will create the records in the marc field
3699 my ( $record, $biblionumber, $frameworkcode ) = @_;
3700 my $dbh = C4::Context->dbh;
3701 my @fields = $record->fields();
3702 if ( !$frameworkcode ) {
3703 $frameworkcode = "";
3706 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3707 $sth->execute( $frameworkcode, $biblionumber );
3709 my $encoding = C4::Context->preference("marcflavour");
3711 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3712 if ( $encoding eq "UNIMARC" ) {
3714 if ( length($record->subfield( 100, "a" )) == 35 ) {
3715 $string = $record->subfield( 100, "a" );
3716 my $f100 = $record->field(100);
3717 $record->delete_field($f100);
3720 $string = POSIX::strftime( "%Y%m%d", localtime );
3722 $string = sprintf( "%-*s", 35, $string );
3724 substr( $string, 22, 6, "frey50" );
3725 unless ( $record->subfield( 100, "a" ) ) {
3726 $record->insert_grouped_field(
3727 MARC::Field->new( 100, "", "", "a" => $string ) );
3730 # warn "biblionumber : ".$biblionumber;
3731 ModZebra($biblionumber,"specialUpdate","biblioserver",$record);
3734 "update biblioitems set marc=?,marcxml=? where biblionumber=?");
3735 $sth->execute( $record->as_usmarc(), $record->as_xml_record(),
3737 # warn $record->as_xml_record();
3739 return $biblionumber;
3742 =head2 AddItemInMarc
3746 $newbiblionumber = AddItemInMarc( $record, $biblionumber, $frameworkcode );
3748 Add an item in a MARC record and save the MARC record
3750 Function exported, but should NOT be used, unless you really know what you're doing
3758 # pass the MARC::Record to this function, and it will create the records in the marc tables
3759 my ( $record, $biblionumber, $frameworkcode ) = @_;
3760 my $newrec = &GetMarcBiblio($biblionumber);
3763 my @fields = $record->fields();
3764 foreach my $field (@fields) {
3765 $newrec->append_fields($field);
3768 # FIXME: should we be making sure the biblionumbers are the same?
3769 my $newbiblionumber =
3770 &ModBiblioMarc( $newrec, $biblionumber, $frameworkcode );
3771 return $newbiblionumber;
3774 =head2 z3950_extended_services
3776 z3950_extended_services($serviceType,$serviceOptions,$record);
3778 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.
3780 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3782 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3784 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3788 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3789 syntax => the record syntax (transfer syntax)
3790 databaseName = Database from connection object
3792 To set serviceOptions, call set_service_options($serviceType)
3794 C<$record> the record, if one is needed for the service type
3796 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3800 sub z3950_extended_services {
3801 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3803 # get our connection object
3804 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3806 # create a new package object
3807 my $Zpackage = $Zconn->package();
3810 $Zpackage->option( action => $action );
3812 if ( $serviceOptions->{'databaseName'} ) {
3813 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3815 if ( $serviceOptions->{'recordIdNumber'} ) {
3817 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3819 if ( $serviceOptions->{'recordIdOpaque'} ) {
3821 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3824 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3825 #if ($serviceType eq 'itemorder') {
3826 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3827 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3828 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3829 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3832 if ( $serviceOptions->{record} ) {
3833 $Zpackage->option( record => $serviceOptions->{record} );
3835 # can be xml or marc
3836 if ( $serviceOptions->{'syntax'} ) {
3837 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3841 # send the request, handle any exception encountered
3842 eval { $Zpackage->send($serviceType) };
3843 if ( $@ && $@->isa("ZOOM::Exception") ) {
3844 return "error: " . $@->code() . " " . $@->message() . "\n";
3847 # free up package resources
3848 $Zpackage->destroy();
3851 =head2 set_service_options
3853 my $serviceOptions = set_service_options($serviceType);
3855 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3857 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3861 sub set_service_options {
3862 my ($serviceType) = @_;
3865 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3866 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3868 if ( $serviceType eq 'commit' ) {
3872 if ( $serviceType eq 'create' ) {
3876 if ( $serviceType eq 'drop' ) {
3877 die "ERROR: 'drop' not currently supported (by Zebra)";
3879 return $serviceOptions;
3882 END { } # module clean-up code here (global destructor)
3890 Koha Developement team <info@koha.org>
3892 Paul POULAIN paul.poulain@free.fr
3894 Joshua Ferraro jmf@liblime.com
3900 # Revision 1.204 2007/05/10 14:45:15 tipaul
3902 # - support for authorities
3903 # - some bugfixes in ordering and "CCL" parsing
3904 # - support for authorities <=> biblios walking
3906 # Seems I can do what I want now, so I consider its done, except for bugfixes that will be needed i m sure !
3908 # Revision 1.203 2007/05/03 15:16:02 tipaul
3909 # BUGFIX for : NoZebra
3910 # - NoZebra features : seems they work fine now (adding, modifying, deleting)
3911 # - Biblio edition major bugfix : before this commit editing a biblio resulted in an item removal in marcxml field
3913 # Revision 1.202 2007/05/02 16:44:31 tipaul
3914 # NoZebra SQL index management :
3915 # * adding 3 subs in Biblio.pm
3916 # - GetNoZebraIndexes, that get the index structure in a new systempreference (added with this commit)
3917 # - _DelBiblioNoZebra, that retrieve all index entries for a biblio and remove in a variable the biblio reference
3918 # - _AddBiblioNoZebra, that add index entries for a biblio.
3919 # Note that the 2 _Add and _Del subs work only in a hash variable, to speed up things in case of a modif (ie : delete+add). The effective SQL update is done in the ModZebra sub (that existed before, and dealed with zebra index).
3920 # I think the code has to be more deeply tested, but it works at least partially.
3922 # Revision 1.201 2007/04/27 14:00:49 hdl
3923 # Removing $dbh from GetMarcFromKohaField (dbh is not used in this function.)
3925 # Revision 1.200 2007/04/25 16:26:42 tipaul
3926 # Koha 3.0 nozebra 1st commit : the script misc/migration_tools/rebuild_nozebra.pl build the nozebra table, and, if you set NoZebra to Yes, queries will be done through zebra. TODO :
3927 # - add nozebra table management on biblio editing
3928 # - the index table content is hardcoded. I still have to add some specific systempref to let the library update it
3929 # - manage pagination (next/previous)
3932 # - NZgetRecords : has exactly the same API & returns as zebra getQuery, except that some parameters are unused
3933 # - search & sort works quite good
3934 # - CQL parser is better that what I thought I could do : title="harry and sally" and publicationyear>2000 not itemtype=LIVR should work fine
3936 # Revision 1.199 2007/04/24 09:07:53 tipaul
3937 # moving dotransfer to Biblio.pm::ModItemTransfer + some CheckReserves fixes
3939 # Revision 1.198 2007/04/23 15:21:17 tipaul
3940 # renaming currenttransfers to transferstoreceive
3942 # Revision 1.197 2007/04/18 17:00:14 tipaul
3943 # removing all useless %env / $env
3945 # Revision 1.196 2007/04/17 08:48:00 tipaul
3946 # circulation cleaning continued: bufixing
3948 # Revision 1.195 2007/04/04 16:46:22 tipaul
3949 # HUGE COMMIT : code cleaning circulation.
3951 # some stuff to do, i'll write a mail on koha-devel NOW !
3953 # Revision 1.194 2007/03/30 12:00:42 tipaul
3954 # why the hell do we need to explicitly utf8 decode this string ? I really don't know, but it seems it's mandatory, otherwise, tag descriptions are not properly encoded...
3956 # Revision 1.193 2007/03/29 16:45:53 tipaul
3957 # Code cleaning of Biblio.pm (continued)
3959 # All subs have be cleaned :
3962 # - reordering Biblio.pm completly
3963 # - using only naming conventions
3965 # Seems to have broken nothing, but it still has to be heavily tested.
3966 # Note that Biblio.pm is now much more efficient than previously & probably more reliable as well.
3968 # Revision 1.192 2007/03/29 13:30:31 tipaul
3970 # == Biblio.pm cleaning (useless) ==
3971 # * some sub declaration dropped
3972 # * removed modbiblio sub
3973 # * removed moditem sub
3974 # * removed newitems. It was used only in finishrecieve. Replaced by a TransformKohaToMarc+AddItem, that is better.
3975 # * removed MARCkoha2marcItem
3976 # * removed MARCdelsubfield declaration
3977 # * removed MARCkoha2marcBiblio
3979 # == Biblio.pm cleaning (naming conventions) ==
3980 # * MARCgettagslib renamed to GetMarcStructure
3981 # * MARCgetitems renamed to GetMarcItem
3982 # * MARCfind_frameworkcode renamed to GetFrameworkCode
3983 # * MARCmarc2koha renamed to TransformMarcToKoha
3984 # * MARChtml2marc renamed to TransformHtmlToMarc
3985 # * MARChtml2xml renamed to TranformeHtmlToXml
3986 # * zebraop renamed to ModZebra
3989 # * removing MARC=OFF related scripts (in cataloguing directory)
3990 # * removed checkitems (function related to MARC=off feature, that is completly broken in head. If someone want to reintroduce it, hard work coming...)
3991 # * removed getitemsbybiblioitem (used only by MARC=OFF scripts, that is removed as well)
3993 # Revision 1.191 2007/03/29 09:42:13 tipaul
3994 # adding default value new feature into cataloguing. The system (definition) part has already been added by toins
3996 # Revision 1.190 2007/03/29 08:45:19 hdl
3997 # Deleting ignore_errors(1) pour MARC::Charset
3999 # Revision 1.189 2007/03/28 10:39:16 hdl
4000 # removing $dbh as a parameter in AuthoritiesMarc functions
4001 # And reporting all differences into the scripts taht relies on those functions.