Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / Koha / AuthorisedValue.pm
1 package Koha::AuthorisedValue;
2
3 # Copyright ByWater Solutions 2014
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 3 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 Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::AuthorisedValue - Koha Authorised value Object class
31
32 =head1 API
33
34 =head2 Class Methods
35
36 =cut
37
38 =head3 branch_limitations
39
40 my $limitations = $av->branch_limitations();
41
42 $av->branch_limitations( \@branchcodes );
43
44 =cut
45
46 sub branch_limitations {
47     my ( $self, $branchcodes ) = @_;
48
49     if ($branchcodes) {
50         return $self->replace_branch_limitations($branchcodes);
51     }
52     else {
53         return $self->get_branch_limitations();
54     }
55
56 }
57
58 =head3 get_branch_limitations
59
60 my $limitations = $av->get_branch_limitations();
61
62 =cut
63
64 sub get_branch_limitations {
65     my ($self) = @_;
66
67     my @branchcodes =
68       $self->_avb_resultset->search( { av_id => $self->id() } )
69       ->get_column('branchcode')->all();
70
71     return \@branchcodes;
72 }
73
74 =head3 add_branch_limitation
75
76 $av->add_branch_limitation( $branchcode );
77
78 =cut
79
80 sub add_branch_limitation {
81     my ( $self, $branchcode ) = @_;
82
83     croak("No branchcode passed in!") unless $branchcode;
84
85     my $limitation = $self->_avb_resultset->update_or_create(
86         { av_id => $self->id(), branchcode => $branchcode } );
87
88     return $limitation ? 1 : undef;
89 }
90
91 =head3 del_branch_limitation
92
93 $av->del_branch_limitation( $branchcode );
94
95 =cut
96
97 sub del_branch_limitation {
98     my ( $self, $branchcode ) = @_;
99
100     croak("No branchcode passed in!") unless $branchcode;
101
102     my $limitation =
103       $self->_avb_resultset->find(
104         { av_id => $self->id(), branchcode => $branchcode } );
105
106     unless ($limitation) {
107         my $id = $self->id();
108         carp(
109 "No branch limit for branch $branchcode found for av_id $id to delete!"
110         );
111         return;
112     }
113
114     return $limitation->delete();
115 }
116
117 =head3 replace_branch_limitations
118
119 $av->replace_branch_limitations( \@branchcodes );
120
121 =cut
122
123 sub replace_branch_limitations {
124     my ( $self, $branchcodes ) = @_;
125
126     $self->_avb_resultset->search( { av_id => $self->id() } )->delete();
127
128     my @return_values =
129       map { $self->add_branch_limitation($_) } @$branchcodes;
130
131     return \@return_values;
132 }
133
134 =head3 opac_description
135
136 my $description = $av->opac_description();
137
138 =cut
139
140 sub opac_description {
141     my ( $self, $value ) = @_;
142
143     return $self->lib_opac() || $self->lib();
144 }
145
146 =head3 _avb_resultset
147
148 Returns the internal resultset or creates it if undefined
149
150 =cut
151
152 sub _avb_resultset {
153     my ($self) = @_;
154
155     $self->{_avb_resultset} ||=
156       Koha::Database->new()->schema()->resultset('AuthorisedValuesBranch');
157
158     $self->{_avb_resultset};
159 }
160
161 =head3 type
162
163 =cut
164
165 sub _type {
166     return 'AuthorisedValue';
167 }
168
169 =head1 AUTHOR
170
171 Kyle M Hall <kyle@bywatersolutions.com>
172
173 =cut
174
175 1;