/
Biblio.pm
3614 lines (2994 loc) · 122 KB
/
Biblio.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package C4::Biblio;
# Copyright 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
use Carp;
use MARC::Record;
use MARC::File::USMARC;
use MARC::File::XML;
use ZOOM;
use POSIX qw(strftime);
use C4::Koha;
use C4::Dates qw/format_date/;
use C4::Log; # logaction
use C4::ClassSource;
use C4::Charset;
use Encode;
require C4::Heading;
require C4::Serials;
use vars qw($VERSION @ISA @EXPORT);
BEGIN {
$VERSION = 1.00;
require Exporter;
@ISA = qw( Exporter );
# to add biblios
# EXPORTED FUNCTIONS.
push @EXPORT, qw(
&AddBiblio
);
# to get something
push @EXPORT, qw(
&GetBiblio
&GetBiblioData
&GetBiblioItemData
&GetBiblioItemInfosOf
&GetBiblioItemByBiblioNumber
&GetBiblioFromItemNumber
&GetISBDView
&GetMarcNotes
&GetMarcSubjects
&GetMarcBiblio
&GetMarcAuthors
&GetMarcSeries
GetMarcUrls
&GetUsedMarcStructure
&GetMarcSeriesSummaries
&GetXmlBiblio
&GetCOinSBiblio
&GetAuthorisedValueDesc
&GetMarcStructure
&GetMarcFromKohaField
&GetFrameworkCode
&GetPublisherNameFromIsbn
&TransformKohaToMarc
&CountItemsIssued
);
# To modify something
push @EXPORT, qw(
&ModBiblio
&ModBiblioframework
&ModZebra
);
# To delete something
push @EXPORT, qw(
&DelBiblio
);
# To link headings in a bib record
# to authority records.
push @EXPORT, qw(
&LinkBibHeadingsToAuthorities
);
# Internal functions
# those functions are exported but should not be used
# they are usefull is few circumstances, so are exported.
# but don't use them unless you're a core developer ;-)
push @EXPORT, qw(
&ModBiblioMarc
);
# Others functions
push @EXPORT, qw(
&TransformMarcToKoha
&TransformHtmlToMarc2
&TransformHtmlToMarc
&TransformHtmlToXml
&PrepareItemrecordDisplay
&GetNoZebraIndexes
&GetAvailableItemsCount
);
}
=head1 NAME
C4::Biblio - cataloging management functions
=head1 DESCRIPTION
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:
=over 4
=item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
=item 2. as raw MARC in the Zebra index and storage engine
=item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
=back
In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
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.
=over 4
=item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
=item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
=back
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:
=over 4
=item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
=item 2. _koha_* - low-level internal functions for managing the koha tables
=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.
=item 4. Zebra functions used to update the Zebra index
=item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
=back
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 :
=over 4
=item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
=item 2. add the biblionumber and biblioitemnumber into the MARC records
=item 3. save the marc record
=back
When dealing with items, we must :
=over 4
=item 1. save the item in items table, that gives us an itemnumber
=item 2. add the itemnumber to the item MARC field
=item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
When modifying a biblio or an item, the behaviour is quite similar.
=back
=head1 EXPORTED FUNCTIONS
=head2 AddBiblio
=over 4
($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
=back
Exported function (core API) for adding a new biblio to koha.
The first argument is a C<MARC::Record> object containing the
bib to add, while the second argument is the desired MARC
framework code.
This function also accepts a third, optional argument: a hashref
to additional options. The only defined option is C<defer_marc_save>,
which if present and mapped to a true value, causes C<AddBiblio>
to omit the call to save the MARC in C<bibilioitems.marc>
and C<biblioitems.marcxml> This option is provided B<only>
for the use of scripts such as C<bulkmarcimport.pl> that may need
to do some manipulation of the MARC record for item parsing before
saving it and which cannot afford the performance hit of saving
the MARC record twice. Consequently, do not use that option
unless you can guarantee that C<ModBiblioMarc> will be called.
=cut
sub AddBiblio {
my $record = shift;
my $frameworkcode = shift;
my $options = @_ ? shift : undef;
my $defer_marc_save = 0;
if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
$defer_marc_save = 1;
}
my ($biblionumber,$biblioitemnumber,$error);
my $dbh = C4::Context->dbh;
# transform the data into koha-table style data
my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
$olddata->{'biblionumber'} = $biblionumber;
($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
_koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
# update MARC subfield that stores biblioitems.cn_sort
_koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
# now add the record
ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
return ( $biblionumber, $biblioitemnumber );
}
=head2 ModBiblio
=over 4
ModBiblio( $record,$biblionumber,$frameworkcode);
=back
Replace an existing bib record identified by C<$biblionumber>
with one supplied by the MARC::Record object C<$record>. The embedded
item, biblioitem, and biblionumber fields from the previous
version of the bib record replace any such fields of those tags that
are present in C<$record>. Consequently, ModBiblio() is not
to be used to try to modify item records.
C<$frameworkcode> specifies the MARC framework to use
when storing the modified bib record; among other things,
this controls how MARC fields get mapped to display columns
in the C<biblio> and C<biblioitems> tables, as well as
which fields are used to store embedded item, biblioitem,
and biblionumber data for indexing.
=cut
sub ModBiblio {
my ( $record, $biblionumber, $frameworkcode ) = @_;
if (C4::Context->preference("CataloguingLog")) {
my $newrecord = GetMarcBiblio($biblionumber);
logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
}
my $dbh = C4::Context->dbh;
$frameworkcode = "" unless $frameworkcode;
# get the items before the record modifications and append them to the biblio before updating the record, atm we just have the biblio
my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
my $oldRecord = GetMarcBiblio( $biblionumber );
# delete any item fields from incoming record to avoid
# duplication or incorrect data - use AddItem() or ModItem()
# to change items
foreach my $field ($record->field($itemtag)) {
$record->delete_field($field);
}
# Parse each oldRecord item, and, for an unknown reason, re-encode each subfield
# if you don't do that, the record will have encoding mixed
# and the biblio will be re-encoded.
# strange, I (Paul P.) searched more than 1 day to understand what happends
# but could only solve the problem this way...
my @fields = $oldRecord->field( $itemtag );
foreach my $fielditem ( @fields ){
my $field;
foreach ($fielditem->subfields()) {
if ($field) {
$field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
} else {
$field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
}
}
$record->append_fields($field);
}
# update biblionumber and biblioitemnumber in MARC
# FIXME - this is assuming a 1 to 1 relationship between
# biblios and biblioitems
my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
$sth->execute($biblionumber);
my ($biblioitemnumber) = $sth->fetchrow;
$sth->finish();
_koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
# load the koha-table data object
my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
# update MARC subfield that stores biblioitems.cn_sort
_koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
# update the MARC record (that now contains biblio and items) with the new record data
&ModBiblioMarc( $record, $biblionumber, $frameworkcode );
# modify the other koha tables
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
return 1;
}
=head2 ModBiblioframework
ModBiblioframework($biblionumber,$frameworkcode);
Exported function to modify a biblio framework
=cut
sub ModBiblioframework {
my ( $biblionumber, $frameworkcode ) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(
"UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
);
$sth->execute($frameworkcode, $biblionumber);
return 1;
}
=head2 DelBiblio
=over
my $error = &DelBiblio($dbh,$biblionumber);
Exported function (core API) for deleting a biblio in koha.
Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
Also backs it up to deleted* tables
Checks to make sure there are not issues on any of the items
return:
C<$error> : undef unless an error occurs
=back
=cut
sub DelBiblio {
my ( $biblionumber ) = @_;
my $dbh = C4::Context->dbh;
my $error; # for error handling
# First make sure this biblio has no items attached
my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
$sth->execute($biblionumber);
if (my $itemnumber = $sth->fetchrow){
# Fix this to use a status the template can understand
$error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
}
return $error if $error;
# We delete attached subscriptions
my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
foreach my $subscription (@$subscriptions){
&C4::Serials::DelSubscription($subscription->{subscriptionid});
}
# Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
# for at least 2 reasons :
# - we need to read the biblio if NoZebra is set (to remove it from the indexes
# - if something goes wrong, the biblio may be deleted from Koha but not from zebra
# 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)
my $oldRecord;
if (C4::Context->preference("NoZebra")) {
# only NoZebra indexing needs to have
# the previous version of the record
$oldRecord = GetMarcBiblio($biblionumber);
}
ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
# delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
$sth =
$dbh->prepare(
"SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
$sth->execute($biblionumber);
while ( my $biblioitemnumber = $sth->fetchrow ) {
# delete this biblioitem
$error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
return $error if $error;
}
# delete biblio from Koha tables and save in deletedbiblio
# must do this *after* _koha_delete_biblioitems, otherwise
# delete cascade will prevent deletedbiblioitems rows
# from being generated by _koha_delete_biblioitems
$error = _koha_delete_biblio( $dbh, $biblionumber );
logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
return;
}
sub GetAvailableItemsCount {
my ( $biblionumber, $branchcode ) = @_;
# warn "GetAvailableItemsCount( $biblionumber, $branchcode )";
my $dbh = C4::Context->dbh;
my @params;
my $query = "SELECT COUNT( * ) AS ItemsAvailableCount
FROM items
LEFT JOIN issues ON issues.itemnumber = items.itemnumber
WHERE issues.timestamp IS NULL
AND items.biblionumber = ?";
push( @params, $biblionumber );
if ( $branchcode ) {
$query .= " AND items.holdingbranch = ?";
push( @params, $branchcode ) if ( $branchcode );
}
my $sth = $dbh->prepare( $query );
$sth->execute( @params );
my $row = $sth->fetchrow_hashref();
my $items_available_count = $row->{'ItemsAvailableCount'};
return $items_available_count;
}
=head2 LinkBibHeadingsToAuthorities
=over 4
my $headings_linked = LinkBibHeadingsToAuthorities($marc);
=back
Links bib headings to authority records by checking
each authority-controlled field in the C<MARC::Record>
object C<$marc>, looking for a matching authority record,
and setting the linking subfield $9 to the ID of that
authority record.
If no matching authority exists, or if multiple
authorities match, no $9 will be added, and any
existing one inthe field will be deleted.
Returns the number of heading links changed in the
MARC record.
=cut
sub LinkBibHeadingsToAuthorities {
my $bib = shift;
my $num_headings_changed = 0;
foreach my $field ($bib->fields()) {
my $heading = C4::Heading->new_from_bib_field($field);
next unless defined $heading;
# check existing $9
my $current_link = $field->subfield('9');
# look for matching authorities
my $authorities = $heading->authorities();
# want only one exact match
if ($#{ $authorities } == 0) {
my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
my $authid = $authority->field('001')->data();
next if defined $current_link and $current_link eq $authid;
$field->delete_subfield(code => '9') if defined $current_link;
$field->add_subfields('9', $authid);
$num_headings_changed++;
} else {
if (defined $current_link) {
$field->delete_subfield(code => '9');
$num_headings_changed++;
}
}
}
return $num_headings_changed;
}
=head2 GetBiblioData
=over 4
$data = &GetBiblioData($biblionumber);
Returns information about the book with the given biblionumber.
C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
the C<biblio> and C<biblioitems> tables in the
Koha database.
In addition, C<$data-E<gt>{subject}> is the list of the book's
subjects, separated by C<" , "> (space, comma, space).
If there are multiple biblioitems with the given biblionumber, only
the first one is considered.
=back
=cut
sub GetBiblioData {
my ( $bibnum ) = @_;
my $dbh = C4::Context->dbh;
# my $query = C4::Context->preference('item-level_itypes') ?
# " SELECT * , biblioitems.notes AS bnotes, biblio.notes
# FROM biblio
# LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
# WHERE biblio.biblionumber = ?
# AND biblioitems.biblionumber = biblio.biblionumber
#";
my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
FROM biblio
LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
WHERE biblio.biblionumber = ?
AND biblioitems.biblionumber = biblio.biblionumber ";
my $sth = $dbh->prepare($query);
$sth->execute($bibnum);
my $data;
$data = $sth->fetchrow_hashref;
$sth->finish;
return ($data);
} # sub GetBiblioData
=head2 &GetBiblioItemData
=over 4
$itemdata = &GetBiblioItemData($biblioitemnumber);
Looks up the biblioitem with the given biblioitemnumber. Returns a
reference-to-hash. The keys are the fields from the C<biblio>,
C<biblioitems>, and C<itemtypes> tables in the Koha database, except
that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
=back
=cut
#'
sub GetBiblioItemData {
my ($biblioitemnumber) = @_;
my $dbh = C4::Context->dbh;
my $query = "SELECT *,biblioitems.notes AS bnotes
FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
unless(C4::Context->preference('item-level_itypes')) {
$query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
}
$query .= " WHERE biblioitemnumber = ? ";
my $sth = $dbh->prepare($query);
my $data;
$sth->execute($biblioitemnumber);
$data = $sth->fetchrow_hashref;
$sth->finish;
return ($data);
} # sub &GetBiblioItemData
=head2 GetBiblioItemByBiblioNumber
=over 4
NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
=back
=cut
sub GetBiblioItemByBiblioNumber {
my ($biblionumber) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
my $count = 0;
my @results;
$sth->execute($biblionumber);
while ( my $data = $sth->fetchrow_hashref ) {
push @results, $data;
}
$sth->finish;
return @results;
}
=head2 GetBiblioFromItemNumber
=over 4
$item = &GetBiblioFromItemNumber($itemnumber,$barcode);
Looks up the item with the given itemnumber. if undef, try the barcode.
C<&itemnodata> returns a reference-to-hash whose keys are the fields
from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
database.
=back
=cut
#'
sub GetBiblioFromItemNumber {
my ( $itemnumber, $barcode ) = @_;
my $dbh = C4::Context->dbh;
my $sth;
if($itemnumber) {
$sth=$dbh->prepare( "SELECT * FROM items
LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
WHERE items.itemnumber = ?") ;
$sth->execute($itemnumber);
} else {
$sth=$dbh->prepare( "SELECT * FROM items
LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
WHERE items.barcode = ?") ;
$sth->execute($barcode);
}
my $data = $sth->fetchrow_hashref;
$sth->finish;
return ($data);
}
=head2 GetISBDView
=over 4
$isbd = &GetISBDView($biblionumber);
Return the ISBD view which can be included in opac and intranet
=back
=cut
sub GetISBDView {
my $biblionumber = shift;
my $record = GetMarcBiblio($biblionumber);
my $itemtype = &GetFrameworkCode($biblionumber);
my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField("items.holdingbranch",$itemtype);
my $tagslib = &GetMarcStructure( 1, $itemtype );
my $ISBD = C4::Context->preference('ISBD');
my $bloc = $ISBD;
my $res;
my $blocres;
foreach my $isbdfield ( split (/#/, $bloc) ) {
# $isbdfield= /(.?.?.?)/;
$isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
my $fieldvalue = $1 || 0;
my $subfvalue = $2 || "";
my $textbefore = $3;
my $analysestring = $4;
my $textafter = $5;
# warn "==> $1 / $2 / $3 / $4";
# my $fieldvalue=substr($isbdfield,0,3);
if ( $fieldvalue > 0 ) {
my $hasputtextbefore = 0;
my @fieldslist = $record->field($fieldvalue);
@fieldslist = sort {$a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf)} @fieldslist if ($fieldvalue eq $holdingbrtagf);
# warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
# warn "FV : $fieldvalue";
if ($subfvalue ne ""){
foreach my $field ( @fieldslist ) {
foreach my $subfield ($field->subfield($subfvalue)){
my $calculated = $analysestring;
my $tag = $field->tag();
if ( $tag < 10 ) {
}
else {
my $subfieldvalue =
GetAuthorisedValueDesc( $tag, $subfvalue,
$subfield, '', $tagslib );
my $tagsubf = $tag . $subfvalue;
$calculated =~
s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
$calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
# field builded, store the result
if ( $calculated && !$hasputtextbefore )
{ # put textbefore if not done
$blocres .= $textbefore;
$hasputtextbefore = 1;
}
# remove punctuation at start
$calculated =~ s/^( |;|:|\.|-)*//g;
$blocres .= $calculated;
}
}
}
$blocres .= $textafter if $hasputtextbefore;
} else {
foreach my $field ( @fieldslist ) {
my $calculated = $analysestring;
my $tag = $field->tag();
if ( $tag < 10 ) {
}
else {
my @subf = $field->subfields;
for my $i ( 0 .. $#subf ) {
my $valuecode = $subf[$i][1];
my $subfieldcode = $subf[$i][0];
my $subfieldvalue =
GetAuthorisedValueDesc( $tag, $subf[$i][0],
$subf[$i][1], '', $tagslib );
my $tagsubf = $tag . $subfieldcode;
$calculated =~ s/ # replace all {{}} codes by the value code.
\{\{$tagsubf\}\} # catch the {{actualcode}}
/
$valuecode # replace by the value code
/gx;
$calculated =~
s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
$calculated =~s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g;
}
# field builded, store the result
if ( $calculated && !$hasputtextbefore )
{ # put textbefore if not done
$blocres .= $textbefore;
$hasputtextbefore = 1;
}
# remove punctuation at start
$calculated =~ s/^( |;|:|\.|-)*//g;
$blocres .= $calculated;
}
}
$blocres .= $textafter if $hasputtextbefore;
}
}
else {
$blocres .= $isbdfield;
}
}
$res .= $blocres;
$res =~ s/\{(.*?)\}//g;
$res =~ s/\\n/\n/g;
$res =~ s/\n/<br\/>/g;
# remove empty ()
$res =~ s/\(\)//g;
return $res;
}
=head2 GetBiblio
=over 4
( $count, @results ) = &GetBiblio($biblionumber);
=back
=cut
sub GetBiblio {
my ($biblionumber) = @_;
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
my $count = 0;
my @results;
$sth->execute($biblionumber);
while ( my $data = $sth->fetchrow_hashref ) {
$results[$count] = $data;
$count++;
} # while
$sth->finish;
return ( $count, @results );
} # sub GetBiblio
=head2 GetBiblioItemInfosOf
=over 4
GetBiblioItemInfosOf(@biblioitemnumbers);
=back
=cut
sub GetBiblioItemInfosOf {
my @biblioitemnumbers = @_;
return undef if not @biblioitemnumbers;
my $query = '
SELECT biblioitemnumber,
publicationyear,
itemtype
FROM biblioitems
WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
';
return get_infos_of( $query, 'biblioitemnumber' );
}
=head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
=head2 GetMarcStructure
=over 4
$res = GetMarcStructure($forlibrarian,$frameworkcode);
Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
$forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
$frameworkcode : the framework code to read
=back
=cut
sub _seed_marcstructure_cache {
my ( $forlibrarian, $frameworkcode ) = @_;
my $dbh=C4::Context->dbh;
$frameworkcode = "" unless $frameworkcode;
my $sth = $dbh->prepare(
"SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
$sth->execute($frameworkcode);
my ($total) = $sth->fetchrow;
$frameworkcode = "" unless ( $total > 0 );
$sth = $dbh->prepare(
"SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
FROM marc_tag_structure
WHERE frameworkcode=?
ORDER BY tagfield"
);
$sth->execute($frameworkcode);
my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
$sth->fetchrow )
{
$res->{$tag}->{lib} =
( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
$res->{$tag}->{tab} = "";
$res->{$tag}->{mandatory} = $mandatory;
$res->{$tag}->{repeatable} = $repeatable;
}
$sth = $dbh->prepare(
"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
"
);
$sth->execute($frameworkcode);
my $subfield;
my $authorised_value;
my $authtypecode;
my $value_builder;
my $kohafield;
my $seealso;
my $hidden;
my $isurl;
my $link;
my $defaultvalue;
while (
(
$tag, $subfield, $liblibrarian,
$libopac, $tab,
$mandatory, $repeatable, $authorised_value,
$authtypecode, $value_builder, $kohafield,
$seealso, $hidden, $isurl,
$link,$defaultvalue
)
= $sth->fetchrow
)
{
$res->{$tag}->{$subfield}->{lib} =
( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
$res->{$tag}->{$subfield}->{tab} = $tab;
$res->{$tag}->{$subfield}->{mandatory} = $mandatory;
$res->{$tag}->{$subfield}->{repeatable} = $repeatable;
$res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
$res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
$res->{$tag}->{$subfield}->{value_builder} = $value_builder;
$res->{$tag}->{$subfield}->{kohafield} = $kohafield;
$res->{$tag}->{$subfield}->{seealso} = $seealso;
$res->{$tag}->{$subfield}->{hidden} = $hidden;
$res->{$tag}->{$subfield}->{isurl} = $isurl;
$res->{$tag}->{$subfield}->{'link'} = $link;
$res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
}
return $res;
}
sub GetMarcStructure {
my ( $forlibrarian, $frameworkcode ) = @_;
my $cache = C4::Context->getcache(__PACKAGE__,
{driver => 'RawMemory',
datastore => C4::Context->cachehash});
return $cache->compute("marc_structure:$forlibrarian:$frameworkcode", '1m',
sub {_seed_marcstructure_cache($forlibrarian, $frameworkcode)});
}
=head2 GetUsedMarcStructure
the same function as GetMarcStructure except it just takes field
in tab 0-9. (used field)
my $results = GetUsedMarcStructure($frameworkcode);
L<$results> is a ref to an array which each case containts a ref
to a hash which each keys is the columns from marc_subfield_structure
L<$frameworkcode> is the framework code.
=cut
sub GetUsedMarcStructure($){
my $frameworkcode = shift || '';
my $query = qq/
SELECT *
FROM marc_subfield_structure
WHERE tab > -1
AND frameworkcode = ?
ORDER BY tagfield, tagsubfield
/;
my $sth = C4::Context->dbh->prepare($query);
$sth->execute($frameworkcode);
return $sth->fetchall_arrayref({});
}
=head2 GetMarcFromKohaField
=over 4
($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
Returns the MARC fields & subfields mapped to the koha field
for the given frameworkcode
=back
=cut
sub GetMarcFromKohaField {
my ( $kohafield, $frameworkcode ) = @_;
return 0, 0 unless $kohafield and defined $frameworkcode;
my $relations = C4::Context->marcfromkohafield;
return (
$relations->{$frameworkcode}->{$kohafield}->[0],
$relations->{$frameworkcode}->{$kohafield}->[1]
);
}
=head2 GetMarcBiblio
=over 4
my $record = GetMarcBiblio($biblionumber);