fixes, improvements and doc
[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 sub plugin_javascript {
53 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
54 my $function_name= "100".(int(rand(100000))+1);
55 my $res="
56 <script>
57 function Focus$function_name(subfield_managed) {
58 return 1;
59 }
60
61 function Blur$function_name(subfield_managed) {
62         return 1;
63 }
64
65 function Clic$function_name(index) {
66 // find the 010a value and the 210c. it will be used in the popup to find possibles collections
67         var isbn_found;
68         for (i=0 ; i<document.f.field_value.length ; i++) {
69                 if (document.f.tag[i].value == '010' && document.f.subfield[i].value == 'a') {
70                         isbn_found=document.f.field_value[i].value;
71                 }
72         }
73         var editor_found;
74         for (i=0 ; i<document.f.field_value.length ; i++) {
75                 if (document.f.tag[i].value == '210' && document.f.subfield[i].value == 'c') {
76                         editor_found=document.f.field_value[i].value;
77                 }
78         }
79
80         defaultvalue=document.f.field_value[index].value;
81         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');
82
83 }
84 </script>
85 ";
86
87 return ($function_name,$res);
88 }
89 sub plugin {
90 my ($input) = @_;
91         my %env;
92
93 #       my $input = new CGI;
94         my $index= $input->param('index');
95         my $result= $input->param('result');
96         my $editor_found = $input->param('editor_found');
97         my $isbn_found = $input->param('isbn_found');
98         my $dbh = C4::Context->dbh;
99         my $authoritysep = C4::Context->preference("authoritysep");
100         my ($template, $loggedinuser, $cookie)
101         = get_template_and_user({template_name => "value_builder/unimarc_field_225a.tmpl",
102                                         query => $input,
103                                         type => "intranet",
104                                         authnotrequired => 0,
105                                         flagsrequired => {parameters => 1},
106                                         debug => 1,
107                                         });
108 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
109         # if there is an isbn, complete search
110                 my @collections;
111         if ($isbn_found) {
112                 my $sth = $dbh->prepare("select stdlib from bibliothesaurus where father=? and category='EDITORS'");
113                 my @splited = split //, $isbn_found;
114                 my $isbn_rebuild='';
115                 foreach my $x (@splited) {
116                         $isbn_rebuild.=$x;
117                         $sth->execute("$isbn_rebuild $authoritysep $editor_found $authoritysep");
118                         while (my ($line)= $sth->fetchrow) {
119                                 push @collections,$line;
120                         }
121                 }
122         } else {
123         # if there is no isbn, search with %
124                 my $sth = $dbh->prepare("select stdlib from bibliothesaurus where father like ? and category='EDITORS'");
125                 $sth->execute("\%$authoritysep $editor_found $authoritysep");
126                 while (my ($line)= $sth->fetchrow) {
127                         push @collections,$line;
128                 }
129         }
130 #       my @collections = ["test"];
131         my $collection =CGI::scrolling_list(-name=>'f1',
132                                                                                                 -values=> \@collections,
133                                                                                                 -default=>"$result",
134                                                                                                 -size=>1,
135                                                                                                 -multiple=>0,
136                                                                                                 );
137         $template->param(index => $index,
138                                                         collection => $collection);
139         print $input->header(-cookie => $cookie),$template->output;
140 }
141
142 1;