(bug #3505) fix 210c and 225a unimarc's plugins
[koha.git] / cataloguing / value_builder / unimarc_field_210c.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
22 use C4::AuthoritiesMarc;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Output;
26 use CGI;
27 use C4::Search;
28 use MARC::Record;
29 use C4::Koha;
30
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
67
68 my $res  = "
69 <script type=\"text/javascript\">
70 function Focus$function_name(index) {
71
72 }
73
74 function Blur$function_name(subfield_managed) {
75     return 1;
76 }
77
78 function Clic$function_name(subfield_managed) {
79     defaultvalue=escape(document.getElementById(\"$field_number\").value);
80     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&index=\"+subfield_managed,\"unimarc 225a\",'width=500,height=600,toolbar=false,scrollbars=yes');
81 }
82 </script>
83 ";
84 return ($function_name,$res);
85 }
86
87 =head1
88
89 plugin : the true value_builded. The screen that is open in the popup window.
90
91 =cut
92
93 sub plugin {
94 my ($input) = @_;
95     my $query=new CGI;
96     my $op = $query->param('op');
97     my $authtypecode = $query->param('authtypecode');
98     my $index = $query->param('index');
99     my $category = $query->param('category');
100     my $resultstring = $query->param('result');
101     my $dbh = C4::Context->dbh;
102     
103     my $startfrom=$query->param('startfrom');
104     $startfrom=0 if(!defined $startfrom);
105     my ($template, $loggedinuser, $cookie);
106     my $resultsperpage;
107     
108     my $authtypes = getauthtypes;
109     my @authtypesloop;
110     foreach my $thisauthtype (keys %$authtypes) {
111         my $selected = 1 if $thisauthtype eq $authtypecode;
112         my %row =(value => $thisauthtype,
113                     selected => $selected,
114                     authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
115                 index => $index,
116                 );
117         push @authtypesloop, \%row;
118     }
119
120     if ($op eq "do_search") {
121         my @marclist = $query->param('marclist');
122         my @and_or = $query->param('and_or');
123         my @excluding = $query->param('excluding');
124         my @operator = $query->param('operator');
125         my @value = $query->param('value');
126         my $orderby   = $query->param('orderby');
127         
128         $resultsperpage= $query->param('resultsperpage');
129         $resultsperpage = 19 if(!defined $resultsperpage);
130     
131         # builds tag and subfield arrays
132         my @tags;
133     
134         my ($results,$total) = SearchAuthorities( \@tags,\@and_or,
135                                             \@excluding, \@operator, \@value,
136                                             $startfrom*$resultsperpage, $resultsperpage,$authtypecode, $orderby);
137                                             
138         ($template, $loggedinuser, $cookie)
139             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
140                     query => $query,
141                     type => 'intranet',
142                     authnotrequired => 0,
143                     flagsrequired => {editcatalogue => 1},
144                     debug => 1,
145                     });
146     
147         # multi page display gestion
148         my $displaynext=0;
149         my $displayprev=$startfrom;
150         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
151             $displaynext = 1;
152         }
153     
154         my @numbers = ();
155     
156         if ($total>$resultsperpage) {
157             for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
158                 if ($i<16) {
159                     my $highlight=0;
160                     ($startfrom==($i-1)) && ($highlight=1);
161                     push @numbers, { number => $i,
162                         highlight => $highlight ,
163                         startfrom => ($i-1)};
164                 }
165             }
166         }
167     
168         my $from = $startfrom*$resultsperpage+1;
169         my $to;
170     
171         if($total < (($startfrom+1)*$resultsperpage)) {
172             $to = $total;
173         } else {
174             $to = (($startfrom+1)*$resultsperpage);
175         }
176         my $link="../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&amp;authtypecode=EDITORS&and_or=$and_or&amp;marclist=$marclist&amp;operator=$operator&amp;orderby=$orderby&amp;excluding=$excluding&amp;".join("&amp;",map {"value=".$_} @value)."&amp;op=do_search&amp;type=intranet&amp;index=$index";
177         warn "$link ,".getnbpages($total, $resultsperpage);
178         $template->param(result => $results) if $results;
179         $template->param('index' => $query->param('index'));
180         $template->param(startfrom=> $startfrom,
181                                 displaynext=> $displaynext,
182                                 displayprev=> $displayprev,
183                                 resultsperpage => $resultsperpage,
184                                 startfromnext => $startfrom+1,
185                                 startfromprev => $startfrom-1,
186                                 total=>$total,
187                                 from=>$from,
188                                 to=>$to,
189                                 numbers=>\@numbers,
190                                 authtypecode =>$authtypecode,
191                                 resultstring =>$value[0],
192                                 pagination_bar => pagination_bar(
193                                     $link,
194                                     getnbpages($total, $resultsperpage),
195                                     $startfrom,
196                                     'startfrom'
197                                 ),
198                                 );
199     } else {
200         ($template, $loggedinuser, $cookie)
201             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
202                     query => $query,
203                     type => 'intranet',
204                     authnotrequired => 0,
205                     flagsrequired => {editcatalogue => 1},
206                     debug => 1,
207                     });
208     
209         $template->param(index => $index,
210                         resultstring => $resultstring
211                         );
212     }
213     
214     $template->param(authtypesloop => \@authtypesloop);
215     $template->param(category => $category);
216     
217     # Print the page
218     output_html_with_http_headers $query, $cookie, $template->output;
219 }
220
221 1;