Finalized XML version for intranet
[koha.git] / 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 use HTML::Template;
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>
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         for (i=0 ; i<document.f.field_value.length ; i++) {
93                 if (document.f.tag[i].value == '010' && document.f.subfield[i].value == 'a') {
94                         isbn_found=document.f.field_value[i].value;
95                 }
96         }
97         for (i=0;i<=isbn_array.length;i++) {
98                 if (isbn_found.substr(0,isbn_array[i][0].length) == isbn_array[i][0]) {
99                         document.f.field_value[index].value =isbn_array[i][1];
100                 }
101         }
102 }
103
104 function Blur$function_name(subfield_managed) {
105         return 1;
106 }
107
108 function Clic$function_name(subfield_managed) {
109         defaultvalue=escape(document.forms[0].field_value[subfield_managed].value);
110         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');
111 }
112 </script>
113 ";
114 return ($function_name,$res);
115 }
116
117 =head1
118
119 plugin : the true value_builded. The screen that is open in the popup window.
120
121 =cut
122
123 sub plugin {
124 my ($input) = @_;
125         my $index = $input->param("index");
126         my $result =  $input->param("result");
127
128         my $query=new CGI;
129         my $op = $query->param('op');
130         my $authtypecode = $query->param('authtypecode');
131         my $index = $query->param('index');
132         my $category = $query->param('category');
133         my $resultstring = $query->param('result');
134         my $dbh = C4::Context->dbh;
135         
136         my $startfrom=$query->param('startfrom');
137         $startfrom=0 if(!defined $startfrom);
138         my ($template, $loggedinuser, $cookie);
139         my $resultsperpage;
140         
141         my $authtypes = getauthtypes;
142         my @authtypesloop;
143         foreach my $thisauthtype (keys %$authtypes) {
144                 my $selected = 1 if $thisauthtype eq $authtypecode;
145                 my %row =(value => $thisauthtype,
146                                         selected => $selected,
147                                         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
148                                 index => $index,
149                                 );
150                 push @authtypesloop, \%row;
151         }
152
153         if ($op eq "do_search") {
154                 my @marclist = $query->param('marclist');
155                 my @and_or = $query->param('and_or');
156                 my @excluding = $query->param('excluding');
157                 my @operator = $query->param('operator');
158                 my @value = $query->param('value');
159         
160                 $resultsperpage= $query->param('resultsperpage');
161                 $resultsperpage = 19 if(!defined $resultsperpage);
162         
163                 # builds tag and subfield arrays
164                 my @tags;
165         
166                 my ($results,$total) = authoritysearch($dbh, \@tags,\@and_or,
167                                                                                         \@excluding, \@operator, \@value,
168                                                                                         $startfrom*$resultsperpage, $resultsperpage,$authtypecode);# $orderby);
169         
170                 ($template, $loggedinuser, $cookie)
171                         = get_template_and_user({template_name => "value_builder/unimarc_field_210c.tmpl",
172                                         query => $query,
173                                         type => 'intranet',
174                                         authnotrequired => 0,
175                                         flagsrequired => {editcatalogue => 1},
176                                         debug => 1,
177                                         });
178         
179                 # multi page display gestion
180                 my $displaynext=0;
181                 my $displayprev=$startfrom;
182                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
183                         $displaynext = 1;
184                 }
185         
186                 my @numbers = ();
187         
188                 if ($total>$resultsperpage) {
189                         for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
190                                 if ($i<16) {
191                                         my $highlight=0;
192                                         ($startfrom==($i-1)) && ($highlight=1);
193                                         push @numbers, { number => $i,
194                                                 highlight => $highlight ,
195                                                 startfrom => ($i-1)};
196                                 }
197                         }
198                 }
199         
200                 my $from = $startfrom*$resultsperpage+1;
201                 my $to;
202         
203                 if($total < (($startfrom+1)*$resultsperpage)) {
204                         $to = $total;
205                 } else {
206                         $to = (($startfrom+1)*$resultsperpage);
207                 }
208                 $template->param(result => $results) if $results;
209                 $template->param(index => $query->param('index'));
210                 $template->param(startfrom=> $startfrom,
211                                                                 displaynext=> $displaynext,
212                                                                 displayprev=> $displayprev,
213                                                                 resultsperpage => $resultsperpage,
214                                                                 startfromnext => $startfrom+1,
215                                                                 startfromprev => $startfrom-1,
216                                                                 index => $index,
217                                                                 total=>$total,
218                                                                 from=>$from,
219                                                                 to=>$to,
220                                                                 numbers=>\@numbers,
221                                                                 authtypecode =>$authtypecode,
222                                                                 resultstring =>$value[0],
223                                                                 );
224         } else {
225                 ($template, $loggedinuser, $cookie)
226                         = get_template_and_user({template_name => "value_builder/unimarc_field_210c.tmpl",
227                                         query => $query,
228                                         type => 'intranet',
229                                         authnotrequired => 0,
230                                         flagsrequired => {catalogue => 1},
231                                         debug => 1,
232                                         });
233         
234                 $template->param(index => $index,
235                                                 resultstring => $resultstring
236                                                 );
237         }
238         
239         $template->param(authtypesloop => \@authtypesloop);
240         $template->param(category => $category);
241         
242         # Print the page
243         output_html_with_http_headers $query, $cookie, $template->output;
244 }
245
246 1;