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