Bug 36552: (QA follow-up) Add POSIX module
[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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Auth qw( get_template_and_user );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( pagination_bar output_html_with_http_headers );
26 use CGI qw ( -utf8 );
27 use C4::Search;
28 use C4::Koha qw( getnbpages );
29 use C4::AuthoritiesMarc qw( GetAuthority SearchAuthorities );
30
31 ###TODO To rewrite in order to use SearchAuthorities
32
33 sub plugin_javascript {
34 my ($dbh,$record,$tagslib,$field_number) = @_;
35 my $function_name= $field_number;
36 #---- build editors list.
37 #---- the editor list is built from the "EDITORS" thesaurus
38 #---- this thesaurus category must be filled as follow :
39 #---- 200$a for isbn
40 #---- 200$b for editor
41 #---- 200$c (repeated) for collections
42
43
44 my $res  = "
45 <script>
46 function Clic$function_name(event) {
47     event.preventDefault();
48     defaultvalue=escape(document.getElementById(event.data.id).value);
49     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_210c.pl&index=\" + event.data.id, \"unimarc_225a\",'width=500,height=600,toolbar=false,scrollbars=yes');
50 }
51 </script>
52 ";
53 return ($function_name,$res);
54 }
55
56 sub plugin {
57 my ($input) = @_;
58     my $query=CGI->new;
59     my $op = $query->param('op');
60     my $authtypecode = $query->param('authtypecode');
61     my $index = $query->param('index');
62     my $category = $query->param('category');
63     my $resultstring = $query->param('result');
64     my $dbh = C4::Context->dbh;
65
66     my $startfrom = $query->param('startfrom') // 1; # Page number starting at 1
67     my ($template, $loggedinuser, $cookie);
68     my $resultsperpage = $query->param('resultsperpage') // 20; # TODO hardcoded
69     my $offset = ( $startfrom - 1 ) * $resultsperpage;
70
71     if ($op eq "do_search") {
72         my @marclist = $query->multi_param('marclist');
73         my @and_or = $query->multi_param('and_or');
74         my @excluding = $query->multi_param('excluding');
75         my @operator = $query->multi_param('operator');
76         my @value = $query->multi_param('value');
77         my $orderby   = $query->param('orderby');
78
79         # builds tag and subfield arrays
80         my @tags;
81
82         my ($results,$total) = SearchAuthorities( \@tags,\@and_or,
83                                             \@excluding, \@operator, \@value,
84                                             $offset, $resultsperpage,$authtypecode, $orderby);
85
86         # Getting the $b if it exists
87         for (@$results) {
88             my $authority = GetAuthority($_->{authid});
89                 if ($authority->field('200') and $authority->subfield('200','b')) {
90                     $_->{to_report} = $authority->subfield('200','b');
91             }
92         }
93
94         ($template, $loggedinuser, $cookie)
95             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tt",
96                     query => $query,
97                     type => 'intranet',
98                     flagsrequired => {editcatalogue => '*'},
99                     });
100
101         # Results displayed in current page
102         my $from = $offset + 1;
103         my $to = ( $offset + $resultsperpage > $total ) ? $total : $offset + $resultsperpage;
104
105         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";
106
107         $template->param(result => $results) if $results;
108         $template->param('index' => scalar $query->param('index'));
109         $template->param(
110                                 total=>$total,
111                                 from=>$from,
112                                 to=>$to,
113                                 authtypecode =>$authtypecode,
114                                 resultstring =>$value[0],
115                                 pagination_bar => pagination_bar(
116                                     $link,
117                                     getnbpages($total, $resultsperpage),
118                                     $startfrom,
119                                     'startfrom'
120                                 ),
121                                 );
122     } else {
123         ($template, $loggedinuser, $cookie)
124             = get_template_and_user({template_name => "cataloguing/value_builder/unimarc_field_210c.tt",
125                     query => $query,
126                     type => 'intranet',
127                     flagsrequired => {editcatalogue => '*'},
128                     });
129
130         $template->param(index => $index,
131                         resultstring => $resultstring
132                         );
133     }
134
135     $template->param(category => $category);
136
137     # Print the page
138     output_html_with_http_headers $query, $cookie, $template->output;
139 }