Merge branch 'master' of http://manage-gmc.dev.kohalibrary.com/koha-installer
[koha.git] / C4 / Koha.pm
1 package C4::Koha;
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
21 use strict;
22 require Exporter;
23 use C4::Context;
24 use C4::Output;
25 our ($VERSION,@ISA,@EXPORT);
26
27 $VERSION = 3.00;
28
29 =head1 NAME
30
31     C4::Koha - Perl Module containing convenience functions for Koha scripts
32
33 =head1 SYNOPSIS
34
35   use C4::Koha;
36
37
38 =head1 DESCRIPTION
39
40     Koha.pm provides many functions for Koha scripts.
41
42 =head1 FUNCTIONS
43
44 =over 2
45
46 =cut
47
48 @ISA    = qw(Exporter);
49 @EXPORT = qw(
50   &slashifyDate
51   &DisplayISBN
52   &subfield_is_koha_internal_p
53   &GetPrinters &GetPrinter
54   &GetItemTypes &getitemtypeinfo
55   &GetCcodes
56   &get_itemtypeinfos_of
57   &getframeworks &getframeworkinfo
58   &getauthtypes &getauthtype
59   &getallthemes
60   &getFacets
61   &displayServers
62   &getnbpages
63   &getitemtypeimagesrcfromurl
64   &get_infos_of
65   &get_notforloan_label_of
66   &getitemtypeimagedir
67   &getitemtypeimagesrc
68   &GetAuthorisedValues
69   &FixEncoding
70   &GetKohaAuthorisedValues
71   &GetAuthValCode
72   &GetManagedTagSubfields
73
74   $DEBUG
75   );
76
77 my $DEBUG = 0;
78
79 =head2 slashifyDate
80
81   $slash_date = &slashifyDate($dash_date);
82
83     Takes a string of the form "DD-MM-YYYY" (or anything separated by
84     dashes), converts it to the form "YYYY/MM/DD", and returns the result.
85
86 =cut
87
88 sub slashifyDate {
89
90     # accepts a date of the form xx-xx-xx[xx] and returns it in the
91     # form xx/xx/xx[xx]
92     my @dateOut = split( '-', shift );
93     return ("$dateOut[2]/$dateOut[1]/$dateOut[0]");
94 }
95
96
97 =head2 DisplayISBN
98
99     my $string = DisplayISBN( $isbn );
100
101 =cut
102
103 sub DisplayISBN {
104     my ($isbn) = @_;
105     if (length ($isbn)<13){
106     my $seg1;
107     if ( substr( $isbn, 0, 1 ) <= 7 ) {
108         $seg1 = substr( $isbn, 0, 1 );
109     }
110     elsif ( substr( $isbn, 0, 2 ) <= 94 ) {
111         $seg1 = substr( $isbn, 0, 2 );
112     }
113     elsif ( substr( $isbn, 0, 3 ) <= 995 ) {
114         $seg1 = substr( $isbn, 0, 3 );
115     }
116     elsif ( substr( $isbn, 0, 4 ) <= 9989 ) {
117         $seg1 = substr( $isbn, 0, 4 );
118     }
119     else {
120         $seg1 = substr( $isbn, 0, 5 );
121     }
122     my $x = substr( $isbn, length($seg1) );
123     my $seg2;
124     if ( substr( $x, 0, 2 ) <= 19 ) {
125
126         # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
127         $seg2 = substr( $x, 0, 2 );
128     }
129     elsif ( substr( $x, 0, 3 ) <= 699 ) {
130         $seg2 = substr( $x, 0, 3 );
131     }
132     elsif ( substr( $x, 0, 4 ) <= 8399 ) {
133         $seg2 = substr( $x, 0, 4 );
134     }
135     elsif ( substr( $x, 0, 5 ) <= 89999 ) {
136         $seg2 = substr( $x, 0, 5 );
137     }
138     elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
139         $seg2 = substr( $x, 0, 6 );
140     }
141     else {
142         $seg2 = substr( $x, 0, 7 );
143     }
144     my $seg3 = substr( $x, length($seg2) );
145     $seg3 = substr( $seg3, 0, length($seg3) - 1 );
146     my $seg4 = substr( $x, -1, 1 );
147     return "$seg1-$seg2-$seg3-$seg4";
148     } else {
149       my $seg1;
150       $seg1 = substr( $isbn, 0, 3 );
151       my $seg2;
152       if ( substr( $isbn, 3, 1 ) <= 7 ) {
153           $seg2 = substr( $isbn, 3, 1 );
154       }
155       elsif ( substr( $isbn, 3, 2 ) <= 94 ) {
156           $seg2 = substr( $isbn, 3, 2 );
157       }
158       elsif ( substr( $isbn, 3, 3 ) <= 995 ) {
159           $seg2 = substr( $isbn, 3, 3 );
160       }
161       elsif ( substr( $isbn, 3, 4 ) <= 9989 ) {
162           $seg2 = substr( $isbn, 3, 4 );
163       }
164       else {
165           $seg2 = substr( $isbn, 3, 5 );
166       }
167       my $x = substr( $isbn, length($seg2) +3);
168       my $seg3;
169       if ( substr( $x, 0, 2 ) <= 19 ) {
170   
171           # if(sTmp2 < 10) sTmp2 = "0" sTmp2;
172           $seg3 = substr( $x, 0, 2 );
173       }
174       elsif ( substr( $x, 0, 3 ) <= 699 ) {
175           $seg3 = substr( $x, 0, 3 );
176       }
177       elsif ( substr( $x, 0, 4 ) <= 8399 ) {
178           $seg3 = substr( $x, 0, 4 );
179       }
180       elsif ( substr( $x, 0, 5 ) <= 89999 ) {
181           $seg3 = substr( $x, 0, 5 );
182       }
183       elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
184           $seg3 = substr( $x, 0, 6 );
185       }
186       else {
187           $seg3 = substr( $x, 0, 7 );
188       }
189       my $seg4 = substr( $x, length($seg3) );
190       $seg4 = substr( $seg4, 0, length($seg4) - 1 );
191       my $seg5 = substr( $x, -1, 1 );
192       return "$seg1-$seg2-$seg3-$seg4-$seg5";       
193     }    
194 }
195
196 # FIXME.. this should be moved to a MARC-specific module
197 sub subfield_is_koha_internal_p ($) {
198     my ($subfield) = @_;
199
200     # We could match on 'lib' and 'tab' (and 'mandatory', & more to come!)
201     # But real MARC subfields are always single-character
202     # so it really is safer just to check the length
203
204     return length $subfield != 1;
205 }
206
207 =head2 GetItemTypes
208
209   $itemtypes = &GetItemTypes();
210
211 Returns information about existing itemtypes.
212
213 build a HTML select with the following code :
214
215 =head3 in PERL SCRIPT
216
217     my $itemtypes = GetItemTypes;
218     my @itemtypesloop;
219     foreach my $thisitemtype (sort keys %$itemtypes) {
220         my $selected = 1 if $thisitemtype eq $itemtype;
221         my %row =(value => $thisitemtype,
222                     selected => $selected,
223                     description => $itemtypes->{$thisitemtype}->{'description'},
224                 );
225         push @itemtypesloop, \%row;
226     }
227     $template->param(itemtypeloop => \@itemtypesloop);
228
229 =head3 in TEMPLATE
230
231     <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
232         <select name="itemtype">
233             <option value="">Default</option>
234         <!-- TMPL_LOOP name="itemtypeloop" -->
235             <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="description" --></option>
236         <!-- /TMPL_LOOP -->
237         </select>
238         <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
239         <input type="submit" value="OK" class="button">
240     </form>
241
242 =cut
243
244 sub GetItemTypes {
245
246     # returns a reference to a hash of references to branches...
247     my %itemtypes;
248     my $dbh   = C4::Context->dbh;
249     my $query = qq|
250         SELECT *
251         FROM   itemtypes
252     |;
253     my $sth = $dbh->prepare($query);
254     $sth->execute;
255     while ( my $IT = $sth->fetchrow_hashref ) {
256         $itemtypes{ $IT->{'itemtype'} } = $IT;
257     }
258     return ( \%itemtypes );
259 }
260
261 sub get_itemtypeinfos_of {
262     my @itemtypes = @_;
263
264     my $query = '
265 SELECT itemtype,
266        description,
267        notforloan
268   FROM itemtypes
269   WHERE itemtype IN (' . join( ',', map( { "'" . $_ . "'" } @itemtypes ) ) . ')
270 ';
271
272     return get_infos_of( $query, 'itemtype' );
273 }
274
275 # this is temporary until we separate collection codes and item types
276 sub GetCcodes {
277     my $count = 0;
278     my @results;
279     my $dbh = C4::Context->dbh;
280     my $sth =
281       $dbh->prepare(
282         "SELECT * FROM authorised_values ORDER BY authorised_value");
283     $sth->execute;
284     while ( my $data = $sth->fetchrow_hashref ) {
285         if ( $data->{category} eq "CCODE" ) {
286             $count++;
287             $results[$count] = $data;
288
289             #warn "data: $data";
290         }
291     }
292     $sth->finish;
293     return ( $count, @results );
294 }
295
296 =head2 getauthtypes
297
298   $authtypes = &getauthtypes();
299
300 Returns information about existing authtypes.
301
302 build a HTML select with the following code :
303
304 =head3 in PERL SCRIPT
305
306 my $authtypes = getauthtypes;
307 my @authtypesloop;
308 foreach my $thisauthtype (keys %$authtypes) {
309     my $selected = 1 if $thisauthtype eq $authtype;
310     my %row =(value => $thisauthtype,
311                 selected => $selected,
312                 authtypetext => $authtypes->{$thisauthtype}->{'authtypetext'},
313             );
314     push @authtypesloop, \%row;
315 }
316 $template->param(itemtypeloop => \@itemtypesloop);
317
318 =head3 in TEMPLATE
319
320 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
321     <select name="authtype">
322     <!-- TMPL_LOOP name="authtypeloop" -->
323         <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="authtypetext" --></option>
324     <!-- /TMPL_LOOP -->
325     </select>
326     <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
327     <input type="submit" value="OK" class="button">
328 </form>
329
330
331 =cut
332
333 sub getauthtypes {
334
335     # returns a reference to a hash of references to authtypes...
336     my %authtypes;
337     my $dbh = C4::Context->dbh;
338     my $sth = $dbh->prepare("select * from auth_types order by authtypetext");
339     $sth->execute;
340     while ( my $IT = $sth->fetchrow_hashref ) {
341         $authtypes{ $IT->{'authtypecode'} } = $IT;
342     }
343     return ( \%authtypes );
344 }
345
346 sub getauthtype {
347     my ($authtypecode) = @_;
348
349     # returns a reference to a hash of references to authtypes...
350     my %authtypes;
351     my $dbh = C4::Context->dbh;
352     my $sth = $dbh->prepare("select * from auth_types where authtypecode=?");
353     $sth->execute($authtypecode);
354     my $res = $sth->fetchrow_hashref;
355     return $res;
356 }
357
358 =head2 getframework
359
360   $frameworks = &getframework();
361
362 Returns information about existing frameworks
363
364 build a HTML select with the following code :
365
366 =head3 in PERL SCRIPT
367
368 my $frameworks = frameworks();
369 my @frameworkloop;
370 foreach my $thisframework (keys %$frameworks) {
371     my $selected = 1 if $thisframework eq $frameworkcode;
372     my %row =(value => $thisframework,
373                 selected => $selected,
374                 description => $frameworks->{$thisframework}->{'frameworktext'},
375             );
376     push @frameworksloop, \%row;
377 }
378 $template->param(frameworkloop => \@frameworksloop);
379
380 =head3 in TEMPLATE
381
382 <form action='<!-- TMPL_VAR name="script_name" -->' method=post>
383     <select name="frameworkcode">
384         <option value="">Default</option>
385     <!-- TMPL_LOOP name="frameworkloop" -->
386         <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="frameworktext" --></option>
387     <!-- /TMPL_LOOP -->
388     </select>
389     <input type=text name=searchfield value="<!-- TMPL_VAR name="searchfield" -->">
390     <input type="submit" value="OK" class="button">
391 </form>
392
393
394 =cut
395
396 sub getframeworks {
397
398     # returns a reference to a hash of references to branches...
399     my %itemtypes;
400     my $dbh = C4::Context->dbh;
401     my $sth = $dbh->prepare("select * from biblio_framework");
402     $sth->execute;
403     while ( my $IT = $sth->fetchrow_hashref ) {
404         $itemtypes{ $IT->{'frameworkcode'} } = $IT;
405     }
406     return ( \%itemtypes );
407 }
408
409 =head2 getframeworkinfo
410
411   $frameworkinfo = &getframeworkinfo($frameworkcode);
412
413 Returns information about an frameworkcode.
414
415 =cut
416
417 sub getframeworkinfo {
418     my ($frameworkcode) = @_;
419     my $dbh             = C4::Context->dbh;
420     my $sth             =
421       $dbh->prepare("select * from biblio_framework where frameworkcode=?");
422     $sth->execute($frameworkcode);
423     my $res = $sth->fetchrow_hashref;
424     return $res;
425 }
426
427 =head2 getitemtypeinfo
428
429   $itemtype = &getitemtype($itemtype);
430
431 Returns information about an itemtype.
432
433 =cut
434
435 sub getitemtypeinfo {
436     my ($itemtype) = @_;
437     my $dbh        = C4::Context->dbh;
438     my $sth        = $dbh->prepare("select * from itemtypes where itemtype=?");
439     $sth->execute($itemtype);
440     my $res = $sth->fetchrow_hashref;
441
442     $res->{imageurl} = getitemtypeimagesrcfromurl( $res->{imageurl} );
443
444     return $res;
445 }
446
447 sub getitemtypeimagesrcfromurl {
448     my ($imageurl) = @_;
449
450     if ( defined $imageurl and $imageurl !~ m/^http/ ) {
451         $imageurl = getitemtypeimagesrc() . '/' . $imageurl;
452     }
453
454     return $imageurl;
455 }
456
457 sub getitemtypeimagedir {
458     return C4::Context->opachtdocs . '/'
459       . C4::Context->preference('template')
460       . '/itemtypeimg';
461 }
462
463 sub getitemtypeimagesrc {
464     return '/opac-tmpl' . '/'
465       . C4::Context->preference('template')
466       . '/itemtypeimg';
467 }
468
469 =head2 GetPrinters
470
471   $printers = &GetPrinters();
472   @queues = keys %$printers;
473
474 Returns information about existing printer queues.
475
476 C<$printers> is a reference-to-hash whose keys are the print queues
477 defined in the printers table of the Koha database. The values are
478 references-to-hash, whose keys are the fields in the printers table.
479
480 =cut
481
482 sub GetPrinters {
483     my %printers;
484     my $dbh = C4::Context->dbh;
485     my $sth = $dbh->prepare("select * from printers");
486     $sth->execute;
487     while ( my $printer = $sth->fetchrow_hashref ) {
488         $printers{ $printer->{'printqueue'} } = $printer;
489     }
490     return ( \%printers );
491 }
492
493 =head2 GetPrinter
494
495 $printer = GetPrinter( $query, $printers );
496
497 =cut
498
499 sub GetPrinter ($$) {
500     my ( $query, $printers ) = @_;    # get printer for this query from printers
501     my $printer = $query->param('printer');
502     my %cookie = $query->cookie('userenv');
503     ($printer) || ( $printer = $cookie{'printer'} ) || ( $printer = '' );
504     ( $printers->{$printer} ) || ( $printer = ( keys %$printers )[0] );
505     return $printer;
506 }
507
508 =item getnbpages
509
510 Returns the number of pages to display in a pagination bar, given the number
511 of items and the number of items per page.
512
513 =cut
514
515 sub getnbpages {
516     my ( $nb_items, $nb_items_per_page ) = @_;
517
518     return int( ( $nb_items - 1 ) / $nb_items_per_page ) + 1;
519 }
520
521 =item getallthemes
522
523   (@themes) = &getallthemes('opac');
524   (@themes) = &getallthemes('intranet');
525
526 Returns an array of all available themes.
527
528 =cut
529
530 sub getallthemes {
531     my $type = shift;
532     my $htdocs;
533     my @themes;
534     if ( $type eq 'intranet' ) {
535         $htdocs = C4::Context->config('intrahtdocs');
536     }
537     else {
538         $htdocs = C4::Context->config('opachtdocs');
539     }
540     opendir D, "$htdocs";
541     my @dirlist = readdir D;
542     foreach my $directory (@dirlist) {
543         -d "$htdocs/$directory/en" and push @themes, $directory;
544     }
545     return @themes;
546 }
547
548 sub getFacets {
549     my $facets;
550     if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
551         $facets = [
552             {
553                 link_value  => 'su-to',
554                 label_value => 'Topics',
555                 tags        =>
556                   [ '600', '601', '602', '603', '604', '605', '606', '610' ],
557                 subfield => 'a',
558             },
559             {
560                 link_value  => 'su-geo',
561                 label_value => 'Places',
562                 tags        => ['651'],
563                 subfield    => 'a',
564             },
565             {
566                 link_value  => 'su-ut',
567                 label_value => 'Titles',
568                 tags        => [ '500', '501', '502', '503', '504', ],
569                 subfield    => 'a',
570             },
571             {
572                 link_value  => 'au',
573                 label_value => 'Authors',
574                 tags        => [ '700', '701', '702', ],
575                 subfield    => 'a',
576             },
577             {
578                 link_value  => 'se',
579                 label_value => 'Series',
580                 tags        => ['225'],
581                 subfield    => 'a',
582             },
583             {
584                 link_value  => 'branch',
585                 label_value => 'Libraries',
586                 tags        => [ '995', ],
587                 subfield    => 'b',
588                 expanded    => '1',
589             },
590         ];
591     }
592     else {
593         $facets = [
594             {
595                 link_value  => 'su-to',
596                 label_value => 'Topics',
597                 tags        => ['650'],
598                 subfield    => 'a',
599             },
600
601             #        {
602             #        link_value => 'su-na',
603             #        label_value => 'People and Organizations',
604             #        tags => ['600', '610', '611'],
605             #        subfield => 'a',
606             #        },
607             {
608                 link_value  => 'su-geo',
609                 label_value => 'Places',
610                 tags        => ['651'],
611                 subfield    => 'a',
612             },
613             {
614                 link_value  => 'su-ut',
615                 label_value => 'Titles',
616                 tags        => ['630'],
617                 subfield    => 'a',
618             },
619             {
620                 link_value  => 'au',
621                 label_value => 'Authors',
622                 tags        => [ '100', '110', '700', ],
623                 subfield    => 'a',
624             },
625             {
626                 link_value  => 'se',
627                 label_value => 'Series',
628                 tags        => [ '440', '490', ],
629                 subfield    => 'a',
630             },
631             {
632                 link_value  => 'branch',
633                 label_value => 'Libraries',
634                 tags        => [ '952', ],
635                 subfield    => 'b',
636                 expanded    => '1',
637             },
638         ];
639     }
640     return $facets;
641 }
642
643 =head2 get_infos_of
644
645 Return a href where a key is associated to a href. You give a query, the
646 name of the key among the fields returned by the query. If you also give as
647 third argument the name of the value, the function returns a href of scalar.
648
649   my $query = '
650 SELECT itemnumber,
651        notforloan,
652        barcode
653   FROM items
654 ';
655
656   # generic href of any information on the item, href of href.
657   my $iteminfos_of = get_infos_of($query, 'itemnumber');
658   print $iteminfos_of->{$itemnumber}{barcode};
659
660   # specific information, href of scalar
661   my $barcode_of_item = get_infos_of($query, 'itemnumber', 'barcode');
662   print $barcode_of_item->{$itemnumber};
663
664 =cut
665
666 sub get_infos_of {
667     my ( $query, $key_name, $value_name ) = @_;
668
669     my $dbh = C4::Context->dbh;
670
671     my $sth = $dbh->prepare($query);
672     $sth->execute();
673
674     my %infos_of;
675     while ( my $row = $sth->fetchrow_hashref ) {
676         if ( defined $value_name ) {
677             $infos_of{ $row->{$key_name} } = $row->{$value_name};
678         }
679         else {
680             $infos_of{ $row->{$key_name} } = $row;
681         }
682     }
683     $sth->finish;
684
685     return \%infos_of;
686 }
687
688 =head2 get_notforloan_label_of
689
690   my $notforloan_label_of = get_notforloan_label_of();
691
692 Each authorised value of notforloan (information available in items and
693 itemtypes) is link to a single label.
694
695 Returns a href where keys are authorised values and values are corresponding
696 labels.
697
698   foreach my $authorised_value (keys %{$notforloan_label_of}) {
699     printf(
700         "authorised_value: %s => %s\n",
701         $authorised_value,
702         $notforloan_label_of->{$authorised_value}
703     );
704   }
705
706 =cut
707
708 # FIXME - why not use GetAuthorisedValues ??
709 #
710 sub get_notforloan_label_of {
711     my $dbh = C4::Context->dbh;
712
713     my $query = '
714 SELECT authorised_value
715   FROM marc_subfield_structure
716   WHERE kohafield = \'items.notforloan\'
717   LIMIT 0, 1
718 ';
719     my $sth = $dbh->prepare($query);
720     $sth->execute();
721     my ($statuscode) = $sth->fetchrow_array();
722
723     $query = '
724 SELECT lib,
725        authorised_value
726   FROM authorised_values
727   WHERE category = ?
728 ';
729     $sth = $dbh->prepare($query);
730     $sth->execute($statuscode);
731     my %notforloan_label_of;
732     while ( my $row = $sth->fetchrow_hashref ) {
733         $notforloan_label_of{ $row->{authorised_value} } = $row->{lib};
734     }
735     $sth->finish;
736
737     return \%notforloan_label_of;
738 }
739
740 sub displayServers {
741     my ( $position, $type ) = @_;
742     my $dbh    = C4::Context->dbh;
743     my $strsth = "SELECT * FROM z3950servers where 1";
744     $strsth .= " AND position=\"$position\"" if ($position);
745     $strsth .= " AND type=\"$type\""         if ($type);
746     my $rq = $dbh->prepare($strsth);
747     $rq->execute;
748     my @primaryserverloop;
749
750     while ( my $data = $rq->fetchrow_hashref ) {
751         my %cell;
752         $cell{label} = $data->{'description'};
753         $cell{id}    = $data->{'name'};
754         $cell{value} =
755             $data->{host}
756           . ( $data->{port} ? ":" . $data->{port} : "" ) . "/"
757           . $data->{database}
758           if ( $data->{host} );
759         $cell{checked} = $data->{checked};
760         push @primaryserverloop,
761           {
762             label => $data->{description},
763             id    => $data->{name},
764             name  => "server",
765             value => $data->{host} . ":"
766               . $data->{port} . "/"
767               . $data->{database},
768             checked    => "checked",
769             icon       => $data->{icon},
770             zed        => $data->{type} eq 'zed',
771             opensearch => $data->{type} eq 'opensearch'
772           };
773     }
774     return \@primaryserverloop;
775 }
776
777 sub displaySecondaryServers {
778
779 #       my $secondary_servers_loop = [
780 #               { inner_sup_servers_loop => [
781 #               {label => "Google", id=>"GOOG", value=>"google",icon => "google.ico",opensearch => "1"},
782 #               {label => "Yahoo", id=>"YAH", value=>"yahoo", icon =>"yahoo.ico", zed => "1"},
783 #               {label => "Worldcat", id=>"WCT", value=>"worldcat", icon => "worldcat.gif", zed => "1"},
784 #               {label => "Library of Congress", id=>"LOC", name=> "server", value=>"z3950.loc.gov:7090/Voyager", icon =>"loc.ico", zed => "1"},
785 #       ],
786 #       },
787 #       ];
788     return;    #$secondary_servers_loop;
789 }
790
791 =head2 GetAuthValCode
792
793 $authvalcode = GetAuthValCode($kohafield,$frameworkcode);
794
795 =cut
796
797 sub GetAuthValCode {
798         my ($kohafield,$fwcode) = @_;
799         my $dbh = C4::Context->dbh;
800         $fwcode='' unless $fwcode;
801         my $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield=? and frameworkcode=?');
802         $sth->execute($kohafield,$fwcode);
803         my ($authvalcode) = $sth->fetchrow_array;
804         return $authvalcode;
805 }
806
807 =head2 GetAuthorisedValues
808
809 $authvalues = GetAuthorisedValues($category);
810
811 this function get all authorised values from 'authosied_value' table into a reference to array which
812 each value containt an hashref.
813
814 Set C<$category> on input args if you want to limits your query to this one. This params is not mandatory.
815
816 =cut
817
818 sub GetAuthorisedValues {
819     my ($category,$selected) = @_;
820         my $count = 0;
821         my @results;
822     my $dbh      = C4::Context->dbh;
823     my $query    = "SELECT * FROM authorised_values";
824     $query .= " WHERE category = '" . $category . "'" if $category;
825
826     my $sth = $dbh->prepare($query);
827     $sth->execute;
828         while (my $data=$sth->fetchrow_hashref) {
829                 if ($selected eq $data->{'authorised_value'} ) {
830                         $data->{'selected'} = 1;
831                 }
832                 $results[$count] = $data;
833                 $count++;
834         }
835     #my $data = $sth->fetchall_arrayref({});
836     return \@results; #$data;
837 }
838
839 =item fixEncoding
840
841   $marcrecord = &fixEncoding($marcblob);
842
843 Returns a well encoded marcrecord.
844
845 =cut
846 sub FixEncoding {
847   my $marc=shift;
848   my $record = MARC::Record->new_from_usmarc($marc);
849   if (C4::Context->preference("MARCFLAVOUR") eq "UNIMARC"){
850     use Encode::Guess;
851     my $targetcharset="utf8" if (C4::Context->preference("TemplateEncoding") eq "utf-8");
852     $targetcharset="latin1" if (C4::Context->preference("TemplateEncoding") eq "iso-8859-1");
853     my $decoder = guess_encoding($marc, qw/utf8 latin1/);
854 #     die $decoder unless ref($decoder);
855     if (ref($decoder)) {
856         my $newRecord=MARC::Record->new();
857         foreach my $field ($record->fields()){
858         if ($field->tag()<'010'){
859             $newRecord->insert_grouped_field($field);
860         } else {
861             my $newField;
862             my $createdfield=0;
863             foreach my $subfield ($field->subfields()){
864             if ($createdfield){
865                 if (($newField->tag eq '100')) {
866                     substr($subfield->[1],26,2,"0103") if ($targetcharset eq "latin1");
867                     substr($subfield->[1],26,4,"5050") if ($targetcharset eq "utf8");
868                 }
869                 map {C4::Biblio::char_decode($_,"UNIMARC")} @$subfield;
870                 $newField->add_subfields($subfield->[0]=>$subfield->[1]);
871             } else {
872                 map {C4::Biblio::char_decode($_,"UNIMARC")} @$subfield;
873                 $newField=MARC::Field->new($field->tag(),$field->indicator(1),$field->indicator(2),$subfield->[0]=>$subfield->[1]);
874                 $createdfield=1;
875             }
876             }
877             $newRecord->insert_grouped_field($newField);
878         }
879         }
880     #     warn $newRecord->as_formatted(); 
881         return $newRecord;
882     } else {
883         return $record;
884     }
885   } else {
886     return $record;
887   }
888 }
889
890 =head2 GetKohaAuthorisedValues
891         
892         Takes $dbh , $kohafield as parameters.
893         returns hashref of authvalCode => liblibrarian
894         or undef if no authvals defined for kohafield.
895
896 =cut
897
898 sub GetKohaAuthorisedValues {
899   my ($kohafield,$fwcode) = @_;
900   $fwcode='' unless $fwcode;
901   my %values;
902   my $dbh = C4::Context->dbh;
903   my $avcode = GetAuthValCode($kohafield,$fwcode);
904   if ($avcode) {  
905     my $sth = $dbh->prepare("select authorised_value, lib from authorised_values where category=? ");
906     $sth->execute($avcode);
907         while ( my ($val, $lib) = $sth->fetchrow_array ) { 
908                 $values{$val}= $lib;
909         }
910   }
911   return \%values;
912 }
913
914 =head2 GetManagedTagSubfields
915
916 =over 4
917
918 $res = GetManagedTagSubfields();
919
920 Returns a reference to a big hash of hash, with the Marc structure fro the given frameworkcode
921 $forlibrarian  :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
922 $frameworkcode : the framework code to read
923
924 =back
925
926 =back
927
928 =cut
929
930 sub GetManagedTagSubfields{
931   my $dbh=C4::Context->dbh;
932   my $rq=$dbh->prepare(qq|
933 SELECT 
934   DISTINCT CONCAT( marc_subfield_structure.tagfield, tagsubfield ) AS tagsubfield, 
935   marc_subfield_structure.liblibrarian as subfielddesc, 
936   marc_tag_structure.liblibrarian as tagdesc
937 FROM marc_subfield_structure
938   LEFT JOIN marc_tag_structure 
939     ON marc_tag_structure.tagfield = marc_subfield_structure.tagfield
940     AND marc_tag_structure.frameworkcode = marc_subfield_structure.frameworkcode
941 WHERE marc_subfield_structure.tab>=0
942 ORDER BY tagsubfield|);
943   $rq->execute;
944   my $data=$rq->fetchall_arrayref({});
945   return $data;
946 }
947
948 1;
949
950 __END__
951
952 =head1 AUTHOR
953
954 Koha Team
955
956 =cut