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