Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / tags / list.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 Athens County Public Libraries
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 use Modern::Perl;
21 use CGI qw ( -utf8 );
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Biblio qw( GetBiblioData );
25 use C4::Context;
26 use C4::Items qw( GetItemsInfo );
27 use C4::Tags qw( get_tag_rows get_tags remove_tag );
28 use C4::Output qw( output_html_with_http_headers );
29
30 my $needed_flags = { tools => 'moderate_tags'
31 };    # FIXME: replace when more specific permission is created.
32
33 my $query        = CGI->new;
34 my $op           = $query->param('op') || '';
35 my $biblionumber = $query->param('biblionumber');
36 my $tag          = $query->param('tag');
37 my $tag_id       = $query->param('tag_id');
38
39 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40     {
41         template_name   => "tags/list.tt",
42         query           => $query,
43         type            => "intranet",
44         flagsrequired   => $needed_flags,
45     }
46 );
47
48 if ( $op eq "del" ) {
49     remove_tag($tag_id);
50     print $query->redirect("/cgi-bin/koha/tags/list.pl?tag=$tag");
51 }
52 else {
53
54     my $marcflavour = C4::Context->preference('marcflavour');
55     my @results;
56
57     if ($tag) {
58         my $taglist = get_tag_rows( { term => $tag } );
59         for ( @{$taglist} ) {
60             my $dat    = &GetBiblioData( $_->{biblionumber} );
61             my @items = GetItemsInfo( $_->{biblionumber} );
62             $dat->{biblionumber} = $_->{biblionumber};
63             $dat->{tag_id}       = $_->{tag_id};
64             $dat->{items}        = \@items;
65             $dat->{TagLoop}      = get_tags(
66                 {
67                     biblionumber => $_->{biblionumber},
68                     'sort'       => '-weight',
69                     limit        => 10
70                 }
71             );
72             push( @results, $dat );
73         }
74
75         my $resultsarray = \@results;
76
77         $template->param(
78             tag    => $tag,
79             titles => $resultsarray,
80         );
81     }
82 }
83
84 output_html_with_http_headers $query, $cookie, $template->output;