323d775e12dbbd803ad5c92254f52cf29c93663d
[koha-equinox.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 qw ( -utf8 );
29 use C4::Search;
30 use MARC::Record;
31 use C4::Koha;
32
33 ###TODO To rewrite in order to use SearchAuthorities
34
35 sub plugin_javascript {
36 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
37 my $function_name= $field_number;
38 #---- build editors list.
39 #---- the editor list is built from the "EDITORS" thesaurus
40 #---- this thesaurus category must be filled as follow :
41 #---- 200$a for isbn
42 #---- 200$b for editor
43 #---- 200$c (repeated) for collections
44
45
46 my $res  = "
47 <script type=\"text/javascript\">
48 function Clic$function_name(subfield_managed) {
49     defaultvalue=escape(document.getElementById(\"$field_number\").value);
50     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');
51 }
52 </script>
53 ";
54 return ($function_name,$res);
55 }
56
57 sub plugin {
58 my ($input) = @_;
59     my $query=new CGI;
60     my $op = $query->param('op');
61     my $authtypecode = $query->param('authtypecode');
62     my $index = $query->param('index');
63     my $category = $query->param('category');
64     my $resultstring = $query->param('result');
65     my $dbh = C4::Context->dbh;
66
67     my $startfrom=$query->param('startfrom');
68     $startfrom=0 if(!defined $startfrom);
69     my ($template, $loggedinuser, $cookie);
70     my $resultsperpage;
71
72     my $authtypes = getauthtypes;
73     my @authtypesloop;
74     foreach my $thisauthtype (keys %$authtypes) {
75         my $selected;
76         if ($thisauthtype eq $authtypecode) {
77             $selected=1;
78         }
79         my %row =(value => $thisauthtype,
80                     selected => $selected,
81                     authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
82                 index => $index,
83                 );
84         push @authtypesloop, \%row;
85     }
86
87     if ($op eq "do_search") {
88         my @marclist = $query->param('marclist');
89         my @and_or = $query->param('and_or');
90         my @excluding = $query->param('excluding');
91         my @operator = $query->param('operator');
92         my @value = $query->param('value');
93         my $orderby   = $query->param('orderby');
94
95         $resultsperpage= $query->param('resultsperpage');
96         $resultsperpage = 19 if(!defined $resultsperpage);
97
98         # builds tag and subfield arrays
99         my @tags;
100
101         my ($results,$total) = SearchAuthorities( \@tags,\@and_or,
102                                             \@excluding, \@operator, \@value,
103                                             $startfrom*$resultsperpage, $resultsperpage,$authtypecode, $orderby);
104
105         # Getting the $b if it exists
106         for (@$results) {
107             my $authority = GetAuthority($_->{authid});
108                 if ($authority->field('200') and $authority->subfield('200','b')) {
109                     $_->{to_report} = $authority->subfield('200','b');
110             }
111         }
112
113         ($template, $loggedinuser, $cookie)
114             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tt",
115                     query => $query,
116                     type => 'intranet',
117                     authnotrequired => 0,
118                     flagsrequired => {editcatalogue => '*'},
119                     debug => 1,
120                     });
121
122         # multi page display gestion
123         my $displaynext=0;
124         my $displayprev=$startfrom;
125         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
126             $displaynext = 1;
127         }
128
129         my @numbers = ();
130
131         if ($total>$resultsperpage) {
132             for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
133                 if ($i<16) {
134                     my $highlight=0;
135                     ($startfrom==($i-1)) && ($highlight=1);
136                     push @numbers, { number => $i,
137                         highlight => $highlight ,
138                         startfrom => ($i-1)};
139                 }
140             }
141         }
142
143         my $from = $startfrom*$resultsperpage+1;
144         my $to;
145
146         if($total < (($startfrom+1)*$resultsperpage)) {
147             $to = $total;
148         } else {
149             $to = (($startfrom+1)*$resultsperpage);
150         }
151         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";
152
153         $template->param(result => $results) if $results;
154         $template->param('index' => $query->param('index'));
155         $template->param(startfrom=> $startfrom,
156                                 displaynext=> $displaynext,
157                                 displayprev=> $displayprev,
158                                 resultsperpage => $resultsperpage,
159                                 startfromnext => $startfrom+1,
160                                 startfromprev => $startfrom-1,
161                                 total=>$total,
162                                 from=>$from,
163                                 to=>$to,
164                                 numbers=>\@numbers,
165                                 authtypecode =>$authtypecode,
166                                 resultstring =>$value[0],
167                                 pagination_bar => pagination_bar(
168                                     $link,
169                                     getnbpages($total, $resultsperpage),
170                                     $startfrom,
171                                     'startfrom'
172                                 ),
173                                 );
174     } else {
175         ($template, $loggedinuser, $cookie)
176             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tt",
177                     query => $query,
178                     type => 'intranet',
179                     authnotrequired => 0,
180                     flagsrequired => {editcatalogue => '*'},
181                     debug => 1,
182                     });
183
184         $template->param(index => $index,
185                         resultstring => $resultstring
186                         );
187     }
188
189     $template->param(authtypesloop => \@authtypesloop);
190     $template->param(category => $category);
191
192     # Print the page
193     output_html_with_http_headers $query, $cookie, $template->output;
194 }