partial 1481: adding back missing updateitem.pl script
[koha.git] / cataloguing / 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
22 =head1 SYNOPSIS
23
24 This plugin is used to map isbn/editor with collection.
25 It need :
26   in thesaurus, a category named EDITORS
27   in this category, datas must be entered like following :
28   isbn separator editor separator collection.
29   for example :
30   2204 -- Cerf -- Cogitatio fidei
31   2204 -- Cerf -- Le Magistere de l'Eglise
32   2204 -- Cerf -- Lectio divina
33   2204 -- Cerf -- Lire la Bible
34   2204 -- Cerf -- Pour lire
35   2204 -- Cerf -- Sources chretiennes
36
37   when the user clic on ... on 225a line, the popup shows the list of collections from the selected editor
38   if the biblio has no isbn, then the search if done on editor only
39   If the biblio ha an isbn, the search is done on isbn and editor. It's faster.
40
41 =over 2
42
43 =cut
44
45 use strict;
46 use C4::Auth;
47 use CGI;
48 use C4::Context;
49
50 use C4::Search;
51 use C4::Output;
52
53 =head1
54
55 plugin_parameters : other parameters added when the plugin is called by the dopop function
56
57 =cut
58
59 sub plugin_parameters {
60     my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
61     return "";
62 }
63
64 sub plugin_javascript {
65     my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
66     my $function_name = $field_number;
67     my $res = "
68     <script type=\"text/javascript\">
69         function Focus$function_name(subfield_managed) {
70             return 1;
71         }
72     
73         function Blur$function_name(subfield_managed) {
74             return 1;
75         }
76     
77         function Clic$function_name(index) {
78         // find the 010a value and the 210c. it will be used in the popup to find possibles collections
79             var isbn_found   = 0;
80             var editor_found = 0;
81             
82             var re1 = /'tag_010_subfield_a_.*'/;
83             var re2 = /'tag_210_subfield_c_.*'/;
84             
85             var inputs = document.getElementsByTagName('input');
86             
87             for(var i=0 , len=inputs.length ; i \< len ; i++ ){
88                 if(inputs[i].id.match(re1)){
89                     isbn_found = inputs[i].value;
90                 }
91                 if(inputs[i].id.match(re2)){
92                     editor_found = inputs[i].value;
93                 }
94                 if(editor_found && isbn_found){
95                     break;
96                 }
97             }
98                     
99             defaultvalue = document.getElementById(\"$field_number\").value;
100             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');
101     
102         }
103     </script>
104 ";
105
106     return ( $function_name, $res );
107 }
108
109 sub plugin {
110     my ($input)      = @_;
111     my $index        = $input->param('index');
112     my $result       = $input->param('result');
113     my $editor_found = $input->param('editor_found');
114     my $isbn_found   = $input->param('isbn_found');
115     my $dbh          = C4::Context->dbh;
116     my $authoritysep = C4::Context->preference("authoritysep");
117     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
118         {
119             template_name =>
120               "cataloguing/value_builder/unimarc_field_225a.tmpl",
121             query           => $input,
122             type            => "intranet",
123             authnotrequired => 0,
124             flagsrequired   => { editcatalogue => 1 },
125             debug           => 1,
126         }
127     );
128
129 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
130 # if there is an isbn, complete search
131     my @collections;
132     if ($isbn_found) {
133         my $sth = $dbh->prepare(
134             "SELECT auth_subfield_table.authid,subfieldvalue
135             FROM   auth_subfield_table
136             LEFT JOIN auth_header ON auth_subfield_table.authid = auth_header.authid 
137             WHERE authtypecode='EDITORS' 
138                AND tag='200'
139                AND subfieldcode='a'
140                AND subfieldvalue=?"
141         );
142         my $sth2 =
143           $dbh->prepare(
144             "SELECT subfieldvalue
145              FROM auth_subfield_table 
146              WHERE tag='200'
147              AND subfieldcode='c'
148              AND authid=?
149              ORDER BY subfieldvalue"
150           );
151         my @splited = split //, $isbn_found;
152         my $isbn_rebuild = '';
153         foreach my $x (@splited) {
154             $isbn_rebuild .= $x;
155             $sth->execute($isbn_rebuild);
156             my ($authid) = $sth->fetchrow;
157             $sth2->execute($authid);
158             while ( my ($line) = $sth2->fetchrow ) {
159                 push @collections, $line;
160             }
161         }
162     }
163     else {
164         my $sth = $dbh->prepare(
165             "SELECT auth_subfield_table.authid,subfieldvalue
166              FROM auth_subfield_table
167              LEFT JOIN auth_header ON auth_subfield_table.authid = auth_header.authid 
168              WHERE authtypecode='EDITORS'
169                AND tag='200'
170                AND subfieldcode='b'
171                AND subfieldvalue=?"
172         );
173         my $sth2 =
174           $dbh->prepare(
175             "SELECT subfieldvalue
176              FROM auth_subfield_table
177              WHERE tag='200'
178                 AND subfieldcode='c'
179                 AND authid=?
180              ORDER BY subfieldvalue"
181           );
182         $sth->execute($editor_found);
183         my ($authid) = $sth->fetchrow;
184         $sth2->execute($authid);
185         while ( my ($line) = $sth2->fetchrow ) {
186             push @collections, $line;
187         }
188     }
189
190     #   my @collections = ["test"];
191     my $collection = CGI::scrolling_list(
192         -name     => 'f1',
193         -values   => \@collections,
194         -default  => "$result",
195         -size     => 1,
196         -multiple => 0,
197     );
198     $template->param(
199         index      => $index,
200         collection => $collection
201     );
202     output_html_with_http_headers $input, $cookie, $template->output;
203 }
204
205 1;