Bug 17047: add a dedicated page for Mana setup
[koha.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(check_cookie_auth), qw(get_template_and_user);
26 use C4::Output qw( output_with_http_headers );
27
28 use CGI;
29 use JSON;
30
31 my $input = new CGI;
32
33 my ( $auth_status, $sessionID ) =
34   check_cookie_auth( $input->cookie('CGISESSID'),
35     { serials => 'create_subscription' } );
36
37 if ( $auth_status ne "ok" ) {
38     exit 0;
39 }
40
41 my $templatename;
42 if ($input->param( "resource" ) eq 'report') {
43     $templatename = "mana/mana-report-search-result.tt";
44 } else {
45     $templatename = "mana/mana-subscription-search-result.tt";
46 }
47
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
49     {
50         template_name   => $templatename,
51         query           => $input,
52         type            => "intranet",
53         authnotrequired => 0,
54
55         # flagsrequired   => { serials => $permission },
56         flagsrequired => { serials => 'create_subscription' },
57         debug         => 1,
58     }
59 );
60
61 my ($identifier, $sub_mana_info);
62 $identifier = $input->param('id');
63 $template->param( lowWarned => 5, warned => 10, highWarned => 20);
64 my $package = "Koha::".ucfirst($input->param( 'resource' ));
65 $sub_mana_info = $package->get_search_info($identifier);
66
67 $sub_mana_info->{ usecomments } = $input->param('usecomments');
68 my $resourcename = $input->param('resource');
69 my $result = Koha::SharedContent::search_entities( $resourcename, $sub_mana_info);
70 my $nbofcomment;
71 foreach my $resource (@{ $result->{data} }){
72     $nbofcomment = 0;
73     foreach my $comment (@{ $resource->{comments} }){
74         $nbofcomment += $comment->{nb};
75     }
76     $resource->{nbofcomment} = $nbofcomment;
77 }
78
79 $template->param( $input->param('resource')."s" => $result->{data} );
80 $template->param( statuscode => $result->{code} );
81 $template->param( msg => $result->{msg} );
82
83 output_with_http_headers $input, $cookie, $template->output, 'json';