Merge remote-tracking branch 'origin/new/bug_6720'
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use warnings;
21 use strict;
22 use CGI;
23
24 use C4::Auth qw(:DEFAULT check_cookie_auth);
25 use C4::Biblio;
26 use C4::Context;
27 use C4::Dates qw(format_date);
28 use C4::Items;
29 use C4::Koha;
30 use C4::Tags qw(get_tags remove_tag get_tag_rows);
31 use C4::Output;
32
33 my $needed_flags = { tools => 'moderate_tags'
34 };    # FIXME: replace when more specific permission is created.
35
36 my $query        = CGI->new;
37 my $op           = $query->param('op') || '';
38 my $biblionumber = $query->param('biblionumber');
39 my $tag          = $query->param('tag');
40 my $tag_id       = $query->param('tag_id');
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "tags/list.tmpl",
45         query           => $query,
46         type            => "intranet",
47         debug           => 1,
48         authnotrequired => 0,
49         flagsrequired   => $needed_flags,
50     }
51 );
52
53 if ( $op eq "del" ) {
54     remove_tag($tag_id);
55     print $query->redirect("/cgi-bin/koha/tags/list.pl?tag=$tag");
56 }
57 else {
58
59     my $marcflavour = C4::Context->preference('marcflavour');
60     my @results;
61
62     if ($tag) {
63         my $taglist = get_tag_rows( { term => $tag } );
64         for ( @{$taglist} ) {
65             my $dat    = &GetBiblioData( $_->{biblionumber} );
66             my $record = &GetMarcBiblio( $_->{biblionumber} );
67             $dat->{'subtitle'} =
68               GetRecordValue( 'subtitle', $record,
69                 GetFrameworkCode( $_->{biblionumber} ) );
70             my @items = GetItemsInfo( $_->{biblionumber} );
71             $dat->{biblionumber} = $_->{biblionumber};
72             $dat->{tag_id}       = $_->{tag_id};
73             $dat->{items}        = \@items;
74             $dat->{TagLoop}      = get_tags(
75                 {
76                     biblionumber => $_->{biblionumber},
77                     'sort'       => '-weight',
78                     limit        => 10
79                 }
80             );
81             push( @results, $dat );
82         }
83
84         my $resultsarray = \@results;
85
86         $template->param(
87             tag    => $tag,
88             titles => $resultsarray,
89         );
90     }
91 }
92
93 output_html_with_http_headers $query, $cookie, $template->output;