adding 'gettext' as this is requred by the translation utility
[koha.git] / cataloguing / value_builder / unimarc_field_225a.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
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 Magistere de l'Eglise
31   2204 -- Cerf -- Lectio divina
32   2204 -- Cerf -- Lire la Bible
33   2204 -- Cerf -- Pour lire
34   2204 -- Cerf -- Sources chretiennes
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
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
58 sub plugin_parameters {
59     my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
60     return "";
61 }
62
63 sub plugin_javascript {
64     my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
65     my $function_name = $field_number;
66     my $res = "
67     <script type=\"text/javascript\">
68         function Focus$function_name(subfield_managed) {
69             return 1;
70         }
71     
72         function Blur$function_name(subfield_managed) {
73             return 1;
74         }
75     
76         function Clic$function_name(index) {
77         // find the 010a value and the 210c. it will be used in the popup to find possibles collections
78             var isbn_found   = 0;
79             var editor_found = 0;
80             
81             var re1 = /'tag_010_subfield_a_.*'/;
82             var re2 = /'tag_210_subfield_c_.*'/;
83             
84             var inputs = document.getElementsByTagName('input');
85             
86             for(var i=0 , len=inputs.length ; i \< len ; i++ ){
87                 if(inputs[i].id.match(re1)){
88                     isbn_found = inputs[i].value;
89                 }
90                 if(inputs[i].id.match(re2)){
91                     editor_found = inputs[i].value;
92                 }
93                 if(editor_found && isbn_found){
94                     break;
95                 }
96             }
97                     
98             defaultvalue = document.getElementById(\"$field_number\").value;
99             window.open(\"../cataloguing/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');
100     
101         }
102     </script>
103 ";
104
105     return ( $function_name, $res );
106 }
107
108 sub plugin {
109     my ($input)      = @_;
110     my $index        = $input->param('index');
111     my $result       = $input->param('result');
112     my $editor_found = $input->param('editor_found');
113     my $isbn_found   = $input->param('isbn_found');
114     my $dbh          = C4::Context->dbh;
115     my $authoritysep = C4::Context->preference("authoritysep");
116     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
117         {
118             template_name =>
119               "cataloguing/value_builder/unimarc_field_225a.tmpl",
120             query           => $input,
121             type            => "intranet",
122             authnotrequired => 0,
123             flagsrequired   => { editcatalogue => 1 },
124             debug           => 1,
125         }
126     );
127
128 # builds collection list : search isbn and editor, in parent, then load collections from bibliothesaurus table
129 # if there is an isbn, complete search
130     my @collections;
131     if ($isbn_found) {
132         my $sth = $dbh->prepare(
133             "SELECT auth_subfield_table.authid,subfieldvalue
134             FROM   auth_subfield_table
135             LEFT JOIN auth_header ON auth_subfield_table.authid = auth_header.authid 
136             WHERE authtypecode='EDITORS' 
137                AND tag='200'
138                AND subfieldcode='a'
139                AND subfieldvalue=?"
140         );
141         my $sth2 =
142           $dbh->prepare(
143             "SELECT subfieldvalue
144              FROM auth_subfield_table 
145              WHERE tag='200'
146              AND subfieldcode='c'
147              AND authid=?
148              ORDER BY subfieldvalue"
149           );
150         my @splited = split //, $isbn_found;
151         my $isbn_rebuild = '';
152         foreach my $x (@splited) {
153             $isbn_rebuild .= $x;
154             $sth->execute($isbn_rebuild);
155             my ($authid) = $sth->fetchrow;
156             $sth2->execute($authid);
157             while ( my ($line) = $sth2->fetchrow ) {
158                 push @collections, $line;
159             }
160         }
161     }
162     else {
163         my $sth = $dbh->prepare(
164             "SELECT auth_subfield_table.authid,subfieldvalue
165              FROM auth_subfield_table
166              LEFT JOIN auth_header ON auth_subfield_table.authid = auth_header.authid 
167              WHERE authtypecode='EDITORS'
168                AND tag='200'
169                AND subfieldcode='b'
170                AND subfieldvalue=?"
171         );
172         my $sth2 =
173           $dbh->prepare(
174             "SELECT subfieldvalue
175              FROM auth_subfield_table
176              WHERE tag='200'
177                 AND subfieldcode='c'
178                 AND authid=?
179              ORDER BY subfieldvalue"
180           );
181         $sth->execute($editor_found);
182         my ($authid) = $sth->fetchrow;
183         $sth2->execute($authid);
184         while ( my ($line) = $sth2->fetchrow ) {
185             push @collections, $line;
186         }
187     }
188
189     #   my @collections = ["test"];
190     my $collection = CGI::scrolling_list(
191         -name     => 'f1',
192         -values   => \@collections,
193         -default  => "$result",
194         -size     => 1,
195         -multiple => 0,
196     );
197     $template->param(
198         index      => $index,
199         collection => $collection
200     );
201     output_html_with_http_headers $input, $cookie, $template->output;
202 }
203
204 1;