*** empty log message ***
[koha.git] / value_builder / unimarc_field_225a.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 =head1 SYNOPSIS
22
23 This plugin is used to map isbn/editor with collection.
24 It need :
25   in thesaurus, a category named EDITORS
26   in this category, datas must be entered like following :
27   isbn separator editor separator collection.
28   for example :
29   2204 -- Cerf -- Cogitatio fidei
30   2204 -- Cerf -- Le Magistère de l'Eglise
31   2204 -- Cerf -- Lectio divina
32   2204 -- Cerf -- Lire la Bible
33   2204 -- Cerf -- Pour lire
34   2204 -- Cerf -- Sources chrétiennes
35
36   when the user clic on ... on 225a line, the popup shows the list of collections from the selected editor
37   if the biblio has no isbn, then the search if done on editor only
38   If the biblio ha an isbn, the search is done on isbn and editor. It's faster.
39
40 =over 2
41
42 =cut
43
44 use strict;
45 use C4::Auth;
46 use CGI;
47 use C4::Context;
48 use HTML::Template;
49 use C4::Search;
50 use C4::Output;
51
52 =head1
53
54 plugin_parameters : other parameters added when the plugin is called by the dopop function
55
56 =cut
57 sub plugin_parameters {
58 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
59 return "";
60 }
61
62 sub plugin_javascript {
63 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
64 my $function_name= "100".(int(rand(100000))+1);
65 my $res="
66 <script>
67 function Focus$function_name(subfield_managed) {
68 return 1;
69 }
70
71 function Blur$function_name(subfield_managed) {
72         return 1;
73 }
74
75 function Clic$function_name(index) {
76 // find the 010a value and the 210c. it will be used in the popup to find possibles collections
77         var isbn_found;
78         for (i=0 ; i<document.f.field_value.length ; i++) {
79                 if (document.f.tag[i].value == '010' && document.f.subfield[i].value == 'a') {
80                         isbn_found=document.f.field_value[i].value;
81                 }
82         }
83         var editor_found;
84         for (i=0 ; i<document.f.field_value.length ; i++) {
85                 if (document.f.tag[i].value == '210' && document.f.subfield[i].value == 'c') {
86                         editor_found=document.f.field_value[i].value;
87                 }
88         }
89
90         defaultvalue=document.f.field_value[index].value;
91         newin=window.open(\"../plugin_launcher.pl?plugin_name=unimarc_field_225a.pl&index=\"+index+\"&result=\"+defaultvalue+\"&isbn_found=\"+isbn_found+\"&editor_found=\"+editor_found,\"unimarc 225a\",'width=500,height=200,toolbar=false,scrollbars=no');
92
93 }
94 </script>
95 ";
96
97 return ($function_name,$res);
98 }
99 sub plugin {
100 my ($input) = @_;
101         my %env;
102
103 #       my $input = new CGI;
104         my $index= $input->param('index');
105         my $result= $input->param('result');
106         my $editor_found = $input->param('editor_found');
107         my $isbn_found = $input->param('isbn_found');
108         my $dbh = C4::Context->dbh;
109         my $authoritysep = C4::Context->preference("authoritysep");
110         my ($template, $loggedinuser, $cookie)
111         = get_template_and_user({template_name => "value_builder/unimarc_field_225a.tmpl",
112                                         query => $input,
113                                         type => "intranet",
114                                         authnotrequired => 0,
115                                         flagsrequired => {parameters => 1},
116                                         debug => 1,
117                                         });
118 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
119         # if there is an isbn, complete search
120                 my @collections;
121         if ($isbn_found) {
122                 my $sth = $dbh->prepare("select stdlib from bibliothesaurus where father=? and category='EDITORS'");
123                 my @splited = split //, $isbn_found;
124                 my $isbn_rebuild='';
125                 foreach my $x (@splited) {
126                         $isbn_rebuild.=$x;
127                         $sth->execute("$isbn_rebuild $authoritysep $editor_found $authoritysep");
128                         while (my ($line)= $sth->fetchrow) {
129                                 push @collections,$line;
130                         }
131                 }
132         } else {
133         # if there is no isbn, search with %
134                 my $sth = $dbh->prepare("select stdlib from bibliothesaurus where father like ? and category='EDITORS'");
135                 $sth->execute("\%$authoritysep $editor_found $authoritysep");
136                 while (my ($line)= $sth->fetchrow) {
137                         push @collections,$line;
138                 }
139         }
140 #       my @collections = ["test"];
141         my $collection =CGI::scrolling_list(-name=>'f1',
142                                                                                                 -values=> \@collections,
143                                                                                                 -default=>"$result",
144                                                                                                 -size=>1,
145                                                                                                 -multiple=>0,
146                                                                                                 );
147         $template->param(index => $index,
148                                                         collection => $collection);
149         print $input->header(-cookie => $cookie),$template->output;
150 }
151
152 1;