patches from paul
[koha.git] / cataloguing / value_builder / unimarc_field_60X.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26
27 use C4::Search;
28 use C4::Output;
29 use C4::Authorities;
30
31 sub plugin_javascript {
32     my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
33     my $function_name = $field_number;
34     my $res           = "
35         <script type=\"text/javascript\">
36             function Focus$function_name(subfield_managed) {
37                 return 1;
38             }
39
40             function Blur$function_name(subfield_managed) {
41                 return 1;
42             }
43
44             function Clic$function_name(index) {
45                 defaultvalue=document.getElementById(\"$field_number\").value;
46                 window.open(\"plugin_launcher.pl?plugin_name=unimarc_field_60X.pl&index=$field_number&result=\"+defaultvalue,\"unimarc 600\",'width=700,height=300,toolbar=false,scrollbars=yes');
47
48             }
49         </script>
50     ";
51
52     return ( $function_name, $res );
53 }
54
55 sub plugin {
56     my ($input)       = @_;
57     my $dbh           = C4::Context->dbh;
58     my $index         = $input->param('index');
59     my $result        = $input->param('result');
60     my $search_string = $input->param('search_string');
61     my $op            = $input->param('op');
62     my $id            = $input->param('id');
63     my $insert        = $input->param('insert');
64     my %stdlib;
65     my $select_list;
66
67     if ( $op eq "add" ) {
68         newauthority( $dbh, 'NC', $insert, $insert, '', 1, '' );
69         $search_string = $insert;
70     }
71     if ( $op eq "select" ) {
72         my $sti =
73           $dbh->prepare("select stdlib from bibliothesaurus where id=?");
74         $sti->execute($id);
75         my ($freelib_text) = $sti->fetchrow_array;
76         $result = $freelib_text;
77     }
78     my $Rsearch_string = "$search_string%";
79     my $authoritysep   = C4::Context->preference('authoritysep');
80     my @splitted       = /$authoritysep/, $search_string;
81     my $level          = $#splitted + 1;
82     my $sti;
83     if ($search_string)
84     {    # if no search pattern, returns only the 50 1st top level values
85         $sti =
86           $dbh->prepare(
87 "select distinct freelib,father,level from bibliothesaurus where category='NC' and freelib like ? order by father,freelib"
88           );
89     }
90     else {
91         $sti =
92           $dbh->prepare(
93 "select distinct freelib,father,level from bibliothesaurus where category='NC' and level=0 and freelib like ? order by father,freelib limit 0,50"
94           );
95     }
96     $sti->execute($Rsearch_string);
97     my @results;
98     while ( my ( $freelib, $father, $level ) = $sti->fetchrow ) {
99         my %line;
100         if ($father) {
101             $line{value} = "$father $freelib";
102         }
103         else {
104             $line{value} = "$freelib";
105         }
106         $line{level}  = $level + 1;
107         $line{father} = $father;
108         push @results, \%line;
109     }
110     my @DeeperResults = SearchDeeper( 'NC', $search_string );
111     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
112         {
113             template_name => "cataloguing/value_builder/unimarc_field_60X.tmpl",
114             query         => $input,
115             type          => "intranet",
116             authnotrequired => 0,
117             flagsrequired   => { editcatalogue => 1 },
118             debug           => 1,
119         }
120     );
121
122 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
123     $template->param(
124         index         => $index,
125         result        => $result,
126         search_string => $search_string ? $search_string : $result,
127         results       => \@results,
128         deeper        => \@DeeperResults,
129     );
130     output_html_with_http_headers $input, $cookie, $template->output;
131 }
132
133 1;