Merge remote-tracking branch 'origin/master' into new/bug_5327
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use C4::AuthoritiesMarc;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use CGI;
29 use C4::Search;
30 use MARC::Record;
31 use C4::Koha;
32
33 ###TODO To rewrite in order to use SearchAuthorities
34
35 =head1
36
37 plugin_parameters : other parameters added when the plugin is called by the dopop function
38
39 =cut
40 sub plugin_parameters {
41 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
42 return "";
43 }
44
45 =head1
46
47 plugin_javascript : the javascript function called when the user enters the subfield.
48 contain 3 javascript functions :
49 * one called when the field is entered (OnFocus). Named FocusXXX
50 * one called when the field is leaved (onBlur). Named BlurXXX
51 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
52
53 returns :
54 * XXX
55 * a variable containing the 3 scripts.
56 the 3 scripts are inserted after the <input> in the html code
57
58 =cut
59 sub plugin_javascript {
60 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
61 my $function_name= $field_number;
62 #---- build editors list.
63 #---- the editor list is built from the "EDITORS" thesaurus
64 #---- this thesaurus category must be filled as follow :
65 #---- 200$a for isbn
66 #---- 200$b for editor
67 #---- 200$c (repeated) for collections
68
69
70 my $res  = "
71 <script type=\"text/javascript\">
72 function Focus$function_name(index) {
73
74 }
75
76 function Blur$function_name(subfield_managed) {
77     return 1;
78 }
79
80 function Clic$function_name(subfield_managed) {
81     defaultvalue=escape(document.getElementById(\"$field_number\").value);
82     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');
83 }
84 </script>
85 ";
86 return ($function_name,$res);
87 }
88
89 =head1
90
91 plugin : the true value_builded. The screen that is open in the popup window.
92
93 =cut
94
95 sub plugin {
96 my ($input) = @_;
97     my $query=new CGI;
98     my $op = $query->param('op');
99     my $authtypecode = $query->param('authtypecode');
100     my $index = $query->param('index');
101     my $category = $query->param('category');
102     my $resultstring = $query->param('result');
103     my $dbh = C4::Context->dbh;
104
105     my $startfrom=$query->param('startfrom');
106     $startfrom=0 if(!defined $startfrom);
107     my ($template, $loggedinuser, $cookie);
108     my $resultsperpage;
109
110     my $authtypes = getauthtypes;
111     my @authtypesloop;
112     foreach my $thisauthtype (keys %$authtypes) {
113         my $selected;
114         if ($thisauthtype eq $authtypecode) {
115             $selected=1;
116         }
117         my %row =(value => $thisauthtype,
118                     selected => $selected,
119                     authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
120                 index => $index,
121                 );
122         push @authtypesloop, \%row;
123     }
124
125     if ($op eq "do_search") {
126         my @marclist = $query->param('marclist');
127         my @and_or = $query->param('and_or');
128         my @excluding = $query->param('excluding');
129         my @operator = $query->param('operator');
130         my @value = $query->param('value');
131         my $orderby   = $query->param('orderby');
132
133         $resultsperpage= $query->param('resultsperpage');
134         $resultsperpage = 19 if(!defined $resultsperpage);
135
136         # builds tag and subfield arrays
137         my @tags;
138
139         my ($results,$total) = SearchAuthorities( \@tags,\@and_or,
140                                             \@excluding, \@operator, \@value,
141                                             $startfrom*$resultsperpage, $resultsperpage,$authtypecode, $orderby);
142
143         # Getting the $b if it exists
144         for (@$results) {
145             my $authority = GetAuthority($_->{authid});
146                 if ($authority->field('200') and $authority->subfield('200','b')) {
147                     $_->{to_report} = $authority->subfield('200','b');
148             }
149         }
150
151         ($template, $loggedinuser, $cookie)
152             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
153                     query => $query,
154                     type => 'intranet',
155                     authnotrequired => 0,
156                     flagsrequired => {editcatalogue => '*'},
157                     debug => 1,
158                     });
159
160         # multi page display gestion
161         my $displaynext=0;
162         my $displayprev=$startfrom;
163         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
164             $displaynext = 1;
165         }
166
167         my @numbers = ();
168
169         if ($total>$resultsperpage) {
170             for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
171                 if ($i<16) {
172                     my $highlight=0;
173                     ($startfrom==($i-1)) && ($highlight=1);
174                     push @numbers, { number => $i,
175                         highlight => $highlight ,
176                         startfrom => ($i-1)};
177                 }
178             }
179         }
180
181         my $from = $startfrom*$resultsperpage+1;
182         my $to;
183
184         if($total < (($startfrom+1)*$resultsperpage)) {
185             $to = $total;
186         } else {
187             $to = (($startfrom+1)*$resultsperpage);
188         }
189         my $link="../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&amp;authtypecode=EDITORS&amp;".join("&amp;",map {"value=".$_} @value)."&amp;op=do_search&amp;type=intranet&amp;index=$index";
190
191         $template->param(result => $results) if $results;
192         $template->param('index' => $query->param('index'));
193         $template->param(startfrom=> $startfrom,
194                                 displaynext=> $displaynext,
195                                 displayprev=> $displayprev,
196                                 resultsperpage => $resultsperpage,
197                                 startfromnext => $startfrom+1,
198                                 startfromprev => $startfrom-1,
199                                 total=>$total,
200                                 from=>$from,
201                                 to=>$to,
202                                 numbers=>\@numbers,
203                                 authtypecode =>$authtypecode,
204                                 resultstring =>$value[0],
205                                 pagination_bar => pagination_bar(
206                                     $link,
207                                     getnbpages($total, $resultsperpage),
208                                     $startfrom,
209                                     'startfrom'
210                                 ),
211                                 );
212     } else {
213         ($template, $loggedinuser, $cookie)
214             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tmpl",
215                     query => $query,
216                     type => 'intranet',
217                     authnotrequired => 0,
218                     flagsrequired => {editcatalogue => '*'},
219                     debug => 1,
220                     });
221
222         $template->param(index => $index,
223                         resultstring => $resultstring
224                         );
225     }
226
227     $template->param(authtypesloop => \@authtypesloop);
228     $template->param(category => $category);
229
230     # Print the page
231     output_html_with_http_headers $query, $cookie, $template->output;
232 }
233
234 1;