new and modified plugins for unimarc biblio management.
[koha.git] / value_builder / unimarc_field_700_701_702.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 CGI;
24 use C4::Context;
25 use HTML::Template;
26 use C4::Search;
27 use C4::Output;
28 use C4::Authorities;
29
30 sub plugin_parameters {
31 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
32 return "";
33 }
34 sub plugin_javascript {
35 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
36 my $function_name= "100".(int(rand(100000))+1);
37 my $res="
38 <script>
39 function Focus$function_name(subfield_managed) {
40 return 1;
41 }
42
43 function Blur$function_name(subfield_managed) {
44         return 1;
45 }
46
47 function Clic$function_name(index) {
48         defaultvalue=document.f.field_value[index].value;
49         newin=window.open(\"../plugin_launcher.pl?plugin_name=unimarc_field_700_701_702.pl&index=\"+index+\"&result=\"+defaultvalue,\"unimarc 700\",'width=700,height=300,toolbar=false,scrollbars=yes');
50
51 }
52 </script>
53 ";
54
55 return ($function_name,$res);
56 }
57 sub plugin {
58         my ($input) = @_;
59         my %env;
60         my $dbh = C4::Context->dbh;
61 #       my $input = new CGI;
62         my $index= $input->param('index');
63         my $result= $input->param('result');
64         my $search_string= $input->param('search_string');
65         my $op = $input->param('op');
66         my $id = $input->param('id');
67         my $insert = $input->param('insert');
68         my @freelib;
69         my %stdlib;
70         my $select_list;
71         my ($a,$b,$c,$f) ; # the 4 managed subfields.
72         if ($op eq "add") {
73                 newauthority($dbh,'NP',$insert,$insert,'',1,'');
74                 $search_string=$insert;
75         }
76         if ($op eq "select") {
77                 my $sti = $dbh->prepare("select stdlib from bibliothesaurus where id=?");
78                 $sti->execute($id);
79                 my ($freelib_text) = $sti->fetchrow_array;
80                 $result = $freelib_text;
81                 # fill the 4 managed subfields
82                 my @arr = split //,$result;
83                 my $where = 1;
84                 foreach my $x (@arr) {
85                         next if ($x eq ')');
86                         if ($x eq ',') {
87                                 $where=2;
88                                 next;
89                         }
90                         if ($x eq '(') {
91                                 if ($result =~ /.*;.*/) {
92                                         $where=3;
93                                 } else {
94                                         $where=4;
95                                 }
96                                 next;
97                         }
98                         if ($x eq ';') {
99                                 $where=4;
100                                 next;
101                         }
102                         if ($where eq 1) {
103                                 $a.=$x;
104                         }
105                         if ($where eq 2) {
106                                 $b.=$x;
107                         }
108                         if ($where eq 3) {
109                                 $c.=$x;
110                         }
111                         if ($where eq 4) {
112                                 $f.=$x;
113                         }
114                 }
115 # remove trailing blanks
116                 $a=~ s/^\s+//g;
117                 $b=~ s/^\s+//g;
118                 $c=~ s/^\s+//g;
119                 $f=~ s/^\s+//g;
120                 $a=~ s/\s+$//g;
121                 $b=~ s/\s+$//g;
122                 $c=~ s/\s+$//g;
123                 $f=~ s/^s+$//g;
124         }
125         if ($search_string) {
126         #       my $sti=$dbh->prepare("select id,freelib from bibliothesaurus where freelib like '".$search_string."%' and category ='$category'");
127                 my $sti=$dbh->prepare("select id,freelib from bibliothesaurus where match (category,freelib) AGAINST ('$search_string') and category ='NP'");
128                 $sti->execute;
129                 while (my $line=$sti->fetchrow_hashref) {
130                         $stdlib{$line->{'id'}} = "$line->{'freelib'}";
131                         push(@freelib,$line->{'id'});
132                 }
133                 $select_list= CGI::scrolling_list( -name=>'id',
134                                 -values=> \@freelib,
135                                 -default=> "",
136                                 -size=>1,
137                                 -multiple=>0,
138                                 -labels=> \%stdlib
139                                 );
140         }
141         my $template = gettemplate("value_builder/unimarc_field_700_701_702.tmpl",0);
142 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
143         $template->param(index => $index,
144                                                         result =>$result,
145                                                         select_list => $select_list,
146                                                         search_string => $search_string,
147                                                         a => $a,
148                                                         b => $b,
149                                                         c => $c,
150                                                         f => $f,);
151         print "Content-Type: text/html\n\n", $template->output;
152 }
153
154 1;