Bugs 6634: manager_id not recorded for payments and rental charges
[koha.git] / C4 / Biblio.pm
1 package C4::Biblio;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2011 Equinox Software, Inc.
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use strict;
23 use warnings;
24 use Carp;
25
26 # use utf8;
27 use MARC::Record;
28 use MARC::File::USMARC;
29 use MARC::File::XML;
30 use ZOOM;
31 use POSIX qw(strftime);
32
33 use C4::Koha;
34 use C4::Dates qw/format_date/;
35 use C4::Log;    # logaction
36 use C4::ClassSource;
37 use C4::Charset;
38 require C4::Heading;
39 require C4::Serials;
40 require C4::Items;
41
42 use vars qw($VERSION @ISA @EXPORT);
43
44 BEGIN {
45     $VERSION = 1.00;
46
47     require Exporter;
48     @ISA = qw( Exporter );
49
50     # to add biblios
51     # EXPORTED FUNCTIONS.
52     push @EXPORT, qw(
53       &AddBiblio
54     );
55
56     # to get something
57     push @EXPORT, qw(
58       &Get
59       &GetBiblio
60       &GetBiblioData
61       &GetBiblioItemData
62       &GetBiblioItemInfosOf
63       &GetBiblioItemByBiblioNumber
64       &GetBiblioFromItemNumber
65       &GetBiblionumberFromItemnumber
66
67       &GetRecordValue
68       &GetFieldMapping
69       &SetFieldMapping
70       &DeleteFieldMapping
71
72       &GetISBDView
73
74       &GetMarcControlnumber
75       &GetMarcNotes
76       &GetMarcISBN
77       &GetMarcSubjects
78       &GetMarcBiblio
79       &GetMarcAuthors
80       &GetMarcSeries
81       GetMarcUrls
82       &GetUsedMarcStructure
83       &GetXmlBiblio
84       &GetCOinSBiblio
85       &GetMarcPrice
86       &GetMarcQuantity
87
88       &GetAuthorisedValueDesc
89       &GetMarcStructure
90       &GetMarcFromKohaField
91       &GetFrameworkCode
92       &TransformKohaToMarc
93
94       &CountItemsIssued
95     );
96
97     # To modify something
98     push @EXPORT, qw(
99       &ModBiblio
100       &ModBiblioframework
101       &ModZebra
102     );
103
104     # To delete something
105     push @EXPORT, qw(
106       &DelBiblio
107     );
108
109     # To link headings in a bib record
110     # to authority records.
111     push @EXPORT, qw(
112       &LinkBibHeadingsToAuthorities
113     );
114
115     # Internal functions
116     # those functions are exported but should not be used
117     # they are usefull is few circumstances, so are exported.
118     # but don't use them unless you're a core developer ;-)
119     push @EXPORT, qw(
120       &ModBiblioMarc
121     );
122
123     # Others functions
124     push @EXPORT, qw(
125       &TransformMarcToKoha
126       &TransformHtmlToMarc2
127       &TransformHtmlToMarc
128       &TransformHtmlToXml
129       &PrepareItemrecordDisplay
130       &GetNoZebraIndexes
131     );
132 }
133
134 eval {
135     my $servers = C4::Context->config('memcached_servers');
136     if ($servers) {
137         require Memoize::Memcached;
138         import Memoize::Memcached qw(memoize_memcached);
139
140         my $memcached = {
141             servers    => [$servers],
142             key_prefix => C4::Context->config('memcached_namespace') || 'koha',
143         };
144         memoize_memcached( 'GetMarcStructure', memcached => $memcached, expire_time => 600 );    #cache for 10 minutes
145     }
146 };
147
148 =head1 NAME
149
150 C4::Biblio - cataloging management functions
151
152 =head1 DESCRIPTION
153
154 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:
155
156 =over 4
157
158 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
159
160 =item 2. as raw MARC in the Zebra index and storage engine
161
162 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
163
164 =back
165
166 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
167
168 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.
169
170 =over 4
171
172 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
173
174 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
175
176 =back
177
178 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:
179
180 =over 4
181
182 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
183
184 =item 2. _koha_* - low-level internal functions for managing the koha tables
185
186 =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.
187
188 =item 4. Zebra functions used to update the Zebra index
189
190 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
191
192 =back
193
194 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 :
195
196 =over 4
197
198 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
199
200 =item 2. add the biblionumber and biblioitemnumber into the MARC records
201
202 =item 3. save the marc record
203
204 =back
205
206 When dealing with items, we must :
207
208 =over 4
209
210 =item 1. save the item in items table, that gives us an itemnumber
211
212 =item 2. add the itemnumber to the item MARC field
213
214 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
215
216 When modifying a biblio or an item, the behaviour is quite similar.
217
218 =back
219
220 =head1 EXPORTED FUNCTIONS
221
222 =head2 AddBiblio
223
224   ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
225
226 Exported function (core API) for adding a new biblio to koha.
227
228 The first argument is a C<MARC::Record> object containing the
229 bib to add, while the second argument is the desired MARC
230 framework code.
231
232 This function also accepts a third, optional argument: a hashref
233 to additional options.  The only defined option is C<defer_marc_save>,
234 which if present and mapped to a true value, causes C<AddBiblio>
235 to omit the call to save the MARC in C<bibilioitems.marc>
236 and C<biblioitems.marcxml>  This option is provided B<only>
237 for the use of scripts such as C<bulkmarcimport.pl> that may need
238 to do some manipulation of the MARC record for item parsing before
239 saving it and which cannot afford the performance hit of saving
240 the MARC record twice.  Consequently, do not use that option
241 unless you can guarantee that C<ModBiblioMarc> will be called.
242
243 =cut
244
245 sub AddBiblio {
246     my $record          = shift;
247     my $frameworkcode   = shift;
248     my $options         = @_ ? shift : undef;
249     my $defer_marc_save = 0;
250     if ( defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'} ) {
251         $defer_marc_save = 1;
252     }
253
254     my ( $biblionumber, $biblioitemnumber, $error );
255     my $dbh = C4::Context->dbh;
256
257     # transform the data into koha-table style data
258     SetUTF8Flag($record);
259     my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
260     ( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
261     $olddata->{'biblionumber'} = $biblionumber;
262     ( $biblioitemnumber, $error ) = _koha_add_biblioitem( $dbh, $olddata );
263
264     _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
265
266     # update MARC subfield that stores biblioitems.cn_sort
267     _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
268
269     # now add the record
270     ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
271
272     logaction( "CATALOGUING", "ADD", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
273     return ( $biblionumber, $biblioitemnumber );
274 }
275
276 =head2 ModBiblio
277
278   ModBiblio( $record,$biblionumber,$frameworkcode);
279
280 Replace an existing bib record identified by C<$biblionumber>
281 with one supplied by the MARC::Record object C<$record>.  The embedded
282 item, biblioitem, and biblionumber fields from the previous
283 version of the bib record replace any such fields of those tags that
284 are present in C<$record>.  Consequently, ModBiblio() is not
285 to be used to try to modify item records.
286
287 C<$frameworkcode> specifies the MARC framework to use
288 when storing the modified bib record; among other things,
289 this controls how MARC fields get mapped to display columns
290 in the C<biblio> and C<biblioitems> tables, as well as
291 which fields are used to store embedded item, biblioitem,
292 and biblionumber data for indexing.
293
294 =cut
295
296 sub ModBiblio {
297     my ( $record, $biblionumber, $frameworkcode ) = @_;
298     croak "No record" unless $record;
299
300     if ( C4::Context->preference("CataloguingLog") ) {
301         my $newrecord = GetMarcBiblio($biblionumber);
302         logaction( "CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>" . $newrecord->as_formatted );
303     }
304
305     # Cleaning up invalid fields must be done early or SetUTF8Flag is liable to
306     # throw an exception which probably won't be handled.
307     foreach my $field ($record->fields()) {
308         if (! $field->is_control_field()) {
309             if (scalar($field->subfields()) == 0 || (scalar($field->subfields()) == 1 && $field->subfield('9'))) {
310                 $record->delete_field($field);
311             }
312         }
313     }
314
315     SetUTF8Flag($record);
316     my $dbh = C4::Context->dbh;
317
318     $frameworkcode = "" unless $frameworkcode;
319
320     _strip_item_fields($record, $frameworkcode);
321
322     # update biblionumber and biblioitemnumber in MARC
323     # FIXME - this is assuming a 1 to 1 relationship between
324     # biblios and biblioitems
325     my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
326     $sth->execute($biblionumber);
327     my ($biblioitemnumber) = $sth->fetchrow;
328     $sth->finish();
329     _koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber );
330
331     # load the koha-table data object
332     my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
333
334     # update MARC subfield that stores biblioitems.cn_sort
335     _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
336
337     # update the MARC record (that now contains biblio and items) with the new record data
338     &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
339
340     # modify the other koha tables
341     _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
342     _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
343     return 1;
344 }
345
346 =head2 _strip_item_fields
347
348   _strip_item_fields($record, $frameworkcode)
349
350 Utility routine to remove item tags from a
351 MARC bib.
352
353 =cut
354
355 sub _strip_item_fields {
356     my $record = shift;
357     my $frameworkcode = shift;
358     # get the items before and append them to the biblio before updating the record, atm we just have the biblio
359     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
360
361     # delete any item fields from incoming record to avoid
362     # duplication or incorrect data - use AddItem() or ModItem()
363     # to change items
364     foreach my $field ( $record->field($itemtag) ) {
365         $record->delete_field($field);
366     }
367 }
368
369 =head2 ModBiblioframework
370
371    ModBiblioframework($biblionumber,$frameworkcode);
372
373 Exported function to modify a biblio framework
374
375 =cut
376
377 sub ModBiblioframework {
378     my ( $biblionumber, $frameworkcode ) = @_;
379     my $dbh = C4::Context->dbh;
380     my $sth = $dbh->prepare( "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?" );
381     $sth->execute( $frameworkcode, $biblionumber );
382     return 1;
383 }
384
385 =head2 DelBiblio
386
387   my $error = &DelBiblio($dbh,$biblionumber);
388
389 Exported function (core API) for deleting a biblio in koha.
390 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
391 Also backs it up to deleted* tables
392 Checks to make sure there are not issues on any of the items
393 return:
394 C<$error> : undef unless an error occurs
395
396 =cut
397
398 sub DelBiblio {
399     my ($biblionumber) = @_;
400     my $dbh = C4::Context->dbh;
401     my $error;    # for error handling
402
403     # First make sure this biblio has no items attached
404     my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
405     $sth->execute($biblionumber);
406     if ( my $itemnumber = $sth->fetchrow ) {
407
408         # Fix this to use a status the template can understand
409         $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
410     }
411
412     return $error if $error;
413
414     # We delete attached subscriptions
415     my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
416     foreach my $subscription (@$subscriptions) {
417         &C4::Serials::DelSubscription( $subscription->{subscriptionid} );
418     }
419
420     # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
421     # for at least 2 reasons :
422     # - we need to read the biblio if NoZebra is set (to remove it from the indexes
423     # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
424     #   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)
425     my $oldRecord;
426     if ( C4::Context->preference("NoZebra") ) {
427
428         # only NoZebra indexing needs to have
429         # the previous version of the record
430         $oldRecord = GetMarcBiblio($biblionumber);
431     }
432     ModZebra( $biblionumber, "recordDelete", "biblioserver", $oldRecord, undef );
433
434     # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
435     $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
436     $sth->execute($biblionumber);
437     while ( my $biblioitemnumber = $sth->fetchrow ) {
438
439         # delete this biblioitem
440         $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
441         return $error if $error;
442     }
443
444     # delete biblio from Koha tables and save in deletedbiblio
445     # must do this *after* _koha_delete_biblioitems, otherwise
446     # delete cascade will prevent deletedbiblioitems rows
447     # from being generated by _koha_delete_biblioitems
448     $error = _koha_delete_biblio( $dbh, $biblionumber );
449
450     logaction( "CATALOGUING", "DELETE", $biblionumber, "" ) if C4::Context->preference("CataloguingLog");
451
452     return;
453 }
454
455 =head2 LinkBibHeadingsToAuthorities
456
457   my $headings_linked = LinkBibHeadingsToAuthorities($marc);
458
459 Links bib headings to authority records by checking
460 each authority-controlled field in the C<MARC::Record>
461 object C<$marc>, looking for a matching authority record,
462 and setting the linking subfield $9 to the ID of that
463 authority record.  
464
465 If no matching authority exists, or if multiple
466 authorities match, no $9 will be added, and any 
467 existing one inthe field will be deleted.
468
469 Returns the number of heading links changed in the
470 MARC record.
471
472 =cut
473
474 sub LinkBibHeadingsToAuthorities {
475     my $bib = shift;
476
477     my $num_headings_changed = 0;
478     foreach my $field ( $bib->fields() ) {
479         my $heading = C4::Heading->new_from_bib_field($field);
480         next unless defined $heading;
481
482         # check existing $9
483         my $current_link = $field->subfield('9');
484
485         # look for matching authorities
486         my $authorities = $heading->authorities();
487
488         # want only one exact match
489         if ( $#{$authorities} == 0 ) {
490             my $authority = MARC::Record->new_from_usmarc( $authorities->[0] );
491             my $authid    = $authority->field('001')->data();
492             next if defined $current_link and $current_link eq $authid;
493
494             $field->delete_subfield( code => '9' ) if defined $current_link;
495             $field->add_subfields( '9', $authid );
496             $num_headings_changed++;
497         } else {
498             if ( defined $current_link ) {
499                 $field->delete_subfield( code => '9' );
500                 $num_headings_changed++;
501             }
502         }
503
504     }
505     return $num_headings_changed;
506 }
507
508 =head2 GetRecordValue
509
510   my $values = GetRecordValue($field, $record, $frameworkcode);
511
512 Get MARC fields from a keyword defined in fieldmapping table.
513
514 =cut
515
516 sub GetRecordValue {
517     my ( $field, $record, $frameworkcode ) = @_;
518     my $dbh = C4::Context->dbh;
519
520     my $sth = $dbh->prepare('SELECT fieldcode, subfieldcode FROM fieldmapping WHERE frameworkcode = ? AND field = ?');
521     $sth->execute( $frameworkcode, $field );
522
523     my @result = ();
524
525     while ( my $row = $sth->fetchrow_hashref ) {
526         foreach my $field ( $record->field( $row->{fieldcode} ) ) {
527             if ( ( $row->{subfieldcode} ne "" && $field->subfield( $row->{subfieldcode} ) ) ) {
528                 foreach my $subfield ( $field->subfield( $row->{subfieldcode} ) ) {
529                     push @result, { 'subfield' => $subfield };
530                 }
531
532             } elsif ( $row->{subfieldcode} eq "" ) {
533                 push @result, { 'subfield' => $field->as_string() };
534             }
535         }
536     }
537
538     return \@result;
539 }
540
541 =head2 SetFieldMapping
542
543   SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
544
545 Set a Field to MARC mapping value, if it already exists we don't add a new one.
546
547 =cut
548
549 sub SetFieldMapping {
550     my ( $framework, $field, $fieldcode, $subfieldcode ) = @_;
551     my $dbh = C4::Context->dbh;
552
553     my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
554     $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
555     if ( not $sth->fetchrow_hashref ) {
556         my @args;
557         $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
558
559         $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
560     }
561 }
562
563 =head2 DeleteFieldMapping
564
565   DeleteFieldMapping($id);
566
567 Delete a field mapping from an $id.
568
569 =cut
570
571 sub DeleteFieldMapping {
572     my ($id) = @_;
573     my $dbh = C4::Context->dbh;
574
575     my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
576     $sth->execute($id);
577 }
578
579 =head2 GetFieldMapping
580
581   GetFieldMapping($frameworkcode);
582
583 Get all field mappings for a specified frameworkcode
584
585 =cut
586
587 sub GetFieldMapping {
588     my ($framework) = @_;
589     my $dbh = C4::Context->dbh;
590
591     my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
592     $sth->execute($framework);
593
594     my @return;
595     while ( my $row = $sth->fetchrow_hashref ) {
596         push @return, $row;
597     }
598     return \@return;
599 }
600
601 =head2 GetBiblioData
602
603   $data = &GetBiblioData($biblionumber);
604
605 Returns information about the book with the given biblionumber.
606 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
607 the C<biblio> and C<biblioitems> tables in the
608 Koha database.
609
610 In addition, C<$data-E<gt>{subject}> is the list of the book's
611 subjects, separated by C<" , "> (space, comma, space).
612 If there are multiple biblioitems with the given biblionumber, only
613 the first one is considered.
614
615 =cut
616
617 sub GetBiblioData {
618     my ($bibnum) = @_;
619     my $dbh = C4::Context->dbh;
620
621     #  my $query =  C4::Context->preference('item-level_itypes') ?
622     #   " SELECT * , biblioitems.notes AS bnotes, biblio.notes
623     #       FROM biblio
624     #        LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
625     #       WHERE biblio.biblionumber = ?
626     #        AND biblioitems.biblionumber = biblio.biblionumber
627     #";
628
629     my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
630             FROM biblio
631             LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
632             LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
633             WHERE biblio.biblionumber = ?
634             AND biblioitems.biblionumber = biblio.biblionumber ";
635
636     my $sth = $dbh->prepare($query);
637     $sth->execute($bibnum);
638     my $data;
639     $data = $sth->fetchrow_hashref;
640     $sth->finish;
641
642     return ($data);
643 }    # sub GetBiblioData
644
645 =head2 &GetBiblioItemData
646
647   $itemdata = &GetBiblioItemData($biblioitemnumber);
648
649 Looks up the biblioitem with the given biblioitemnumber. Returns a
650 reference-to-hash. The keys are the fields from the C<biblio>,
651 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
652 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
653
654 =cut
655
656 #'
657 sub GetBiblioItemData {
658     my ($biblioitemnumber) = @_;
659     my $dbh                = C4::Context->dbh;
660     my $query              = "SELECT *,biblioitems.notes AS bnotes
661         FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
662     unless ( C4::Context->preference('item-level_itypes') ) {
663         $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
664     }
665     $query .= " WHERE biblioitemnumber = ? ";
666     my $sth = $dbh->prepare($query);
667     my $data;
668     $sth->execute($biblioitemnumber);
669     $data = $sth->fetchrow_hashref;
670     $sth->finish;
671     return ($data);
672 }    # sub &GetBiblioItemData
673
674 =head2 GetBiblioItemByBiblioNumber
675
676 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
677
678 =cut
679
680 sub GetBiblioItemByBiblioNumber {
681     my ($biblionumber) = @_;
682     my $dbh            = C4::Context->dbh;
683     my $sth            = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
684     my $count          = 0;
685     my @results;
686
687     $sth->execute($biblionumber);
688
689     while ( my $data = $sth->fetchrow_hashref ) {
690         push @results, $data;
691     }
692
693     $sth->finish;
694     return @results;
695 }
696
697 =head2 GetBiblionumberFromItemnumber
698
699
700 =cut
701
702 sub GetBiblionumberFromItemnumber {
703     my ($itemnumber) = @_;
704     my $dbh            = C4::Context->dbh;
705     my $sth            = $dbh->prepare("Select biblionumber FROM items WHERE itemnumber = ?");
706
707     $sth->execute($itemnumber);
708     my ($result) = $sth->fetchrow;
709     return ($result);
710 }
711
712 =head2 GetBiblioFromItemNumber
713
714   $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
715
716 Looks up the item with the given itemnumber. if undef, try the barcode.
717
718 C<&itemnodata> returns a reference-to-hash whose keys are the fields
719 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
720 database.
721
722 =cut
723
724 #'
725 sub GetBiblioFromItemNumber {
726     my ( $itemnumber, $barcode ) = @_;
727     my $dbh = C4::Context->dbh;
728     my $sth;
729     if ($itemnumber) {
730         $sth = $dbh->prepare(
731             "SELECT * FROM items 
732             LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
733             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
734              WHERE items.itemnumber = ?"
735         );
736         $sth->execute($itemnumber);
737     } else {
738         $sth = $dbh->prepare(
739             "SELECT * FROM items 
740             LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
741             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
742              WHERE items.barcode = ?"
743         );
744         $sth->execute($barcode);
745     }
746     my $data = $sth->fetchrow_hashref;
747     $sth->finish;
748     return ($data);
749 }
750
751 =head2 GetISBDView 
752
753   $isbd = &GetISBDView($biblionumber);
754
755 Return the ISBD view which can be included in opac and intranet
756
757 =cut
758
759 sub GetISBDView {
760     my ( $biblionumber, $template ) = @_;
761     my $record   = GetMarcBiblio($biblionumber, 1);
762     return undef unless defined $record;
763     my $itemtype = &GetFrameworkCode($biblionumber);
764     my ( $holdingbrtagf, $holdingbrtagsubf ) = &GetMarcFromKohaField( "items.holdingbranch", $itemtype );
765     my $tagslib = &GetMarcStructure( 1, $itemtype );
766
767     my $ISBD = C4::Context->preference('isbd');
768     my $bloc = $ISBD;
769     my $res;
770     my $blocres;
771
772     foreach my $isbdfield ( split( /#/, $bloc ) ) {
773
774         #         $isbdfield= /(.?.?.?)/;
775         $isbdfield =~ /(\d\d\d)([^\|])?\|(.*)\|(.*)\|(.*)/;
776         my $fieldvalue = $1 || 0;
777         my $subfvalue  = $2 || "";
778         my $textbefore = $3;
779         my $analysestring = $4;
780         my $textafter     = $5;
781
782         #         warn "==> $1 / $2 / $3 / $4";
783         #         my $fieldvalue=substr($isbdfield,0,3);
784         if ( $fieldvalue > 0 ) {
785             my $hasputtextbefore = 0;
786             my @fieldslist       = $record->field($fieldvalue);
787             @fieldslist = sort { $a->subfield($holdingbrtagsubf) cmp $b->subfield($holdingbrtagsubf) } @fieldslist if ( $fieldvalue eq $holdingbrtagf );
788
789             #         warn "ERROR IN ISBD DEFINITION at : $isbdfield" unless $fieldvalue;
790             #             warn "FV : $fieldvalue";
791             if ( $subfvalue ne "" ) {
792                 foreach my $field (@fieldslist) {
793                     foreach my $subfield ( $field->subfield($subfvalue) ) {
794                         my $calculated = $analysestring;
795                         my $tag        = $field->tag();
796                         if ( $tag < 10 ) {
797                         } else {
798                             my $subfieldvalue = GetAuthorisedValueDesc( $tag, $subfvalue, $subfield, '', $tagslib );
799                             my $tagsubf = $tag . $subfvalue;
800                             $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
801                             if ( $template eq "opac" ) { $calculated =~ s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
802
803                             # field builded, store the result
804                             if ( $calculated && !$hasputtextbefore ) {    # put textbefore if not done
805                                 $blocres .= $textbefore;
806                                 $hasputtextbefore = 1;
807                             }
808
809                             # remove punctuation at start
810                             $calculated =~ s/^( |;|:|\.|-)*//g;
811                             $blocres .= $calculated;
812
813                         }
814                     }
815                 }
816                 $blocres .= $textafter if $hasputtextbefore;
817             } else {
818                 foreach my $field (@fieldslist) {
819                     my $calculated = $analysestring;
820                     my $tag        = $field->tag();
821                     if ( $tag < 10 ) {
822                     } else {
823                         my @subf = $field->subfields;
824                         for my $i ( 0 .. $#subf ) {
825                             my $valuecode     = $subf[$i][1];
826                             my $subfieldcode  = $subf[$i][0];
827                             my $subfieldvalue = GetAuthorisedValueDesc( $tag, $subf[$i][0], $subf[$i][1], '', $tagslib );
828                             my $tagsubf       = $tag . $subfieldcode;
829
830                             $calculated =~ s/                  # replace all {{}} codes by the value code.
831                                   \{\{$tagsubf\}\} # catch the {{actualcode}}
832                                 /
833                                   $valuecode     # replace by the value code
834                                /gx;
835
836                             $calculated =~ s/\{(.?.?.?.?)$tagsubf(.*?)\}/$1$subfieldvalue$2\{$1$tagsubf$2\}/g;
837                             if ( $template eq "opac" ) { $calculated =~ s#/cgi-bin/koha/[^/]+/([^.]*.pl\?.*)$#opac-$1#g; }
838                         }
839
840                         # field builded, store the result
841                         if ( $calculated && !$hasputtextbefore ) {    # put textbefore if not done
842                             $blocres .= $textbefore;
843                             $hasputtextbefore = 1;
844                         }
845
846                         # remove punctuation at start
847                         $calculated =~ s/^( |;|:|\.|-)*//g;
848                         $blocres .= $calculated;
849                     }
850                 }
851                 $blocres .= $textafter if $hasputtextbefore;
852             }
853         } else {
854             $blocres .= $isbdfield;
855         }
856     }
857     $res .= $blocres;
858
859     $res =~ s/\{(.*?)\}//g;
860     $res =~ s/\\n/\n/g;
861     $res =~ s/\n/<br\/>/g;
862
863     # remove empty ()
864     $res =~ s/\(\)//g;
865
866     return $res;
867 }
868
869 =head2 GetBiblio
870
871   ( $count, @results ) = &GetBiblio($biblionumber);
872
873 =cut
874
875 sub GetBiblio {
876     my ($biblionumber) = @_;
877     my $dbh            = C4::Context->dbh;
878     my $sth            = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
879     my $count          = 0;
880     my @results;
881     $sth->execute($biblionumber);
882     while ( my $data = $sth->fetchrow_hashref ) {
883         $results[$count] = $data;
884         $count++;
885     }    # while
886     $sth->finish;
887     return ( $count, @results );
888 }    # sub GetBiblio
889
890 =head2 GetBiblioItemInfosOf
891
892   GetBiblioItemInfosOf(@biblioitemnumbers);
893
894 =cut
895
896 sub GetBiblioItemInfosOf {
897     my @biblioitemnumbers = @_;
898
899     my $query = '
900         SELECT biblioitemnumber,
901             publicationyear,
902             itemtype
903         FROM biblioitems
904         WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
905     ';
906     return get_infos_of( $query, 'biblioitemnumber' );
907 }
908
909 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
910
911 =head2 GetMarcStructure
912
913   $res = GetMarcStructure($forlibrarian,$frameworkcode);
914
915 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
916 $forlibrarian  :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
917 $frameworkcode : the framework code to read
918
919 =cut
920
921 # cache for results of GetMarcStructure -- needed
922 # for batch jobs
923 our $marc_structure_cache;
924
925 sub GetMarcStructure {
926     my ( $forlibrarian, $frameworkcode ) = @_;
927     my $dbh = C4::Context->dbh;
928     $frameworkcode = "" unless $frameworkcode;
929
930     if ( defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode} ) {
931         return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
932     }
933
934     #     my $sth = $dbh->prepare(
935     #         "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
936     #     $sth->execute($frameworkcode);
937     #     my ($total) = $sth->fetchrow;
938     #     $frameworkcode = "" unless ( $total > 0 );
939     my $sth = $dbh->prepare(
940         "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable 
941         FROM marc_tag_structure 
942         WHERE frameworkcode=? 
943         ORDER BY tagfield"
944     );
945     $sth->execute($frameworkcode);
946     my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
947
948     while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) = $sth->fetchrow ) {
949         $res->{$tag}->{lib}        = ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
950         $res->{$tag}->{tab}        = "";
951         $res->{$tag}->{mandatory}  = $mandatory;
952         $res->{$tag}->{repeatable} = $repeatable;
953     }
954
955     $sth = $dbh->prepare(
956         "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue 
957          FROM   marc_subfield_structure 
958          WHERE  frameworkcode=? 
959          ORDER BY tagfield,tagsubfield
960         "
961     );
962
963     $sth->execute($frameworkcode);
964
965     my $subfield;
966     my $authorised_value;
967     my $authtypecode;
968     my $value_builder;
969     my $kohafield;
970     my $seealso;
971     my $hidden;
972     my $isurl;
973     my $link;
974     my $defaultvalue;
975
976     while (
977         (   $tag,          $subfield,      $liblibrarian, $libopac, $tab,    $mandatory, $repeatable, $authorised_value,
978             $authtypecode, $value_builder, $kohafield,    $seealso, $hidden, $isurl,     $link,       $defaultvalue
979         )
980         = $sth->fetchrow
981       ) {
982         $res->{$tag}->{$subfield}->{lib}              = ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
983         $res->{$tag}->{$subfield}->{tab}              = $tab;
984         $res->{$tag}->{$subfield}->{mandatory}        = $mandatory;
985         $res->{$tag}->{$subfield}->{repeatable}       = $repeatable;
986         $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
987         $res->{$tag}->{$subfield}->{authtypecode}     = $authtypecode;
988         $res->{$tag}->{$subfield}->{value_builder}    = $value_builder;
989         $res->{$tag}->{$subfield}->{kohafield}        = $kohafield;
990         $res->{$tag}->{$subfield}->{seealso}          = $seealso;
991         $res->{$tag}->{$subfield}->{hidden}           = $hidden;
992         $res->{$tag}->{$subfield}->{isurl}            = $isurl;
993         $res->{$tag}->{$subfield}->{'link'}           = $link;
994         $res->{$tag}->{$subfield}->{defaultvalue}     = $defaultvalue;
995     }
996
997     $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
998
999     return $res;
1000 }
1001
1002 =head2 GetUsedMarcStructure
1003
1004 The same function as GetMarcStructure except it just takes field
1005 in tab 0-9. (used field)
1006
1007   my $results = GetUsedMarcStructure($frameworkcode);
1008
1009 C<$results> is a ref to an array which each case containts a ref
1010 to a hash which each keys is the columns from marc_subfield_structure
1011
1012 C<$frameworkcode> is the framework code. 
1013
1014 =cut
1015
1016 sub GetUsedMarcStructure($) {
1017     my $frameworkcode = shift || '';
1018     my $query = qq/
1019         SELECT *
1020         FROM   marc_subfield_structure
1021         WHERE   tab > -1 
1022             AND frameworkcode = ?
1023         ORDER BY tagfield, tagsubfield
1024     /;
1025     my $sth = C4::Context->dbh->prepare($query);
1026     $sth->execute($frameworkcode);
1027     return $sth->fetchall_arrayref( {} );
1028 }
1029
1030 =head2 GetMarcFromKohaField
1031
1032   ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
1033
1034 Returns the MARC fields & subfields mapped to the koha field 
1035 for the given frameworkcode
1036
1037 =cut
1038
1039 sub GetMarcFromKohaField {
1040     my ( $kohafield, $frameworkcode ) = @_;
1041     return 0, 0 unless $kohafield and defined $frameworkcode;
1042     my $relations = C4::Context->marcfromkohafield;
1043     return ( $relations->{$frameworkcode}->{$kohafield}->[0], $relations->{$frameworkcode}->{$kohafield}->[1] );
1044 }
1045
1046 =head2 GetMarcBiblio
1047
1048   my $record = GetMarcBiblio($biblionumber, [$embeditems]);
1049
1050 Returns MARC::Record representing bib identified by
1051 C<$biblionumber>.  If no bib exists, returns undef.
1052 C<$embeditems>.  If set to true, items data are included.
1053 The MARC record contains biblio data, and items data if $embeditems is set to true.
1054
1055 =cut
1056
1057 sub GetMarcBiblio {
1058     my $biblionumber = shift;
1059     my $embeditems   = shift || 0;
1060     my $dbh          = C4::Context->dbh;
1061     my $sth          = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1062     $sth->execute($biblionumber);
1063     my $row     = $sth->fetchrow_hashref;
1064     my $marcxml = StripNonXmlChars( $row->{'marcxml'} );
1065     MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') );
1066     my $record = MARC::Record->new();
1067
1068     if ($marcxml) {
1069         $record = eval { MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour') ) };
1070         if ($@) { warn " problem with :$biblionumber : $@ \n$marcxml"; }
1071         return unless $record;
1072
1073         C4::Biblio::EmbedItemsInMarcBiblio($record, $biblionumber) if ($embeditems);
1074
1075         #      $record = MARC::Record::new_from_usmarc( $marc) if $marc;
1076         return $record;
1077     } else {
1078         return undef;
1079     }
1080 }
1081
1082 =head2 GetXmlBiblio
1083
1084   my $marcxml = GetXmlBiblio($biblionumber);
1085
1086 Returns biblioitems.marcxml of the biblionumber passed in parameter.
1087 The XML contains both biblio & item datas
1088
1089 =cut
1090
1091 sub GetXmlBiblio {
1092     my ($biblionumber) = @_;
1093     my $dbh            = C4::Context->dbh;
1094     my $sth            = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
1095     $sth->execute($biblionumber);
1096     my ($marcxml) = $sth->fetchrow;
1097     return $marcxml;
1098 }
1099
1100 =head2 GetCOinSBiblio
1101
1102   my $coins = GetCOinSBiblio($biblionumber);
1103
1104 Returns the COinS(a span) which can be included in a biblio record
1105
1106 =cut
1107
1108 sub GetCOinSBiblio {
1109     my ($biblionumber) = @_;
1110     my $record = GetMarcBiblio($biblionumber);
1111
1112     # get the coin format
1113     if ( ! $record ) {
1114         # can't get a valid MARC::Record object, bail out at this point
1115         warn "We called GetMarcBiblio with a biblionumber that doesn't exist biblionumber=$biblionumber";
1116         return;
1117     }
1118     my $pos7 = substr $record->leader(), 7, 1;
1119     my $pos6 = substr $record->leader(), 6, 1;
1120     my $mtx;
1121     my $genre;
1122     my ( $aulast, $aufirst ) = ( '', '' );
1123     my $oauthors  = '';
1124     my $title     = '';
1125     my $subtitle  = '';
1126     my $pubyear   = '';
1127     my $isbn      = '';
1128     my $issn      = '';
1129     my $publisher = '';
1130     my $pages     = '';
1131     my $titletype = 'b';
1132
1133     # For the purposes of generating COinS metadata, LDR/06-07 can be
1134     # considered the same for UNIMARC and MARC21
1135     my $fmts6;
1136     my $fmts7;
1137     %$fmts6 = (
1138                 'a' => 'book',
1139                 'b' => 'manuscript',
1140                 'c' => 'book',
1141                 'd' => 'manuscript',
1142                 'e' => 'map',
1143                 'f' => 'map',
1144                 'g' => 'film',
1145                 'i' => 'audioRecording',
1146                 'j' => 'audioRecording',
1147                 'k' => 'artwork',
1148                 'l' => 'document',
1149                 'm' => 'computerProgram',
1150                 'o' => 'document',
1151                 'r' => 'document',
1152             );
1153     %$fmts7 = (
1154                     'a' => 'journalArticle',
1155                     's' => 'journal',
1156               );
1157
1158     $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book';
1159
1160     if ( $genre eq 'book' ) {
1161             $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
1162     }
1163
1164     ##### We must transform mtx to a valable mtx and document type ####
1165     if ( $genre eq 'book' ) {
1166             $mtx = 'book';
1167     } elsif ( $genre eq 'journal' ) {
1168             $mtx = 'journal';
1169             $titletype = 'j';
1170     } elsif ( $genre eq 'journalArticle' ) {
1171             $mtx   = 'journal';
1172             $genre = 'article';
1173             $titletype = 'a';
1174     } else {
1175             $mtx = 'dc';
1176     }
1177
1178     $genre = ( $mtx eq 'dc' ) ? "&amp;rft.type=$genre" : "&amp;rft.genre=$genre";
1179
1180     if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
1181
1182         # Setting datas
1183         $aulast  = $record->subfield( '700', 'a' ) || '';
1184         $aufirst = $record->subfield( '700', 'b' ) || '';
1185         $oauthors = "&amp;rft.au=$aufirst $aulast";
1186
1187         # others authors
1188         if ( $record->field('200') ) {
1189             for my $au ( $record->field('200')->subfield('g') ) {
1190                 $oauthors .= "&amp;rft.au=$au";
1191             }
1192         }
1193         $title =
1194           ( $mtx eq 'dc' )
1195           ? "&amp;rft.title=" . $record->subfield( '200', 'a' )
1196           : "&amp;rft.title=" . $record->subfield( '200', 'a' ) . "&amp;rft.btitle=" . $record->subfield( '200', 'a' );
1197         $pubyear   = $record->subfield( '210', 'd' ) || '';
1198         $publisher = $record->subfield( '210', 'c' ) || '';
1199         $isbn      = $record->subfield( '010', 'a' ) || '';
1200         $issn      = $record->subfield( '011', 'a' ) || '';
1201     } else {
1202
1203         # MARC21 need some improve
1204
1205         # Setting datas
1206         if ( $record->field('100') ) {
1207             $oauthors .= "&amp;rft.au=" . $record->subfield( '100', 'a' );
1208         }
1209
1210         # others authors
1211         if ( $record->field('700') ) {
1212             for my $au ( $record->field('700')->subfield('a') ) {
1213                 $oauthors .= "&amp;rft.au=$au";
1214             }
1215         }
1216         $title = "&amp;rft." . $titletype . "title=" . $record->subfield( '245', 'a' );
1217         $subtitle = $record->subfield( '245', 'b' ) || '';
1218         $title .= $subtitle;
1219         if ($titletype eq 'a') {
1220             $pubyear   = substr $record->field('008')->data(), 7, 4;
1221             $isbn      = $record->subfield( '773', 'z' ) || '';
1222             $issn      = $record->subfield( '773', 'x' ) || '';
1223             if ($mtx eq 'journal') {
1224                 $title    .= "&amp;rft.title=" . (($record->subfield( '773', 't' ) || $record->subfield( '773', 'a')));
1225             } else {
1226                 $title    .= "&amp;rft.btitle=" . (($record->subfield( '773', 't' ) || $record->subfield( '773', 'a')) || '');
1227             }
1228             foreach my $rel ($record->subfield( '773', 'g' )) {
1229                 if ($pages) {
1230                     $pages .= ', ';
1231                 }
1232                 $pages .= $rel;
1233             }
1234         } else {
1235             $pubyear   = $record->subfield( '260', 'c' ) || '';
1236             $publisher = $record->subfield( '260', 'b' ) || '';
1237             $isbn      = $record->subfield( '020', 'a' ) || '';
1238             $issn      = $record->subfield( '022', 'a' ) || '';
1239         }
1240
1241     }
1242     my $coins_value =
1243 "ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&amp;rft.isbn=$isbn&amp;rft.issn=$issn&amp;rft.aulast=$aulast&amp;rft.aufirst=$aufirst$oauthors&amp;rft.pub=$publisher&amp;rft.date=$pubyear&amp;rft.pages=$pages";
1244     $coins_value =~ s/(\ |&[^a])/\+/g;
1245     $coins_value =~ s/\"/\&quot\;/g;
1246
1247 #<!-- TMPL_VAR NAME="ocoins_format" -->&amp;rft.au=<!-- TMPL_VAR NAME="author" -->&amp;rft.btitle=<!-- TMPL_VAR NAME="title" -->&amp;rft.date=<!-- TMPL_VAR NAME="publicationyear" -->&amp;rft.pages=<!-- TMPL_VAR NAME="pages" -->&amp;rft.isbn=<!-- TMPL_VAR NAME=amazonisbn -->&amp;rft.aucorp=&amp;rft.place=<!-- TMPL_VAR NAME="place" -->&amp;rft.pub=<!-- TMPL_VAR NAME="publishercode" -->&amp;rft.edition=<!-- TMPL_VAR NAME="edition" -->&amp;rft.series=<!-- TMPL_VAR NAME="series" -->&amp;rft.genre="
1248
1249     return $coins_value;
1250 }
1251
1252
1253 =head2 GetMarcPrice
1254
1255 return the prices in accordance with the Marc format.
1256 =cut
1257
1258 sub GetMarcPrice {
1259     my ( $record, $marcflavour ) = @_;
1260     my @listtags;
1261     my $subfield;
1262     
1263     if ( $marcflavour eq "MARC21" ) {
1264         @listtags = ('345', '020');
1265         $subfield="c";
1266     } elsif ( $marcflavour eq "UNIMARC" ) {
1267         @listtags = ('345', '010');
1268         $subfield="d";
1269     } else {
1270         return;
1271     }
1272     
1273     for my $field ( $record->field(@listtags) ) {
1274         for my $subfield_value  ($field->subfield($subfield)){
1275             #check value
1276             return $subfield_value if ($subfield_value);
1277         }
1278     }
1279     return 0; # no price found
1280 }
1281
1282 =head2 GetMarcQuantity
1283
1284 return the quantity of a book. Used in acquisition only, when importing a file an iso2709 from a bookseller
1285 Warning : this is not really in the marc standard. In Unimarc, Electre (the most widely used bookseller) use the 969$a
1286
1287 =cut
1288
1289 sub GetMarcQuantity {
1290     my ( $record, $marcflavour ) = @_;
1291     my @listtags;
1292     my $subfield;
1293     
1294     if ( $marcflavour eq "MARC21" ) {
1295         return 0
1296     } elsif ( $marcflavour eq "UNIMARC" ) {
1297         @listtags = ('969');
1298         $subfield="a";
1299     } else {
1300         return;
1301     }
1302     
1303     for my $field ( $record->field(@listtags) ) {
1304         for my $subfield_value  ($field->subfield($subfield)){
1305             #check value
1306             if ($subfield_value) {
1307                  # in France, the cents separator is the , but sometimes, ppl use a .
1308                  # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
1309                 $subfield_value =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
1310                 return $subfield_value;
1311             }
1312         }
1313     }
1314     return 0; # no price found
1315 }
1316
1317
1318 =head2 GetAuthorisedValueDesc
1319
1320   my $subfieldvalue =get_authorised_value_desc(
1321     $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category, $opac);
1322
1323 Retrieve the complete description for a given authorised value.
1324
1325 Now takes $category and $value pair too.
1326
1327   my $auth_value_desc =GetAuthorisedValueDesc(
1328     '','', 'DVD' ,'','','CCODE');
1329
1330 If the optional $opac parameter is set to a true value, displays OPAC 
1331 descriptions rather than normal ones when they exist.
1332
1333 =cut
1334
1335 sub GetAuthorisedValueDesc {
1336     my ( $tag, $subfield, $value, $framework, $tagslib, $category, $opac ) = @_;
1337     my $dbh = C4::Context->dbh;
1338
1339     if ( !$category ) {
1340
1341         return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1342
1343         #---- branch
1344         if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1345             return C4::Branch::GetBranchName($value);
1346         }
1347
1348         #---- itemtypes
1349         if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1350             return getitemtypeinfo($value)->{description};
1351         }
1352
1353         #---- "true" authorized value
1354         $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1355     }
1356
1357     if ( $category ne "" ) {
1358         my $sth = $dbh->prepare( "SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ?" );
1359         $sth->execute( $category, $value );
1360         my $data = $sth->fetchrow_hashref;
1361         return ( $opac && $data->{'lib_opac'} ) ? $data->{'lib_opac'} : $data->{'lib'};
1362     } else {
1363         return $value;    # if nothing is found return the original value
1364     }
1365 }
1366
1367 =head2 GetMarcControlnumber
1368
1369   $marccontrolnumber = GetMarcControlnumber($record,$marcflavour);
1370
1371 Get the control number / record Identifier from the MARC record and return it.
1372
1373 =cut
1374
1375 sub GetMarcControlnumber {
1376     my ( $record, $marcflavour ) = @_;
1377     my $controlnumber = "";
1378     # Control number or Record identifier are the same field in MARC21 and UNIMARC
1379     # Keep $marcflavour for possible later use
1380     if ($marcflavour eq "MARC21" || $marcflavour eq "UNIMARC") {
1381         my $controlnumberField = $record->field('001');
1382         if ($controlnumberField) {
1383             $controlnumber = $controlnumberField->data();
1384         }
1385     }
1386     return $controlnumber;
1387 }
1388
1389 =head2 GetMarcISBN
1390
1391   $marcisbnsarray = GetMarcISBN( $record, $marcflavour );
1392
1393 Get all ISBNs from the MARC record and returns them in an array.
1394 ISBNs stored in differents places depending on MARC flavour
1395
1396 =cut
1397
1398 sub GetMarcISBN {
1399     my ( $record, $marcflavour ) = @_;
1400     my $scope;
1401     if ( $marcflavour eq "UNIMARC" ) {
1402         $scope = '010';
1403     } else {    # assume marc21 if not unimarc
1404         $scope = '020';
1405     }
1406     my @marcisbns;
1407     my $isbn = "";
1408     my $tag  = "";
1409     my $marcisbn;
1410     foreach my $field ( $record->field($scope) ) {
1411         my $value = $field->as_string();
1412         if ( $isbn ne "" ) {
1413             $marcisbn = { marcisbn => $isbn, };
1414             push @marcisbns, $marcisbn;
1415             $isbn = $value;
1416         }
1417         if ( $isbn ne $value ) {
1418             $isbn = $isbn . " " . $value;
1419         }
1420     }
1421
1422     if ($isbn) {
1423         $marcisbn = { marcisbn => $isbn };
1424         push @marcisbns, $marcisbn;    #load last tag into array
1425     }
1426     return \@marcisbns;
1427 }    # end GetMarcISBN
1428
1429 =head2 GetMarcNotes
1430
1431   $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1432
1433 Get all notes from the MARC record and returns them in an array.
1434 The note are stored in differents places depending on MARC flavour
1435
1436 =cut
1437
1438 sub GetMarcNotes {
1439     my ( $record, $marcflavour ) = @_;
1440     my $scope;
1441     if ( $marcflavour eq "UNIMARC" ) {
1442         $scope = '3..';
1443     } else {    # assume marc21 if not unimarc
1444         $scope = '5..';
1445     }
1446     my @marcnotes;
1447     my $note = "";
1448     my $tag  = "";
1449     my $marcnote;
1450     foreach my $field ( $record->field($scope) ) {
1451         my $value = $field->as_string();
1452         if ( $note ne "" ) {
1453             $marcnote = { marcnote => $note, };
1454             push @marcnotes, $marcnote;
1455             $note = $value;
1456         }
1457         if ( $note ne $value ) {
1458             $note = $note . " " . $value;
1459         }
1460     }
1461
1462     if ($note) {
1463         $marcnote = { marcnote => $note };
1464         push @marcnotes, $marcnote;    #load last tag into array
1465     }
1466     return \@marcnotes;
1467 }    # end GetMarcNotes
1468
1469 =head2 GetMarcSubjects
1470
1471   $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1472
1473 Get all subjects from the MARC record and returns them in an array.
1474 The subjects are stored in differents places depending on MARC flavour
1475
1476 =cut
1477
1478 sub GetMarcSubjects {
1479     my ( $record, $marcflavour ) = @_;
1480     my ( $mintag, $maxtag );
1481     if ( $marcflavour eq "UNIMARC" ) {
1482         $mintag = "600";
1483         $maxtag = "611";
1484     } else {    # assume marc21 if not unimarc
1485         $mintag = "600";
1486         $maxtag = "699";
1487     }
1488
1489     my @marcsubjects;
1490     my $subject  = "";
1491     my $subfield = "";
1492     my $marcsubject;
1493
1494     my $subject_limit = C4::Context->preference("TraceCompleteSubfields") ? 'su,complete-subfield' : 'su';
1495
1496     foreach my $field ( $record->field('6..') ) {
1497         next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1498         my @subfields_loop;
1499         my @subfields = $field->subfields();
1500         my $counter   = 0;
1501         my @link_loop;
1502
1503         # if there is an authority link, build the link with an= subfield9
1504         my $found9 = 0;
1505         for my $subject_subfield (@subfields) {
1506
1507             # don't load unimarc subfields 3,4,5
1508             next if ( ( $marcflavour eq "UNIMARC" ) and ( $subject_subfield->[0] =~ /2|3|4|5/ ) );
1509
1510             # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1511             next if ( ( $marcflavour eq "MARC21" ) and ( $subject_subfield->[0] =~ /2/ ) );
1512             my $code      = $subject_subfield->[0];
1513             my $value     = $subject_subfield->[1];
1514             my $linkvalue = $value;
1515             $linkvalue =~ s/(\(|\))//g;
1516             my $operator;
1517             if ( $counter != 0 ) {
1518                 $operator = ' and ';
1519             }
1520             if ( $code eq 9 ) {
1521                 $found9 = 1;
1522                 @link_loop = ( { 'limit' => 'an', link => "$linkvalue" } );
1523             }
1524             if ( not $found9 ) {
1525                 push @link_loop, { 'limit' => $subject_limit, link => $linkvalue, operator => $operator };
1526             }
1527             my $separator;
1528             if ( $counter != 0 ) {
1529                 $separator = C4::Context->preference('authoritysep');
1530             }
1531
1532             # ignore $9
1533             my @this_link_loop = @link_loop;
1534             push @subfields_loop, { code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator } unless ( $subject_subfield->[0] eq 9 );
1535             $counter++;
1536         }
1537
1538         push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1539
1540     }
1541     return \@marcsubjects;
1542 }    #end getMARCsubjects
1543
1544 =head2 GetMarcAuthors
1545
1546   authors = GetMarcAuthors($record,$marcflavour);
1547
1548 Get all authors from the MARC record and returns them in an array.
1549 The authors are stored in differents places depending on MARC flavour
1550
1551 =cut
1552
1553 sub GetMarcAuthors {
1554     my ( $record, $marcflavour ) = @_;
1555     my ( $mintag, $maxtag );
1556
1557     # tagslib useful for UNIMARC author reponsabilities
1558     my $tagslib =
1559       &GetMarcStructure( 1, '' );    # FIXME : we don't have the framework available, we take the default framework. May be buggy on some setups, will be usually correct.
1560     if ( $marcflavour eq "UNIMARC" ) {
1561         $mintag = "700";
1562         $maxtag = "712";
1563     } elsif ( $marcflavour eq "MARC21" || $marcflavour eq "NORMARC" ) { # assume marc21 or normarc if not unimarc
1564         $mintag = "700";
1565         $maxtag = "720";
1566     } else {
1567         return;
1568     }
1569     my @marcauthors;
1570
1571     foreach my $field ( $record->fields ) {
1572         next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1573         my @subfields_loop;
1574         my @link_loop;
1575         my @subfields  = $field->subfields();
1576         my $count_auth = 0;
1577
1578         # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1579         my $subfield9 = $field->subfield('9');
1580         for my $authors_subfield (@subfields) {
1581
1582             # don't load unimarc subfields 3, 5
1583             next if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /3|5/ ) );
1584             my $subfieldcode = $authors_subfield->[0];
1585             my $value        = $authors_subfield->[1];
1586             my $linkvalue    = $value;
1587             $linkvalue =~ s/(\(|\))//g;
1588             my $operator;
1589             if ( $count_auth != 0 ) {
1590                 $operator = ' and ';
1591             }
1592
1593             # if we have an authority link, use that as the link, otherwise use standard searching
1594             if ($subfield9) {
1595                 @link_loop = ( { 'limit' => 'an', link => "$subfield9" } );
1596             } else {
1597
1598                 # reset $linkvalue if UNIMARC author responsibility
1599                 if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] eq "4" ) ) {
1600                     $linkvalue = "(" . GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) . ")";
1601                 }
1602                 push @link_loop, { 'limit' => 'au', link => $linkvalue, operator => $operator };
1603             }
1604             $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib )
1605               if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /4/ ) );
1606             my @this_link_loop = @link_loop;
1607             my $separator;
1608             if ( $count_auth != 0 ) {
1609                 $separator = C4::Context->preference('authoritysep');
1610             }
1611             push @subfields_loop,
1612               { code      => $subfieldcode,
1613                 value     => $value,
1614                 link_loop => \@this_link_loop,
1615                 separator => $separator
1616               }
1617               unless ( $authors_subfield->[0] eq '9' );
1618             $count_auth++;
1619         }
1620         push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1621     }
1622     return \@marcauthors;
1623 }
1624
1625 =head2 GetMarcUrls
1626
1627   $marcurls = GetMarcUrls($record,$marcflavour);
1628
1629 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1630 Assumes web resources (not uncommon in MARC21 to omit resource type ind) 
1631
1632 =cut
1633
1634 sub GetMarcUrls {
1635     my ( $record, $marcflavour ) = @_;
1636
1637     my @marcurls;
1638     for my $field ( $record->field('856') ) {
1639         my @notes;
1640         for my $note ( $field->subfield('z') ) {
1641             push @notes, { note => $note };
1642         }
1643         my @urls = $field->subfield('u');
1644         foreach my $url (@urls) {
1645             my $marcurl;
1646             if ( $marcflavour eq 'MARC21' ) {
1647                 my $s3   = $field->subfield('3');
1648                 my $link = $field->subfield('y');
1649                 unless ( $url =~ /^\w+:/ ) {
1650                     if ( $field->indicator(1) eq '7' ) {
1651                         $url = $field->subfield('2') . "://" . $url;
1652                     } elsif ( $field->indicator(1) eq '1' ) {
1653                         $url = 'ftp://' . $url;
1654                     } else {
1655
1656                         #  properly, this should be if ind1=4,
1657                         #  however we will assume http protocol since we're building a link.
1658                         $url = 'http://' . $url;
1659                     }
1660                 }
1661
1662                 # TODO handle ind 2 (relationship)
1663                 $marcurl = {
1664                     MARCURL => $url,
1665                     notes   => \@notes,
1666                 };
1667                 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1668                 $marcurl->{'part'} = $s3 if ($link);
1669                 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^[Tt]able/ );
1670             } else {
1671                 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1672                 $marcurl->{'MARCURL'} = $url;
1673             }
1674             push @marcurls, $marcurl;
1675         }
1676     }
1677     return \@marcurls;
1678 }
1679
1680 =head2 GetMarcSeries
1681
1682   $marcseriesarray = GetMarcSeries($record,$marcflavour);
1683
1684 Get all series from the MARC record and returns them in an array.
1685 The series are stored in differents places depending on MARC flavour
1686
1687 =cut
1688
1689 sub GetMarcSeries {
1690     my ( $record, $marcflavour ) = @_;
1691     my ( $mintag, $maxtag );
1692     if ( $marcflavour eq "UNIMARC" ) {
1693         $mintag = "600";
1694         $maxtag = "619";
1695     } else {    # assume marc21 if not unimarc
1696         $mintag = "440";
1697         $maxtag = "490";
1698     }
1699
1700     my @marcseries;
1701     my $subjct   = "";
1702     my $subfield = "";
1703     my $marcsubjct;
1704
1705     foreach my $field ( $record->field('440'), $record->field('490') ) {
1706         my @subfields_loop;
1707
1708         #my $value = $field->subfield('a');
1709         #$marcsubjct = {MARCSUBJCT => $value,};
1710         my @subfields = $field->subfields();
1711
1712         #warn "subfields:".join " ", @$subfields;
1713         my $counter = 0;
1714         my @link_loop;
1715         for my $series_subfield (@subfields) {
1716             my $volume_number;
1717             undef $volume_number;
1718
1719             # see if this is an instance of a volume
1720             if ( $series_subfield->[0] eq 'v' ) {
1721                 $volume_number = 1;
1722             }
1723
1724             my $code      = $series_subfield->[0];
1725             my $value     = $series_subfield->[1];
1726             my $linkvalue = $value;
1727             $linkvalue =~ s/(\(|\))//g;
1728             if ( $counter != 0 ) {
1729                 push @link_loop, { link => $linkvalue, operator => ' and ', };
1730             } else {
1731                 push @link_loop, { link => $linkvalue, operator => undef, };
1732             }
1733             my $separator;
1734             if ( $counter != 0 ) {
1735                 $separator = C4::Context->preference('authoritysep');
1736             }
1737             if ($volume_number) {
1738                 push @subfields_loop, { volumenum => $value };
1739             } else {
1740                 if ( $series_subfield->[0] ne '9' ) {
1741                     push @subfields_loop, {
1742                         code      => $code,
1743                         value     => $value,
1744                         link_loop => \@link_loop,
1745                         separator => $separator,
1746                         volumenum => $volume_number,
1747                     };
1748                 }
1749             }
1750             $counter++;
1751         }
1752         push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1753
1754         #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1755         #push @marcsubjcts, $marcsubjct;
1756         #$subjct = $value;
1757
1758     }
1759     my $marcseriessarray = \@marcseries;
1760     return $marcseriessarray;
1761 }    #end getMARCseriess
1762
1763 =head2 GetFrameworkCode
1764
1765   $frameworkcode = GetFrameworkCode( $biblionumber )
1766
1767 =cut
1768
1769 sub GetFrameworkCode {
1770     my ($biblionumber) = @_;
1771     my $dbh            = C4::Context->dbh;
1772     my $sth            = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1773     $sth->execute($biblionumber);
1774     my ($frameworkcode) = $sth->fetchrow;
1775     return $frameworkcode;
1776 }
1777
1778 =head2 TransformKohaToMarc
1779
1780     $record = TransformKohaToMarc( $hash )
1781
1782 This function builds partial MARC::Record from a hash
1783 Hash entries can be from biblio or biblioitems.
1784
1785 This function is called in acquisition module, to create a basic catalogue entry from user entry
1786
1787 =cut
1788
1789 sub TransformKohaToMarc {
1790     my ($hash) = @_;
1791     my $sth    = C4::Context->dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1792     my $record = MARC::Record->new();
1793     SetMarcUnicodeFlag( $record, C4::Context->preference("marcflavour") );
1794     foreach ( keys %{$hash} ) {
1795         &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
1796     }
1797     return $record;
1798 }
1799
1800 =head2 TransformKohaToMarcOneField
1801
1802     $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1803
1804 =cut
1805
1806 sub TransformKohaToMarcOneField {
1807     my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1808     $frameworkcode = '' unless $frameworkcode;
1809     my $tagfield;
1810     my $tagsubfield;
1811
1812     if ( !defined $sth ) {
1813         my $dbh = C4::Context->dbh;
1814         $sth = $dbh->prepare( "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?" );
1815     }
1816     $sth->execute( $frameworkcode, $kohafieldname );
1817     if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1818         my @values = split(/\s?\|\s?/, $value, -1);
1819         
1820         foreach my $itemvalue (@values){
1821         my $tag = $record->field($tagfield);
1822         if ($tag) {
1823                 $tag->add_subfields( $tagsubfield => $itemvalue );
1824             $record->delete_field($tag);
1825             $record->insert_fields_ordered($tag);
1826             }
1827             else {
1828                 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $itemvalue );
1829             }
1830         }
1831     }
1832     return $record;
1833 }
1834
1835 =head2 TransformHtmlToXml
1836
1837   $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, 
1838                              $ind_tag, $auth_type )
1839
1840 $auth_type contains :
1841
1842 =over
1843
1844 =item - nothing : rebuild a biblio. In UNIMARC the encoding is in 100$a pos 26/27
1845
1846 =item - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1847
1848 =item - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1849
1850 =back
1851
1852 =cut
1853
1854 sub TransformHtmlToXml {
1855     my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1856     my $xml = MARC::File::XML::header('UTF-8');
1857     $xml .= "<record>\n";
1858     $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1859     MARC::File::XML->default_record_format($auth_type);
1860
1861     # in UNIMARC, field 100 contains the encoding
1862     # check that there is one, otherwise the
1863     # MARC::Record->new_from_xml will fail (and Koha will die)
1864     my $unimarc_and_100_exist = 0;
1865     $unimarc_and_100_exist = 1 if $auth_type eq 'ITEM';    # if we rebuild an item, no need of a 100 field
1866     my $prevvalue;
1867     my $prevtag = -1;
1868     my $first   = 1;
1869     my $j       = -1;
1870     for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1871
1872         if ( C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a" ) {
1873
1874             # if we have a 100 field and it's values are not correct, skip them.
1875             # if we don't have any valid 100 field, we will create a default one at the end
1876             my $enc = substr( @$values[$i], 26, 2 );
1877             if ( $enc eq '01' or $enc eq '50' or $enc eq '03' ) {
1878                 $unimarc_and_100_exist = 1;
1879             } else {
1880                 next;
1881             }
1882         }
1883         @$values[$i] =~ s/&/&amp;/g;
1884         @$values[$i] =~ s/</&lt;/g;
1885         @$values[$i] =~ s/>/&gt;/g;
1886         @$values[$i] =~ s/"/&quot;/g;
1887         @$values[$i] =~ s/'/&apos;/g;
1888
1889         #         if ( !utf8::is_utf8( @$values[$i] ) ) {
1890         #             utf8::decode( @$values[$i] );
1891         #         }
1892         if ( ( @$tags[$i] ne $prevtag ) ) {
1893             $j++ unless ( @$tags[$i] eq "" );
1894             my $indicator1 = eval { substr( @$indicator[$j], 0, 1 ) };
1895             my $indicator2 = eval { substr( @$indicator[$j], 1, 1 ) };
1896             my $ind1       = _default_ind_to_space($indicator1);
1897             my $ind2;
1898             if ( @$indicator[$j] ) {
1899                 $ind2 = _default_ind_to_space($indicator2);
1900             } else {
1901                 warn "Indicator in @$tags[$i] is empty";
1902                 $ind2 = " ";
1903             }
1904             if ( !$first ) {
1905                 $xml .= "</datafield>\n";
1906                 if (   ( @$tags[$i] && @$tags[$i] > 10 )
1907                     && ( @$values[$i] ne "" ) ) {
1908                     $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1909                     $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1910                     $first = 0;
1911                 } else {
1912                     $first = 1;
1913                 }
1914             } else {
1915                 if ( @$values[$i] ne "" ) {
1916
1917                     # leader
1918                     if ( @$tags[$i] eq "000" ) {
1919                         $xml .= "<leader>@$values[$i]</leader>\n";
1920                         $first = 1;
1921
1922                         # rest of the fixed fields
1923                     } elsif ( @$tags[$i] < 10 ) {
1924                         $xml .= "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1925                         $first = 1;
1926                     } else {
1927                         $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1928                         $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1929                         $first = 0;
1930                     }
1931                 }
1932             }
1933         } else {    # @$tags[$i] eq $prevtag
1934             my $indicator1 = eval { substr( @$indicator[$j], 0, 1 ) };
1935             my $indicator2 = eval { substr( @$indicator[$j], 1, 1 ) };
1936             my $ind1       = _default_ind_to_space($indicator1);
1937             my $ind2;
1938             if ( @$indicator[$j] ) {
1939                 $ind2 = _default_ind_to_space($indicator2);
1940             } else {
1941                 warn "Indicator in @$tags[$i] is empty";
1942                 $ind2 = " ";
1943             }
1944             if ( @$values[$i] eq "" ) {
1945             } else {
1946                 if ($first) {
1947                     $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1948                     $first = 0;
1949                 }
1950                 $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1951             }
1952         }
1953         $prevtag = @$tags[$i];
1954     }
1955     $xml .= "</datafield>\n" if @$tags > 0;
1956     if ( C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist ) {
1957
1958         #     warn "SETTING 100 for $auth_type";
1959         my $string = strftime( "%Y%m%d", localtime(time) );
1960
1961         # set 50 to position 26 is biblios, 13 if authorities
1962         my $pos = 26;
1963         $pos = 13 if $auth_type eq 'UNIMARCAUTH';
1964         $string = sprintf( "%-*s", 35, $string );
1965         substr( $string, $pos, 6, "50" );
1966         $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1967         $xml .= "<subfield code=\"a\">$string</subfield>\n";
1968         $xml .= "</datafield>\n";
1969     }
1970     $xml .= "</record>\n";
1971     $xml .= MARC::File::XML::footer();
1972     return $xml;
1973 }
1974
1975 =head2 _default_ind_to_space
1976
1977 Passed what should be an indicator returns a space
1978 if its undefined or zero length
1979
1980 =cut
1981
1982 sub _default_ind_to_space {
1983     my $s = shift;
1984     if ( !defined $s || $s eq q{} ) {
1985         return ' ';
1986     }
1987     return $s;
1988 }
1989
1990 =head2 TransformHtmlToMarc
1991
1992     L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1993     L<$params> is a ref to an array as below:
1994     {
1995         'tag_010_indicator1_531951' ,
1996         'tag_010_indicator2_531951' ,
1997         'tag_010_code_a_531951_145735' ,
1998         'tag_010_subfield_a_531951_145735' ,
1999         'tag_200_indicator1_873510' ,
2000         'tag_200_indicator2_873510' ,
2001         'tag_200_code_a_873510_673465' ,
2002         'tag_200_subfield_a_873510_673465' ,
2003         'tag_200_code_b_873510_704318' ,
2004         'tag_200_subfield_b_873510_704318' ,
2005         'tag_200_code_e_873510_280822' ,
2006         'tag_200_subfield_e_873510_280822' ,
2007         'tag_200_code_f_873510_110730' ,
2008         'tag_200_subfield_f_873510_110730' ,
2009     }
2010     L<$cgi> is the CGI object which containts the value.
2011     L<$record> is the MARC::Record object.
2012
2013 =cut
2014
2015 sub TransformHtmlToMarc {
2016     my $params = shift;
2017     my $cgi    = shift;
2018
2019     # explicitly turn on the UTF-8 flag for all
2020     # 'tag_' parameters to avoid incorrect character
2021     # conversion later on
2022     my $cgi_params = $cgi->Vars;
2023     foreach my $param_name ( keys %$cgi_params ) {
2024         if ( $param_name =~ /^tag_/ ) {
2025             my $param_value = $cgi_params->{$param_name};
2026             if ( utf8::decode($param_value) ) {
2027                 $cgi_params->{$param_name} = $param_value;
2028             }
2029
2030             # FIXME - need to do something if string is not valid UTF-8
2031         }
2032     }
2033
2034     # creating a new record
2035     my $record = MARC::Record->new();
2036     my $i      = 0;
2037     my @fields;
2038     while ( $params->[$i] ) {    # browse all CGI params
2039         my $param    = $params->[$i];
2040         my $newfield = 0;
2041
2042         # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
2043         if ( $param eq 'biblionumber' ) {
2044             my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
2045             if ( $biblionumbertagfield < 10 ) {
2046                 $newfield = MARC::Field->new( $biblionumbertagfield, $cgi->param($param), );
2047             } else {
2048                 $newfield = MARC::Field->new( $biblionumbertagfield, '', '', "$biblionumbertagsubfield" => $cgi->param($param), );
2049             }
2050             push @fields, $newfield if ($newfield);
2051         } elsif ( $param =~ /^tag_(\d*)_indicator1_/ ) {    # new field start when having 'input name="..._indicator1_..."
2052             my $tag = $1;
2053
2054             my $ind1 = _default_ind_to_space( substr( $cgi->param($param), 0, 1 ) );
2055             my $ind2 = _default_ind_to_space( substr( $cgi->param( $params->[ $i + 1 ] ), 0, 1 ) );
2056             $newfield = 0;
2057             my $j = $i + 2;
2058
2059             if ( $tag < 10 ) {                              # no code for theses fields
2060                                                             # in MARC editor, 000 contains the leader.
2061                 if ( $tag eq '000' ) {
2062                     # Force a fake leader even if not provided to avoid crashing
2063                     # during decoding MARC record containing UTF-8 characters
2064                     $record->leader(
2065                         length( $cgi->param($params->[$j+1]) ) == 24
2066                         ? $cgi->param( $params->[ $j + 1 ] )
2067                         : '     nam a22        4500'
2068                         )
2069                     ;
2070                     # between 001 and 009 (included)
2071                 } elsif ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) {
2072                     $newfield = MARC::Field->new( $tag, $cgi->param( $params->[ $j + 1 ] ), );
2073                 }
2074
2075                 # > 009, deal with subfields
2076             } else {
2077                 while ( defined $params->[$j] && $params->[$j] =~ /_code_/ ) {    # browse all it's subfield
2078                     my $inner_param = $params->[$j];
2079                     if ($newfield) {
2080                         if ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) {         # only if there is a value (code => value)
2081                             $newfield->add_subfields( $cgi->param($inner_param) => $cgi->param( $params->[ $j + 1 ] ) );
2082                         }
2083                     } else {
2084                         if ( $cgi->param( $params->[ $j + 1 ] ) ne '' ) {         # creating only if there is a value (code => value)
2085                             $newfield = MARC::Field->new( $tag, $ind1, $ind2, $cgi->param($inner_param) => $cgi->param( $params->[ $j + 1 ] ), );
2086                         }
2087                     }
2088                     $j += 2;
2089                 }
2090             }
2091             push @fields, $newfield if ($newfield);
2092         }
2093         $i++;
2094     }
2095
2096     $record->append_fields(@fields);
2097     return $record;
2098 }
2099
2100 # cache inverted MARC field map
2101 our $inverted_field_map;
2102
2103 =head2 TransformMarcToKoha
2104
2105   $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
2106
2107 Extract data from a MARC bib record into a hashref representing
2108 Koha biblio, biblioitems, and items fields. 
2109
2110 =cut
2111
2112 sub TransformMarcToKoha {
2113     my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
2114
2115     my $result;
2116     $limit_table = $limit_table || 0;
2117     $frameworkcode = '' unless defined $frameworkcode;
2118
2119     unless ( defined $inverted_field_map ) {
2120         $inverted_field_map = _get_inverted_marc_field_map();
2121     }
2122
2123     my %tables = ();
2124     if ( defined $limit_table && $limit_table eq 'items' ) {
2125         $tables{'items'} = 1;
2126     } else {
2127         $tables{'items'}       = 1;
2128         $tables{'biblio'}      = 1;
2129         $tables{'biblioitems'} = 1;
2130     }
2131
2132     # traverse through record
2133   MARCFIELD: foreach my $field ( $record->fields() ) {
2134         my $tag = $field->tag();
2135         next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
2136         if ( $field->is_control_field() ) {
2137             my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
2138           ENTRY: foreach my $entry ( @{$kohafields} ) {
2139                 my ( $subfield, $table, $column ) = @{$entry};
2140                 next ENTRY unless exists $tables{$table};
2141                 my $key = _disambiguate( $table, $column );
2142                 if ( $result->{$key} ) {
2143                     unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $field->data() eq "" ) ) {
2144                         $result->{$key} .= " | " . $field->data();
2145                     }
2146                 } else {
2147                     $result->{$key} = $field->data();
2148                 }
2149             }
2150         } else {
2151
2152             # deal with subfields
2153           MARCSUBFIELD: foreach my $sf ( $field->subfields() ) {
2154                 my $code = $sf->[0];
2155                 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
2156                 my $value = $sf->[1];
2157               SFENTRY: foreach my $entry ( @{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} } ) {
2158                     my ( $table, $column ) = @{$entry};
2159                     next SFENTRY unless exists $tables{$table};
2160                     my $key = _disambiguate( $table, $column );
2161                     if ( $result->{$key} ) {
2162                         unless ( ( $key eq "biblionumber" or $key eq "biblioitemnumber" ) and ( $value eq "" ) ) {
2163                             $result->{$key} .= " | " . $value;
2164                         }
2165                     } else {
2166                         $result->{$key} = $value;
2167                     }
2168                 }
2169             }
2170         }
2171     }
2172
2173     # modify copyrightdate to keep only the 1st year found
2174     if ( exists $result->{'copyrightdate'} ) {
2175         my $temp = $result->{'copyrightdate'};
2176         $temp =~ m/c(\d\d\d\d)/;
2177         if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) {    # search cYYYY first
2178             $result->{'copyrightdate'} = $1;
2179         } else {                                       # if no cYYYY, get the 1st date.
2180             $temp =~ m/(\d\d\d\d)/;
2181             $result->{'copyrightdate'} = $1;
2182         }
2183     }
2184
2185     # modify publicationyear to keep only the 1st year found
2186     if ( exists $result->{'publicationyear'} ) {
2187         my $temp = $result->{'publicationyear'};
2188         if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) {    # search cYYYY first
2189             $result->{'publicationyear'} = $1;
2190         } else {                                       # if no cYYYY, get the 1st date.
2191             $temp =~ m/(\d\d\d\d)/;
2192             $result->{'publicationyear'} = $1;
2193         }
2194     }
2195
2196     return $result;
2197 }
2198
2199 sub _get_inverted_marc_field_map {
2200     my $field_map = {};
2201     my $relations = C4::Context->marcfromkohafield;
2202
2203     foreach my $frameworkcode ( keys %{$relations} ) {
2204         foreach my $kohafield ( keys %{ $relations->{$frameworkcode} } ) {
2205             next unless @{ $relations->{$frameworkcode}->{$kohafield} };    # not all columns are mapped to MARC tag & subfield
2206             my $tag      = $relations->{$frameworkcode}->{$kohafield}->[0];
2207             my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
2208             my ( $table, $column ) = split /[.]/, $kohafield, 2;
2209             push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
2210             push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
2211         }
2212     }
2213     return $field_map;
2214 }
2215
2216 =head2 _disambiguate
2217
2218   $newkey = _disambiguate($table, $field);
2219
2220 This is a temporary hack to distinguish between the
2221 following sets of columns when using TransformMarcToKoha.
2222
2223   items.cn_source & biblioitems.cn_source
2224   items.cn_sort & biblioitems.cn_sort
2225
2226 Columns that are currently NOT distinguished (FIXME
2227 due to lack of time to fully test) are:
2228
2229   biblio.notes and biblioitems.notes
2230   biblionumber
2231   timestamp
2232   biblioitemnumber
2233
2234 FIXME - this is necessary because prefixing each column
2235 name with the table name would require changing lots
2236 of code and templates, and exposing more of the DB
2237 structure than is good to the UI templates, particularly
2238 since biblio and bibloitems may well merge in a future
2239 version.  In the future, it would also be good to 
2240 separate DB access and UI presentation field names
2241 more.
2242
2243 =cut
2244
2245 sub CountItemsIssued {
2246     my ($biblionumber) = @_;
2247     my $dbh            = C4::Context->dbh;
2248     my $sth            = $dbh->prepare('SELECT COUNT(*) as issuedCount FROM items, issues WHERE items.itemnumber = issues.itemnumber AND items.biblionumber = ?');
2249     $sth->execute($biblionumber);
2250     my $row = $sth->fetchrow_hashref();
2251     return $row->{'issuedCount'};
2252 }
2253
2254 sub _disambiguate {
2255     my ( $table, $column ) = @_;
2256     if ( $column eq "cn_sort" or $column eq "cn_source" ) {
2257         return $table . '.' . $column;
2258     } else {
2259         return $column;
2260     }
2261
2262 }
2263
2264 =head2 get_koha_field_from_marc
2265
2266   $result->{_disambiguate($table, $field)} = 
2267      get_koha_field_from_marc($table,$field,$record,$frameworkcode);
2268
2269 Internal function to map data from the MARC record to a specific non-MARC field.
2270 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
2271
2272 =cut
2273
2274 sub get_koha_field_from_marc {
2275     my ( $koha_table, $koha_column, $record, $frameworkcode ) = @_;
2276     my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table . '.' . $koha_column, $frameworkcode );
2277     my $kohafield;
2278     foreach my $field ( $record->field($tagfield) ) {
2279         if ( $field->tag() < 10 ) {
2280             if ($kohafield) {
2281                 $kohafield .= " | " . $field->data();
2282             } else {
2283                 $kohafield = $field->data();
2284             }
2285         } else {
2286             if ( $field->subfields ) {
2287                 my @subfields = $field->subfields();
2288                 foreach my $subfieldcount ( 0 .. $#subfields ) {
2289                     if ( $subfields[$subfieldcount][0] eq $subfield ) {
2290                         if ($kohafield) {
2291                             $kohafield .= " | " . $subfields[$subfieldcount][1];
2292                         } else {
2293                             $kohafield = $subfields[$subfieldcount][1];
2294                         }
2295                     }
2296                 }
2297             }
2298         }
2299     }
2300     return $kohafield;
2301 }
2302
2303 =head2 TransformMarcToKohaOneField
2304
2305   $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
2306
2307 =cut
2308
2309 sub TransformMarcToKohaOneField {
2310
2311     # FIXME ? if a field has a repeatable subfield that is used in old-db,
2312     # only the 1st will be retrieved...
2313     my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
2314     my $res = "";
2315     my ( $tagfield, $subfield ) = GetMarcFromKohaField( $kohatable . "." . $kohafield, $frameworkcode );
2316     foreach my $field ( $record->field($tagfield) ) {
2317         if ( $field->tag() < 10 ) {
2318             if ( $result->{$kohafield} ) {
2319                 $result->{$kohafield} .= " | " . $field->data();
2320             } else {
2321                 $result->{$kohafield} = $field->data();
2322             }
2323         } else {
2324             if ( $field->subfields ) {
2325                 my @subfields = $field->subfields();
2326                 foreach my $subfieldcount ( 0 .. $#subfields ) {
2327                     if ( $subfields[$subfieldcount][0] eq $subfield ) {
2328                         if ( $result->{$kohafield} ) {
2329                             $result->{$kohafield} .= " | " . $subfields[$subfieldcount][1];
2330                         } else {
2331                             $result->{$kohafield} = $subfields[$subfieldcount][1];
2332                         }
2333                     }
2334                 }
2335             }
2336         }
2337     }
2338     return $result;
2339 }
2340
2341 =head1  OTHER FUNCTIONS
2342
2343
2344 =head2 PrepareItemrecordDisplay
2345
2346   PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber,$frameworkcode);
2347
2348 Returns a hash with all the fields for Display a given item data in a template
2349
2350 The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
2351
2352 =cut
2353
2354 sub PrepareItemrecordDisplay {
2355
2356     my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
2357
2358     my $dbh = C4::Context->dbh;
2359     $frameworkcode = &GetFrameworkCode($bibnum) if $bibnum;
2360     my ( $itemtagfield, $itemtagsubfield ) = &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2361     my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2362
2363     # return nothing if we don't have found an existing framework.
2364     return q{} unless $tagslib;
2365     my $itemrecord;
2366     if ($itemnum) {
2367         $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum );
2368     }
2369     my @loop_data;
2370     my $authorised_values_sth = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib" );
2371     foreach my $tag ( sort keys %{$tagslib} ) {
2372         my $previous_tag = '';
2373         if ( $tag ne '' ) {
2374
2375             # loop through each subfield
2376             my $cntsubf;
2377             foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2378                 next if ( subfield_is_koha_internal_p($subfield) );
2379                 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2380                 my %subfield_data;
2381                 $subfield_data{tag}           = $tag;
2382                 $subfield_data{subfield}      = $subfield;
2383                 $subfield_data{countsubfield} = $cntsubf++;
2384                 $subfield_data{kohafield}     = $tagslib->{$tag}->{$subfield}->{'kohafield'};
2385
2386                 #        $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2387                 $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
2388                 $subfield_data{mandatory}  = $tagslib->{$tag}->{$subfield}->{mandatory};
2389                 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
2390                 $subfield_data{hidden}     = "display:none"
2391                   if $tagslib->{$tag}->{$subfield}->{hidden};
2392                 my ( $x, $defaultvalue );
2393                 if ($itemrecord) {
2394                     ( $x, $defaultvalue ) = _find_value( $tag, $subfield, $itemrecord );
2395                 }
2396                 $defaultvalue = $tagslib->{$tag}->{$subfield}->{defaultvalue} unless $defaultvalue;
2397                 if ( !defined $defaultvalue ) {
2398                     $defaultvalue = q||;
2399                 }
2400                 $defaultvalue =~ s/"/&quot;/g;
2401
2402                 # search for itemcallnumber if applicable
2403                 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber'
2404                     && C4::Context->preference('itemcallnumber') ) {
2405                     my $CNtag      = substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2406                     my $CNsubfield = substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2407                     if ($itemrecord) {
2408                         my $temp = $itemrecord->field($CNtag);
2409                         if ($temp) {
2410                             $defaultvalue = $temp->subfield($CNsubfield);
2411                         }
2412                     }
2413                 }
2414                 if (   $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.itemcallnumber'
2415                     && $defaultvalues
2416                     && $defaultvalues->{'callnumber'} ) {
2417                     my $temp;
2418                     if ($itemrecord) {
2419                         $temp = $itemrecord->field($subfield);
2420                     }
2421                     unless ($temp) {
2422                         $defaultvalue = $defaultvalues->{'callnumber'} if $defaultvalues;
2423                     }
2424                 }
2425                 if (   ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.holdingbranch' || $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.homebranch' )
2426                     && $defaultvalues
2427                     && $defaultvalues->{'branchcode'} ) {
2428                     my $temp;
2429                     if ($itemrecord) {
2430                         $temp = $itemrecord->field($subfield);
2431                     }
2432                     unless ($temp) {
2433                         $defaultvalue = $defaultvalues->{branchcode} if $defaultvalues;
2434                     }
2435                 }
2436                 if (   ( $tagslib->{$tag}->{$subfield}->{kohafield} eq 'items.location' )
2437                     && $defaultvalues
2438                     && $defaultvalues->{'location'} ) {
2439                     my $temp = $itemrecord->field($subfield) if ($itemrecord);
2440                     unless ($temp) {
2441                         $defaultvalue = $defaultvalues->{location} if $defaultvalues;
2442                     }
2443                 }
2444                 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2445                     my @authorised_values;
2446                     my %authorised_lib;
2447
2448                     # builds list, depending on authorised value...
2449                     #---- branch
2450                     if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
2451                         if (   ( C4::Context->preference("IndependantBranches") )
2452                             && ( C4::Context->userenv->{flags} % 2 != 1 ) ) {
2453                             my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname" );
2454                             $sth->execute( C4::Context->userenv->{branch} );
2455                             push @authorised_values, ""
2456                               unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2457                             while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2458                                 push @authorised_values, $branchcode;
2459                                 $authorised_lib{$branchcode} = $branchname;
2460                             }
2461                         } else {
2462                             my $sth = $dbh->prepare( "SELECT branchcode,branchname FROM branches ORDER BY branchname" );
2463                             $sth->execute;
2464                             push @authorised_values, ""
2465                               unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2466                             while ( my ( $branchcode, $branchname ) = $sth->fetchrow_array ) {
2467                                 push @authorised_values, $branchcode;
2468                                 $authorised_lib{$branchcode} = $branchname;
2469                             }
2470                         }
2471
2472                         #----- itemtypes
2473                     } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
2474                         my $sth = $dbh->prepare( "SELECT itemtype,description FROM itemtypes ORDER BY description" );
2475                         $sth->execute;
2476                         push @authorised_values, ""
2477                           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2478                         while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
2479                             push @authorised_values, $itemtype;
2480                             $authorised_lib{$itemtype} = $description;
2481                         }
2482                         #---- class_sources
2483                     } elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
2484                         push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2485
2486                         my $class_sources = GetClassSources();
2487                         my $default_source = C4::Context->preference("DefaultClassificationSource");
2488
2489                         foreach my $class_source (sort keys %$class_sources) {
2490                             next unless $class_sources->{$class_source}->{'used'} or
2491                                         ($class_source eq $default_source);
2492                             push @authorised_values, $class_source;
2493                             $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
2494                         }
2495
2496                         #---- "true" authorised value
2497                     } else {
2498                         $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
2499                         push @authorised_values, ""
2500                           unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2501                         while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
2502                             push @authorised_values, $value;
2503                             $authorised_lib{$value} = $lib;
2504                         }
2505                     }
2506                     $subfield_data{marc_value} = CGI::scrolling_list(
2507                         -name     => 'field_value',
2508                         -values   => \@authorised_values,
2509                         -default  => "$defaultvalue",
2510                         -labels   => \%authorised_lib,
2511                         -size     => 1,
2512                         -tabindex => '',
2513                         -multiple => 0,
2514                     );
2515                 } elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) {
2516                         # opening plugin
2517                         my $plugin = C4::Context->intranetdir . "/cataloguing/value_builder/" . $tagslib->{$tag}->{$subfield}->{'value_builder'};
2518                         if (do $plugin) {
2519                             my $temp;
2520                             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
2521                             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
2522                             $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
2523                             my $index_subfield = int(rand(1000000));
2524                             $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
2525                             $subfield_data{marc_value} = qq[<input tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255"
2526                                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
2527                                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />
2528                                 <a href="#" class="buttonDot" onclick="Clic$function_name('$subfield_data{id}'); return false;" title="Tag Editor">...</a>
2529                                 $javascript];
2530                         } else {
2531                             warn "Plugin Failed: $plugin";
2532                             $subfield_data{marc_value} = qq(<input tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" />); # supply default input form
2533                         }
2534                 }
2535                 elsif ( $tag eq '' ) {       # it's an hidden field
2536                     $subfield_data{marc_value} = qq(<input type="hidden" tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" value="$defaultvalue" />);
2537                 }
2538                 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) {   # FIXME: shouldn't input type be "hidden" ?
2539                     $subfield_data{marc_value} = qq(<input type="text" tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" value="$defaultvalue" />);
2540                 }
2541                 elsif ( length($defaultvalue) > 100
2542                             or (C4::Context->preference("marcflavour") eq "UNIMARC" and
2543                                   300 <= $tag && $tag < 400 && $subfield eq 'a' )
2544                             or (C4::Context->preference("marcflavour") eq "MARC21"  and
2545                                   500 <= $tag && $tag < 600                     )
2546                           ) {
2547                     # oversize field (textarea)
2548                     $subfield_data{marc_value} = qq(<textarea tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255">$defaultvalue</textarea>\n");
2549                 } else {
2550                     $subfield_data{marc_value} = "<input type=\"text\" name=\"field_value\" value=\"$defaultvalue\" size=\"50\" maxlength=\"255\" />";
2551                 }
2552                 push( @loop_data, \%subfield_data );
2553             }
2554         }
2555     }
2556     my $itemnumber;
2557     if ( $itemrecord && $itemrecord->field($itemtagfield) ) {
2558         $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield );
2559     }
2560     return {
2561         'itemtagfield'    => $itemtagfield,
2562         'itemtagsubfield' => $itemtagsubfield,
2563         'itemnumber'      => $itemnumber,
2564         'iteminformation' => \@loop_data
2565     };
2566 }
2567
2568 #"
2569
2570 #
2571 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2572 # at the same time
2573 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2574 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2575 # =head2 ModZebrafiles
2576 #
2577 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2578 #
2579 # =cut
2580 #
2581 # sub ModZebrafiles {
2582 #
2583 #     my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2584 #
2585 #     my $op;
2586 #     my $zebradir =
2587 #       C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2588 #     unless ( opendir( DIR, "$zebradir" ) ) {
2589 #         warn "$zebradir not found";
2590 #         return;
2591 #     }
2592 #     closedir DIR;
2593 #     my $filename = $zebradir . $biblionumber;
2594 #
2595 #     if ($record) {
2596 #         open( OUTPUT, ">", $filename . ".xml" );
2597 #         print OUTPUT $record;
2598 #         close OUTPUT;
2599 #     }
2600 # }
2601
2602 =head2 ModZebra
2603
2604   ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2605
2606 $biblionumber is the biblionumber we want to index
2607
2608 $op is specialUpdate or delete, and is used to know what we want to do
2609
2610 $server is the server that we want to update
2611
2612 $oldRecord is the MARC::Record containing the previous version of the record.  This is used only when 
2613 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2614 do an update.
2615
2616 $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.
2617
2618 =cut
2619
2620 sub ModZebra {
2621 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2622     my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2623     my $dbh = C4::Context->dbh;
2624
2625     # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2626     # at the same time
2627     # replaced by a zebraqueue table, that is filled with ModZebra to run.
2628     # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2629
2630     if ( C4::Context->preference("NoZebra") ) {
2631
2632         # lock the nozebra table : we will read index lines, update them in Perl process
2633         # and write everything in 1 transaction.
2634         # lock the table to avoid someone else overwriting what we are doing
2635         $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2636         my %result;    # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2637         if ( $op eq 'specialUpdate' ) {
2638
2639             # OK, we have to add or update the record
2640             # 1st delete (virtually, in indexes), if record actually exists
2641             if ($oldRecord) {
2642                 %result = _DelBiblioNoZebra( $biblionumber, $oldRecord, $server );
2643             }
2644
2645             # ... add the record
2646             %result = _AddBiblioNoZebra( $biblionumber, $newRecord, $server, %result );
2647         } else {
2648
2649             # it's a deletion, delete the record...
2650             # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2651             %result = _DelBiblioNoZebra( $biblionumber, $oldRecord, $server );
2652         }
2653
2654         # ok, now update the database...
2655         my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2656         foreach my $key ( keys %result ) {
2657             foreach my $index ( keys %{ $result{$key} } ) {
2658                 $sth->execute( $result{$key}->{$index}, $server, $key, $index );
2659             }
2660         }
2661         $dbh->do('UNLOCK TABLES');
2662     } else {
2663
2664         #
2665         # we use zebra, just fill zebraqueue table
2666         #
2667         my $check_sql = "SELECT COUNT(*) FROM zebraqueue 
2668                          WHERE server = ?
2669                          AND   biblio_auth_number = ?
2670                          AND   operation = ?
2671                          AND   done = 0";
2672         my $check_sth = $dbh->prepare_cached($check_sql);
2673         $check_sth->execute( $server, $biblionumber, $op );
2674         my ($count) = $check_sth->fetchrow_array;
2675         $check_sth->finish();
2676         if ( $count == 0 ) {
2677             my $sth = $dbh->prepare("INSERT INTO zebraqueue  (biblio_auth_number,server,operation) VALUES(?,?,?)");
2678             $sth->execute( $biblionumber, $server, $op );
2679             $sth->finish;
2680         }
2681     }
2682 }
2683
2684 =head2 GetNoZebraIndexes
2685
2686   %indexes = GetNoZebraIndexes;
2687
2688 return the data from NoZebraIndexes syspref.
2689
2690 =cut
2691
2692 sub GetNoZebraIndexes {
2693     my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2694     my %indexes;
2695   INDEX: foreach my $line ( split /['"],[\n\r]*/, $no_zebra_indexes ) {
2696         $line =~ /(.*)=>(.*)/;
2697         my $index  = $1;    # initial ' or " is removed afterwards
2698         my $fields = $2;
2699         $index  =~ s/'|"|\s//g;
2700         $fields =~ s/'|"|\s//g;
2701         $indexes{$index} = $fields;
2702     }
2703     return %indexes;
2704 }
2705
2706 =head2 EmbedItemsInMarcBiblio
2707
2708     EmbedItemsInMarcBiblio($marc, $biblionumber);
2709
2710 Given a MARC::Record object containing a bib record,
2711 modify it to include the items attached to it as 9XX
2712 per the bib's MARC framework.
2713
2714 =cut
2715
2716 sub EmbedItemsInMarcBiblio {
2717     my ($marc, $biblionumber) = @_;
2718     croak "No MARC record" unless $marc;
2719
2720     my $frameworkcode = GetFrameworkCode($biblionumber);
2721     _strip_item_fields($marc, $frameworkcode);
2722
2723     # ... and embed the current items
2724     my $dbh = C4::Context->dbh;
2725     my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
2726     $sth->execute($biblionumber);
2727     my @item_fields;
2728     my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2729     while (my ($itemnumber) = $sth->fetchrow_array) {
2730         my $item_marc = C4::Items::GetMarcItem($biblionumber, $itemnumber);
2731         push @item_fields, $item_marc->field($itemtag);
2732     }
2733     $marc->insert_fields_ordered(@item_fields);
2734 }
2735
2736 =head1 INTERNAL FUNCTIONS
2737
2738 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2739
2740 function to delete a biblio in NoZebra indexes
2741 This function does NOT delete anything in database : it reads all the indexes entries
2742 that have to be deleted & delete them in the hash
2743
2744 The SQL part is done either :
2745  - after the Add if we are modifying a biblio (delete + add again)
2746  - immediatly after this sub if we are doing a true deletion.
2747
2748 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2749
2750 =cut
2751
2752 sub _DelBiblioNoZebra {
2753     my ( $biblionumber, $record, $server ) = @_;
2754
2755     # Get the indexes
2756     my $dbh = C4::Context->dbh;
2757
2758     # Get the indexes
2759     my %index;
2760     my $title;
2761     if ( $server eq 'biblioserver' ) {
2762         %index = GetNoZebraIndexes;
2763
2764         # get title of the record (to store the 10 first letters with the index)
2765         my ( $titletag, $titlesubfield ) = GetMarcFromKohaField( 'biblio.title', '' );    # FIXME: should be GetFrameworkCode($biblionumber) ??
2766         $title = lc( $record->subfield( $titletag, $titlesubfield ) );
2767     } else {
2768
2769         # for authorities, the "title" is the $a mainentry
2770         my ( $auth_type_tag, $auth_type_sf ) = C4::AuthoritiesMarc::get_auth_type_location();
2771         my $authref = C4::AuthoritiesMarc::GetAuthType( $record->subfield( $auth_type_tag, $auth_type_sf ) );
2772         warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
2773         $title = $record->subfield( $authref->{auth_tag_to_report}, 'a' );
2774         $index{'mainmainentry'} = $authref->{'auth_tag_to_report'} . 'a';
2775         $index{'mainentry'}     = $authref->{'auth_tag_to_report'} . '*';
2776         $index{'auth_type'}     = "${auth_type_tag}${auth_type_sf}";
2777     }
2778
2779     my %result;
2780
2781     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2782     $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2783
2784     # limit to 10 char, should be enough, and limit the DB size
2785     $title = substr( $title, 0, 10 );
2786
2787     #parse each field
2788     my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2789     foreach my $field ( $record->fields() ) {
2790
2791         #parse each subfield
2792         next if $field->tag < 10;
2793         foreach my $subfield ( $field->subfields() ) {
2794             my $tag          = $field->tag();
2795             my $subfieldcode = $subfield->[0];
2796             my $indexed      = 0;
2797
2798             # check each index to see if the subfield is stored somewhere
2799             # otherwise, store it in __RAW__ index
2800             foreach my $key ( keys %index ) {
2801
2802                 #                 warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2803                 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
2804                     $indexed = 1;
2805                     my $line = lc $subfield->[1];
2806
2807                     # remove meaningless value in the field...
2808                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2809
2810                     # ... and split in words
2811                     foreach ( split / /, $line ) {
2812                         next unless $_;    # skip  empty values (multiple spaces)
2813                                            # if the entry is already here, do nothing, the biblionumber has already be removed
2814                         unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/ ) ) {
2815
2816                             # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2817                             $sth2->execute( $server, $key, $_ );
2818                             my $existing_biblionumbers = $sth2->fetchrow;
2819
2820                             # it exists
2821                             if ($existing_biblionumbers) {
2822
2823                                 #                                 warn " existing for $key $_: $existing_biblionumbers";
2824                                 $result{$key}->{$_} = $existing_biblionumbers;
2825                                 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2826                             }
2827                         }
2828                     }
2829                 }
2830             }
2831
2832             # the subfield is not indexed, store it in __RAW__ index anyway
2833             unless ($indexed) {
2834                 my $line = lc $subfield->[1];
2835                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2836
2837                 # ... and split in words
2838                 foreach ( split / /, $line ) {
2839                     next unless $_;    # skip  empty values (multiple spaces)
2840                                        # if the entry is already here, do nothing, the biblionumber has already be removed
2841                     unless ( $result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/ ) {
2842
2843                         # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2844                         $sth2->execute( $server, '__RAW__', $_ );
2845                         my $existing_biblionumbers = $sth2->fetchrow;
2846
2847                         # it exists
2848                         if ($existing_biblionumbers) {
2849                             $result{'__RAW__'}->{$_} = $existing_biblionumbers;
2850                             $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2851                         }
2852                     }
2853                 }
2854             }
2855         }
2856     }
2857     return %result;
2858 }
2859
2860 =head2 _AddBiblioNoZebra
2861
2862   _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2863
2864 function to add a biblio in NoZebra indexes
2865
2866 =cut
2867
2868 sub _AddBiblioNoZebra {
2869     my ( $biblionumber, $record, $server, %result ) = @_;
2870     my $dbh = C4::Context->dbh;
2871
2872     # Get the indexes
2873     my %index;
2874     my $title;
2875     if ( $server eq 'biblioserver' ) {
2876         %index = GetNoZebraIndexes;
2877
2878         # get title of the record (to store the 10 first letters with the index)
2879         my ( $titletag, $titlesubfield ) = GetMarcFromKohaField( 'biblio.title', '' );    # FIXME: should be GetFrameworkCode($biblionumber) ??
2880         $title = lc( $record->subfield( $titletag, $titlesubfield ) );
2881     } else {
2882
2883         # warn "server : $server";
2884         # for authorities, the "title" is the $a mainentry
2885         my ( $auth_type_tag, $auth_type_sf ) = C4::AuthoritiesMarc::get_auth_type_location();
2886         my $authref = C4::AuthoritiesMarc::GetAuthType( $record->subfield( $auth_type_tag, $auth_type_sf ) );
2887         warn "ERROR : authtype undefined for " . $record->as_formatted unless $authref;
2888         $title = $record->subfield( $authref->{auth_tag_to_report}, 'a' );
2889         $index{'mainmainentry'} = $authref->{auth_tag_to_report} . 'a';
2890         $index{'mainentry'}     = $authref->{auth_tag_to_report} . '*';
2891         $index{'auth_type'}     = "${auth_type_tag}${auth_type_sf}";
2892     }
2893
2894     # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2895     $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2896
2897     # limit to 10 char, should be enough, and limit the DB size
2898     $title = substr( $title, 0, 10 );
2899
2900     #parse each field
2901     my $sth2 = $dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2902     foreach my $field ( $record->fields() ) {
2903
2904         #parse each subfield
2905         ###FIXME: impossible to index a 001-009 value with NoZebra
2906         next if $field->tag < 10;
2907         foreach my $subfield ( $field->subfields() ) {
2908             my $tag          = $field->tag();
2909             my $subfieldcode = $subfield->[0];
2910             my $indexed      = 0;
2911
2912             #             warn "INDEXING :".$subfield->[1];
2913             # check each index to see if the subfield is stored somewhere
2914             # otherwise, store it in __RAW__ index
2915             foreach my $key ( keys %index ) {
2916
2917                 #                 warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2918                 if ( $index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/ ) {
2919                     $indexed = 1;
2920                     my $line = lc $subfield->[1];
2921
2922                     # remove meaningless value in the field...
2923                     $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2924
2925                     # ... and split in words
2926                     foreach ( split / /, $line ) {
2927                         next unless $_;    # skip  empty values (multiple spaces)
2928                                            # if the entry is already here, improve weight
2929
2930                         #                         warn "managing $_";
2931                         if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
2932                             my $weight = $1 + 1;
2933                             $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2934                             $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2935                         } else {
2936
2937                             # get the value if it exist in the nozebra table, otherwise, create it
2938                             $sth2->execute( $server, $key, $_ );
2939                             my $existing_biblionumbers = $sth2->fetchrow;
2940
2941                             # it exists
2942                             if ($existing_biblionumbers) {
2943                                 $result{$key}->{"$_"} = $existing_biblionumbers;
2944                                 my $weight = defined $1 ? $1 + 1 : 1;
2945                                 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2946                                 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2947
2948                                 # create a new ligne for this entry
2949                             } else {
2950
2951                                 #                             warn "INSERT : $server / $key / $_";
2952                                 $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ', indexname=' . $dbh->quote($key) . ',value=' . $dbh->quote($_) );
2953                                 $result{$key}->{"$_"} .= "$biblionumber,$title-1;";
2954                             }
2955                         }
2956                     }
2957                 }
2958             }
2959
2960             # the subfield is not indexed, store it in __RAW__ index anyway
2961             unless ($indexed) {
2962                 my $line = lc $subfield->[1];
2963                 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2964
2965                 # ... and split in words
2966                 foreach ( split / /, $line ) {
2967                     next unless $_;    # skip  empty values (multiple spaces)
2968                                        # if the entry is already here, improve weight
2969                     my $tmpstr = $result{'__RAW__'}->{"$_"} || "";
2970                     if ( $tmpstr =~ /$biblionumber,\Q$title\E\-(\d+);/ ) {
2971                         my $weight = $1 + 1;
2972                         $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2973                         $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2974                     } else {
2975
2976                         # get the value if it exist in the nozebra table, otherwise, create it
2977                         $sth2->execute( $server, '__RAW__', $_ );
2978                         my $existing_biblionumbers = $sth2->fetchrow;
2979
2980                         # it exists
2981                         if ($existing_biblionumbers) {
2982                             $result{'__RAW__'}->{"$_"} = $existing_biblionumbers;
2983                             my $weight = ( $1 ? $1 : 0 ) + 1;
2984                             $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2985                             $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2986
2987                             # create a new ligne for this entry
2988                         } else {
2989                             $dbh->do( 'INSERT INTO nozebra SET server=' . $dbh->quote($server) . ',  indexname="__RAW__",value=' . $dbh->quote($_) );
2990                             $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-1;";
2991                         }
2992                     }
2993                 }
2994             }
2995         }
2996     }
2997     return %result;
2998 }
2999
3000 =head2 _find_value
3001
3002   ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
3003
3004 Find the given $subfield in the given $tag in the given
3005 MARC::Record $record.  If the subfield is found, returns
3006 the (indicators, value) pair; otherwise, (undef, undef) is
3007 returned.
3008
3009 PROPOSITION :
3010 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
3011 I suggest we export it from this module.
3012
3013 =cut
3014
3015 sub _find_value {
3016     my ( $tagfield, $insubfield, $record, $encoding ) = @_;
3017     my @result;
3018     my $indicator;
3019     if ( $tagfield < 10 ) {
3020         if ( $record->field($tagfield) ) {
3021             push @result, $record->field($tagfield)->data();
3022         } else {
3023             push @result, "";
3024         }
3025     } else {
3026         foreach my $field ( $record->field($tagfield) ) {
3027             my @subfields = $field->subfields();
3028             foreach my $subfield (@subfields) {
3029                 if ( @$subfield[0] eq $insubfield ) {
3030                     push @result, @$subfield[1];
3031                     $indicator = $field->indicator(1) . $field->indicator(2);
3032                 }
3033             }
3034         }
3035     }
3036     return ( $indicator, @result );
3037 }
3038
3039 =head2 _koha_marc_update_bib_ids
3040
3041
3042   _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
3043
3044 Internal function to add or update biblionumber and biblioitemnumber to
3045 the MARC XML.
3046
3047 =cut
3048
3049 sub _koha_marc_update_bib_ids {
3050     my ( $record, $frameworkcode, $biblionumber, $biblioitemnumber ) = @_;
3051
3052     # we must add bibnum and bibitemnum in MARC::Record...
3053     # we build the new field with biblionumber and biblioitemnumber
3054     # we drop the original field
3055     # we add the new builded field.
3056     my ( $biblio_tag,     $biblio_subfield )     = GetMarcFromKohaField( "biblio.biblionumber",          $frameworkcode );
3057     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.biblioitemnumber", $frameworkcode );
3058
3059     if ( $biblio_tag != $biblioitem_tag ) {
3060
3061         # biblionumber & biblioitemnumber are in different fields
3062
3063         # deal with biblionumber
3064         my ( $new_field, $old_field );
3065         if ( $biblio_tag < 10 ) {
3066             $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
3067         } else {
3068             $new_field = MARC::Field->new( $biblio_tag, '', '', "$biblio_subfield" => $biblionumber );
3069         }
3070
3071         # drop old field and create new one...
3072         $old_field = $record->field($biblio_tag);
3073         $record->delete_field($old_field) if $old_field;
3074         $record->insert_fields_ordered($new_field);
3075
3076         # deal with biblioitemnumber
3077         if ( $biblioitem_tag < 10 ) {
3078             $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
3079         } else {
3080             $new_field = MARC::Field->new( $biblioitem_tag, '', '', "$biblioitem_subfield" => $biblioitemnumber, );
3081         }
3082
3083         # drop old field and create new one...
3084         $old_field = $record->field($biblioitem_tag);
3085         $record->delete_field($old_field) if $old_field;
3086         $record->insert_fields_ordered($new_field);
3087
3088     } else {
3089
3090         # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
3091         my $new_field = MARC::Field->new(
3092             $biblio_tag, '', '',
3093             "$biblio_subfield"     => $biblionumber,
3094             "$biblioitem_subfield" => $biblioitemnumber
3095         );
3096
3097         # drop old field and create new one...
3098         my $old_field = $record->field($biblio_tag);
3099         $record->delete_field($old_field) if $old_field;
3100         $record->insert_fields_ordered($new_field);
3101     }
3102 }
3103
3104 =head2 _koha_marc_update_biblioitem_cn_sort
3105
3106   _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
3107
3108 Given a MARC bib record and the biblioitem hash, update the
3109 subfield that contains a copy of the value of biblioitems.cn_sort.
3110
3111 =cut
3112
3113 sub _koha_marc_update_biblioitem_cn_sort {
3114     my $marc          = shift;
3115     my $biblioitem    = shift;
3116     my $frameworkcode = shift;
3117
3118     my ( $biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField( "biblioitems.cn_sort", $frameworkcode );
3119     return unless $biblioitem_tag;
3120
3121     my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3122
3123     if ( my $field = $marc->field($biblioitem_tag) ) {
3124         $field->delete_subfield( code => $biblioitem_subfield );
3125         if ( $cn_sort ne '' ) {
3126             $field->add_subfields( $biblioitem_subfield => $cn_sort );
3127         }
3128     } else {
3129
3130         # if we get here, no biblioitem tag is present in the MARC record, so
3131         # we'll create it if $cn_sort is not empty -- this would be
3132         # an odd combination of events, however
3133         if ($cn_sort) {
3134             $marc->insert_grouped_field( MARC::Field->new( $biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort ) );
3135         }
3136     }
3137 }
3138
3139 =head2 _koha_add_biblio
3140
3141   my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
3142
3143 Internal function to add a biblio ($biblio is a hash with the values)
3144
3145 =cut
3146
3147 sub _koha_add_biblio {
3148     my ( $dbh, $biblio, $frameworkcode ) = @_;
3149
3150     my $error;
3151
3152     # set the series flag
3153     unless (defined $biblio->{'serial'}){
3154         $biblio->{'serial'} = 0;
3155         if ( $biblio->{'seriestitle'} ) { $biblio->{'serial'} = 1 }
3156     }
3157
3158     my $query = "INSERT INTO biblio
3159         SET frameworkcode = ?,
3160             author = ?,
3161             title = ?,
3162             unititle =?,
3163             notes = ?,
3164             serial = ?,
3165             seriestitle = ?,
3166             copyrightdate = ?,
3167             datecreated=NOW(),
3168             abstract = ?
3169         ";
3170     my $sth = $dbh->prepare($query);
3171     $sth->execute(
3172         $frameworkcode, $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
3173         $biblio->{'serial'},        $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}
3174     );
3175
3176     my $biblionumber = $dbh->{'mysql_insertid'};
3177     if ( $dbh->errstr ) {
3178         $error .= "ERROR in _koha_add_biblio $query" . $dbh->errstr;
3179         warn $error;
3180     }
3181
3182     $sth->finish();
3183
3184     #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
3185     return ( $biblionumber, $error );
3186 }
3187
3188 =head2 _koha_modify_biblio
3189
3190   my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
3191
3192 Internal function for updating the biblio table
3193
3194 =cut
3195
3196 sub _koha_modify_biblio {
3197     my ( $dbh, $biblio, $frameworkcode ) = @_;
3198     my $error;
3199
3200     my $query = "
3201         UPDATE biblio
3202         SET    frameworkcode = ?,
3203                author = ?,
3204                title = ?,
3205                unititle = ?,
3206                notes = ?,
3207                serial = ?,
3208                seriestitle = ?,
3209                copyrightdate = ?,
3210                abstract = ?
3211         WHERE  biblionumber = ?
3212         "
3213       ;
3214     my $sth = $dbh->prepare($query);
3215
3216     $sth->execute(
3217         $frameworkcode,      $biblio->{'author'},      $biblio->{'title'},         $biblio->{'unititle'}, $biblio->{'notes'},
3218         $biblio->{'serial'}, $biblio->{'seriestitle'}, $biblio->{'copyrightdate'}, $biblio->{'abstract'}, $biblio->{'biblionumber'}
3219     ) if $biblio->{'biblionumber'};
3220
3221     if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
3222         $error .= "ERROR in _koha_modify_biblio $query" . $dbh->errstr;
3223         warn $error;
3224     }
3225     return ( $biblio->{'biblionumber'}, $error );
3226 }
3227
3228 =head2 _koha_modify_biblioitem_nonmarc
3229
3230   my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
3231
3232 Updates biblioitems row except for marc and marcxml, which should be changed
3233 via ModBiblioMarc
3234
3235 =cut
3236
3237 sub _koha_modify_biblioitem_nonmarc {
3238     my ( $dbh, $biblioitem ) = @_;
3239     my $error;
3240
3241     # re-calculate the cn_sort, it may have changed
3242     my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3243
3244     my $query = "UPDATE biblioitems 
3245     SET biblionumber    = ?,
3246         volume          = ?,
3247         number          = ?,
3248         itemtype        = ?,
3249         isbn            = ?,
3250         issn            = ?,
3251         publicationyear = ?,
3252         publishercode   = ?,
3253         volumedate      = ?,
3254         volumedesc      = ?,
3255         collectiontitle = ?,
3256         collectionissn  = ?,
3257         collectionvolume= ?,
3258         editionstatement= ?,
3259         editionresponsibility = ?,
3260         illus           = ?,
3261         pages           = ?,
3262         notes           = ?,
3263         size            = ?,
3264         place           = ?,
3265         lccn            = ?,
3266         url             = ?,
3267         cn_source       = ?,
3268         cn_class        = ?,
3269         cn_item         = ?,
3270         cn_suffix       = ?,
3271         cn_sort         = ?,
3272         totalissues     = ?
3273         where biblioitemnumber = ?
3274         ";
3275     my $sth = $dbh->prepare($query);
3276     $sth->execute(
3277         $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3278         $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3279         $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3280         $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3281         $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3282         $biblioitem->{'lccn'},             $biblioitem->{'url'},              $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'},
3283         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},        $cn_sort,                               $biblioitem->{'totalissues'},
3284         $biblioitem->{'biblioitemnumber'}
3285     );
3286     if ( $dbh->errstr ) {
3287         $error .= "ERROR in _koha_modify_biblioitem_nonmarc $query" . $dbh->errstr;
3288         warn $error;
3289     }
3290     return ( $biblioitem->{'biblioitemnumber'}, $error );
3291 }
3292
3293 =head2 _koha_add_biblioitem
3294
3295   my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
3296
3297 Internal function to add a biblioitem
3298
3299 =cut
3300
3301 sub _koha_add_biblioitem {
3302     my ( $dbh, $biblioitem ) = @_;
3303     my $error;
3304
3305     my ($cn_sort) = GetClassSort( $biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
3306     my $query = "INSERT INTO biblioitems SET
3307         biblionumber    = ?,
3308         volume          = ?,
3309         number          = ?,
3310         itemtype        = ?,
3311         isbn            = ?,
3312         issn            = ?,
3313         publicationyear = ?,
3314         publishercode   = ?,
3315         volumedate      = ?,
3316         volumedesc      = ?,
3317         collectiontitle = ?,
3318         collectionissn  = ?,
3319         collectionvolume= ?,
3320         editionstatement= ?,
3321         editionresponsibility = ?,
3322         illus           = ?,
3323         pages           = ?,
3324         notes           = ?,
3325         size            = ?,
3326         place           = ?,
3327         lccn            = ?,
3328         marc            = ?,
3329         url             = ?,
3330         cn_source       = ?,
3331         cn_class        = ?,
3332         cn_item         = ?,
3333         cn_suffix       = ?,
3334         cn_sort         = ?,
3335         totalissues     = ?
3336         ";
3337     my $sth = $dbh->prepare($query);
3338     $sth->execute(
3339         $biblioitem->{'biblionumber'},     $biblioitem->{'volume'},           $biblioitem->{'number'},                $biblioitem->{'itemtype'},
3340         $biblioitem->{'isbn'},             $biblioitem->{'issn'},             $biblioitem->{'publicationyear'},       $biblioitem->{'publishercode'},
3341         $biblioitem->{'volumedate'},       $biblioitem->{'volumedesc'},       $biblioitem->{'collectiontitle'},       $biblioitem->{'collectionissn'},
3342         $biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
3343         $biblioitem->{'pages'},            $biblioitem->{'bnotes'},           $biblioitem->{'size'},                  $biblioitem->{'place'},
3344         $biblioitem->{'lccn'},             $biblioitem->{'marc'},             $biblioitem->{'url'},                   $biblioitem->{'biblioitems.cn_source'},
3345         $biblioitem->{'cn_class'},         $biblioitem->{'cn_item'},          $biblioitem->{'cn_suffix'},             $cn_sort,
3346         $biblioitem->{'totalissues'}
3347     );
3348     my $bibitemnum = $dbh->{'mysql_insertid'};
3349
3350     if ( $dbh->errstr ) {
3351         $error .= "ERROR in _koha_add_biblioitem $query" . $dbh->errstr;
3352         warn $error;
3353     }
3354     $sth->finish();
3355     return ( $bibitemnum, $error );
3356 }
3357
3358 =head2 _koha_delete_biblio
3359
3360   $error = _koha_delete_biblio($dbh,$biblionumber);
3361
3362 Internal sub for deleting from biblio table -- also saves to deletedbiblio
3363
3364 C<$dbh> - the database handle
3365
3366 C<$biblionumber> - the biblionumber of the biblio to be deleted
3367
3368 =cut
3369
3370 # FIXME: add error handling
3371
3372 sub _koha_delete_biblio {
3373     my ( $dbh, $biblionumber ) = @_;
3374
3375     # get all the data for this biblio
3376     my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3377     $sth->execute($biblionumber);
3378
3379     if ( my $data = $sth->fetchrow_hashref ) {
3380
3381         # save the record in deletedbiblio
3382         # find the fields to save
3383         my $query = "INSERT INTO deletedbiblio SET ";
3384         my @bind  = ();
3385         foreach my $temp ( keys %$data ) {
3386             $query .= "$temp = ?,";
3387             push( @bind, $data->{$temp} );
3388         }
3389
3390         # replace the last , by ",?)"
3391         $query =~ s/\,$//;
3392         my $bkup_sth = $dbh->prepare($query);
3393         $bkup_sth->execute(@bind);
3394         $bkup_sth->finish;
3395
3396         # delete the biblio
3397         my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3398         $del_sth->execute($biblionumber);
3399         $del_sth->finish;
3400     }
3401     $sth->finish;
3402     return undef;
3403 }
3404
3405 =head2 _koha_delete_biblioitems
3406
3407   $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3408
3409 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3410
3411 C<$dbh> - the database handle
3412 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3413
3414 =cut
3415
3416 # FIXME: add error handling
3417
3418 sub _koha_delete_biblioitems {
3419     my ( $dbh, $biblioitemnumber ) = @_;
3420
3421     # get all the data for this biblioitem
3422     my $sth = $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3423     $sth->execute($biblioitemnumber);
3424
3425     if ( my $data = $sth->fetchrow_hashref ) {
3426
3427         # save the record in deletedbiblioitems
3428         # find the fields to save
3429         my $query = "INSERT INTO deletedbiblioitems SET ";
3430         my @bind  = ();
3431         foreach my $temp ( keys %$data ) {
3432             $query .= "$temp = ?,";
3433             push( @bind, $data->{$temp} );
3434         }
3435
3436         # replace the last , by ",?)"
3437         $query =~ s/\,$//;
3438         my $bkup_sth = $dbh->prepare($query);
3439         $bkup_sth->execute(@bind);
3440         $bkup_sth->finish;
3441
3442         # delete the biblioitem
3443         my $del_sth = $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3444         $del_sth->execute($biblioitemnumber);
3445         $del_sth->finish;
3446     }
3447     $sth->finish;
3448     return undef;
3449 }
3450
3451 =head1 UNEXPORTED FUNCTIONS
3452
3453 =head2 ModBiblioMarc
3454
3455   &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3456
3457 Add MARC data for a biblio to koha 
3458
3459 Function exported, but should NOT be used, unless you really know what you're doing
3460
3461 =cut
3462
3463 sub ModBiblioMarc {
3464
3465     # pass the MARC::Record to this function, and it will create the records in the marc field
3466     my ( $record, $biblionumber, $frameworkcode ) = @_;
3467     my $dbh    = C4::Context->dbh;
3468     my @fields = $record->fields();
3469     if ( !$frameworkcode ) {
3470         $frameworkcode = "";
3471     }
3472     my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3473     $sth->execute( $frameworkcode, $biblionumber );
3474     $sth->finish;
3475     my $encoding = C4::Context->preference("marcflavour");
3476
3477     # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3478     if ( $encoding eq "UNIMARC" ) {
3479         my $string = $record->subfield( 100, "a" );
3480         if ( ($string) && ( length( $record->subfield( 100, "a" ) ) == 36 ) ) {
3481             my $f100 = $record->field(100);
3482             $record->delete_field($f100);
3483         } else {
3484             $string = POSIX::strftime( "%Y%m%d", localtime );
3485             $string =~ s/\-//g;
3486             $string = sprintf( "%-*s", 35, $string );
3487         }
3488         substr( $string, 22, 6, "frey50" );
3489         unless ( $record->subfield( 100, "a" ) ) {
3490             $record->insert_fields_ordered( MARC::Field->new( 100, "", "", "a" => $string ) );
3491         }
3492     }
3493
3494     #enhancement 5374: update transaction date (005) for marc21/unimarc
3495     if($encoding =~ /MARC21|UNIMARC/) {
3496       my @a= (localtime) [5,4,3,2,1,0]; $a[0]+=1900; $a[1]++;
3497         # YY MM DD HH MM SS (update year and month)
3498       my $f005= $record->field('005');
3499       $f005->update(sprintf("%4d%02d%02d%02d%02d%04.1f",@a)) if $f005;
3500     }
3501
3502     my $oldRecord;
3503     if ( C4::Context->preference("NoZebra") ) {
3504
3505         # only NoZebra indexing needs to have
3506         # the previous version of the record
3507         $oldRecord = GetMarcBiblio($biblionumber);
3508     }
3509     $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3510     $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding), $biblionumber );
3511     $sth->finish;
3512     ModZebra( $biblionumber, "specialUpdate", "biblioserver", $oldRecord, $record );
3513     return $biblionumber;
3514 }
3515
3516 =head2 z3950_extended_services
3517
3518   z3950_extended_services($serviceType,$serviceOptions,$record);
3519
3520 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.
3521
3522 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3523
3524 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3525
3526  action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3527
3528 and maybe
3529
3530   recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3531   syntax => the record syntax (transfer syntax)
3532   databaseName = Database from connection object
3533
3534 To set serviceOptions, call set_service_options($serviceType)
3535
3536 C<$record> the record, if one is needed for the service type
3537
3538 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3539
3540 =cut
3541
3542 sub z3950_extended_services {
3543     my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3544
3545     # get our connection object
3546     my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3547
3548     # create a new package object
3549     my $Zpackage = $Zconn->package();
3550
3551     # set our options
3552     $Zpackage->option( action => $action );
3553
3554     if ( $serviceOptions->{'databaseName'} ) {
3555         $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3556     }
3557     if ( $serviceOptions->{'recordIdNumber'} ) {
3558         $Zpackage->option( recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3559     }
3560     if ( $serviceOptions->{'recordIdOpaque'} ) {
3561         $Zpackage->option( recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3562     }
3563
3564     # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3565     #if ($serviceType eq 'itemorder') {
3566     #   $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3567     #   $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3568     #   $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3569     #   $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3570     #}
3571
3572     if ( $serviceOptions->{record} ) {
3573         $Zpackage->option( record => $serviceOptions->{record} );
3574
3575         # can be xml or marc
3576         if ( $serviceOptions->{'syntax'} ) {
3577             $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3578         }
3579     }
3580
3581     # send the request, handle any exception encountered
3582     eval { $Zpackage->send($serviceType) };
3583     if ( $@ && $@->isa("ZOOM::Exception") ) {
3584         return "error:  " . $@->code() . " " . $@->message() . "\n";
3585     }
3586
3587     # free up package resources
3588     $Zpackage->destroy();
3589 }
3590
3591 =head2 set_service_options
3592
3593   my $serviceOptions = set_service_options($serviceType);
3594
3595 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3596
3597 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3598
3599 =cut
3600
3601 sub set_service_options {
3602     my ($serviceType) = @_;
3603     my $serviceOptions;
3604
3605     # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3606     #   $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3607
3608     if ( $serviceType eq 'commit' ) {
3609
3610         # nothing to do
3611     }
3612     if ( $serviceType eq 'create' ) {
3613
3614         # nothing to do
3615     }
3616     if ( $serviceType eq 'drop' ) {
3617         die "ERROR: 'drop' not currently supported (by Zebra)";
3618     }
3619     return $serviceOptions;
3620 }
3621
3622 =head2 get_biblio_authorised_values
3623
3624 find the types and values for all authorised values assigned to this biblio.
3625
3626 parameters:
3627     biblionumber
3628     MARC::Record of the bib
3629
3630 returns: a hashref mapping the authorised value to the value set for this biblionumber
3631
3632   $authorised_values = {
3633                        'Scent'     => 'flowery',
3634                        'Audience'  => 'Young Adult',
3635                        'itemtypes' => 'SER',
3636                         };
3637
3638 Notes: forlibrarian should probably be passed in, and called something different.
3639
3640 =cut
3641
3642 sub get_biblio_authorised_values {
3643     my $biblionumber = shift;
3644     my $record       = shift;
3645
3646     my $forlibrarian  = 1;                                 # are we in staff or opac?
3647     my $frameworkcode = GetFrameworkCode($biblionumber);
3648
3649     my $authorised_values;
3650
3651     my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3652       or return $authorised_values;
3653
3654     # assume that these entries in the authorised_value table are bibliolevel.
3655     # ones that start with 'item%' are item level.
3656     my $query = q(SELECT distinct authorised_value, kohafield
3657                     FROM marc_subfield_structure
3658                     WHERE authorised_value !=''
3659                       AND (kohafield like 'biblio%'
3660                        OR  kohafield like '') );
3661     my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3662
3663     foreach my $tag ( keys(%$tagslib) ) {
3664         foreach my $subfield ( keys( %{ $tagslib->{$tag} } ) ) {
3665
3666             # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3667             if ( 'HASH' eq ref $tagslib->{$tag}{$subfield} ) {
3668                 if ( defined $tagslib->{$tag}{$subfield}{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } ) {
3669                     if ( defined $record->field($tag) ) {
3670                         my $this_subfield_value = $record->field($tag)->subfield($subfield);
3671                         if ( defined $this_subfield_value ) {
3672                             $authorised_values->{ $tagslib->{$tag}{$subfield}{'authorised_value'} } = $this_subfield_value;
3673                         }
3674                     }
3675                 }
3676             }
3677         }
3678     }
3679
3680     # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3681     return $authorised_values;
3682 }
3683
3684 1;
3685
3686 __END__
3687
3688 =head1 AUTHOR
3689
3690 Koha Development Team <http://koha-community.org/>
3691
3692 Paul POULAIN paul.poulain@free.fr
3693
3694 Joshua Ferraro jmf@liblime.com
3695
3696 =cut