Bug 33146: Unit tests
[koha-ffzg.git] / svc / mana / search
1 #!/usr/bin/perl
2
3 # Copyright 2016 BibLibre Morgane Alonso
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 #
20
21 use Modern::Perl;
22
23 use Koha::SharedContent;
24 use Koha::Subscription;
25 use C4::Auth qw(get_template_and_user);
26 use C4::Output qw( output_html_with_http_headers );
27
28 use CGI;
29
30 my $input = CGI->new;
31
32 my $templatename;
33 if ($input->param( "resource" ) eq 'report') {
34     $templatename = "mana/mana-report-search-result.tt";
35 } else {
36     $templatename = "mana/mana-subscription-search-result.tt";
37 }
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => $templatename,
42         query           => $input,
43         type            => "intranet",
44
45         # flagsrequired   => { serials => $permission },
46         flagsrequired => { serials => 'create_subscription' },
47         debug         => 1,
48     }
49 );
50
51 my ($identifier, $sub_mana_info);
52 $identifier = $input->param('id');
53 $template->param( lowWarned => 5, warned => 10, highWarned => 20);
54 my $package = "Koha::".ucfirst($input->param( 'resource' ));
55 $sub_mana_info = $package->get_search_info($identifier);
56
57 $sub_mana_info->{ usecomments } = $input->param('usecomments');
58 my $resourcename = $input->param('resource');
59 my $result = Koha::SharedContent::search_entities( $resourcename, $sub_mana_info);
60 my $nbofcomment;
61 foreach my $resource (@{ $result->{data} }){
62     $nbofcomment = 0;
63     foreach my $comment (@{ $resource->{comments} }){
64         $nbofcomment += $comment->{nb};
65     }
66     $resource->{nbofcomment} = $nbofcomment;
67 }
68
69 $template->param( $input->param('resource')."s" => $result->{data} );
70 $template->param( statuscode => $result->{code} );
71 $template->param( msg => $result->{msg} );
72
73 output_html_with_http_headers $input, $cookie, $template->output;