rel_3_0 moved to HEAD (introducing new files)
[koha.git] / cataloguing / value_builder / unimarc_field_210c.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 require Exporter;
23 use C4::AuthoritiesMarc;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Output;
27 use C4::Interface::CGI::Output;
28 use CGI;
29 use C4::Search;
30 use MARC::Record;
31 use C4::Koha;
32
33
34 =head1
35
36 plugin_parameters : other parameters added when the plugin is called by the dopop function
37
38 =cut
39 sub plugin_parameters {
40 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
41 return "";
42 }
43
44 =head1
45
46 plugin_javascript : the javascript function called when the user enters the subfield.
47 contain 3 javascript functions :
48 * one called when the field is entered (OnFocus). Named FocusXXX
49 * one called when the field is leaved (onBlur). Named BlurXXX
50 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
51
52 returns :
53 * XXX
54 * a variable containing the 3 scripts.
55 the 3 scripts are inserted after the <input> in the html code
56
57 =cut
58 sub plugin_javascript {
59 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
60 my $function_name= "210c".(int(rand(100000))+1);
61 #---- build editors list.
62 #---- the editor list is built from the "EDITORS" thesaurus
63 #---- this thesaurus category must be filled as follow :
64 #---- 200$a for isbn
65 #---- 200$b for editor
66 #---- 200$c (repeated) for collections
67 my $sth = $dbh->prepare("select auth_subfield_table.authid,subfieldvalue from auth_subfield_table 
68                         left join auth_header on auth_subfield_table.authid=auth_header.authid
69                         where authtypecode='EDITORS' and tag='200' and subfieldcode='a'");
70 my $sth2 = $dbh->prepare("select subfieldvalue from auth_subfield_table where tag='200' and subfieldcode='b' and authid=?");
71 $sth->execute;
72 my @editors;
73 my $authoritysep = C4::Context->preference("authoritysep");
74 while (my ($authid,$isbn) = $sth->fetchrow) {
75     $sth2->execute($authid);
76     my ($editor) = $sth2->fetchrow;
77     push(@editors,"$isbn $authoritysep $editor");
78 }
79 my $res  = "
80 <script type=\"text/javascript\">
81 function Focus$function_name(index) {
82 var isbn_array = [ ";
83 foreach my $editor (@editors) {
84     my @arr = split (/ $authoritysep /,$editor);
85     $res .='["'.$arr[0].'","'.$arr[1].'","'.$arr[2].'"],';
86 }
87 chop $res;
88 $res .= "
89 ];
90     // search isbn subfield. it''s 010a
91     var isbn_found;
92     var nb_fields = document.f.field_value.length;
93     for (i=0 ; i< nb_fields; i++) {
94         if (document.f.tag[i].value == '010' && document.f.subfield[i].value == 'a') {
95             isbn_found=document.f.field_value[i].value;
96             break;
97         }
98     }
99     try{
100         isbn_found.getAttribute('value'); // throw an exception if doesn't (if no 010a)
101     }
102     catch(e){
103         return;
104     }
105     for (i=0;i<=isbn_array.length;i++) {
106         if (isbn_found.substr(0,isbn_array[i][0].length) == isbn_array[i][0]) {
107             document.f.field_value[index].value =isbn_array[i][1];
108         }
109     }
110 }
111
112 function Blur$function_name(subfield_managed) {
113     return 1;
114 }
115
116 function Clic$function_name(subfield_managed) {
117     defaultvalue=escape(document.forms['f'].field_value[subfield_managed].value);
118     newin=window.open(\"plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&index=\"+subfield_managed,\"unimarc 225a\",'width=500,height=600,toolbar=false,scrollbars=yes');
119 }
120 </script>
121 ";
122 return ($function_name,$res);
123 }
124
125 =head1
126
127 plugin : the true value_builded. The screen that is open in the popup window.
128
129 =cut
130
131 sub plugin {
132 my ($input) = @_;
133     my $index = $input->param("index");
134     my $result =  $input->param("result");
135
136     my $query=new CGI;
137     my $op = $query->param('op');
138     my $authtypecode = $query->param('authtypecode');
139     my $index = $query->param('index');
140     my $category = $query->param('category');
141     my $resultstring = $query->param('result');
142     my $dbh = C4::Context->dbh;
143     
144     my $startfrom=$query->param('startfrom');
145     $startfrom=0 if(!defined $startfrom);
146     my ($template, $loggedinuser, $cookie);
147     my $resultsperpage;
148     
149     my $authtypes = getauthtypes;
150     my @authtypesloop;
151     foreach my $thisauthtype (keys %$authtypes) {
152         my $selected = 1 if $thisauthtype eq $authtypecode;
153         my %row =(value => $thisauthtype,
154                     selected => $selected,
155                     authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
156                 index => $index,
157                 );
158         push @authtypesloop, \%row;
159     }
160
161     if ($op eq "do_search") {
162         my @marclist = $query->param('marclist');
163         my @and_or = $query->param('and_or');
164         my @excluding = $query->param('excluding');
165         my @operator = $query->param('operator');
166         my @value = $query->param('value');
167     
168         $resultsperpage= $query->param('resultsperpage');
169         $resultsperpage = 19 if(!defined $resultsperpage);
170     
171         # builds tag and subfield arrays
172         my @tags;
173     
174         my ($results,$total) = authoritysearch($dbh, \@tags,\@and_or,
175                                             \@excluding, \@operator, \@value,
176                                             $startfrom*$resultsperpage, $resultsperpage,$authtypecode);# $orderby);
177     
178         ($template, $loggedinuser, $cookie)
179             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
180                     query => $query,
181                     type => 'intranet',
182                     authnotrequired => 0,
183                     flagsrequired => {editcatalogue => 1},
184                     debug => 1,
185                     });
186     
187         # multi page display gestion
188         my $displaynext=0;
189         my $displayprev=$startfrom;
190         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
191             $displaynext = 1;
192         }
193     
194         my @numbers = ();
195     
196         if ($total>$resultsperpage) {
197             for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
198                 if ($i<16) {
199                     my $highlight=0;
200                     ($startfrom==($i-1)) && ($highlight=1);
201                     push @numbers, { number => $i,
202                         highlight => $highlight ,
203                         startfrom => ($i-1)};
204                 }
205             }
206         }
207     
208         my $from = $startfrom*$resultsperpage+1;
209         my $to;
210     
211         if($total < (($startfrom+1)*$resultsperpage)) {
212             $to = $total;
213         } else {
214             $to = (($startfrom+1)*$resultsperpage);
215         }
216         $template->param(result => $results) if $results;
217         $template->param(index => $query->param('index'));
218         $template->param(startfrom=> $startfrom,
219                                 displaynext=> $displaynext,
220                                 displayprev=> $displayprev,
221                                 resultsperpage => $resultsperpage,
222                                 startfromnext => $startfrom+1,
223                                 startfromprev => $startfrom-1,
224                                 index => $index,
225                                 total=>$total,
226                                 from=>$from,
227                                 to=>$to,
228                                 numbers=>\@numbers,
229                                 authtypecode =>$authtypecode,
230                                 resultstring =>$value[0],
231                                 );
232     } else {
233         ($template, $loggedinuser, $cookie)
234             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
235                     query => $query,
236                     type => 'intranet',
237                     authnotrequired => 0,
238                     flagsrequired => {editcatalogue => 1},
239                     debug => 1,
240                     });
241     
242         $template->param(index => $index,
243                         resultstring => $resultstring
244                         );
245     }
246     
247     $template->param(authtypesloop => \@authtypesloop);
248     $template->param(category => $category);
249     
250     # Print the page
251     output_html_with_http_headers $query, $cookie, $template->output;
252 }
253
254 1;