rel_3_0 moved to HEAD (introducing new files)
[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= "100".(int(rand(100000))+1);
34 my $res="
35 <script>
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.f.field_value[index].value;
46         newin=window.open(\"plugin_launcher.pl?plugin_name=unimarc_field_60X.pl&index=\"+index+\"&result=\"+defaultvalue,\"unimarc 700\",'width=700,height=300,toolbar=false,scrollbars=yes');
47
48 }
49 </script>
50 ";
51
52 return ($function_name,$res);
53 }
54 sub plugin {
55         my ($input) = @_;
56         my %env;
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         if ($op eq "add") {
67                 newauthority($dbh,'NC',$insert,$insert,'',1,'');
68                 $search_string=$insert;
69         }
70         if ($op eq "select") {
71                 my $sti = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
72                 $sti->execute($id);
73                 my ($freelib_text) = $sti->fetchrow_array;
74                 $result = $freelib_text;
75         }
76         my $Rsearch_string="$search_string%";
77         my $authoritysep = C4::Context->preference('authoritysep');
78         my @splitted = /$authoritysep/,$search_string;
79         my $level = $#splitted+1;
80         my $sti;
81         if ($search_string) { # if no search pattern, returns only the 50 1st top level values
82                 $sti=$dbh->prepare("select distinct freelib,father,level from bibliothesaurus where category='NC' and freelib like ? order by father,freelib");
83         } else {
84                 $sti=$dbh->prepare("select distinct freelib,father,level from bibliothesaurus where category='NC' and level=0 and freelib like ? order by father,freelib limit 0,50");
85         }
86         $sti->execute($Rsearch_string);
87         my @results;
88         while (my ($freelib,$father,$level)=$sti->fetchrow) {
89                 my %line;
90                 if ($father) {
91                         $line{value} = "$father $freelib";
92                 } else {
93                         $line{value} = "$freelib";
94                 }
95                 $line{level} = $level+1;
96                 $line{father} = $father;
97                 push @results, \%line;
98         }
99         my @DeeperResults = SearchDeeper('NC',$search_string);
100         my ($template, $loggedinuser, $cookie)
101         = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_60X.tmpl",
102                                         query => $input,
103                                         type => "intranet",
104                                         authnotrequired => 0,
105                                         flagsrequired => {editcatalogue => 1},
106                                         debug => 1,
107                                         });
108 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
109         $template->param(index => $index,
110                                                         result =>$result,
111                                                         search_string => $search_string?$search_string:$result,
112                                                         results => \@results,
113                                                         deeper => \@DeeperResults,
114                                 );
115         print $input->header(-cookie => $cookie),$template->output;
116 }
117
118 1;