Bug 9573: Lost items report - Add items.notforloan as a filter
[koha.git] / Koha / Template / Plugin / AuthorisedValues.pm
1 package Koha::Template::Plugin::AuthorisedValues;
2
3 # Copyright 2012 ByWater Solutions
4 # Copyright 2013-2014 BibLibre
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Template::Plugin;
22 use base qw( Template::Plugin );
23
24 use C4::Koha;
25 use Koha::AuthorisedValues;
26
27 sub GetByCode {
28     my ( $self, $category, $code, $opac ) = @_;
29     my $av = Koha::AuthorisedValues->search({ category => $category, authorised_value => $code });
30     return $av->count
31             ? $opac
32                 ? $av->next->opac_description
33                 : $av->next->lib
34             : $code;
35 }
36
37 sub Get {
38     my ( $self, $category, $selected, $opac ) = @_;
39     return GetAuthorisedValues( $category, $selected, $opac );
40 }
41
42 sub GetAuthValueDropbox {
43     my ( $self, $category ) = @_;
44     my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
45     return Koha::AuthorisedValues->search(
46         {
47             branchcode => $branch_limit,
48             category => $category,
49         },
50         {
51             group_by => 'lib',
52             order_by => [ 'category', 'lib', 'lib_opac' ],
53         }
54     );
55 }
56
57 sub GetCategories {
58     my ( $self, $params ) = @_;
59     my $selected = $params->{selected};
60     my @categories = Koha::AuthorisedValues->new->categories;
61     return [
62         map {
63             {
64                 category => $_,
65                 ( ( $selected and $selected eq $_ ) ? ( selected => 1 ) : () ),
66             }
67         } @categories
68     ];
69 }
70
71 sub GetDescriptionsByKohaField {
72     my ( $self, $params ) = @_;
73     return Koha::AuthorisedValues->get_descriptions_by_koha_field(
74         { kohafield => $params->{kohafield} } );
75 }
76
77 sub GetDescriptionByKohaField {
78     my ( $self, $params ) = @_;
79     my $av = Koha::AuthorisedValues->get_description_by_koha_field(
80         {
81             kohafield        => $params->{kohafield},
82             authorised_value => $params->{authorised_value},
83         }
84     );
85     return %$av
86             ? $params->{opac}
87                 ? $av->{opac_description}
88                 : $av->{lib}
89             : ''; # Maybe we should return $params->{authorised_value}?
90
91 }
92
93 1;
94
95 =head1 NAME
96
97 Koha::Template::Plugin::AuthorisedValues - TT Plugin for authorised values
98
99 =head1 SYNOPSIS
100
101 [% USE AuthorisedValues %]
102
103 [% AuthorisedValues.GetByCode( 'CATEGORY', 'AUTHORISED_VALUE_CODE', 'IS_OPAC' ) %]
104
105 [% AuthorisedValues.GetAuthValueDropbox( $category, $default ) %]
106
107 =head1 ROUTINES
108
109 =head2 GetByCode
110
111 In a template, you can get the description for an authorised value with
112 the following TT code: [% AuthorisedValues.GetByCode( 'CATEGORY', 'AUTHORISED_VALUE_CODE', 'IS_OPAC' ) %]
113
114 =head2 GetAuthValueDropbox
115
116 The parameters are identical to those used by the subroutine C4::Koha::GetAuthValueDropbox
117
118 =head2 GetDescriptionsByKohaField
119
120 The parameters are identical to those used by the subroutine Koha::AuthorisedValues->get_descriptions_by_koha_field
121
122 =head2 GetDescriptionByKohaField
123
124 The parameters are identical to those used by the subroutine Koha::AuthorisedValues->get_description_by_koha_field
125
126 =head1 AUTHOR
127
128 Kyle M Hall <kyle@bywatersolutions.com>
129
130 Jonathan Druart <jonathan.druart@biblibre.com>
131
132 =cut